summaryrefslogtreecommitdiffstats
path: root/plugin
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-05-18 22:49:50 +0200
committerThomas Vanbesien <tvanbesi@proton.me>2026-05-18 22:49:50 +0200
commit5e304cc843fddf52900faa522713bcbd295e6a1d (patch)
treefc282b800cbaaedb6f49492174c2994d8d8f7555 /plugin
parent0f0812fd82d2a511f994b93271e0348b44903035 (diff)
downloadnvim-config-5e304cc843fddf52900faa522713bcbd295e6a1d.tar.gz
nvim-config-5e304cc843fddf52900faa522713bcbd295e6a1d.zip
feat(nvim): persist tabpage names across sessions
Diffstat (limited to 'plugin')
-rw-r--r--plugin/50-session.lua34
1 files changed, 34 insertions, 0 deletions
diff --git a/plugin/50-session.lua b/plugin/50-session.lua
index b4aadda..d1610e6 100644
--- a/plugin/50-session.lua
+++ b/plugin/50-session.lua
@@ -9,12 +9,42 @@ if not vim.uv.fs_stat(session_dir) then
vim.notify("Sessions save directory created at " .. session_dir)
end
+-- :mksession doesn't persist tab-local vars (t:tabname), so we write tab names to a sidecar
+-- JSON file alongside the .vim session. Names are stored positionally because :mksession
+-- recreates tabs in their original order, making the index stable across reloads.
+local function tabnames_path(path)
+ return (path:gsub("%.vim$", "")) .. ".tabnames.json"
+end
+
local function save_session(path)
vim.cmd.mksession({ path, bang = true })
+ local names = {}
+ for i, tp in ipairs(vim.api.nvim_list_tabpages()) do
+ -- pcall: nvim_tabpage_get_var errors when the variable isn't set on that tab
+ local ok, name = pcall(vim.api.nvim_tabpage_get_var, tp, "tabname")
+ names[i] = (ok and type(name) == "string") and name or ""
+ end
+ local f = io.open(tabnames_path(path), "w")
+ if f then
+ f:write(vim.json.encode(names))
+ f:close()
+ end
end
local function load_session(path)
vim.cmd.source(path)
+ local f = io.open(tabnames_path(path), "r")
+ if not f then
+ return
+ end
+ local names = vim.json.decode(f:read("*a"))
+ f:close()
+ for i, tp in ipairs(vim.api.nvim_list_tabpages()) do
+ if names[i] and names[i] ~= "" then
+ vim.api.nvim_tabpage_set_var(tp, "tabname", names[i])
+ end
+ end
+ vim.cmd.redrawtabline()
end
local function reload_session(path)
@@ -24,6 +54,10 @@ end
local function delete_session(path)
vim.fs.rm(path)
+ local sidecar = tabnames_path(path)
+ if vim.uv.fs_stat(sidecar) then
+ vim.fs.rm(sidecar)
+ end
end
local function session_completefunc(arg_lead, _, _)