2012-09-30 08:04:46 +00:00
|
|
|
"=============================================================================
|
|
|
|
" FILE: util.vim
|
2017-06-15 00:11:15 +00:00
|
|
|
" AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
|
2017-06-15 00:04:27 +00:00
|
|
|
" License: MIT license
|
2012-09-30 08:04:46 +00:00
|
|
|
"=============================================================================
|
|
|
|
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#get_vital() abort "{{{
|
2016-12-13 21:23:37 +00:00
|
|
|
if !exists('s:V')
|
|
|
|
let s:V = vital#neosnippet#new()
|
|
|
|
endif
|
2014-02-15 01:53:59 +00:00
|
|
|
return s:V
|
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! s:get_prelude() abort "{{{
|
2014-02-15 01:53:59 +00:00
|
|
|
if !exists('s:Prelude')
|
|
|
|
let s:Prelude = neosnippet#util#get_vital().import('Prelude')
|
|
|
|
endif
|
|
|
|
return s:Prelude
|
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! s:get_list() abort "{{{
|
2014-02-15 01:53:59 +00:00
|
|
|
if !exists('s:List')
|
|
|
|
let s:List = neosnippet#util#get_vital().import('Data.List')
|
|
|
|
endif
|
|
|
|
return s:List
|
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! s:get_string() abort "{{{
|
2015-05-12 05:12:09 +00:00
|
|
|
if !exists('s:String')
|
|
|
|
let s:String = neosnippet#util#get_vital().import('Data.String')
|
|
|
|
endif
|
|
|
|
return s:String
|
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! s:get_process() abort "{{{
|
2014-02-15 01:53:59 +00:00
|
|
|
if !exists('s:Process')
|
|
|
|
let s:Process = neosnippet#util#get_vital().import('Process')
|
|
|
|
endif
|
|
|
|
return s:Process
|
|
|
|
endfunction"}}}
|
2012-09-30 08:04:46 +00:00
|
|
|
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#substitute_path_separator(...) abort "{{{
|
2014-02-15 01:53:59 +00:00
|
|
|
return call(s:get_prelude().substitute_path_separator, a:000)
|
2012-09-30 08:04:46 +00:00
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#system(...) abort "{{{
|
2014-02-15 01:53:59 +00:00
|
|
|
return call(s:get_process().system, a:000)
|
2012-09-30 08:04:46 +00:00
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#has_vimproc(...) abort "{{{
|
2014-02-15 01:53:59 +00:00
|
|
|
return call(s:get_process().has_vimproc, a:000)
|
2012-09-30 08:04:46 +00:00
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#is_windows(...) abort "{{{
|
2014-02-15 01:53:59 +00:00
|
|
|
return call(s:get_prelude().is_windows, a:000)
|
2012-09-30 08:04:46 +00:00
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#is_mac(...) abort "{{{
|
2014-02-15 01:53:59 +00:00
|
|
|
return call(s:get_prelude().is_mac, a:000)
|
2012-09-30 08:04:46 +00:00
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#get_last_status(...) abort "{{{
|
2014-02-15 01:53:59 +00:00
|
|
|
return call(s:get_process().get_last_status, a:000)
|
2012-09-30 08:04:46 +00:00
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#escape_pattern(...) abort "{{{
|
2016-12-13 21:23:37 +00:00
|
|
|
return call(s:get_string().escape_pattern, a:000)
|
2012-09-30 08:04:46 +00:00
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#iconv(...) abort "{{{
|
2014-02-15 01:53:59 +00:00
|
|
|
return call(s:get_process().iconv, a:000)
|
2012-09-30 08:04:46 +00:00
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#truncate(...) abort "{{{
|
2015-05-12 05:12:09 +00:00
|
|
|
return call(s:get_string().truncate, a:000)
|
2013-03-02 09:27:24 +00:00
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#strwidthpart(...) abort "{{{
|
2015-05-12 05:12:09 +00:00
|
|
|
return call(s:get_string().strwidthpart, a:000)
|
2013-03-02 09:31:05 +00:00
|
|
|
endfunction"}}}
|
2012-09-30 08:04:46 +00:00
|
|
|
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#expand(path) abort "{{{
|
2012-11-16 23:49:18 +00:00
|
|
|
return neosnippet#util#substitute_path_separator(
|
|
|
|
\ expand(escape(a:path, '*?[]"={}'), 1))
|
2012-09-30 08:09:49 +00:00
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#set_default(var, val, ...) abort "{{{
|
2015-12-26 02:42:46 +00:00
|
|
|
let old_var = get(a:000, 0, '')
|
|
|
|
if exists(old_var)
|
|
|
|
let {a:var} = {old_var}
|
|
|
|
elseif !exists(a:var)
|
2013-11-12 21:15:57 +00:00
|
|
|
let {a:var} = a:val
|
2012-09-30 08:17:31 +00:00
|
|
|
endif
|
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#set_dictionary_helper(...) abort "{{{
|
2014-02-15 01:53:59 +00:00
|
|
|
return call(s:get_prelude().set_dictionary_helper, a:000)
|
2012-09-30 08:04:46 +00:00
|
|
|
endfunction"}}}
|
|
|
|
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#get_cur_text() abort "{{{
|
2012-09-30 08:21:47 +00:00
|
|
|
return
|
|
|
|
\ (mode() ==# 'i' ? (col('.')-1) : col('.')) >= len(getline('.')) ?
|
|
|
|
\ getline('.') :
|
|
|
|
\ matchstr(getline('.'),
|
|
|
|
\ '^.*\%' . col('.') . 'c' . (mode() ==# 'i' ? '' : '.'))
|
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#get_next_text() abort "{{{
|
2016-01-04 03:13:47 +00:00
|
|
|
return getline('.')[len(neosnippet#util#get_cur_text()) :]
|
|
|
|
endfunction"}}}
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#print_error(string) abort "{{{
|
2015-08-15 00:24:17 +00:00
|
|
|
echohl Error | echomsg '[neosnippet] ' . a:string | echohl None
|
2012-09-30 08:30:21 +00:00
|
|
|
endfunction"}}}
|
2012-09-30 08:21:47 +00:00
|
|
|
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#parse_options(args, options_list) abort "{{{
|
2012-09-30 10:10:25 +00:00
|
|
|
let args = []
|
|
|
|
let options = {}
|
|
|
|
for arg in split(a:args, '\%(\\\@<!\s\)\+')
|
|
|
|
let arg = substitute(arg, '\\\( \)', '\1', 'g')
|
|
|
|
|
|
|
|
let matched_list = filter(copy(a:options_list),
|
|
|
|
\ 'stridx(arg, v:val) == 0')
|
|
|
|
for option in matched_list
|
|
|
|
let key = substitute(substitute(option, '-', '_', 'g'), '=$', '', '')[1:]
|
|
|
|
let options[key] = (option =~ '=$') ?
|
|
|
|
\ arg[len(option) :] : 1
|
|
|
|
break
|
|
|
|
endfor
|
|
|
|
|
|
|
|
if empty(matched_list)
|
|
|
|
call add(args, arg)
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return [args, options]
|
|
|
|
endfunction"}}}
|
2016-01-18 02:50:11 +00:00
|
|
|
function! neosnippet#util#get_buffer_config(
|
|
|
|
\ filetype, buffer_var, user_var, default_var, ...) abort "{{{
|
|
|
|
let default_val = get(a:000, 0, '')
|
|
|
|
|
|
|
|
if exists(a:buffer_var)
|
|
|
|
return {a:buffer_var}
|
|
|
|
endif
|
|
|
|
|
|
|
|
let filetype = !has_key({a:user_var}, a:filetype)
|
|
|
|
\ && !has_key(eval(a:default_var), a:filetype) ? '_' : a:filetype
|
|
|
|
|
|
|
|
return get({a:user_var}, filetype,
|
|
|
|
\ get(eval(a:default_var), filetype, default_val))
|
|
|
|
endfunction"}}}
|
2012-09-30 10:10:25 +00:00
|
|
|
|
2013-09-26 06:24:48 +00:00
|
|
|
" Sudo check.
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#is_sudo() abort "{{{
|
2013-09-26 06:24:48 +00:00
|
|
|
return $SUDO_USER != '' && $USER !=# $SUDO_USER
|
|
|
|
\ && $HOME !=# expand('~'.$USER)
|
|
|
|
\ && $HOME ==# expand('~'.$SUDO_USER)
|
|
|
|
endfunction"}}}
|
|
|
|
|
2016-02-08 12:48:14 +00:00
|
|
|
function! neosnippet#util#option2list(str) abort "{{{
|
2013-11-12 08:31:06 +00:00
|
|
|
return type(a:str) == type('') ? split(a:str, '\s*,\s*') : a:str
|
|
|
|
endfunction"}}}
|
2012-09-30 10:10:25 +00:00
|
|
|
|
2012-09-30 08:04:46 +00:00
|
|
|
" vim: foldmethod=marker
|