diff --git a/autoload/vimtex/fold.vim b/autoload/vimtex/fold.vim index 97aa482..a7898f4 100644 --- a/autoload/vimtex/fold.vim +++ b/autoload/vimtex/fold.vim @@ -28,6 +28,7 @@ function! vimtex#fold#init_options() " {{{1 \ ]) call vimtex#util#set_default('g:vimtex_fold_documentclass', 0) call vimtex#util#set_default('g:vimtex_fold_usepackage', 1) + call vimtex#util#set_default('g:vimtex_fold_newcommands', 1) " Disable manual mode in vimdiff let g:vimtex_fold_manual = &diff ? 0 : g:vimtex_fold_manual @@ -44,6 +45,7 @@ function! vimtex#fold#init_script() " {{{1 " List of identifiers for improving efficiency " let s:folded = '\v^\s*\%|^\s*\]\{' + let s:folded .= '|^\s*}\s*$' let s:folded .= '|\\%(' . join([ \ 'begin', \ 'end', @@ -54,6 +56,7 @@ function! vimtex#fold#init_script() " {{{1 \ 'appendix', \ 'part', \ 'usepackage', + \ '%(re)?new%(command|environment)', \ ], '|') . ')' endfunction @@ -171,6 +174,20 @@ function! vimtex#fold#level(lnum) " {{{1 endif endif + " Fold newcommands (and similar) + if g:vimtex_fold_newcommands + if line =~# '\v^\s*\\%(re)?new%(command|environment)\*?' + \ && indent(a:lnum+1) > indent(a:lnum) + let s:newcommand_indent = indent(a:lnum) + return 'a1' + elseif exists('s:newcommand_indent') + \ && indent(a:lnum) == s:newcommand_indent + \ && line =~# '^\s*}\s*$' + unlet s:newcommand_indent + return 's1' + endif + endif + " Fold chapters and sections for [part, level] in b:vimtex_fold.parts if line =~# part @@ -275,6 +292,13 @@ function! vimtex#fold#text() " {{{1 \ . '}' endif + " Text for newcommand (and similar) + if g:vimtex_fold_newcommands + \ && line =~# '\v^\s*\\%(re)?new%(command|environment)' + return matchstr(line, + \ '\v^\s*\\%(re)?new%(command|environment)\*?\{[^}]*\}') . ' ...' + endif + " Text for documentclass if g:vimtex_fold_documentclass && line =~# '^\s*\\documentclass' return '\documentclass[...]{' diff --git a/doc/vimtex.txt b/doc/vimtex.txt index b902d59..6f2689c 100644 --- a/doc/vimtex.txt +++ b/doc/vimtex.txt @@ -488,6 +488,20 @@ Options~ < Default value: 1 +*g:vimtex_fold_newcommands* + Use this option to disable/enable folding of long `\[re]newcommand` and + `\[re]newenvironment` lines. The lines must be formatted like this for + folding to work properly: > + + \[re]newcommand{\command}{ + ..., + } +< + That is, the fold region starts at the command and ends when at the final `}` + at the same indent level as the start of the command. + + Default value: 1 + *g:vimtex_fold_documentclass* Use this option to disable/enable folding of long `\documentclass` lines. This works similar to |g:vimtex_fold_usepackage|. @@ -1667,6 +1681,7 @@ Associated settings: |g:vimtex_fold_sections| |g:vimtex_fold_envs| |g:vimtex_fold_usepackage| + |g:vimtex_fold_newcommands| ============================================================================== INDENTATION *vimtex-indent* diff --git a/test/features/folding/preamble.tex b/test/features/folding/preamble.tex new file mode 100644 index 0000000..204e133 --- /dev/null +++ b/test/features/folding/preamble.tex @@ -0,0 +1,87 @@ +\documentclass[% Options of scrbook + % draft, + fontsize=12pt, + %smallheadings, + headings=big, + english, + paper=a4, + twoside, + open=right, + DIV=14, + BCOR=20mm, + headinclude=false, + footinclude=false, + mpinclude=false, + pagesize, + titlepage, + parskip=half, + headsepline, + chapterprefix=false, + appendixprefix=Appendix, + appendixwithprefixline=true, + bibliography=totoc, + toc=graduated, + numbers=noenddot, +]{scrbook} + +% +% Fold usepackages +% +\usepackage[ + ... +]{test} +\usepackage[ + backend=biber, + style=numeric-comp, + maxcitenames=99, + doi=false, + url=false, + giveninits=true, +]{biblatex} + +% +% Fold newcommands and similar +% +\renewcommand{\marginpar}{% + \marginnote% +} +\newcommand*{\StoreCiteField}[3]{% + \begingroup + \global\let\StoreCiteField@Result\relax + \citefield{#2}[StoreCiteField]{#3}% + \endgroup + \let#1\StoreCiteField@Result +} +\newenvironment{theo}[1][]{% + \stepcounter{theo}% + \ifstrempty{#1}{% + \mdfsetup{ + frametitle={% + \tikz[baseline=(current bounding box.east),outer sep=0pt]% + \node[anchor=east,rectangle,fill=blue!20] {\strut Theorem~\thetheo};% + } + }% + }% + {% else ifstrempty: + \mdfsetup{ + frametitle={ + \tikz[baseline=(current bounding box.east),outer sep=0pt]% + \node[anchor=east,rectangle,fill=blue!20]{\strut Theorem~\thetheo:~#1};}% + } + }% + \mdfsetup{ + innertopmargin=10pt, + linecolor=blue!20, + linewidth=2pt,topline=true, + frametitleaboveskip=\dimexpr-\ht\strutbox\relax, + } + \begin{mdframed}[]\relax% +}{% + \end{mdframed}% +} + +\begin{document} + +Hello World + +\end{document}