Skip to content

Commit

Permalink
feat: support autosave on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Sep 9, 2022
1 parent 71f45fd commit a383c7f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lua/resession/config.lua
Original file line number Diff line number Diff line change
@@ -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" },
Expand All @@ -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
17 changes: 13 additions & 4 deletions lua/resession/init.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a383c7f

Please sign in to comment.