feat(persistence): pre_save option to call before saving (#22)

* feat(persistence): `pre_save` option to call before saving

This commit adds the `pre_save` option, which is a `fun()|nil` that will
be called (if present) before saving the current session. Use cases
include filtering buffers, executing `User` autocmds, etc.

* docs: `pre_save` option
This commit is contained in:
Iron-E 2023-02-28 09:05:58 +00:00 committed by GitHub
parent d8a3eda0e1
commit f4bb0c5641
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 8 additions and 0 deletions

View File

@ -52,6 +52,7 @@ Persistence comes with the following defaults:
{ {
dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved
options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving
pre_save = nil, -- a function to call before saving the session
} }
``` ```

View File

@ -71,6 +71,7 @@ Persistence comes with the following defaults:
{ {
dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved
options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving
pre_save = nil, -- a function to call before saving the session
} }
< <

View File

@ -71,6 +71,7 @@ Persistence comes with the following defaults:
{ {
dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved
options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving
pre_save = nil, -- a function to call before saving the session
} }
< <

View File

@ -1,6 +1,7 @@
local M = {} local M = {}
---@class PersistenceOptions ---@class PersistenceOptions
---@field pre_save? fun()
local defaults = { local defaults = {
dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved
options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving

View File

@ -30,6 +30,10 @@ function M.start()
vim.api.nvim_create_autocmd("VimLeavePre", { vim.api.nvim_create_autocmd("VimLeavePre", {
group = vim.api.nvim_create_augroup("persistence", { clear = true }), group = vim.api.nvim_create_augroup("persistence", { clear = true }),
callback = function() callback = function()
if Config.options.pre_save then
Config.options.pre_save()
end
M.save() M.save()
end, end,
}) })