neosnippet.vim/autoload/vital/_neosnippet/Prelude.vim

431 lines
12 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#Prelude#import() abort
return map({'escape_pattern': '', 'is_funcref': '', 'path2directory': '', 'wcswidth': '', 'is_string': '', 'input_helper': '', 'is_number': '', 'is_cygwin': '', 'path2project_directory': '', 'strwidthpart_reverse': '', 'input_safe': '', 'is_list': '', 'truncate_skipping': '', 'glob': '', 'truncate': '', 'is_dict': '', 'set_default': '', 'is_numeric': '', 'getchar_safe': '', 'substitute_path_separator': '', 'is_mac': '', 'strwidthpart': '', 'getchar': '', 'is_unix': '', 'is_windows': '', 'globpath': '', 'escape_file_searching': '', 'is_float': '', 'smart_execute_command': ''}, '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#Prelude#import() abort', printf("return map({'escape_pattern': '', 'is_funcref': '', 'path2directory': '', 'wcswidth': '', 'is_string': '', 'input_helper': '', 'is_number': '', 'is_cygwin': '', 'path2project_directory': '', 'strwidthpart_reverse': '', 'input_safe': '', 'is_list': '', 'truncate_skipping': '', 'glob': '', 'truncate': '', 'is_dict': '', 'set_default': '', 'is_numeric': '', 'getchar_safe': '', 'substitute_path_separator': '', 'is_mac': '', 'strwidthpart': '', 'getchar': '', 'is_unix': '', 'is_windows': '', 'globpath': '', 'escape_file_searching': '', 'is_float': '', 'smart_execute_command': ''}, \"function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n")
delfunction s:_SID
endif
" ___vital___
2012-12-25 13:58:04 +00:00
let s:save_cpo = &cpo
set cpo&vim
2012-09-30 08:06:28 +00:00
2016-12-13 21:23:37 +00:00
if v:version > 703 ||
\ (v:version == 703 && has('patch465'))
2015-05-12 05:11:55 +00:00
function! s:glob(expr) abort
2012-09-30 08:06:28 +00:00
return glob(a:expr, 1, 1)
endfunction
else
2015-05-12 05:11:55 +00:00
function! s:glob(expr) abort
2016-12-13 21:23:37 +00:00
return split(glob(a:expr, 1), '\n')
2012-09-30 08:06:28 +00:00
endfunction
endif
2013-07-06 07:46:13 +00:00
2016-12-13 21:23:37 +00:00
if v:version > 704 ||
\ (v:version == 704 && has('patch279'))
function! s:globpath(path, expr) abort
return globpath(a:path, a:expr, 1, 1)
endfunction
else
function! s:globpath(path, expr) abort
return split(globpath(a:path, a:expr, 1), '\n')
endfunction
endif
2012-09-30 08:06:28 +00:00
" Wrapper functions for type().
2016-12-13 21:23:37 +00:00
" NOTE: __TYPE_FLOAT = -1 when -float.
" this doesn't match to anything.
if has('patch-7.4.2071')
let [
\ s:__TYPE_NUMBER,
\ s:__TYPE_STRING,
\ s:__TYPE_FUNCREF,
\ s:__TYPE_LIST,
\ s:__TYPE_DICT,
\ s:__TYPE_FLOAT] = [
\ v:t_number,
\ v:t_string,
\ v:t_func,
\ v:t_list,
\ v:t_dict,
\ v:t_float]
else
let [
\ s:__TYPE_NUMBER,
\ s:__TYPE_STRING,
\ s:__TYPE_FUNCREF,
\ s:__TYPE_LIST,
\ s:__TYPE_DICT,
\ s:__TYPE_FLOAT] = [
\ type(3),
\ type(''),
\ type(function('tr')),
\ type([]),
\ type({}),
\ has('float') ? type(str2float('0')) : -1]
endif
2012-09-30 08:06:28 +00:00
" Number or Float
2015-05-12 05:11:55 +00:00
function! s:is_numeric(Value) abort
2012-09-30 08:06:28 +00:00
let _ = type(a:Value)
return _ ==# s:__TYPE_NUMBER
\ || _ ==# s:__TYPE_FLOAT
endfunction
2013-07-06 07:46:13 +00:00
2012-09-30 08:06:28 +00:00
" Number
2015-05-12 05:11:55 +00:00
function! s:is_number(Value) abort
2012-09-30 08:06:28 +00:00
return type(a:Value) ==# s:__TYPE_NUMBER
endfunction
2013-07-06 07:46:13 +00:00
2012-09-30 08:06:28 +00:00
" String
2015-05-12 05:11:55 +00:00
function! s:is_string(Value) abort
2012-09-30 08:06:28 +00:00
return type(a:Value) ==# s:__TYPE_STRING
endfunction
2016-12-13 21:23:37 +00:00
2012-09-30 08:06:28 +00:00
" Funcref
2015-05-12 05:11:55 +00:00
function! s:is_funcref(Value) abort
2012-09-30 08:06:28 +00:00
return type(a:Value) ==# s:__TYPE_FUNCREF
endfunction
2016-12-13 21:23:37 +00:00
2012-09-30 08:06:28 +00:00
" List
2015-05-12 05:11:55 +00:00
function! s:is_list(Value) abort
2012-09-30 08:06:28 +00:00
return type(a:Value) ==# s:__TYPE_LIST
endfunction
2016-12-13 21:23:37 +00:00
2012-09-30 08:06:28 +00:00
" Dictionary
2015-05-12 05:11:55 +00:00
function! s:is_dict(Value) abort
2012-09-30 08:06:28 +00:00
return type(a:Value) ==# s:__TYPE_DICT
endfunction
2016-12-13 21:23:37 +00:00
" Float
function! s:is_float(Value) abort
return type(a:Value) ==# s:__TYPE_FLOAT
endfunction
2015-05-12 05:11:55 +00:00
function! s:truncate_skipping(str, max, footer_width, separator) abort
2016-03-26 10:46:01 +00:00
call s:_warn_deprecated('truncate_skipping', 'Data.String.truncate_skipping')
2013-07-06 07:46:13 +00:00
2012-09-30 08:06:28 +00:00
let width = s:wcswidth(a:str)
if width <= a:max
let ret = a:str
else
let header_width = a:max - s:wcswidth(a:separator) - a:footer_width
let ret = s:strwidthpart(a:str, header_width) . a:separator
\ . s:strwidthpart_reverse(a:str, a:footer_width)
endif
return s:truncate(ret, a:max)
2012-12-25 13:58:04 +00:00
endfunction
2012-09-30 08:06:28 +00:00
2015-05-12 05:11:55 +00:00
function! s:truncate(str, width) abort
2012-09-30 08:06:28 +00:00
" Original function is from mattn.
" http://github.com/mattn/googlereader-vim/tree/master
2016-03-26 10:46:01 +00:00
call s:_warn_deprecated('truncate', 'Data.String.truncate')
2015-05-12 05:11:55 +00:00
2012-09-30 08:06:28 +00:00
if a:str =~# '^[\x00-\x7f]*$'
return len(a:str) < a:width ?
\ printf('%-'.a:width.'s', a:str) : strpart(a:str, 0, a:width)
endif
let ret = a:str
let width = s:wcswidth(a:str)
if width > a:width
let ret = s:strwidthpart(ret, a:width)
let width = s:wcswidth(ret)
endif
if width < a:width
let ret .= repeat(' ', a:width - width)
endif
return ret
2012-12-25 13:58:04 +00:00
endfunction
2012-09-30 08:06:28 +00:00
2015-05-12 05:11:55 +00:00
function! s:strwidthpart(str, width) abort
2016-03-26 10:46:01 +00:00
call s:_warn_deprecated('strwidthpart', 'Data.String.strwidthpart')
2015-05-12 05:11:55 +00:00
2012-09-30 08:06:28 +00:00
if a:width <= 0
return ''
endif
let ret = a:str
let width = s:wcswidth(a:str)
while width > a:width
let char = matchstr(ret, '.$')
let ret = ret[: -1 - len(char)]
let width -= s:wcswidth(char)
endwhile
return ret
2012-12-25 13:58:04 +00:00
endfunction
2015-05-12 05:11:55 +00:00
function! s:strwidthpart_reverse(str, width) abort
2016-03-26 10:46:01 +00:00
call s:_warn_deprecated('strwidthpart_reverse', 'Data.String.strwidthpart_reverse')
2015-05-12 05:11:55 +00:00
2012-09-30 08:06:28 +00:00
if a:width <= 0
return ''
endif
let ret = a:str
let width = s:wcswidth(a:str)
while width > a:width
let char = matchstr(ret, '^.')
let ret = ret[len(char) :]
let width -= s:wcswidth(char)
endwhile
return ret
2012-12-25 13:58:04 +00:00
endfunction
2012-09-30 08:06:28 +00:00
if v:version >= 703
" Use builtin function.
2015-05-12 05:11:55 +00:00
function! s:wcswidth(str) abort
2016-03-26 10:46:01 +00:00
call s:_warn_deprecated('wcswidth', 'Data.String.wcswidth')
2012-09-30 08:06:28 +00:00
return strwidth(a:str)
2012-12-25 13:58:04 +00:00
endfunction
2012-09-30 08:06:28 +00:00
else
2015-05-12 05:11:55 +00:00
function! s:wcswidth(str) abort
2016-03-26 10:46:01 +00:00
call s:_warn_deprecated('wcswidth', 'Data.String.wcswidth')
2015-05-12 05:11:55 +00:00
2012-09-30 08:06:28 +00:00
if a:str =~# '^[\x00-\x7f]*$'
return strlen(a:str)
end
let mx_first = '^\(.\)'
let str = a:str
let width = 0
while 1
let ucs = char2nr(substitute(str, mx_first, '\1', ''))
if ucs == 0
break
endif
let width += s:_wcwidth(ucs)
let str = substitute(str, mx_first, '', '')
endwhile
return width
2012-12-25 13:58:04 +00:00
endfunction
2012-09-30 08:06:28 +00:00
" UTF-8 only.
2015-05-12 05:11:55 +00:00
function! s:_wcwidth(ucs) abort
2012-09-30 08:06:28 +00:00
let ucs = a:ucs
if (ucs >= 0x1100
\ && (ucs <= 0x115f
\ || ucs == 0x2329
\ || ucs == 0x232a
\ || (ucs >= 0x2e80 && ucs <= 0xa4cf
\ && ucs != 0x303f)
\ || (ucs >= 0xac00 && ucs <= 0xd7a3)
\ || (ucs >= 0xf900 && ucs <= 0xfaff)
\ || (ucs >= 0xfe30 && ucs <= 0xfe6f)
\ || (ucs >= 0xff00 && ucs <= 0xff60)
\ || (ucs >= 0xffe0 && ucs <= 0xffe6)
\ || (ucs >= 0x20000 && ucs <= 0x2fffd)
\ || (ucs >= 0x30000 && ucs <= 0x3fffd)
\ ))
return 2
endif
return 1
2012-12-25 13:58:04 +00:00
endfunction
2012-09-30 08:06:28 +00:00
endif
2013-07-06 07:46:13 +00:00
let s:is_windows = has('win16') || has('win32') || has('win64') || has('win95')
2012-09-30 08:06:28 +00:00
let s:is_cygwin = has('win32unix')
2013-07-06 07:46:13 +00:00
let s:is_mac = !s:is_windows && !s:is_cygwin
2012-09-30 08:06:28 +00:00
\ && (has('mac') || has('macunix') || has('gui_macvim') ||
2012-12-25 13:58:04 +00:00
\ (!isdirectory('/proc') && executable('sw_vers')))
2013-07-06 07:46:13 +00:00
let s:is_unix = has('unix')
2015-05-12 05:11:55 +00:00
function! s:is_windows() abort
2012-09-30 08:06:28 +00:00
return s:is_windows
2012-12-25 13:58:04 +00:00
endfunction
2013-07-06 07:46:13 +00:00
2015-05-12 05:11:55 +00:00
function! s:is_cygwin() abort
2012-09-30 08:06:28 +00:00
return s:is_cygwin
2012-12-25 13:58:04 +00:00
endfunction
2013-07-06 07:46:13 +00:00
2015-05-12 05:11:55 +00:00
function! s:is_mac() abort
2012-09-30 08:06:28 +00:00
return s:is_mac
2012-12-25 13:58:04 +00:00
endfunction
2012-09-30 08:06:28 +00:00
2015-05-12 05:11:55 +00:00
function! s:is_unix() abort
2013-07-06 07:46:13 +00:00
return s:is_unix
endfunction
2015-05-12 05:11:55 +00:00
function! s:_warn_deprecated(name, alternative) abort
try
echohl Error
2016-03-26 10:46:01 +00:00
echomsg 'Prelude.' . a:name . ' is deprecated! Please use ' . a:alternative . ' instead.'
2015-05-12 05:11:55 +00:00
finally
echohl None
endtry
2012-12-25 13:58:04 +00:00
endfunction
2012-09-30 08:06:28 +00:00
2015-05-12 05:11:55 +00:00
function! s:smart_execute_command(action, word) abort
2016-03-26 10:46:01 +00:00
execute a:action . ' ' . (a:word ==# '' ? '' : '`=a:word`')
2012-12-25 13:58:04 +00:00
endfunction
2012-09-30 08:06:28 +00:00
2015-05-12 05:11:55 +00:00
function! s:escape_file_searching(buffer_name) abort
2012-09-30 08:06:28 +00:00
return escape(a:buffer_name, '*[]?{}, ')
2012-12-25 13:58:04 +00:00
endfunction
2013-07-06 07:46:13 +00:00
2015-05-12 05:11:55 +00:00
function! s:escape_pattern(str) abort
2016-12-13 21:23:37 +00:00
call s:_warn_deprecated(
\ 'escape_pattern',
\ 'Data.String.escape_pattern',
\)
2012-09-30 08:06:28 +00:00
return escape(a:str, '~"\.^$[]*')
2012-12-25 13:58:04 +00:00
endfunction
2013-07-06 07:46:13 +00:00
2015-05-12 05:11:55 +00:00
function! s:getchar(...) abort
2012-09-30 08:06:28 +00:00
let c = call('getchar', a:000)
return type(c) == type(0) ? nr2char(c) : c
endfunction
2013-07-06 07:46:13 +00:00
2015-05-12 05:11:55 +00:00
function! s:getchar_safe(...) abort
2012-09-30 08:06:28 +00:00
let c = s:input_helper('getchar', a:000)
2016-03-26 10:46:01 +00:00
return type(c) == type('') ? c : nr2char(c)
2012-09-30 08:06:28 +00:00
endfunction
2013-07-06 07:46:13 +00:00
2015-05-12 05:11:55 +00:00
function! s:input_safe(...) abort
2014-02-15 01:53:59 +00:00
return s:input_helper('input', a:000)
2012-09-30 08:06:28 +00:00
endfunction
2013-07-06 07:46:13 +00:00
2015-05-12 05:11:55 +00:00
function! s:input_helper(funcname, args) abort
2014-02-15 01:53:59 +00:00
let success = 0
if inputsave() !=# success
2016-03-26 10:46:01 +00:00
throw 'vital: Prelude: inputsave() failed'
2014-02-15 01:53:59 +00:00
endif
try
return call(a:funcname, a:args)
finally
if inputrestore() !=# success
2016-03-26 10:46:01 +00:00
throw 'vital: Prelude: inputrestore() failed'
2012-09-30 08:06:28 +00:00
endif
2014-02-15 01:53:59 +00:00
endtry
2012-09-30 08:06:28 +00:00
endfunction
2015-05-12 05:11:55 +00:00
function! s:set_default(var, val) abort
2012-09-30 08:06:28 +00:00
if !exists(a:var) || type({a:var}) != type(a:val)
let {a:var} = a:val
endif
2012-12-25 13:58:04 +00:00
endfunction
2013-07-06 07:46:13 +00:00
2015-05-12 05:11:55 +00:00
function! s:substitute_path_separator(path) abort
2012-09-30 08:06:28 +00:00
return s:is_windows ? substitute(a:path, '\\', '/', 'g') : a:path
2012-12-25 13:58:04 +00:00
endfunction
2013-07-06 07:46:13 +00:00
2015-05-12 05:11:55 +00:00
function! s:path2directory(path) abort
2012-09-30 08:06:28 +00:00
return s:substitute_path_separator(isdirectory(a:path) ? a:path : fnamemodify(a:path, ':p:h'))
2012-12-25 13:58:04 +00:00
endfunction
2013-07-06 07:46:13 +00:00
2015-05-12 05:11:55 +00:00
function! s:_path2project_directory_git(path) abort
2013-09-30 21:38:24 +00:00
let parent = a:path
while 1
let path = parent . '/.git'
if isdirectory(path) || filereadable(path)
return parent
endif
let next = fnamemodify(parent, ':h')
if next == parent
return ''
endif
let parent = next
endwhile
endfunction
2015-05-12 05:11:55 +00:00
function! s:_path2project_directory_svn(path) abort
2013-09-30 21:38:24 +00:00
let search_directory = a:path
let directory = ''
let find_directory = s:escape_file_searching(search_directory)
let d = finddir('.svn', find_directory . ';')
2016-03-26 10:46:01 +00:00
if d ==# ''
2013-09-30 21:38:24 +00:00
return ''
endif
let directory = fnamemodify(d, ':p:h:h')
" Search parent directories.
let parent_directory = s:path2directory(
\ fnamemodify(directory, ':h'))
2016-03-26 10:46:01 +00:00
if parent_directory !=# ''
2013-09-30 21:38:24 +00:00
let d = finddir('.svn', parent_directory . ';')
2016-03-26 10:46:01 +00:00
if d !=# ''
2013-09-30 21:38:24 +00:00
let directory = s:_path2project_directory_svn(parent_directory)
endif
endif
return directory
endfunction
2015-05-12 05:11:55 +00:00
function! s:_path2project_directory_others(vcs, path) abort
2013-09-30 21:38:24 +00:00
let vcs = a:vcs
let search_directory = a:path
let find_directory = s:escape_file_searching(search_directory)
let d = finddir(vcs, find_directory . ';')
2016-03-26 10:46:01 +00:00
if d ==# ''
2013-09-30 21:38:24 +00:00
return ''
endif
return fnamemodify(d, ':p:h:h')
endfunction
2015-05-12 05:11:55 +00:00
function! s:path2project_directory(path, ...) abort
2012-09-30 08:06:28 +00:00
let is_allow_empty = get(a:000, 0, 0)
let search_directory = s:path2directory(a:path)
let directory = ''
" Search VCS directory.
2013-07-06 07:46:13 +00:00
for vcs in ['.git', '.bzr', '.hg', '.svn']
2013-09-30 21:38:24 +00:00
if vcs ==# '.git'
let directory = s:_path2project_directory_git(search_directory)
elseif vcs ==# '.svn'
let directory = s:_path2project_directory_svn(search_directory)
else
let directory = s:_path2project_directory_others(vcs, search_directory)
2013-07-06 07:46:13 +00:00
endif
2016-03-26 10:46:01 +00:00
if directory !=# ''
2013-09-30 21:38:24 +00:00
break
2012-09-30 08:06:28 +00:00
endif
endfor
" Search project file.
2016-03-26 10:46:01 +00:00
if directory ==# ''
2014-02-15 01:53:59 +00:00
for d in ['build.xml', 'prj.el', '.project', 'pom.xml', 'package.json',
\ 'Makefile', 'configure', 'Rakefile', 'NAnt.build',
\ 'P4CONFIG', 'tags', 'gtags']
2012-09-30 08:06:28 +00:00
let d = findfile(d, s:escape_file_searching(search_directory) . ';')
2016-03-26 10:46:01 +00:00
if d !=# ''
2012-09-30 08:06:28 +00:00
let directory = fnamemodify(d, ':p:h')
break
endif
endfor
endif
2016-03-26 10:46:01 +00:00
if directory ==# ''
2012-09-30 08:06:28 +00:00
" Search /src/ directory.
let base = s:substitute_path_separator(search_directory)
if base =~# '/src/'
let directory = base[: strridx(base, '/src/') + 3]
endif
endif
2016-03-26 10:46:01 +00:00
if directory ==# '' && !is_allow_empty
2012-09-30 08:06:28 +00:00
" Use original path.
let directory = search_directory
endif
return s:substitute_path_separator(directory)
2012-12-25 13:58:04 +00:00
endfunction
2013-07-06 07:46:13 +00:00
2012-12-25 13:58:04 +00:00
let &cpo = s:save_cpo
unlet s:save_cpo
2012-09-30 08:06:28 +00:00
" vim:set et ts=2 sts=2 sw=2 tw=0: