- Updated vital.

This commit is contained in:
Shougo Matsushita 2013-10-01 06:38:24 +09:00
parent 4c67a1b1ac
commit 382d86577d
3 changed files with 78 additions and 35 deletions

View File

@ -1,5 +1,8 @@
let s:self_version = expand('<sfile>:t:r') let s:self_version = expand('<sfile>: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 = {} let s:loaded = {}
function! s:import(name, ...) function! s:import(name, ...)
@ -13,7 +16,7 @@ function! s:import(name, ...)
endif endif
unlet a unlet a
endfor endfor
let module = s:_import(a:name, s:_scripts()) let module = s:_import(a:name)
if empty(functions) if empty(functions)
call extend(target, module, 'keep') call extend(target, module, 'keep')
else else
@ -27,7 +30,6 @@ function! s:import(name, ...)
endfunction endfunction
function! s:load(...) dict function! s:load(...) dict
let scripts = s:_scripts()
for arg in a:000 for arg in a:000
let [name; as] = type(arg) == type([]) ? arg[: 1] : [arg, arg] let [name; as] = type(arg) == type([]) ? arg[: 1] : [arg, arg]
let target = split(join(as, ''), '\W\+') let target = split(join(as, ''), '\W\+')
@ -46,7 +48,7 @@ function! s:load(...) dict
endwhile endwhile
if exists('dict') if exists('dict')
call extend(dict, s:_import(name, scripts)) call extend(dict, s:_import(name))
endif endif
unlet arg unlet arg
endfor endfor
@ -57,7 +59,7 @@ function! s:unload()
let s:loaded = {} let s:loaded = {}
endfunction endfunction
function! s:_import(name, scripts) function! s:_import(name)
if type(a:name) == type(0) if type(a:name) == type(0)
return s:_build_module(a:name) return s:_build_module(a:name)
endif endif
@ -65,7 +67,7 @@ function! s:_import(name, scripts)
if path ==# '' if path ==# ''
throw 'vital: module not found: ' . a:name throw 'vital: module not found: ' . a:name
endif endif
let sid = get(a:scripts, path, 0) let sid = get(s:_scripts(), path, 0)
if !sid if !sid
try try
execute 'source' fnameescape(path) execute 'source' fnameescape(path)
@ -75,8 +77,7 @@ function! s:_import(name, scripts)
" Ignore. " Ignore.
endtry endtry
let sid = len(a:scripts) + 1 " We expect that the file newly read is +1. let sid = s:_scripts()[path]
let a:scripts[path] = sid
endif endif
return s:_build_module(sid) return s:_build_module(sid)
endfunction endfunction
@ -91,11 +92,10 @@ function! s:_get_module_path(name)
let target = '/' . substitute(a:name, '\W\+', '/', 'g') let target = '/' . substitute(a:name, '\W\+', '/', 'g')
let tailpath = printf('autoload/vital/%s%s.vim', s:self_version, target) let tailpath = printf('autoload/vital/%s%s.vim', s:self_version, target)
else else
let tailpath = a:name throw 'vital: Invalid module name: ' . a:name
endif endif
" Note: The extra argument to globpath() was added in Patch 7.2.051. if s:globpath_third_arg
if v:version > 702 || v:version == 702 && has('patch51')
let paths = split(globpath(&runtimepath, tailpath, 1), "\n") let paths = split(globpath(&runtimepath, tailpath, 1), "\n")
else else
let paths = split(globpath(&runtimepath, tailpath), "\n") let paths = split(globpath(&runtimepath, tailpath), "\n")
@ -106,7 +106,8 @@ endfunction
function! s:_scripts() function! s:_scripts()
let scripts = {} let scripts = {}
for line in split(s:_redir('scriptnames'), "\n") for line in filter(split(s:_redir('scriptnames'), "\n"),
\ 'stridx(v:val, s:self_version) > 0')
let list = matchlist(line, '^\s*\(\d\+\):\s\+\(.\+\)\s*$') let list = matchlist(line, '^\s*\(\d\+\):\s\+\(.\+\)\s*$')
if !empty(list) if !empty(list)
let scripts[s:_unify_path(list[2])] = list[1] - 0 let scripts[s:_unify_path(list[2])] = list[1] - 0
@ -145,11 +146,9 @@ function! s:_build_module(sid)
return copy(s:loaded[a:sid]) return copy(s:loaded[a:sid])
endif endif
let prefix = '<SNR>' . a:sid . '_' let prefix = '<SNR>' . a:sid . '_'
let funcs = s:_redir('function') let funcs = s:_redir("function /^\<SNR>" . a:sid . '_')
let filter_pat = '^\s*function ' . prefix
let map_pat = prefix . '\zs\w\+' let map_pat = prefix . '\zs\w\+'
let functions = map(filter(split(funcs, "\n"), 'v:val =~# filter_pat'), let functions = map(split(funcs, "\n"), 'matchstr(v:val, map_pat)')
\ 'matchstr(v:val, map_pat)')
let module = {} let module = {}
for func in functions for func in functions
@ -184,5 +183,5 @@ function! s:_redir(cmd)
endfunction endfunction
function! vital#{s:self_version}#new() function! vital#{s:self_version}#new()
return s:_import('', s:_scripts()).load(['Prelude', '']) return s:_import('').load(['Prelude', ''])
endfunction endfunction

View File

@ -305,6 +305,60 @@ function! s:path2directory(path)
return s:substitute_path_separator(isdirectory(a:path) ? a:path : fnamemodify(a:path, ':p:h')) return s:substitute_path_separator(isdirectory(a:path) ? a:path : fnamemodify(a:path, ':p:h'))
endfunction endfunction
function! s:_path2project_directory_git(path)
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
function! s:_path2project_directory_svn(path)
let search_directory = a:path
let directory = ''
let find_directory = s:escape_file_searching(search_directory)
let d = finddir('.svn', find_directory . ';')
if d == ''
return ''
endif
let directory = fnamemodify(d, ':p:h:h')
" Search parent directories.
let parent_directory = s:path2directory(
\ fnamemodify(directory, ':h'))
if parent_directory != ''
let d = finddir('.svn', parent_directory . ';')
if d != ''
let directory = s:_path2project_directory_svn(parent_directory)
endif
endif
return directory
endfunction
function! s:_path2project_directory_others(vcs, path)
let vcs = a:vcs
let search_directory = a:path
let directory = ''
let find_directory = s:escape_file_searching(search_directory)
let d = finddir(vcs, find_directory . ';')
if d == ''
return ''
endif
return fnamemodify(d, ':p:h:h')
endfunction
function! s:path2project_directory(path, ...) function! s:path2project_directory(path, ...)
let is_allow_empty = get(a:000, 0, 0) let is_allow_empty = get(a:000, 0, 0)
let search_directory = s:path2directory(a:path) let search_directory = s:path2directory(a:path)
@ -312,25 +366,15 @@ function! s:path2project_directory(path, ...)
" Search VCS directory. " Search VCS directory.
for vcs in ['.git', '.bzr', '.hg', '.svn'] for vcs in ['.git', '.bzr', '.hg', '.svn']
let find_directory = s:escape_file_searching(search_directory) if vcs ==# '.git'
let d = finddir(vcs, find_directory . ';') let directory = s:_path2project_directory_git(search_directory)
if d == '' elseif vcs ==# '.svn'
continue let directory = s:_path2project_directory_svn(search_directory)
endif else
let directory = s:_path2project_directory_others(vcs, search_directory)
let directory = fnamemodify(d, ':p:h:h')
if vcs ==# '.svn'
" Search parent directories.
let parent_directory = s:path2directory(
\ fnamemodify(directory, ':h'))
if parent_directory != ''
let d = finddir(vcs, parent_directory . ';')
if d != ''
let directory = s:path2project_directory(parent_directory)
endif
endif endif
if directory != ''
break
endif endif
endfor endfor

View File

@ -1,3 +1,3 @@
13055f8 821d01d
Prelude Prelude