feat: add toggle function

main
olimorris 2022-01-21 16:38:00 +00:00
parent cbb601eaec
commit 8c3bc1791d
2 changed files with 21 additions and 5 deletions

View File

@ -66,7 +66,7 @@ Persisted comes with the following defaults:
Some example keybindings are contained below: Some example keybindings are contained below:
```lua ```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>]]) vim.api.nvim_set_keymap("n", "<leader>qr", [[<cmd>lua require("persisted").load()<cr>]])
-- restore the last session -- 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 -- stop persisted => session won't be saved on exit
vim.api.nvim_set_keymap("n", "<leader>qx", [[<cmd>lua require("persisted").stop()<cr>]]) 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).

View File

@ -54,7 +54,7 @@ function M.start()
autocmd VimLeavePre * lua require("persisted").save() autocmd VimLeavePre * lua require("persisted").save()
augroup end augroup end
]]) ]])
vim.g.using_persisted = true vim.g.persisting = true
end end
function M.stop() function M.stop()
@ -62,7 +62,7 @@ function M.stop()
autocmd! Persisted autocmd! Persisted
augroup! Persisted augroup! Persisted
]]) ]])
vim.g.using_persisted = false vim.g.persisting = false
end end
function M.save() function M.save()
@ -70,7 +70,7 @@ function M.save()
vim.o.sessionoptions = table.concat(Config.options.options, ",") vim.o.sessionoptions = table.concat(Config.options.options, ",")
vim.cmd("mks! " .. e(M.get_current())) vim.cmd("mks! " .. e(M.get_current()))
vim.o.sessionoptions = tmp vim.o.sessionoptions = tmp
vim.g.using_persisted = true vim.g.persisting = true
end end
function M.load(opt) function M.load(opt)
@ -78,7 +78,7 @@ function M.load(opt)
local sfile = opt.last and M.get_last() or M.get_current() local sfile = opt.last and M.get_last() or M.get_current()
if sfile and vim.fn.filereadable(sfile) ~= 0 then if sfile and vim.fn.filereadable(sfile) ~= 0 then
vim.cmd("source " .. e(sfile)) vim.cmd("source " .. e(sfile))
vim.g.using_persisted = true vim.g.persisting = true
end end
end end
@ -86,4 +86,14 @@ function M.list()
return vim.fn.glob(Config.options.dir .. "*.vim", true, true) return vim.fn.glob(Config.options.dir .. "*.vim", true, true)
end 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 return M