2015-03-07 17:02:15 -05:00
|
|
|
" vimtex - LaTeX plugin for Vim
|
2014-08-10 12:45:32 -04:00
|
|
|
"
|
|
|
|
" Maintainer: Karl Yngve Lervåg
|
|
|
|
" Email: karl.yngve@gmail.com
|
|
|
|
"
|
|
|
|
|
2015-03-07 17:02:15 -05:00
|
|
|
function! vimtextoc#init() " {{{1
|
|
|
|
if !exists('b:toc') | return | endif
|
|
|
|
|
|
|
|
" Fill TOC entries
|
|
|
|
call s:add_start()
|
|
|
|
call s:add_entries()
|
|
|
|
call s:add_help()
|
|
|
|
call s:add_end()
|
|
|
|
|
|
|
|
" Jump to closest index
|
|
|
|
call setpos('.', b:toc_pos_closest)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
function! vimtextoc#fold_level(lnum) " {{{1
|
2014-08-10 12:45:32 -04:00
|
|
|
let pline = getline(a:lnum - 1)
|
|
|
|
let cline = getline(a:lnum)
|
|
|
|
let nline = getline(a:lnum + 1)
|
|
|
|
let l:pn = matchstr(pline, '\d$')
|
|
|
|
let l:cn = matchstr(cline, '\d$')
|
|
|
|
let l:nn = matchstr(nline, '\d$')
|
|
|
|
|
|
|
|
" Don't fold options
|
|
|
|
if cline =~# '^\s*$'
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
2015-03-07 17:02:15 -05:00
|
|
|
if l:nn > l:cn && g:vimtex_toc_fold_levels >= l:nn
|
2014-08-10 12:45:32 -04:00
|
|
|
return '>' . l:nn
|
|
|
|
endif
|
|
|
|
|
2015-03-07 17:02:15 -05:00
|
|
|
if l:cn < l:pn && l:cn >= l:nn && g:vimtex_toc_fold_levels >= l:cn
|
2014-08-10 12:45:32 -04:00
|
|
|
return l:cn
|
|
|
|
endif
|
|
|
|
|
|
|
|
return '='
|
|
|
|
endfunction
|
|
|
|
|
2015-03-07 17:02:15 -05:00
|
|
|
" }}}1
|
|
|
|
function! vimtextoc#fold_text() " {{{1
|
2014-08-10 12:45:32 -04:00
|
|
|
return getline(v:foldstart)
|
|
|
|
endfunction
|
2014-08-11 16:30:33 -04:00
|
|
|
|
|
|
|
" }}}1
|
2015-03-07 17:02:15 -05:00
|
|
|
function! vimtextoc#refresh() " {{{1
|
2014-08-10 12:45:32 -04:00
|
|
|
if !exists('b:toc') | return | endif
|
|
|
|
|
2014-08-11 16:30:33 -04:00
|
|
|
" Fill TOC entries
|
|
|
|
call s:add_start()
|
|
|
|
call s:add_entries()
|
|
|
|
call s:add_help()
|
|
|
|
call s:add_end()
|
|
|
|
|
|
|
|
" Restore old position
|
|
|
|
call setpos('.', b:toc_pos_saved)
|
|
|
|
endfunction
|
2015-03-07 17:02:15 -05:00
|
|
|
|
2014-08-11 16:30:33 -04:00
|
|
|
" }}}1
|
|
|
|
|
|
|
|
function! s:add_start() " {{{1
|
|
|
|
let b:toc_pos_saved = getpos('.')
|
|
|
|
setlocal modifiable
|
2014-08-10 12:45:32 -04:00
|
|
|
%delete
|
2014-08-11 16:30:33 -04:00
|
|
|
endfunction
|
2014-08-10 12:45:32 -04:00
|
|
|
|
2014-08-11 16:30:33 -04:00
|
|
|
" }}}1
|
|
|
|
function! s:add_entries() " {{{1
|
2014-08-10 12:45:32 -04:00
|
|
|
let closest_index = 0
|
2015-03-07 17:02:15 -05:00
|
|
|
if g:vimtex_toc_numbers_width
|
|
|
|
let s:width = g:vimtex_toc_numbers_width
|
2014-11-22 11:29:30 -05:00
|
|
|
else
|
|
|
|
let s:width = 2*(b:toc_secnumdepth+2)
|
|
|
|
endi
|
|
|
|
let s:width = max([0, s:width])
|
|
|
|
let s:num_format = '%-' . s:width . 's'
|
2014-08-10 12:45:32 -04:00
|
|
|
|
2014-08-11 16:30:33 -04:00
|
|
|
let index = 0
|
|
|
|
for entry in b:toc
|
2014-08-10 12:45:32 -04:00
|
|
|
let index += 1
|
2014-08-11 16:30:33 -04:00
|
|
|
call s:print_entry(entry)
|
2014-08-10 12:45:32 -04:00
|
|
|
if entry.file == b:calling_file && entry.line <= b:calling_line
|
|
|
|
let closest_index = index
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
2014-08-11 16:30:33 -04:00
|
|
|
let b:toc_pos_closest = [0, closest_index, 0, 0]
|
|
|
|
endfunction
|
|
|
|
" }}}1
|
|
|
|
function! s:add_help() " {{{1
|
2015-03-07 17:02:15 -05:00
|
|
|
if !g:vimtex_toc_hide_help
|
2014-08-10 12:45:32 -04:00
|
|
|
call append('$', "")
|
|
|
|
call append('$', "<Esc>/q: close")
|
|
|
|
call append('$', "<Space>: jump")
|
|
|
|
call append('$', "<Enter>: jump and close")
|
|
|
|
call append('$', "-: decrease secnumpdeth")
|
|
|
|
call append('$', "+: increase secnumpdeth")
|
|
|
|
call append('$', "s: hide numbering")
|
|
|
|
endif
|
2014-08-11 16:30:33 -04:00
|
|
|
endfunction
|
|
|
|
" }}}1
|
|
|
|
function! s:add_end() " {{{1
|
2014-08-10 12:45:32 -04:00
|
|
|
0delete _
|
2014-08-11 16:30:33 -04:00
|
|
|
setlocal nomodifiable
|
2014-08-10 12:45:32 -04:00
|
|
|
endfunction
|
2014-08-11 16:30:33 -04:00
|
|
|
|
2014-08-10 12:45:32 -04:00
|
|
|
" }}}1
|
|
|
|
|
|
|
|
function! s:print_entry(entry) " {{{1
|
2014-08-11 11:19:00 -04:00
|
|
|
let level = b:toc_max_level - a:entry.level
|
|
|
|
|
|
|
|
" Create entry string
|
2014-08-10 12:45:32 -04:00
|
|
|
let entry = ''
|
2015-03-07 17:02:15 -05:00
|
|
|
if g:vimtex_toc_numbers
|
2014-08-11 11:19:00 -04:00
|
|
|
let entry .= printf(s:num_format, level >= b:toc_secnumdepth + 2
|
2014-11-22 11:29:30 -05:00
|
|
|
\ ? '' : strpart(s:print_number(a:entry.number), 0, s:width))
|
2014-08-10 12:45:32 -04:00
|
|
|
endif
|
2015-01-01 09:02:07 -05:00
|
|
|
let entry .= printf('%-140S%s', a:entry.title, level)
|
2014-08-10 12:45:32 -04:00
|
|
|
|
|
|
|
call append('$', entry)
|
|
|
|
endfunction
|
|
|
|
" }}}1
|
|
|
|
function! s:print_number(number) " {{{1
|
|
|
|
if empty(a:number) | return "" | endif
|
|
|
|
|
|
|
|
let number = [
|
|
|
|
\ a:number.part,
|
|
|
|
\ a:number.chapter,
|
|
|
|
\ a:number.section,
|
|
|
|
\ a:number.subsection,
|
|
|
|
\ a:number.subsubsection,
|
|
|
|
\ a:number.subsubsubsection,
|
|
|
|
\ ]
|
|
|
|
|
|
|
|
" Remove unused parts
|
2014-08-11 11:19:00 -04:00
|
|
|
while number[0] == 0
|
2014-08-10 12:45:32 -04:00
|
|
|
call remove(number, 0)
|
|
|
|
endwhile
|
2014-08-11 11:19:00 -04:00
|
|
|
while number[-1] == 0
|
2014-08-10 12:45:32 -04:00
|
|
|
call remove(number, -1)
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
" Change numbering in frontmatter, appendix, and backmatter
|
|
|
|
if b:toc_topmatters > 1
|
|
|
|
\ && (a:number.frontmatter || a:number.backmatter)
|
|
|
|
return ""
|
|
|
|
elseif a:number.appendix
|
|
|
|
let number[0] = nr2char(number[0] + 64)
|
|
|
|
endif
|
|
|
|
|
|
|
|
return join(number, '.')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
|
2014-12-08 14:44:17 -05:00
|
|
|
" vim: fdm=marker sw=2
|