mirror of
https://github.com/folke/persistence.nvim.git
synced 2025-07-16 12:24:41 +02:00
feat(persistence): add pre-
and post-
load hooks (#24)
* Add optional hook typings to config object * Add optional hooks conditional calls * Add hooks description to main readme
This commit is contained in:
parent
4982499c16
commit
3d443bd0a7
3 changed files with 18 additions and 0 deletions
|
@ -37,7 +37,10 @@ Persistence comes with the following defaults:
|
|||
dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved
|
||||
options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving
|
||||
pre_save = nil, -- a function to call before saving the session
|
||||
post_save = nil, -- a function to call after saving the session
|
||||
save_empty = false, -- don't save if there are no open file buffers
|
||||
pre_load = nil, -- a function to call before loading the session
|
||||
post_load = nil, -- a function to call after loading the session
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -2,6 +2,9 @@ local M = {}
|
|||
|
||||
---@class PersistenceOptions
|
||||
---@field pre_save? fun()
|
||||
---@field post_save? fun()
|
||||
---@field pre_load? fun()
|
||||
---@field post_load? fun()
|
||||
local defaults = {
|
||||
dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved
|
||||
options = { "buffers", "curdir", "tabpages", "winsize", "skiprtp" }, -- sessionoptions used for saving
|
||||
|
|
|
@ -56,6 +56,10 @@ function M.start()
|
|||
end
|
||||
|
||||
M.save()
|
||||
|
||||
if type(Config.options.post_save) == "function" then
|
||||
Config.options.post_save()
|
||||
end
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
@ -76,7 +80,15 @@ function M.load(opt)
|
|||
opt = opt or {}
|
||||
local sfile = opt.last and M.get_last() or M.get_current()
|
||||
if sfile and vim.fn.filereadable(sfile) ~= 0 then
|
||||
if type(Config.options.pre_load) == "function" then
|
||||
Config.options.pre_load()
|
||||
end
|
||||
|
||||
vim.cmd("silent! source " .. e(sfile))
|
||||
|
||||
if type(Config.options.post_load) == "function" then
|
||||
Config.options.post_load()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue