From df435cee434eb1e71fa797f6c51f7a0d567892a6 Mon Sep 17 00:00:00 2001 From: olimorris Date: Fri, 7 Oct 2022 12:46:58 +0100 Subject: [PATCH] feat: :sparkles: #31 should_autosave callback --- README.md | 16 ++++++++++++++++ lua/persisted/config.lua | 1 + lua/persisted/init.lua | 9 ++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6e0f316..2976ff8 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,7 @@ require("persisted").setup({ use_git_branch = false, -- create session files based on the branch of the git enabled repository branch_separator = "_", -- string used to separate session directory name from branch name autosave = true, -- automatically save session files when exiting Neovim + should_autosave = nil, -- function to determine if a session should be autosaved autoload = false, -- automatically load the session for the cwd on Neovim startup on_autoload_no_session = nil, -- function to run when `autoload = true` but there is no session to load allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from @@ -197,6 +198,21 @@ require("persisted").setup({ Autosaving can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`. +There may be occasions when you do not wish to autosave; perhaps when a dashboard or terminal are open. To control this, +a callback function, `should_autosave`, may be specified. This function should return a boolean value. + +```lua +require("persisted").setup({ + should_autosave = function() + -- do not autosave if the alpha dashboard is the current filetype + if vim.bo.filetype == "alpha" then + return false + end + return true + end, +}) +``` + ### Autoloading The plugin can be enabled to automatically load sessions when Neovim is started. Whilst off by default, this can be turned on by: diff --git a/lua/persisted/config.lua b/lua/persisted/config.lua index 4d73b71..c751ed7 100644 --- a/lua/persisted/config.lua +++ b/lua/persisted/config.lua @@ -8,6 +8,7 @@ local defaults = { use_git_branch = false, -- create session files based on the branch of the git enabled repository branch_separator = "_", -- string used to separate session directory name from branch name autosave = true, -- automatically save session files when exiting Neovim + should_autosave = nil, -- function to determine if a session should be autosaved autoload = false, -- automatically load the session for the cwd on Neovim startup allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from ignored_dirs = nil, -- table of dirs that are ignored for auto-saving and auto-loading diff --git a/lua/persisted/init.lua b/lua/persisted/init.lua index 45eb0df..501c0ef 100644 --- a/lua/persisted/init.lua +++ b/lua/persisted/init.lua @@ -105,7 +105,7 @@ function M.load(opt) if session then if vim.fn.filereadable(session) ~= 0 then utils.load_session(session, config.options.before_source, config.options.after_source, config.options.silent) - elseif type(config.options.on_autoload_no_session) == 'function' then + elseif type(config.options.on_autoload_no_session) == "function" then config.options.on_autoload_no_session() end end @@ -149,6 +149,13 @@ function M.save() config.options.before_save() end + if + (config.options.autosave and type(config.options.should_autosave) == "function") + and not config.options.should_autosave() + then + return + end + vim.cmd("mks! " .. e(get_current())) vim.g.persisting = true