Extend #571: Fold cmds following 3 patterns

with options to assign commands to these patterns
This commit is contained in:
kiryph 2016-10-06 18:41:09 +02:00 committed by Karl Yngve Lervåg
parent fb10a2426c
commit 9c91576f52

View File

@ -27,10 +27,37 @@ function! vimtex#fold#init_options() " {{{1
\ 'subsubsection', \ 'subsubsection',
\ ]) \ ])
call vimtex#util#set_default('g:vimtex_fold_documentclass', 0) 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) call vimtex#util#set_default('g:vimtex_fold_markers', 1)
" Fold command pattern 1: \<command>{<multi-line mandatory arg>}
" folded to '\<command>{...}'
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: \<command>[<multi-line optional arg>]{<short mandatory arg>}
" folded to '\<command>[...]{<short mandatory arg>}'
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: \<command>{short mandatory arg}[]{}{\n}
" folded to '\<command>{<short mandatory arg>} ...'
" 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 " Disable manual mode in vimdiff
let g:vimtex_fold_manual = &diff ? 0 : g:vimtex_fold_manual let g:vimtex_fold_manual = &diff ? 0 : g:vimtex_fold_manual
endfunction endfunction
@ -59,9 +86,10 @@ function! vimtex#fold#init_script() " {{{1
\ '%(front|main|back)matter', \ '%(front|main|back)matter',
\ 'appendix', \ 'appendix',
\ 'part', \ '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 endfunction
" }}}1 " }}}1
@ -130,7 +158,6 @@ function! s:foldmethod_in_modeline()
endfunction endfunction
" }}}1 " }}}1
function! vimtex#fold#refresh(map) " {{{1 function! vimtex#fold#refresh(map) " {{{1
setlocal foldmethod=expr setlocal foldmethod=expr
execute 'normal! ' . a:map execute 'normal! ' . a:map
@ -167,27 +194,38 @@ function! vimtex#fold#level(lnum) " {{{1
return '0' return '0'
endif endif
" Fold usepackages " Fold command pattern 1 (e.g. hypersetup)
if g:vimtex_fold_usepackage if g:vimtex_fold_cmd_pattern1
if line =~# '^\s*\\usepackage\s*\[\s*\%($\|%\)' if line =~# '^\s*\\\%('.join(g:vimtex_fold_cmd_pattern1_list, '\|').'\)\s*{\s*\%($\|%\)'
let s:usepackage = 1 let s:cmd_pattern1 = 1
return 'a1' return 'a1'
elseif get(s:, 'usepackage', 0) && line =~# '^\s*\]{' elseif get(s:, 'cmd_pattern1', 0) && line =~# '^\s*}'
let s:usepackage = 0 let s:cmd_pattern1 = 0
return 's1' return 's1'
endif endif
endif endif
" Fold newcommands (and similar) " Fold command pattern 2 (e.g. usepackages)
if g:vimtex_fold_newcommands if g:vimtex_fold_cmd_pattern2
if line =~# '\v^\s*\\%(re)?new%(command|environment)\*?' if line =~# '^\s*\\\%('.join(g:vimtex_fold_cmd_pattern2_list, '\|').'\)\s*\[\s*\%($\|%\)'
\ && indent(a:lnum+1) > indent(a:lnum) let s:cmd_pattern2 = 1
let s:newcommand_indent = indent(a:lnum)
return 'a1' return 'a1'
elseif exists('s:newcommand_indent') elseif get(s:, 'cmd_pattern2', 0) && line =~# '^\s*\]{'
\ && indent(a:lnum) == s:newcommand_indent 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*$' \ && line =~# '^\s*}\s*$'
unlet s:newcommand_indent unlet s:cmd_pattern3
return 's1' return 's1'
endif endif
endif endif
@ -296,24 +334,35 @@ endfunction
function! vimtex#fold#text() " {{{1 function! vimtex#fold#text() " {{{1
let line = getline(v:foldstart) let line = getline(v:foldstart)
" Text for usepackage " Text for marker folding
if g:vimtex_fold_usepackage && line =~# '^\s*\\usepackage'
return '\usepackage[...]{'
\ . vimtex#cmd#get_at(v:foldstart, 1).args[0].text
\ . '}'
endif
if line =~# '%\s*{{{' if line =~# '%\s*{{{'
return ' ' . matchstr(line, '%\s*{{{\s*\zs.*') return ' ' . matchstr(line, '%\s*{{{\s*\zs.*')
elseif line =~# '%.*{{{' elseif line =~# '%.*{{{'
return ' ' . matchstr(line, '%\s*\zs.*\ze{{{') return ' ' . matchstr(line, '%\s*\zs.*\ze{{{')
endif endif
" Text for newcommand (and similar) " Text for command pattern 1 (e.g. hypersetup)
if g:vimtex_fold_newcommands if g:vimtex_fold_cmd_pattern1
\ && line =~# '\v^\s*\\%(re)?new%(command|environment)' \ && line =~# '\v^\s*\\%('.join(g:vimtex_fold_cmd_pattern1_list, '|').')'
return matchstr(line, 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 endif
" Text for documentclass " Text for documentclass
@ -371,7 +420,7 @@ function! vimtex#fold#text() " {{{1
let caption = s:parse_caption(line) let caption = s:parse_caption(line)
endif endif
" Add paranthesis to label " Add parenthesis to label
if label !=# '' if label !=# ''
let label = substitute(strpart(label,0,nt-ne-2), '\(.*\)', '(\1)','') let label = substitute(strpart(label,0,nt-ne-2), '\(.*\)', '(\1)','')
endif endif