From e5e5f2b07bf373b0cf1b5a1c3e5436a2bd20555a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Mon, 11 Aug 2014 22:30:33 +0200 Subject: [PATCH] Separate toc init and toc refresh --- autoload/latextoc.vim | 55 +++++++++++++++++++++++++++++++++---------- ftplugin/latextoc.vim | 2 +- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/autoload/latextoc.vim b/autoload/latextoc.vim index 5fbd6e2..b45d4ce 100644 --- a/autoload/latextoc.vim +++ b/autoload/latextoc.vim @@ -33,27 +33,57 @@ function! latextoc#fold_text() " {{{1 endfunction " }}}1 +function! latextoc#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! latextoc#refresh() " {{{1 if !exists('b:toc') | return | endif - set modifiable - " Clean the buffer + " 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 +" }}}1 + +function! s:add_start() " {{{1 + let b:toc_pos_saved = getpos('.') + setlocal modifiable %delete +endfunction - " Add TOC entries (and keep track of closest index) - let index = 0 +" }}}1 +function! s:add_entries() " {{{1 let closest_index = 0 let s:num_format = '%-' . 2*(b:toc_secnumdepth+2) . 's' - for entry in b:toc - call s:print_entry(entry) + let index = 0 + for entry in b:toc let index += 1 + call s:print_entry(entry) if entry.file == b:calling_file && entry.line <= b:calling_line let closest_index = index endif endfor - " Add help info (if desired) + let b:toc_pos_closest = [0, closest_index, 0, 0] +endfunction +" }}}1 +function! s:add_help() " {{{1 if !g:latex_toc_hide_help call append('$', "") call append('$', "/q: close") @@ -63,12 +93,13 @@ function! latextoc#refresh() " {{{1 call append('$', "+: increase secnumpdeth") call append('$', "s: hide numbering") endif - - " Delete empty first line and jump to the closest section - 0delete _ - set nomodifiable - call setpos('.', [0, closest_index, 0, 0]) endfunction +" }}}1 +function! s:add_end() " {{{1 + 0delete _ + setlocal nomodifiable +endfunction + " }}}1 function! s:print_entry(entry) " {{{1 diff --git a/ftplugin/latextoc.vim b/ftplugin/latextoc.vim index 656a47f..c87eede 100644 --- a/ftplugin/latextoc.vim +++ b/ftplugin/latextoc.vim @@ -30,7 +30,7 @@ if g:latex_toc_fold endif " Refresh/Initialize TOC content -call latextoc#refresh() +call latextoc#init() " Define mappings nnoremap G G4k