我使用Latex-Box
但我真的很喜欢vim-latex
(又名 Latex-Suite)折叠。我尝试将 Latex-Suite 的folding.vim
文件复制到我的ftplugin/tex
文件夹中但无法使其工作。有没有人写过一个不错的折叠函数或使用 Latex-Suite 折叠函数而无需 Latex-Suite 完整插件?
答案1
我已经Latex-Box
通过两种方式修改了(优秀)折叠:i)因为我只喜欢折叠部分(部分、章节等),抽象环境和框架(在 beamer 类中)我添加了一个新变量 g:LatexBox_folded_environments
来控制折叠哪些环境 ii)我将其更改LatexBox_FoldText()
为类似于Latex-Suite
。
您应该将此文件保存为,ftplugin/tex/folding.vim
这样它就可以立即使用。
" Initialization {{{
if exists('b:loaded_mylatexfolding')
finish
endif
let b:loaded_mylatexfolding=1
" }}}
" Set options {{{
setlocal foldmethod=expr
setlocal foldexpr=LatexBox_FoldLevel(v:lnum)
setlocal foldtext=LatexBox_FoldText()
if !exists('g:LatexBox_fold_preamble')
let g:LatexBox_fold_preamble=1
endif
if !exists('g:LatexBox_fold_parts')
let g:LatexBox_fold_parts=[
\ "appendix",
\ "frontmatter",
\ "mainmatter",
\ "backmatter"
\ ]
endif
if !exists('g:LatexBox_fold_sections')
let g:LatexBox_fold_sections=[
\ "part",
\ "chapter",
\ "section",
\ "subsection",
\ "subsubsection"
\ ]
endif
if !exists('g:LatexBox_fold_envs')
let g:LatexBox_fold_envs=1
endif
if !exists('g:LatexBox_folded_environments')
let g:LatexBox_folded_environments = [
\ "abstract",
\ "frame"
\ ]
endif
" }}}
" LatexBox_FoldLevel helper functions {{{
" 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.
function! s:FoldSectionLevels()
" 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:LatexBox_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:LatexBox_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
" }}}
" LatexBox_FoldLevel {{{
" Parse file to dynamically set the sectioning fold levels
let b:LatexBox_FoldSections = s:FoldSectionLevels()
" Optimize by predefine common patterns
let s:foldparts = '^\s*\\\%(' . join(g:LatexBox_fold_parts, '\|') . '\)'
let s:folded = '\(% Fake\|\\\(document\|begin\|end\|'
\ . 'front\|main\|back\|app\|sub\|section\|chapter\|part\)\)'
" Fold certain selected environments
let s:notbslash = '\%(\\\@<!\%(\\\\\)*\)\@<='
let s:notcomment = '\%(\%(\\\@<!\%(\\\\\)*\)\@<=%.*\)\@<!'
let s:envbeginpattern = s:notcomment . s:notbslash .
\ '\\begin\s*{\('. join(g:LatexBox_folded_environments, '\|') .'\)}'
let s:envendpattern = s:notcomment . s:notbslash .
\ '\\end\s*{\('. join(g:LatexBox_folded_environments, '\|') . '\)}'
function! LatexBox_FoldLevel(lnum)
" Check for normal lines first (optimization)
let line = getline(a:lnum)
if line !~ s:folded
return "="
endif
" Fold preamble
if g:LatexBox_fold_preamble == 1
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:foldparts
return ">1"
endif
" Fold chapters and sections
for [part, level] in b:LatexBox_FoldSections
if line =~# part
return ">" . level
endif
endfor
" Fold environments
if g:LatexBox_fold_envs == 1
if line =~# s:envbeginpattern
return "a1"
elseif line =~# '^\s*\\end{document}'
" Never fold \end{document}
return 0
elseif line =~# s:envendpattern
return "s1"
endif
endif
" Return foldlevel of previous line
return "="
endfunction
" }}}
" LatexBox_FoldText helper functions {{{
function! s:CaptionFrame(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.\+')
end
let i += 1
endwhile
return ""
endif
endfunction
" }}}
" LatexBox_FoldText {{{
function! LatexBox_FoldText()
" Initialize
let line = getline(v:foldstart)
let nlines = v:foldend - v:foldstart + 1
let level = ''
let title = 'Not defined'
" Fold level and number of lines
let level = '+-' . repeat('-', v:foldlevel-1) . ' '
let alignlnr = repeat(' ', 6-(v:foldlevel-1)-len(nlines))
let lineinfo = nlines . ' lines: '
" Preamble
if line =~ '\s*\\documentclass'
let title = "Preamble"
endif
" Parts, sections and fakesections
let sections = '\(\(sub\)*section\|part\|chapter\)'
let secpat1 = '^\s*\\' . sections . '\*\?\s*{'
let secpat2 = '^\s*\\' . sections . '\*\?\s*\['
if line =~ '\\frontmatter'
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 = line
elseif line =~ secpat1
let title = line
elseif line =~ secpat2 . '.*\]'
let title = line
elseif line =~ secpat2
let title = line
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}')
if env == 'abstract'
let title = 'Abstract'
elseif env == 'frame'
let caption = s:CaptionFrame(line)
let title = 'Frame - ' . substitute(caption, '}\s*$', '','')
endif
endif
return level . alignlnr . lineinfo . title . ' '
endfunction
" }}}
如果您想要折叠图形、表格和其他环境,您应该添加函数LabelEnv()
,CaptionEnv()
并CaptionTable()
从中LaTeX-Box/ftplugin/latex-box/folding.vim
修改函数LatexBox_FoldText()
以包含所提到的函数,就像在 Latex-Box 折叠中完成的那样(但将格式更改为适合您需要的格式)。