Improve vimscript code

This commit is contained in:
Karl Yngve Lervåg 2015-03-24 14:32:49 +01:00
parent 9f4cbead8c
commit f77df6eb6a

View File

@ -14,18 +14,18 @@ function! vimtex#fold#init(initialized) " {{{1
call vimtex#util#set_default('g:vimtex_fold_envs', 1) call vimtex#util#set_default('g:vimtex_fold_envs', 1)
call vimtex#util#set_default('g:vimtex_fold_parts', call vimtex#util#set_default('g:vimtex_fold_parts',
\ [ \ [
\ "part", \ 'part',
\ "appendix", \ 'appendix',
\ "frontmatter", \ 'frontmatter',
\ "mainmatter", \ 'mainmatter',
\ "backmatter", \ 'backmatter',
\ ]) \ ])
call vimtex#util#set_default('g:vimtex_fold_sections', call vimtex#util#set_default('g:vimtex_fold_sections',
\ [ \ [
\ "chapter", \ 'chapter',
\ "section", \ 'section',
\ "subsection", \ 'subsection',
\ "subsubsection", \ 'subsubsection',
\ ]) \ ])
" Define some script variables " Define some script variables
@ -87,22 +87,22 @@ function! vimtex#fold#level(lnum) " {{{1
let line = getline(a:lnum) let line = getline(a:lnum)
if line !~ '\(% Fake\|\\\(document\|begin\|end\|' if line !~ '\(% Fake\|\\\(document\|begin\|end\|'
\ . 'front\|main\|back\|app\|sub\|section\|chapter\|part\)\)' \ . 'front\|main\|back\|app\|sub\|section\|chapter\|part\)\)'
return "=" return '='
endif endif
" Fold preamble " Fold preamble
if g:vimtex_fold_preamble if g:vimtex_fold_preamble
if line =~# '^\s*\\documentclass' if line =~# '^\s*\\documentclass'
return ">1" return '>1'
elseif line =~# '^\s*\\begin\s*{\s*document\s*}' elseif line =~# '^\s*\\begin\s*{\s*document\s*}'
return "0" return '0'
endif endif
endif endif
" Fold chapters and sections " Fold chapters and sections
for [part, level] in b:vimtex.fold_parts for [part, level] in b:vimtex.fold_parts
if line =~# part if line =~# part
return ">" . level return '>' . level
endif endif
endfor endfor
@ -115,17 +115,17 @@ function! vimtex#fold#level(lnum) " {{{1
if g:vimtex_fold_envs if g:vimtex_fold_envs
if line =~# s:notcomment . s:notbslash . '\\begin\s*{.\{-}}' if line =~# s:notcomment . s:notbslash . '\\begin\s*{.\{-}}'
if line !~# '\\end' if line !~# '\\end'
return "a1" return 'a1'
endif endif
elseif line =~# s:notcomment . s:notbslash . '\\end\s*{.\{-}}' elseif line =~# s:notcomment . s:notbslash . '\\end\s*{.\{-}}'
if line !~# '\\begin' if line !~# '\\begin'
return "s1" return 's1'
endif endif
endif endif
endif endif
" Return foldlevel of previous line " Return foldlevel of previous line
return "=" return '='
endfunction endfunction
function! vimtex#fold#text() " {{{1 function! vimtex#fold#text() " {{{1
@ -139,16 +139,16 @@ function! vimtex#fold#text() " {{{1
let sections = '\(\(sub\)*section\|part\|chapter\)' let sections = '\(\(sub\)*section\|part\|chapter\)'
let secpat1 = '^\s*\\' . sections . '\*\?\s*{' let secpat1 = '^\s*\\' . sections . '\*\?\s*{'
let secpat2 = '^\s*\\' . sections . '\*\?\s*\[' let secpat2 = '^\s*\\' . sections . '\*\?\s*\['
if line =~ '\s*\\documentclass' if line =~# '\s*\\documentclass'
let title = "Preamble" let title = 'Preamble'
elseif line =~ '\\frontmatter' elseif line =~# '\\frontmatter'
let title = "Frontmatter" let title = 'Frontmatter'
elseif line =~ '\\mainmatter' elseif line =~# '\\mainmatter'
let title = "Mainmatter" let title = 'Mainmatter'
elseif line =~ '\\backmatter' elseif line =~# '\\backmatter'
let title = "Backmatter" let title = 'Backmatter'
elseif line =~ '\\appendix' elseif line =~# '\\appendix'
let title = "Appendix" let title = 'Appendix'
elseif line =~ secpat1 . '.*}' elseif line =~ secpat1 . '.*}'
let title = matchstr(line, secpat1 . '\zs.*\ze}') let title = matchstr(line, secpat1 . '\zs.*\ze}')
elseif line =~ secpat1 elseif line =~ secpat1
@ -164,16 +164,16 @@ function! vimtex#fold#text() " {{{1
endif endif
" Environments " Environments
if line =~ '\\begin' if line =~# '\\begin'
" Capture environment name " Capture environment name
let env = matchstr(line,'\\begin\*\?{\zs\w*\*\?\ze}') let env = matchstr(line,'\\begin\*\?{\zs\w*\*\?\ze}')
let ne = 12 let ne = 12
" Set caption/label based on type of environment " Set caption/label based on type of environment
if env == 'frame' if env ==# 'frame'
let label = '' let label = ''
let caption = s:parse_caption_frame(line) let caption = s:parse_caption_frame(line)
elseif env == 'table' elseif env ==# 'table'
let label = s:parse_label() let label = s:parse_label()
let caption = s:parse_caption_table(line) let caption = s:parse_caption_table(line)
else else
@ -182,7 +182,7 @@ function! vimtex#fold#text() " {{{1
endif endif
" Add paranthesis to label " Add paranthesis 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
@ -274,18 +274,18 @@ endfunction
function! s:parse_label() " {{{1 function! s:parse_label() " {{{1
let i = v:foldend let i = v:foldend
while i >= v:foldstart while i >= v:foldstart
if getline(i) =~ '^\s*\\label' if getline(i) =~# '^\s*\\label'
return matchstr(getline(i), '^\s*\\label{\zs.*\ze}') return matchstr(getline(i), '^\s*\\label{\zs.*\ze}')
end end
let i -= 1 let i -= 1
endwhile endwhile
return "" return ''
endfunction endfunction
function! s:parse_caption(line) " {{{1 function! s:parse_caption(line) " {{{1
let i = v:foldend let i = v:foldend
while i >= v:foldstart while i >= v:foldstart
if getline(i) =~ '^\s*\\caption' if getline(i) =~# '^\s*\\caption'
return matchstr(getline(i), return matchstr(getline(i),
\ '^\s*\\caption\(\[.*\]\)\?{\zs.\{-1,}\ze\(}\s*\)\?$') \ '^\s*\\caption\(\[.*\]\)\?{\zs.\{-1,}\ze\(}\s*\)\?$')
end end
@ -299,7 +299,7 @@ endfunction
function! s:parse_caption_table(line) " {{{1 function! s:parse_caption_table(line) " {{{1
let i = v:foldstart let i = v:foldstart
while i <= v:foldend while i <= v:foldend
if getline(i) =~ '^\s*\\caption' if getline(i) =~# '^\s*\\caption'
return matchstr(getline(i), return matchstr(getline(i),
\ '^\s*\\caption\(\[.*\]\)\?{\zs.\{-1,}\ze\(}\s*\)\?$') \ '^\s*\\caption\(\[.*\]\)\?{\zs.\{-1,}\ze\(}\s*\)\?$')
end end
@ -322,7 +322,7 @@ function! s:parse_caption_frame(line) " {{{1
else else
let i = v:foldstart let i = v:foldstart
while i <= v:foldend while i <= v:foldend
if getline(i) =~ '^\s*\\frametitle' if getline(i) =~# '^\s*\\frametitle'
return matchstr(getline(i), return matchstr(getline(i),
\ '^\s*\\frametitle\(\[.*\]\)\?{\zs.\{-1,}\ze\(}\s*\)\?$') \ '^\s*\\frametitle\(\[.*\]\)\?{\zs.\{-1,}\ze\(}\s*\)\?$')
end end