From f35ad357bd0bdff0d9003c55d49c011f034c315a Mon Sep 17 00:00:00 2001 From: ray Date: Wed, 28 Oct 2026 19:02:43 +0000 Subject: [PATCH] toggle netrw previews with p --- LOG.md | 1 + MIGRATION_PLAN.md | 3 ++- README.md | 10 +++++----- lua/netrw-preview.lua | 9 ++++++++- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/LOG.md b/LOG.md index f319dee..ac48d07 100644 --- a/LOG.md +++ b/LOG.md @@ -221,6 +221,7 @@ Record every decision here with a short rationale. Append new entries; do not re - 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. +- 2026-10-28: Changed the enhanced netrw `p` mapping into a true toggle. With no preview it opens the selected file or directory and begins live cursor updates; with an existing preview it closes that window and stops updates, matching manual `:pclose` behavior. ## Project-Local Configuration diff --git a/MIGRATION_PLAN.md b/MIGRATION_PLAN.md index 67edfd3..62c1f6b 100644 --- a/MIGRATION_PLAN.md +++ b/MIGRATION_PLAN.md @@ -610,7 +610,7 @@ Installed layout: Target workflow: ```text -p on file/directory open preview and begin live updates +p on file/directory toggle preview and live updates j / k replace preview with the selected entry , o, v, t open the file and close the preview :pclose / z close the preview and stop live updates @@ -672,6 +672,7 @@ file 3.2K 2026-10-27 12:55 telescope.lua - [x] Suppress remaining Vim syntax and indent-guide decoration in file previews, restoring prior state on edit - [x] Add count-aware `` / `` preview scrolling without moving focus from netrw - [x] Make preview scrolling silently clamp at file/window boundaries +- [x] Make `p` toggle an existing preview closed as well as opening it - [ ] Test the workflow in an actual SSH session - [ ] Confirm stock netrw remains understandable when this configuration is unavailable diff --git a/README.md b/README.md index ab7744e..a0699ae 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ Core keymaps available globally (not plugin-specific). These provide fallbacks f | Key | Description | | --- | --- | -| `p` | Preview the selected file or directory below and enable live cursor updates | +| `p` | Toggle the selected file/directory preview and live cursor updates | | `j` / `k` | Update an existing preview to the newly selected entry | | `` / `` | Scroll the preview down/up without leaving netrw; accepts a count | | `:pclose` / `z` | Close the preview and stop live updates | @@ -120,10 +120,10 @@ Preview-only file buffers remain loaded but unlisted until Neovim exits, avoidin the parser detachment that triggers this Neovim 0.12 race. Opening a file with ``, `o`, `v`, or `t`, or otherwise leaving netrw, closes -the preview. Closing it manually also disables live updates until `p` is pressed -again. Enhanced directory summaries apply to local paths (including local paths -when Neovim runs over SSH); netrw URI browsing retains its native preview -behavior. +the preview. Closing it manually or pressing `p` again disables live updates +until `p` is pressed again. Enhanced directory summaries apply to local paths +(including local paths when Neovim runs over SSH); netrw URI browsing retains +its native preview behavior. ##### File Operations diff --git a/lua/netrw-preview.lua b/lua/netrw-preview.lua index 90aaded..80d1725 100644 --- a/lua/netrw-preview.lua +++ b/lua/netrw-preview.lua @@ -657,10 +657,17 @@ function M.setup(buf) vim.keymap.set('n', 'p', function() state.win = vim.api.nvim_get_current_win() state.tab = vim.api.nvim_get_current_tabpage() + + if find_preview_window(state.tab) then + close_timer(state) + close_preview(state) + return + end + preview_selected(state, true) end, { buffer = buf, - desc = 'Netrw: Preview selection and enable live updates', + desc = 'Netrw: Toggle preview and live updates', nowait = true, silent = true, })