Skip to content

Commit

Permalink
feat: allow more dynamic output
Browse files Browse the repository at this point in the history
refer: #2138
  • Loading branch information
lervag committed Aug 25, 2021
1 parent 72a4623 commit 4f085cc
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 67 deletions.
144 changes: 80 additions & 64 deletions autoload/vimtex/compiler.vim
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ function! vimtex#compiler#callback(status) abort " {{{1

let b:vimtex.compiler.status = a:status

if exists('s:output')
call s:output.update()
endif

if a:status == 1
if exists('#User#VimtexEventCompiling')
doautocmd <nomodeline> User VimtexEventCompiling
Expand Down Expand Up @@ -152,77 +148,25 @@ endfunction

" }}}1
function! vimtex#compiler#output() abort " {{{1
let l:file = get(b:vimtex.compiler, 'output', '')
if empty(l:file)
if !exists('b:vimtex.compiler.output')
\ || !filereadable(b:vimtex.compiler.output)
call vimtex#log#warning('No output exists!')
return
endif

" If window already open, then go there
" If relevant output is open, then reuse it
if exists('s:output')
if bufwinnr(l:file) == s:output.winnr
execute s:output.winnr . 'wincmd w'
if s:output.name ==# a:file
if bufwinnr(a:file) == s:output.winnr
execute s:output.winnr . 'wincmd w'
endif
return
else
call s:output.destroy()
endif
endif

" Create new output window
silent execute 'split' l:file

" Create the output object
let s:output = {}
let s:output.name = l:file
let s:output.bufnr = bufnr('%')
let s:output.winnr = bufwinnr('%')
function! s:output.update() dict abort
if bufwinnr(self.name) != self.winnr
return
endif

if mode() ==? 'v' || mode() ==# "\<c-v>"
return
endif

" Go to last line of file if it is not the current window
if bufwinnr('%') != self.winnr
let l:return = bufwinnr('%')
execute 'keepalt' self.winnr . 'wincmd w'
edit
normal! Gzb
execute 'keepalt' l:return . 'wincmd w'
redraw
endif
endfunction
function! s:output.destroy() dict abort
autocmd! vimtex_output_window
augroup! vimtex_output_window
unlet s:output
endfunction

" Better automatic update
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

" Set some mappings
nnoremap <silent><buffer><nowait> q :bwipeout<cr>
if has('nvim') || has('gui_running')
nnoremap <silent><buffer><nowait> <esc> :bwipeout<cr>
endif

" Set some buffer options
setlocal autoread
setlocal nomodifiable
setlocal bufhidden=wipe
call s:output_factory.create(b:vimtex.compiler.output)
endfunction

" }}}1
Expand Down Expand Up @@ -365,6 +309,78 @@ endfunction
" }}}1


let s:output_factory = {}
function! s:output_factory.create(file) dict abort " {{{1
silent execute 'split' a:file
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.bufnr = bufnr('%')
let s:output.winnr = bufwinnr('%')
let s:output.timer = timer_start(150,
\ {_ -> 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.update() dict abort " {{{1
if bufwinnr(self.name) != self.winnr
let self.winnr = bufwinnr(self.name)
endif

if mode() ==? 'v' || mode() ==# "\<c-v>"
return
endif

let l:swap = bufwinnr('%') != self.winnr
if l:swap
let l:return = bufwinnr('%')
execute 'keepalt' self.winnr . 'wincmd w'
endif

" Reload content with :edit
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 !g:vimtex_compiler_enabled | finish | endif
Expand Down
7 changes: 4 additions & 3 deletions autoload/vimtex/compiler/_template.vim
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,13 @@ function! s:compiler.start(...) abort dict " {{{1
if self.continuous
call vimtex#log#info('Compiler started in continuous mode'
\ . (a:0 > 0 ? ' (single shot)' : ''))
if exists('#User#VimtexEventCompileStarted')
doautocmd <nomodeline> User VimtexEventCompileStarted
endif
else
call vimtex#log#info('Compiler started in background!')
endif

if exists('#User#VimtexEventCompileStarted')
doautocmd <nomodeline> User VimtexEventCompileStarted
endif
endfunction

" }}}1
Expand Down

0 comments on commit 4f085cc

Please sign in to comment.