1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
|
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve LervÄg
" Email: karl.yngve@gmail.com
"
function! vimtex#state#init_buffer() abort " {{{1
command! -buffer VimtexToggleMain call vimtex#state#toggle_main()
command! -buffer VimtexReloadState call vimtex#state#reload()
nnoremap <buffer> <plug>(vimtex-toggle-main) :VimtexToggleMain<cr>
nnoremap <buffer> <plug>(vimtex-reload-state) :VimtexReloadState<cr>
endfunction
" }}}1
function! vimtex#state#init() abort " {{{1
let [l:main, l:main_parser, l:unsupported_modules] = s:get_main()
let l:id = s:get_main_id(l:main)
if exists('s:cand_fallback')
call vimtex#log#warning(
\ 'Ignored latexmain specifier which points to: ',
\ s:cand_fallback,
\ 'Reason: That main file did not include this file!',
\)
unlet s:cand_fallback
endif
if l:id >= 0
let b:vimtex_id = l:id
let b:vimtex = s:vimtex_states[l:id]
else
let b:vimtex_id = s:vimtex_next_id
let b:vimtex = vimtex#state#class#new({
\ 'main': l:main,
\ 'main_parser': l:main_parser,
\ 'unsupported_modules': l:unsupported_modules
\})
let s:vimtex_next_id += 1
let s:vimtex_states[b:vimtex_id] = b:vimtex
endif
endfunction
" }}}1
function! vimtex#state#init_local() abort " {{{1
let l:preserve_root = get(s:, 'subfile_preserve_root')
unlet! s:subfile_preserve_root
if &filetype !=# 'tex' || empty(b:vimtex.tex) | return | endif
let l:filename = expand('%:p')
if b:vimtex.tex ==# l:filename | return | endif
let l:vimtex_id = s:get_main_id(l:filename)
if l:vimtex_id < 0
let l:vimtex_id = s:vimtex_next_id
let l:vimtex = vimtex#state#class#new({
\ 'main': l:filename,
\ 'main_parser': 'local file',
\ 'preserve_root': l:preserve_root || s:check_standalone(),
\})
let s:vimtex_next_id += 1
let s:vimtex_states[l:vimtex_id] = l:vimtex
if !has_key(b:vimtex, 'subids')
let b:vimtex.subids = []
endif
call add(b:vimtex.subids, l:vimtex_id)
let l:vimtex.main_id = b:vimtex_id
endif
let b:vimtex_local = {
\ 'active' : 0,
\ 'main_id' : b:vimtex_id,
\ 'sub_id' : l:vimtex_id,
\}
if b:vimtex.main_parser ==# 'subfiles' && g:vimtex_subfile_start_local
silent call vimtex#state#toggle_main()
endif
endfunction
" }}}1
function! vimtex#state#reload() abort " {{{1
let l:id = s:get_main_id(expand('%:p'))
if has_key(s:vimtex_states, l:id)
let l:vimtex = remove(s:vimtex_states, l:id)
call l:vimtex.cleanup()
endif
if has_key(s:vimtex_states, get(b:, 'vimtex_id', -1))
let l:vimtex = remove(s:vimtex_states, b:vimtex_id)
call l:vimtex.cleanup()
endif
call vimtex#state#init()
call vimtex#state#init_local()
endfunction
" }}}1
function! vimtex#state#toggle_main() abort " {{{1
if exists('b:vimtex_local')
let b:vimtex_local.active = !b:vimtex_local.active
let b:vimtex_id = b:vimtex_local.active
\ ? b:vimtex_local.sub_id
\ : b:vimtex_local.main_id
let b:vimtex = vimtex#state#get(b:vimtex_id)
call vimtex#log#info('Changed to `' . b:vimtex.base . "' "
\ . (b:vimtex_local.active ? '[local]' : '[main]'))
endif
endfunction
" }}}1
function! vimtex#state#list_all() abort " {{{1
return values(s:vimtex_states)
endfunction
" }}}1
function! vimtex#state#exists(id) abort " {{{1
return has_key(s:vimtex_states, a:id)
endfunction
" }}}1
function! vimtex#state#get(id) abort " {{{1
return s:vimtex_states[a:id]
endfunction
" }}}1
function! vimtex#state#get_all() abort " {{{1
return s:vimtex_states
endfunction
" }}}1
function! vimtex#state#cleanup(id) abort " {{{1
if !vimtex#state#exists(a:id) | return | endif
"
" Count the number of open buffers for the given blob
"
let l:buffers = filter(range(1, bufnr('$')), 'buflisted(v:val)')
let l:ids = map(l:buffers, "getbufvar(v:val, 'vimtex_id', -1)")
let l:count = count(l:ids, a:id)
"
" Don't clean up if there are more than one buffer connected to the current
" blob
"
if l:count > 1 | return | endif
let l:vimtex = vimtex#state#get(a:id)
"
" Handle possible subfiles properly
"
if has_key(l:vimtex, 'subids')
let l:subcount = 0
for l:sub_id in get(l:vimtex, 'subids', [])
let l:subcount += count(l:ids, l:sub_id)
endfor
if l:count + l:subcount > 1 | return | endif
for l:sub_id in get(l:vimtex, 'subids', [])
call remove(s:vimtex_states, l:sub_id).cleanup()
endfor
call remove(s:vimtex_states, a:id).cleanup()
else
call remove(s:vimtex_states, a:id).cleanup()
if has_key(l:vimtex, 'main_id')
let l:main = vimtex#state#get(l:vimtex.main_id)
let l:count_main = count(l:ids, l:vimtex.main_id)
for l:sub_id in get(l:main, 'subids', [])
let l:count_main += count(l:ids, l:sub_id)
endfor
if l:count_main + l:count <= 1
call remove(s:vimtex_states, l:vimtex.main_id).cleanup()
endif
endif
endif
endfunction
" }}}1
function! s:get_main_id(main) abort " {{{1
for [l:id, l:state] in items(s:vimtex_states)
if l:state.tex == a:main
return str2nr(l:id)
endif
endfor
return -1
endfunction
function! s:get_main() abort " {{{1
" Use buffer variable if it exists
if exists('b:vimtex_main') && filereadable(b:vimtex_main)
return [fnamemodify(b:vimtex_main, ':p'), 'buffer variable', []]
endif
" Search for TEX root specifier at the beginning of file. This is used by
" several other plugins and editors.
let l:candidate = s:get_main_from_texroot()
if !empty(l:candidate)
return [l:candidate, 'texroot specifier', []]
endif
if &filetype ==# 'tex'
" Check if the current file is a main file
if s:file_is_main(expand('%:p'))
return [expand('%:p'), 'current file verified', []]
endif
" Support for subfiles package
let l:candidate = s:get_main_from_subfile()
if !empty(l:candidate)
return [l:candidate, 'subfiles', []]
endif
endif
" Search for .latexmain-specifier
let l:candidate = s:get_main_latexmain(expand('%:p'))
if !empty(l:candidate)
return [l:candidate, 'latexmain specifier', []]
endif
" Search for .latexmkrc @default_files specifier
let l:candidate = s:get_main_latexmk()
if !empty(l:candidate)
return [l:candidate, 'latexmkrc @default_files', []]
endif
" Check if we are class or style file
if index(['cls', 'sty'], expand('%:e')) >= 0
let l:id = getbufvar('#', 'vimtex_id', -1)
if l:id >= 0 && has_key(s:vimtex_states, l:id)
return [
\ s:vimtex_states[l:id].tex,
\ 'cls/sty file (inherit from alternate)',
\ []
\]
else
return [
\ expand('%:p'),
\ 'cls/sty file',
\ ['compiler', 'view', 'toc', 'qf']
\]
endif
endif
" Search for main file recursively through include specifiers
if &filetype ==# 'tex'
let l:candidate = s:get_main_choose(s:get_main_recurse())
if !empty(l:candidate)
return [l:candidate, 'recursive search', []]
endif
else
let l:candidate = s:get_main_choose(s:get_main_recurse_from_bib())
if !empty(l:candidate)
return [l:candidate, 'recursive search (bib)', []]
endif
endif
" Fallbacks:
" 1. fallback candidate from get_main_latexmain
" 2. a. tex: current file
" b. bib: check alternate file or current
if exists('s:cand_fallback')
let l:candidate = s:cand_fallback
unlet s:cand_fallback
return [l:candidate, 'fallback', []]
elseif &filetype ==# 'bib'
let l:id = getbufvar('#', 'vimtex_id', -1)
if l:id >= 0 && has_key(s:vimtex_states, l:id)
return [
\ s:vimtex_states[l:id].tex,
\ 'bib file (inherit from alternate)',
\ []
\]
else
return [
\ expand('%:p'),
\ 'bib file',
\ ['compiler', 'view', 'toc', 'qf', 'fold']
\]
endif
else
return [expand('%:p'), 'fallback current file', []]
endif
endfunction
" }}}1
function! s:get_main_from_texroot() abort " {{{1
for l:line in getline(1, 5)
let l:file_pattern = matchstr(l:line, g:vimtex#re#tex_input_root)
if empty(l:file_pattern) | continue | endif
if !vimtex#paths#is_abs(l:file_pattern)
let l:file_pattern = simplify(expand('%:p:h') . '/' . l:file_pattern)
endif
let l:candidates = glob(l:file_pattern, 0, 1)
if len(l:candidates) > 1
return s:get_main_choose(l:candidates)
elseif len(l:candidates) == 1
return l:candidates[0]
endif
endfor
return ''
endfunction
" }}}1
function! s:get_main_from_subfile() abort " {{{1
for l:line in getline(1, 5)
let l:filename = matchstr(l:line,
\ '^\C\s*\\documentclass\[\zs.*\ze\]{subfiles}')
if len(l:filename) > 0
if l:filename !~# '\.tex$'
let l:filename .= '.tex'
endif
if vimtex#paths#is_abs(l:filename)
" Specified path is absolute
if filereadable(l:filename) | return l:filename | endif
else
" Try specified path as relative to current file path
let l:candidate = simplify(expand('%:p:h') . '/' . l:filename)
if filereadable(l:candidate) | return l:candidate | endif
" Try specified path as relative to the project main file. This is
" difficult, since the main file is the one we are looking for. We
" therefore assume that the main file lives somewhere upwards in the
" directory tree.
let l:candidate = fnamemodify(findfile(l:filename, '.;'), ':p')
if filereadable(l:candidate)
\ && s:file_reaches_current(l:candidate)
let s:subfile_preserve_root = 1
return fnamemodify(candidate, ':p')
endif
" Check the alternate buffer. This seems sensible e.g. in cases where one
" enters an "outer" subfile through a 'gf' motion from the main file.
let l:vimtex = getbufvar('#', 'vimtex', {})
for l:file in get(l:vimtex, 'sources', [])
if expand('%:p') ==# simplify(l:vimtex.root . '/' . l:file)
let s:subfile_preserve_root = 1
return l:vimtex.tex
endif
endfor
endif
endif
endfor
return ''
endfunction
" }}}1
function! s:get_main_latexmain(file) abort " {{{1
for l:cand in s:globpath_upwards('*.latexmain', expand('%:p:h'))
let l:cand = fnamemodify(l:cand, ':p:r')
if s:file_reaches_current(l:cand)
return l:cand
else
let s:cand_fallback = l:cand
endif
endfor
return ''
endfunction
function! s:get_main_latexmk() abort " {{{1
let l:root = expand('%:p:h')
let l:results = vimtex#compiler#latexmk#get_rc_opt(
\ l:root, 'default_files', 2, [])
if l:results[1] < 1 | return '' | endif
for l:candidate in l:results[0]
let l:file = l:root . '/' . l:candidate
if filereadable(l:file)
return l:file
endif
endfor
return ''
endfunction
function! s:get_main_recurse(...) abort " {{{1
" Either start the search from the original file, or check if the supplied
" file is a main file (or invalid)
if a:0 == 0
let l:file = expand('%:p')
let l:tried = {}
else
let l:file = a:1
let l:tried = a:2
if s:file_is_main(l:file)
return [l:file]
elseif !filereadable(l:file)
return []
endif
endif
" Create list of candidates that was already tried for the current file
if !has_key(l:tried, l:file)
let l:tried[l:file] = [l:file]
endif
" Apply filters successively (minor optimization)
let l:re_filter1 = fnamemodify(l:file, ':t:r')
let l:re_filter2 = g:vimtex#re#tex_input . '\s*\f*' . l:re_filter1
" Search through candidates found upwards in the directory tree
let l:results = []
for l:cand in s:globpath_upwards('*.tex', fnamemodify(l:file, ':p:h'))
if index(l:tried[l:file], l:cand) >= 0 | continue | endif
call add(l:tried[l:file], l:cand)
if len(filter(filter(readfile(l:cand),
\ 'v:val =~# l:re_filter1'),
\ 'v:val =~# l:re_filter2')) > 0
let l:results += s:get_main_recurse(fnamemodify(l:cand, ':p'), l:tried)
endif
endfor
return l:results
endfunction
" }}}1
function! s:get_main_recurse_from_bib() abort " {{{1
let l:file = expand('%:p')
let l:tried = {}
let l:tried[l:file] = [l:file]
" Apply filters successively (minor optimization)
let l:re_filter1 = fnamemodify(l:file, ':t:r')
let l:re_filter2 = g:vimtex#re#bib_input . '\s*\f*' . l:re_filter1
" Search through candidates found upwards in the directory tree
let l:results = []
for l:cand in s:globpath_upwards('*.tex', fnamemodify(l:file, ':p:h'))
if index(l:tried[l:file], l:cand) >= 0 | continue | endif
call add(l:tried[l:file], l:cand)
if len(filter(filter(readfile(l:cand),
\ 'v:val =~# l:re_filter1'),
\ 'v:val =~# l:re_filter2')) > 0
let l:results += s:get_main_recurse(fnamemodify(l:cand, ':p'), l:tried)
endif
endfor
return l:results
endfunction
" }}}1
function! s:get_main_choose(list) abort " {{{1
let l:list = vimtex#util#uniq_unsorted(a:list)
if empty(l:list) | return '' | endif
if len(l:list) == 1 | return l:list[0] | endif
let l:all = map(copy(l:list), {_, x -> [s:get_main_id(x), x]})
let l:new = map(filter(copy(l:all), 'v:val[0] < 0'), 'v:val[1]')
let l:existing = {}
for [l:key, l:val] in filter(copy(l:all), 'v:val[0] >= 0')
let l:existing[l:key] = l:val
endfor
let l:alternate_id = getbufvar('#', 'vimtex_id', -1)
if len(l:existing) == 1
return values(l:existing)[0]
elseif len(l:existing) > 1 && has_key(l:existing, l:alternate_id)
return l:existing[l:alternate_id]
elseif len(l:existing) < 1 && len(l:new) == 1
return l:new[0]
else
let l:choices = {}
for l:tex in l:list
let l:choices[l:tex] = vimtex#paths#relative(l:tex, getcwd())
endfor
unsilent return vimtex#ui#select(l:choices, {
\ 'prompt': 'Please select an appropriate main file:',
\ 'return': 'key',
\})
endif
endfunction
" }}}1
function! s:file_is_main(file) abort " {{{1
if !filereadable(a:file) | return 0 | endif
let l:preamble = vimtex#parser#preamble(a:file, {
\ 'root' : fnamemodify(a:file, ':p:h'),
\})
" Check if a:file is a main file by looking for the \documentclass command,
" but ignore the following:
" * \documentclass[...]{subfiles}
" * \documentclass[...]{standalone}
let l:lines = copy(l:preamble)
call filter(l:lines, 'v:val =~# ''^\s*\\documentclass\_\s*[\[{]''')
call filter(l:lines, 'v:val !~# ''{subfiles}''')
call filter(l:lines, 'v:val !~# ''{standalone}''')
if len(l:lines) == 0 | return 0 | endif
" A main file must also contain `\begin{document}`
let l:lines = copy(l:preamble)
call filter(l:lines, 'v:val =~# ''^\s*\\begin\s*{document}''')
return len(l:lines) > 0
endfunction
" }}}1
function! s:file_reaches_current(file, ...) abort " {{{1
let l:visited = a:0 > 0 ? a:1 : []
" Note: This function assumes that the input a:file is an absolute path
if !filereadable(a:file) | return 0 | endif
if index(l:visited, a:file) >= 0 | return 0 | endif
call add(l:visited, a:file)
for l:line in filter(readfile(a:file), 'v:val =~# g:vimtex#re#tex_input')
let l:file = get(
\ vimtex#parser#tex#input_parser(l:line, a:file, ''),
\ 'file', '')
if empty(l:file) | continue | endif
if !vimtex#paths#is_abs(l:file)
let l:file = fnamemodify(a:file, ':h') . '/' . l:file
endif
if l:file !~# '\.tex$'
let l:file .= '.tex'
endif
if expand('%:p') ==# l:file || s:file_reaches_current(l:file, l:visited)
return 1
endif
endfor
return 0
endfunction
" }}}1
function! s:globpath_upwards(expr, path) abort " {{{1
" Returns the list of files (NOT directories) obtained by globpath(p, a:expr)
" with p going from a:path and upwards in the directory tree.
let l:path = a:path
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
return filter(
\ split(globpath(fnameescape(l:dirs), a:expr), '\n'),
\ 'filereadable(v:val)')
endfunction
" }}}1
function! s:check_standalone() abort " {{{1
return match(getline(1, 5),
\ '\v^\C\s*\\documentclass%(\[.*\])?\{standalone\}') >= 0
endfunction
" }}}1
" Initialize module
let s:vimtex_states = {}
let s:vimtex_next_id = 0
|