diff options
Diffstat (limited to 'autoload/vital/_crystal/Process.vim')
-rw-r--r-- | autoload/vital/_crystal/Process.vim | 88 |
1 files changed, 40 insertions, 48 deletions
diff --git a/autoload/vital/_crystal/Process.vim b/autoload/vital/_crystal/Process.vim index c2deb9e5..1f7b001c 100644 --- a/autoload/vital/_crystal/Process.vim +++ b/autoload/vital/_crystal/Process.vim @@ -1,5 +1,14 @@ if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'crystal') == -1 +" ___vital___ +" NOTE: lines between '" ___vital___' is generated by :Vitalize. +" Do not mofidify the code nor insert new lines before '" ___vital___' +function! s:_SID() abort + return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$') +endfunction +execute join(['function! vital#_crystal#Process#import() abort', printf("return map({'shellescape': '', 'has_vimproc': '', 'system': '', 'iconv': '', 'spawn': '', 'get_last_status': ''}, \"vital#_crystal#function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n") +delfunction s:_SID +" ___vital___ " TODO: move all comments to doc file. " " @@ -21,62 +30,36 @@ let s:need_trans = v:version < 704 || (v:version == 704 && !has('patch122')) let s:TYPE_DICT = type({}) let s:TYPE_LIST = type([]) -let s:TYPE_STRING = type("") - +let s:TYPE_STRING = type('') -" Execute program in the background from Vim. -" Return an empty string always. -" -" If a:expr is a List, shellescape() each argument. -" If a:expr is a String, the arguments are passed as-is. -" -" Windows: -" Using :!start , execute program without via cmd.exe. -" Spawning 'expr' with 'noshellslash' -" keep special characters from unwanted expansion. -" (see :help shellescape()) -" -" Unix: -" using :! , execute program in the background by shell. function! s:spawn(expr, ...) abort - let shellslash = 0 + if type(a:expr) is s:TYPE_LIST + let special = 1 + let cmdline = join(map(a:expr, 's:shellescape(v:val, special)'), ' ') + elseif type(a:expr) is s:TYPE_STRING + let cmdline = a:expr + if a:0 && a:1 + " for :! command + let cmdline = substitute(cmdline, '\([!%#]\|<[^<>]\+>\)', '\\\1', 'g') + endif + else + throw 'vital: Process: invalid argument (value type:' . type(a:expr) . ')' + endif if s:is_windows - let shellslash = &l:shellslash - setlocal noshellslash + silent execute '!start' cmdline + else + silent execute '!' cmdline '&' endif - try - if type(a:expr) is s:TYPE_LIST - let special = 1 - let cmdline = join(map(a:expr, 'shellescape(v:val, special)'), ' ') - elseif type(a:expr) is s:TYPE_STRING - let cmdline = a:expr - if a:0 && a:1 - " for :! command - let cmdline = substitute(cmdline, '\([!%#]\|<[^<>]\+>\)', '\\\1', 'g') - endif - else - throw 'Process.spawn(): invalid argument (value type:'.type(a:expr).')' - endif - if s:is_windows - silent execute '!start' cmdline - else - silent execute '!' cmdline '&' - endif - finally - if s:is_windows - let &l:shellslash = shellslash - endif - endtry return '' endfunction " iconv() wrapper for safety. function! s:iconv(expr, from, to) abort - if a:from == '' || a:to == '' || a:from ==? a:to + if a:from ==# '' || a:to ==# '' || a:from ==? a:to return a:expr endif let result = iconv(a:expr, a:from, a:to) - return result != '' ? result : a:expr + return result !=# '' ? result : a:expr endfunction " Check vimproc. @@ -128,7 +111,7 @@ function! s:system(str, ...) abort elseif type(a:1) is s:TYPE_STRING let args += [s:iconv(a:1, &encoding, 'char')] else - throw 'Process.system(): invalid argument (value type:'.type(a:1).')' + throw 'vital: Process: invalid argument (value type:' . type(a:1) . ')' endif elseif a:0 >= 2 " {command} [, {input} [, {timeout}]] @@ -145,13 +128,16 @@ function! s:system(str, ...) abort elseif type(a:str) is s:TYPE_STRING let command = a:str else - throw 'Process.system(): invalid argument (value type:'.type(a:str).')' + throw 'vital: Process: invalid argument (value type:' . type(a:str) . ')' endif if s:need_trans let command = s:iconv(command, &encoding, 'char') endif let args = [command] + args if background && (use_vimproc || !s:is_windows) + if has('nvim') + throw "vital: Process: neovim's system() doesn't support background(&) process (cmdline:" . string(a:str) . ')' + endif let args[0] = args[0] . ' &' endif @@ -167,8 +153,14 @@ function! s:get_last_status() abort endfunction if s:is_windows - function! s:shellescape(command) abort - return substitute(a:command, '[&()[\]{}^=;!''+,`~]', '^\0', 'g') + function! s:shellescape(...) abort + try + let shellslash = &shellslash + set noshellslash + return call('shellescape', a:000) + finally + let &shellslash = shellslash + endtry endfunction else function! s:shellescape(...) abort |