From cfa335176a4440bbaf3963416e030a4ba1935072 Mon Sep 17 00:00:00 2001 From: Dane Sabo Date: Thu, 28 May 2026 16:07:26 -0400 Subject: [PATCH] Replace auto-session with simple named-session commands Drop the rmagatti/auto-session plugin (automatic cwd-keyed sessions went unused). Add custom.sessions: :SessionWrite/:SessionRead/ :SessionDelete over plain :mksession files in /sessions/, with tab-completion and a picker fallback when no name is given. --- lua/custom/init.lua | 1 + lua/custom/plugins.lua | 32 --------------- lua/custom/sessions.lua | 89 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 32 deletions(-) create mode 100644 lua/custom/sessions.lua diff --git a/lua/custom/init.lua b/lua/custom/init.lua index 4f935cf..89d8039 100644 --- a/lua/custom/init.lua +++ b/lua/custom/init.lua @@ -6,4 +6,5 @@ vim.o.sessionoptions = "blank,buffers,curdir,folds,help,tabpages,winsize,winpos, require "custom.language_specific_commands.cadquery" require "custom.git_quickpush" +require "custom.sessions" print "Custom init settings loaded." diff --git a/lua/custom/plugins.lua b/lua/custom/plugins.lua index 526b7ea..d5696c6 100644 --- a/lua/custom/plugins.lua +++ b/lua/custom/plugins.lua @@ -90,38 +90,6 @@ local plugins = { return M end, }, - { - "rmagatti/auto-session", - lazy = false, - keys = { - -- Will use Telescope if installed or a vim.ui.select picker otherwise - { "fs", "AutoSession search", desc = "Session search" }, - { - "ws", - function() - -- prompt for a session name using the built-in input function: - local session_name = vim.fn.input "Enter session name: " - - -- optionally check if the user provided something - if session_name ~= nil and session_name ~= "" then - -- construct the command with the chosen name - vim.cmd("AutoSession save " .. session_name) - else - print "No session name given. Session not saved." - end - end, - desc = "Save session (prompts for name)", - }, - { "wa", "AutoSession ToggleAutoSave", desc = "Toggle autosave" }, - }, - ---enables autocomplete for opts - ---@module "auto-session" - ---@type AutoSession.Config - opts = { - suppressed_dirs = { "~/", "~/Projects", "~/Downloads", "/" }, - root_dir = os.getenv "HOME" .. "/Documents/Dane's Vault/.sessions/", - }, - }, { "stevearc/conform.nvim", event = { "BufWritePre" }, diff --git a/lua/custom/sessions.lua b/lua/custom/sessions.lua new file mode 100644 index 0000000..7db84af --- /dev/null +++ b/lua/custom/sessions.lua @@ -0,0 +1,89 @@ +-- Simple named-session management: write / read / delete. +-- Sessions are plain :mksession files (so 'sessionoptions' governs content) +-- stored under one directory. No automatic save/restore. + +local session_dir = vim.fs.normalize(vim.fn.stdpath "data") .. "/sessions/" +vim.fn.mkdir(session_dir, "p") + +local function session_path(name) + return session_dir .. name .. ".vim" +end + +local function list_sessions() + local names = {} + for _, path in ipairs(vim.fn.glob(session_dir .. "*.vim", true, true)) do + table.insert(names, vim.fn.fnamemodify(path, ":t:r")) + end + return names +end + +local function write_session(name) + if name == "" then + name = vim.fn.input "Session name: " + end + if name == "" then + vim.notify("Session write aborted: no name given.", vim.log.levels.WARN) + return + end + vim.cmd("mksession! " .. vim.fn.fnameescape(session_path(name))) + vim.notify("Session saved: " .. name, vim.log.levels.INFO) +end + +local function read_session(name) + local function load(n) + local path = session_path(n) + if vim.fn.filereadable(path) == 0 then + vim.notify("No session named: " .. n, vim.log.levels.ERROR) + return + end + vim.cmd("source " .. vim.fn.fnameescape(path)) + end + if name ~= "" then + load(name) + else + vim.ui.select(list_sessions(), { prompt = "Load session:" }, function(choice) + if choice then + load(choice) + end + end) + end +end + +local function delete_session(name) + local function del(n) + local path = session_path(n) + if vim.fn.filereadable(path) == 0 then + vim.notify("No session named: " .. n, vim.log.levels.ERROR) + return + end + vim.fn.delete(path) + vim.notify("Session deleted: " .. n, vim.log.levels.INFO) + end + if name ~= "" then + del(name) + else + vim.ui.select(list_sessions(), { prompt = "Delete session:" }, function(choice) + if choice then + del(choice) + end + end) + end +end + +local function complete_sessions(arg_lead) + return vim.tbl_filter(function(name) + return name:find(arg_lead, 1, true) == 1 + end, list_sessions()) +end + +vim.api.nvim_create_user_command("SessionWrite", function(o) + write_session(o.args) +end, { nargs = "?", desc = "Write current session (prompts for name if omitted)" }) + +vim.api.nvim_create_user_command("SessionRead", function(o) + read_session(o.args) +end, { nargs = "?", complete = complete_sessions, desc = "Read a session (picker if name omitted)" }) + +vim.api.nvim_create_user_command("SessionDelete", function(o) + delete_session(o.args) +end, { nargs = "?", complete = complete_sessions, desc = "Delete a session (picker if name omitted)" })