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
|
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve LervÄg
" Email: karl.yngve@gmail.com
"
function! vimtex#compiler#init_buffer() abort " {{{1
if !g:vimtex_compiler_enabled | return | endif
" Define commands
command! -buffer -nargs=* VimtexCompile call vimtex#compiler#compile(<f-args>)
command! -buffer -bang -nargs=* VimtexCompileSS call vimtex#compiler#compile_ss(<f-args>)
command! -buffer -range VimtexCompileSelected <line1>,<line2>call vimtex#compiler#compile_selected('command')
command! -buffer VimtexCompileOutput call vimtex#compiler#output()
command! -buffer VimtexStop call vimtex#compiler#stop()
command! -buffer VimtexStopAll call vimtex#compiler#stop_all()
command! -buffer -bang VimtexClean call vimtex#compiler#clean(<q-bang> == "!")
command! -buffer -bang VimtexStatus call vimtex#compiler#status(<q-bang> == "!")
" Define mappings
nnoremap <buffer> <plug>(vimtex-compile) :call vimtex#compiler#compile()<cr>
nnoremap <buffer> <plug>(vimtex-compile-ss) :call vimtex#compiler#compile_ss()<cr>
nnoremap <buffer> <plug>(vimtex-compile-selected) :set opfunc=vimtex#compiler#compile_selected<cr>g@
xnoremap <buffer> <plug>(vimtex-compile-selected) :<c-u>call vimtex#compiler#compile_selected('visual')<cr>
nnoremap <buffer> <plug>(vimtex-compile-output) :call vimtex#compiler#output()<cr>
nnoremap <buffer> <plug>(vimtex-stop) :call vimtex#compiler#stop()<cr>
nnoremap <buffer> <plug>(vimtex-stop-all) :call vimtex#compiler#stop_all()<cr>
nnoremap <buffer> <plug>(vimtex-clean) :call vimtex#compiler#clean(0)<cr>
nnoremap <buffer> <plug>(vimtex-clean-full) :call vimtex#compiler#clean(1)<cr>
nnoremap <buffer> <plug>(vimtex-status) :call vimtex#compiler#status(0)<cr>
nnoremap <buffer> <plug>(vimtex-status-all) :call vimtex#compiler#status(1)<cr>
endfunction
" }}}1
function! vimtex#compiler#init_state(state) abort " {{{1
if !g:vimtex_compiler_enabled | return | endif
let a:state.compiler = s:init_compiler({
\ 'file_info': {
\ 'root': a:state.root,
\ 'target': a:state.tex,
\ 'target_name': a:state.name,
\ 'target_basename': a:state.base,
\ 'jobname': a:state.name,
\ }
\})
endfunction
" }}}1
function! vimtex#compiler#callback(status) abort " {{{1
" Status:
" 1: Compilation cycle has started
" 2: Compilation complete - Success
" 3: Compilation complete - Failed
if !exists('b:vimtex.compiler') | return | endif
silent! call s:output.pause()
let l:__silent = b:vimtex.compiler.silence_next_callback
if l:__silent
let b:vimtex.compiler.silence_next_callback = v:false
if g:vimtex_compiler_silent
let l:__silent = v:false
else
call vimtex#log#set_silent()
endif
endif
let b:vimtex.compiler.status = a:status
if a:status == 1
if exists('#User#VimtexEventCompiling')
doautocmd <nomodeline> User VimtexEventCompiling
endif
elseif a:status == 2
if !g:vimtex_compiler_silent
call vimtex#log#info('Compilation completed')
endif
if exists('b:vimtex')
call b:vimtex.update_packages()
call vimtex#syntax#packages#init()
endif
call vimtex#qf#open(0)
if exists('#User#VimtexEventCompileSuccess')
doautocmd <nomodeline> User VimtexEventCompileSuccess
endif
elseif a:status == 3
if !g:vimtex_compiler_silent
call vimtex#log#warning('Compilation failed!')
endif
call vimtex#qf#open(0)
if exists('#User#VimtexEventCompileFailed')
doautocmd <nomodeline> User VimtexEventCompileFailed
endif
endif
if l:__silent
call vimtex#log#set_silent_restore()
endif
silent! call s:output.resume()
endfunction
" }}}1
function! vimtex#compiler#compile(...) abort " {{{1
if !b:vimtex.compiler.enabled | return | endif
if b:vimtex.compiler.is_running()
call vimtex#compiler#stop()
else
call call('vimtex#compiler#start', a:000)
endif
endfunction
" }}}1
function! vimtex#compiler#compile_ss(...) abort " {{{1
if !b:vimtex.compiler.enabled | return | endif
if b:vimtex.compiler.is_running()
call vimtex#log#info(
\ 'Compiler is already running, use :VimtexStop to stop it!')
return
endif
call b:vimtex.compiler.start_single(expandcmd(join(a:000)))
if g:vimtex_compiler_silent | return | endif
call vimtex#log#info('Compiler started in background!')
endfunction
" }}}1
function! vimtex#compiler#compile_selected(type) abort range " {{{1
if !b:vimtex.compiler.enabled | return | endif
" Values of a:firstline and a:lastline are not available in nested function
" calls, so we must handle them here.
let l:opts = a:type ==# 'command'
\ ? {'type': 'range', 'range': [a:firstline, a:lastline]}
\ : {'type': a:type =~# 'line\|char\|block' ? 'operator' : a:type}
let l:state = vimtex#parser#selection_to_texfile(l:opts)
if empty(l:state) | return | endif
" Create and initialize temporary compiler
let l:compiler = s:init_compiler({
\ 'file_info': {
\ 'root': l:state.root,
\ 'target': l:state.tex,
\ 'target_name': l:state.name,
\ 'target_basename': l:state.base,
\ 'jobname': l:state.name,
\ },
\ 'out_dir': '',
\ 'continuous': 0,
\ 'callback': 0,
\})
if empty(l:compiler) | return | endif
call vimtex#log#info('Compiling selected lines ...')
call vimtex#log#set_silent()
call l:compiler.start()
call l:compiler.wait()
" Check if successful
if vimtex#qf#inquire(l:state.tex)
call vimtex#log#set_silent_restore()
call vimtex#log#warning('Compiling selected lines ... failed!')
botright cwindow
return
else
call l:compiler.clean(0)
call b:vimtex.viewer.view(l:state.pdf)
call vimtex#log#set_silent_restore()
call vimtex#log#info('Compiling selected lines ... done')
endif
endfunction
" }}}1
function! vimtex#compiler#output() abort " {{{1
if !b:vimtex.compiler.enabled | return | endif
if !exists('b:vimtex.compiler.output')
\ || !filereadable(b:vimtex.compiler.output)
call vimtex#log#warning('No output exists!')
return
endif
" If relevant output is open, then reuse it
if exists('s:output')
if s:output.name ==# b:vimtex.compiler.output
if bufwinnr(b:vimtex.compiler.output) == s:output.winnr
execute s:output.winnr . 'wincmd w'
endif
return
else
call s:output.destroy()
endif
endif
call s:output_factory.create(b:vimtex.compiler.output)
endfunction
" }}}1
function! vimtex#compiler#start(...) abort " {{{1
if !b:vimtex.compiler.enabled | return | endif
if !b:vimtex.is_compileable()
call vimtex#log#error(
\ 'Compilation error due to failed mainfile detection!',
\ 'Please ensure that VimTeX can locate the proper main .tex file.',
\ 'Read ":help vimtex-multi-file" for more info.'
\)
return
endif
if b:vimtex.compiler.is_running()
call vimtex#log#warning(
\ 'Compiler is already running for `' . b:vimtex.base . "'")
return
endif
call b:vimtex.compiler.start(expandcmd(join(a:000)))
if g:vimtex_compiler_silent | return | endif
" We add a redraw here to clear messages (e.g. file written). This is useful
" to avoid the "Press ENTER" prompt in some cases, see e.g.
" https://github.com/lervag/vimtex/issues/2149
redraw
if b:vimtex.compiler.continuous
call vimtex#log#info('Compiler started in continuous mode')
else
call vimtex#log#info('Compiler started in background!')
endif
endfunction
" }}}1
function! vimtex#compiler#stop() abort " {{{1
if !b:vimtex.compiler.enabled | return | endif
if !b:vimtex.compiler.is_running()
call vimtex#log#warning(
\ 'There is no process to stop (' . b:vimtex.base . ')')
return
endif
call b:vimtex.compiler.stop()
if g:vimtex_compiler_silent | return | endif
call vimtex#log#info('Compiler stopped (' . b:vimtex.base . ')')
endfunction
" }}}1
function! vimtex#compiler#stop_all() abort " {{{1
for l:state in vimtex#state#list_all()
if exists('l:state.compiler.enabled')
\ && l:state.compiler.enabled
\ && l:state.compiler.is_running()
call l:state.compiler.stop()
call vimtex#log#info('Compiler stopped ('
\ . l:state.compiler.file_info.target_basename . ')')
endif
endfor
endfunction
" }}}1
function! vimtex#compiler#clean(full) abort " {{{1
if !b:vimtex.compiler.enabled | return | endif
let l:restart = b:vimtex.compiler.is_running()
if l:restart
call b:vimtex.compiler.stop()
endif
call b:vimtex.compiler.clean(a:full)
sleep 100m
call b:vimtex.compiler.remove_dirs()
call vimtex#log#info('Compiler clean finished' . (a:full ? ' (full)' : ''))
if l:restart
let b:vimtex.compiler.silence_next_callback = 1
silent call b:vimtex.compiler.start()
endif
endfunction
" }}}1
function! vimtex#compiler#status(detailed) abort " {{{1
if !b:vimtex.compiler.enabled | return | endif
if a:detailed
let l:running = []
for l:data in vimtex#state#list_all()
if l:data.compiler.is_running()
let l:name = l:data.tex
if len(l:name) >= winwidth('.') - 20
let l:name = '...' . l:name[-winwidth('.')+23:]
endif
call add(l:running, printf('%-6s %s',
\ string(l:data.compiler.get_pid()) . ':', l:name))
endif
endfor
if empty(l:running)
call vimtex#log#info('Compiler is not running!')
else
call vimtex#log#info('Compiler is running', l:running)
endif
else
if exists('b:vimtex.compiler')
\ && b:vimtex.compiler.is_running()
call vimtex#log#info('Compiler is running')
else
call vimtex#log#info('Compiler is not running!')
endif
endif
endfunction
" }}}1
function! s:init_compiler(options) abort " {{{1
if type(g:vimtex_compiler_method) == v:t_func
\ || exists('*' . g:vimtex_compiler_method)
let l:method = call(g:vimtex_compiler_method, [a:options.file_info.target])
else
let l:method = g:vimtex_compiler_method
endif
if index([
\ 'arara',
\ 'generic',
\ 'latexmk',
\ 'latexrun',
\ 'tectonic',
\], l:method) < 0
call vimtex#log#error('Error! Invalid compiler method: ' . l:method)
let l:method = 'latexmk'
endif
let l:options = extend(
\ deepcopy(get(g:, 'vimtex_compiler_' . l:method, {})),
\ a:options)
return vimtex#compiler#{l:method}#init(l:options)
endfunction
" }}}1
let s:output_factory = {}
function! s:output_factory.create(file) dict abort " {{{1
let l:vimtex = b:vimtex
silent execute 'split' a:file
let b:vimtex = l:vimtex
setlocal autoread
setlocal nomodifiable
setlocal bufhidden=wipe
nnoremap <silent><buffer><nowait> q :bwipeout<cr>
if has('nvim') || has('gui_running')
nnoremap <silent><buffer><nowait> <esc> :bwipeout<cr>
endif
let s:output = deepcopy(self)
unlet s:output.create
let s:output.name = a:file
let s:output.ftime = -1
let s:output.paused = v:false
let s:output.bufnr = bufnr('%')
let s:output.winnr = bufwinnr('%')
let s:output.timer = timer_start(100,
\ {_ -> s:output.update()},
\ {'repeat': -1})
augroup vimtex_output_window
autocmd!
autocmd BufDelete <buffer> call s:output.destroy()
autocmd BufEnter * call s:output.update()
autocmd FocusGained * call s:output.update()
autocmd CursorHold * call s:output.update()
autocmd CursorHoldI * call s:output.update()
autocmd CursorMoved * call s:output.update()
autocmd CursorMovedI * call s:output.update()
augroup END
endfunction
" }}}1
function! s:output_factory.pause() dict abort " {{{1
let self.paused = v:true
endfunction
" }}}1
function! s:output_factory.resume() dict abort " {{{1
let self.paused = v:false
endfunction
" }}}1
function! s:output_factory.update() dict abort " {{{1
if self.paused | return | endif
let l:ftime = getftime(self.name)
if self.ftime >= l:ftime
\ || mode() ==? 'v' || mode() ==# "\<c-v>"
return
endif
let self.ftime = getftime(self.name)
if bufwinnr(self.name) != self.winnr
let self.winnr = bufwinnr(self.name)
endif
let l:swap = bufwinnr('%') != self.winnr
if l:swap
let l:return = bufwinnr('%')
execute 'keepalt' self.winnr . 'wincmd w'
endif
" Force reload file content
silent edit
if l:swap
" Go to last line of file if it is not the current window
normal! Gzb
execute 'keepalt' l:return . 'wincmd w'
redraw
endif
endfunction
" }}}1
function! s:output_factory.destroy() dict abort " {{{1
call timer_stop(self.timer)
autocmd! vimtex_output_window
augroup! vimtex_output_window
unlet s:output
endfunction
" }}}1
" {{{1 Initialize module
if !get(g:, 'vimtex_compiler_enabled') | finish | endif
augroup vimtex_compiler
autocmd!
autocmd VimLeave * call vimtex#compiler#stop_all()
augroup END
" }}}1
|