From d25d3aa0b75431195bdd5987c5c84893a6168170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Fri, 6 Nov 2015 23:39:14 +0100 Subject: [PATCH] Added VimtexQuit event (fixes #253) Also updated latexmk.vim to use the VimtexQuit event to ensure that latexmk processes are stopped when a project has been closed. --- autoload/vimtex.vim | 37 +++++++++++++++++++++++++++++--- autoload/vimtex/latexmk.vim | 42 +++++++------------------------------ 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/autoload/vimtex.vim b/autoload/vimtex.vim index 16195c6..79f17d5 100644 --- a/autoload/vimtex.vim +++ b/autoload/vimtex.vim @@ -172,11 +172,11 @@ function! s:init_buffer() " {{{1 " " Attach autocommands " - - augroup vimtex - au! + augroup vimtex_buffers au BufFilePre call s:filename_changed_pre() au BufFilePost call s:filename_changed_post() + au BufLeave call s:buffer_left() + au BufDelete call s:buffer_deleted() augroup END endfunction @@ -550,6 +550,8 @@ endfunction " }}}1 +" +" Private functions function! s:print_dict(dict, ...) " {{{1 let level = a:0 > 0 ? a:1 : 0 @@ -586,4 +588,33 @@ endfunction " }}}1 +function! s:buffer_left() " {{{1 + let s:vimtex_id = b:vimtex_id +endfunction + +" }}}1 +function! s:buffer_deleted() " {{{1 + " Check if the deleted buffer was the last remaining buffer of an opened + " latex project + 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 + if exists('b:vimtex') + let b:vimtex_tmp = b:vimtex + endif + let b:vimtex = g:vimtex_data[s:vimtex_id] + doautocmd User VimtexQuit + if exists('b:vimtex_tmp') + unlet b:vimtex_tmp + else + unlet b:vimtex + endif + + " Clean up the global data list + call remove(g:vimtex_data, s:vimtex_id) + endif +endfunction + +" }}}1 + " vim: fdm=marker sw=2 diff --git a/autoload/vimtex/latexmk.vim b/autoload/vimtex/latexmk.vim index 08f9072..85c04d7 100644 --- a/autoload/vimtex/latexmk.vim +++ b/autoload/vimtex/latexmk.vim @@ -29,11 +29,13 @@ function! vimtex#latexmk#init_script() " {{{1 if !g:vimtex_latexmk_enabled | return | endif - " Ensure that all latexmk processes are stopped when vim exits + " Ensure that all latexmk processes are stopped when a latex project is + " closed and when vim exits if g:vimtex_latexmk_continuous augroup vimtex_latexmk autocmd! autocmd VimLeave * call vimtex#latexmk#stop_all() + autocmd User VimtexQuit call s:stop_before_leaving() augroup END endif endfunction @@ -73,14 +75,6 @@ function! vimtex#latexmk#init_buffer() " {{{1 nnoremap (vimtex-status) :call vimtex#latexmk#status(0) nnoremap (vimtex-status-all) :call vimtex#latexmk#status(1) nnoremap (vimtex-lacheck) :call vimtex#latexmk#lacheck() - - " Kill running latexmk process if all buffers for a latex project are closed - if g:vimtex_latexmk_continuous - augroup vimtex_latexmk - autocmd BufLeave call s:buffer_left() - autocmd BufDelete call s:buffer_deleted() - augroup END - endif endfunction " }}}1 @@ -505,31 +499,11 @@ endfunction " }}}1 -function! s:buffer_left() " {{{1 - " - " Store buffer variables as script variables so they are available if the - " buffer is deleted. This is done in order to be able to kill remaining - " latexmk processes. - " - let s:vimtex = b:vimtex - let s:vimtex_id = b:vimtex_id -endfunction - -function! s:buffer_deleted() " {{{1 - if get(get(s:, 'vimtex', {}), 'pid', 0) == 0 | return | endif - - " - " The buffer is deleted, so we must kill the remaining latexmk process if the - " current buffer was the last open buffer for the current LaTeX blob/project. - " - " The buffer variables has already been stored as script variables by - " s:buffer_left(). - " - if len(filter( - \ map(filter(range(1, bufnr('$')), 'buflisted(v:val)'), - \ 'getbufvar(v:val, ''vimtex_id'', -1)'), - \ 'v:val == s:vimtex_id')) == 1 - silent call s:latexmk_kill(s:vimtex) +function! s:stop_before_leaving() " {{{1 + if b:vimtex.pid > 0 + call s:latexmk_kill(b:vimtex) + call vimtex#echo#status(['latexmk compile: ', + \ ['VimtexSuccess', 'stopped (' . b:vimtex.base . ')']]) endif endfunction