vimtex/autoload/vimtex.vim

379 lines
9.5 KiB
VimL
Raw Normal View History

2015-03-07 17:02:15 -05:00
" vimtex - LaTeX plugin for Vim
2014-07-22 18:08:57 -04:00
"
" Maintainer: Karl Yngve Lervåg
" Email: karl.yngve@gmail.com
"
2015-03-07 17:02:15 -05:00
" vimtex is not initialized until vimtex#init() has been run once
2015-03-23 14:18:19 -04:00
if !exists('s:initialized')
let s:initialized = 0
endif
2015-03-07 17:02:15 -05:00
function! vimtex#init() " {{{1
2014-07-16 17:08:00 -04:00
call s:init_options()
2013-10-05 07:53:42 -04:00
call s:init_environment()
2015-03-07 17:02:15 -05:00
call vimtex#toc#init(s:initialized)
call vimtex#echo#init(s:initialized)
2015-03-07 17:02:15 -05:00
call vimtex#fold#init(s:initialized)
call vimtex#view#init(s:initialized)
call vimtex#index#init(s:initialized)
2015-03-07 17:02:15 -05:00
call vimtex#motion#init(s:initialized)
call vimtex#labels#init(s:initialized)
call vimtex#change#init(s:initialized)
call vimtex#latexmk#init(s:initialized)
call vimtex#complete#init(s:initialized)
call vimtex#mappings#init(s:initialized)
2013-10-05 07:53:42 -04:00
"
" This variable is used to allow a distinction between global and buffer
" initialization
"
let s:initialized = 1
endfunction
2015-03-23 14:18:19 -04:00
" }}}1
2015-03-07 17:02:15 -05:00
function! vimtex#info() " {{{1
if !s:initialized
2015-03-20 16:30:59 -04:00
echoerr 'Error: vimtex has not been initialized!'
return
endif
2014-08-03 03:14:05 -04:00
" Print buffer data
call vimtex#echo#echo("b:vimtex\n")
2015-03-07 17:02:15 -05:00
call s:print_dict(b:vimtex)
2013-10-05 07:53:42 -04:00
2014-08-03 03:14:05 -04:00
" Print global data
let n = 0
2015-03-07 17:02:15 -05:00
for data in g:vimtex#data
2015-02-27 02:30:58 -05:00
" Prepare for printing
let d = deepcopy(data)
2014-08-03 03:14:05 -04:00
for f in ['aux', 'out', 'log']
2015-02-27 02:30:58 -05:00
silent execute 'let d.' . f . ' = data.' . f . '()'
2013-10-05 07:53:42 -04:00
endfor
2014-08-03 03:14:05 -04:00
2015-02-27 02:30:58 -05:00
" Print data blob title line
call vimtex#echo#formatted([
\ "\n\ng:vimtex#data[",
\ ['VimtexSuccess', n],
\ '] : ',
\ ['VimtexSuccess', remove(d, 'name') . "\n"]])
2015-02-27 02:30:58 -05:00
call s:print_dict(d)
2014-08-03 03:14:05 -04:00
let n += 1
2013-10-05 07:53:42 -04:00
endfor
endfunction
2015-05-16 16:14:52 -04:00
" }}}1
function! vimtex#wordcount(detailed) " {{{1
" Run texcount, save output to lines variable
let data = g:vimtex#data[b:vimtex.id]
let cmd = 'cd ' . vimtex#util#fnameescape(data.root)
let cmd .= '; texcount -nosub -sum '
let cmd .= a:detailed > 0 ? '-inc ' : '-merge '
let cmd .= vimtex#util#fnameescape(data.base)
let lines = split(system(cmd), '\n')
" Create wordcount window
if bufnr('TeXcount') >= 0
bwipeout TeXcount
endif
split TeXcount
" Add lines to buffer
for line in lines
call append('$', printf('%s', line))
endfor
0delete _
" Set mappings
nnoremap <buffer> <silent> q :bwipeout<cr>
" Set buffer options
setlocal bufhidden=wipe
setlocal buftype=nofile
setlocal cursorline
setlocal listchars=
setlocal nobuflisted
setlocal nolist
setlocal nospell
setlocal noswapfile
setlocal nowrap
setlocal tabstop=8
setlocal nomodifiable
" Set highlighting
syntax match TexcountText /^.*:.*/ contains=TexcountValue
syntax match TexcountValue /.*:\zs.*/
highlight link TexcountText VimtexMsg
highlight link TexcountValue Constant
endfunction
2013-10-05 07:53:42 -04:00
" }}}1
function! s:init_environment() " {{{1
2013-10-05 07:53:42 -04:00
" Initialize global and local data blobs
2015-03-07 17:02:15 -05:00
call vimtex#util#set_default('g:vimtex#data', [])
call vimtex#util#set_default('b:vimtex', {})
2013-10-05 07:53:42 -04:00
2014-08-03 03:14:05 -04:00
" Create new or link to existing blob
let main = s:get_main()
2013-10-05 07:53:42 -04:00
let id = s:get_id(main)
if id >= 0
2015-03-07 17:02:15 -05:00
let b:vimtex.id = id
2013-10-05 07:53:42 -04:00
else
let data = {}
let data.tex = main
let data.root = fnamemodify(data.tex, ':h')
let data.base = fnamemodify(data.tex, ':t')
let data.name = fnamemodify(data.tex, ':t:r')
2013-10-05 07:53:42 -04:00
function data.aux() dict
return s:get_main_ext(self, 'aux')
endfunction
function data.log() dict
return s:get_main_ext(self, 'log')
endfunction
function data.out() dict
return s:get_main_ext(self, 'pdf')
2013-10-05 07:53:42 -04:00
endfunction
2015-03-07 17:02:15 -05:00
call add(g:vimtex#data, data)
let b:vimtex.id = len(g:vimtex#data) - 1
2013-10-05 07:53:42 -04:00
endif
" Define commands
2015-04-01 09:54:14 -04:00
command! -buffer VimtexInfo call vimtex#info()
2015-05-16 16:14:52 -04:00
command! -buffer -bang VimtexWordCount call vimtex#wordcount(<q-bang> == "!")
" Define mappings
2015-04-01 09:54:14 -04:00
nnoremap <buffer> <plug>(vimtex-info) :call vimtex#info()<cr>
2013-10-05 07:53:42 -04:00
endfunction
2014-07-16 17:08:00 -04:00
function! s:init_options() " {{{1
2015-04-01 09:54:14 -04:00
let s:save_cpo = &cpo
set cpo&vim
" Ensure tex files are prioritized when listing files
for suf in [
\ '.log',
\ '.aux',
\ '.bbl',
\ '.out',
\ '.blg',
\ '.brf',
\ '.cb',
\ '.dvi',
\ '.fdb_latexmk',
\ '.fls',
\ '.idx',
\ '.ilg',
\ '.ind',
\ '.inx',
\ '.pdf',
\ '.synctex.gz',
\ '.toc',
\ ]
execute 'set suffixes+=' . suf
endfor
setlocal suffixesadd=.tex
setlocal comments=sO:%\ -,mO:%\ \ ,eO:%%,:%
setlocal commentstring=%%s
let &l:define = '\\\([egx]\|char\|mathchar\|count\|dimen\|muskip\|skip'
let &l:define .= '\|toks\)\=def\|\\font\|\\\(future\)\=let'
let &l:define .= '\|\\new\(count\|dimen\|skip'
let &l:define .= '\|muskip\|box\|toks\|read\|write\|fam\|insert\)'
let &l:define .= '\|\\\(re\)\=new\(boolean\|command\|counter\|environment'
let &l:define .= '\|font\|if\|length\|savebox'
let &l:define .= '\|theorem\(style\)\=\)\s*\*\=\s*{\='
let &l:define .= '\|DeclareMathOperator\s*{\=\s*'
let &l:include = '\\\(input\|include\){'
2015-04-01 09:57:02 -04:00
let &l:includeexpr = 'substitute('
let &l:includeexpr .= "substitute(v:fname, '\\\\space', '', 'g'),"
let &l:includeexpr .= "'^.\\{-}{\"\\?\\|\"\\?}.*', '', 'g')"
2015-04-01 09:54:14 -04:00
let &cpo = s:save_cpo
unlet s:save_cpo
2014-07-16 17:08:00 -04:00
endfunction
2015-04-01 09:54:14 -04:00
2014-07-16 17:08:00 -04:00
" }}}1
2013-10-05 07:53:42 -04:00
function! s:get_id(main) " {{{1
2015-03-07 17:02:15 -05:00
if exists('g:vimtex#data') && !empty(g:vimtex#data)
2013-10-05 07:53:42 -04:00
let id = 0
2015-03-07 17:02:15 -05:00
while id < len(g:vimtex#data)
if g:vimtex#data[id].tex == a:main
2013-10-05 07:53:42 -04:00
return id
endif
let id += 1
endwhile
endif
return -1
endfunction
function! s:get_main() " {{{1
"
" Use buffer variable if it exists
"
if exists('b:vimtex_main') && filereadable(b:vimtex_main)
return fnamemodify(b:vimtex_main, ':p')
endif
"
" Search for main file specifier at the beginning of file. Recognized
" specifiers are:
"
" 1. The TEX root specifier, which is used by by several other plugins and
" editors.
" 2. Subfiles package specifier. This parses the main tex file option in the
" \documentclass line for the subfiles package.
"
for regexp in [
\ '^\c\s*%\s*!\?\s*tex\s\+root\s*=\s*\zs.*\ze\s*$',
\ '^\C\s*\\documentclass\[\zs.*\ze\]{subfiles}',
\ ]
for line in getline(1, 5)
let filename = matchstr(line, regexp)
if len(filename) > 0
if filename[0] !=# '/'
let candidates = [
\ expand('%:h') . '/' . filename,
\ getcwd() . '/' . filename,
\ ]
else
let candidates = [fnamemodify(filename, ':p')]
endif
for main in candidates
if filereadable(main)
return main
endif
endfor
endif
endfor
endfor
"
" Search for main file recursively through \input and \include specifiers
"
let main = s:get_main_recurse(expand('%:p'))
if filereadable(main)
return main
endif
"
" If not found, use current file
"
return expand('%:p')
endfunction
function! s:get_main_recurse(file) " {{{1
2013-12-26 06:19:53 -05:00
"
" Check if file is readable
"
if !filereadable(a:file)
return 0
endif
"
" Check if current file is a main file
"
if len(filter(readfile(a:file),
2015-03-20 16:30:59 -04:00
\ 'v:val =~# ''\C\\begin\_\s*{document}''')) > 0
return fnamemodify(a:file, ':p')
endif
"
" Gather candidate files
"
let l:path = expand('%:p:h')
let l:dirs = l:path
2014-07-17 12:43:44 -04:00
while l:path != fnamemodify(l:path, ':h')
let l:path = fnamemodify(l:path, ':h')
let l:dirs .= ',' . l:path
endwhile
2014-10-30 14:54:49 -04:00
let l:candidates = split(globpath(l:dirs, '*.tex'), '\n')
"
" Search through candidates for \include{current file}
"
for l:file in l:candidates
2015-04-23 01:49:09 -04:00
" Avoid infinite recursion (checking the same file repeatedly)
if l:file == a:file | continue | endif
if len(filter(readfile(l:file), 'v:val =~ ''\v\\(input|include)\{'
2014-11-19 03:58:48 -05:00
\ . '\s*((.*)\/)?'
\ . fnamemodify(a:file, ':t:r') . '(\.tex)?\s*''')) > 0
return s:get_main_recurse(l:file)
endif
endfor
"
" If not found, return 0
"
return 0
2013-10-05 07:53:42 -04:00
endfunction
function! s:get_main_ext(self, ext) " {{{1
" First check build dir (latexmk -output_directory option)
if g:vimtex_latexmk_build_dir !=# ''
let cand = g:vimtex_latexmk_build_dir . '/' . a:self.name . '.' . a:ext
if g:vimtex_latexmk_build_dir[0] !=# '/'
let cand = a:self.root . '/' . cand
2013-10-05 07:53:42 -04:00
endif
if filereadable(cand)
return fnamemodify(cand, ':p')
endif
endif
" Next check for file in project root folder
let cand = a:self.root . '/' . a:self.name . '.' . a:ext
if filereadable(cand)
return fnamemodify(cand, ':p')
endif
" Finally return empty string if no entry is found
return ''
endfunction
2015-02-27 02:30:58 -05:00
" }}}1
function! s:print_dict(dict, ...) " {{{1
let level = a:0 > 0 ? a:1 : 0
for entry in sort(sort(items(a:dict),
2015-03-20 16:30:59 -04:00
\ 's:print_dict_sort_2'),
\ 's:print_dict_sort_1')
let title = repeat(' ', 2 + 2*level) . entry[0]
2015-02-27 02:30:58 -05:00
if type(entry[1]) == type([])
call vimtex#echo#echo(title)
2015-02-27 02:30:58 -05:00
for val in entry[1]
call vimtex#echo#echo(repeat(' ', 4 + 2*level) . string(val), 'None')
2015-02-27 02:30:58 -05:00
endfor
elseif type(entry[1]) == type({})
call vimtex#echo#echo(title . "\n")
2015-02-27 02:30:58 -05:00
call s:print_dict(entry[1], level + 1)
else
call vimtex#echo#formatted([title . ' : ',
\ ['None', string(entry[1]) . "\n"]])
2015-02-27 02:30:58 -05:00
endif
endfor
2013-10-05 07:53:42 -04:00
endfunction
2015-02-27 02:30:58 -05:00
" }}}1
function! s:print_dict_sort_1(i1, i2) " {{{1
return type(a:i1[1]) - type(a:i2[1])
endfunction
" }}}1
function! s:print_dict_sort_2(i1, i2) " {{{1
return string(a:i1[1]) == string(a:i2[1]) ? 0
\ : string(a:i1[1]) > string(a:i2[1]) ? 1
\ : -1
endfunction
2013-10-05 07:53:42 -04:00
" }}}1
2014-12-08 14:44:17 -05:00
" vim: fdm=marker sw=2