vimtex/autoload/latex/toc.vim

151 lines
3.5 KiB
VimL
Raw Normal View History

2013-10-05 07:53:42 -04:00
" {{{1 latex#toc#init
function! latex#toc#init(initialized)
2013-10-10 15:48:52 -04:00
if g:latex_mappings_enabled && g:latex_toc_enabled
2013-10-05 07:53:42 -04:00
nnoremap <silent><buffer> <LocalLeader>lt :call latex#toc#open()<cr>
nnoremap <silent><buffer> <LocalLeader>lT :call latex#toc#toggle()<cr>
endif
endfunction
" {{{1 latex#toc#open
function! latex#toc#open()
" Check if buffer exists
let winnr = bufwinnr(bufnr('LaTeX TOC'))
if winnr >= 0
silent execute winnr . 'wincmd w'
return
endif
2014-07-01 16:34:26 -04:00
" Store buffer and file info
2013-10-05 07:53:42 -04:00
let calling_buf = bufnr('%')
let calling_file = expand('%:p')
2014-07-01 16:34:26 -04:00
" Resize if wanted
2013-10-05 07:53:42 -04:00
if g:latex_toc_resize
silent exe "set columns+=" . g:latex_toc_width
endif
" Parse TOC data
2014-07-01 16:34:26 -04:00
let toc = s:read_toc(g:latex#data[b:latex.id].tex)
"let closest_index = s:find_closest_section(toc, calling_file)
" Create TOC window
silent exe g:latex_toc_split_side g:latex_toc_width . 'vnew LaTeX\ TOC'
let b:toc = toc
let b:toc_numbers = 1
let b:calling_win = bufwinnr(calling_buf)
" Add TOC entries
for entry in toc
call append('$', entry['number'] . "\t" . entry['text'])
endfor
2013-10-05 07:53:42 -04:00
" Add help info
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
0delete _
2013-10-23 15:00:20 -04:00
" Jump to the closest section
2014-07-01 16:34:26 -04:00
"execute 'normal! ' . (closest_index + 1) . 'G'
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
" {{{1 latex#toc#toggle
function! latex#toc#toggle()
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
" {{{1 s:read_toc
2014-07-01 16:34:26 -04:00
function! s:read_toc(texfile, ...)
" Allow recursion
2013-10-05 07:53:42 -04:00
if a:0 != 2
let toc = []
else
let toc = a:1
endif
2014-07-01 16:34:26 -04:00
let lnum = 1
for line in readfile(a:texfile)
let line_stat = s:test_line
if s:test_line(line)
call add(toc, s:parse_line(line, lnum, a:texfile))
2013-10-05 07:53:42 -04:00
endif
2014-07-01 16:34:26 -04:00
let lnum += 1
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
2014-07-01 16:34:26 -04:00
" {{{1 s:test_line
function! s:test_line(line)
return line =~#
endfunction
2013-10-05 07:53:42 -04:00
2014-07-01 16:34:26 -04:00
" {{{1 s:parse_input_line
function! s:parse_input_line(line)
endfunction
2013-10-05 07:53:42 -04:00
2014-07-01 16:34:26 -04:00
" {{{1 s:parse_toc_line
function! s:parse_toc_line(line, lnum, file)
return {
\ 'title': "sec",
\ 'file': a:texfile,
\ 'line': a:lnum,
\ }
2013-10-05 07:53:42 -04:00
endfunction
" {{{1 s:find_closest_section
"
" 1. Binary search for the closest section
" 2. Return the index of the TOC entry
"
function! s:find_closest_section(toc, file)
if !has_key(a:toc.fileindices, a:file)
return
endif
let imax = len(a:toc.fileindices[a:file])
if imax > 0
let imin = 0
while imin < imax - 1
let i = (imax + imin) / 2
let tocindex = a:toc.fileindices[a:file][i]
let entry = a:toc.data[tocindex]
2013-10-23 14:45:48 -04:00
let titlestr = substitute(entry['text'],
\ '\\\w*\>\s*\%({[^}]*}\)\?', '.*', 'g')
2013-10-05 07:53:42 -04:00
let titlestr = escape(titlestr, '\')
let titlestr = substitute(titlestr, ' ', '\\_\\s\\+', 'g')
let [lnum, cnum]
2013-10-23 14:45:48 -04:00
\ = searchpos('\\'.entry['level'].'\_\s*{'.titlestr.'}', 'cnW')
2013-10-05 07:53:42 -04:00
if lnum
let imax = i
else
let imin = i
endif
endwhile
return a:toc.fileindices[a:file][imin]
else
return 0
endif
endfunction
" }}}1
2014-02-10 08:21:43 -05:00
" vim: fdm=marker