Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Enable autosave.cwd even if a session is already loaded #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lua/possession/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ local function defaults()
tmp_name = 'tmp', -- or fun(): string
on_load = true,
on_quit = true,
-- TODO: actually use this option in implementation
all_matching = true,

},
autoload = false, -- or 'last' or 'auto_cwd' or 'last_cwd' or fun(): string
commands = {
Expand Down
44 changes: 22 additions & 22 deletions lua/possession/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,31 +157,29 @@ local function autosave_skip()
return not unscratch_buffers or not next(unscratch_buffers)
end

---@return { name: string, variant: 'current'|'cwd'|'tmp' }?
---@return { name: string, variant: 'current'|'cwd'|'tmp' }[]
function M.autosave_info()
if state.session_name then
if not utils.as_function(config.autosave.current)(state.session_name) then
return
end
return { name = state.session_name, variant = 'current' }
elseif utils.as_function(config.autosave.cwd)() then
if autosave_skip() then
return
local infos = {}
if state.session_name and utils.as_function(config.autosave.current)(state.session_name) then
table.insert(infos, { name = state.session_name, variant = 'current' })
end
if not autosave_skip() then
if utils.as_function(config.autosave.cwd)() then
table.insert(infos, { name = paths.cwd_session_name(), variant = 'cwd' })
end
return { name = paths.cwd_session_name(), variant = 'cwd' }
elseif utils.as_function(config.autosave.tmp)() then
if autosave_skip() then
return
if utils.as_function(config.autosave.tmp)() then
table.insert(infos, { name = utils.as_function(config.autosave.tmp_name)(), variant = 'tmp' })
end
return { name = utils.as_function(config.autosave.tmp_name)(), variant = 'tmp' }
end
return infos
end

function M.autosave(autosave_info)
local info = autosave_info or M.autosave_info()
if info then
utils.debug('Auto-saving %s session "%s"', info.variant, info.name)
M.save(info.name, { no_confirm = true })
function M.autosave(autosave_info, skip)
for _, info in ipairs(autosave_info or M.autosave_info()) do
if not (skip and skip(info)) then
utils.debug('Auto-saving %s session "%s"', info.variant, info.name)
M.save(info.name, { no_confirm = true })
end
end
end

Expand Down Expand Up @@ -240,9 +238,11 @@ function M.load(name_or_data, opts)

-- Autosave if not loading the auto-saved session itself
if not opts.skip_autosave then
local autosave_info = M.autosave_info()
if config.autosave.on_load and (autosave_info and session_data.name ~= autosave_info.name) then
M.autosave(autosave_info)
if config.autosave.on_load then
M.autosave(nil, function(info)
local skip = session_data.name == info.name
return skip
end)
end
end

Expand Down
Loading