diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-05-04 08:44:50 +0200 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-05-06 16:43:16 +0200 |
| commit | 34510ebc63ff65d51f6979a86169bfc52de6180c (patch) | |
| tree | 36c8b73c99a3c566ccee888818c60204398991fd /plugin/50-session.lua | |
| download | nvim-config-34510ebc63ff65d51f6979a86169bfc52de6180c.tar.gz nvim-config-34510ebc63ff65d51f6979a86169bfc52de6180c.zip | |
feat: initial setup
- `dotfiles` (this project's CLI)
- foot configuration
- tmux configuration
- bash configuration
- nvim (as a git submodule) + configuration
- ranger configuration
- fzf configuration
- KDE global shortcuts
- Other miscellaneous dependencies
Diffstat (limited to 'plugin/50-session.lua')
| -rw-r--r-- | plugin/50-session.lua | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/plugin/50-session.lua b/plugin/50-session.lua new file mode 100644 index 0000000..b4aadda --- /dev/null +++ b/plugin/50-session.lua @@ -0,0 +1,69 @@ +-- +-- Session plugin +-- + +local session_dir = vim.fn.stdpath("state") .. "/sessions" +local session_default = session_dir .. "/default.vim" +if not vim.uv.fs_stat(session_dir) then + vim.uv.fs_mkdir(session_dir, tonumber("755", 8)) + vim.notify("Sessions save directory created at " .. session_dir) +end + +local function save_session(path) + vim.cmd.mksession({ path, bang = true }) +end + +local function load_session(path) + vim.cmd.source(path) +end + +local function reload_session(path) + save_session(path) + vim.cmd.restart({ args = { "+qall", "SessionLoad", path } }) +end + +local function delete_session(path) + vim.fs.rm(path) +end + +local function session_completefunc(arg_lead, _, _) + local completions = {} + for path in vim.fs.dir(session_dir) do + if string.match(path, "^" .. arg_lead) and string.match(path, ".vim$") then + completions[#completions + 1] = path:sub(1, -5) + end + end + return completions +end + +local function session_op(base, op) + local path = #base > 0 and base or session_default + if not string.match(path, "^" .. session_dir) then + path = session_dir .. "/" .. path + end + if not string.match(path, "%.vim$") then + path = path .. ".vim" + end + op(path) +end + +vim.api.nvim_create_user_command("SessionSave", function(ev) + session_op(ev.args, save_session) +end, { desc = "Save session", nargs = "?", complete = session_completefunc }) +vim.api.nvim_create_user_command("SessionLoad", function(ev) + session_op(ev.args, load_session) +end, { desc = "Load session", nargs = "?", complete = session_completefunc }) +vim.api.nvim_create_user_command("SessionDelete", function(ev) + session_op(ev.args, delete_session) +end, { desc = "Delete session", nargs = "?", complete = session_completefunc }) +vim.api.nvim_create_user_command("SessionRestart", function(ev) + session_op(ev.args, reload_session) +end, { desc = "Reload session", nargs = "?", complete = session_completefunc }) +vim.api.nvim_create_user_command("SessionExitSave", function(ev) + session_op(ev.args, save_session) + vim.cmd.qall() +end, { desc = "Save and exit session", nargs = "?", complete = session_completefunc }) +vim.api.nvim_create_user_command("SessionExitNoSave", function(ev) + session_op(ev.args, save_session) + vim.cmd.qall({ bang = true }) +end, { desc = "Save and exit session", nargs = "?", complete = session_completefunc }) |
