From 4998af215a81d4b27b40cf5456980830492824a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sun, 24 Jan 2016 23:02:33 +0100 Subject: [PATCH] Initial unite source for TOC (#330) --- autoload/unite/sources/vimtex_toc.vim | 27 ++++ autoload/vimtex/toc.vim | 181 +++++++++++++------------- 2 files changed, 118 insertions(+), 90 deletions(-) create mode 100644 autoload/unite/sources/vimtex_toc.vim diff --git a/autoload/unite/sources/vimtex_toc.vim b/autoload/unite/sources/vimtex_toc.vim new file mode 100644 index 0000000..76e3d76 --- /dev/null +++ b/autoload/unite/sources/vimtex_toc.vim @@ -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 diff --git a/autoload/vimtex/toc.vim b/autoload/vimtex/toc.vim index 05aae1b..65f11b9 100644 --- a/autoload/vimtex/toc.vim +++ b/autoload/vimtex/toc.vim @@ -111,7 +111,7 @@ function! vimtex#toc#open() " {{{1 \ 'name' : s:name, \ 'calling_file' : expand('%:p'), \ 'calling_line' : line('.'), - \ 'entries' : s:parse_toc(), + \ 'entries' : vimtex#toc#get_entries(), \ 'show_numbers' : g:vimtex_toc_show_numbers, \ 'max_level' : s:max_level, \ 'topmatters' : s:count_matters, @@ -146,6 +146,96 @@ endfunction " }}}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 let pline = getline(a:lnum - 1) let cline = getline(a:lnum) @@ -303,95 +393,6 @@ endfunction " }}}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 let title = matchstr(a:line, s:re_sec_title) let level = matchstr(a:line, s:re_sec_level)