vimtex/autoload/latex.vim

413 lines
12 KiB
VimL
Raw Normal View History

2014-07-22 18:08:57 -04:00
" LaTeX plugin for Vim
"
" Maintainer: Karl Yngve Lervåg
" Email: karl.yngve@gmail.com
"
" vim-latex is not initialized until latex#init() has been run once
2013-10-05 07:53:42 -04:00
let s:initialized = 0
function! latex#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()
call s:init_errorformat()
call latex#toc#init(s:initialized)
call latex#fold#init(s:initialized)
call latex#motion#init(s:initialized)
call latex#change#init(s:initialized)
call latex#latexmk#init(s:initialized)
call latex#complete#init(s:initialized)
"
" This variable is used to allow a distinction between global and buffer
" initialization
"
let s:initialized = 1
endfunction
function! latex#info() " {{{1
if !s:initialized
echoerr "Error: vim-latex has not been initialized!"
return
endif
2014-08-03 03:14:05 -04:00
" Print buffer data
echo printf('%-19s%-s', 'b:latex.id', b:latex.id)
if has_key(b:latex, 'fold_parts') && !empty(b:latex.fold_parts)
2014-08-03 03:14:05 -04:00
echo 'b:latex.fold_parts'
for entry in reverse(copy(b:latex.fold_parts))
2014-08-03 03:14:05 -04:00
echo printf(' %s %s', entry[1], entry[0])
2013-10-11 17:11:31 -04:00
endfor
endif
2013-10-05 07:53:42 -04:00
2014-08-03 03:14:05 -04:00
" Print global data
let n = 0
for d in g:latex#data
echo "\n"
echo "g:latex#data[" . n . "]"
2014-08-10 15:13:21 -04:00
if has_key(d, 'pid') && d.pid
2014-08-03 03:14:05 -04:00
echo printf(' %-6s%-s', 'pid', d.pid)
2013-10-05 07:53:42 -04:00
endif
2014-08-03 03:14:05 -04:00
echo printf(' %-6s%-s', 'name', s:truncate(d.name))
echo printf(' %-6s%-s', 'base', s:truncate(d.base))
echo printf(' %-6s%-s', 'root', s:truncate(d.root))
echo printf(' %-6s%-s', 'tex', s:truncate(d.tex))
for f in ['aux', 'out', 'log']
silent execute 'let l:tmp = d.' . f . '()'
if l:tmp != ''
echo printf(' %-6s%-s', f, s:truncate(l:tmp))
2013-10-05 07:53:42 -04:00
endif
endfor
2014-08-03 03:14:05 -04:00
let cmds = items(d.cmds)
if len(cmds) > 0
for [key, val] in cmds
echo printf(' command: %-9s', key)
echo printf(' %-s', val)
endfor
endif
let n += 1
2013-10-05 07:53:42 -04:00
endfor
endfunction
function! latex#help() " {{{1
2013-10-10 15:48:52 -04:00
if g:latex_mappings_enabled
2013-10-05 07:53:42 -04:00
nmap <buffer>
2014-07-27 15:18:01 -04:00
xmap <buffer>
2013-10-05 07:53:42 -04:00
omap <buffer>
else
echo "Mappings not enabled"
2013-10-05 07:53:42 -04:00
endif
endfunction
function! latex#reinit() " {{{1
2013-10-05 07:53:42 -04:00
"
" Stop latexmk processes (if running)
"
call latex#latexmk#stop_all()
"
2013-10-06 05:44:50 -04:00
" Reset global variables
2013-10-05 07:53:42 -04:00
"
let s:initialized = 0
unlet g:latex#data
"
2013-10-06 05:44:50 -04:00
" Reset and reinitialize buffers
2013-10-05 07:53:42 -04:00
"
2013-10-10 15:48:52 -04:00
let n = bufnr('%')
2013-10-06 05:44:50 -04:00
bufdo if getbufvar('%', '&filetype') == 'tex' |
2013-10-10 15:48:52 -04:00
\ unlet b:latex |
\ call latex#init() |
2013-10-06 05:44:50 -04:00
\ endif
2013-10-10 15:48:52 -04:00
silent execute 'buffer ' . n
2013-10-05 07:53:42 -04:00
endfunction
function! latex#view(...) " {{{1
2014-08-03 04:22:49 -04:00
let args = ' '
if a:0 > 0
let args .= join(a:000, ' ')
else
2014-08-04 08:13:37 -04:00
let outfile = g:latex#data[b:latex.id].out()
if !filereadable(outfile)
echomsg "Can't view: Output file is not readable!"
return
endif
let args .= shellescape(outfile)
2014-08-03 04:22:49 -04:00
endif
let exe = {}
2014-08-03 04:22:49 -04:00
let exe.cmd = g:latex_viewer . args
call latex#util#execute(exe)
2014-08-03 03:14:05 -04:00
let g:latex#data[b:latex.id].cmds.view = exe.cmd
2013-10-05 07:53:42 -04:00
endfunction
" }}}1
function! s:init_environment() " {{{1
2013-10-05 07:53:42 -04:00
"
" Initialize global and local data blobs
"
call latex#util#set_default('g:latex#data', [])
call latex#util#set_default('b:latex', {})
2014-07-16 17:08:00 -04:00
"
" Set some file type specific vim options
2014-08-03 02:06:12 -04:00
setlocal suffixesadd+=.tex
setlocal commentstring=\%\ %s
2014-07-16 17:08:00 -04:00
2013-10-05 07:53:42 -04:00
"
2014-08-03 03:14:05 -04:00
" Create new or link to existing blob
2013-10-05 07:53:42 -04:00
"
let main = s:get_main()
2013-10-05 07:53:42 -04:00
let id = s:get_id(main)
if id >= 0
let b:latex.id = id
else
let data = {}
2014-08-03 03:14:05 -04:00
let data.cmds = {}
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, g:latex_latexmk_output)
endfunction
call add(g:latex#data, data)
let b:latex.id = len(g:latex#data) - 1
endif
2014-07-30 04:59:43 -04:00
command! -buffer VimLatexInfo call latex#info()
command! -buffer VimLatexHelp call latex#help()
command! -buffer -nargs=* VimLatexView call latex#view('<args>')
command! -buffer VimLatexReinitialize call latex#reinit()
2013-10-10 15:48:52 -04:00
if g:latex_mappings_enabled
2013-10-05 07:53:42 -04:00
nnoremap <silent><buffer> <localleader>li :call latex#info()<cr>
nnoremap <silent><buffer> <localleader>lh :call latex#help()<cr>
nnoremap <silent><buffer> <localleader>lv :call latex#view()<cr>
2014-11-24 14:25:00 -05:00
nnoremap <silent><buffer> <localleader>lR :call latex#reinit()<cr>
2013-10-05 07:53:42 -04:00
endif
endfunction
function! s:init_errorformat() " {{{1
2013-10-05 07:53:42 -04:00
"
" Note: The error formats assume we're using the -file-line-error with
" [pdf]latex. For more info, see |errorformat-LaTeX|.
"
" Push file to file stack
setlocal efm=%-P**%f
setlocal efm+=%-P**\"%f\"
2013-10-05 07:53:42 -04:00
" Match errors
2014-06-10 14:02:36 -04:00
setlocal efm+=%E!\ LaTeX\ %trror:\ %m
2013-10-05 07:53:42 -04:00
setlocal efm+=%E%f:%l:\ %m
setlocal efm+=%E!\ %m
2014-06-11 02:17:25 -04:00
" More info for undefined control sequences
setlocal efm+=%Z<argument>\ %m
2014-06-10 14:02:36 -04:00
" More info for some errors
setlocal efm+=%Cl.%l\ %m
2013-10-05 07:53:42 -04:00
" Show warnings
if ! g:latex_quickfix_ignore_all_warnings
2013-10-05 07:53:42 -04:00
" Ignore some warnings
for w in g:latex_quickfix_ignored_warnings
2013-10-05 07:53:42 -04:00
let warning = escape(substitute(w, '[\,]', '%\\\\&', 'g'), ' ')
exe 'setlocal efm+=%-G%.%#'. warning .'%.%#'
endfor
setlocal efm+=%+WLaTeX\ %.%#Warning:\ %.%#line\ %l%.%#
setlocal efm+=%+W%.%#\ at\ lines\ %l--%*\\d
setlocal efm+=%+WLaTeX\ %.%#Warning:\ %m
setlocal efm+=%+W%.%#%.%#Warning:\ %m
2014-06-11 02:17:25 -04:00
" Parse biblatex warnings
setlocal efm+=%-C(biblatex)%.%#in\ t%.%#
setlocal efm+=%-C(biblatex)%.%#Please\ v%.%#
setlocal efm+=%-C(biblatex)%.%#LaTeX\ a%.%#
setlocal efm+=%-Z(biblatex)%m
" Parse hyperref warnings
setlocal efm+=%-C(hyperref)%.%#on\ input\ line\ %l.
2013-10-05 07:53:42 -04:00
endif
" Ignore unmatched lines
setlocal efm+=%-G%.%#
endfunction
" }}}1
2014-07-16 17:08:00 -04:00
function! s:init_options() " {{{1
call latex#util#error_deprecated('g:latex_errorformat_ignore_warnings')
call latex#util#error_deprecated('g:latex_errorformat_show_warnings')
call latex#util#error_deprecated('g:latex_latexmk_autojump')
call latex#util#error_deprecated('g:latex_latexmk_quickfix')
call latex#util#error_deprecated('g:latex_latexmk_options')
2014-07-16 17:08:00 -04:00
call latex#util#set_default('g:latex_build_dir', '.')
call latex#util#set_default('g:latex_complete_enabled', 1)
call latex#util#set_default('g:latex_complete_close_braces', 0)
call latex#util#set_default('g:latex_complete_recursive_bib', 0)
call latex#util#set_default('g:latex_complete_patterns',
\ {
\ 'ref' : '\C\\v\?\(eq\|page\|[cC]\|labelc\)\?ref\*\?\_\s*{[^{}]*',
\ 'bib' : '\C\\\a*cite\a*\*\?\(\[[^\]]*\]\)*\_\s*{[^{}]*',
\ })
call latex#util#set_default('g:latex_fold_enabled', 1)
2014-07-24 13:31:42 -04:00
call latex#util#set_default('g:latex_fold_automatic', 1)
2014-07-16 17:08:00 -04:00
call latex#util#set_default('g:latex_fold_preamble', 1)
call latex#util#set_default('g:latex_fold_envs', 1)
call latex#util#set_default('g:latex_fold_parts',
\ [
\ "part",
\ "appendix",
\ "frontmatter",
\ "mainmatter",
\ "backmatter",
\ ])
call latex#util#set_default('g:latex_fold_sections',
\ [
\ "chapter",
\ "section",
\ "subsection",
\ "subsubsection",
\ ])
call latex#util#set_default('g:latex_indent_enabled', 1)
call latex#util#set_default('g:latex_latexmk_enabled', 1)
call latex#util#set_default('g:latex_latexmk_callback', 1)
call latex#util#set_default('g:latex_latexmk_continuous', 1)
call latex#util#set_default('g:latex_latexmk_background', 0)
2014-07-16 17:08:00 -04:00
call latex#util#set_default('g:latex_latexmk_output', 'pdf')
call latex#util#set_default('g:latex_mappings_enabled', 1)
call latex#util#set_default('g:latex_motion_enabled', 1)
call latex#util#set_default('g:latex_motion_matchparen', 1)
call latex#util#set_default('g:latex_quickfix_autojump', '0')
call latex#util#set_default('g:latex_quickfix_ignore_all_warnings', 0)
call latex#util#set_default('g:latex_quickfix_ignored_warnings', [])
call latex#util#set_default('g:latex_quickfix_mode', '2')
call latex#util#set_default('g:latex_quickfix_open_on_warning', '1')
call latex#util#set_default('g:latex_toc_enabled', 1)
call latex#util#set_default('g:latex_toc_fold', 0)
2014-07-20 09:59:20 -04:00
call latex#util#set_default('g:latex_toc_fold_levels', 10)
2014-11-22 11:29:30 -05:00
call latex#util#set_default('g:latex_toc_hide_help', 0)
call latex#util#set_default('g:latex_toc_hide_preamble', 0)
call latex#util#set_default('g:latex_toc_hide_line_numbers', 1)
2014-11-22 11:29:30 -05:00
call latex#util#set_default('g:latex_toc_numbers', 1)
call latex#util#set_default('g:latex_toc_numbers_width', 0)
call latex#util#set_default('g:latex_toc_resize', 1)
call latex#util#set_default('g:latex_toc_secnumdepth', 3)
2014-11-22 11:29:30 -05:00
call latex#util#set_default('g:latex_toc_split_side', 'leftabove')
call latex#util#set_default('g:latex_toc_width', 30)
2014-07-16 17:08:00 -04:00
call latex#util#set_default('g:latex_viewer', 'xdg-open')
endfunction
" }}}1
2013-10-05 07:53:42 -04:00
function! s:get_id(main) " {{{1
2013-10-05 07:53:42 -04:00
if exists('g:latex#data') && !empty(g:latex#data)
let id = 0
while id < len(g:latex#data)
if g:latex#data[id].tex == a:main
return id
endif
let id += 1
endwhile
endif
return -1
endfunction
function! s:get_main() " {{{1
"
" Search for main file specifier at the beginning of file. This is similar
" to the method used by several other plugins and editors, such as vim with
" LaTeX-Box, TextMate, TexWorks, and texmaker.
"
for line in getline(1, 5)
let candidate = matchstr(line,
\ '^\s*%\s*!\s*[tT][eE][xX]\s\+root\s*=\s*\zs.*\ze\s*$')
if len(candidate) > 0
let main = fnamemodify(candidate, ':p')
if filereadable(main)
return main
endif
endif
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),
\ '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
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(texdata, ext) " {{{1
2013-10-05 07:53:42 -04:00
" Create set of candidates
let candidates = [
\ a:texdata.name,
\ g:latex_build_dir . '/' . a:texdata.name,
\ ]
" Search through the candidates
for f in map(candidates,
\ 'a:texdata.root . ''/'' . v:val . ''.'' . a:ext')
if filereadable(f)
2014-07-21 19:26:27 -04:00
return fnamemodify(f, ':p')
2013-10-05 07:53:42 -04:00
endif
endfor
" Return empty string if no entry is found
return ''
endfunction
function! s:truncate(string) " {{{1
2013-10-05 07:53:42 -04:00
if len(a:string) >= winwidth('.') - 9
return a:string[0:10] . "..." . a:string[-winwidth('.')+23:]
else
return a:string
endif
endfunction
" }}}1
2014-02-10 08:21:43 -05:00
" vim: fdm=marker