Initial unite source for TOC (#330)

This commit is contained in:
Karl Yngve Lervåg 2016-01-24 23:02:33 +01:00
parent bd69bed3e1
commit 4998af215a
2 changed files with 118 additions and 90 deletions

View File

@ -0,0 +1,27 @@
" 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_toc',
\ 'sorters' : 'sorter_nothing',
\}
function! s:source.gather_candidates(args, context)
let entries = vimtex#toc#get_entries()
return map(entries, '{
\ "word" : v:val.title,
\ }')
endfunction
function! unite#sources#vimtex_toc#define()
return s:source
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo

View File

@ -111,7 +111,7 @@ function! vimtex#toc#open() " {{{1
\ 'name' : s:name, \ 'name' : s:name,
\ 'calling_file' : expand('%:p'), \ 'calling_file' : expand('%:p'),
\ 'calling_line' : line('.'), \ 'calling_line' : line('.'),
\ 'entries' : s:parse_toc(), \ 'entries' : vimtex#toc#get_entries(),
\ 'show_numbers' : g:vimtex_toc_show_numbers, \ 'show_numbers' : g:vimtex_toc_show_numbers,
\ 'max_level' : s:max_level, \ 'max_level' : s:max_level,
\ 'topmatters' : s:count_matters, \ 'topmatters' : s:count_matters,
@ -146,6 +146,96 @@ endfunction
" }}}1 " }}}1
function! vimtex#toc#get_entries() " {{{1
"
" Parses tex project for TOC entries
"
" The function returns a list of entries. Each entry is a dictionary:
"
" entry = {
" title : "Some title",
" number : "3.1.2",
" file : /path/to/file.tex,
" line : 142,
" level : 2,
" }
"
let l:parsed = vimtex#parser#tex(b:vimtex.tex)
let s:max_level = 0
let s:count_matters = 0
for [l:file, l:lnum, l:line] in l:parsed
if l:line =~# s:re_sec
let s:max_level = max([s:max_level,
\ s:sec_to_value[matchstr(l:line, s:re_sec_level)]])
elseif l:line =~# s:re_matters
let s:count_matters += 1
endif
endfor
call s:number_reset('preamble')
let l:toc = []
for [l:file, l:lnum, l:line] in l:parsed
" Bibliography files
if l:line =~# s:re_bibs
call add(l:toc, s:parse_bib_input(l:line))
continue
endif
" Preamble
if s:number.preamble
if g:vimtex_toc_show_preamble && l:line =~# '\v^\s*\\documentclass'
call add(l:toc, {
\ 'title' : 'Preamble',
\ 'number' : '',
\ 'file' : l:file,
\ 'line' : l:lnum,
\ 'level' : s:max_level,
\ })
continue
endif
if l:line =~# '\v^\s*\\begin\{document\}'
let s:number.preamble = 0
endif
continue
endif
" Document structure (front-/main-/backmatter, appendix)
if l:line =~# s:re_structure
call s:number_reset(matchstr(l:line, s:re_structure_match))
continue
endif
" Sections (\parts, \chapters, \sections, and \subsections, ...)
if l:line =~# s:re_sec
call add(l:toc, s:parse_line_sec(l:file, l:lnum, l:line))
continue
endif
" Other stuff
for l:other in values(s:re_other)
if l:line =~# l:other.re
call add(l:toc, {
\ 'title' : l:other.title,
\ 'number' : '',
\ 'file' : l:file,
\ 'line' : l:lnum,
\ 'level' : s:max_level,
\ })
continue
endif
endfor
endfor
return l:toc
endfunction
" }}}1
function! s:index_fold_level(lnum) " {{{1 function! s:index_fold_level(lnum) " {{{1
let pline = getline(a:lnum - 1) let pline = getline(a:lnum - 1)
let cline = getline(a:lnum) let cline = getline(a:lnum)
@ -303,95 +393,6 @@ endfunction
" }}}1 " }}}1
function! s:parse_toc() " {{{1
"
" Parses tex project for TOC entries
"
" The function returns a list of entries. Each entry is a dictionary:
"
" entry = {
" title : "Some title",
" number : "3.1.2",
" file : /path/to/file.tex,
" line : 142,
" level : 2,
" }
"
let l:parsed = vimtex#parser#tex(b:vimtex.tex)
let s:max_level = 0
let s:count_matters = 0
for [l:file, l:lnum, l:line] in l:parsed
if l:line =~# s:re_sec
let s:max_level = max([s:max_level,
\ s:sec_to_value[matchstr(l:line, s:re_sec_level)]])
elseif l:line =~# s:re_matters
let s:count_matters += 1
endif
endfor
call s:number_reset('preamble')
let l:toc = []
for [l:file, l:lnum, l:line] in l:parsed
" Bibliography files
if l:line =~# s:re_bibs
call add(l:toc, s:parse_bib_input(l:line))
continue
endif
" Preamble
if s:number.preamble
if g:vimtex_toc_show_preamble && l:line =~# '\v^\s*\\documentclass'
call add(l:toc, {
\ 'title' : 'Preamble',
\ 'number' : '',
\ 'file' : l:file,
\ 'line' : l:lnum,
\ 'level' : s:max_level,
\ })
continue
endif
if l:line =~# '\v^\s*\\begin\{document\}'
let s:number.preamble = 0
endif
continue
endif
" Document structure (front-/main-/backmatter, appendix)
if l:line =~# s:re_structure
call s:number_reset(matchstr(l:line, s:re_structure_match))
continue
endif
" Sections (\parts, \chapters, \sections, and \subsections, ...)
if l:line =~# s:re_sec
call add(l:toc, s:parse_line_sec(l:file, l:lnum, l:line))
continue
endif
" Other stuff
for l:other in values(s:re_other)
if l:line =~# l:other.re
call add(l:toc, {
\ 'title' : l:other.title,
\ 'number' : '',
\ 'file' : l:file,
\ 'line' : l:lnum,
\ 'level' : s:max_level,
\ })
continue
endif
endfor
endfor
return l:toc
endfunction
" }}}1
function! s:parse_line_sec(file, lnum, line) " {{{1 function! s:parse_line_sec(file, lnum, line) " {{{1
let title = matchstr(a:line, s:re_sec_title) let title = matchstr(a:line, s:re_sec_title)
let level = matchstr(a:line, s:re_sec_level) let level = matchstr(a:line, s:re_sec_level)