feat: add global variable for last loaded session

main
olimorris 2023-07-26 16:17:42 +01:00
parent 06946ed12d
commit c1c4bbff8a
3 changed files with 20 additions and 3 deletions

View File

@ -123,9 +123,12 @@ Once opened, the available keymaps are:
- `<CR>` - Source the session file - `<CR>` - Source the session file
- `<C-d>` - Delete the session file - `<C-d>` - Delete the session file
**Statuslines** **Global variables**
The plugin sets a global variable, `vim.g.persisting`, which is set to `true` when a session is started and `false` when it is stopped. Also, the plugin offers a `PersistedStateChange` event that can be hooked into via an autocmd (see the [events/callbacks](#events--callbacks) section). The plugin sets global variables which can be utilised in your configuration:
- `vim.g.persisting` - This is set to `true` when a session is started and `false` when a session is stopped
- `vim.g.persisted_loaded_session` - The file path to the currently loaded session
## :wrench: Configuration ## :wrench: Configuration

View File

@ -140,6 +140,12 @@ end
---@return nil ---@return nil
function M.save(opt) function M.save(opt)
opt = opt or {} opt = opt or {}
local session = opt.session or (vim.g.persisted_branch_session or vim.g.persisting_session or get_current())
if opt.session then
write(session)
return
end
-- Do not save the session if the user has manually stopped it, but if there's an override, then do it -- Do not save the session if the user has manually stopped it, but if there's an override, then do it
if (vim.g.persisting == false or vim.g.persisting == nil) and not opt.override then if (vim.g.persisting == false or vim.g.persisting == nil) and not opt.override then
@ -156,8 +162,15 @@ function M.save(opt)
return return
end end
write(session)
end
---Write the session to disk
---@param session string
---@return nil
function write(session)
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePre" }) vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePre" })
vim.cmd("mks! " .. e(opt.session or vim.g.persisted_branch_session or vim.g.persisting_session or get_current())) vim.cmd("mks! " .. e(session))
vim.g.persisting = true vim.g.persisting = true
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePost" }) vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePost" })
end end

View File

@ -71,6 +71,7 @@ function M.load_session(session, silent)
return echoerr("Error loading the session! ", result) return echoerr("Error loading the session! ", result)
end end
vim.g.persisted_loaded_session = session
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedLoadPost", data = session }) vim.api.nvim_exec_autocmds("User", { pattern = "PersistedLoadPost", data = session })
end end