From bf39cd1ecd85b046ff84e2380a08e55dd7c9a70e Mon Sep 17 00:00:00 2001 From: ray Date: Wed, 28 Oct 2026 19:00:31 +0000 Subject: [PATCH] omit netrw from saved sessions --- LOG.md | 1 + MIGRATION_PLAN.md | 1 + README.md | 1 + lua/autocmds.lua | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) diff --git a/LOG.md b/LOG.md index 89fa67c..f319dee 100644 --- a/LOG.md +++ b/LOG.md @@ -220,6 +220,7 @@ Record every decision here with a short rationale. Append new entries; do not re - 2026-10-28: Made netrw file previews intentionally plain after CSS and HTML exposed residual colour. The remaining decoration was Vim regex syntax plus indent-blankline extmarks, not diagnostics or Tree-sitter. Preview buffers now clear `syntax` and disable their buffer-local indent guides, then restore both recorded states on real edit. - 2026-10-28: Added buffer-local netrw `` / `` mappings to scroll the current preview with native `` / `` behavior while focus remains in the tree. The mappings accept Vim counts and quietly do nothing when no preview exists. - 2026-10-28: Hardened preview scrolling after an interactive over-scroll surfaced a window-local error that did not reproduce headlessly. The temporary preview-window call is now protected, so stale-window and boundary failures are treated like native top/bottom clamping instead of reaching the command line. +- 2026-10-28: Excluded netrw from automatic session persistence after `:mksession` serialized its generated `NetrwTreeListing` name and restored an empty explorer buffer. Before the existing exit-time save, preview windows and netrw windows are removed; a netrw-only tab is dropped, while the sole remaining netrw window is replaced with an ordinary file buffer or a blank fallback. Netrw is intentionally not rebuilt on load. ## Project-Local Configuration diff --git a/MIGRATION_PLAN.md b/MIGRATION_PLAN.md index 7abfa92..67edfd3 100644 --- a/MIGRATION_PLAN.md +++ b/MIGRATION_PLAN.md @@ -500,6 +500,7 @@ Source of truth for the step-by-step rebuild. Keep this concise and up to date. ## Phase 12.6 — Final polish - [x] Review all keymaps and ensure they're documented (README.md + :KeymapsGuide) - [x] Update LOG.md with final architecture and usage +- [x] Omit transient netrw and preview windows from automatic session saves - [ ] Clean up any remaining debug code or temporary comments ## Phase 13 — Controlled Neovim 0.12 Upgrade diff --git a/README.md b/README.md index f7f9e66..ab7744e 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Session support is automatic but user-controlled: **Auto-load/save** (via `lua/autocmds.lua`): - On startup: auto-loads `Session.vim` if it exists (unless files specified on command line) - On exit: auto-saves to `Session.vim` if it already exists +- Netrw and its preview windows are omitted from the saved layout; generated `NetrwTreeListing` buffers are not restorable - Sessions are per-directory and opt-in (just create one to start using) **Stop using a session**: diff --git a/lua/autocmds.lua b/lua/autocmds.lua index 1ce5b07..6659ae5 100644 --- a/lua/autocmds.lua +++ b/lua/autocmds.lua @@ -137,6 +137,78 @@ vim.api.nvim_create_autocmd("SessionLoadPost", { end, }) +local function is_netrw_session_buffer(buf) + return vim.api.nvim_buf_is_valid(buf) + and (vim.bo[buf].filetype == "netrw" or vim.api.nvim_buf_get_name(buf):match("NetrwTreeListing$") ~= nil) +end + +local function session_fallback_buffer() + for _, buf in ipairs(vim.api.nvim_list_bufs()) do + if + vim.api.nvim_buf_is_valid(buf) + and vim.bo[buf].buflisted + and vim.bo[buf].buftype == "" + and vim.api.nvim_buf_get_name(buf) ~= "" + and not is_netrw_session_buffer(buf) + then + return buf + end + end + + return vim.api.nvim_create_buf(true, false) +end + +local function close_session_tab(tab) + if vim.api.nvim_tabpage_close then + pcall(vim.api.nvim_tabpage_close, tab, true) + return + end + + pcall(function() + vim.api.nvim_set_current_tabpage(tab) + vim.cmd("tabclose!") + end) +end + +local function omit_netrw_from_session() + local tabs_with_netrw = {} + for _, tab in ipairs(vim.api.nvim_list_tabpages()) do + for _, win in ipairs(vim.api.nvim_tabpage_list_wins(tab)) do + if is_netrw_session_buffer(vim.api.nvim_win_get_buf(win)) then + tabs_with_netrw[tab] = true + break + end + end + end + + for tab in pairs(tabs_with_netrw) do + if vim.api.nvim_tabpage_is_valid(tab) then + for _, win in ipairs(vim.api.nvim_tabpage_list_wins(tab)) do + if vim.api.nvim_win_is_valid(win) and vim.wo[win].previewwindow then + pcall(vim.api.nvim_win_close, win, true) + end + end + + if vim.api.nvim_tabpage_is_valid(tab) then + for _, win in ipairs(vim.api.nvim_tabpage_list_wins(tab)) do + if + vim.api.nvim_win_is_valid(win) + and is_netrw_session_buffer(vim.api.nvim_win_get_buf(win)) + then + if #vim.api.nvim_tabpage_list_wins(tab) > 1 then + pcall(vim.api.nvim_win_close, win, true) + elseif #vim.api.nvim_list_tabpages() > 1 then + close_session_tab(tab) + else + vim.api.nvim_win_set_buf(win, session_fallback_buffer()) + end + end + end + end + end + end +end + -- Auto-save session on exit, but ONLY if Session.vim already exists vim.api.nvim_create_autocmd("VimLeavePre", { group = session_aug, @@ -144,6 +216,7 @@ vim.api.nvim_create_autocmd("VimLeavePre", { callback = function() -- Only save if Session.vim exists (user manually created it) if vim.fn.filereadable("Session.vim") == 1 then + omit_netrw_from_session() vim.cmd("mksession! Session.vim") end end,