From b62870bd427104116df808b2e9387edb93e6ee2c Mon Sep 17 00:00:00 2001 From: nuchs Date: Tue, 4 Jun 2024 07:46:58 +0100 Subject: [PATCH 1/5] Allow multiple autoload options to be specified --- lua/session_manager/init.lua | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lua/session_manager/init.lua b/lua/session_manager/init.lua index 481b01b..2554797 100644 --- a/lua/session_manager/init.lua +++ b/lua/session_manager/init.lua @@ -68,13 +68,27 @@ function session_manager.save_current_session() end end +local get_autoload_mode = function() + if config.autoload_mode == nil or vim.fn.argc() > 0 or vim.g.started_with_stdin then + return { AutoloadMode.Disabled } + end + if type(config.autoload_mode) ~= 'table' then + return { config.autoload_mode } + end + return config.autoload_mode +end + +local autoloaders = { + [AutoloadMode.Disabled] = function() return true end, + [AutoloadMode.CurrentDir] = session_manager.load_current_dir_session, + [AutoloadMode.LastSession] = session_manager.load_last_session, +} + --- Loads a session based on settings. Executed after starting the editor. function session_manager.autoload_session() - if config.autoload_mode ~= AutoloadMode.Disabled and vim.fn.argc() == 0 and not vim.g.started_with_stdin then - if config.autoload_mode == AutoloadMode.CurrentDir then - session_manager.load_current_dir_session() - elseif config.autoload_mode == AutoloadMode.LastSession then - session_manager.load_last_session() + for _, mode in ipairs(get_autoload_mode()) do + if autoloaders[mode]() then + return end end end From 23110764fe01f5156212a2ae8356f98bec176264 Mon Sep 17 00:00:00 2001 From: nuchs Date: Tue, 4 Jun 2024 08:31:36 +0100 Subject: [PATCH 2/5] Wasn't detecting existing config settings correctly and not stopping hwen config loaded --- lua/session_manager/init.lua | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lua/session_manager/init.lua b/lua/session_manager/init.lua index 2554797..529e89c 100644 --- a/lua/session_manager/init.lua +++ b/lua/session_manager/init.lua @@ -46,7 +46,9 @@ function session_manager.load_last_session(discard_current) local last_session = utils.get_last_session_filename() if last_session then utils.load_session(last_session, discard_current) + return true end + return false end --- Loads a session for the current working directory. @@ -56,8 +58,10 @@ function session_manager.load_current_dir_session(discard_current) local session = config.dir_to_session_filename(cwd) if session:exists() then utils.load_session(session.filename, discard_current) + return true end end + return false end --- Saves a session for the current working directory. @@ -69,10 +73,14 @@ function session_manager.save_current_session() end local get_autoload_mode = function() - if config.autoload_mode == nil or vim.fn.argc() > 0 or vim.g.started_with_stdin then + if config.autoload_mode == AutoloadMode.Disabled or + vim.fn.argc() > 0 + or vim.g.started_with_stdin then return { AutoloadMode.Disabled } end - if type(config.autoload_mode) ~= 'table' then + if config.autoload_mode == AutoloadMode.CurrentDir or + config.autoload_mode == AutoloadMode.LastSession then + -- Don't break existing configurations return { config.autoload_mode } end return config.autoload_mode From eb168c9520fa1c40d4671fd3417ff1887c7e82c0 Mon Sep 17 00:00:00 2001 From: nuchs Date: Tue, 4 Jun 2024 09:56:24 +0100 Subject: [PATCH 3/5] Updated for review comments --- README.md | 22 +++++++++++++++++++++- lua/session_manager/init.lua | 23 ++++++++--------------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 9290b9a..63d4253 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ require('session_manager').setup({ sessions_dir = Path:new(vim.fn.stdpath('data'), 'sessions'), -- The directory where the session files will be saved. session_filename_to_dir = session_filename_to_dir, -- Function that replaces symbols into separators and colons to transform filename into a session directory. dir_to_session_filename = dir_to_session_filename, -- Function that replaces separators and colons into special symbols to transform session directory into a filename. Should use `vim.uv.cwd()` if the passed `dir` is `nil`. - autoload_mode = config.AutoloadMode.LastSession, -- Define what to do when Neovim is started without arguments. Possible values: Disabled, CurrentDir, LastSession + autoload_mode = config.AutoloadMode.LastSession, -- Define what to do when Neovim is started without arguments. autosave_last_session = true, -- Automatically save last session on exit and on session switch. autosave_ignore_not_normal = true, -- Plugin will not save a session when no buffers are opened, or all of them aren't writable or listed. autosave_ignore_dirs = {}, -- A list of directories where the session will not be autosaved. @@ -50,6 +50,26 @@ require('session_manager').setup({ }) ``` +### Autoload mode + +If Neovim is started without arguments the value of the autoload_mode option is used to determine which session to initially load. The following modes are supported: + +| Mode | Description | +| --- | --- | +| Disabled | No session will be loaded. | +| CurrentDir | The session in the current working directory will be loaded. | +| LastSession | The last session will be loaded. This is the default.| + +Autoload_mode can be set to either a single mode or an array of modes, in which +case each mode will be tried until one succeeds e.g. + +``` +autoload_mode = { config.AutoloadMode.CurrentDir, config.AutoloadMode.LastSession } +``` + +Would attempt to load the current directory session and then fallback to the last session. + + ## Autocommands You can specify commands to be executed automatically after saving or loading a session using the following events: diff --git a/lua/session_manager/init.lua b/lua/session_manager/init.lua index 529e89c..ec86060 100644 --- a/lua/session_manager/init.lua +++ b/lua/session_manager/init.lua @@ -72,20 +72,6 @@ function session_manager.save_current_session() end end -local get_autoload_mode = function() - if config.autoload_mode == AutoloadMode.Disabled or - vim.fn.argc() > 0 - or vim.g.started_with_stdin then - return { AutoloadMode.Disabled } - end - if config.autoload_mode == AutoloadMode.CurrentDir or - config.autoload_mode == AutoloadMode.LastSession then - -- Don't break existing configurations - return { config.autoload_mode } - end - return config.autoload_mode -end - local autoloaders = { [AutoloadMode.Disabled] = function() return true end, [AutoloadMode.CurrentDir] = session_manager.load_current_dir_session, @@ -94,7 +80,14 @@ local autoloaders = { --- Loads a session based on settings. Executed after starting the editor. function session_manager.autoload_session() - for _, mode in ipairs(get_autoload_mode()) do + if vim.fn.argc() > 0 or vim.g.started_with_stdin then + return + end + local modes = config.autoload_mode + if not vim.isarray(config.autoload_mode) then + modes = { config.autoload_mode } + end + for _, mode in ipairs(modes) do if autoloaders[mode]() then return end From f3001cf384b26a0410b8b38cca863f73a6c111b3 Mon Sep 17 00:00:00 2001 From: nuchs Date: Tue, 4 Jun 2024 10:08:16 +0100 Subject: [PATCH 4/5] Update function docs --- README.md | 4 ++-- lua/session_manager/init.lua | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 63d4253..ea1119a 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ Use the command `:SessionManager[!]` with one of the following arguments: | Argument | Description | | -----------------------------| -------------------------------------------------------------------------------------------- | | `load_session` | Select and load session. (Your current session won't appear on the list). | -| `load_last_session` | Will remove all buffers and `:source` the last saved session. | -| `load_current_dir_session` | Will remove all buffers and `:source` the last saved session file of the current directory. | +| `load_last_session` | Will remove all buffers and `:source` the last saved session. (returns true if the session was restored and false otherwise) | +| `load_current_dir_session` | Will remove all buffers and `:source` the last saved session file of the current directory. (returns true if the session was restored and false otherwise) | | `save_current_session` | Works like `:mksession`, but saves/creates current directory as a session in `sessions_dir`. | | `delete_session` | Select and delete session. | | `delete_current_dir_session`| Deletes the session associated with the current directory. | diff --git a/lua/session_manager/init.lua b/lua/session_manager/init.lua index ec86060..b66df4a 100644 --- a/lua/session_manager/init.lua +++ b/lua/session_manager/init.lua @@ -42,6 +42,7 @@ end --- Loads saved used session. ---@param discard_current boolean?: If `true`, do not check for unsaved buffers. +---@return boolean: `true` if session was loaded, `false` otherwise. function session_manager.load_last_session(discard_current) local last_session = utils.get_last_session_filename() if last_session then @@ -52,6 +53,7 @@ function session_manager.load_last_session(discard_current) end --- Loads a session for the current working directory. +---@return boolean: `true` if session was loaded, `false` otherwise. function session_manager.load_current_dir_session(discard_current) local cwd = vim.uv.cwd() if cwd then From f81c97c09d4df3080f32f3a05e94612b69588c12 Mon Sep 17 00:00:00 2001 From: nuchs Date: Tue, 4 Jun 2024 10:42:33 +0100 Subject: [PATCH 5/5] Formatting and documentation fixes --- README.md | 32 ++++++++++++++++---------------- lua/session_manager/init.lua | 6 ++++-- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index ea1119a..b5b5a65 100644 --- a/README.md +++ b/README.md @@ -12,14 +12,14 @@ The plugin saves the sessions in the specified folder (see [configuration](#conf Use the command `:SessionManager[!]` with one of the following arguments: -| Argument | Description | -| -----------------------------| -------------------------------------------------------------------------------------------- | -| `load_session` | Select and load session. (Your current session won't appear on the list). | -| `load_last_session` | Will remove all buffers and `:source` the last saved session. (returns true if the session was restored and false otherwise) | -| `load_current_dir_session` | Will remove all buffers and `:source` the last saved session file of the current directory. (returns true if the session was restored and false otherwise) | -| `save_current_session` | Works like `:mksession`, but saves/creates current directory as a session in `sessions_dir`. | -| `delete_session` | Select and delete session. | -| `delete_current_dir_session`| Deletes the session associated with the current directory. | +| Argument | Description | +| -----------------------------| ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `load_session` | Select and load session. (Your current session won't appear on the list). | +| `load_last_session` | Removes all buffers and tries to `:source` the last saved session. Returns `true` if the session was restored and `false` otherwise. | +| `load_current_dir_session` | Removes all buffers and tries to `:source` the last saved session file of the current directory. Returns `true` if the session was restored and `false` otherwise. | +| `save_current_session` | Works like `:mksession`, but saves/creates current directory as a session in `sessions_dir`. | +| `delete_session` | Select and delete session. | +| `delete_current_dir_session` | Deletes the session associated with the current directory. | When `!` is specified, the modified buffers will not be saved. @@ -36,7 +36,7 @@ require('session_manager').setup({ sessions_dir = Path:new(vim.fn.stdpath('data'), 'sessions'), -- The directory where the session files will be saved. session_filename_to_dir = session_filename_to_dir, -- Function that replaces symbols into separators and colons to transform filename into a session directory. dir_to_session_filename = dir_to_session_filename, -- Function that replaces separators and colons into special symbols to transform session directory into a filename. Should use `vim.uv.cwd()` if the passed `dir` is `nil`. - autoload_mode = config.AutoloadMode.LastSession, -- Define what to do when Neovim is started without arguments. + autoload_mode = config.AutoloadMode.LastSession, -- Define what to do when Neovim is started without arguments. See "Autoload mode" section below. autosave_last_session = true, -- Automatically save last session on exit and on session switch. autosave_ignore_not_normal = true, -- Plugin will not save a session when no buffers are opened, or all of them aren't writable or listed. autosave_ignore_dirs = {}, -- A list of directories where the session will not be autosaved. @@ -54,16 +54,16 @@ require('session_manager').setup({ If Neovim is started without arguments the value of the autoload_mode option is used to determine which session to initially load. The following modes are supported: -| Mode | Description | -| --- | --- | -| Disabled | No session will be loaded. | -| CurrentDir | The session in the current working directory will be loaded. | -| LastSession | The last session will be loaded. This is the default.| +| Mode | Description | +| ----------- | ------------------------------------------------------------ | +| Disabled | No session will be loaded. | +| CurrentDir | The session in the current working directory will be loaded. | +| LastSession | The last session will be loaded. This is the default. | -Autoload_mode can be set to either a single mode or an array of modes, in which +`autoload_mode` can be set to either a single mode or an array of modes, in which case each mode will be tried until one succeeds e.g. -``` +```lua autoload_mode = { config.AutoloadMode.CurrentDir, config.AutoloadMode.LastSession } ``` diff --git a/lua/session_manager/init.lua b/lua/session_manager/init.lua index b66df4a..dbf0a4e 100644 --- a/lua/session_manager/init.lua +++ b/lua/session_manager/init.lua @@ -40,7 +40,7 @@ function session_manager.load_session(discard_current) end) end ---- Loads saved used session. +--- Tries to load the last saved session. ---@param discard_current boolean?: If `true`, do not check for unsaved buffers. ---@return boolean: `true` if session was loaded, `false` otherwise. function session_manager.load_last_session(discard_current) @@ -52,7 +52,7 @@ function session_manager.load_last_session(discard_current) return false end ---- Loads a session for the current working directory. +--- Tries to load a session for the current working directory. ---@return boolean: `true` if session was loaded, `false` otherwise. function session_manager.load_current_dir_session(discard_current) local cwd = vim.uv.cwd() @@ -85,10 +85,12 @@ function session_manager.autoload_session() if vim.fn.argc() > 0 or vim.g.started_with_stdin then return end + local modes = config.autoload_mode if not vim.isarray(config.autoload_mode) then modes = { config.autoload_mode } end + for _, mode in ipairs(modes) do if autoloaders[mode]() then return