Implement hooks for viewers

This implements capability for view and callback hooks for all viewers (though
not all viewers have callback functionalities implemented).

For Zathura and MuPDF this change also implements a default focus behaviour
which may now be changed through the use of these hooks.
This commit is contained in:
Karl Yngve Lervåg 2015-06-09 22:21:32 +02:00
parent 602ba319ff
commit b40cf63c61

View File

@ -52,6 +52,18 @@ function! vimtex#view#init_buffer() " {{{1
execute 'let b:vimtex.viewer = ' . viewer execute 'let b:vimtex.viewer = ' . viewer
call b:vimtex.viewer.init() call b:vimtex.viewer.init()
"
" Create view and/or callback hooks (if they exist)
"
for hook in ['view', 'callback']
execute 'let hookfunc = ''*''
\ . g:vimtex_view_' . g:vimtex_view_method . '_hook_' . hook
if exists(hookfunc)
execute 'let b:vimtex.viewer.hook_' . hook . ' = function(g:vimtex_view_'
\ . g:vimtex_view_method . '_hook_' . hook . ')'
endif
endfor
" "
" Define commands " Define commands
" "
@ -98,6 +110,10 @@ function! s:general.view(file) dict " {{{2
let exe.cmd .= ' ' . vimtex#util#fnameescape(outfile) let exe.cmd .= ' ' . vimtex#util#fnameescape(outfile)
call vimtex#util#execute(exe) call vimtex#util#execute(exe)
let self.cmd_view = exe.cmd let self.cmd_view = exe.cmd
if has_key(self, 'hook_view')
call self.hook_view()
endif
endfunction endfunction
" }}}2 " }}}2
@ -125,6 +141,14 @@ function! s:mupdf.init() dict " {{{2
call vimtex#echo#warning('vimtex viewer MuPDF requires xdotool!') call vimtex#echo#warning('vimtex viewer MuPDF requires xdotool!')
endif endif
"
" Default MuPDF settings
"
call vimtex#util#set_default('g:vimtex_view_mupdf_hook_callback',
\ 's:focus_vim')
call vimtex#util#set_default('g:vimtex_view_mupdf_hook_view',
\ 's:focus_viewer')
let self.class = 'MuPDF' let self.class = 'MuPDF'
let self.xwin_id = 0 let self.xwin_id = 0
let self.xwin_exists = function('s:xwin_exists') let self.xwin_exists = function('s:xwin_exists')
@ -144,6 +168,10 @@ function! s:mupdf.view(file) dict " {{{2
else else
call self.forward_search(outfile) call self.forward_search(outfile)
endif endif
if has_key(self, 'hook_view')
call self.hook_view()
endif
endfunction endfunction
" }}}2 " }}}2
@ -235,7 +263,9 @@ function! s:mupdf.latexmk_callback() dict " {{{2
if self.xwin_get_id() if self.xwin_get_id()
call self.xwin_send_keys(g:vimtex_view_mupdf_send_keys) call self.xwin_send_keys(g:vimtex_view_mupdf_send_keys)
call self.forward_search(b:vimtex.out()) call self.forward_search(b:vimtex.out())
call self.focus_vim() if has_key(self, 'hook_callback')
call self.hook_callback()
endif
endif endif
endif endif
endfunction endfunction
@ -270,6 +300,10 @@ function! s:okular.view(file) dict " {{{2
let exe.cmd .= '\#src:' . line('.') . vimtex#util#fnameescape(expand('%:p')) let exe.cmd .= '\#src:' . line('.') . vimtex#util#fnameescape(expand('%:p'))
call vimtex#util#execute(exe) call vimtex#util#execute(exe)
let self.cmd_view = exe.cmd let self.cmd_view = exe.cmd
if has_key(self, 'hook_view')
call self.hook_view()
endif
endfunction endfunction
" }}}2 " }}}2
@ -294,6 +328,10 @@ function! s:qpdfview.view(file) dict " {{{2
let exe.cmd .= ':' . col('.') let exe.cmd .= ':' . col('.')
call vimtex#util#execute(exe) call vimtex#util#execute(exe)
let self.cmd_view = exe.cmd let self.cmd_view = exe.cmd
if has_key(self, 'hook_view')
call self.hook_view()
endif
endfunction endfunction
" }}}2 " }}}2
@ -317,6 +355,10 @@ function! s:sumatrapdf.view(file) dict " {{{2
let exe.cmd .= ' ' . vimtex#util#fnameescape(outfile) let exe.cmd .= ' ' . vimtex#util#fnameescape(outfile)
call vimtex#util#execute(exe) call vimtex#util#execute(exe)
let self.cmd_view = exe.cmd let self.cmd_view = exe.cmd
if has_key(self, 'hook_view')
call self.hook_view()
endif
endfunction endfunction
" }}}2 " }}}2
@ -334,12 +376,18 @@ function! s:zathura.init() dict " {{{2
call vimtex#echo#warning('vimtex viewer Zathura requires xdotool!') call vimtex#echo#warning('vimtex viewer Zathura requires xdotool!')
endif endif
"
" Default Zathura settings
"
call vimtex#util#set_default('g:vimtex_view_zathura_hook_callback',
\ 's:focus_vim')
call vimtex#util#set_default('g:vimtex_view_zathura_hook_view',
\ 's:focus_viewer')
let self.class = 'Zathura' let self.class = 'Zathura'
let self.xwin_id = 0 let self.xwin_id = 0
let self.xwin_get_id = function('s:xwin_get_id') let self.xwin_get_id = function('s:xwin_get_id')
let self.xwin_exists = function('s:xwin_exists') let self.xwin_exists = function('s:xwin_exists')
let self.focus_vim = function('s:focus_vim')
let self.focus_viewer = function('s:focus_viewer')
endfunction endfunction
" }}}2 " }}}2
@ -352,6 +400,10 @@ function! s:zathura.view(file) dict " {{{2
else else
call self.forward_search(outfile) call self.forward_search(outfile)
endif endif
if has_key(self, 'hook_view')
call self.hook_view()
endif
endfunction endfunction
" }}}2 " }}}2
@ -379,7 +431,6 @@ function! s:zathura.forward_search(outfile) dict " {{{2
let exe.cmd .= ' ' . vimtex#util#fnameescape(a:outfile) let exe.cmd .= ' ' . vimtex#util#fnameescape(a:outfile)
call vimtex#util#execute(exe) call vimtex#util#execute(exe)
let self.cmd_forward_search = exe.cmd let self.cmd_forward_search = exe.cmd
call self.focus_viewer()
endfunction endfunction
" }}}2 " }}}2
@ -387,7 +438,9 @@ function! s:zathura.latexmk_callback() dict " {{{2
if !self.xwin_exists() if !self.xwin_exists()
if self.xwin_get_id() if self.xwin_get_id()
call self.forward_search(b:vimtex.out()) call self.forward_search(b:vimtex.out())
call self.focus_vim() if has_key(self, 'hook_callback')
call self.hook_callback()
endif
endif endif
endif endif
endfunction endfunction
@ -466,6 +519,10 @@ function! s:xwin_send_keys(keys) dict " {{{1
endfunction endfunction
" }}}1 " }}}1
"
" Hook functions (used as default hooks in some cases)
"
function! s:focus_viewer() dict " {{{1 function! s:focus_viewer() dict " {{{1
if !executable('xdotool') | return | endif if !executable('xdotool') | return | endif