Some refactoring of TOC

This commit is contained in:
Karl Yngve Lervåg 2014-07-27 20:41:23 +02:00
parent 07bd5f134e
commit 4c83713129

View File

@ -31,12 +31,8 @@ function! latex#toc#open() " {{{1
let calling_file = expand('%:p') let calling_file = expand('%:p')
let calling_line = line('.') let calling_line = line('.')
" Parse tex files for max level " Parse TOC data
let s:count_matters = 0 let toc = s:parse_toc()
let s:max_level = s:set_max_level(g:latex#data[b:latex.id].tex)
" Parse tex files for TOC data
let toc = s:parse_file(g:latex#data[b:latex.id].tex, 1)
" Resize vim session if wanted, then create TOC window " Resize vim session if wanted, then create TOC window
if g:latex_toc_resize if g:latex_toc_resize
@ -94,9 +90,10 @@ function! latex#toc#toggle() " {{{1
silent execute 'wincmd w' silent execute 'wincmd w'
endif endif
endfunction endfunction
" }}}1 " }}}1
" {{{1 TOC number variables " {{{1 TOC variables
let s:max_level = 0 let s:max_level = 0
let s:count_matters = 0 let s:count_matters = 0
@ -157,7 +154,43 @@ let s:re_other = {
" }}}1 " }}}1
function! s:parse_file(file, ...) " {{{1 function! s:parse_toc() " {{{1
let file = g:latex#data[b:latex.id].tex
" Reset TOC numbering
call s:number_reset('preamble')
" Find max level and number of \*matter commands
let s:max_level = 0
let s:count_matters = 0
call s:parse_limits(file)
" Parse TOC data
return s:parse_file(file)
endfunction
" }}}1
function! s:parse_limits(file) " {{{1
if !filereadable(a:file)
echoerr "Error in latex#toc s:get_depth:"
echoerr "File not readable: " . a:file
return ''
endif
for line in readfile(a:file)
if line =~# s:re_input
call s:parse_limits(s:parse_line_input(line))
elseif line =~# s:re_sec
let s:max_level = max([s:max_level,
\ s:sec_to_value[matchstr(line, s:re_sec_level)]])
elseif line =~# s:re_matters
let s:count_matters += 1
endif
endfor
endfunction
" }}}1
function! s:parse_file(file) " {{{1
" Parses tex file for TOC entries " Parses tex file for TOC entries
" "
" The function returns a list of entries. Each entry is a dictionary: " The function returns a list of entries. Each entry is a dictionary:
@ -171,16 +204,11 @@ function! s:parse_file(file, ...) " {{{1
" } " }
if !filereadable(a:file) if !filereadable(a:file)
echoerr "Error in latex#toc s:parse_file:" echoerr "Error in latex#toc s:parse_file"
echoerr "File not readable: " . a:file echoerr "File not readable: " . a:file
return [] return []
endif endif
" Reset TOC numbering
if a:0 > 0
call s:number_reset('preamble')
endif
let toc = [] let toc = []
let lnum = 0 let lnum = 0
@ -348,28 +376,4 @@ endfunction
" }}}1 " }}}1
function! s:set_max_level(file) " {{{1
if !filereadable(a:file)
echoerr "Error in latex#toc s:get_depth:"
echoerr "File not readable: " . a:file
return ''
endif
let n = 0
for line in readfile(a:file)
if line =~# s:re_input
let n = max([n, s:set_max_level(s:parse_line_input(line))])
elseif line =~# s:re_sec
let n = max([n, s:sec_to_value[matchstr(line, s:re_sec_level)]])
elseif line =~# s:re_matters
let s:count_matters += 1
endif
endfor
return n
endfunction
" }}}1
" vim: fdm=marker " vim: fdm=marker