feat: add toggle function
parent
cbb601eaec
commit
8c3bc1791d
|
|
@ -66,7 +66,7 @@ Persisted comes with the following defaults:
|
|||
|
||||
Some example keybindings are contained below:
|
||||
```lua
|
||||
-- restore the session for the current directory
|
||||
-- restore the session for the current directory and current branch (if `git_use_branch` is enabled)
|
||||
vim.api.nvim_set_keymap("n", "<leader>qr", [[<cmd>lua require("persisted").load()<cr>]])
|
||||
|
||||
-- restore the last session
|
||||
|
|
@ -77,4 +77,10 @@ vim.api.nvim_set_keymap("n", "<leader>qs", [[<cmd>lua require("persisted").start
|
|||
|
||||
-- stop persisted => session won't be saved on exit
|
||||
vim.api.nvim_set_keymap("n", "<leader>qx", [[<cmd>lua require("persisted").stop()<cr>]])
|
||||
|
||||
-- toggle persisted => toggle a session
|
||||
vim.api.nvim_set_keymap("n", "<leader>qt", [[<cmd>lua require("persisted").toggle()<cr>]])
|
||||
```
|
||||
|
||||
### Helpers
|
||||
**Persisted** sets a global variable, `vim.g.persisting`, which is set to `true` when the plugin is enabled. The author uses this to display an icon in their [statusline](https://github.com/olimorris/dotfiles/blob/0cdaee183c64f872778952f90f62b9366851101c/.config/nvim/lua/Oli/plugins/statusline.lua#L257).
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ function M.start()
|
|||
autocmd VimLeavePre * lua require("persisted").save()
|
||||
augroup end
|
||||
]])
|
||||
vim.g.using_persisted = true
|
||||
vim.g.persisting = true
|
||||
end
|
||||
|
||||
function M.stop()
|
||||
|
|
@ -62,7 +62,7 @@ function M.stop()
|
|||
autocmd! Persisted
|
||||
augroup! Persisted
|
||||
]])
|
||||
vim.g.using_persisted = false
|
||||
vim.g.persisting = false
|
||||
end
|
||||
|
||||
function M.save()
|
||||
|
|
@ -70,7 +70,7 @@ function M.save()
|
|||
vim.o.sessionoptions = table.concat(Config.options.options, ",")
|
||||
vim.cmd("mks! " .. e(M.get_current()))
|
||||
vim.o.sessionoptions = tmp
|
||||
vim.g.using_persisted = true
|
||||
vim.g.persisting = true
|
||||
end
|
||||
|
||||
function M.load(opt)
|
||||
|
|
@ -78,7 +78,7 @@ function M.load(opt)
|
|||
local sfile = opt.last and M.get_last() or M.get_current()
|
||||
if sfile and vim.fn.filereadable(sfile) ~= 0 then
|
||||
vim.cmd("source " .. e(sfile))
|
||||
vim.g.using_persisted = true
|
||||
vim.g.persisting = true
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -86,4 +86,14 @@ function M.list()
|
|||
return vim.fn.glob(Config.options.dir .. "*.vim", true, true)
|
||||
end
|
||||
|
||||
function M.toggle()
|
||||
if vim.g.persisting == nil then
|
||||
return M.load()
|
||||
end
|
||||
if vim.g.persisting then
|
||||
return M.stop()
|
||||
end
|
||||
return M.start()
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
|
|||
Loading…
Reference in New Issue