diff --git a/README.md b/README.md index b5e54a6..9a10090 100644 --- a/README.md +++ b/README.md @@ -114,19 +114,19 @@ EOF ### Options -| Config | Options | Default | Description | -| -------------------------------- | ------------------------ | ------------------------------------ | --------------------------------------------------------------- | -| log_level | 'debug', 'info', 'error' | 'info' | Sets the log level of the plugin | -| auto_session_enable_last_session | false, true | false | Loads the last loaded session if session for cwd does not exist | -| auto_session_root_dir | "/some/path/you/want" | vim.fn.stdpath('data').."/sessions/" | Changes the root dir for sessions | -| auto_session_enabled | false, true | true | Enables/disables the plugin's auto save _and_ restore features | -| auto_session_create_enabled | false, true | true | Enables/disables the plugin's session auto creation | -| auto_save_enabled | false, true, nil | nil | Enables/disables auto saving | -| auto_restore_enabled | false, true, nil | nil | Enables/disables auto restoring | -| auto_restore_lazy_delay_enabled | false, true, nil | true | Enables/disables delaying auto-restore if Lazy.nvim is used | -| auto_session_suppress_dirs | ["list", "of paths"] | nil | Suppress session create/restore if in one of the list of dirs | -| auto_session_allowed_dirs | ["list", "of paths"] | nil | Allow session create/restore if in one of the list of dirs | -| auto_session_use_git_branch | false, true, nil | nil | Use the git branch to differentiate the session name | +| Config | Options | Default | Description | +| -------------------------------- | ------------------------ | ------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- | +| log_level | 'debug', 'info', 'error' | 'info' | Sets the log level of the plugin | +| auto_session_enable_last_session | false, true | false | Loads the last loaded session if session for cwd does not exist | +| auto_session_root_dir | "/some/path/you/want" | vim.fn.stdpath('data').."/sessions/" | Changes the root dir for sessions | +| auto_session_enabled | false, true | true | Enables/disables the plugin's auto save _and_ restore features | +| auto_session_create_enabled | false, true, function | true | Enables/disables the plugin's session auto creation. Can also be a Lua function that returns true if a session should be created and false otherwise | +| auto_save_enabled | false, true, nil | nil | Enables/disables auto saving | +| auto_restore_enabled | false, true, nil | nil | Enables/disables auto restoring | +| auto_restore_lazy_delay_enabled | false, true, nil | true | Enables/disables delaying auto-restore if Lazy.nvim is used | +| auto_session_suppress_dirs | ["list", "of paths"] | nil | Suppress session create/restore if in one of the list of dirs | +| auto_session_allowed_dirs | ["list", "of paths"] | nil | Allow session create/restore if in one of the list of dirs | +| auto_session_use_git_branch | false, true, nil | nil | Use the git branch to differentiate the session name | #### Notes @@ -266,6 +266,27 @@ require('auto-session').setup { } ``` +## Conditionally creating a session + +`auto_session_create_enabled` can take a function that returns if a session should be created or not as part of auto saving. As one example, you could +use this to only create sessions for git projects: + +```lua + + config = function() + require('auto-session').setup({ + auto_save_enabled = true, + auto_restore_enabled = true, + + auto_session_create_enabled = function() + local cmd = 'git rev-parse --is-inside-work-tree' + return vim.fn.system(cmd) == 'true\n' + end, + }) + end + +``` + ## Argument Handling By default, when `nvim` is run with a single directory argument, auto-session will try to restore the session for that directory. If `nvim` is run with multiple directories or any file arguments, auto-session won't try to restore a session and won't auto-save a session on exit (if enabled). Those behaviors can be changed with these config parameters: diff --git a/lua/auto-session/init.lua b/lua/auto-session/init.lua index caac797..76647b2 100644 --- a/lua/auto-session/init.lua +++ b/lua/auto-session/init.lua @@ -38,7 +38,7 @@ end ---@field auto_session_enable_last_session? boolean ---@field auto_session_root_dir? string root directory for session files, by default is `vim.fn.stdpath('data')/sessions/` ---@field auto_session_enabled? boolean enable auto session ----@field auto_session_create_enabled boolean|nil Enables/disables auto creating new sessions +---@field auto_session_create_enabled boolean|function|nil Enables/disables auto creating new sessions. Can take a function that should return true/false if a session should be created or not ---@field auto_save_enabled? boolean Enables/disables auto saving session ---@field auto_restore_enabled? boolean Enables/disables auto restoring session ---@field auto_restore_lazy_delay_enabled? boolean Automatically detect if Lazy.nvim is being used and wait until Lazy is done to make sure session is restored correctly. Does nothing if Lazy isn't being used. Can be disabled if a problem is suspected or for debugging @@ -53,7 +53,7 @@ local defaultConf = { auto_session_enable_last_session = vim.g.auto_session_enable_last_session or false, -- Enables/disables the "last session" feature auto_session_root_dir = vim.fn.stdpath "data" .. "/sessions/", -- Root dir where sessions will be stored auto_session_enabled = true, -- Enables/disables auto creating, saving and restoring - auto_session_create_enabled = nil, -- Enables/disables auto creating new sessions + auto_session_create_enabled = nil, -- Enables/disables auto creating new sessions. Can take a function that should return true/false if a session should be created or not auto_save_enabled = nil, -- Enables/disables auto save feature auto_restore_enabled = nil, -- Enables/disables auto restore feature auto_restore_lazy_delay_enabled = true, -- Enables/disables Lazy delay feature @@ -175,9 +175,31 @@ end local function is_auto_create_enabled() if vim.g.auto_session_create_enabled ~= nil then - return vim.g.auto_session_create_enabled == Lib._VIM_TRUE - elseif AutoSession.conf.auto_session_create_enabled ~= nil then - return AutoSession.conf.auto_session_create_enabled + if type(vim.g.auto_session_create_enabled) == 'function' then + if (vim.g.auto_session_create_enabled()) then + Lib.logger.debug('vim.g.auto_session_create_enabled returned true, allowing creation') + return true + else + Lib.logger.debug('vim.g.auto_session_create_enabled returned false, not allowing creation') + return false + end + else + return vim.g.auto_session_create_enabled == Lib._VIM_TRUE + end + end + + if AutoSession.conf.auto_session_create_enabled ~= nil then + if type(AutoSession.conf.auto_session_create_enabled) == 'function' then + if AutoSession.conf.auto_session_create_enabled() then + Lib.logger.debug('AutoSession.conf.auto_session_create_enabled returned true, allowing creation') + return true + else + Lib.logger.debug('AutoSession.conf.auto_session_create_enabled returned false, not allowing creation') + return false + end + else + return AutoSession.conf.auto_session_create_enabled + end end return true @@ -449,6 +471,7 @@ function AutoSession.AutoSaveSession(sessions_dir) if not is_auto_create_enabled() then local session_file_name = get_session_file_name(sessions_dir) if not Lib.is_readable(session_file_name) then + Lib.logger.debug('Create not enabled and no existing session, not creating session') return end end