neosnippet.vim/autoload/vital/_neosnippet/Process.vim

182 lines
5.3 KiB
VimL
Raw Normal View History

2016-03-26 10:46:01 +00:00
" ___vital___
" NOTE: lines between '" ___vital___' is generated by :Vitalize.
" Do not mofidify the code nor insert new lines before '" ___vital___'
if v:version > 703 || v:version == 703 && has('patch1170')
function! vital#_neosnippet#Process#import() abort
return map({'shellescape': '', 'has_vimproc': '', 'system': '', 'iconv': '', 'spawn': '', 'get_last_status': ''}, 'function("s:" . v:key)')
endfunction
else
function! s:_SID() abort
return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
endfunction
execute join(['function! vital#_neosnippet#Process#import() abort', printf("return map({'shellescape': '', 'has_vimproc': '', 'system': '', 'iconv': '', 'spawn': '', 'get_last_status': ''}, \"function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n")
delfunction s:_SID
endif
" ___vital___
2014-02-15 01:53:59 +00:00
" TODO: move all comments to doc file.
"
"
" FIXME: This module name should be Vital.System ?
" But the name has been already taken.
let s:save_cpo = &cpo
set cpo&vim
" FIXME: Unfortunately, can't use s:_vital_loaded() for this purpose.
" Because these variables are used when this script file is loaded.
let s:is_windows = has('win16') || has('win32') || has('win64') || has('win95')
let s:is_unix = has('unix')
2015-05-12 05:11:55 +00:00
" As of 7.4.122, the system()'s 1st argument is converted internally by Vim.
" Note that Patch 7.4.122 does not convert system()'s 2nd argument and
" return-value. We must convert them manually.
let s:need_trans = v:version < 704 || (v:version == 704 && !has('patch122'))
let s:TYPE_DICT = type({})
let s:TYPE_LIST = type([])
2016-03-26 10:46:01 +00:00
let s:TYPE_STRING = type('')
2014-02-15 01:53:59 +00:00
2015-05-12 05:11:55 +00:00
function! s:spawn(expr, ...) abort
2014-06-30 23:05:07 +00:00
let shellslash = 0
2014-02-15 01:53:59 +00:00
if s:is_windows
let shellslash = &l:shellslash
setlocal noshellslash
endif
try
2015-05-12 05:11:55 +00:00
if type(a:expr) is s:TYPE_LIST
2014-02-15 01:53:59 +00:00
let special = 1
let cmdline = join(map(a:expr, 'shellescape(v:val, special)'), ' ')
2015-05-12 05:11:55 +00:00
elseif type(a:expr) is s:TYPE_STRING
2014-02-15 01:53:59 +00:00
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.
2015-05-12 05:11:55 +00:00
function! s:iconv(expr, from, to) abort
2016-03-26 10:46:01 +00:00
if a:from ==# '' || a:to ==# '' || a:from ==? a:to
2014-02-15 01:53:59 +00:00
return a:expr
endif
let result = iconv(a:expr, a:from, a:to)
2016-03-26 10:46:01 +00:00
return result !=# '' ? result : a:expr
2014-02-15 01:53:59 +00:00
endfunction
" Check vimproc.
2015-05-12 05:11:55 +00:00
function! s:has_vimproc() abort
2014-02-15 01:53:59 +00:00
if !exists('s:exists_vimproc')
try
call vimproc#version()
let s:exists_vimproc = 1
catch
let s:exists_vimproc = 0
endtry
endif
return s:exists_vimproc
endfunction
" * {command} [, {input} [, {timeout}]]
" * {command} [, {dict}]
" {dict} = {
" use_vimproc: bool,
" input: string,
" timeout: bool,
2015-05-12 05:11:55 +00:00
" background: bool,
2014-02-15 01:53:59 +00:00
" }
2015-05-12 05:11:55 +00:00
function! s:system(str, ...) abort
" Process optional arguments at first
" because use_vimproc is required later
" for a:str argument.
2014-02-15 01:53:59 +00:00
let input = ''
let use_vimproc = s:has_vimproc()
2015-05-12 05:11:55 +00:00
let background = 0
let args = []
2014-02-15 01:53:59 +00:00
if a:0 ==# 1
2015-05-12 05:11:55 +00:00
" {command} [, {dict}]
" a:1 = {dict}
if type(a:1) is s:TYPE_DICT
2014-02-15 01:53:59 +00:00
if has_key(a:1, 'use_vimproc')
let use_vimproc = a:1.use_vimproc
endif
if has_key(a:1, 'input')
let args += [s:iconv(a:1.input, &encoding, 'char')]
endif
if use_vimproc && has_key(a:1, 'timeout')
" ignores timeout unless you have vimproc.
let args += [a:1.timeout]
endif
2015-05-12 05:11:55 +00:00
if has_key(a:1, 'background')
let background = a:1.background
endif
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).')'
2014-02-15 01:53:59 +00:00
endif
elseif a:0 >= 2
2015-05-12 05:11:55 +00:00
" {command} [, {input} [, {timeout}]]
" a:000 = [{input} [, {timeout}]]
2014-02-15 01:53:59 +00:00
let [input; rest] = a:000
2015-05-12 05:11:55 +00:00
let input = s:iconv(input, &encoding, 'char')
2014-02-15 01:53:59 +00:00
let args += [input] + rest
endif
2015-05-12 05:11:55 +00:00
" Process a:str argument.
if type(a:str) is s:TYPE_LIST
let expr = use_vimproc ? '"''" . v:val . "''"' : 's:shellescape(v:val)'
let command = join(map(copy(a:str), expr), ' ')
elseif type(a:str) is s:TYPE_STRING
let command = a:str
else
throw 'Process.system(): 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)
let args[0] = args[0] . ' &'
endif
2014-05-11 08:46:20 +00:00
let funcname = use_vimproc ? 'vimproc#system' : 'system'
2014-02-15 01:53:59 +00:00
let output = call(funcname, args)
let output = s:iconv(output, 'char', &encoding)
return output
endfunction
2015-05-12 05:11:55 +00:00
function! s:get_last_status() abort
2014-02-15 01:53:59 +00:00
return s:has_vimproc() ?
\ vimproc#get_last_status() : v:shell_error
endfunction
if s:is_windows
2015-05-12 05:11:55 +00:00
function! s:shellescape(command) abort
2014-02-15 01:53:59 +00:00
return substitute(a:command, '[&()[\]{}^=;!''+,`~]', '^\0', 'g')
endfunction
else
2015-05-12 05:11:55 +00:00
function! s:shellescape(...) abort
2014-02-15 01:53:59 +00:00
return call('shellescape', a:000)
endfunction
endif
let &cpo = s:save_cpo
unlet s:save_cpo
" vim:set et ts=2 sts=2 sw=2 tw=0: