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 nobuflisted
|
|
|
|
setlocal noswapfile
|
|
|
|
setlocal nowrap
|
|
|
|
setlocal nonumber
|
|
|
|
setlocal nolist
|
|
|
|
setlocal nospell
|
|
|
|
setlocal cursorline
|
|
|
|
setlocal tabstop=8
|
|
|
|
setlocal cole=0
|
|
|
|
setlocal cocu=nvic
|
|
|
|
if g:latex_toc_fold
|
2013-10-23 14:47:03 -04:00
|
|
|
setlocal foldmethod=expr
|
|
|
|
setlocal foldexpr=toc#fold(v:lnum)
|
|
|
|
setlocal foldtext=toc#fold_tex()
|
2013-10-05 07:53:42 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
" 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> 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>
|
|
|
|
|
|
|
|
" {{{1 s:toc_activate
|
|
|
|
function! s:toc_activate(close)
|
2013-10-23 14:47:03 -04:00
|
|
|
let n = getpos('.')[1] - 1
|
2013-10-05 07:53:42 -04:00
|
|
|
|
2013-10-23 14:47:03 -04:00
|
|
|
if n >= len(b:toc)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let entry = b:toc[n]
|
|
|
|
|
|
|
|
let titlestr = s:toc_escape_title(entry['text'])
|
2013-10-05 07:53:42 -04:00
|
|
|
|
2013-10-23 14:47:03 -04:00
|
|
|
" Search for duplicates
|
|
|
|
let i=0
|
|
|
|
let entry_hash = entry['level'].titlestr
|
|
|
|
let duplicates = 0
|
|
|
|
while i<n
|
|
|
|
let i_hash = b:toc[i]['level'].s:toc_escape_title(b:toc[i]['text'])
|
|
|
|
if i_hash == entry_hash
|
|
|
|
let duplicates += 1
|
2013-10-05 07:53:42 -04:00
|
|
|
endif
|
2013-10-23 14:47:03 -04:00
|
|
|
let i += 1
|
|
|
|
endwhile
|
|
|
|
let toc_bnr = bufnr('%')
|
|
|
|
let toc_wnr = winnr()
|
2013-10-05 07:53:42 -04:00
|
|
|
|
2013-10-23 14:47:03 -04:00
|
|
|
execute b:calling_win . 'wincmd w'
|
2013-10-05 07:53:42 -04:00
|
|
|
|
2013-10-23 14:47:03 -04:00
|
|
|
let files = [entry['file']]
|
|
|
|
for line in filter(readfile(entry['file']), 'v:val =~ ''\\input{''')
|
|
|
|
call add(files, matchstr(line, '{\zs.*\ze\(\.tex\)\?}') . '.tex')
|
|
|
|
endfor
|
2013-10-05 07:53:42 -04:00
|
|
|
|
2013-10-23 14:47:03 -04:00
|
|
|
" Find section in buffer (or inputted files)
|
|
|
|
call s:toc_find_match('\\' . entry['level'] . '\_\s*{' . titlestr . '}',
|
|
|
|
\ duplicates, files)
|
2013-10-05 07:53:42 -04:00
|
|
|
|
2013-10-23 14:47:03 -04:00
|
|
|
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
|
2013-10-23 14:47:03 -04:00
|
|
|
execute 'bwipeout ' . toc_bnr
|
|
|
|
else
|
|
|
|
execute toc_wnr . 'wincmd w'
|
|
|
|
endif
|
2013-10-05 07:53:42 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" {{{1 s:toc_close
|
|
|
|
function! s:toc_close()
|
2013-10-23 14:47:03 -04:00
|
|
|
if g:latex_toc_resize
|
|
|
|
silent exe "set columns-=" . g:latex_toc_width
|
|
|
|
endif
|
|
|
|
bwipeout
|
2013-10-05 07:53:42 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" {{{1 s:toc_escape_title
|
|
|
|
function! s:toc_escape_title(titlestr)
|
2014-04-13 09:06:06 -04:00
|
|
|
let titlestr = substitute(a:titlestr, '\\[a-zA-Z@]*\>\s*{\?', '.*', 'g')
|
|
|
|
let titlestr = substitute(titlestr, '}', '', 'g')
|
|
|
|
let titlestr = substitute(titlestr, '\%(\.\*\s*\)\{2,}', '.*', 'g')
|
|
|
|
return titlestr
|
2013-10-05 07:53:42 -04:00
|
|
|
endfunction
|
|
|
|
|
2013-10-23 14:47:03 -04:00
|
|
|
" {{{1 s:toc_find_match
|
2014-04-13 09:06:06 -04:00
|
|
|
function! s:toc_find_match(strsearch, duplicates, files)
|
|
|
|
if len(a:files) == 0
|
|
|
|
echoerr "Could not find: " . a:strsearch
|
|
|
|
return
|
|
|
|
endif
|
2013-10-23 14:47:03 -04:00
|
|
|
|
|
|
|
call s:toc_open_buf(a:files[0])
|
|
|
|
let dups = a:duplicates
|
|
|
|
|
|
|
|
" Skip duplicates
|
|
|
|
while dups > 0
|
|
|
|
if search(a:strsearch, 'w')
|
|
|
|
let dups -= 1
|
2013-10-05 07:53:42 -04:00
|
|
|
else
|
2013-10-23 14:47:03 -04:00
|
|
|
break
|
2013-10-05 07:53:42 -04:00
|
|
|
endif
|
2013-10-23 14:47:03 -04:00
|
|
|
endwhile
|
|
|
|
|
|
|
|
if search(a:strsearch, 'w')
|
|
|
|
normal! zv
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2014-04-13 09:06:06 -04:00
|
|
|
call s:toc_find_match(a:strsearch, dups, a:files[1:])
|
2013-10-23 14:47:03 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" {{{1 s:toc_open_buf
|
|
|
|
function! s:toc_open_buf(file)
|
|
|
|
let bnr = bufnr(a:file)
|
|
|
|
if bnr == -1
|
|
|
|
execute 'badd ' . a:file
|
|
|
|
let bnr = bufnr(a:file)
|
|
|
|
endif
|
|
|
|
execute 'buffer! ' . bnr
|
2014-04-13 09:17:47 -04:00
|
|
|
normal gg
|
2013-10-23 14:47:03 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" {{{1 s:toc_toggle_numbers
|
|
|
|
function! s:toc_toggle_numbers()
|
|
|
|
if b:toc_numbers
|
|
|
|
setlocal conceallevel=3
|
|
|
|
let b:toc_numbers = 0
|
|
|
|
else
|
|
|
|
setlocal conceallevel=0
|
|
|
|
let b:toc_numbers = 1
|
|
|
|
endif
|
2013-10-05 07:53:42 -04:00
|
|
|
endfunction
|
|
|
|
" }}}1
|
|
|
|
|
2014-02-10 08:21:43 -05:00
|
|
|
" vim: fdm=marker
|