vimtex/autoload/latex/toc.vim

275 lines
6.7 KiB
VimL
Raw Normal View History

function! latex#toc#init(initialized) " {{{1
if !g:latex_toc_enabled | return | endif
" Define commands
command! -buffer VimLatexTocOpen call latex#toc#open()
command! -buffer VimLatexTocToggle call latex#toc#toggle()
" Define mappings
if g:latex_mappings_enabled
nnoremap <buffer> <LocalLeader>lt :call latex#toc#open()<cr>
nnoremap <buffer> <LocalLeader>lT :call latex#toc#toggle()<cr>
2013-10-05 07:53:42 -04:00
endif
endfunction
function! latex#toc#open() " {{{1
2014-07-04 14:15:28 -04:00
" Go to TOC if it already exists
2013-10-05 07:53:42 -04:00
let winnr = bufwinnr(bufnr('LaTeX TOC'))
if winnr >= 0
silent execute winnr . 'wincmd w'
return
endif
2014-07-04 14:15:28 -04:00
" Store current buffer number and position
2013-10-05 07:53:42 -04:00
let calling_buf = bufnr('%')
let calling_file = expand('%:p')
2014-07-04 14:15:28 -04:00
let calling_line = line('.')
2014-07-01 16:34:26 -04:00
2014-07-04 14:15:28 -04:00
" Parse tex files for TOC data
2014-07-08 06:32:41 -04:00
let toc = s:parse_file(g:latex#data[b:latex.id].tex, 1)
2014-07-04 14:15:28 -04:00
" Resize vim session if wanted, then create TOC window
2013-10-05 07:53:42 -04:00
if g:latex_toc_resize
silent exe "set columns+=" . g:latex_toc_width
endif
2014-07-01 16:34:26 -04:00
silent exe g:latex_toc_split_side g:latex_toc_width . 'vnew LaTeX\ TOC'
2014-07-04 14:15:28 -04:00
" Set buffer local variables
2014-07-01 16:34:26 -04:00
let b:toc = toc
let b:toc_numbers = 1
let b:calling_win = bufwinnr(calling_buf)
2014-07-15 07:41:12 -04:00
" Add TOC entries (and keep track of closest index)
2014-07-04 14:15:28 -04:00
let index = 0
let closest_index = 0
2014-07-01 16:34:26 -04:00
for entry in toc
2014-07-04 14:15:28 -04:00
call append('$', entry.number . "\t" . entry.title)
2014-07-15 07:41:12 -04:00
2014-07-04 14:15:28 -04:00
let index += 1
2014-07-15 07:41:12 -04:00
if entry.file == calling_file && entry.line <= calling_line
let closest_index = index
2014-07-04 14:15:28 -04:00
endif
2014-07-01 16:34:26 -04:00
endfor
2013-10-05 07:53:42 -04:00
2014-07-04 14:15:28 -04:00
" Add help info (if desired)
2013-10-05 07:53:42 -04:00
if !g:latex_toc_hide_help
call append('$', "")
call append('$', "<Esc>/q: close")
call append('$', "<Space>: jump")
call append('$', "<Enter>: jump and close")
call append('$', "s: hide numbering")
endif
2014-07-04 14:15:28 -04:00
" Delete empty first line and jump to the closest section
0delete _
call setpos('.', [0, closest_index, 0, 0])
2013-10-23 15:00:20 -04:00
2013-10-05 07:53:42 -04:00
" Set filetype and lock buffer
setlocal filetype=latextoc
setlocal nomodifiable
endfunction
function! latex#toc#toggle() " {{{1
2013-10-05 07:53:42 -04:00
if bufwinnr(bufnr('LaTeX TOC')) >= 0
if g:latex_toc_resize
silent exe "set columns-=" . g:latex_toc_width
endif
silent execute 'bwipeout' . bufnr('LaTeX TOC')
else
call latex#toc#open()
silent execute 'wincmd w'
endif
endfunction
" }}}1
" Define dictionary to keep track of TOC numbers
let s:number = {}
2014-07-16 17:20:11 -04:00
" Define regular expressions to match document parts
let s:re_input = '\v^\s*\\%(input|include)\s*\{'
let s:re_input_file = s:re_input . '\zs[^\}]+\ze}'
let s:re_sec = '\v^\s*\\%(part|chapter|%(sub)*section)\*?\s*\{'
let s:re_sec_level = '\v^\s*\\\zs%(part|chapter|%(sub)*section)\*?'
let s:re_sec_title = s:re_sec . '\zs.{-}\ze\}?$'
2014-07-16 17:20:11 -04:00
let s:re_bib = '\v^\s*\\%((print)?bibliography|printbibheading)\s*\{'
function! s:parse_file(file, ...) " {{{1
2014-07-04 14:15:28 -04:00
" Parses tex file for TOC entries
"
" The function returns a list of entries. Each entry is a dictionary:
"
" entry = {
" title : "Some title",
" number : "3.1.2",
" file : /path/to/file.tex,
" line : 142,
" }
" Test if file is readable
if ! filereadable(a:file)
echoerr "Error in latex#toc s:parse_file:"
echoerr "File not readable: " . a:file
return []
2013-10-05 07:53:42 -04:00
endif
2014-07-09 06:11:55 -04:00
" Reset TOC numbering
2014-07-08 06:32:41 -04:00
if a:0 > 0
call s:number_reset()
2014-07-08 06:32:41 -04:00
endif
2014-07-04 14:15:28 -04:00
let toc = []
let lnum = 0
for line in readfile(a:file)
2014-07-01 16:34:26 -04:00
let lnum += 1
2014-07-04 14:15:28 -04:00
2014-07-08 06:32:41 -04:00
" 1. Parse inputs or includes
if line =~# s:re_input
2014-07-04 14:15:28 -04:00
call extend(toc, s:parse_file(s:parse_line_input(line)))
continue
endif
" 2. Parse preamble
if s:number.preamble
if line =~# '\v^\s*\\documentclass'
call add(toc, {
\ 'title' : 'Preamble',
\ 'number' : '',
\ 'file' : a:file,
\ 'line' : lnum,
\ })
continue
endif
if line =~# '\v^\s*\\begin\{document\}'
let s:number.preamble = 0
endif
continue
endif
" 3. Parse chapters, sections, and subsections
2014-07-08 06:32:41 -04:00
if line =~# s:re_sec
2014-07-04 14:15:28 -04:00
call add(toc, s:parse_line_sec(a:file, lnum, line))
continue
endif
2014-07-09 06:11:55 -04:00
2014-07-16 17:20:11 -04:00
" 4. Parse biblography
if line =~# s:re_bib
call add(toc, {
\ 'title' : 'Bibliography',
\ 'number' : '',
\ 'file' : a:file,
\ 'line' : lnum,
\ })
continue
endif
" 5. Reset and change numbering for the appendix
2014-07-09 06:11:55 -04:00
if line =~# '\v^\s*\\appendix'
call s:number_start_appendix()
2014-07-09 06:11:55 -04:00
endif
2014-07-01 16:34:26 -04:00
endfor
2013-10-05 07:53:42 -04:00
2014-07-01 16:34:26 -04:00
return toc
endfunction
2013-10-05 07:53:42 -04:00
function! s:parse_line_input(line) " {{{1
2014-07-08 06:32:41 -04:00
let l:file = matchstr(a:line, s:re_input_file)
2014-07-04 14:15:28 -04:00
if l:file !~# '.tex$'
let l:file .= '.tex'
2013-10-05 07:53:42 -04:00
endif
2014-07-04 14:15:28 -04:00
return fnamemodify(l:file, ':p')
endfunction
function! s:parse_line_sec(file, lnum, line) " {{{1
2014-07-08 06:32:41 -04:00
let title = matchstr(a:line, s:re_sec_title)
2014-07-09 06:43:10 -04:00
let number = s:number_increment(matchstr(a:line, s:re_sec_level))
2014-07-04 14:15:28 -04:00
return {
2014-07-08 06:32:41 -04:00
\ 'title' : title,
2014-07-09 06:11:55 -04:00
\ 'number' : number,
2014-07-04 14:15:28 -04:00
\ 'file' : a:file,
\ 'line' : a:lnum,
\ }
2013-10-05 07:53:42 -04:00
endfunction
function! s:number_reset() " {{{1
let s:number.part = 0
let s:number.chapter = 0
let s:number.section = 0
let s:number.subsection = 0
let s:number.subsubsection = 0
let s:number.appendix = 0
let s:number.preamble = 1
endfunction
function! s:number_start_appendix() " {{{1
2014-07-09 06:11:55 -04:00
let s:number.part = 0
let s:number.chapter = 0
let s:number.section = 0
let s:number.subsection = 0
let s:number.subsubsection = 0
let s:number.appendix = 1
2014-07-09 06:11:55 -04:00
endfunction
function! s:number_increment(level) " {{{1
2014-07-09 06:11:55 -04:00
" Check if level should be incremented
if a:level !~# '\v%(part|chapter|(sub)*section)$'
2014-07-08 06:32:41 -04:00
return ''
endif
2013-10-05 07:53:42 -04:00
2014-07-09 06:11:55 -04:00
" Increment numbers
if a:level == 'part'
let s:number.part += 1
let s:number.chapter = 0
let s:number.section = 0
let s:number.subsection = 0
let s:number.subsubsection = 0
elseif a:level == 'chapter'
let s:number.chapter += 1
let s:number.section = 0
let s:number.subsection = 0
let s:number.subsubsection = 0
elseif a:level == 'section'
let s:number.section += 1
let s:number.subsection = 0
let s:number.subsubsection = 0
elseif a:level == 'subsection'
let s:number.subsection += 1
let s:number.subsubsection = 0
elseif a:level == 'subsubsection'
let s:number.subsubsection += 1
endif
2014-07-09 06:43:10 -04:00
return s:number_print()
2014-07-09 06:11:55 -04:00
endfunction
function! s:number_print() " {{{1
2014-07-09 06:11:55 -04:00
let number = [
2014-07-09 06:43:10 -04:00
\ s:number.part,
\ s:number.chapter,
\ s:number.section,
\ s:number.subsection,
\ s:number.subsubsection,
2014-07-09 06:11:55 -04:00
\ ]
2014-07-09 06:43:10 -04:00
while number[0] == 0
call remove(number, 0)
endwhile
while number[-1] == 0
call remove(number, -1)
endwhile
if s:number.appendix
let number[0] = nr2char(number[0] + 64)
endif
return join(number, '.')
2014-07-04 14:15:28 -04:00
endfunction
2014-07-08 06:32:41 -04:00
2014-07-04 14:15:28 -04:00
" }}}1
2014-02-10 08:21:43 -05:00
" vim: fdm=marker