diff --git a/README.md b/README.md index 35d4e94..b5a8c0a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ A replacement for `:mksession` with a better API This is a work in progress TODO: -- autosave on quit - periodic saving - tab-scoped sessions - hooks for plugins diff --git a/lua/resession/config.lua b/lua/resession/config.lua index d94e13e..f58c1d0 100644 --- a/lua/resession/config.lua +++ b/lua/resession/config.lua @@ -1,6 +1,9 @@ local M = {} local default_config = { + -- Set this to save a session automatically when quitting vim + -- Can be a string or a function that returns a string + autosave_name = false, buffers = { buftypes = { "", "acwrite", "help" }, options = { "buflisted" }, @@ -9,10 +12,25 @@ local default_config = { } M.setup = function(config) + local resession = require("resession") local newconf = vim.tbl_deep_extend("force", default_config, config) for k, v in pairs(newconf) do M[k] = v end + + local autosave_group = vim.api.nvim_create_augroup("ResessionAutosave", { clear = true }) + if newconf.autosave_name then + vim.api.nvim_create_autocmd("VimLeave", { + group = autosave_group, + callback = function() + local name = newconf.autosave_name + if type(name) == "function" then + name = name() + end + resession.save(name) + end, + }) + end end return M diff --git a/lua/resession/init.lua b/lua/resession/init.lua index 2bfbd60..1429216 100644 --- a/lua/resession/init.lua +++ b/lua/resession/init.lua @@ -1,8 +1,20 @@ local M = {} local pending_config + +local function do_setup() + if pending_config then + require("resession.config").setup(pending_config) + pending_config = nil + end +end + M.setup = function(config) pending_config = config or {} + -- We have to complete the setup if we're autosaving + if pending_config.autosave_name then + do_setup() + end end ---@param name string @@ -164,10 +176,7 @@ end for k, v in pairs(M) do if type(v) == "function" and k ~= "setup" then M[k] = function(...) - if pending_config then - require("resession.config").setup(pending_config) - pending_config = nil - end + do_setup() return v(...) end end