diff --git a/README.md b/README.md index ac19688..6034425 100644 --- a/README.md +++ b/README.md @@ -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", "qr", [[lua require("persisted").load()]]) -- restore the last session @@ -77,4 +77,10 @@ vim.api.nvim_set_keymap("n", "qs", [[lua require("persisted").start -- stop persisted => session won't be saved on exit vim.api.nvim_set_keymap("n", "qx", [[lua require("persisted").stop()]]) + +-- toggle persisted => toggle a session +vim.api.nvim_set_keymap("n", "qt", [[lua require("persisted").toggle()]]) ``` + +### 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). diff --git a/lua/persisted/init.lua b/lua/persisted/init.lua index f0fc78b..7efcbea 100644 --- a/lua/persisted/init.lua +++ b/lua/persisted/init.lua @@ -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