vimtex/ftplugin/latextoc.vim

156 lines
3.8 KiB
VimL
Raw Normal View History

2013-10-05 07:53:42 -04:00
" LaTeX plugin for Vim
"
" Maintainer: Karl Yngve Lervåg
" Email: karl.yngve@gmail.com
"
if exists('b:did_ftplugin')
finish
endif
let b:did_ftplugin = 1
" Set local buffer settings
setlocal buftype=nofile
setlocal bufhidden=wipe
setlocal listchars=
2013-10-05 07:53:42 -04:00
setlocal nobuflisted
setlocal noswapfile
setlocal nowrap
if g:latex_toc_hide_line_numbers
setlocal nonumber
setlocal norelativenumber
endif
2013-10-05 07:53:42 -04:00
setlocal nolist
setlocal nospell
setlocal cursorline
setlocal tabstop=8
setlocal cole=0
setlocal cocu=nvic
if g:latex_toc_fold
setlocal foldmethod=expr
setlocal foldexpr=latextoc#fold_level(v:lnum)
setlocal foldtext=latextoc#fold_text()
2013-10-05 07:53:42 -04:00
endif
" Refresh/Initialize TOC content
2014-08-11 16:30:33 -04:00
call latextoc#init()
2013-10-05 07:53:42 -04:00
" Define mappings
nnoremap <buffer> <silent> G G4k
nnoremap <buffer> <silent> <Esc>OA k
nnoremap <buffer> <silent> <Esc>OB j
nnoremap <buffer> <silent> <Esc>OC l
nnoremap <buffer> <silent> <Esc>OD h
nnoremap <buffer> <silent> s :call <SID>toc_toggle_numbers()<cr>
nnoremap <buffer> <silent> - :call <SID>toc_dec_secnumdepth()<cr>
nnoremap <buffer> <silent> + :call <SID>toc_inc_secnumdepth()<cr>
2013-10-05 07:53:42 -04:00
nnoremap <buffer> <silent> q :call <SID>toc_close()<cr>
nnoremap <buffer> <silent> <Esc> :call <SID>toc_close()<cr>
nnoremap <buffer> <silent> <Space> :call <SID>toc_activate(0)<cr>
nnoremap <buffer> <silent> <leftrelease> :call <SID>toc_activate(0)<cr>
nnoremap <buffer> <silent> <CR> :call <SID>toc_activate(1)<cr>
nnoremap <buffer> <silent> <2-leftmouse> :call <SID>toc_activate(1)<cr>
function! s:toc_activate(close) "{{{1
2014-07-04 14:15:28 -04:00
" Get TOC entry, do nothing if no entry found
" entry = {
" title : ...,
" number : ...,
" file : ...,
" line : ...,
" }
let n = getpos('.')[1] - 1
if n >= len(b:toc)
return
endif
let entry = b:toc[n]
2014-07-04 14:15:28 -04:00
" Save TOC buffer info for later use
let toc_bnr = bufnr('%')
let toc_wnr = winnr()
2013-10-05 07:53:42 -04:00
2014-07-04 14:15:28 -04:00
" Return to calling window
2014-08-10 14:43:57 -04:00
execute b:calling_winnr . 'wincmd w'
2013-10-05 07:53:42 -04:00
2014-07-04 14:15:28 -04:00
" Open file and line for given TOC entry
call s:toc_open_entry(entry)
2013-10-05 07:53:42 -04:00
2014-07-04 14:15:28 -04:00
" Keep or close TOC window (based on options)
if a:close
if g:latex_toc_resize
silent exe "set columns-=" . g:latex_toc_width
2013-10-05 07:53:42 -04:00
endif
execute 'bwipeout ' . toc_bnr
else
execute toc_wnr . 'wincmd w'
endif
2013-10-05 07:53:42 -04:00
endfunction
function! s:toc_close() "{{{1
if g:latex_toc_resize
silent exe "set columns-=" . g:latex_toc_width
endif
bwipeout
2013-10-05 07:53:42 -04:00
endfunction
function! s:toc_open_entry(entry) "{{{1
2014-11-19 03:59:01 -05:00
" Get buffer number (add buffer if necessary)
2014-07-04 14:15:28 -04:00
let bnr = bufnr(a:entry.file)
if bnr == -1
execute 'badd ' . fnameescape(a:entry.file)
2014-07-04 14:15:28 -04:00
let bnr = bufnr(a:entry.file)
endif
2014-11-19 03:59:01 -05:00
" Set bufferopen command
" The point here is to use existing open buffer if the user has turned on
" the &switchbuf option to either 'useopen' or 'usetab'
let cmd = 'buffer! '
if &switchbuf =~ 'usetab'
for i in range(tabpagenr('$'))
if index(tabpagebuflist(i + 1), bnr) >= 0
let cmd = 'sbuffer! '
break
endif
endfor
elseif &switchbuf =~ 'useopen'
if bufwinnr(bnr) > 0
let cmd = 'sbuffer! '
endif
endif
" Open file buffer
execute cmd . bnr
2014-07-04 14:15:28 -04:00
" Go to entry line
call setpos('.', [0, a:entry.line, 0, 0])
" Ensure folds are opened
normal! zv
endfunction
function! s:toc_toggle_numbers() "{{{1
2014-11-22 11:29:30 -05:00
if g:latex_toc_numbers
let g:latex_toc_numbers = 0
else
2014-11-22 11:29:30 -05:00
let g:latex_toc_numbers = 1
endif
call latextoc#refresh()
endfunction
function! s:toc_inc_secnumdepth() "{{{1
2014-08-11 11:19:00 -04:00
let b:toc_secnumdepth = min([b:toc_secnumdepth + 1, 5])
let g:latex_toc_secnumdepth = b:toc_secnumdepth
call latextoc#refresh()
2013-10-05 07:53:42 -04:00
endfunction
function! s:toc_dec_secnumdepth() "{{{1
2014-08-11 11:19:00 -04:00
let b:toc_secnumdepth = max([b:toc_secnumdepth - 1, -2])
let g:latex_toc_secnumdepth = b:toc_secnumdepth
call latextoc#refresh()
endfunction
2013-10-05 07:53:42 -04:00
" }}}1
2014-02-10 08:21:43 -05:00
" vim: fdm=marker