diff --git a/autoload/vital.vim b/autoload/vital.vim deleted file mode 100644 index bc0b525..0000000 --- a/autoload/vital.vim +++ /dev/null @@ -1,12 +0,0 @@ -function! vital#of(name) - let files = globpath(&runtimepath, 'autoload/vital/' . a:name . '.vital') - let file = split(files, "\n") - if empty(file) - throw 'vital: version file not found: ' . a:name - endif - let ver = readfile(file[0], 'b') - if empty(ver) - throw 'vital: invalid version file: ' . a:name - endif - return vital#_{substitute(ver[0], '\W', '', 'g')}#new() -endfunction diff --git a/autoload/vital/_autodirmake.vim b/autoload/vital/_autodirmake.vim index d65da5e..5510495 100644 --- a/autoload/vital/_autodirmake.vim +++ b/autoload/vital/_autodirmake.vim @@ -1,304 +1,9 @@ -let s:self_version = expand(':t:r') +let s:_plugin_name = expand(':t:r') -" 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') - -let s:loaded = {} - -function! s:import(name, ...) - let target = {} - let functions = [] - for a in a:000 - if type(a) == type({}) - let target = a - elseif type(a) == type([]) - let functions = a - endif - unlet a - endfor - let module = s:_import(a:name) - 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 -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') - call extend(dict, s:_import(name)) - endif - unlet arg - endfor - return self -endfunction - -function! s:unload() - let s:loaded = {} -endfunction - -function! s:exists(name) - return s:_get_module_path(a:name) !=# '' -endfunction - -function! s:search(pattern) - let paths = s:_vital_files(a:pattern) - let modules = sort(map(paths, 's:_file2module(v:val)')) - return s:_uniq(modules) -endfunction - -function! s:expand_modules(entry, all) - 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) - if type(a:name) == type(0) - return s:_build_module(a:name) - endif - let path = s:_get_module_path(a:name) - if path ==# '' - throw 'vital: module not found: ' . a:name - endif - let sid = s:_get_sid_by_script(path) - if !sid - try - execute 'source' fnameescape(path) - catch /^Vim\%((\a\+)\)\?:E484/ - throw 'vital: module not found: ' . a:name - catch /^Vim\%((\a\+)\)\?:E127/ - " Ignore. - endtry - - let sid = s:_get_sid_by_script(path) - endif - return s:_build_module(sid) -endfunction - -function! s:_get_module_path(name) - if s:_is_absolute_path(a:name) && filereadable(a:name) - return a:name - endif - if a:name ==# '' - let paths = [s:self_file] - elseif a:name =~# '\v^\u\w*%(\.\u\w*)*$' - let paths = s:_vital_files(a:name) - else - throw 'vital: Invalid module name: ' . a:name - endif - - call filter(paths, 'filereadable(expand(v:val))') - let path = get(paths, 0, '') - return path !=# '' ? path : '' +function! vital#{s:_plugin_name}#new() abort + return vital#{s:_plugin_name[1:]}#new() endfunction -function! s:_get_sid_by_script(path) - let path = s:_unify_path(a:path) - for line in filter(split(s:_redir('scriptnames'), "\n"), - \ 'stridx(v:val, s:self_version) > 0') - let list = matchlist(line, '^\s*\(\d\+\):\s\+\(.\+\)\s*$') - if !empty(list) && s:_unify_path(list[2]) ==# path - return list[1] - 0 - endif - endfor - return 0 +function! vital#{s:_plugin_name}#function(funcname) abort + silent! return function(a:funcname) endfunction - -function! s:_file2module(file) - let filename = fnamemodify(a:file, ':p:gs?[\\/]\+?/?') - let tail = matchstr(filename, 'autoload/vital/_\w\+/\zs.*\ze\.vim$') - return join(split(tail, '[\\/]\+'), '.') -endfunction - -if filereadable(expand(':r') . '.VIM') - " 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 and $HOME was set as 8.3 format, - " vital load duplicated scripts. Below's :~ avoid this issue. - function! s:_unify_path(path) - 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 - endfunction -else - function! s:_unify_path(path) - return resolve(fnamemodify(a:path, ':p:gs?[\\/]\+?/?')) - endfunction -endif - -if s:globpath_third_arg - function! s:_runtime_files(path) - return split(globpath(&runtimepath, a:path, 1), "\n") - endfunction -else - function! s:_runtime_files(path) - return split(globpath(&runtimepath, a:path), "\n") - endfunction -endif - -let s:_vital_files_cache_runtimepath = '' -let s:_vital_files_cache = [] -function! s:_vital_files(pattern) - 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 - -" 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 - -function! s:_build_module(sid) - if has_key(s:loaded, a:sid) - return copy(s:loaded[a:sid]) - endif - let functions = s:_get_functions(a:sid) - - let prefix = '' . a:sid . '_' - 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') - let all = {} - let modules = - \ s:_concat(map(module._vital_depends(), - \ 's:expand_modules(v:val, all)')) - call call(V.load, modules, V) - endif - try - call module._vital_loaded(V) - catch - " FIXME: Show an error message for debug. - endtry - endif - if !get(g:, 'vital_debug', 0) - 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^\%d_", a:sid)) - let map_pat = '' . a:sid . '_\zs\w\+' - return map(split(funcs, "\n"), 'matchstr(v:val, map_pat)') - endfunction -else - function! s:_get_functions(sid) - let prefix = '' . 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 - -if exists('*uniq') - function! s:_uniq(list) - return uniq(a:list) - endfunction -else - function! s:_uniq(list) - 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 - -function! s:_concat(lists) - let result_list = [] - for list in a:lists - let result_list += list - endfor - return result_list -endfunction - -function! s:_redir(cmd) - let [save_verbose, save_verbosefile] = [&verbose, &verbosefile] - set verbose=0 verbosefile= - redir => res - silent! execute a:cmd - redir END - let [&verbose, &verbosefile] = [save_verbose, save_verbosefile] - return res -endfunction - -function! vital#{s:self_version}#new() - return s:_import('') -endfunction - -let s:self_file = s:_unify_path(expand('')) diff --git a/autoload/vital/_autodirmake/Prelude.vim b/autoload/vital/_autodirmake/Prelude.vim index 0738810..c72bc7e 100644 --- a/autoload/vital/_autodirmake/Prelude.vim +++ b/autoload/vital/_autodirmake/Prelude.vim @@ -1,21 +1,21 @@ +" ___vital___ +" NOTE: lines between '" ___vital___' is generated by :Vitalize. +" Do not modify the code nor insert new lines before '" ___vital___' +function! s:_SID() abort + return matchstr(expand(''), '\zs\d\+\ze__SID$') +endfunction +execute join(['function! vital#_autodirmake#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_infinity': '', 'is_mac': '', 'strwidthpart': '', 'getchar': '', 'is_unix': '', 'is_windows': '', 'globpath': '', 'is_float': '', 'smart_execute_command': ''}, \"vital#_autodirmake#function('%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n") +delfunction s:_SID +" ___vital___ let s:save_cpo = &cpo set cpo&vim -if v:version ># 703 || -\ (v:version is 703 && has('patch465')) - function! s:glob(expr) - return glob(a:expr, 1, 1) - endfunction -else - function! s:glob(expr) - let R = glob(a:expr, 1) - return split(R, '\n') - endfunction -endif +function! s:glob(expr) abort + return glob(a:expr, 1, 1) +endfunction -function! s:globpath(path, expr) - let R = globpath(a:path, a:expr, 1) - return split(R, '\n') +function! s:globpath(path, expr) abort + return globpath(a:path, a:expr, 1, 1) endfunction " Wrapper functions for type(). @@ -26,54 +26,73 @@ let [ \ s:__TYPE_LIST, \ s:__TYPE_DICT, \ s:__TYPE_FLOAT] = [ - \ type(3), - \ type(""), - \ type(function('tr')), - \ type([]), - \ type({}), - \ has('float') ? type(str2float('0')) : -1] -" __TYPE_FLOAT = -1 when -float -" This doesn't match to anything. + \ v:t_number, + \ v:t_string, + \ v:t_func, + \ v:t_list, + \ v:t_dict, + \ v:t_float] " Number or Float -function! s:is_numeric(Value) +function! s:is_numeric(Value) abort let _ = type(a:Value) return _ ==# s:__TYPE_NUMBER \ || _ ==# s:__TYPE_FLOAT endfunction " Number -function! s:is_number(Value) +function! s:is_number(Value) abort return type(a:Value) ==# s:__TYPE_NUMBER endfunction -" Float -function! s:is_float(Value) - return type(a:Value) ==# s:__TYPE_FLOAT -endfunction " String -function! s:is_string(Value) +function! s:is_string(Value) abort return type(a:Value) ==# s:__TYPE_STRING endfunction + " Funcref -function! s:is_funcref(Value) +function! s:is_funcref(Value) abort return type(a:Value) ==# s:__TYPE_FUNCREF endfunction + " List -function! s:is_list(Value) +function! s:is_list(Value) abort return type(a:Value) ==# s:__TYPE_LIST endfunction + " Dictionary -function! s:is_dict(Value) +function! s:is_dict(Value) abort return type(a:Value) ==# s:__TYPE_DICT endfunction -function! s:truncate_smart(str, max, footer_width, separator) - echoerr 'Prelude.truncate_smart() is obsolete. Use its truncate_skipping() instead; they are equivalent.' - return s:truncate_skipping(a:str, a:max, a:footer_width, a:separator) +" Float +function! s:is_float(Value) abort + return type(a:Value) ==# s:__TYPE_FLOAT endfunction -function! s:truncate_skipping(str, max, footer_width, separator) +" Infinity +if exists('*isinf') + function! s:is_infinity(Value) abort + return isinf(a:Value) + endfunction +else + function! s:is_infinity(Value) abort + if type(a:Value) ==# s:__TYPE_FLOAT + let s = string(a:Value) + if s ==# 'inf' + return 1 + elseif s ==# '-inf' + return -1 + endif + endif + return 0 + endfunction +endif + + +function! s:truncate_skipping(str, max, footer_width, separator) abort + call s:_warn_deprecated('truncate_skipping', 'Data.String.truncate_skipping') + let width = s:wcswidth(a:str) if width <= a:max let ret = a:str @@ -86,10 +105,12 @@ function! s:truncate_skipping(str, max, footer_width, separator) return s:truncate(ret, a:max) endfunction -function! s:truncate(str, width) +function! s:truncate(str, width) abort " Original function is from mattn. " http://github.com/mattn/googlereader-vim/tree/master + call s:_warn_deprecated('truncate', 'Data.String.truncate') + if a:str =~# '^[\x00-\x7f]*$' return len(a:str) < a:width ? \ printf('%-'.a:width.'s', a:str) : strpart(a:str, 0, a:width) @@ -109,7 +130,9 @@ function! s:truncate(str, width) return ret endfunction -function! s:strwidthpart(str, width) +function! s:strwidthpart(str, width) abort + call s:_warn_deprecated('strwidthpart', 'Data.String.strwidthpart') + if a:width <= 0 return '' endif @@ -123,7 +146,9 @@ function! s:strwidthpart(str, width) return ret endfunction -function! s:strwidthpart_reverse(str, width) +function! s:strwidthpart_reverse(str, width) abort + call s:_warn_deprecated('strwidthpart_reverse', 'Data.String.strwidthpart_reverse') + if a:width <= 0 return '' endif @@ -138,147 +163,102 @@ function! s:strwidthpart_reverse(str, width) return ret endfunction -if v:version >= 703 - " Use builtin function. - function! s:wcswidth(str) - return strwidth(a:str) - endfunction -else - function! s:wcswidth(str) - 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 - endfunction - - " UTF-8 only. - function! s:_wcwidth(ucs) - 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 - endfunction -endif +function! s:wcswidth(str) abort + call s:_warn_deprecated('wcswidth', 'Data.String.wcswidth') + return strwidth(a:str) +endfunction -let s:is_windows = has('win16') || has('win32') || has('win64') || has('win95') +let s:is_windows = has('win32') " This means any versions of windows https://github.com/vim-jp/vital.vim/wiki/Coding-Rule#how-to-check-if-the-runtime-os-is-windows let s:is_cygwin = has('win32unix') let s:is_mac = !s:is_windows && !s:is_cygwin \ && (has('mac') || has('macunix') || has('gui_macvim') || \ (!isdirectory('/proc') && executable('sw_vers'))) let s:is_unix = has('unix') -function! s:is_windows() +function! s:is_windows() abort return s:is_windows endfunction -function! s:is_cygwin() +function! s:is_cygwin() abort return s:is_cygwin endfunction -function! s:is_mac() +function! s:is_mac() abort return s:is_mac endfunction -function! s:is_unix() +function! s:is_unix() abort return s:is_unix endfunction -function! s:_deprecated2(fname) - echomsg printf("Vital.Prelude.%s is deprecated!", - \ a:fname) +function! s:_warn_deprecated(name, alternative) abort + try + echohl Error + echomsg 'Prelude.' . a:name . ' is deprecated! Please use ' . a:alternative . ' instead.' + finally + echohl None + endtry endfunction -function! s:smart_execute_command(action, word) - execute a:action . ' ' . (a:word == '' ? '' : '`=a:word`') +function! s:smart_execute_command(action, word) abort + execute a:action . ' ' . (a:word ==# '' ? '' : '`=a:word`') endfunction -function! s:escape_file_searching(buffer_name) +function! s:_escape_file_searching(buffer_name) abort return escape(a:buffer_name, '*[]?{}, ') endfunction -function! s:escape_pattern(str) +function! s:escape_pattern(str) abort + call s:_warn_deprecated( + \ 'escape_pattern', + \ 'Data.String.escape_pattern', + \) return escape(a:str, '~"\.^$[]*') endfunction -function! s:getchar(...) +function! s:getchar(...) abort let c = call('getchar', a:000) return type(c) == type(0) ? nr2char(c) : c endfunction -function! s:getchar_safe(...) +function! s:getchar_safe(...) abort let c = s:input_helper('getchar', a:000) - return type(c) == type("") ? c : nr2char(c) + return type(c) == type('') ? c : nr2char(c) endfunction -function! s:input_safe(...) +function! s:input_safe(...) abort return s:input_helper('input', a:000) endfunction -function! s:input_helper(funcname, args) +function! s:input_helper(funcname, args) abort let success = 0 if inputsave() !=# success - throw 'inputsave() failed' + throw 'vital: Prelude: inputsave() failed' endif try return call(a:funcname, a:args) finally if inputrestore() !=# success - throw 'inputrestore() failed' + throw 'vital: Prelude: inputrestore() failed' endif endtry endfunction -function! s:set_default(var, val) +function! s:set_default(var, val) abort if !exists(a:var) || type({a:var}) != type(a:val) let {a:var} = a:val endif endfunction -function! s:set_dictionary_helper(variable, keys, pattern) - call s:_deprecated2('set_dictionary_helper') - - for key in split(a:keys, '\s*,\s*') - if !has_key(a:variable, key) - let a:variable[key] = a:pattern - endif - endfor -endfunction - -function! s:substitute_path_separator(path) +function! s:substitute_path_separator(path) abort return s:is_windows ? substitute(a:path, '\\', '/', 'g') : a:path endfunction -function! s:path2directory(path) +function! s:path2directory(path) abort return s:substitute_path_separator(isdirectory(a:path) ? a:path : fnamemodify(a:path, ':p:h')) endfunction -function! s:_path2project_directory_git(path) +function! s:_path2project_directory_git(path) abort let parent = a:path while 1 @@ -294,13 +274,13 @@ function! s:_path2project_directory_git(path) endwhile endfunction -function! s:_path2project_directory_svn(path) +function! s:_path2project_directory_svn(path) abort let search_directory = a:path let directory = '' - let find_directory = s:escape_file_searching(search_directory) + let find_directory = s:_escape_file_searching(search_directory) let d = finddir('.svn', find_directory . ';') - if d == '' + if d ==# '' return '' endif @@ -310,28 +290,28 @@ function! s:_path2project_directory_svn(path) let parent_directory = s:path2directory( \ fnamemodify(directory, ':h')) - if parent_directory != '' + if parent_directory !=# '' let d = finddir('.svn', parent_directory . ';') - if d != '' + if d !=# '' let directory = s:_path2project_directory_svn(parent_directory) endif endif return directory endfunction -function! s:_path2project_directory_others(vcs, path) +function! s:_path2project_directory_others(vcs, path) abort let vcs = a:vcs let search_directory = a:path - let find_directory = s:escape_file_searching(search_directory) + let find_directory = s:_escape_file_searching(search_directory) let d = finddir(vcs, find_directory . ';') - if d == '' + if d ==# '' return '' endif return fnamemodify(d, ':p:h:h') endfunction -function! s:path2project_directory(path, ...) +function! s:path2project_directory(path, ...) abort let is_allow_empty = get(a:000, 0, 0) let search_directory = s:path2directory(a:path) let directory = '' @@ -345,25 +325,25 @@ function! s:path2project_directory(path, ...) else let directory = s:_path2project_directory_others(vcs, search_directory) endif - if directory != '' + if directory !=# '' break endif endfor " Search project file. - if directory == '' + if directory ==# '' for d in ['build.xml', 'prj.el', '.project', 'pom.xml', 'package.json', \ 'Makefile', 'configure', 'Rakefile', 'NAnt.build', \ 'P4CONFIG', 'tags', 'gtags'] - let d = findfile(d, s:escape_file_searching(search_directory) . ';') - if d != '' + let d = findfile(d, s:_escape_file_searching(search_directory) . ';') + if d !=# '' let directory = fnamemodify(d, ':p:h') break endif endfor endif - if directory == '' + if directory ==# '' " Search /src/ directory. let base = s:substitute_path_separator(search_directory) if base =~# '/src/' @@ -371,7 +351,7 @@ function! s:path2project_directory(path, ...) endif endif - if directory == '' && !is_allow_empty + if directory ==# '' && !is_allow_empty " Use original path. let directory = search_directory endif diff --git a/autoload/vital/autodirmake.vim b/autoload/vital/autodirmake.vim new file mode 100644 index 0000000..72d1dc9 --- /dev/null +++ b/autoload/vital/autodirmake.vim @@ -0,0 +1,329 @@ +let s:plugin_name = expand(':t:r') +let s:vital_base_dir = expand(':h') +let s:project_root = expand(':h:h:h') +let s:is_vital_vim = s:plugin_name is# 'vital' + +let s:loaded = {} +let s:cache_sid = {} + +function! vital#{s:plugin_name}#new() abort + return s:new(s:plugin_name) +endfunction + +function! vital#{s:plugin_name}#import(...) abort + if !exists('s:V') + let s:V = s:new(s:plugin_name) + endif + return call(s:V.import, a:000, s:V) +endfunction + +let s:Vital = {} + +function! s:new(plugin_name) abort + let base = deepcopy(s:Vital) + let base._plugin_name = a:plugin_name + return base +endfunction + +function! s:vital_files() abort + if !exists('s:vital_files') + let s:vital_files = map( + \ s:is_vital_vim ? s:_global_vital_files() : s:_self_vital_files(), + \ 'fnamemodify(v:val, ":p:gs?[\\\\/]?/?")') + endif + return copy(s:vital_files) +endfunction +let s:Vital.vital_files = function('s:vital_files') + +function! s:import(name, ...) abort dict + let target = {} + let functions = [] + for a in a:000 + if type(a) == type({}) + let target = a + elseif type(a) == type([]) + let functions = a + endif + unlet a + endfor + let module = self._import(a:name) + 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 +endfunction +let s:Vital.import = function('s:import') + +function! s:load(...) abort 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 + let dict_type = type({}) + while !empty(target) + let ns = remove(target, 0) + if !has_key(dict, ns) + let dict[ns] = {} + endif + if type(dict[ns]) == dict_type + let dict = dict[ns] + else + unlet dict + break + endif + endwhile + if exists('dict') + call extend(dict, self._import(name)) + endif + unlet arg + endfor + return self +endfunction +let s:Vital.load = function('s:load') + +function! s:unload() abort dict + let s:loaded = {} + let s:cache_sid = {} + unlet! s:vital_files +endfunction +let s:Vital.unload = function('s:unload') + +function! s:exists(name) abort dict + if a:name !~# '\v^\u\w*%(\.\u\w*)*$' + throw 'vital: Invalid module name: ' . a:name + endif + return s:_module_path(a:name) isnot# '' +endfunction +let s:Vital.exists = function('s:exists') + +function! s:search(pattern) abort dict + let paths = s:_extract_files(a:pattern, self.vital_files()) + let modules = sort(map(paths, 's:_file2module(v:val)')) + return uniq(modules) +endfunction +let s:Vital.search = function('s:search') + +function! s:plugin_name() abort dict + return self._plugin_name +endfunction +let s:Vital.plugin_name = function('s:plugin_name') + +function! s:_self_vital_files() abort + let builtin = printf('%s/__%s__/', s:vital_base_dir, s:plugin_name) + let installed = printf('%s/_%s/', s:vital_base_dir, s:plugin_name) + let base = builtin . ',' . installed + return split(globpath(base, '**/*.vim', 1), "\n") +endfunction + +function! s:_global_vital_files() abort + let pattern = 'autoload/vital/__*__/**/*.vim' + return split(globpath(&runtimepath, pattern, 1), "\n") +endfunction + +function! s:_extract_files(pattern, files) abort + let tr = {'.': '/', '*': '[^/]*', '**': '.*'} + let target = substitute(a:pattern, '\.\|\*\*\?', '\=tr[submatch(0)]', 'g') + let regexp = printf('autoload/vital/[^/]\+/%s.vim$', target) + return filter(a:files, 'v:val =~# regexp') +endfunction + +function! s:_file2module(file) abort + let filename = fnamemodify(a:file, ':p:gs?[\\/]?/?') + let tail = matchstr(filename, 'autoload/vital/_\w\+/\zs.*\ze\.vim$') + return join(split(tail, '[\\/]\+'), '.') +endfunction + +" @param {string} name e.g. Data.List +function! s:_import(name) abort dict + if has_key(s:loaded, a:name) + return copy(s:loaded[a:name]) + endif + let module = self._get_module(a:name) + if has_key(module, '_vital_created') + call module._vital_created(module) + endif + let export_module = filter(copy(module), 'v:key =~# "^\\a"') + " Cache module before calling module.vital_loaded() to avoid cyclic + " dependences but remove the cache if module._vital_loaded() fails. + " let s:loaded[a:name] = export_module + let s:loaded[a:name] = export_module + if has_key(module, '_vital_loaded') + try + call module._vital_loaded(vital#{s:plugin_name}#new()) + catch + unlet s:loaded[a:name] + throw 'vital: fail to call ._vital_loaded(): ' . v:exception . " from:\n" . s:_format_throwpoint(v:throwpoint) + endtry + endif + return copy(s:loaded[a:name]) +endfunction +let s:Vital._import = function('s:_import') + +function! s:_format_throwpoint(throwpoint) abort + let funcs = [] + let stack = matchstr(a:throwpoint, '^function \zs.*\ze, line \d\+$') + for line in split(stack, '\.\.') + let m = matchlist(line, '^\%(\(\d\+\)_\)\?\(.\+\)\[\(\d\+\)\]$') + if empty(m) + call add(funcs, line) + continue + endif + let [sid, name, lnum] = m[1:3] + let attr = '' + if !empty(sid) + let name = printf('%d_%s', sid, name) + endif + let file = s:_get_file_by_func_name(name) + call add(funcs, printf('function %s(...)%s Line:%d (%s)', name, attr, lnum, file)) + endfor + return join(funcs, "\n") +endfunction + +function! s:_get_file_by_func_name(name) abort + try + redir => body + silent execute 'verbose function' a:name + finally + redir END + endtry + let lines = split(body, "\n") + let signature = matchstr(lines[0], '^\s*\zs.*') + let file = matchstr(lines[1], '^\t\%(Last set from\|.\{-}:\)\s*\zs.*$') + return substitute(file, '[/\\]\+', '/', 'g') +endfunction + +" s:_get_module() returns module object wihch has all script local functions. +function! s:_get_module(name) abort dict + let funcname = s:_import_func_name(self.plugin_name(), a:name) + try + return call(funcname, []) + catch /^Vim\%((\a\+)\)\?:E117:/ + return s:_get_builtin_module(a:name) + endtry +endfunction + +function! s:_get_builtin_module(name) abort + return s:sid2sfuncs(s:_module_sid(a:name)) +endfunction + +if s:is_vital_vim + " For vital.vim, we can use s:_get_builtin_module directly + let s:Vital._get_module = function('s:_get_builtin_module') +else + let s:Vital._get_module = function('s:_get_module') +endif + +function! s:_import_func_name(plugin_name, module_name) abort + return printf('vital#_%s#%s#import', a:plugin_name, s:_dot_to_sharp(a:module_name)) +endfunction + +function! s:_module_sid(name) abort + let path = s:_module_path(a:name) + if !filereadable(path) + throw 'vital: module not found: ' . a:name + endif + let vital_dir = s:is_vital_vim ? '__\w\+__' : printf('_\{1,2}%s\%%(__\)\?', s:plugin_name) + let base = join([vital_dir, ''], '[/\\]\+') + let p = base . substitute('' . a:name, '\.', '[/\\\\]\\+', 'g') + let sid = s:_sid(path, p) + if !sid + call s:_source(path) + let sid = s:_sid(path, p) + if !sid + throw printf('vital: cannot get from path: %s', path) + endif + endif + return sid +endfunction + +function! s:_module_path(name) abort + return get(s:_extract_files(a:name, s:vital_files()), 0, '') +endfunction + +function! s:_module_sid_base_dir() abort + return s:is_vital_vim ? &rtp : s:project_root +endfunction + +function! s:_dot_to_sharp(name) abort + return substitute(a:name, '\.', '#', 'g') +endfunction + +function! s:_source(path) abort + execute 'source' fnameescape(a:path) +endfunction + +" @vimlint(EVL102, 1, l:_) +" @vimlint(EVL102, 1, l:__) +function! s:_sid(path, filter_pattern) abort + let unified_path = s:_unify_path(a:path) + if has_key(s:cache_sid, unified_path) + return s:cache_sid[unified_path] + endif + for line in filter(split(s:_execute(':scriptnames'), "\n"), 'v:val =~# a:filter_pattern') + let [_, sid, path; __] = matchlist(line, '^\s*\(\d\+\):\s\+\(.\+\)\s*$') + if s:_unify_path(path) is# unified_path + let s:cache_sid[unified_path] = sid + return s:cache_sid[unified_path] + endif + endfor + return 0 +endfunction + +" We want to use an execute() builtin function instead of s:_execute(), +" however there is a bug in execute(). +" execute() returns empty string when it is called in +" completion function of user defined ex command. +" https://github.com/vim-jp/issues/issues/1129 +function! s:_execute(cmd) abort + let [save_verbose, save_verbosefile] = [&verbose, &verbosefile] + set verbose=0 verbosefile= + redir => res + silent! execute a:cmd + redir END + let [&verbose, &verbosefile] = [save_verbose, save_verbosefile] + return res +endfunction + +if filereadable(expand(':r') . '.VIM') " is case-insensitive or not + let s:_unify_path_cache = {} + " resolve() is slow, so we cache results. + " Note: On windows, vim can't expand path names from 8.3 formats. + " So if getting full path via 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 + endfunction +else + function! s:_unify_path(path) abort + return resolve(fnamemodify(a:path, ':p:gs?[\\/]?/?')) + endfunction +endif + +" copied and modified from Vim.ScriptLocal +let s:SNR = join(map(range(len("\")), '"[\\x" . printf("%0x", char2nr("\"[v:val])) . "]"'), '') +function! s:sid2sfuncs(sid) abort + let fs = split(s:_execute(printf(':function /^%s%s_', s:SNR, a:sid)), "\n") + let r = {} + let pattern = printf('\m^function\s%d_\zs\w\{-}\ze(', a:sid) + for fname in map(fs, 'matchstr(v:val, pattern)') + let r[fname] = function(s:_sfuncname(a:sid, fname)) + endfor + return r +endfunction + +"" Return funcname of script local functions with SID +function! s:_sfuncname(sid, funcname) abort + return printf('%s_%s', a:sid, a:funcname) +endfunction diff --git a/autoload/vital/autodirmake.vital b/autoload/vital/autodirmake.vital index 98133c3..866bd81 100644 --- a/autoload/vital/autodirmake.vital +++ b/autoload/vital/autodirmake.vital @@ -1,4 +1,4 @@ autodirmake -a9aa86b +b8641ed90604ae5c36273b1e4c24866a24aa3c25 Prelude