neosnippet.vim/autoload/vital/_neosnippet.vim

204 lines
5.3 KiB
VimL
Raw Normal View History

2012-09-30 08:06:28 +00:00
let s:self_version = expand('<sfile>:t:r')
2013-09-30 21:38:24 +00:00
" Note: The extra argument to globpath() was added in Patch 7.2.051.
let s:globpath_third_arg = v:version > 702 || v:version == 702 && has('patch51')
2012-09-30 08:06:28 +00:00
let s:loaded = {}
function! s:import(name, ...)
2012-12-25 13:58:04 +00:00
let target = {}
let functions = []
2012-09-30 08:06:28 +00:00
for a in a:000
if type(a) == type({})
2012-12-25 13:58:04 +00:00
let target = a
elseif type(a) == type([])
let functions = a
2012-09-30 08:06:28 +00:00
endif
unlet a
endfor
2013-09-30 21:38:24 +00:00
let module = s:_import(a:name)
2012-12-25 13:58:04 +00:00
if empty(functions)
call extend(target, module, 'keep')
else
for f in functions
if has_key(module, f) && !has_key(target, f)
let target[f] = module[f]
endif
endfor
endif
return target
2012-09-30 08:06:28 +00:00
endfunction
function! s:load(...) dict
for arg in a:000
let [name; as] = type(arg) == type([]) ? arg[: 1] : [arg, arg]
let target = split(join(as, ''), '\W\+')
let dict = self
while 1 <= len(target)
let ns = remove(target, 0)
if !has_key(dict, ns)
let dict[ns] = {}
endif
if type(dict[ns]) == type({})
let dict = dict[ns]
else
unlet dict
break
endif
endwhile
if exists('dict')
2013-09-30 21:38:24 +00:00
call extend(dict, s:_import(name))
2012-09-30 08:06:28 +00:00
endif
unlet arg
endfor
return self
endfunction
function! s:unload()
let s:loaded = {}
endfunction
2013-09-30 21:38:24 +00:00
function! s:_import(name)
2012-09-30 08:06:28 +00:00
if type(a:name) == type(0)
2012-12-25 13:58:04 +00:00
return s:_build_module(a:name)
2012-09-30 08:06:28 +00:00
endif
2013-07-06 07:46:13 +00:00
let path = s:_get_module_path(a:name)
if path ==# ''
throw 'vital: module not found: ' . a:name
2012-09-30 08:06:28 +00:00
endif
2013-09-30 21:38:24 +00:00
let sid = get(s:_scripts(), path, 0)
2012-09-30 08:06:28 +00:00
if !sid
try
2012-12-25 13:58:04 +00:00
execute 'source' fnameescape(path)
2012-09-30 08:06:28 +00:00
catch /^Vim\%((\a\+)\)\?:E484/
throw 'vital: module not found: ' . a:name
catch /^Vim\%((\a\+)\)\?:E127/
" Ignore.
endtry
2013-09-30 21:38:24 +00:00
let sid = s:_scripts()[path]
2012-09-30 08:06:28 +00:00
endif
2012-12-25 13:58:04 +00:00
return s:_build_module(sid)
2012-09-30 08:06:28 +00:00
endfunction
2013-07-06 07:46:13 +00:00
function! s:_get_module_path(name)
2013-08-18 11:32:40 +00:00
if s:_is_absolute_path(a:name) && filereadable(a:name)
2013-07-06 07:46:13 +00:00
return s:_unify_path(a:name)
endif
if a:name ==# ''
let tailpath = printf('autoload/vital/%s.vim', s:self_version)
elseif a:name =~# '\v^\u\w*%(\.\u\w*)*$'
let target = '/' . substitute(a:name, '\W\+', '/', 'g')
let tailpath = printf('autoload/vital/%s%s.vim', s:self_version, target)
else
2013-09-30 21:38:24 +00:00
throw 'vital: Invalid module name: ' . a:name
2013-07-06 07:46:13 +00:00
endif
2013-09-30 21:38:24 +00:00
if s:globpath_third_arg
2013-07-06 07:46:13 +00:00
let paths = split(globpath(&runtimepath, tailpath, 1), "\n")
else
let paths = split(globpath(&runtimepath, tailpath), "\n")
endif
call filter(paths, 'filereadable(v:val)')
return s:_unify_path(get(paths, 0, ''))
endfunction
2012-09-30 08:06:28 +00:00
function! s:_scripts()
let scripts = {}
2013-09-30 21:38:24 +00:00
for line in filter(split(s:_redir('scriptnames'), "\n"),
\ 'stridx(v:val, s:self_version) > 0')
2012-09-30 08:06:28 +00:00
let list = matchlist(line, '^\s*\(\d\+\):\s\+\(.\+\)\s*$')
if !empty(list)
let scripts[s:_unify_path(list[2])] = list[1] - 0
endif
endfor
return scripts
endfunction
if filereadable(expand('<sfile>:r') . '.VIM')
function! s:_unify_path(path)
" Note: On windows, vim can't expand path names from 8.3 formats.
" So if getting full path via <sfile> and $HOME was set as 8.3 format,
" vital load duplicated scripts. Below's :~ avoid this issue.
return tolower(fnamemodify(resolve(fnamemodify(
\ a:path, ':p:gs?[\\/]\+?/?')), ':~'))
endfunction
else
function! s:_unify_path(path)
return resolve(fnamemodify(a:path, ':p:gs?[\\/]\+?/?'))
endfunction
endif
2013-08-18 11:32:40 +00:00
" Copy from System.Filepath
if has('win16') || has('win32') || has('win64')
function! s:_is_absolute_path(path)
return a:path =~? '^[a-z]:[/\\]'
endfunction
else
function! s:_is_absolute_path(path)
return a:path[0] ==# '/'
endfunction
endif
2012-12-25 13:58:04 +00:00
function! s:_build_module(sid)
2012-09-30 08:06:28 +00:00
if has_key(s:loaded, a:sid)
return copy(s:loaded[a:sid])
endif
let functions = s:_get_functions(a:sid)
2012-09-30 08:06:28 +00:00
let prefix = '<SNR>' . a:sid . '_'
2012-09-30 08:06:28 +00:00
let module = {}
for func in functions
let module[func] = function(prefix . func)
endfor
if has_key(module, '_vital_loaded')
let V = vital#{s:self_version}#new()
if has_key(module, '_vital_depends')
call call(V.load, module._vital_depends(), V)
endif
try
call module._vital_loaded(V)
catch
" FIXME: Show an error message for debug.
endtry
endif
2012-12-25 13:58:04 +00:00
if !get(g:, 'vital_debug', 0)
2012-09-30 08:06:28 +00:00
call filter(module, 'v:key =~# "^\\a"')
endif
let s:loaded[a:sid] = module
return copy(module)
endfunction
if exists('+regexpengine')
function! s:_get_functions(sid)
let funcs = s:_redir(printf("function /\\%%#=2^\<SNR>%d_", a:sid))
let map_pat = '<SNR>' . a:sid . '_\zs\w\+'
return map(split(funcs, "\n"), 'matchstr(v:val, map_pat)')
endfunction
else
function! s:_get_functions(sid)
let prefix = '<SNR>' . a:sid . '_'
let funcs = s:_redir('function')
let filter_pat = '^\s*function ' . prefix
let map_pat = prefix . '\zs\w\+'
return map(filter(split(funcs, "\n"),
\ 'stridx(v:val, prefix) > 0 && v:val =~# filter_pat'),
\ 'matchstr(v:val, map_pat)')
endfunction
endif
2012-09-30 08:06:28 +00:00
function! s:_redir(cmd)
2013-07-06 07:46:13 +00:00
let [save_verbose, save_verbosefile] = [&verbose, &verbosefile]
set verbose=0 verbosefile=
2012-09-30 08:06:28 +00:00
redir => res
silent! execute a:cmd
redir END
2013-07-06 07:46:13 +00:00
let [&verbose, &verbosefile] = [save_verbose, save_verbosefile]
2012-09-30 08:06:28 +00:00
return res
endfunction
function! vital#{s:self_version}#new()
2014-02-15 01:53:59 +00:00
return s:_import('')
2012-09-30 08:06:28 +00:00
endfunction