vim:ftplug:man: Enable resizing with hard-wrapping

From :h :Man:

> when running `man` from the shell and with that `MANPAGER` [='nvim
> +Man!'] in your environment, `man` will pre-format the manpage using
> `groff`. Thus, Nvim will inevitably display the manual page as it was
> passed to it from stdin. One of the caveats of this is that the width
> will _always_ be hard-wrapped

Since I actually don't like `g:man_hardwrap=0`/`MANPAGER=999` (e.g.
scrolling can be a mess with very long wrapped lines), add an
autocommand that is meant to reload the manpage through `:edit` after
every resize, so that its hard-wrapping adjusts to the new size.

This is slightly hacky, but does its job quite well.

Move the man.vim into after/ftplugin so that it overwrites the `set
wrap` of the global ftplugin, which I want turned off, since it messes
with the buffer shortly when resizing.
This commit is contained in:
2025-01-25 02:49:54 +01:00
parent 467df52c66
commit 78917a686e
Notes: Julian Prein 2025-07-04 14:15:09 +02:00
`MANPAGER=999` should have been `MANWIDTH=999`
2 changed files with 52 additions and 7 deletions

View File

@@ -0,0 +1,52 @@
" NOTE: The global man ftplugin enables wrapping which I want off (see below),
" which is why this needs to be in after/.
setlocal nospell
" man(1) will assume it can use the full width of the terminal when
" hard-wrapping the lines. When signcolumn is enabled the width is one cell
" smaller and thus, lines that have a character in the last column will be
" wrapped by vim (i.e. almost all of them).
setlocal signcolumn=no
" ------------------------------------------------------------------------------
" From :h :Man:
"
" when running `man` from the shell and with that `MANPAGER` [='nvim +Man!']
" in your environment, `man` will pre-format the manpage using `groff`.
" Thus, Nvim will inevitably display the manual page as it was passed to it
" from stdin. One of the caveats of this is that the width will _always_ be
" hard-wrapped
"
" Since I actually don't like `g:man_hardwrap=0`/`MANPAGER=999` (e.g. scrolling
" can be a mess with very long wrapped lines), the following autocommand is
" meant to reload the manpage through `:edit` after every resize, so that its
" hard-wrapping adjusts to the new size.
"
" This is slightly hacky, but does its job quite well.
augroup man_resized
" The reload has to be delayed slightly, since with an `edit` directly in
" the autocmd, the buffer will just be cleared? (TODO)
" NOTE: One could add a wrapper function that checks for the existence of
" already running timers similar to how it's done in the
" highlight_current group (see autocommands.vim), but this feels
" overkill here.
au! WinResized <buffer> call timer_start(10, function("s:edit_delayed"))
augroup END
" The function can't be redefined during the reload of the ftplugin (i.e.
" triggered by `edit`), since it is currently executing (i.e. `edit`):
"
" E127: Cannot redefine function <SNR>94_edit_delayed: It is in use
"
if !exists("*s:edit_delayed")
function s:edit_delayed(timer_id)
edit
endfunction
endif
" When wrapping is turned on, shrinking the window will momentarily rewrap the
" lines, messing up the whole buffer. Since this is distracting, turn it off.
setlocal nowrap
" ------------------------------------------------------------------------------