From 9c91576f52ca31295ec2949a2fbd504294889e5f Mon Sep 17 00:00:00 2001 From: kiryph Date: Thu, 6 Oct 2016 18:41:09 +0200 Subject: [PATCH] Extend #571: Fold cmds following 3 patterns with options to assign commands to these patterns --- autoload/vimtex/fold.vim | 113 ++++++++++++++++++++++++++++----------- 1 file changed, 81 insertions(+), 32 deletions(-) diff --git a/autoload/vimtex/fold.vim b/autoload/vimtex/fold.vim index 1e351c8..441a83f 100644 --- a/autoload/vimtex/fold.vim +++ b/autoload/vimtex/fold.vim @@ -27,10 +27,37 @@ function! vimtex#fold#init_options() " {{{1 \ 'subsubsection', \ ]) 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) call vimtex#util#set_default('g:vimtex_fold_markers', 1) + " Fold command pattern 1: \{} + " folded to '\{...}' + call vimtex#util#set_default('g:vimtex_fold_cmd_pattern1', 1) + call vimtex#util#set_default('g:vimtex_fold_cmd_pattern1_list', + \ [ + \ 'hypersetup', + \ 'tikzset', + \ ]) + " Fold command pattern 2: \[]{} + " folded to '\[...]{}' + call vimtex#util#set_default('g:vimtex_fold_cmd_pattern2', 1) + call vimtex#util#set_default('g:vimtex_fold_cmd_pattern2_list', + \ [ + \ 'usepackage', + \ 'includepdf', + \ ]) + " Fold command pattern 3: \{short mandatory arg}[]{}{\n} + " folded to '\{} ...' + " NOTE: final '}' has to be on its own line! + call vimtex#util#set_default('g:vimtex_fold_cmd_pattern3', 1) + call vimtex#util#set_default('g:vimtex_fold_cmd_pattern3_list', + \ [ + \ '%(re)?new%(command|environment)', + \ 'providecommand', + \ 'presetkeys', + \ 'Declare%(Multi|Auto)?CiteCommand', + \ 'Declare%(Index)?%(Field|List|Name)%(Format|Alias)', + \ ]) + " Disable manual mode in vimdiff let g:vimtex_fold_manual = &diff ? 0 : g:vimtex_fold_manual endfunction @@ -59,9 +86,10 @@ function! vimtex#fold#init_script() " {{{1 \ '%(front|main|back)matter', \ 'appendix', \ 'part', - \ 'usepackage', - \ '%(re)?new%(command|environment)', - \ ], '|') . ')' + \ ] + \ + g:vimtex_fold_cmd_pattern1_list + \ + g:vimtex_fold_cmd_pattern2_list + \ + g:vimtex_fold_cmd_pattern3_list, '|') . ')' endfunction " }}}1 @@ -130,7 +158,6 @@ function! s:foldmethod_in_modeline() endfunction " }}}1 - function! vimtex#fold#refresh(map) " {{{1 setlocal foldmethod=expr execute 'normal! ' . a:map @@ -167,27 +194,38 @@ function! vimtex#fold#level(lnum) " {{{1 return '0' endif - " Fold usepackages - if g:vimtex_fold_usepackage - if line =~# '^\s*\\usepackage\s*\[\s*\%($\|%\)' - let s:usepackage = 1 + " Fold command pattern 1 (e.g. hypersetup) + if g:vimtex_fold_cmd_pattern1 + if line =~# '^\s*\\\%('.join(g:vimtex_fold_cmd_pattern1_list, '\|').'\)\s*{\s*\%($\|%\)' + let s:cmd_pattern1 = 1 return 'a1' - elseif get(s:, 'usepackage', 0) && line =~# '^\s*\]{' - let s:usepackage = 0 + elseif get(s:, 'cmd_pattern1', 0) && line =~# '^\s*}' + let s:cmd_pattern1 = 0 return 's1' 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) + " Fold command pattern 2 (e.g. usepackages) + if g:vimtex_fold_cmd_pattern2 + if line =~# '^\s*\\\%('.join(g:vimtex_fold_cmd_pattern2_list, '\|').'\)\s*\[\s*\%($\|%\)' + let s:cmd_pattern2 = 1 return 'a1' - elseif exists('s:newcommand_indent') - \ && indent(a:lnum) == s:newcommand_indent + elseif get(s:, 'cmd_pattern2', 0) && line =~# '^\s*\]{' + let s:cmd_pattern2 = 0 + return 's1' + endif + endif + + " Fold command pattern 3 (e.g. newcommands) + if g:vimtex_fold_cmd_pattern3 + if line =~# '\v^\s*\\%('.join(g:vimtex_fold_cmd_pattern3_list, '|').')\*?' + \ && indent(a:lnum+1) > indent(a:lnum) + let s:cmd_pattern3 = indent(a:lnum) + return 'a1' + elseif exists('s:cmd_pattern3') + \ && indent(a:lnum) == s:cmd_pattern3 \ && line =~# '^\s*}\s*$' - unlet s:newcommand_indent + unlet s:cmd_pattern3 return 's1' endif endif @@ -296,24 +334,35 @@ endfunction function! vimtex#fold#text() " {{{1 let line = getline(v:foldstart) - " Text for usepackage - if g:vimtex_fold_usepackage && line =~# '^\s*\\usepackage' - return '\usepackage[...]{' - \ . vimtex#cmd#get_at(v:foldstart, 1).args[0].text - \ . '}' - endif - + " Text for marker folding if line =~# '%\s*{{{' return ' ' . matchstr(line, '%\s*{{{\s*\zs.*') elseif line =~# '%.*{{{' return ' ' . matchstr(line, '%\s*\zs.*\ze{{{') endif - " Text for newcommand (and similar) - if g:vimtex_fold_newcommands - \ && line =~# '\v^\s*\\%(re)?new%(command|environment)' + " Text for command pattern 1 (e.g. hypersetup) + if g:vimtex_fold_cmd_pattern1 + \ && line =~# '\v^\s*\\%('.join(g:vimtex_fold_cmd_pattern1_list, '|').')' return matchstr(line, - \ '\v^\s*\\%(re)?new%(command|environment)\*?\{[^}]*\}') . ' ...' + \ '\v^\s*\\%('.join(g:vimtex_fold_cmd_pattern1_list, '|').')\*?') . '{...}' + endif + + " Text for command pattern 2 (e.g. usepackage) + if g:vimtex_fold_cmd_pattern2 + \ && line =~# '\v^\s*\\%('.join(g:vimtex_fold_cmd_pattern2_list, '|').')' + return matchstr(line, + \ '\v^\s*\\%('.join(g:vimtex_fold_cmd_pattern2_list, '|').')\*?') + \ .'[...]{' + \ . vimtex#cmd#get_at(v:foldstart, 1).args[0].text + \ . '}' + endif + + " Text for command pattern 3 (e.g. newcommand) + if g:vimtex_fold_cmd_pattern3 + \ && line =~# '\v^\s*\\%('.join(g:vimtex_fold_cmd_pattern3_list, '|').')' + return matchstr(line, + \ '\v^\s*\\%('.join(g:vimtex_fold_cmd_pattern3_list, '|').')\*?\{[^}]*\}') . ' ...' endif " Text for documentclass @@ -371,7 +420,7 @@ function! vimtex#fold#text() " {{{1 let caption = s:parse_caption(line) endif - " Add paranthesis to label + " Add parenthesis to label if label !=# '' let label = substitute(strpart(label,0,nt-ne-2), '\(.*\)', '(\1)','') endif