vimtex/autoload/latex/fold.vim

305 lines
8.2 KiB
VimL
Raw Normal View History

2013-10-05 07:53:42 -04:00
" {{{1 latex#fold#init
function! latex#fold#init(initialized)
if g:latex_fold_enabled
setl foldmethod=expr
setl foldexpr=latex#fold#level(v:lnum)
setl foldtext=latex#fold#text()
call latex#fold#refresh()
2013-10-10 15:48:52 -04:00
if g:latex_mappings_enabled
2013-10-05 07:53:42 -04:00
nnoremap <silent><buffer> zx :call latex#fold#refresh()<cr>zx
endif
2013-10-15 04:19:00 -04:00
"
" For some reason, foldmethod=expr makes undo slow (at least in some cases)
"
nnoremap <silent><buffer> u :call FdmSave()<cr>u:call FdmRestore()<cr>
2013-10-05 07:53:42 -04:00
"
" The foldexpr function returns "=" for most lines, which means it can
" become slow for large files. The following is a hack that is based on
" this reply to a discussion on the Vim Developer list:
" http://permalink.gmane.org/gmane.editors.vim.devel/14100
"
if !a:initialized
augroup latex_fold
autocmd!
2013-10-15 04:19:00 -04:00
autocmd InsertEnter *.tex call FdmSave()
autocmd InsertLeave *.tex call FdmRestore()
2013-10-05 07:53:42 -04:00
augroup END
endif
endif
endfunction
" {{{1 latex#fold#refresh
function! latex#fold#refresh()
" Parse tex file to dynamically set the sectioning fold levels
let b:latex.fold_sections = s:find_fold_sections()
endfunction
" {{{1 latex#fold#level
function! latex#fold#level(lnum)
" Check for normal lines first (optimization)
let line = getline(a:lnum)
if line !~ '\(% Fake\|\\\(document\|begin\|end\|'
\ . 'front\|main\|back\|app\|sub\|section\|chapter\|part\)\)'
return "="
endif
" Fold preamble
if g:latex_fold_preamble
if line =~# '\s*\\documentclass'
return ">1"
elseif line =~# '^\s*\\begin\s*{\s*document\s*}'
return "0"
endif
endif
" Fold parts (\frontmatter, \mainmatter, \backmatter, and \appendix)
if line =~# '^\s*\\\%(' . join(g:latex_fold_parts, '\|') . '\)'
return ">1"
endif
" Fold chapters and sections
for [part, level] in b:latex.fold_sections
if line =~# part
return ">" . level
endif
endfor
" Never fold \end{document}
if line =~# '^\s*\\end{document}'
return 0
endif
" Fold environments
if g:latex_fold_envs
2013-10-10 15:48:52 -04:00
if line =~# s:notcomment . s:notbslash . '\\begin\s*{.\{-}}'
2013-10-05 07:53:42 -04:00
return "a1"
2013-10-10 15:48:52 -04:00
elseif line =~# s:notcomment . s:notbslash . '\\end\s*{.\{-}}'
2013-10-05 07:53:42 -04:00
return "s1"
endif
endif
" Return foldlevel of previous line
return "="
endfunction
" {{{1 latex#fold#text
function! latex#fold#text()
" Initialize
let line = getline(v:foldstart)
2013-10-16 04:01:22 -04:00
let level = v:foldlevel > 1 ? repeat('-', v:foldlevel-2) . '*' : ''
2013-10-05 07:53:42 -04:00
let title = 'Not defined'
2013-10-16 04:01:22 -04:00
let nt = 73
2013-10-05 07:53:42 -04:00
2013-10-16 04:01:22 -04:00
" Preamble, parts, sections and fakesections
2013-10-05 07:53:42 -04:00
let sections = '\(\(sub\)*section\|part\|chapter\)'
let secpat1 = '^\s*\\' . sections . '\*\?\s*{'
let secpat2 = '^\s*\\' . sections . '\*\?\s*\['
2013-10-16 04:01:22 -04:00
if line =~ '\s*\\documentclass'
let title = "Preamble"
elseif line =~ '\\frontmatter'
2013-10-05 07:53:42 -04:00
let title = "Frontmatter"
elseif line =~ '\\mainmatter'
let title = "Mainmatter"
elseif line =~ '\\backmatter'
let title = "Backmatter"
elseif line =~ '\\appendix'
let title = "Appendix"
elseif line =~ secpat1 . '.*}'
let title = matchstr(line, secpat1 . '\zs.*\ze}')
elseif line =~ secpat1
let title = matchstr(line, secpat1 . '\zs.*')
elseif line =~ secpat2 . '.*\]'
let title = matchstr(line, secpat2 . '\zs.*\ze\]')
elseif line =~ secpat2
let title = matchstr(line, secpat2 . '\zs.*')
elseif line =~ 'Fake' . sections . ':'
let title = matchstr(line,'Fake' . sections . ':\s*\zs.*')
elseif line =~ 'Fake' . sections
let title = matchstr(line, 'Fake' . sections)
endif
" Environments
if line =~ '\\begin'
" Capture environment name
let env = matchstr(line,'\\begin\*\?{\zs\w*\*\?\ze}')
2013-10-16 04:01:22 -04:00
let ne = 12
2013-10-05 07:53:42 -04:00
" Set caption/label based on type of environment
if env == 'frame'
let label = ''
let caption = s:parse_caption_frame(line)
elseif env == 'table'
let label = s:parse_label()
let caption = s:parse_caption_table(line)
else
let label = s:parse_label()
let caption = s:parse_caption(line)
endif
" Add paranthesis to label
if label != ''
2013-10-16 04:01:22 -04:00
let label = substitute(strpart(label,0,nt-ne-2), '\(.*\)', '(\1)','')
2013-10-05 07:53:42 -04:00
endif
" Set size of label and caption part of string
2013-10-16 04:01:22 -04:00
let nl = len(label) > nt - ne ? nt - ne : len(label)
let nc = nt - ne - nl - 1
2013-10-05 07:53:42 -04:00
let caption = strpart(caption, 0, nc)
" Create title based on env, caption and label
2013-10-16 04:01:22 -04:00
let title = printf('%-' . ne . 's%-' . nc . 's %' . nl . 's',
2013-10-05 07:53:42 -04:00
\ env, caption, label)
endif
2013-10-16 04:01:22 -04:00
let title = strpart(title, 0, nt)
return printf('%-5s %-' . nt . 's', level, title)
2013-10-05 07:53:42 -04:00
endfunction
" }}}1
2013-10-15 04:19:00 -04:00
" {{{1 FdmRestore
function! FdmRestore()
let &l:foldmethod = s:fdm
endfunction
2013-10-15 04:19:00 -04:00
" {{{1 FdmSave
let s:fdm=''
2013-10-15 04:19:00 -04:00
function! FdmSave()
let s:fdm = &l:foldmethod
setlocal foldmethod=manual
endfunction
2013-10-15 04:19:00 -04:00
" }}}1
" {{{1 s:notbslash and s:notcomment
let s:notbslash = '\%(\\\@<!\%(\\\\\)*\)\@<='
let s:notcomment = '\%(\%(\\\@<!\%(\\\\\)*\)\@<=%.*\)\@<!'
2013-10-05 07:53:42 -04:00
" {{{1 s:find_fold_sections
function! s:find_fold_sections()
"
" This function parses the tex file to find the sections that are to be
" folded and their levels, and then predefines the patterns for optimized
" folding.
"
" Initialize
let level = 1
let foldsections = []
" If we use two or more of the *matter commands, we need one more foldlevel
let nparts = 0
for part in g:latex_fold_parts
let i = 1
while i < line("$")
if getline(i) =~ '^\s*\\' . part . '\>'
let nparts += 1
break
endif
let i += 1
endwhile
if nparts > 1
let level = 2
break
endif
endfor
" Combine sections and levels, but ignore unused section commands: If we
" don't use the part command, then chapter should have the highest
" level. If we don't use the chapter command, then section should be the
" highest level. And so on.
let ignore = 1
for part in g:latex_fold_sections
" For each part, check if it is used in the file. We start adding the
" part patterns to the fold sections array whenever we find one.
let partpattern = '^\s*\(\\\|% Fake\)' . part . '\>'
if ignore
let i = 1
while i < line("$")
if getline(i) =~# partpattern
call insert(foldsections, [partpattern, level])
let level += 1
let ignore = 0
break
endif
let i += 1
endwhile
else
call insert(foldsections, [partpattern, level])
let level += 1
endif
endfor
return foldsections
endfunction
" {{{1 s:parse_label
function! s:parse_label()
let i = v:foldend
while i >= v:foldstart
if getline(i) =~ '^\s*\\label'
return matchstr(getline(i), '^\s*\\label{\zs.*\ze}')
end
let i -= 1
endwhile
return ""
endfunction
" {{{1 s:parse_caption
function! s:parse_caption(line)
let i = v:foldend
while i >= v:foldstart
if getline(i) =~ '^\s*\\caption'
return matchstr(getline(i),
\ '^\s*\\caption\(\[.*\]\)\?{\zs.\{-1,}\ze\(}\s*\)\?$')
end
let i -= 1
endwhile
" If no caption found, check for a caption comment
return matchstr(a:line,'\\begin\*\?{.*}\s*%\s*\zs.*')
endfunction
" {{{1 s:parse_caption_table
function! s:parse_caption_table(line)
let i = v:foldstart
while i <= v:foldend
if getline(i) =~ '^\s*\\caption'
return matchstr(getline(i),
\ '^\s*\\caption\(\[.*\]\)\?{\zs.\{-1,}\ze\(}\s*\)\?$')
end
let i += 1
endwhile
" If no caption found, check for a caption comment
return matchstr(a:line,'\\begin\*\?{.*}\s*%\s*\zs.*')
endfunction
" {{{1 s:parse_caption_frame
function! s:parse_caption_frame(line)
" Test simple variants first
let caption1 = matchstr(a:line,'\\begin\*\?{.*}{\zs.\+\ze}')
let caption2 = matchstr(a:line,'\\begin\*\?{.*}{\zs.\+')
if len(caption1) > 0
return caption1
elseif len(caption2) > 0
return caption2
else
let i = v:foldstart
while i <= v:foldend
if getline(i) =~ '^\s*\\frametitle'
return matchstr(getline(i),
\ '^\s*\\frametitle\(\[.*\]\)\?{\zs.\{-1,}\ze\(}\s*\)\?$')
end
let i += 1
endwhile
" If no caption found, check for a caption comment
return matchstr(a:line,'\\begin\*\?{.*}\s*%\s*\zs.*')
endif
endfunction
" }}}1
" vim:fdm=marker:ff=unix