File: view.vim

package info (click to toggle)
vim-vimtex 2.17-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,844 kB
  • sloc: makefile: 360; python: 103
file content (194 lines) | stat: -rw-r--r-- 5,302 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve LervÄg
" Email:      karl.yngve@gmail.com
"

function! vimtex#view#init_buffer() abort
  if !g:vimtex_view_enabled | return | endif

  command! -buffer -nargs=? -complete=file VimtexView
        \ call vimtex#view#view(<q-args>)

  nnoremap <buffer> <plug>(vimtex-view) :VimtexView<cr>

  if has('nvim')
    call s:nvim_prune_servernames()
  endif
endfunction

function! vimtex#view#init_state(state) abort
  if !g:vimtex_view_enabled | return | endif
  if has_key(a:state, 'viewer') | return | endif

  augroup vimtex_viewer
    autocmd!
    autocmd User VimtexEventCompileSuccess call vimtex#view#compiler_callback()
    autocmd User VimtexEventCompileStopped call vimtex#view#compiler_stopped()
  augroup END

  try
    let a:state.viewer = vimtex#view#{g:vimtex_view_method}#new()
  catch /E117/
    call vimtex#log#warning(
          \ 'Invalid viewer: ' . g:vimtex_view_method,
          \ 'Please see :h g:vimtex_view_method')
    return
  endtry
endfunction


function! vimtex#view#view(...) abort
  if exists('*b:vimtex.viewer.view')
    call b:vimtex.viewer.view(a:0 > 0 ? a:1 : '')
  endif
endfunction

function! vimtex#view#compiler_callback() abort
  if exists('*b:vimtex.viewer.compiler_callback')
    if !b:vimtex.viewer.check() | return | endif

    let l:pdf = b:vimtex.viewer.out()
    if empty(l:pdf) | return | endif

    call b:vimtex.viewer.compiler_callback(l:pdf)
  endif
endfunction

function! vimtex#view#compiler_stopped() abort
  if exists('*b:vimtex.viewer.compiler_stopped')
    call b:vimtex.viewer.compiler_stopped()
  endif
endfunction


function! vimtex#view#inverse_search(line, filename, column = 0) abort
  " Only activate in VimTeX buffers
  if !exists('b:vimtex') | return -1 | endif

  " Only activate in relevant VimTeX projects
  let l:file = resolve(a:filename)
  let l:sources = b:vimtex.get_sources(#{ refresh: v:true })
  if vimtex#paths#is_abs(l:file)
    call map(l:sources, {_, x -> vimtex#paths#join(b:vimtex.root, x)})
  endif
  if index(l:sources, l:file) < 0 | return -2 | endif


  if mode() ==# 'i' | stopinsert | endif

  " Open file if necessary
  if !bufloaded(l:file)
    if filereadable(l:file)
      try
        execute g:vimtex_view_reverse_search_edit_cmd l:file
      catch
        call vimtex#log#warning([
              \ 'Reverse goto failed!',
              \ printf('Command error: %s %s',
              \        g:vimtex_view_reverse_search_edit_cmd, l:file)])
        return -3
      endtry
    else
      call vimtex#log#warning([
            \ 'Reverse goto failed!',
            \ printf('File not readable: "%s"', l:file)])
      return -4
    endif
  endif

  " Get buffer, window, and tab numbers
  " * If tab/window exists, switch to it/them
  let l:bufnr = bufnr(l:file)
  try
    let [l:winid; _] = win_findbuf(l:bufnr)
    let [l:tabnr, l:winnr] = win_id2tabwin(l:winid)
    execute l:tabnr . 'tabnext'
    execute l:winnr . 'wincmd w'
  catch
    execute g:vimtex_view_reverse_search_edit_cmd l:file
  endtry

  execute 'normal!' a:line . 'G'
  if a:column > 0
    execute 'normal!' a:column . '|'
  endif
  if b:vimtex.viewer.xdo_check()
    call b:vimtex.viewer.xdo_focus_vim()
  endif
  redraw

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

function! vimtex#view#inverse_search_cmd(line, filename, column) abort
  " One may call this function manually, but the main usage is to through the
  " command "VimtexInverseSearch". See ":help vimtex-synctex-inverse-search"
  " for more info.

  if a:line > 0 && !empty(a:filename)
    try
      if has('nvim')
        call s:inverse_search_cmd_nvim(a:line, a:filename, a:column)
      else
        call s:inverse_search_cmd_vim(a:line, a:filename, a:column)
      endif
    catch
    endtry
  endif

  quitall!
endfunction


function! s:inverse_search_cmd_nvim(line, filename, column) abort
  if !filereadable(s:nvim_servernames) | return | endif

  for l:server in readfile(s:nvim_servernames)
    try
      let l:socket = sockconnect('pipe', l:server, {'rpc': 1})
    catch
    endtry

    call rpcnotify(l:socket,
          \ 'nvim_call_function',
          \ 'vimtex#view#inverse_search',
          \ [a:line, a:filename, a:column])
    call chanclose(l:socket)
  endfor
endfunction

function! s:inverse_search_cmd_vim(line, filename, column) abort
  for l:server in split(serverlist(), "\n")
    call remote_expr(l:server,
          \ printf("vimtex#view#inverse_search(%d, '%s', %d)",
          \        a:line, a:filename, a:column))
  endfor
endfunction


function! s:nvim_prune_servernames() abort
  " Load servernames from file
  let l:servers = filereadable(s:nvim_servernames)
        \ ? readfile(s:nvim_servernames)
        \ : []

  " Check which servers are available
  let l:available_servernames = []
  for l:server in vimtex#util#uniq_unsorted(l:servers + [v:servername])
    try
      let l:socket = sockconnect('pipe', l:server)
      call add(l:available_servernames, l:server)
      call chanclose(l:socket)
    catch
    endtry
  endfor

  " Write the pruned list to file
  call writefile(l:available_servernames, s:nvim_servernames)
endfunction


let s:nvim_servernames = vimtex#cache#path('nvim_servernames.log')