File: neovim.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 (181 lines) | stat: -rw-r--r-- 3,905 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
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve LervÄg
" Email:      karl.yngve@gmail.com
"

function! vimtex#jobs#neovim#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#neovim#run(cmd) abort " {{{1
  call s:neovim_{s:os}_run(a:cmd)
endfunction

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

" }}}1

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

" }}}1
function! vimtex#jobs#neovim#shell_restore() abort " {{{1
  let [&shell, &shellcmdflag, &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 = []
    let l:options.on_stdout = function('s:__callback')
    let l:options.on_stderr = function('s:__callback')
    let l:options.stdout_buffered = v:true
    let l:options.stderr_buffered = v:true
    let l:options.output = self._output
  endif
  if self.detached
    let l:options.detach = v:true
  endif
  if !empty(self.cwd)
    let l:options.cwd = self.cwd
  endif

  call vimtex#jobs#neovim#shell_default()
  let self.job = jobstart(self.cmd, l:options)
  call vimtex#jobs#neovim#shell_restore()

  return self
endfunction

function! s:__callback(id, data, event) abort dict
  call extend(self.output, a:data)
endfunction

" }}}1
function! s:job.stop() abort dict " {{{1
  call jobstop(self.job)
endfunction

" }}}1
function! s:job.wait() abort dict " {{{1
  let l:retvals = jobwait([self.job], self.wait_timeout)
  if empty(l:retvals) | return | endif
  let l:status = l:retvals[0]
  if l:status >= 0 | return | endif

  if l:status == -1
    call vimtex#log#warning('Job timed out while waiting!', join(self.cmd))
    call self.stop()
  elseif l:status == -2
    call vimtex#log#warning('Job interrupted!', self.cmd)
  endif
endfunction

" }}}1
function! s:job.is_running() abort dict " {{{1
  try
    let l:pid = jobpid(self.job)
    return l:pid > 0
  catch
    return v:false
  endtry
endfunction

" }}}1
function! s:job.get_pid() abort dict " {{{1
  try
    return jobpid(self.job)
  catch
    return 0
  endtry
endfunction

" }}}1
function! s:job.signal_hup() abort dict " {{{1
  let l:pid = self.get_pid()
  if l:pid > 0
    call system(['kill', '-HUP', l:pid])
  endif
endfunction

" }}}1
function! s:job.output() abort dict " {{{1
  call self.wait()

  if !self.capture_output | return [] | endif

  " Trim output
  while len(self._output) > 0
    if !empty(self._output[0]) | break | endif
    call remove(self._output, 0)
  endwhile
  while len(self._output) > 0
    if !empty(self._output[-1]) | break | endif
    call remove(self._output, -1)
  endwhile

  return 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:neovim_unix_run(cmd) abort " {{{1
  call system(['sh', '-c', a:cmd])
endfunction

" }}}1
function! s:neovim_unix_capture(cmd) abort " {{{1
  return systemlist(['sh', '-c', a:cmd])
endfunction

" }}}1

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

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

  return l:output
endfunction

" }}}1