Vitalize.

This commit is contained in:
ujihisa 2013-07-06 00:46:13 -07:00
parent 39cd6d1f28
commit 157863b7cb
3 changed files with 90 additions and 39 deletions

View File

@ -61,21 +61,10 @@ function! s:_import(name, scripts)
if type(a:name) == type(0)
return s:_build_module(a:name)
endif
if a:name =~# '^[^A-Z]' || a:name =~# '\W[^A-Z]'
throw 'vital: module name must start with capital letter: ' . a:name
let path = s:_get_module_path(a:name)
if path ==# ''
throw 'vital: module not found: ' . a:name
endif
let target = a:name ==# '' ? '' : '/' . substitute(a:name, '\W\+', '/', 'g')
let target = substitute(target, '\l\zs\ze\u', '_', 'g') " OrderedSet -> Ordered_Set
let target = substitute(target, '[/_]\zs\u', '\l\0', 'g') " Ordered_Set -> ordered_set
let tailpath = printf('autoload/vital/%s%s.vim', s:self_version, target)
" Note: The extra argument to globpath() was added in Patch 7.2.051.
if v:version > 702 || v:version == 702 && has('patch51')
let paths = split(globpath(&runtimepath, tailpath, 1), "\n")
else
let paths = split(globpath(&runtimepath, tailpath), "\n")
endif
let path = s:_unify_path(get(paths, 0, ''))
let sid = get(a:scripts, path, 0)
if !sid
try
@ -92,6 +81,29 @@ function! s:_import(name, scripts)
return s:_build_module(sid)
endfunction
function! s:_get_module_path(name)
if filereadable(a:name)
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
let tailpath = a:name
endif
" Note: The extra argument to globpath() was added in Patch 7.2.051.
if v:version > 702 || v:version == 702 && has('patch51')
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
function! s:_scripts()
let scripts = {}
for line in split(s:_redir('scriptnames'), "\n")
@ -151,12 +163,12 @@ function! s:_build_module(sid)
endfunction
function! s:_redir(cmd)
let oldverbosefile = &verbosefile
set verbosefile=
let [save_verbose, save_verbosefile] = [&verbose, &verbosefile]
set verbose=0 verbosefile=
redir => res
silent! execute a:cmd
redir END
let &verbosefile = oldverbosefile
let [&verbose, &verbosefile] = [save_verbose, save_verbosefile]
return res
endfunction

View File

@ -15,6 +15,7 @@ else
return split(R, '\n')
endfunction
endif
" globpath() wrapper which returns List
" and 'suffixes' and 'wildignore' does not affect
" this function's return value.
@ -30,15 +31,13 @@ let [
\ 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
\]
\ 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.
@ -48,13 +47,17 @@ function! s:is_numeric(Value)
return _ ==# s:__TYPE_NUMBER
\ || _ ==# s:__TYPE_FLOAT
endfunction
" Number
function! s:is_integer(Value)
return type(a:Value) ==# s:__TYPE_NUMBER
echoerr 'Prelude.is_integer() is obsolete. Use its is_number() instead; they are equivalent.'
return s:is_number(a:Value)
endfunction
function! s:is_number(Value)
return type(a:Value) ==# s:__TYPE_NUMBER
endfunction
" Float
function! s:is_float(Value)
return type(a:Value) ==# s:__TYPE_FLOAT
@ -77,6 +80,11 @@ function! s:is_dict(Value)
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)
endfunction
function! s:truncate_skipping(str, max, footer_width, separator)
let width = s:wcswidth(a:str)
if width <= a:max
let ret = a:str
@ -112,10 +120,6 @@ function! s:truncate(str, width)
return ret
endfunction
function! s:strchars(str)
return len(substitute(a:str, '.', 'x', 'g'))
endfunction
function! s:strwidthpart(str, width)
if a:width <= 0
return ''
@ -193,21 +197,29 @@ else
endfunction
endif
let s:is_windows = has('win16') || has('win32') || has('win64')
let s:is_windows = has('win16') || has('win32') || has('win64') || has('win95')
let s:is_cygwin = has('win32unix')
let s:is_mac = !s:is_windows
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()
return s:is_windows
endfunction
function! s:is_cygwin()
return s:is_cygwin
endfunction
function! s:is_mac()
return s:is_mac
endfunction
function! s:is_unix()
return s:is_unix
endfunction
function! s:print_error(message)
echohl ErrorMsg
for m in split(a:message, "\n")
@ -223,9 +235,11 @@ endfunction
function! s:escape_file_searching(buffer_name)
return escape(a:buffer_name, '*[]?{}, ')
endfunction
function! s:escape_pattern(str)
return escape(a:str, '~"\.^$[]*')
endfunction
" iconv() wrapper for safety.
function! s:iconv(expr, from, to)
if a:from == '' || a:to == '' || a:from ==? a:to
@ -234,22 +248,26 @@ function! s:iconv(expr, from, to)
let result = iconv(a:expr, a:from, a:to)
return result != '' ? result : a:expr
endfunction
" Like builtin getchar() but returns string always.
function! s:getchar(...)
let c = call('getchar', a:000)
return type(c) == type(0) ? nr2char(c) : c
endfunction
" Like builtin getchar() but returns string always.
" and do inputsave()/inputrestore() before/after getchar().
function! s:getchar_safe(...)
let c = s:input_helper('getchar', a:000)
return type(c) == type("") ? c : nr2char(c)
endfunction
" Like builtin getchar() but
" do inputsave()/inputrestore() before/after input().
function! s:input_safe(...)
return s:input_helper('input', a:000)
endfunction
" Do inputsave()/inputrestore() before/after calling a:funcname.
function! s:input_helper(funcname, args)
let success = 0
@ -270,6 +288,7 @@ function! s:set_default(var, val)
let {a:var} = a:val
endif
endfunction
function! s:set_dictionary_helper(variable, keys, pattern)
for key in split(a:keys, '\s*,\s*')
if !has_key(a:variable, key)
@ -277,23 +296,41 @@ function! s:set_dictionary_helper(variable, keys, pattern)
endif
endfor
endfunction
function! s:substitute_path_separator(path)
return s:is_windows ? substitute(a:path, '\\', '/', 'g') : a:path
endfunction
function! s:path2directory(path)
return s:substitute_path_separator(isdirectory(a:path) ? a:path : fnamemodify(a:path, ':p:h'))
endfunction
function! s:path2project_directory(path, ...)
let is_allow_empty = get(a:000, 0, 0)
let search_directory = s:path2directory(a:path)
let directory = ''
" Search VCS directory.
for d in ['.git', '.bzr', '.hg']
let d = finddir(d, s:escape_file_searching(search_directory) . ';')
if d != ''
let directory = fnamemodify(d, ':p:h:h')
break
for vcs in ['.git', '.bzr', '.hg', '.svn']
let find_directory = s:escape_file_searching(search_directory)
let d = finddir(vcs, find_directory . ';')
if d == ''
continue
endif
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
endfor
@ -324,6 +361,7 @@ function! s:path2project_directory(path, ...)
return s:substitute_path_separator(directory)
endfunction
" Check vimproc.
function! s:has_vimproc()
if !exists('s:exists_vimproc')
@ -359,6 +397,7 @@ function! s:system(str, ...)
return output
endfunction
function! s:get_last_status()
return s:has_vimproc() ?
\ vimproc#get_last_status() : v:shell_error

View File

@ -1,3 +1,3 @@
08a462e
de83b96
Prelude