From 59b48b4786c2c7d8f9a9d70f1247f2b7fd26d3b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Fr=C3=B3es=20Franco?= Date: Tue, 26 May 2026 23:02:17 -0300 Subject: [PATCH] feat(folding): open every heading fold by default on file open MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Heading-block folds were computed but all collapsed on file open, so users had to `zR` after every `:edit`. Set `foldlevel=99` once at ftplugin attach so the fold structure is still there (closeable with `zc`/`zM`) but the buffer renders fully expanded. `foldlevel` is window-local; we set it on initial attach only — not in the Lua BufWinEnter re-apply — so closing folds with `zc` in a window survives navigating away and back to that buffer. Split windows still get the same value because Vim copies window-local options to the new window when splitting. Co-Authored-By: Claude Sonnet 4.6 --- ftplugin/vimwiki.vim | 6 +++++- lua/nuwiki/ftplugin.lua | 4 ++++ scripts/test-keymaps-vim.vim | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ftplugin/vimwiki.vim b/ftplugin/vimwiki.vim index 6652b22..e628756 100644 --- a/ftplugin/vimwiki.vim +++ b/ftplugin/vimwiki.vim @@ -221,7 +221,11 @@ if !has('nvim') setlocal foldmethod=expr setlocal foldexpr=nuwiki#folding#expr() setlocal foldtext=nuwiki#folding#foldtext() - let b:undo_ftplugin .= ' | setlocal foldmethod< foldexpr< foldtext<' + " Start with every heading-block fold open; the user can still close + " them with `zc` and that state survives because `foldlevel` only + " controls the initial state, not subsequent manual fold operations. + setlocal foldlevel=99 + let b:undo_ftplugin .= ' | setlocal foldmethod< foldexpr< foldtext< foldlevel<' endif " Text objects — vimwiki parity (matches lua/nuwiki/textobjects.lua). diff --git a/lua/nuwiki/ftplugin.lua b/lua/nuwiki/ftplugin.lua index 26d117a..f729560 100644 --- a/lua/nuwiki/ftplugin.lua +++ b/lua/nuwiki/ftplugin.lua @@ -35,6 +35,10 @@ local function setup_folding(bufnr, folding_mode) -- ftplugin). Also re-apply on any future split that views this -- buffer, so `v` / `:split` preserves the fold method. apply() + -- Start with every heading-block fold open. `foldlevel` is window-local + -- but only sets the initial state; once the user closes folds with `zc` + -- those stay closed, so we don't re-apply this on BufWinEnter. + vim.opt_local.foldlevel = 99 vim.api.nvim_create_autocmd('BufWinEnter', { buffer = bufnr, group = vim.api.nvim_create_augroup('nuwiki_folding_' .. bufnr, { clear = true }), diff --git a/scripts/test-keymaps-vim.vim b/scripts/test-keymaps-vim.vim index 328f6bd..5b7c401 100644 --- a/scripts/test-keymaps-vim.vim +++ b/scripts/test-keymaps-vim.vim @@ -300,6 +300,12 @@ call s:record( \ foldlevel(2) > 0 ? 1 : 0, \ 'folding.body_inherits_fold', \ 'foldlevel(2)=' . foldlevel(2)) +" ftplugin sets `foldlevel=99` so every heading-block fold starts open +" rather than collapsed on file open. +call s:record( + \ &l:foldlevel == 99 ? 1 : 0, + \ 'folding.starts_with_all_folds_open', + \ '&l:foldlevel=' . &l:foldlevel) " ===== Wrap up =====