Skip to content

Commit 9e6b70f

Browse files
feat: autosave in background
1 parent adaa2ba commit 9e6b70f

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ This is a work in progress
66

77
TODO:
88

9-
- periodic saving
109
- tab-scoped sessions
1110
- documentation
1211
- make filepaths relative so sessions are portable

lua/resession/config.lua

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

33
local default_config = {
4+
autosave = {
5+
enabled = false,
6+
-- How often to save (in seconds)
7+
interval = 60,
8+
-- Notify when autosaved
9+
notify = true,
10+
},
411
buffers = {
512
-- Only save buffers with these buftypes
613
buftypes = { "", "acwrite", "help" },
@@ -64,11 +71,30 @@ local default_config = {
6471
extensions = {},
6572
}
6673

74+
local autosave_timer
6775
M.setup = function(config)
76+
local resession = require("resession")
6877
local newconf = vim.tbl_deep_extend("force", default_config, config)
6978
for k, v in pairs(newconf) do
7079
M[k] = v
7180
end
81+
if autosave_timer then
82+
autosave_timer:close()
83+
autosave_timer = nil
84+
end
85+
if M.autosave.enabled then
86+
autosave_timer = vim.loop.new_timer()
87+
timer = vim.loop.new_timer()
88+
timer:start(
89+
M.autosave.interval * 1000,
90+
M.autosave.interval * 1000,
91+
vim.schedule_wrap(function()
92+
if resession.get_current() then
93+
resession.save(nil, { notify = M.autosave.notify })
94+
end
95+
end)
96+
)
97+
end
7298
end
7399

74100
---@return string

lua/resession/init.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ end
191191

192192
local function close_everything()
193193
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
194-
vim.api.nvim_buf_delete(bufnr, { force = true })
194+
if vim.bo[bufnr].buflisted then
195+
vim.api.nvim_buf_delete(bufnr, { force = true })
196+
end
195197
end
196198
vim.cmd("silent! tabonly")
197199
vim.cmd("silent! only")
@@ -283,7 +285,9 @@ M.load = function(name, opts)
283285
for k, v in pairs(data.global.options) do
284286
vim.o[k] = v
285287
end
286-
if not opts.detach then
288+
if opts.detach then
289+
current_session = nil
290+
else
287291
current_session = name
288292
end
289293
end

0 commit comments

Comments
 (0)