Skip to content

Commit a383c7f

Browse files
committed
feat: support autosave on exit
1 parent 71f45fd commit a383c7f

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ A replacement for `:mksession` with a better API
55
This is a work in progress
66

77
TODO:
8-
- autosave on quit
98
- periodic saving
109
- tab-scoped sessions
1110
- hooks for plugins

lua/resession/config.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
local M = {}
22

33
local default_config = {
4+
-- Set this to save a session automatically when quitting vim
5+
-- Can be a string or a function that returns a string
6+
autosave_name = false,
47
buffers = {
58
buftypes = { "", "acwrite", "help" },
69
options = { "buflisted" },
@@ -9,10 +12,25 @@ local default_config = {
912
}
1013

1114
M.setup = function(config)
15+
local resession = require("resession")
1216
local newconf = vim.tbl_deep_extend("force", default_config, config)
1317
for k, v in pairs(newconf) do
1418
M[k] = v
1519
end
20+
21+
local autosave_group = vim.api.nvim_create_augroup("ResessionAutosave", { clear = true })
22+
if newconf.autosave_name then
23+
vim.api.nvim_create_autocmd("VimLeave", {
24+
group = autosave_group,
25+
callback = function()
26+
local name = newconf.autosave_name
27+
if type(name) == "function" then
28+
name = name()
29+
end
30+
resession.save(name)
31+
end,
32+
})
33+
end
1634
end
1735

1836
return M

lua/resession/init.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
local M = {}
22

33
local pending_config
4+
5+
local function do_setup()
6+
if pending_config then
7+
require("resession.config").setup(pending_config)
8+
pending_config = nil
9+
end
10+
end
11+
412
M.setup = function(config)
513
pending_config = config or {}
14+
-- We have to complete the setup if we're autosaving
15+
if pending_config.autosave_name then
16+
do_setup()
17+
end
618
end
719

820
---@param name string
@@ -164,10 +176,7 @@ end
164176
for k, v in pairs(M) do
165177
if type(v) == "function" and k ~= "setup" then
166178
M[k] = function(...)
167-
if pending_config then
168-
require("resession.config").setup(pending_config)
169-
pending_config = nil
170-
end
179+
do_setup()
171180
return v(...)
172181
end
173182
end

0 commit comments

Comments
 (0)