summaryrefslogtreecommitdiffstats
path: root/autoload/vimtex/process.vim
blob: a22709c3a5e16971e55374046404e1c67c2150e4 (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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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#process#new(...) abort " {{{1
  let l:opts = a:0 > 0 ? a:1 : {}
  return extend(deepcopy(s:process), l:opts)
endfunction

" }}}1
function! vimtex#process#run(cmd, ...) abort " {{{1
  let l:opts = a:0 > 0 ? a:1 : {}
  let l:opts.cmd = a:cmd
  let l:process = vimtex#process#new(l:opts)

  return l:process.run()
endfunction

" }}}1
function! vimtex#process#capture(cmd) abort " {{{1
  return vimtex#process#run(a:cmd, {'capture': 1})
endfunction

" }}}1
function! vimtex#process#start(cmd, ...) abort " {{{1
  let l:opts = a:0 > 0 ? a:1 : {}
  let l:opts.continuous = 1
  return vimtex#process#run(a:cmd, l:opts)
endfunction

" }}}1

let s:process = {
      \ 'cmd' : '',
      \ 'pid' : 0,
      \ 'background' : 1,
      \ 'continuous' : 0,
      \ 'output' : '',
      \ 'workdir' : '',
      \ 'silent' : 1,
      \ 'capture' : 0,
      \ 'result' : '',
      \}

function! s:process.run() abort dict " {{{1
  if self._do_not_run() | return | endif

  call self._pre_run()
  call self._prepare()
  call self._execute()
  call self._restore()
  call self._post_run()

  return self.capture ? self.result : self
endfunction

" }}}1
function! s:process.stop() abort dict " {{{1
  if !self.pid | return | endif

  let l:cmd = has('win32')
        \ ? 'taskkill /PID ' . self.pid . ' /T /F'
        \ : 'kill ' . self.pid
  call vimtex#process#run(l:cmd, {'background': 0})

  let self.pid = 0
endfunction

" }}}1
function! s:process.pprint_items() abort dict " {{{1
  let l:list = [
        \ ['pid', self.pid ? self.pid : '-'],
        \ ['cmd', get(self, 'prepared_cmd', self.cmd)],
        \]

  return l:list
endfunction

" }}}1

function! s:process._do_not_run() abort dict " {{{1
  if empty(self.cmd)
    call vimtex#log#warning('Can''t run empty command')
    return 1
  endif
  if self.pid
    call vimtex#log#warning('Process already running!')
    return 1
  endif

  return 0
endfunction

" }}}1
function! s:process._pre_run() abort dict " {{{1
  if self.capture
    let self.silent = 0
    let self.background = 0
  elseif empty(self.output) && self.background
    let self.output = 'null'
  endif

  call vimtex#paths#pushd(self.workdir)
endfunction

" }}}1
function! s:process._execute() abort dict " {{{1
  if self.capture
    let self.result = split(system(self.prepared_cmd), '\n')
  elseif self.silent
    silent call system(self.prepared_cmd)
  elseif self.background
    silent execute '!' . self.prepared_cmd
    if !has('gui_running')
      redraw!
    endif
  else
    execute '!' . self.prepared_cmd
  endif

  " Capture the pid if relevant
  if has_key(self, 'set_pid') && self.continuous
    call self.set_pid()
  endif
endfunction

" }}}1
function! s:process._post_run() abort dict " {{{1
  call vimtex#paths#popd()
endfunction

" }}}1

if has('win32')
  function! s:process._prepare() abort dict " {{{1
    if &shell !~? 'cmd'
      let self.win32_restore_shell = 1
      let self.win32_saved_shell = [
            \ &shell,
            \ &shellcmdflag,
            \ &shellxquote,
            \ &shellxescape,
            \ &shellquote,
            \ &shellpipe,
            \ &shellredir,
            \ &shellslash
            \]
      set shell& shellcmdflag& shellxquote& shellxescape&
      set shellquote& shellpipe& shellredir& shellslash&
    else
      let self.win32_restore_shell = 0
    endif

    let l:cmd = self.cmd

    if self.background
      if !empty(self.output)
        let l:cmd .= self.output ==# 'null'
              \ ? ' >nul'
              \ : ' >'  . self.output
        let l:cmd = 'cmd /s /c "' . l:cmd . '"'
      else
        let l:cmd = 'cmd /c "' . l:cmd . '"'
      endif
      let l:cmd = 'start /b ' . cmd
    endif

    if self.silent && self.output ==# 'null'
      let self.prepared_cmd = '"' . l:cmd . '"'
    else
      let self.prepared_cmd = l:cmd
    endif
  endfunction

  " }}}1
  function! s:process._restore() abort dict " {{{1
    if self.win32_restore_shell
      let [   &shell,
            \ &shellcmdflag,
            \ &shellxquote,
            \ &shellxescape,
            \ &shellquote,
            \ &shellpipe,
            \ &shellredir,
            \ &shellslash] = self.win32_saved_shell
    endif
  endfunction

  " }}}1
  function! s:process.get_pid() abort dict " {{{1
    let self.pid = 0
  endfunction

  " }}}1
else
  function! s:process._prepare() abort dict " {{{1
    let l:cmd = self.cmd

    if self.background
      if !empty(self.output)
        let l:cmd .= ' >'
              \ . (self.output ==# 'null'
              \    ? '/dev/null'
              \    : shellescape(self.output))
              \ . ' 2>&1'
      endif
      let l:cmd .= ' &'
    endif

    if !self.silent
      let l:cmd = escape(l:cmd, '%#')
    endif

    let self.prepared_cmd = l:cmd
  endfunction

  " }}}1
  function! s:process._restore() abort dict " {{{1
  endfunction

  " }}}1
  function! s:process.get_pid() abort dict " {{{1
    let self.pid = 0
  endfunction

  " }}}1
endif

endif