Added unite source for table of labels (#330)

This commit is contained in:
Karl Yngve Lervåg 2016-01-25 22:42:04 +01:00
parent 0aa80b3040
commit 01adbfe579
2 changed files with 82 additions and 27 deletions

View File

@ -0,0 +1,54 @@
" vimtex - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve Lervåg
" Email: karl.yngve@gmail.com
"
let s:save_cpo = &cpo
set cpo&vim
let s:source = {
\ 'name' : 'vimtex_labels',
\ 'sorters' : 'sorter_nothing',
\ 'default_kind' : 'jump_list',
\ 'syntax' : 'uniteSource__vimtex',
\ 'hooks' : {},
\}
function! s:source.gather_candidates(args, context) " {{{1
let entries = vimtex#labels#get_entries()
return map(entries, '{
\ "word" : v:val.title,
\ "action__path" : v:val.file,
\ "action__line" : v:val.line,
\ }')
endfunction
" }}}1
function! s:source.hooks.on_syntax(args, context) " {{{1
syntax match VimtexLabelsChap /chap:.*$/ contains=@Tex
\ contained containedin=uniteSource__vimtex
syntax match VimtexLabelsEq /eq:.*$/ contains=@Tex
\ contained containedin=uniteSource__vimtex
syntax match VimtexLabelsFig /fig:.*$/ contains=@Tex
\ contained containedin=uniteSource__vimtex
syntax match VimtexLabelsSec /sec:.*$/ contains=@Tex
\ contained containedin=uniteSource__vimtex
syntax match VimtexLabelsTab /tab:.*$/ contains=@Tex
\ contained containedin=uniteSource__vimtex
highlight link VimtexLabelsChap PreProc
highlight link VimtexLabelsEq Statement
highlight link VimtexLabelsFig Identifier
highlight link VimtexLabelsSec Type
highlight link VimtexLabelsTab String
endfunction
" }}}1
function! unite#sources#vimtex_labels#define()
return s:source
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo

View File

@ -35,7 +35,7 @@ function! vimtex#labels#open() " {{{1
let index = {}
let index.name = s:name
let index.entries = s:gather_labels(b:vimtex.tex)
let index.entries = vimtex#labels#get_entries()
let index.all_entries = deepcopy(index.entries)
let index.hook_init_post = function('s:index_hook_init_post')
let index.help = [
@ -61,6 +61,33 @@ endfunction
" }}}1
function! vimtex#labels#get_entries(...) " {{{1
let l:file = a:0 > 0 ? a:1 : b:vimtex.tex
let l:tac = []
let l:preamble = 1
for [l:file, l:lnum, l:line] in vimtex#parser#tex(l:file)
if l:line =~# '\v^\s*\\begin\{document\}'
let l:preamble = 0
endif
if l:preamble
continue
endif
if l:line =~# '\v\\label\{'
call add(tac, {
\ 'title' : matchstr(l:line, '\v\\label\{\zs.{-}\ze\}?\s*$'),
\ 'file' : l:file,
\ 'line' : l:lnum,
\ })
continue
endif
endfor
return l:tac
endfunction
" }}}1
function! s:index_clear_filter() dict "{{{1
let self.entries = copy(self.all_entries)
call self.refresh()
@ -100,30 +127,4 @@ endfunction
" }}}1
function! s:gather_labels(file) " {{{1
let l:tac = []
let l:preamble = 1
for [l:file, l:lnum, l:line] in vimtex#parser#tex(a:file)
if l:line =~# '\v^\s*\\begin\{document\}'
let l:preamble = 0
endif
if l:preamble
continue
endif
if l:line =~# '\v\\label\{'
call add(tac, {
\ 'title' : matchstr(l:line, '\v\\label\{\zs.{-}\ze\}?\s*$'),
\ 'file' : l:file,
\ 'line' : l:lnum,
\ })
continue
endif
endfor
return l:tac
endfunction
" }}}1
" vim: fdm=marker sw=2