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-11-07 13:38:15 -05:00
|
|
|
" {{{1 Script Initialization
|
2015-06-06 17:49:28 -04:00
|
|
|
|
|
|
|
"
|
|
|
|
" The flag s:initialized is set to 1 after vimtex has been initialized to
|
|
|
|
" prevent errors if the scripts are loaded more than once (e.g. when opening
|
|
|
|
" more than one LaTeX buffer in one vim instance). Thus it allows us to
|
|
|
|
" distinguish between global initialization and buffer initialization.
|
|
|
|
"
|
2015-03-23 14:18:19 -04:00
|
|
|
if !exists('s:initialized')
|
|
|
|
let s:initialized = 0
|
|
|
|
endif
|
2014-07-15 08:52:45 -04:00
|
|
|
|
2015-06-06 17:49:28 -04:00
|
|
|
"
|
|
|
|
" Define list of vimtex modules
|
|
|
|
"
|
|
|
|
if !exists('s:modules')
|
|
|
|
let s:modules = map(
|
|
|
|
\ split(
|
|
|
|
\ globpath(
|
|
|
|
\ fnamemodify(expand('<sfile>'), ':r'),
|
|
|
|
\ '*.vim'),
|
|
|
|
\ '\n'),
|
|
|
|
\ 'fnamemodify(v:val, '':t:r'')')
|
|
|
|
endif
|
2013-10-05 07:53:42 -04:00
|
|
|
|
2015-06-06 17:49:28 -04:00
|
|
|
" }}}1
|
2013-10-05 07:53:42 -04:00
|
|
|
|
2015-06-06 17:49:28 -04:00
|
|
|
function! vimtex#init() " {{{1
|
2015-11-10 17:26:24 -05:00
|
|
|
call s:check_version()
|
2013-10-05 07:53:42 -04:00
|
|
|
"
|
2015-06-06 17:49:28 -04:00
|
|
|
" First initialize buffer options and construct (if necessary) the vimtex
|
|
|
|
" data blob.
|
2013-10-05 07:53:42 -04:00
|
|
|
"
|
2015-06-06 17:49:28 -04:00
|
|
|
call s:init_buffer()
|
2013-10-05 07:53:42 -04:00
|
|
|
|
2015-06-06 17:49:28 -04:00
|
|
|
"
|
|
|
|
" Then we initialize the modules. This is done in three steps:
|
|
|
|
"
|
|
|
|
" 1. Initialize options (load default options if not otherwise set). This is
|
|
|
|
" only done once for each vim session.
|
|
|
|
"
|
|
|
|
" 2. Initialize module scripts (set script variables and similar). This is
|
|
|
|
" also only done once for each vim session.
|
|
|
|
"
|
|
|
|
" 3. Initialize module for current buffer. This is done for each new LaTeX
|
|
|
|
" buffer.
|
|
|
|
"
|
2014-07-16 05:10:34 -04:00
|
|
|
if !s:initialized
|
2015-06-06 17:49:28 -04:00
|
|
|
call s:init_modules('options')
|
|
|
|
call s:init_modules('script')
|
2015-06-05 17:55:34 -04:00
|
|
|
endif
|
2015-06-06 17:49:28 -04:00
|
|
|
call s:init_modules('buffer')
|
2013-10-05 07:53:42 -04:00
|
|
|
|
2015-06-22 14:45:10 -04:00
|
|
|
"
|
2016-01-14 16:19:12 -05:00
|
|
|
" Initialize local blob (if main file is different then current file)
|
2015-06-22 14:45:10 -04:00
|
|
|
"
|
2016-01-14 16:19:12 -05:00
|
|
|
call s:init_local_blob()
|
2015-11-06 17:51:39 -05:00
|
|
|
|
2016-01-14 08:57:35 -05:00
|
|
|
"
|
2016-01-14 16:19:12 -05:00
|
|
|
" Finally we create the mappings
|
2016-01-14 08:57:35 -05:00
|
|
|
"
|
2016-01-14 16:19:12 -05:00
|
|
|
call s:init_mappings()
|
2016-01-14 08:57:35 -05:00
|
|
|
|
2016-01-14 16:19:12 -05:00
|
|
|
let s:initialized = 1
|
2016-01-14 08:57:35 -05:00
|
|
|
|
2015-11-06 17:43:17 -05:00
|
|
|
"
|
|
|
|
" Allow custom configuration through an event hook
|
|
|
|
"
|
2015-11-10 04:35:12 -05:00
|
|
|
if exists('#User#VimtexEventInitPost')
|
|
|
|
doautocmd User VimtexEventInitPost
|
|
|
|
endif
|
2015-05-16 16:14:52 -04:00
|
|
|
endfunction
|
|
|
|
|
2013-10-05 07:53:42 -04:00
|
|
|
" }}}1
|
2015-11-10 17:38:56 -05:00
|
|
|
function! vimtex#info(global) " {{{1
|
|
|
|
if !s:initialized
|
|
|
|
echoerr 'Error: vimtex has not been initialized!'
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:global
|
|
|
|
for [id, data] in items(g:vimtex_data)
|
|
|
|
let d = deepcopy(data)
|
|
|
|
for f in ['aux', 'out', 'log']
|
|
|
|
silent execute 'let d.' . f . ' = data.' . f . '()'
|
|
|
|
endfor
|
|
|
|
|
|
|
|
call vimtex#echo#formatted([
|
|
|
|
\ "\ng:vimtex_data[", ['VimtexSuccess', id], '] : ',
|
|
|
|
\ ['VimtexSuccess', remove(d, 'name') . "\n"]])
|
|
|
|
call s:print_dict(d)
|
|
|
|
endfor
|
|
|
|
else
|
|
|
|
let d = deepcopy(b:vimtex)
|
|
|
|
for f in ['aux', 'out', 'log']
|
|
|
|
silent execute 'let d.' . f . ' = b:vimtex.' . f . '()'
|
|
|
|
endfor
|
|
|
|
call vimtex#echo#formatted([
|
|
|
|
\ 'b:vimtex : ',
|
|
|
|
\ ['VimtexSuccess', remove(d, 'name') . "\n"]])
|
|
|
|
call s:print_dict(d)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
2016-06-07 09:24:27 -04:00
|
|
|
function! vimtex#wc(detailed, ...) " {{{1
|
2015-11-10 17:38:56 -05:00
|
|
|
" Run texcount, save output to lines variable
|
|
|
|
let cmd = 'cd ' . vimtex#util#shellescape(b:vimtex.root)
|
|
|
|
let cmd .= '; texcount -nosub -sum '
|
2016-06-07 09:24:27 -04:00
|
|
|
let cmd .= a:0 > 0 ? '-letter ' : ''
|
2015-11-10 17:38:56 -05:00
|
|
|
let cmd .= a:detailed > 0 ? '-inc ' : '-merge '
|
|
|
|
let cmd .= vimtex#util#shellescape(b:vimtex.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 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
|
|
|
|
|
2015-11-15 17:01:02 -05:00
|
|
|
" }}}1
|
|
|
|
" {{{1 function! vimtex#reload()
|
|
|
|
let s:file = expand('<sfile>')
|
|
|
|
|
|
|
|
if !exists('s:reloading_script')
|
|
|
|
function! vimtex#reload()
|
|
|
|
let s:reloading_script = 1
|
|
|
|
|
|
|
|
let l:scripts = [s:file]
|
|
|
|
\ + map(copy(s:modules),
|
|
|
|
\ 'fnamemodify(s:file, '':h'') . ''/vimtex/'' . v:val . ''.vim''')
|
|
|
|
|
|
|
|
for l:file in l:scripts
|
|
|
|
execute 'source' l:file
|
|
|
|
endfor
|
|
|
|
|
|
|
|
let s:initialized = 0
|
|
|
|
call vimtex#init()
|
|
|
|
|
2016-02-23 15:03:53 -05:00
|
|
|
" Reload indent file
|
|
|
|
if exists('b:did_vimtex_indent')
|
|
|
|
unlet b:did_indent
|
|
|
|
runtime indent/tex.vim
|
|
|
|
endif
|
|
|
|
|
2015-11-15 17:01:02 -05:00
|
|
|
call vimtex#echo#formatted([
|
|
|
|
\ 'vimtex: ', ['VimtexWarning', 'reloaded']])
|
|
|
|
|
|
|
|
unlet s:reloading_script
|
|
|
|
endfunction
|
|
|
|
endif
|
|
|
|
|
2016-01-14 08:57:35 -05:00
|
|
|
" }}}1
|
2016-01-15 17:37:08 -05:00
|
|
|
function! vimtex#toggle_main() " {{{1
|
2016-01-14 16:19:12 -05:00
|
|
|
if exists('b:vimtex_local')
|
|
|
|
let b:vimtex_local.active = !b:vimtex_local.active
|
2016-01-14 08:57:35 -05:00
|
|
|
|
2016-01-14 16:19:12 -05:00
|
|
|
let b:vimtex_id = b:vimtex_local.active
|
|
|
|
\ ? b:vimtex_local.sub_id
|
|
|
|
\ : b:vimtex_local.main_id
|
2016-01-14 08:57:35 -05:00
|
|
|
let b:vimtex = g:vimtex_data[b:vimtex_id]
|
|
|
|
|
|
|
|
call vimtex#echo#status(['vimtex: ',
|
|
|
|
\ ['Normal', 'Changed to `'],
|
|
|
|
\ ['VimtexSuccess', b:vimtex.base],
|
|
|
|
\ ['Normal', "' "],
|
2016-01-14 16:19:12 -05:00
|
|
|
\ ['VimtexInfo', b:vimtex_local.active ? '[local]' : '[main]' ]])
|
2016-01-14 08:57:35 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2015-11-10 17:38:56 -05:00
|
|
|
" }}}1
|
|
|
|
|
2013-10-05 07:53:42 -04:00
|
|
|
|
2015-11-10 17:26:24 -05:00
|
|
|
function! s:check_version() " {{{1
|
|
|
|
if s:initialized || get(g:, 'vimtex_disable_version_warning', 0)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2016-03-27 16:09:06 -04:00
|
|
|
if v:version <= 703 && !has('patch544')
|
|
|
|
echoerr 'vimtex error: Please use Vim version 7.3.544 or newer!'
|
|
|
|
endif
|
2015-11-10 17:26:24 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
2015-11-10 17:38:56 -05:00
|
|
|
|
2015-06-06 17:49:28 -04:00
|
|
|
function! s:init_buffer() " {{{1
|
|
|
|
"
|
|
|
|
" First we set some vim options
|
|
|
|
"
|
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
|
2015-04-01 09:56:27 -04:00
|
|
|
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*'
|
|
|
|
|
2016-02-16 16:35:09 -05:00
|
|
|
let &l:include = '\v\\%(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
|
2015-06-06 17:49:28 -04:00
|
|
|
|
|
|
|
"
|
|
|
|
" Next we initialize the data blob
|
|
|
|
"
|
|
|
|
|
|
|
|
" Create container for data blobs if it does not exist
|
2015-11-10 04:35:12 -05:00
|
|
|
let g:vimtex_data = get(g:, 'vimtex_data', {})
|
2015-06-06 17:49:28 -04:00
|
|
|
|
|
|
|
" Get main file number and check if data blob already exists
|
|
|
|
let main = s:get_main()
|
|
|
|
let id = s:get_id(main)
|
|
|
|
if id >= 0
|
|
|
|
" Link to existing blob
|
|
|
|
let b:vimtex_id = id
|
2015-11-10 13:33:40 -05:00
|
|
|
let b:vimtex = g:vimtex_data[id]
|
2015-06-06 17:49:28 -04:00
|
|
|
else
|
|
|
|
" Create new blob
|
|
|
|
let b:vimtex = {}
|
|
|
|
let b:vimtex.tex = main
|
|
|
|
let b:vimtex.root = fnamemodify(b:vimtex.tex, ':h')
|
|
|
|
let b:vimtex.base = fnamemodify(b:vimtex.tex, ':t')
|
|
|
|
let b:vimtex.name = fnamemodify(b:vimtex.tex, ':t:r')
|
|
|
|
function b:vimtex.aux() dict
|
|
|
|
return s:get_main_ext(self, 'aux')
|
|
|
|
endfunction
|
|
|
|
function b:vimtex.log() dict
|
|
|
|
return s:get_main_ext(self, 'log')
|
|
|
|
endfunction
|
|
|
|
function b:vimtex.out() dict
|
|
|
|
return s:get_main_ext(self, 'pdf')
|
|
|
|
endfunction
|
|
|
|
|
2015-11-10 04:35:12 -05:00
|
|
|
let s:vimtex_next_id = get(s:, 'vimtex_next_id', -1) + 1
|
|
|
|
let b:vimtex_id = s:vimtex_next_id
|
|
|
|
let g:vimtex_data[b:vimtex_id] = b:vimtex
|
2015-06-06 17:49:28 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
"
|
2015-06-20 17:16:53 -04:00
|
|
|
" Define commands and mappings
|
2015-06-06 17:49:28 -04:00
|
|
|
"
|
|
|
|
|
|
|
|
" Define commands
|
2016-06-07 09:24:27 -04:00
|
|
|
command! -buffer -bang VimtexInfo call vimtex#info(<q-bang> == "!")
|
|
|
|
command! -buffer -bang VimtexCountWords call vimtex#wc(<q-bang> == "!")
|
|
|
|
command! -buffer -bang VimtexCountLetters call vimtex#wc(<q-bang> == "!", 1)
|
|
|
|
command! -buffer VimtexReload call vimtex#reload()
|
|
|
|
command! -buffer VimtexToggleMain call vimtex#toggle_main()
|
2015-06-06 17:49:28 -04:00
|
|
|
|
|
|
|
" Define mappings
|
2016-01-15 17:37:08 -05:00
|
|
|
nnoremap <buffer> <plug>(vimtex-info) :VimtexInfo<cr>
|
|
|
|
nnoremap <buffer> <plug>(vimtex-info-full) :VimtexInfo!<cr>
|
|
|
|
nnoremap <buffer> <plug>(vimtex-reload) :VimtexReload<cr>
|
|
|
|
nnoremap <buffer> <plug>(vimtex-toggle-main) :VimtexToggleMain<cr>
|
2015-06-20 17:16:53 -04:00
|
|
|
|
|
|
|
"
|
|
|
|
" Attach autocommands
|
|
|
|
"
|
2015-11-07 17:28:03 -05:00
|
|
|
|
2015-11-06 17:39:14 -05:00
|
|
|
augroup vimtex_buffers
|
2015-06-20 17:16:53 -04:00
|
|
|
au BufFilePre <buffer> call s:filename_changed_pre()
|
|
|
|
au BufFilePost <buffer> call s:filename_changed_post()
|
2015-11-06 17:39:14 -05:00
|
|
|
au BufLeave <buffer> call s:buffer_left()
|
|
|
|
au BufDelete <buffer> call s:buffer_deleted()
|
2015-11-10 04:35:12 -05:00
|
|
|
au QuitPre <buffer> silent! doautocmd User VimtexEventQuit
|
2015-06-20 17:16:53 -04:00
|
|
|
augroup END
|
2015-06-06 17:49:28 -04:00
|
|
|
endfunction
|
|
|
|
|
2015-06-22 14:45:10 -04:00
|
|
|
" }}}1
|
|
|
|
function! s:init_mappings() " {{{1
|
|
|
|
if !get(g:,'vimtex_mappings_enabled', 1) | return | endif
|
|
|
|
|
2016-02-09 15:42:48 -05:00
|
|
|
function! s:map(mode, lhs, rhs, ...)
|
2016-06-28 09:13:51 -04:00
|
|
|
if (a:0 > 0) || (maparg(a:lhs, a:mode) ==# '')
|
2015-11-07 17:28:03 -05:00
|
|
|
silent execute a:mode . 'map <silent><buffer>' a:lhs a:rhs
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call s:map('n', '<localleader>li', '<plug>(vimtex-info)')
|
2015-11-07 17:28:38 -05:00
|
|
|
call s:map('n', '<localleader>lI', '<plug>(vimtex-info-full)')
|
2015-11-15 17:01:02 -05:00
|
|
|
call s:map('n', '<localleader>lx', '<plug>(vimtex-reload)')
|
2016-01-15 17:37:08 -05:00
|
|
|
call s:map('n', '<localleader>ls', '<plug>(vimtex-toggle-main)')
|
2015-06-22 14:45:10 -04:00
|
|
|
|
2016-02-13 16:45:14 -05:00
|
|
|
call s:map('n', 'ds$', '<plug>(vimtex-env-delete-math)')
|
|
|
|
call s:map('n', 'cs$', '<plug>(vimtex-env-change-math)')
|
2016-02-06 14:21:27 -05:00
|
|
|
call s:map('n', 'dse', '<plug>(vimtex-env-delete)')
|
|
|
|
call s:map('n', 'cse', '<plug>(vimtex-env-change)')
|
|
|
|
call s:map('n', 'tse', '<plug>(vimtex-env-toggle-star)')
|
|
|
|
|
|
|
|
call s:map('n', 'dsc', '<plug>(vimtex-cmd-delete)')
|
|
|
|
call s:map('n', 'csc', '<plug>(vimtex-cmd-change)')
|
|
|
|
call s:map('n', '<F7>', '<plug>(vimtex-cmd-create)')
|
|
|
|
call s:map('i', '<F7>', '<plug>(vimtex-cmd-create)')
|
|
|
|
|
|
|
|
call s:map('n', 'tsd', '<plug>(vimtex-delim-toggle-modifier)')
|
2016-03-22 03:34:26 -04:00
|
|
|
call s:map('v', 'tsd', '<plug>(vimtex-delim-toggle-modifier)')
|
2016-02-06 14:21:27 -05:00
|
|
|
call s:map('i', ']]', '<plug>(vimtex-delim-close)')
|
2015-06-22 14:45:10 -04:00
|
|
|
|
|
|
|
if g:vimtex_latexmk_enabled
|
2015-11-07 17:28:03 -05:00
|
|
|
call s:map('n', '<localleader>ll', '<plug>(vimtex-compile-toggle)')
|
|
|
|
call s:map('n', '<localleader>lo', '<plug>(vimtex-compile-output)')
|
|
|
|
call s:map('n', '<localleader>lk', '<plug>(vimtex-stop)')
|
|
|
|
call s:map('n', '<localleader>lK', '<plug>(vimtex-stop-all)')
|
|
|
|
call s:map('n', '<localleader>le', '<plug>(vimtex-errors)')
|
|
|
|
call s:map('n', '<localleader>lc', '<plug>(vimtex-clean)')
|
|
|
|
call s:map('n', '<localleader>lC', '<plug>(vimtex-clean-full)')
|
|
|
|
call s:map('n', '<localleader>lg', '<plug>(vimtex-status)')
|
|
|
|
call s:map('n', '<localleader>lG', '<plug>(vimtex-status-all)')
|
2015-06-22 14:45:10 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
if g:vimtex_motion_enabled
|
2015-11-07 17:28:03 -05:00
|
|
|
call s:map('n', ']]', '<plug>(vimtex-]])')
|
|
|
|
call s:map('n', '][', '<plug>(vimtex-][)')
|
|
|
|
call s:map('n', '[]', '<plug>(vimtex-[])')
|
|
|
|
call s:map('n', '[[', '<plug>(vimtex-[[)')
|
|
|
|
call s:map('x', ']]', '<plug>(vimtex-]])')
|
|
|
|
call s:map('x', '][', '<plug>(vimtex-][)')
|
|
|
|
call s:map('x', '[]', '<plug>(vimtex-[])')
|
|
|
|
call s:map('x', '[[', '<plug>(vimtex-[[)')
|
|
|
|
call s:map('o', ']]', '<plug>(vimtex-]])')
|
|
|
|
call s:map('o', '][', '<plug>(vimtex-][)')
|
|
|
|
call s:map('o', '[]', '<plug>(vimtex-[])')
|
|
|
|
call s:map('o', '[[', '<plug>(vimtex-[[)')
|
2016-02-09 15:42:48 -05:00
|
|
|
|
|
|
|
" These are forced in order to overwrite matchit mappings
|
|
|
|
call s:map('n', '%', '<plug>(vimtex-%)', 1)
|
|
|
|
call s:map('x', '%', '<plug>(vimtex-%)', 1)
|
|
|
|
call s:map('o', '%', '<plug>(vimtex-%)', 1)
|
2016-01-21 16:39:35 -05:00
|
|
|
endif
|
|
|
|
|
|
|
|
if g:vimtex_text_obj_enabled
|
2016-02-13 05:39:18 -05:00
|
|
|
call s:map('x', 'ic', '<plug>(vimtex-ic)')
|
|
|
|
call s:map('x', 'ac', '<plug>(vimtex-ac)')
|
|
|
|
call s:map('o', 'ic', '<plug>(vimtex-ic)')
|
|
|
|
call s:map('o', 'ac', '<plug>(vimtex-ac)')
|
|
|
|
call s:map('x', 'id', '<plug>(vimtex-id)')
|
|
|
|
call s:map('x', 'ad', '<plug>(vimtex-ad)')
|
|
|
|
call s:map('o', 'id', '<plug>(vimtex-id)')
|
|
|
|
call s:map('o', 'ad', '<plug>(vimtex-ad)')
|
2015-11-07 17:28:03 -05:00
|
|
|
call s:map('x', 'ie', '<plug>(vimtex-ie)')
|
|
|
|
call s:map('x', 'ae', '<plug>(vimtex-ae)')
|
|
|
|
call s:map('o', 'ie', '<plug>(vimtex-ie)')
|
|
|
|
call s:map('o', 'ae', '<plug>(vimtex-ae)')
|
|
|
|
call s:map('x', 'i$', '<plug>(vimtex-i$)')
|
|
|
|
call s:map('x', 'a$', '<plug>(vimtex-a$)')
|
|
|
|
call s:map('o', 'i$', '<plug>(vimtex-i$)')
|
|
|
|
call s:map('o', 'a$', '<plug>(vimtex-a$)')
|
2015-06-22 14:45:10 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
if g:vimtex_toc_enabled
|
2015-11-07 17:28:03 -05:00
|
|
|
call s:map('n', '<localleader>lt', '<plug>(vimtex-toc-open)')
|
|
|
|
call s:map('n', '<localleader>lT', '<plug>(vimtex-toc-toggle)')
|
2015-06-22 14:45:10 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
if g:vimtex_labels_enabled
|
2015-11-07 17:28:03 -05:00
|
|
|
call s:map('n', '<localleader>ly', '<plug>(vimtex-labels-open)')
|
|
|
|
call s:map('n', '<localleader>lY', '<plug>(vimtex-labels-toggle)')
|
2015-06-22 14:45:10 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
if g:vimtex_view_enabled
|
2015-11-07 17:28:03 -05:00
|
|
|
call s:map('n', '<localleader>lv', '<plug>(vimtex-view)')
|
2015-06-22 14:45:10 -04:00
|
|
|
if has_key(b:vimtex.viewer, 'reverse_search')
|
2015-11-07 17:28:03 -05:00
|
|
|
call s:map('n', '<localleader>lr', '<plug>(vimtex-reverse-search)')
|
2015-06-22 14:45:10 -04:00
|
|
|
endif
|
|
|
|
endif
|
2015-12-21 17:37:32 -05:00
|
|
|
|
|
|
|
if g:vimtex_imaps_enabled
|
|
|
|
call s:map('n', '<localleader>lm', '<plug>(vimtex-imaps-list)')
|
|
|
|
endif
|
2015-06-22 14:45:10 -04:00
|
|
|
endfunction
|
|
|
|
|
2015-06-06 17:49:28 -04:00
|
|
|
" }}}1
|
|
|
|
function! s:init_modules(initmode) " {{{1
|
|
|
|
for module in s:modules
|
2016-05-15 05:22:43 -04:00
|
|
|
try
|
|
|
|
execute 'call vimtex#' . module . '#init_' . a:initmode . '()'
|
|
|
|
catch /E117.*#init_/
|
|
|
|
endtry
|
2015-06-06 17:49:28 -04:00
|
|
|
endfor
|
2014-07-16 17:08:00 -04:00
|
|
|
endfunction
|
2015-04-01 09:54:14 -04:00
|
|
|
|
2016-01-14 16:19:12 -05:00
|
|
|
" }}}1
|
|
|
|
function! s:init_local_blob() " {{{1
|
|
|
|
let l:filename = expand('%:p')
|
|
|
|
|
|
|
|
if b:vimtex.tex !=# l:filename
|
|
|
|
let l:local = deepcopy(b:vimtex)
|
|
|
|
let l:local.tex = l:filename
|
|
|
|
let l:local.pid = 0
|
|
|
|
let l:local.name = fnamemodify(l:filename, ':t:r')
|
|
|
|
let l:local.root = fnamemodify(l:filename, ':h')
|
|
|
|
let l:local.base = fnamemodify(l:filename, ':t')
|
|
|
|
|
|
|
|
let s:vimtex_next_id += 1
|
|
|
|
let g:vimtex_data[s:vimtex_next_id] = l:local
|
|
|
|
|
|
|
|
let b:vimtex_local = {
|
|
|
|
\ 'active' : 0,
|
|
|
|
\ 'main_id' : b:vimtex_id,
|
|
|
|
\ 'sub_id' : s:vimtex_next_id,
|
|
|
|
\}
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-07-16 17:08:00 -04:00
|
|
|
" }}}1
|
2013-10-05 07:53:42 -04:00
|
|
|
|
2014-07-15 08:52:45 -04:00
|
|
|
function! s:get_id(main) " {{{1
|
2015-11-10 04:35:12 -05:00
|
|
|
for [id, data] in items(g:vimtex_data)
|
|
|
|
if data.tex == a:main
|
|
|
|
return id
|
|
|
|
endif
|
|
|
|
endfor
|
2013-10-05 07:53:42 -04:00
|
|
|
|
|
|
|
return -1
|
|
|
|
endfunction
|
|
|
|
|
2014-07-15 08:52:45 -04:00
|
|
|
function! s:get_main() " {{{1
|
2015-04-26 03:02:28 -04:00
|
|
|
"
|
|
|
|
" Use buffer variable if it exists
|
|
|
|
"
|
|
|
|
if exists('b:vimtex_main') && filereadable(b:vimtex_main)
|
|
|
|
return fnamemodify(b:vimtex_main, ':p')
|
|
|
|
endif
|
|
|
|
|
2013-12-10 11:42:25 -05:00
|
|
|
"
|
2016-01-13 17:31:59 -05:00
|
|
|
" Search for TEX root specifier at the beginning of file. This is used by
|
|
|
|
" several other plugins and editors.
|
2013-12-10 11:42:25 -05:00
|
|
|
"
|
2016-01-13 17:31:59 -05:00
|
|
|
let l:candidate = s:get_main_from_specifier(
|
|
|
|
\ '^\c\s*%\s*!\?\s*tex\s\+root\s*=\s*\zs.*\ze\s*$')
|
|
|
|
if l:candidate !=# ''
|
|
|
|
return l:candidate
|
|
|
|
endif
|
|
|
|
|
2015-04-29 09:40:08 -04:00
|
|
|
"
|
2016-01-13 17:31:59 -05:00
|
|
|
" Support for subfiles package
|
|
|
|
"
|
|
|
|
let l:candidate = s:get_main_from_specifier(
|
|
|
|
\ '^\C\s*\\documentclass\[\zs.*\ze\]{subfiles}')
|
|
|
|
if l:candidate !=# ''
|
|
|
|
return l:candidate
|
|
|
|
endif
|
2013-12-10 11:42:25 -05:00
|
|
|
|
2015-07-10 16:45:42 -04:00
|
|
|
"
|
|
|
|
" Search for .latexmain-specifier
|
|
|
|
"
|
2016-01-13 17:31:59 -05:00
|
|
|
let l:candidate = s:get_main_latexmain(expand('%:p'))
|
|
|
|
if l:candidate !=# ''
|
|
|
|
return l:candidate
|
2015-07-10 16:45:42 -04:00
|
|
|
endif
|
|
|
|
|
2013-12-10 11:42:25 -05:00
|
|
|
"
|
2016-01-12 17:14:02 -05:00
|
|
|
" Search for main file recursively through include specifiers
|
2013-12-10 11:42:25 -05:00
|
|
|
"
|
2016-01-13 17:31:59 -05:00
|
|
|
let l:candidate = s:get_main_recurse(expand('%:p'))
|
|
|
|
if l:candidate !=# ''
|
|
|
|
return l:candidate
|
2013-12-10 11:42:25 -05:00
|
|
|
endif
|
|
|
|
|
|
|
|
"
|
|
|
|
" If not found, use current file
|
|
|
|
"
|
|
|
|
return expand('%:p')
|
|
|
|
endfunction
|
|
|
|
|
2016-01-13 17:31:59 -05:00
|
|
|
" }}}1
|
|
|
|
function! s:get_main_from_specifier(spec) " {{{1
|
|
|
|
for l:line in getline(1, 5)
|
|
|
|
let l:filename = matchstr(l:line, a:spec)
|
|
|
|
if len(l:filename) > 0
|
|
|
|
if l:filename[0] ==# '/'
|
|
|
|
if filereadable(l:filename) | return l:filename | endif
|
|
|
|
else
|
2016-06-22 16:31:42 -04:00
|
|
|
" The candidate may be relative both to the current buffer file and to
|
|
|
|
" the working directory (for subfile package)
|
|
|
|
for l:candidate in map([
|
|
|
|
\ expand('%:p:h'),
|
|
|
|
\ getcwd()],
|
|
|
|
\ 'simplify(v:val . ''/'' . l:filename)')
|
2016-01-13 17:31:59 -05:00
|
|
|
if filereadable(l:candidate) | return l:candidate | endif
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2015-06-20 17:16:53 -04:00
|
|
|
" }}}1
|
2015-07-10 16:45:42 -04:00
|
|
|
function! s:get_main_latexmain(file) " {{{1
|
|
|
|
"
|
|
|
|
" Gather candidate files
|
|
|
|
"
|
|
|
|
let l:path = expand('%:p:h')
|
|
|
|
let l:dirs = l:path
|
|
|
|
while l:path != fnamemodify(l:path, ':h')
|
|
|
|
let l:path = fnamemodify(l:path, ':h')
|
|
|
|
let l:dirs .= ',' . l:path
|
|
|
|
endwhile
|
2015-10-04 16:49:07 -04:00
|
|
|
let l:candidates = split(globpath(fnameescape(l:dirs), '*.latexmain'), '\n')
|
2015-07-10 16:45:42 -04:00
|
|
|
|
2013-12-26 06:19:53 -05:00
|
|
|
"
|
2015-07-10 16:45:42 -04:00
|
|
|
" If any candidates found, use the first one (corresponding to the one
|
|
|
|
" closest to the current file in the directory tree)
|
2013-12-26 06:19:53 -05:00
|
|
|
"
|
2015-07-10 16:45:42 -04:00
|
|
|
if len(l:candidates) > 0
|
2016-01-13 17:31:59 -05:00
|
|
|
let l:candidate = fnamemodify(l:candidates[0], ':p:r')
|
|
|
|
return filereadable(l:candidate) ? l:candidate : ''
|
2013-12-26 06:19:53 -05:00
|
|
|
endif
|
2015-07-10 16:45:42 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:get_main_recurse(file) " {{{1
|
2016-01-13 17:31:59 -05:00
|
|
|
if !filereadable(a:file) | return '' | endif
|
2013-12-26 06:19:53 -05:00
|
|
|
|
2013-12-10 11:42:25 -05:00
|
|
|
"
|
|
|
|
" Check if current file is a main file
|
|
|
|
"
|
|
|
|
if len(filter(readfile(a:file),
|
2015-07-07 16:54:24 -04:00
|
|
|
\ 'v:val =~# ''\C\\documentclass\_\s*[\[{]''')) > 0
|
2013-12-10 11:42:25 -05:00
|
|
|
return fnamemodify(a:file, ':p')
|
2013-12-08 13:32:30 -05:00
|
|
|
endif
|
2013-12-10 11:42:25 -05:00
|
|
|
|
|
|
|
"
|
2014-07-16 05:37:39 -04:00
|
|
|
" Gather candidate files
|
2013-12-10 11:42:25 -05:00
|
|
|
"
|
2014-07-16 05:37:39 -04:00
|
|
|
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')
|
2014-07-16 05:37:39 -04:00
|
|
|
let l:path = fnamemodify(l:path, ':h')
|
|
|
|
let l:dirs .= ',' . l:path
|
|
|
|
endwhile
|
2015-10-04 16:49:07 -04:00
|
|
|
let l:candidates = split(globpath(fnameescape(l:dirs), '*.tex'), '\n')
|
2014-07-16 05:37:39 -04:00
|
|
|
|
|
|
|
"
|
2016-01-12 17:14:02 -05:00
|
|
|
" Search through candidates
|
2014-07-16 05:37:39 -04:00
|
|
|
"
|
|
|
|
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
|
|
|
|
|
2016-01-12 17:14:02 -05:00
|
|
|
let l:file_re = '\s*((.*)\/)?' . fnamemodify(a:file, ':t:r')
|
|
|
|
|
|
|
|
let l:filter = 'v:val =~# ''\v'
|
2016-02-16 16:35:09 -05:00
|
|
|
let l:filter .= '\\%(input|include)\{' . l:file_re
|
2016-01-12 17:14:02 -05:00
|
|
|
let l:filter .= '|\\subimport\{[^\}]*\}\{' . l:file_re
|
|
|
|
let l:filter .= ''''
|
|
|
|
|
|
|
|
if len(filter(readfile(l:file), l:filter)) > 0
|
2013-12-10 11:42:25 -05:00
|
|
|
return s:get_main_recurse(l:file)
|
|
|
|
endif
|
|
|
|
endfor
|
2013-10-05 07:53:42 -04:00
|
|
|
endfunction
|
|
|
|
|
2015-05-05 14:49:29 -04:00
|
|
|
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
|
2015-05-05 14:49:29 -04:00
|
|
|
if filereadable(cand)
|
|
|
|
return fnamemodify(cand, ':p')
|
2015-01-01 09:20:07 -05:00
|
|
|
endif
|
2015-05-05 14:49:29 -04:00
|
|
|
endif
|
2015-01-01 09:20:07 -05:00
|
|
|
|
2015-05-05 14:49:29 -04:00
|
|
|
" 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
|
2015-01-01 09:20:07 -05:00
|
|
|
|
2015-05-05 14:49:29 -04:00
|
|
|
" Finally return empty string if no entry is found
|
2015-01-01 09:20:07 -05:00
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2015-02-27 02:30:58 -05:00
|
|
|
" }}}1
|
|
|
|
|
2015-06-20 17:16:53 -04:00
|
|
|
function! s:filename_changed_pre() " {{{1
|
|
|
|
let thisfile = fnamemodify(expand('%'), ':p')
|
2015-06-20 17:45:05 -04:00
|
|
|
let s:filename_changed = thisfile ==# b:vimtex.tex
|
|
|
|
let s:filename_old = b:vimtex.base
|
2015-06-20 17:16:53 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
function! s:filename_changed_post() " {{{1
|
2015-06-20 17:45:05 -04:00
|
|
|
if s:filename_changed
|
2015-06-20 17:16:53 -04:00
|
|
|
let b:vimtex.tex = fnamemodify(expand('%'), ':p')
|
|
|
|
let b:vimtex.base = fnamemodify(b:vimtex.tex, ':t')
|
|
|
|
let b:vimtex.name = fnamemodify(b:vimtex.tex, ':t:r')
|
2015-06-20 17:45:05 -04:00
|
|
|
let message = ['vimtex: ',
|
|
|
|
\ ['VimtexWarning', 'Filename change detected!'],
|
|
|
|
\ "\n Old filename: ", ['VimtexInfo', s:filename_old],
|
|
|
|
\ "\n New filename: ", ['VimtexInfo', b:vimtex.base]]
|
|
|
|
|
|
|
|
if b:vimtex.pid
|
|
|
|
let message += ["\n latexmk process: ",
|
|
|
|
\ ['VimtexInfo', b:vimtex.pid],
|
|
|
|
\ ['VimtexWarning', ' killed!']]
|
|
|
|
call vimtex#latexmk#stop()
|
|
|
|
endif
|
|
|
|
|
|
|
|
call vimtex#echo#status(message)
|
2015-06-20 17:16:53 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
|
2015-02-27 02:30:58 -05:00
|
|
|
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')
|
2015-03-10 17:28:33 -04:00
|
|
|
let title = repeat(' ', 2 + 2*level) . entry[0]
|
2015-02-27 02:30:58 -05:00
|
|
|
if type(entry[1]) == type([])
|
2015-03-10 17:28:33 -04:00
|
|
|
call vimtex#echo#echo(title)
|
2015-02-27 02:30:58 -05:00
|
|
|
for val in entry[1]
|
2015-03-10 17:28:33 -04:00
|
|
|
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({})
|
2015-03-10 17:28:33 -04:00
|
|
|
call vimtex#echo#echo(title . "\n")
|
2015-02-27 02:30:58 -05:00
|
|
|
call s:print_dict(entry[1], level + 1)
|
|
|
|
else
|
2015-03-10 17:28:33 -04:00
|
|
|
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
|
|
|
|
|
2015-11-06 17:39:14 -05:00
|
|
|
function! s:buffer_left() " {{{1
|
|
|
|
let s:vimtex_id = b:vimtex_id
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
function! s:buffer_deleted() " {{{1
|
2015-11-10 04:35:12 -05:00
|
|
|
"
|
2015-11-06 17:39:14 -05:00
|
|
|
" Check if the deleted buffer was the last remaining buffer of an opened
|
|
|
|
" latex project
|
2015-11-10 04:35:12 -05:00
|
|
|
"
|
2015-11-18 02:32:15 -05:00
|
|
|
if !exists('s:vimtex_id') | return | endif
|
2015-11-10 04:35:12 -05:00
|
|
|
if !has_key(g:vimtex_data, s:vimtex_id) | return | endif
|
|
|
|
|
2015-11-06 17:39:14 -05:00
|
|
|
let l:listed_buffers = filter(range(1, bufnr('$')), 'buflisted(v:val)')
|
|
|
|
let l:vimtex_ids = map(l:listed_buffers, 'getbufvar(v:val, ''vimtex_id'', -1)')
|
|
|
|
if count(l:vimtex_ids, s:vimtex_id) - 1 <= 0
|
2015-11-10 04:35:12 -05:00
|
|
|
let l:vimtex = remove(g:vimtex_data, s:vimtex_id)
|
2015-11-06 17:39:14 -05:00
|
|
|
|
2015-11-10 04:35:12 -05:00
|
|
|
if exists('#User#VimtexEventQuit')
|
|
|
|
if exists('b:vimtex')
|
|
|
|
let b:vimtex_tmp = b:vimtex
|
|
|
|
endif
|
|
|
|
let b:vimtex = l:vimtex
|
|
|
|
doautocmd User VimtexEventQuit
|
|
|
|
if exists('b:vimtex_tmp')
|
|
|
|
unlet b:vimtex_tmp
|
|
|
|
else
|
|
|
|
unlet b:vimtex
|
|
|
|
endif
|
|
|
|
endif
|
2015-11-06 17:39:14 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
|
2014-12-08 14:44:17 -05:00
|
|
|
" vim: fdm=marker sw=2
|