Skip to content

Commit b3058f6

Browse files
authored
Allow multiple startup options to be tried in order (#126)
1 parent b552ee8 commit b3058f6

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

README.md

+29-9
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ The plugin saves the sessions in the specified folder (see [configuration](#conf
1212

1313
Use the command `:SessionManager[!]` with one of the following arguments:
1414

15-
| Argument | Description |
16-
| -----------------------------| -------------------------------------------------------------------------------------------- |
17-
| `load_session` | Select and load session. (Your current session won't appear on the list). |
18-
| `load_last_session` | Will remove all buffers and `:source` the last saved session. |
19-
| `load_current_dir_session` | Will remove all buffers and `:source` the last saved session file of the current directory. |
20-
| `save_current_session` | Works like `:mksession`, but saves/creates current directory as a session in `sessions_dir`. |
21-
| `delete_session` | Select and delete session. |
22-
| `delete_current_dir_session`| Deletes the session associated with the current directory. |
15+
| Argument | Description |
16+
| -----------------------------| ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
17+
| `load_session` | Select and load session. (Your current session won't appear on the list). |
18+
| `load_last_session` | Removes all buffers and tries to `:source` the last saved session. Returns `true` if the session was restored and `false` otherwise. |
19+
| `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. |
20+
| `save_current_session` | Works like `:mksession`, but saves/creates current directory as a session in `sessions_dir`. |
21+
| `delete_session` | Select and delete session. |
22+
| `delete_current_dir_session` | Deletes the session associated with the current directory. |
2323

2424
When `!` is specified, the modified buffers will not be saved.
2525

@@ -36,7 +36,7 @@ require('session_manager').setup({
3636
sessions_dir = Path:new(vim.fn.stdpath('data'), 'sessions'), -- The directory where the session files will be saved.
3737
session_filename_to_dir = session_filename_to_dir, -- Function that replaces symbols into separators and colons to transform filename into a session directory.
3838
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`.
39-
autoload_mode = config.AutoloadMode.LastSession, -- Define what to do when Neovim is started without arguments. Possible values: Disabled, CurrentDir, LastSession
39+
autoload_mode = config.AutoloadMode.LastSession, -- Define what to do when Neovim is started without arguments. See "Autoload mode" section below.
4040
autosave_last_session = true, -- Automatically save last session on exit and on session switch.
4141
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.
4242
autosave_ignore_dirs = {}, -- A list of directories where the session will not be autosaved.
@@ -50,6 +50,26 @@ require('session_manager').setup({
5050
})
5151
```
5252

53+
### Autoload mode
54+
55+
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:
56+
57+
| Mode | Description |
58+
| ----------- | ------------------------------------------------------------ |
59+
| Disabled | No session will be loaded. |
60+
| CurrentDir | The session in the current working directory will be loaded. |
61+
| LastSession | The last session will be loaded. This is the default. |
62+
63+
`autoload_mode` can be set to either a single mode or an array of modes, in which
64+
case each mode will be tried until one succeeds e.g.
65+
66+
```lua
67+
autoload_mode = { config.AutoloadMode.CurrentDir, config.AutoloadMode.LastSession }
68+
```
69+
70+
Would attempt to load the current directory session and then fallback to the last session.
71+
72+
5373
## Autocommands
5474

5575
You can specify commands to be executed automatically after saving or loading a session using the following events:

lua/session_manager/init.lua

+26-7
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,30 @@ function session_manager.load_session(discard_current)
4040
end)
4141
end
4242

43-
--- Loads saved used session.
43+
--- Tries to load the last saved session.
4444
---@param discard_current boolean?: If `true`, do not check for unsaved buffers.
45+
---@return boolean: `true` if session was loaded, `false` otherwise.
4546
function session_manager.load_last_session(discard_current)
4647
local last_session = utils.get_last_session_filename()
4748
if last_session then
4849
utils.load_session(last_session, discard_current)
50+
return true
4951
end
52+
return false
5053
end
5154

52-
--- Loads a session for the current working directory.
55+
--- Tries to load a session for the current working directory.
56+
---@return boolean: `true` if session was loaded, `false` otherwise.
5357
function session_manager.load_current_dir_session(discard_current)
5458
local cwd = vim.uv.cwd()
5559
if cwd then
5660
local session = config.dir_to_session_filename(cwd)
5761
if session:exists() then
5862
utils.load_session(session.filename, discard_current)
63+
return true
5964
end
6065
end
66+
return false
6167
end
6268

6369
--- Saves a session for the current working directory.
@@ -68,13 +74,26 @@ function session_manager.save_current_session()
6874
end
6975
end
7076

77+
local autoloaders = {
78+
[AutoloadMode.Disabled] = function() return true end,
79+
[AutoloadMode.CurrentDir] = session_manager.load_current_dir_session,
80+
[AutoloadMode.LastSession] = session_manager.load_last_session,
81+
}
82+
7183
--- Loads a session based on settings. Executed after starting the editor.
7284
function session_manager.autoload_session()
73-
if config.autoload_mode ~= AutoloadMode.Disabled and vim.fn.argc() == 0 and not vim.g.started_with_stdin then
74-
if config.autoload_mode == AutoloadMode.CurrentDir then
75-
session_manager.load_current_dir_session()
76-
elseif config.autoload_mode == AutoloadMode.LastSession then
77-
session_manager.load_last_session()
85+
if vim.fn.argc() > 0 or vim.g.started_with_stdin then
86+
return
87+
end
88+
89+
local modes = config.autoload_mode
90+
if not vim.isarray(config.autoload_mode) then
91+
modes = { config.autoload_mode }
92+
end
93+
94+
for _, mode in ipairs(modes) do
95+
if autoloaders[mode]() then
96+
return
7897
end
7998
end
8099
end

0 commit comments

Comments
 (0)