From c1c4bbff8a4e9b9f66812b2daa9d3338916e8da2 Mon Sep 17 00:00:00 2001 From: olimorris Date: Wed, 26 Jul 2023 16:17:42 +0100 Subject: [PATCH] feat: add global variable for last loaded session --- README.md | 7 +++++-- lua/persisted/init.lua | 15 ++++++++++++++- lua/persisted/utils.lua | 1 + 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 58c5e9a..e3d5854 100644 --- a/README.md +++ b/README.md @@ -123,9 +123,12 @@ Once opened, the available keymaps are: - `` - Source the session file - `` - 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 diff --git a/lua/persisted/init.lua b/lua/persisted/init.lua index 6d18c6e..4eec5cd 100644 --- a/lua/persisted/init.lua +++ b/lua/persisted/init.lua @@ -140,6 +140,12 @@ end ---@return nil function M.save(opt) 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 if (vim.g.persisting == false or vim.g.persisting == nil) and not opt.override then @@ -156,8 +162,15 @@ function M.save(opt) return 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.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.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePost" }) end diff --git a/lua/persisted/utils.lua b/lua/persisted/utils.lua index d0df86b..a1f2a97 100644 --- a/lua/persisted/utils.lua +++ b/lua/persisted/utils.lua @@ -71,6 +71,7 @@ function M.load_session(session, silent) return echoerr("Error loading the session! ", result) end + vim.g.persisted_loaded_session = session vim.api.nvim_exec_autocmds("User", { pattern = "PersistedLoadPost", data = session }) end