2012-09-30 08:06:28 +00:00
|
|
|
let s:self_version = expand('<sfile>:t:r')
|
2015-05-12 05:11:55 +00:00
|
|
|
let s:self_file = expand('<sfile>')
|
2012-09-30 08:06:28 +00:00
|
|
|
|
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 = {}
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:import(name, ...) abort
|
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
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:load(...) dict abort
|
2012-09-30 08:06:28 +00:00
|
|
|
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
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:unload() abort
|
2012-09-30 08:06:28 +00:00
|
|
|
let s:loaded = {}
|
|
|
|
endfunction
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:exists(name) abort
|
2014-05-11 08:46:20 +00:00
|
|
|
return s:_get_module_path(a:name) !=# ''
|
|
|
|
endfunction
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:search(pattern) abort
|
|
|
|
let paths = s:_vital_files(a:pattern)
|
2014-05-11 08:46:20 +00:00
|
|
|
let modules = sort(map(paths, 's:_file2module(v:val)'))
|
|
|
|
return s:_uniq(modules)
|
|
|
|
endfunction
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:expand_modules(entry, all) abort
|
|
|
|
if type(a:entry) == type([])
|
|
|
|
let candidates = s:_concat(map(copy(a:entry), 's:search(v:val)'))
|
|
|
|
if empty(candidates)
|
|
|
|
throw printf('vital: Any of module %s is not found', string(a:entry))
|
|
|
|
endif
|
|
|
|
if eval(join(map(copy(candidates), 'has_key(a:all, v:val)'), '+'))
|
|
|
|
let modules = []
|
|
|
|
else
|
|
|
|
let modules = [candidates[0]]
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
let modules = s:search(a:entry)
|
|
|
|
if empty(modules)
|
|
|
|
throw printf('vital: Module %s is not found', a:entry)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
call filter(modules, '!has_key(a:all, v:val)')
|
|
|
|
for module in modules
|
|
|
|
let a:all[module] = 1
|
|
|
|
endfor
|
|
|
|
return modules
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:_import(name) abort
|
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
|
2015-05-12 05:11:55 +00:00
|
|
|
let sid = s:_get_sid_by_script(path)
|
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
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
let sid = s:_get_sid_by_script(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
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_get_module_path(name) abort
|
2013-08-18 11:32:40 +00:00
|
|
|
if s:_is_absolute_path(a:name) && filereadable(a:name)
|
2015-05-12 05:11:55 +00:00
|
|
|
return a:name
|
2013-07-06 07:46:13 +00:00
|
|
|
endif
|
|
|
|
if a:name ==# ''
|
2015-05-12 05:11:55 +00:00
|
|
|
let paths = [s:self_file]
|
2013-07-06 07:46:13 +00:00
|
|
|
elseif a:name =~# '\v^\u\w*%(\.\u\w*)*$'
|
2015-05-12 05:11:55 +00:00
|
|
|
let paths = s:_vital_files(a:name)
|
2013-07-06 07:46:13 +00:00
|
|
|
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
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
call filter(paths, 'filereadable(expand(v:val, 1))')
|
2014-05-11 08:46:20 +00:00
|
|
|
let path = get(paths, 0, '')
|
2015-05-12 05:11:55 +00:00
|
|
|
return path !=# '' ? path : ''
|
2013-07-06 07:46:13 +00:00
|
|
|
endfunction
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_get_sid_by_script(path) abort
|
|
|
|
let path = s:_unify_path(a:path)
|
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*$')
|
2015-05-12 05:11:55 +00:00
|
|
|
if !empty(list) && s:_unify_path(list[2]) ==# path
|
|
|
|
return list[1] - 0
|
2012-09-30 08:06:28 +00:00
|
|
|
endif
|
|
|
|
endfor
|
2015-05-12 05:11:55 +00:00
|
|
|
return 0
|
2012-09-30 08:06:28 +00:00
|
|
|
endfunction
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_file2module(file) abort
|
|
|
|
let filename = fnamemodify(a:file, ':p:gs?[\\/]?/?')
|
2014-05-11 08:46:20 +00:00
|
|
|
let tail = matchstr(filename, 'autoload/vital/_\w\+/\zs.*\ze\.vim$')
|
|
|
|
return join(split(tail, '[\\/]\+'), '.')
|
|
|
|
endfunction
|
|
|
|
|
2012-09-30 08:06:28 +00:00
|
|
|
if filereadable(expand('<sfile>:r') . '.VIM')
|
2015-05-12 05:11:55 +00:00
|
|
|
" resolve() is slow, so we cache results.
|
|
|
|
let s:_unify_path_cache = {}
|
|
|
|
" 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.
|
|
|
|
function! s:_unify_path(path) abort
|
|
|
|
if has_key(s:_unify_path_cache, a:path)
|
|
|
|
return s:_unify_path_cache[a:path]
|
|
|
|
endif
|
|
|
|
let value = tolower(fnamemodify(resolve(fnamemodify(
|
|
|
|
\ a:path, ':p')), ':~:gs?[\\/]?/?'))
|
|
|
|
let s:_unify_path_cache[a:path] = value
|
|
|
|
return value
|
2012-09-30 08:06:28 +00:00
|
|
|
endfunction
|
|
|
|
else
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_unify_path(path) abort
|
|
|
|
return resolve(fnamemodify(a:path, ':p:gs?[\\/]?/?'))
|
2012-09-30 08:06:28 +00:00
|
|
|
endfunction
|
|
|
|
endif
|
|
|
|
|
2014-05-11 08:46:20 +00:00
|
|
|
if s:globpath_third_arg
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_runtime_files(path) abort
|
2014-05-11 08:46:20 +00:00
|
|
|
return split(globpath(&runtimepath, a:path, 1), "\n")
|
|
|
|
endfunction
|
|
|
|
else
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_runtime_files(path) abort
|
2014-05-11 08:46:20 +00:00
|
|
|
return split(globpath(&runtimepath, a:path), "\n")
|
|
|
|
endfunction
|
|
|
|
endif
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
let s:_vital_files_cache_runtimepath = ''
|
|
|
|
let s:_vital_files_cache = []
|
|
|
|
function! s:_vital_files(pattern) abort
|
|
|
|
if s:_vital_files_cache_runtimepath !=# &runtimepath
|
|
|
|
let path = printf('autoload/vital/%s/**/*.vim', s:self_version)
|
|
|
|
let s:_vital_files_cache = s:_runtime_files(path)
|
|
|
|
let mod = ':p:gs?[\\/]\+?/?'
|
|
|
|
call map(s:_vital_files_cache, 'fnamemodify(v:val, mod)')
|
|
|
|
let s:_vital_files_cache_runtimepath = &runtimepath
|
|
|
|
endif
|
|
|
|
let target = substitute(a:pattern, '\.', '/', 'g')
|
|
|
|
let target = substitute(target, '\*', '[^/]*', 'g')
|
|
|
|
let regexp = printf('autoload/vital/%s/%s.vim', s:self_version, target)
|
|
|
|
return filter(copy(s:_vital_files_cache), 'v:val =~# regexp')
|
|
|
|
endfunction
|
|
|
|
|
2013-08-18 11:32:40 +00:00
|
|
|
" Copy from System.Filepath
|
|
|
|
if has('win16') || has('win32') || has('win64')
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_is_absolute_path(path) abort
|
2013-08-18 11:32:40 +00:00
|
|
|
return a:path =~? '^[a-z]:[/\\]'
|
|
|
|
endfunction
|
|
|
|
else
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_is_absolute_path(path) abort
|
2013-08-18 11:32:40 +00:00
|
|
|
return a:path[0] ==# '/'
|
|
|
|
endfunction
|
|
|
|
endif
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_build_module(sid) abort
|
2012-09-30 08:06:28 +00:00
|
|
|
if has_key(s:loaded, a:sid)
|
|
|
|
return copy(s:loaded[a:sid])
|
|
|
|
endif
|
2014-01-01 02:00:43 +00:00
|
|
|
let functions = s:_get_functions(a:sid)
|
2012-09-30 08:06:28 +00:00
|
|
|
|
2014-01-01 02:00:43 +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')
|
2015-05-12 05:11:55 +00:00
|
|
|
let all = {}
|
|
|
|
let modules =
|
|
|
|
\ s:_concat(map(module._vital_depends(),
|
|
|
|
\ 's:expand_modules(v:val, all)'))
|
|
|
|
call call(V.load, modules, V)
|
2012-09-30 08:06:28 +00:00
|
|
|
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
|
|
|
|
|
2014-01-01 02:00:43 +00:00
|
|
|
if exists('+regexpengine')
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_get_functions(sid) abort
|
2014-01-01 02:00:43 +00:00
|
|
|
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
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_get_functions(sid) abort
|
2014-01-01 02:00:43 +00:00
|
|
|
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
|
|
|
|
|
2014-05-11 08:46:20 +00:00
|
|
|
if exists('*uniq')
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_uniq(list) abort
|
2014-05-11 08:46:20 +00:00
|
|
|
return uniq(a:list)
|
|
|
|
endfunction
|
|
|
|
else
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_uniq(list) abort
|
2014-05-11 08:46:20 +00:00
|
|
|
let i = len(a:list) - 1
|
|
|
|
while 0 < i
|
|
|
|
if a:list[i] ==# a:list[i - 1]
|
|
|
|
call remove(a:list, i)
|
|
|
|
let i -= 2
|
|
|
|
else
|
|
|
|
let i -= 1
|
|
|
|
endif
|
|
|
|
endwhile
|
|
|
|
return a:list
|
|
|
|
endfunction
|
|
|
|
endif
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
function! s:_concat(lists) abort
|
|
|
|
let result_list = []
|
|
|
|
for list in a:lists
|
|
|
|
let result_list += list
|
|
|
|
endfor
|
|
|
|
return result_list
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:_redir(cmd) abort
|
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
|
|
|
|
|
2015-05-12 05:11:55 +00:00
|
|
|
function! vital#{s:self_version}#new() abort
|
2014-02-15 01:53:59 +00:00
|
|
|
return s:_import('')
|
2012-09-30 08:06:28 +00:00
|
|
|
endfunction
|