Getting closer and closer ...

This commit is contained in:
Karl Yngve Lervåg 2014-07-08 12:32:41 +02:00
parent 17175b2351
commit 1e3f10ee58

View File

@ -21,9 +21,7 @@ function! latex#toc#open()
let calling_line = line('.') let calling_line = line('.')
" Parse tex files for TOC data " Parse tex files for TOC data
let toc = s:parse_file(g:latex#data[b:latex.id].tex) let toc = s:parse_file(g:latex#data[b:latex.id].tex, 1)
PP(toc)
return 0
" 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
@ -82,7 +80,7 @@ endfunction
" }}}1 " }}}1
" {{{1 s:parse_file " {{{1 s:parse_file
function! s:parse_file(file) function! s:parse_file(file, ...)
" 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:
@ -101,59 +99,40 @@ function! s:parse_file(file)
return [] return []
endif endif
" Set limits for TOC numbering (only for the first call)
if a:0 > 0
let s:number = {}
endif
let toc = [] let toc = []
let lnum = 0 let lnum = 0
for line in readfile(a:file) for line in readfile(a:file)
let lnum += 1 let lnum += 1
" 1. input or include " 1. Parse inputs or includes
if s:test_input(line) if line =~# s:re_input
call extend(toc, s:parse_file(s:parse_line_input(line))) call extend(toc, s:parse_file(s:parse_line_input(line)))
"call add(toc, s:parse_line_input(line))
continue continue
endif endif
" 2. sections, subsections, paragraphs " 2. Parse chapters, sections, and subsections
if s:test_sec(line) if line =~# s:re_sec
call add(toc, s:parse_line_sec(a:file, lnum, line)) call add(toc, s:parse_line_sec(a:file, lnum, line))
continue continue
endif endif
" 3. misc (parts, preamble, etc.)
if s:test_misc(line)
call add(toc, s:parse_line_misc(a:file, lnum, line))
continue
endif
endfor endfor
return toc return toc
endfunction endfunction
"}}}1 "}}}1
" {{{1 s:test_input
function! s:test_input(line)
return a:line =~# '\v^\s*\\%(input|include)\s*\{'
endfunction
" }}}1
" {{{1 s:test_sec
function! s:test_sec(line)
return a:line =~# '\v^\s*\\%(chapter|%(sub)*section|paragraph)\s*\{'
endfunction
" }}}1
" {{{1 s:test_misc
function! s:test_misc(line)
return a:line =~# '\v^\s*\\%(frontmatter|appendix|part|documentclass)'
endfunction
" }}}1
" {{{1 s:parse_line_input " {{{1 s:parse_line_input
let s:re_input = '\v^\s*\\%(input|include)\s*\{'
let s:re_input_file = s:re_input . '\zs[^\}]+\ze}'
function! s:parse_line_input(line) function! s:parse_line_input(line)
let l:file = matchstr(a:line, '\v%(input|include)\s*\{\zs[^\}]+\ze}') let l:file = matchstr(a:line, s:re_input_file)
if l:file !~# '.tex$' if l:file !~# '.tex$'
let l:file .= '.tex' let l:file .= '.tex'
endif endif
@ -162,35 +141,36 @@ endfunction
" }}}1 " }}}1
" {{{1 s:parse_line_sec " {{{1 s:parse_line_sec
let s:re_sec = '\v^\s*\\%(chapter|%(sub)*section)\*?\s*\{'
let s:re_sec_level = '\v^\s*\\\zs%(chapter|%(sub)*section)\*?'
let s:re_sec_title = s:re_sec . '\zs.{-}\ze\}?$'
function! s:parse_line_sec(file, lnum, line) function! s:parse_line_sec(file, lnum, line)
let title = matchstr(a:line, s:re_sec_title)
let level = matchstr(a:line, s:re_sec_level)
return { return {
\ 'title' : "sec", \ 'title' : title,
\ 'number' : "todo", \ 'number' : s:get_number(level),
\ 'file' : a:file, \ 'file' : a:file,
\ 'line' : a:lnum, \ 'line' : a:lnum,
\ } \ }
endfunction endfunction
" }}}1 function! s:get_number(level)
" {{{1 s:parse_line_misc if a:level =~# '\v%(part|chapter|(sub)*section)$'
function! s:parse_line_misc(file, lnum, line) if has_key(s:number, a:level)
return { let s:number.a:level += 1
\ 'title' : "misc", else
\ 'number' : "todo", let s:number.a:level = 1
\ 'file' : a:file, endif
\ 'line' : a:lnum, else
\ } return ''
endif
return PP(s:number)
endfunction endfunction
" }}}1 " }}}1
" {{{1 s:toc_escape_title
function! s:toc_escape_title(titlestr)
let titlestr = substitute(a:titlestr, '\\[a-zA-Z@]*\>\s*{\?', '.*', 'g')
let titlestr = substitute(titlestr, '}', '', 'g')
let titlestr = substitute(titlestr, '\%(\.\*\s*\)\{2,}', '.*', 'g')
return titlestr
endfunction
" }}}1
" vim: fdm=marker " vim: fdm=marker