summaryrefslogtreecommitdiffstats
path: root/autoload/vimtex/view/general.vim
blob: 701fe33cc97667bede5852c628ecf80982164229 (plain) (blame)
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
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'latex') == -1

" vimtex - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve Lervåg
" Email:      karl.yngve@gmail.com
"

function! vimtex#view#general#new() abort " {{{1
  " Check if the viewer is executable
  " * split to ensure that we handle stuff like "gio open"
  let l:exe = get(split(g:vimtex_view_general_viewer), 0, '')
  if empty(l:exe) || !executable(l:exe)
    call vimtex#log#warning(
          \ 'Selected viewer is not executable!',
          \ '- Selection: ' . g:vimtex_view_general_viewer .
          \ '- Executable: ' . l:exe .
          \ '- Please see :h g:vimtex_view_general_viewer')
    return {}
  endif

  " Start from standard template
  let l:viewer = vimtex#view#common#apply_common_template(deepcopy(s:general))

  " Add callback hook
  if exists('g:vimtex_view_general_callback')
    let l:viewer.compiler_callback = function(g:vimtex_view_general_callback)
  endif

  return l:viewer
endfunction

" }}}1

let s:general = {
      \ 'name' : 'General'
      \}

function! s:general.view(file) dict abort " {{{1
  if empty(a:file)
    let outfile = self.out()

    " Only copy files if they don't exist
    if g:vimtex_view_use_temp_files
          \ && vimtex#view#common#not_readable(outfile)
      call self.copy_files()
    endif
  else
    let outfile = a:file
  endif

  " Update the path for Windows on cygwin
  if executable('cygpath')
    let outfile = join(
          \ vimtex#process#capture('cygpath -aw "' . outfile . '"'), '')
  endif

  if vimtex#view#common#not_readable(outfile) | return | endif

  " Parse options
  let l:cmd  = g:vimtex_view_general_viewer
  let l:cmd .= ' ' . g:vimtex_view_general_options

  " Substitute magic patterns
  let l:cmd = substitute(l:cmd, '@line', line('.'), 'g')
  let l:cmd = substitute(l:cmd, '@col', col('.'), 'g')
  let l:cmd = substitute(l:cmd, '@tex',
        \ vimtex#util#shellescape(expand('%:p')), 'g')
  let l:cmd = substitute(l:cmd, '@pdf', vimtex#util#shellescape(outfile), 'g')

  " Start the view process
  let self.process = vimtex#process#start(l:cmd, {'silent': 0})

  if has_key(self, 'hook_view')
    call self.hook_view()
  endif
endfunction

" }}}1
function! s:general.latexmk_append_argument() dict abort " {{{1
  if g:vimtex_view_use_temp_files
    return ' -view=none'
  else
    let l:option = g:vimtex_view_general_viewer
    if !empty(g:vimtex_view_general_options_latexmk)
      let l:option .= ' '
      let l:option .= substitute(g:vimtex_view_general_options_latexmk,
            \                    '@line', line('.'), 'g')
    endif
    return vimtex#compiler#latexmk#wrap_option('pdf_previewer', l:option)
  endif
endfunction

" }}}1
function! s:general.compiler_callback(status) dict abort " {{{1
  if !a:status && g:vimtex_view_use_temp_files < 2
    return
  endif

  if g:vimtex_view_use_temp_files
    call self.copy_files()
  endif

  if has_key(self, 'hook_callback')
    call self.hook_callback()
  endif
endfunction

" }}}1

endif