Permalink
Browse files

- Fixed vital bug.

  • Loading branch information...
1 parent 782db4f commit 140f53d5dee82cf2b2413473f1f5c6fb075cd518 @Shougo Shougo committed Jun 27, 2011
@@ -1,37 +0,0 @@
-" Utilities for cmdline.
-
-let s:save_cpo = &cpo
-set cpo&vim
-
-
-" getchar() wrapper.
-" do inputsave()/inputrestore() before/after getchar().
-" and always returns String.
-function! s:getchar(...) "{{{
- let c = s:input_helper('getchar', a:000)
- return type(c) == type("") ? c : nr2char(c)
-endfunction "}}}
-
-" input() wrapper.
-" do inputsave()/inputrestore() before/after input().
-function! s:input(...) "{{{
- 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
- if inputsave() !=# success
- throw 'inputsave() failed'
- endif
- try
- return call(a:funcname, a:args)
- finally
- if inputrestore() !=# success
- throw 'inputrestore() failed'
- endif
- endtry
-endfunction
-
-
-let &cpo = s:save_cpo
@@ -45,17 +45,11 @@ function! s:_import(name, scripts)
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 pat = substitute(s:base_dir . target, '[/\\]', '[/\\\\]', 'g') . '\.vim$'
- let sid = 0
- for script in a:scripts
- if script =~? pat
- let sid = matchstr(script, '^\s*\zs\d\+') - 0
- break
- endif
- endfor
+ let target = s:base_dir . target . '.vim'
+ let sid = get(a:scripts, s:_unify_path(target), 0)
if !sid
try
- source `=s:base_dir . target . '.vim'`
+ source `=target`
catch /^Vim\%((\a\+)\)\?:E484/
throw 'vital: module not found: ' . a:name
endtry
@@ -65,7 +59,18 @@ function! s:_import(name, scripts)
endfunction
function! s:_scripts()
- return split(s:_redir('scriptnames'), "\n")
+ let scripts = {}
+ for line in split(s:_redir('scriptnames'), "\n")
+ 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
+
+function! s:_unify_path(path)
+ return fnamemodify(resolve(a:path), ':p:gs?\\\+?/?')
endfunction
function! s:_build_module(sid)
@@ -76,6 +76,12 @@ function! s:char_range(from, to) " {{{
\)
endfunction "}}}
+" Returns true if a:list has a:Value.
+" Returns false otherwise.
+function! s:has(list, Value) "{{{
+ return index(a:list, a:Value) isnot -1
+endfunction "}}}
+
" Returns true if a:list[a:index] exists.
" Returns false otherwise.
" NOTE: Returns false when a:index is negative number.
@@ -56,6 +56,8 @@ function! s:split_leftright(haystack, needle)
endfunction
" Returns the number of character in a:str.
+" NOTE: This returns proper value
+" even if a:str contains multibyte character(s).
" s:strchars(str) {{{
if exists('*strchars')
" TODO: Why can't I write like this?
@@ -70,19 +72,21 @@ else
endif "}}}
" Remove last character from a:str.
+" NOTE: This returns proper value
+" even if a:str contains multibyte character(s).
function! s:chop(str) "{{{
return substitute(a:str, '.$', '', '')
endfunction "}}}
-" iconv() wrapper.
-" Returns a:expr for error. not empty string.
-function! s:iconv(expr, from, to) "{{{
- if a:from == '' || a:to == '' || a:from ==? a:to
- return a:expr
- endif
- let result = iconv(a:expr, a:from, a:to)
- return result != '' ? result : a:expr
-endfunction "}}}
+" iconv() wrapper for safety.
+" NOTE: This returns a:expr when the conversion fails.
+function! s:iconv(expr, from, to)
+ if a:from == '' || a:to == '' || a:from ==? a:to
+ return a:expr
+ endif
+ let result = iconv(a:expr, a:from, a:to)
+ return result != '' ? result : a:expr
+endfunction
let &cpo = s:save_cpo
@@ -4,18 +4,25 @@ let s:save_cpo = &cpo
set cpo&vim
+" [Callable Object] is one of the following values:
+" - function name (String)
+" - Funcref value
+" - callable object
+"
+" [Functor] is a Dictionary which has the key "do" of Funcref value.
+" Please note that `Functor.wrap([Callable Object]).do` is always Funcref value.
+" So you can always call .do() method without checking return value of `Functor.wrap()`.
+" e.g.: `Functor.wrap("").do()`
+
+
" The same arguments as call()
-" but first argument is callable object.
+" but first argument is [Callable Object].
function! s:call(callable, args, ...)
let functor = s:wrap(a:callable)
return call(functor.do, a:args, (a:0 ? a:1 : functor))
endfunction
-" Wrap
-" - function name (String)
-" - Funcref value
-" - callable object
-" with callable object.
+" Convert [Callable Object] to [Functor].
" NOTE: `s:wrap(callable).do` must be Funcref value.
let s:TYPE_STRING = type("")
let s:TYPE_FUNCREF = type(function('tr'))
@@ -1,3 +1,65 @@
+
+" glob() wrapper which returns List.
+function! s:glob(...)
+ let R = call('glob', a:000)
+ return split(R, '\n')
+endfunction
+" globpath() wrapper which returns List.
+function! s:globpath(...)
+ let R = call('globpath', a:000)
+ return split(R, '\n')
+endfunction
+
+" Wrapper functions for type().
+let [
+\ s:__TYPE_NUMBER,
+\ s:__TYPE_STRING,
+\ s:__TYPE_FUNCREF,
+\ s:__TYPE_LIST,
+\ s:__TYPE_DICT,
+\ s:__TYPE_FLOAT
+\] = [
+\ type(3),
+\ type(""),
+\ type(function('tr')),
+\ type([]),
+\ type({}),
+\ type(3.14159)
+\]
+" Number or Float
+function! s:is_numeric(Value)
+ let _ = type(a:Value)
+ return _ ==# s:__TYPE_NUMBER
+ \ || _ ==# s:__TYPE_FLOAT
+endfunction
+" Number
+function! s:is_integer(Value)
+ return type(a:Value) ==# s:__TYPE_NUMBER
+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
+endfunction
+" String
+function! s:is_string(Value)
+ return type(a:Value) ==# s:__TYPE_STRING
+endfunction
+" Funcref
+function! s:is_funcref(Value)
+ return type(a:Value) ==# s:__TYPE_FUNCREF
+endfunction
+" List
+function! s:is_list(Value)
+ return type(a:Value) ==# s:__TYPE_LIST
+endfunction
+" Dictionary
+function! s:is_dict(Value)
+ return type(a:Value) ==# s:__TYPE_DICT
+endfunction
+
function! s:truncate_smart(str, max, footer_width, separator)"{{{
let width = s:wcswidth(a:str)
if width <= a:max
@@ -80,14 +142,14 @@ else
if ucs == 0
break
endif
- let width += s:wcwidth(ucs)
+ let width += s:_wcwidth(ucs)
let str = substitute(str, mx_first, '', '')
endwhile
return width
endfunction"}}}
" UTF-8 only.
- function! s:wcwidth(ucs)"{{{
+ function! s:_wcwidth(ucs)"{{{
let ucs = a:ucs
if (ucs >= 0x1100
\ && (ucs <= 0x115f
@@ -136,19 +198,36 @@ 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
- return a:expr
- endif
- 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
+ if inputsave() !=# success
+ throw 'inputsave() failed'
+ endif
+ try
+ return call(a:funcname, a:args)
+ finally
+ if inputrestore() !=# success
+ throw 'inputrestore() failed'
+ endif
+ endtry
+endfunction
function! s:set_default(var, val) "{{{
if !exists(a:var) || type({a:var}) != type(a:val)
@@ -6,7 +6,7 @@
let s:save_cpo = &cpo
set cpo&vim
-let s:path_sep_pattern = exists('+shellslash') ? '[\\/]' : '/'
+let s:path_sep_pattern = (exists('+shellslash') ? '[\\/]' : '/') . '\+'
" Get the path separator.
function! s:separator()
@@ -32,15 +32,12 @@ function! s:join(...)
let sep = s:separator()
let path = ''
for part in a:000
- if type(part) is type([])
- let path .= sep . call('s:join', part)
- else
- let path = substitute(path, s:path_sep_pattern . '$', '', '') . sep .
- \ substitute(part, '^' . s:path_sep_pattern, '', '')
- endif
+ let path .= sep .
+ \ (type(part) is type([]) ? call('s:join', part) :
+ \ part)
unlet part
endfor
- return path[1 :] " Remove an extra pass separator of the head.
+ return substitute(path[1 :], s:path_sep_pattern, sep, 'g')
endfunction
" Check if the path is absolute path.
@@ -1 +1 @@
-217a9e
+c980ec

0 comments on commit 140f53d

Please sign in to comment.