From 614af6b3c3b1aeb7e2673f18741a806cebc6676c Mon Sep 17 00:00:00 2001 From: Alexander Karle Date: Wed, 7 Nov 2018 23:58:20 -0500 Subject: [PATCH] vim: Add custom folding for help doc in Vim This is particularly useful for writing my own doc. --- vim/ftplugin/help.vim | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 vim/ftplugin/help.vim diff --git a/vim/ftplugin/help.vim b/vim/ftplugin/help.vim new file mode 100644 index 0000000..1b11000 --- /dev/null +++ b/vim/ftplugin/help.vim @@ -0,0 +1,32 @@ +" Some folding settings I like to use for writing Vim plugin documentation +" Won't work universally, but doesn't hurt to enable + +" Simple fold function to fold until the next ===== or ------ +function! DocTxtFoldExpr(lnum) abort + if getline(a:lnum + 1) =~ '^\(=\+\|-\+\)$' + return '<1' + else + return 1 + endif +endfunction + +" Fold Text that prints number lines folded, a preview of what header it is, +" and the section name. Attempted to align nicely, but it breaks with tabs +" - 6 lines: ===== INTRO ..... +" - 12 lines: ----- Example ..... +function! DocTxtFoldText() abort + let l = getline(v:foldstart) + let lp1 = getline(v:foldstart + 1) + let num_lines = v:foldend - v:foldstart + 1 + let lines_text = printf("%4s", num_lines) . ' lines:' + let header_preview = l[0:5] + let section_name = lp1[0:stridx(lp1, ' ')] + if section_name !~ ' $' + let section_name = section_name . ' ' + endif + return join([v:folddashes, lines_text, header_preview, section_name], ' ') +endfunction + +setlocal foldexpr=DocTxtFoldExpr(v:lnum) +setlocal foldtext=DocTxtFoldText() +setlocal foldmethod=expr -- libgit2 0.28.4