File: vim.vim

package info (click to toggle)
vim-vimtex 2.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,660 kB
  • sloc: makefile: 367; python: 103
file content (204 lines) | stat: -rw-r--r-- 4,330 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
195
196
197
198
199
200
201
202
203
204
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve LervÄg
" Email:      karl.yngve@gmail.com
"

function! vimtex#jobs#vim#new(cmd) abort " {{{1
  let l:job = deepcopy(s:job)
  let l:job.cmd = has('win32')
        \ ? 'cmd /s /c "' . a:cmd . '"'
        \ : ['sh', '-c', a:cmd]
  return l:job
endfunction

" }}}1
function! vimtex#jobs#vim#run(cmd) abort " {{{1
  call s:vim_{s:os}_run(a:cmd)
endfunction

" }}}1
function! vimtex#jobs#vim#capture(cmd) abort " {{{1
  return s:vim_{s:os}_capture(a:cmd)
endfunction

" }}}1

function! vimtex#jobs#vim#shell_default() abort " {{{1
  let s:saveshell = [
        \ &shell,
        \ &shellcmdflag,
        \ &shellquote,
        \ &shellxquote,
        \ &shellredir,
        \ &shellslash
        \]
  let &shell = 'cmd.exe'
  let &shellcmdflag = '/s /c'
  set shellquote& shellxquote& shellredir& shellslash&
endfunction

" }}}1
function! vimtex#jobs#vim#shell_restore() abort " {{{1
  let [   &shell,
        \ &shellcmdflag,
        \ &shellquote,
        \ &shellxquote,
        \ &shellredir,
        \ &shellslash] = s:saveshell
endfunction

" }}}1

let s:os = has('win32') ? 'win' : 'unix'


let s:job = {}

function! s:job.start() abort dict " {{{1
  let l:options = {}

  if self.capture_output
    let self._output = tempname()
    let l:options.out_io = 'file'
    let l:options.err_io = 'file'
    let l:options.out_name = self._output
    let l:options.err_name = self._output
  else
    let l:options.in_io = 'null'
    let l:options.out_io = 'null'
    let l:options.err_io = 'null'
  endif
  if self.detached
    let l:options.stoponexit = ''
  endif
  if !empty(self.cwd)
    let l:options.cwd = self.cwd
  endif

  let self.job = job_start(self.cmd, l:options)

  return self
endfunction

" }}}1
function! s:job.stop() abort dict " {{{1
  call job_stop(self.job)
  for l:dummy in range(25)
    sleep 1m
    if !self.is_running() | return | endif
  endfor
endfunction

" }}}1
function! s:job.wait() abort dict " {{{1
  for l:dummy in range(self.wait_timeout/10)
    sleep 10m
    if !self.is_running() | return | endif
  endfor

  call vimtex#log#warning('Job timed out while waiting!', join(self.cmd))
  call self.stop()
endfunction

" }}}1
function! s:job.is_running() abort dict " {{{1
  return job_status(self.job) ==# 'run'
endfunction

" }}}1
function! s:job.get_pid() abort dict " {{{1
  try
    return get(job_info(self.job), 'process')
  catch
    return 0
  endtry
endfunction

" }}}1
function! s:job.signal_hup() abort dict " {{{1
  if self.is_running()
    call job_stop(self.job, 'hup')
  endif
endfunction

" }}}1
function! s:job.output() abort dict " {{{1
  call self.wait()
  return self.capture_output ? readfile(self._output) : []
endfunction

" }}}1

function! s:job.__pprint() abort dict " {{{1
  let l:pid = self.get_pid()

  return [
        \ ['pid', l:pid ? l:pid : '-'],
        \ ['cmd', self.cmd_raw],
        \]
endfunction

" }}}1


function! s:vim_unix_run(cmd) abort " {{{1
  let s:saveshell = [
        \ &shell,
        \ &shellcmdflag,
        \ &shellquote,
        \ &shellredir,
        \]
  let &shell = s:shell
  set shellcmdflag& shellquote& shellredir&
  silent! call system(a:cmd)
  let [   &shell,
        \ &shellcmdflag,
        \ &shellquote,
        \ &shellredir] = s:saveshell
endfunction

" }}}1
function! s:vim_unix_capture(cmd) abort " {{{1
  let s:saveshell = [
        \ &shell,
        \ &shellcmdflag,
        \ &shellquote,
        \ &shellredir,
        \]
  let &shell = s:shell
  set shellcmdflag& shellquote& shellredir&

  silent! let l:output = systemlist(a:cmd)

  let [   &shell,
        \ &shellcmdflag,
        \ &shellquote,
        \ &shellredir] = s:saveshell

  return v:shell_error == 127 ? ['command not found'] : l:output
endfunction

let s:shell = executable('sh')
      \ ? 'sh'
      \ : (executable('/usr/bin/sh')
      \    ? '/usr/bin/sh' : '/bin/sh')

" }}}1

function! s:vim_win_run(cmd) abort " {{{1
  call vimtex#jobs#vim#shell_default()
  silent! call system('cmd /s /c "' . a:cmd . '"')
  call vimtex#jobs#vim#shell_restore()
endfunction

" }}}1
function! s:vim_win_capture(cmd) abort " {{{1
  call vimtex#jobs#vim#shell_default()
  silent! let l:output = systemlist('cmd /s /c "' . a:cmd . '"')
  call vimtex#jobs#vim#shell_restore()

  return l:output
endfunction

" }}}1