Skip to content

Commit

Permalink
Version 0.9.2: fix TOC bug with amsart
Browse files Browse the repository at this point in the history
  • Loading branch information
mungerd authored and vim-scripts committed Oct 18, 2010
1 parent 893602c commit e46a8f4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
4 changes: 3 additions & 1 deletion doc/latex-box.txt
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ A: Instead of putting the settings in your ~/.latexmkrc file, put them in your

TODO *latex-box-todo*

- Automatically find the main TeX document
- Automatically find the main TeX document.
- Improve TOC jumping and filter out weird characters. Deal with multiple
sections with the same name.
- Fix bugs?

==============================================================================
Expand Down
44 changes: 44 additions & 0 deletions ftplugin/latex-box/common.vim
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,48 @@ function! LatexBox_GetCurrentEnvironment(...)
endfunction
" }}}


" Tex To Tree {{{
" stores nested braces in a tree structure
function! LatexBox_TexToTree(str)
let tree = []
let i1 = 0
let i2 = -1
let depth = 0
while i2 < len(a:str)
let i2 = match(a:str, '[{}]', i2 + 1)
if i2 < 0
let i2 = len(a:str)
endif
if i2 >= len(a:str) || a:str[i2] == '{'
if depth == 0
let item = substitute(strpart(a:str, i1, i2 - i1), '^\s*\|\s*$', '', 'g')
if !empty(item)
call add(tree, item)
endif
let i1 = i2 + 1
endif
let depth += 1
else
let depth -= 1
if depth == 0
call add(tree, LatexBox_TexToTree(strpart(a:str, i1, i2 - i1)))
let i1 = i2 + 1
endif
endif
endwhile
return tree
endfunction
" }}}

" Tree To Tex {{{
function! LatexBox_TreeToTex(tree)
if type(a:tree) == type('')
return a:tree
else
return '{' . join(map(a:tree, 'LatexBox_TreeToTex(v:val)'), '') . '}'
endif
endfunction
" }}}

" vim:fdm=marker:ff=unix:noet:ts=4:sw=4
38 changes: 17 additions & 21 deletions ftplugin/latex-box/motion.vim
Original file line number Diff line number Diff line change
Expand Up @@ -228,29 +228,25 @@ function! s:ReadTOC(auxfile)
continue
endif

let m = matchlist(line,
\ '^\\@writefile{toc}{\\contentsline\s*' .
\ '{\([^}]*\)}{\\numberline {\([^}]*\)}\(.*\)')

if !empty(m)
let str = m[3]
let nbraces = 0
let istr = 0
while nbraces >= 0 && istr < len(str)
if str[istr] == '{'
let nbraces += 1
elseif str[istr] == '}'
let nbraces -= 1
endif
let istr += 1
endwhile
let text = str[:(istr-2)]
let page = matchstr(str[(istr):], '{\([^}]*\)}')

call add(toc, {'file': fnamemodify(a:auxfile, ':r') . '.tex',
\ 'level': m[1], 'number': m[2], 'text': text, 'page': page})
" Parse lines like:
" \@writefile{toc}{\contentsline {section}{\numberline {secnum}Section Title}{pagenumber}}
" \@writefile{toc}{\contentsline {section}{\tocsection {}{1}{Section Title}}{pagenumber}}
" \@writefile{toc}{\contentsline {section}{\numberline {secnum}Section Title}{pagenumber}{otherstuff}}

let line = matchstr(line, '^\\@writefile{toc}{\\contentsline\s*\zs.*\ze}\s*$')
if empty(line)
continue
endif

let tree = LatexBox_TexToTree(line)
if empty(tree[1][1])
call remove(tree[1], 1)
endif
let text = LatexBox_TreeToTex(tree[1][2:])
let text = substitute(text, '^{\+\|}\+$', '', 'g')

call add(toc, {'file': fnamemodify(a:auxfile, ':r') . '.tex',
\ 'level': tree[0][0], 'number': tree[1][1][0], 'text': text, 'page': tree[2][0]})
endfor

return toc
Expand Down

0 comments on commit e46a8f4

Please sign in to comment.