File: mupdf.vim

package info (click to toggle)
vim-vimtex 2.16-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,660 kB
  • sloc: makefile: 367; python: 103
file content (141 lines) | stat: -rw-r--r-- 4,004 bytes parent folder | download
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
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve LervÄg
" Email:      karl.yngve@gmail.com
"

function! vimtex#view#mupdf#new() abort " {{{1
  " Add reverse search mapping
  nnoremap <buffer> <plug>(vimtex-reverse-search)
        \ :<c-u>call b:vimtex.viewer.reverse_search()<cr>

  return s:viewer.init()
endfunction

" }}}1


let s:viewer = vimtex#view#_template#new({
      \ 'name': 'MuPDF',
      \ 'xwin_id': 0,
      \})

function! s:viewer.compiler_callback(outfile) dict abort " {{{1
  if g:vimtex_view_automatic && !has_key(self, 'started_through_callback')
    let self.started_through_callback = 1
    call self._start(a:outfile)
  endif

  if has_key(self, 'job') && self.job.get_pid() > 0
    call self.job.signal_hup()
  endif
endfunction

" }}}1
function! s:viewer.reverse_search() dict abort " {{{1
  if !executable('xdotool') | return | endif
  if !executable('synctex') | return | endif

  let l:outfile = self.out()
  if !filereadable(l:outfile) || !self.xdo_exists()
    call vimtex#log#warning('Reverse search failed (is MuPDF open?)')
    return
  endif

  " Get page number
  let self.cmd_getpage
        \ = 'xdotool getwindowname ' . self.xwin_id
        \ . "| sed 's:.* - \\([0-9]*\\)/.*:\\1:'"
        \ . "| tr -d '\n'"
  let self.page = vimtex#jobs#capture(self.cmd_getpage)[0]
  if self.page <= 0 | return | endif

  " Get file
  let self.cmd_getfile  = 'synctex edit '
        \ . "-o \"" . self.page . ':288:108:' . l:outfile . "\""
        \ . "| grep 'Input:' | sed 's/Input://' "
        \ . "| head -n1 | tr -d '\n' 2>/dev/null"
  let self.file = vimtex#jobs#capture(self.cmd_getfile)[0]

  " Get line
  let self.cmd_getline  = 'synctex edit '
        \ . "-o \"" . self.page . ':288:108:' . l:outfile . "\""
        \ . "| grep -m1 'Line:' | sed 's/Line://' "
        \ . "| head -n1 | tr -d '\n'"
  let self.line = vimtex#jobs#capture(self.cmd_getline)[0]

  " Go to file and line
  silent exec 'edit ' . fnameescape(self.file)
  if self.line > 0
    silent exec ':' . self.line
    " 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_
  endif
endfunction

" }}}1

function! s:viewer._check() dict abort " {{{1
  " Check if MuPDF is executable
  if !executable('mupdf')
    call vimtex#log#error('MuPDF is not executable!')
    return v:false
  endif

  return v:true
endfunction

" }}}1
function! s:viewer._exists() dict abort " {{{1
  return self.xdo_exists()
endfunction

" }}}1
function! s:viewer._start(outfile) dict abort " {{{1
  let l:cmd = 'mupdf'
  if !empty(g:vimtex_view_mupdf_options)
    let l:cmd .= ' ' . g:vimtex_view_mupdf_options
  endif
  let l:cmd .= ' ' . vimtex#util#shellescape(a:outfile)
  let self.cmd_start = l:cmd

  let self.job = vimtex#jobs#start(self.cmd_start, {'detached': v:true})
  if g:vimtex_view_forward_search_on_start
    call self._forward_search(a:outfile)
  endif

  call timer_start(500, self._start_post)
endfunction

" }}}1
function! s:viewer._start_post(_timer_id) dict abort " {{{1
  call self.xdo_get_id()
  call self.xdo_send_keys(g:vimtex_view_mupdf_send_keys)
endfunction

" }}}1
function! s:viewer._forward_search(outfile) dict abort " {{{1
  if !executable('xdotool') | return | endif
  if !executable('synctex') | return | endif
  if self.xwin_id <= 0 | return | endif

  let self.cmd_synctex_view = 'synctex view -i '
        \ . (line('.') + 1) . ':'
        \ . (col('.') + 1) . ':'
        \ . vimtex#util#shellescape(expand('%:p'))
        \ . ' -o ' . vimtex#util#shellescape(a:outfile)
        \ . " | grep -m1 'Page:' | sed 's/Page://' | tr -d '\n'"
  let self.page = vimtex#jobs#capture(self.cmd_synctex_view)[0]

  if self.page > 0
    let self.cmd_forward_search = 'xdotool'
          \ . ' type --window ' . self.xwin_id
          \ . ' "' . self.page . 'g"'
    call vimtex#jobs#run(self.cmd_forward_search)
  endif

  call self.xdo_focus_viewer()
endfunction

" }}}1