|
| 1 | +let s:save_cpoptions = &cpoptions |
| 2 | +set cpoptions&vim |
| 3 | + |
| 4 | +let s:is_windows = has('win32') " This means any versions of windows https://github.com/vim-jp/vital.vim/wiki/Coding-Rule#how-to-check-if-the-runtime-os-is-windows |
| 5 | +let s:is_nvim = has('nvim') |
| 6 | + |
| 7 | +let s:TYPE_DICT = type({}) |
| 8 | +let s:TYPE_LIST = type([]) |
| 9 | +let s:TYPE_STRING = type('') |
| 10 | + |
| 11 | +function! s:_vital_loaded(V) abort |
| 12 | + let s:V = a:V |
| 13 | +endfunction |
| 14 | + |
| 15 | +function! s:_vital_depends() abort |
| 16 | + return [] |
| 17 | +endfunction |
| 18 | + |
| 19 | +if !s:is_nvim |
| 20 | + " inner callbacks for Vim |
| 21 | + function! s:inner_out_cb(user_out_cb, ch, msg) abort |
| 22 | + call a:user_out_cb(a:msg) |
| 23 | + endfunction |
| 24 | + |
| 25 | + function! s:inner_exit_cb(user_exit_cb, job, exit_code) abort |
| 26 | + call a:user_exit_cb(a:exit_code) |
| 27 | + endfunction |
| 28 | + |
| 29 | + function! s:inner_err_cb(user_err_cb, ch, msg) abort |
| 30 | + call a:user_err_cb(a:msg) |
| 31 | + endfunction |
| 32 | +else |
| 33 | + " inner callbacks for Neovim |
| 34 | + function! s:inner_out_cb(user_out_cb, job_id, data, event) abort |
| 35 | + for line in a:data |
| 36 | + if line !=# '' |
| 37 | + call a:user_out_cb(line) |
| 38 | + endif |
| 39 | + endfor |
| 40 | + endfunction |
| 41 | + |
| 42 | + function! s:inner_exit_cb(user_exit_cb, job_id, exit_code, event) abort |
| 43 | + call a:user_exit_cb(a:exit_code) |
| 44 | + endfunction |
| 45 | + |
| 46 | + function! s:inner_err_cb(user_err_cb, job_id, data, event) abort |
| 47 | + for line in a:data |
| 48 | + if line !=# '' |
| 49 | + call a:user_err_cb(line) |
| 50 | + endif |
| 51 | + endfor |
| 52 | + endfunction |
| 53 | +endif |
| 54 | + |
| 55 | +" execute({command}, {options}) |
| 56 | +" {command} = string |
| 57 | +" {options} = { |
| 58 | +" out_cb: function(stdout_msg), " call per line |
| 59 | +" err_cb: function(stderr_msg), " call per line |
| 60 | +" exit_cb: function(exit_code), " call on exit |
| 61 | +" } |
| 62 | +function! s:execute(command, options) abort |
| 63 | + if !type(a:options) is s:TYPE_DICT |
| 64 | + throw 'vital: AsyncProcess: invalid argument (value type:' . type(a:options) . ')' |
| 65 | + endif |
| 66 | + |
| 67 | + " Process a:command argument. |
| 68 | + if type(a:command) is s:TYPE_STRING |
| 69 | + let command = a:command |
| 70 | + elseif type(a:command) is s:TYPE_LIST |
| 71 | + let command = join(a:command, ' ') |
| 72 | + else |
| 73 | + throw 'vital: AsyncProcess: invalid argument (value type:' . type(a:command) . ')' |
| 74 | + endif |
| 75 | + |
| 76 | + " build args |
| 77 | + let args = [] |
| 78 | + if s:is_windows |
| 79 | + let args = args + ['/c'] |
| 80 | + else |
| 81 | + let args = args + ['-c'] |
| 82 | + endif |
| 83 | + let args = args + [command] |
| 84 | + |
| 85 | + if s:is_nvim |
| 86 | + let options = {} |
| 87 | + let options['on_stdout'] = function('s:inner_out_cb', [a:options.out_cb]) |
| 88 | + let options['on_stderr'] = function('s:inner_err_cb', [a:options.err_cb]) |
| 89 | + let options['on_exit'] = function('s:inner_exit_cb', [a:options.exit_cb]) |
| 90 | + |
| 91 | + call jobstart([&shell] + args, options) |
| 92 | + else |
| 93 | + let options = {} |
| 94 | + let options['out_cb'] = function('s:inner_out_cb', [a:options.out_cb]) |
| 95 | + let options['err_cb'] = function('s:inner_err_cb', [a:options.err_cb]) |
| 96 | + let options['exit_cb'] = function('s:inner_exit_cb', [a:options.exit_cb]) |
| 97 | + |
| 98 | + call job_start([&shell] + args, options) |
| 99 | + endif |
| 100 | +endfunction |
| 101 | + |
| 102 | +let &cpoptions = s:save_cpoptions |
| 103 | +unlet s:save_cpoptions |
| 104 | + |
0 commit comments