2014-12-07 14:08:19 -05:00
|
|
|
" LaTeX plugin for Vim
|
|
|
|
"
|
|
|
|
" Maintainer: Karl Yngve Lervåg
|
|
|
|
" Email: karl.yngve@gmail.com
|
|
|
|
"
|
|
|
|
|
|
|
|
function! latex#view#init(initialized) " {{{1
|
2015-01-26 09:41:40 -05:00
|
|
|
call latex#util#set_default('g:latex_view_enabled', 1)
|
2014-12-07 14:08:19 -05:00
|
|
|
if !g:latex_view_enabled | return | endif
|
2014-12-08 11:34:21 -05:00
|
|
|
|
2015-01-26 09:41:40 -05:00
|
|
|
" Set default options
|
2014-12-07 14:08:19 -05:00
|
|
|
call latex#util#set_default('g:latex_view_general_options', '')
|
2015-01-18 16:43:53 -05:00
|
|
|
call latex#util#set_default_os_specific('g:latex_view_general_viewer',
|
|
|
|
\ {
|
|
|
|
\ 'linux' : 'xdg-open',
|
|
|
|
\ 'mac' : 'open',
|
|
|
|
\ })
|
2015-01-29 07:37:38 -05:00
|
|
|
call latex#util#set_default('g:latex_view_method', '')
|
|
|
|
call latex#util#set_default('g:latex_view_mupdf_options', '')
|
2015-01-29 15:12:43 -05:00
|
|
|
call latex#util#set_default('g:latex_view_mupdf_send_keys', '')
|
2015-01-29 07:37:38 -05:00
|
|
|
call latex#util#set_default('g:latex_view_okular_options', '')
|
|
|
|
call latex#util#set_default('g:latex_view_sumatrapdf_options', '')
|
2015-01-26 09:41:40 -05:00
|
|
|
call latex#util#error_deprecated('g:latex_viewer')
|
2014-12-07 14:08:19 -05:00
|
|
|
|
2015-01-26 09:41:40 -05:00
|
|
|
" Set view functions
|
2015-01-02 12:54:43 -05:00
|
|
|
let data = g:latex#data[b:latex.id]
|
2014-12-07 14:08:19 -05:00
|
|
|
if g:latex_view_method == 'mupdf'
|
2015-01-30 03:19:24 -05:00
|
|
|
call s:init_mupdf()
|
2015-01-02 12:54:43 -05:00
|
|
|
let data.view = function('latex#view#mupdf')
|
|
|
|
let data.rsearch = function('latex#view#mupdf_rsearch')
|
2015-01-29 07:37:38 -05:00
|
|
|
elseif g:latex_view_method == 'okular'
|
2015-01-30 03:19:24 -05:00
|
|
|
call s:init_okular()
|
2015-01-29 07:37:38 -05:00
|
|
|
let data.view = function('latex#view#okular')
|
2014-12-08 11:34:21 -05:00
|
|
|
elseif g:latex_view_method == 'sumatrapdf'
|
2015-01-30 03:19:24 -05:00
|
|
|
call s:init_sumatrapdf()
|
2015-01-02 12:54:43 -05:00
|
|
|
let data.view = function('latex#view#sumatrapdf')
|
2014-12-07 14:08:19 -05:00
|
|
|
else
|
2015-01-30 03:19:24 -05:00
|
|
|
call s:init_general()
|
2015-01-02 12:54:43 -05:00
|
|
|
let data.view = function('latex#view#general')
|
2014-12-07 14:08:19 -05:00
|
|
|
endif
|
|
|
|
|
2015-01-26 09:41:40 -05:00
|
|
|
" Define commands
|
2015-01-29 07:37:38 -05:00
|
|
|
command! -buffer VimLatexView call g:latex#data[b:latex.id].view()
|
2015-01-02 12:54:43 -05:00
|
|
|
if has_key(data, 'rsearch')
|
|
|
|
command! -buffer -nargs=* VimLatexRSearch
|
|
|
|
\ call g:latex#data[b:latex.id].rsearch()
|
|
|
|
endif
|
2014-12-07 14:08:19 -05:00
|
|
|
|
2015-01-26 09:41:40 -05:00
|
|
|
" Define mappings
|
2015-01-30 13:47:19 -05:00
|
|
|
nnoremap <buffer> <plug>(vl-view) :call g:latex#data[b:latex.id].view()<cr>
|
2015-01-30 07:02:27 -05:00
|
|
|
if has_key(data, 'rsearch')
|
2015-01-30 13:47:19 -05:00
|
|
|
nnoremap <buffer> <plug>(vl-reverse-search)
|
2015-01-30 07:02:27 -05:00
|
|
|
\ :call g:latex#data[b:latex.id].rsearch()<cr>
|
2014-12-07 14:08:19 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-12-08 11:34:21 -05:00
|
|
|
"}}}1
|
2015-01-29 07:37:38 -05:00
|
|
|
function! latex#view#general() " {{{1
|
2014-12-07 14:08:19 -05:00
|
|
|
let exe = {}
|
|
|
|
let exe.cmd = g:latex_view_general_viewer
|
|
|
|
|
2015-01-29 07:37:38 -05:00
|
|
|
let outfile = g:latex#data[b:latex.id].out()
|
|
|
|
if !filereadable(outfile)
|
|
|
|
echomsg "Can't view: Output file is not readable!"
|
|
|
|
return
|
2014-12-07 14:08:19 -05:00
|
|
|
endif
|
2015-01-29 07:37:38 -05:00
|
|
|
let exe.cmd .= ' ' . g:latex_view_general_options
|
2015-01-29 21:40:55 -05:00
|
|
|
let exe.cmd .= ' ' . latex#util#fnameescape(outfile)
|
2014-12-07 14:08:19 -05:00
|
|
|
|
|
|
|
call latex#util#execute(exe)
|
|
|
|
let g:latex#data[b:latex.id].cmds.view = exe.cmd
|
|
|
|
endfunction
|
|
|
|
|
2014-12-08 11:34:21 -05:00
|
|
|
"}}}1
|
2015-01-29 07:37:38 -05:00
|
|
|
function! latex#view#mupdf() "{{{1
|
2015-01-29 07:38:59 -05:00
|
|
|
if !s:mupdf_exists_win()
|
|
|
|
call s:mupdf_start()
|
2015-01-30 03:01:53 -05:00
|
|
|
elseif s:mupdf_forward_search
|
2015-01-29 07:38:59 -05:00
|
|
|
call s:mupdf_forward_search()
|
2014-12-07 14:08:19 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2015-01-02 12:54:43 -05:00
|
|
|
" }}}1
|
2015-01-29 15:12:43 -05:00
|
|
|
function! latex#view#mupdf_poststart() "{{{1
|
|
|
|
" First get the window id
|
2015-01-30 03:19:24 -05:00
|
|
|
if g:latex#data[b:latex.id].mupdf_id == 0
|
|
|
|
let mupdf_ids = []
|
|
|
|
if executable('xdotool')
|
|
|
|
let cmd = 'xdotool search --class MuPDF'
|
|
|
|
let mupdf_ids = systemlist(cmd)
|
|
|
|
endif
|
|
|
|
if len(mupdf_ids) == 0
|
|
|
|
echomsg "Couldn't find MuPDF window ID!"
|
|
|
|
let g:latex#data[b:latex.id].mupdf_id = 0
|
|
|
|
else
|
|
|
|
let g:latex#data[b:latex.id].mupdf_id = mupdf_ids[-1]
|
|
|
|
endif
|
2015-01-29 15:12:43 -05:00
|
|
|
endif
|
|
|
|
|
|
|
|
" Next return focus to vim and send some keys to mupdf if desired
|
|
|
|
if executable('xdotool')
|
|
|
|
if g:latex_view_mupdf_send_keys != ''
|
|
|
|
let cmd = 'xdotool key --window ' . g:latex#data[b:latex.id].mupdf_id
|
|
|
|
let cmd .= ' ' . g:latex_view_mupdf_send_keys
|
|
|
|
call system(cmd)
|
|
|
|
endif
|
|
|
|
|
|
|
|
silent execute '!xdotool windowfocus ' . v:windowid
|
|
|
|
endif
|
2015-01-30 03:01:53 -05:00
|
|
|
|
|
|
|
" Finally do a forward search
|
|
|
|
if s:mupdf_forward_search
|
|
|
|
call s:mupdf_forward_search()
|
|
|
|
endif
|
2015-01-29 15:12:43 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
"}}}1
|
2015-01-02 12:54:43 -05:00
|
|
|
function! latex#view#mupdf_rsearch() "{{{1
|
2015-01-29 07:38:59 -05:00
|
|
|
if !s:mupdf_exists_win()
|
|
|
|
echomsg "Can't search backwards: Is the PDF file open?"
|
2015-01-10 08:57:19 -05:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2015-01-29 07:38:59 -05:00
|
|
|
let data = g:latex#data[b:latex.id]
|
|
|
|
let outfile = data.out()
|
2015-01-10 08:57:19 -05:00
|
|
|
let mupdf_id = data.mupdf_id
|
2015-01-13 06:27:46 -05:00
|
|
|
let data.mupdf_rsearch = {}
|
2015-01-02 12:54:43 -05:00
|
|
|
|
|
|
|
" Get page number
|
|
|
|
let cmd = "xdotool getwindowname " . mupdf_id
|
2015-01-29 13:47:40 -05:00
|
|
|
let cmd .= " | sed 's:.* - \\([0-9]*\\)/.*:\\1:'"
|
|
|
|
let cmd .= " | tr -d '\n'"
|
2015-01-02 12:54:43 -05:00
|
|
|
let mupdf_page = system(cmd)
|
|
|
|
let data.mupdf_rsearch.page = mupdf_page
|
|
|
|
let data.mupdf_rsearch.page_cmd = cmd
|
|
|
|
if mupdf_page <= 0 | return | endif
|
|
|
|
|
2015-01-04 08:15:00 -05:00
|
|
|
" Get file
|
2015-01-02 12:54:43 -05:00
|
|
|
let cmd = "synctex edit "
|
|
|
|
let cmd .= "-o \"" . mupdf_page . ":288:108:" . outfile . "\""
|
|
|
|
let cmd .= "| grep 'Input:' | sed 's/Input://' "
|
|
|
|
let cmd .= "| head -n1 | tr -d '\n' 2>/dev/null"
|
|
|
|
let mupdf_infile = system(cmd)
|
|
|
|
let data.mupdf_rsearch.infile = mupdf_infile
|
|
|
|
let data.mupdf_rsearch.infile_cmd = cmd
|
|
|
|
|
2015-01-04 08:15:00 -05:00
|
|
|
" Get line
|
2015-01-02 12:54:43 -05:00
|
|
|
let cmd = "synctex edit "
|
|
|
|
let cmd .= "-o \"" . mupdf_page . ":288:108:" . outfile . "\""
|
|
|
|
let cmd .= "| grep -m1 'Line:' | sed 's/Line://' "
|
|
|
|
let cmd .= "| head -n1 | tr -d '\n'"
|
|
|
|
let line = system(cmd)
|
|
|
|
let data.mupdf_rsearch.line = line
|
|
|
|
let data.mupdf_rsearch.line_cmd = cmd
|
2015-01-04 08:15:00 -05:00
|
|
|
|
|
|
|
" Go to file and line
|
|
|
|
silent exec "edit " . mupdf_infile
|
2015-01-02 12:54:43 -05:00
|
|
|
if line > 0
|
|
|
|
silent exec ":" . line
|
2015-01-12 14:48:46 -05:00
|
|
|
" Unfold, move to top line to correspond to top pdf line, and go to end of
|
|
|
|
" line in case the corresponding pdf line begins on previous pdf page.
|
|
|
|
normal! zvztg_
|
2015-01-02 12:54:43 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-12-07 14:08:19 -05:00
|
|
|
" }}}1
|
2015-01-29 07:37:38 -05:00
|
|
|
function! latex#view#sumatrapdf() "{{{1
|
2014-12-08 11:34:21 -05:00
|
|
|
let outfile = g:latex#data[b:latex.id].out()
|
|
|
|
if !filereadable(outfile)
|
|
|
|
echomsg "Can't view: Output file is not readable!"
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let exe = {}
|
|
|
|
let exe.cmd = 'SumatraPDF ' . g:latex_view_sumatrapdf_options
|
2015-01-29 21:40:55 -05:00
|
|
|
let exe.cmd .= ' -forward-search ' . latex#util#fnameescape(expand('%:p'))
|
2014-12-08 11:34:21 -05:00
|
|
|
let exe.cmd .= ' ' . line('.')
|
2015-01-29 21:40:55 -05:00
|
|
|
let exe.cmd .= ' ' . latex#util#fnameescape(outfile)
|
2014-12-07 14:08:19 -05:00
|
|
|
|
2014-12-08 11:34:21 -05:00
|
|
|
call latex#util#execute(exe)
|
|
|
|
let g:latex#data[b:latex.id].cmds.view = exe.cmd
|
|
|
|
endfunction
|
|
|
|
|
2015-01-29 07:37:38 -05:00
|
|
|
" }}}1
|
|
|
|
function! latex#view#okular() "{{{1
|
|
|
|
let outfile = g:latex#data[b:latex.id].out()
|
|
|
|
if !filereadable(outfile)
|
|
|
|
echomsg "Can't view: Output file is not readable!"
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let exe = {}
|
|
|
|
let exe.cmd = 'okular ' . g:latex_view_okular_options
|
2015-01-29 21:40:55 -05:00
|
|
|
let exe.cmd .= ' --unique ' . latex#util#fnameescape(outfile)
|
|
|
|
let exe.cmd .= '\#src:' . line('.') . latex#util#fnameescape(expand('%:p'))
|
2015-01-29 07:37:38 -05:00
|
|
|
|
|
|
|
call latex#util#execute(exe)
|
|
|
|
let g:latex#data[b:latex.id].cmds.view = exe.cmd
|
|
|
|
endfunction
|
|
|
|
|
2014-12-08 11:34:21 -05:00
|
|
|
" }}}1
|
2014-12-08 14:37:42 -05:00
|
|
|
|
2015-01-30 03:19:24 -05:00
|
|
|
function! s:init_general() "{{{1
|
2014-12-07 14:08:19 -05:00
|
|
|
if !executable(g:latex_view_general_viewer)
|
|
|
|
echoerr "General viewer is not available!"
|
|
|
|
echoerr "g:latex_view_general_viewer = "
|
|
|
|
\ . g:latex_view_general_viewer
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
2015-01-30 03:19:24 -05:00
|
|
|
function! s:init_mupdf() "{{{1
|
2014-12-07 14:08:19 -05:00
|
|
|
if !executable('mupdf')
|
|
|
|
echoerr "MuPDF is not available!"
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Check if forward search is possible
|
|
|
|
let s:mupdf_forward_search = executable('synctex') && executable('xdotool')
|
2015-01-30 03:19:24 -05:00
|
|
|
|
|
|
|
" Initialize mupdf_id
|
|
|
|
if !has_key(g:latex#data[b:latex.id], 'mupdf_id')
|
|
|
|
let g:latex#data[b:latex.id].mupdf_id = 0
|
|
|
|
endif
|
2014-12-07 14:08:19 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
2015-01-30 03:19:24 -05:00
|
|
|
function! s:init_sumatrapdf() "{{{1
|
2014-12-08 11:34:21 -05:00
|
|
|
if !executable('SumatraPDF')
|
|
|
|
echoerr "SumatraPDF is not available!"
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2015-01-29 07:37:38 -05:00
|
|
|
"}}}1
|
2015-01-30 03:19:24 -05:00
|
|
|
function! s:init_okular() "{{{1
|
2015-01-29 07:37:38 -05:00
|
|
|
if !executable('okular')
|
|
|
|
echoerr "okular is not available!"
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-12-08 11:34:21 -05:00
|
|
|
"}}}1
|
2014-12-07 14:08:19 -05:00
|
|
|
|
2015-01-29 07:38:59 -05:00
|
|
|
function! s:mupdf_exists_win() "{{{1
|
|
|
|
if executable('xdotool')
|
|
|
|
let cmd = 'xdotool search --class MuPDF'
|
|
|
|
let mupdf_ids = systemlist(cmd)
|
|
|
|
for id in mupdf_ids
|
|
|
|
if id == g:latex#data[b:latex.id].mupdf_id | return 1 | endif
|
|
|
|
endfor
|
|
|
|
endif
|
2015-01-10 08:57:19 -05:00
|
|
|
|
|
|
|
return 0
|
|
|
|
endfunction
|
|
|
|
|
2015-01-29 07:37:38 -05:00
|
|
|
"}}}1
|
2015-01-29 07:38:59 -05:00
|
|
|
function! s:mupdf_forward_search() "{{{1
|
|
|
|
let outfile = g:latex#data[b:latex.id].out()
|
|
|
|
|
|
|
|
let l:cmd = "synctex view -i "
|
|
|
|
\ . (line(".") + 1) . ":"
|
|
|
|
\ . (col(".") + 1) . ":"
|
2015-01-30 04:05:35 -05:00
|
|
|
\ . latex#util#fnameescape(expand("%:p"))
|
2015-01-29 21:40:55 -05:00
|
|
|
\ . " -o " . latex#util#fnameescape(outfile)
|
2015-01-29 07:38:59 -05:00
|
|
|
\ . " | grep -m1 'Page:' | sed 's/Page://' | tr -d '\n'"
|
|
|
|
let l:page = system(l:cmd)
|
|
|
|
let g:latex#data[b:latex.id].cmds.view_mupdf_synctex = l:cmd
|
|
|
|
let g:latex#data[b:latex.id].cmds.view_mupdf_synctex_page = l:page
|
|
|
|
|
|
|
|
if l:page > 0
|
|
|
|
let exe = {}
|
|
|
|
let exe.cmd = 'xdotool'
|
|
|
|
let exe.cmd .= ' type --window ' . g:latex#data[b:latex.id].mupdf_id
|
|
|
|
let exe.cmd .= ' "' . l:page . 'g"'
|
|
|
|
call latex#util#execute(exe)
|
|
|
|
let g:latex#data[b:latex.id].cmds.view_mupdf_xdotool = exe.cmd
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
"}}}1
|
|
|
|
function! s:mupdf_start() "{{{1
|
|
|
|
let outfile = g:latex#data[b:latex.id].out()
|
2015-01-10 08:57:19 -05:00
|
|
|
if !filereadable(outfile)
|
|
|
|
echomsg "Can't view: Output file is not readable!"
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2015-01-29 07:38:59 -05:00
|
|
|
" Start MuPDF
|
2015-01-10 08:57:19 -05:00
|
|
|
let exe = {}
|
|
|
|
let exe.cmd = 'mupdf ' . g:latex_view_mupdf_options
|
2015-01-29 21:40:55 -05:00
|
|
|
let exe.cmd .= ' ' . latex#util#fnameescape(outfile)
|
2015-01-10 08:57:19 -05:00
|
|
|
call latex#util#execute(exe)
|
|
|
|
let g:latex#data[b:latex.id].cmds.view = exe.cmd
|
|
|
|
|
2015-01-29 15:12:43 -05:00
|
|
|
call latex#view#mupdf_poststart()
|
2015-01-10 08:57:19 -05:00
|
|
|
endfunction
|
2015-01-29 07:37:38 -05:00
|
|
|
|
2015-01-10 08:57:19 -05:00
|
|
|
"}}}1
|
|
|
|
|
2014-12-08 14:44:17 -05:00
|
|
|
" vim: fdm=marker sw=2
|