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

F/ordered autoload #126

Merged
merged 5 commits into from
Jun 4, 2024
Merged
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
38 changes: 29 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
| `load_current_dir_session` | Will remove all buffers and `:source` the last saved session file of the current directory. |
| `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.

Expand All @@ -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. 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.
Expand All @@ -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.

```lua
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:
Expand Down
33 changes: 26 additions & 7 deletions lua/session_manager/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,30 @@ 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)
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.
--- 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()
if cwd then
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.
Expand All @@ -68,13 +74,26 @@ function session_manager.save_current_session()
end
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
Shatur marked this conversation as resolved.
Show resolved Hide resolved
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()
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
Shatur marked this conversation as resolved.
Show resolved Hide resolved

for _, mode in ipairs(modes) do
if autoloaders[mode]() then
return
end
end
end
Expand Down
Loading