Permalink
Browse files

- Vitalized.

  • Loading branch information...
1 parent 40c834d commit 58c9e6af77b18f3e9df1f330e07b0ab1a51b4a44 @Shougo Shougo committed Jun 21, 2011
File renamed without changes.
@@ -0,0 +1,37 @@
+" 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
@@ -5,7 +5,7 @@ set cpo&vim
" Removes duplicates from a list.
" FIXME: string only.
-function! s:uniq(list) " {{{
+function! s:uniq(list) "{{{
let i = 0
let seen = {}
while i < len(a:list)
@@ -18,54 +18,72 @@ function! s:uniq(list) " {{{
endif
endwhile
return a:list
-endfunction " }}}
+endfunction "}}}
" Concatenates a list of lists.
" XXX: Should we verify the input?
-function! s:concat(list) " {{{
+function! s:concat(list) "{{{
let list = []
- for i in a:list
- let list += i
+ for Value in a:list
+ let list += Value
endfor
return list
-endfunction " }}}
+endfunction "}}}
" Flattens a list.
-function! s:flatten(list) " {{{
+function! s:flatten(list) "{{{
let list = []
- for i in a:list
- if type(i) == type([])
- let list += s:flatten(i)
+ for Value in a:list
+ if type(Value) == type([])
+ let list += s:flatten(Value)
else
- call add(list, i)
+ call add(list, Value)
endif
- unlet! i
+ unlet! Value
endfor
return list
-endfunction " }}}
+endfunction "}}}
" Sorts a list with expression to compare each two values.
" a:a and a:b can be used in {expr}.
-function! s:sort(list, expr) " {{{2
+function! s:sort(list, expr) "{{{
if type(a:expr) == type(function('function'))
return sort(a:list, a:expr)
endif
let s:expr = a:expr
return sort(a:list, 's:_compare')
-endfunction
+endfunction "}}}
-function! s:_compare(a, b) " {{{2
+function! s:_compare(a, b) " {{{
return eval(s:expr)
-endfunction
+endfunction "}}}
" Sorts a list using a set of keys generated by mapping the values in the list
" through the given expr.
" v:val is used in {expr}
-function! s:sort_by(list, expr) " {{{2
+function! s:sort_by(list, expr) " {{{
let pairs = map(a:list, printf('[v:val, %s]', a:expr))
return map(s:sort(pairs,
\ 'a:a[1] ==# a:b[1] ? 0 : a:a[1] ># a:b[1] ? 1 : -1'), 'v:val[0]')
-endfunction
+endfunction "}}}
+
+" Returns List of character sequence between [a:from, a:to]
+" e.g.: s:char_range('a', 'c') returns ['a', 'b', 'c']
+function! s:char_range(from, to) " {{{
+ return map(
+ \ range(char2nr(a:from), char2nr(a:to)),
+ \ 'nr2char(v:val)'
+ \)
+endfunction "}}}
+
+" Returns true if a:list[a:index] exists.
+" Returns false otherwise.
+" NOTE: Returns false when a:index is negative number.
+function! s:has_index(list, index) "{{{
+ " Return true when negative index?
+ " let index = a:index >= 0 ? a:index : len(a:list) + a:index
+ return 0 <= a:index && a:index < len(a:list)
+endfunction "}}}
let &cpo = s:save_cpo
@@ -55,4 +55,34 @@ function! s:split_leftright(haystack, needle)
return [left, right]
endfunction
+" Returns the number of character in a:str.
+" s:strchars(str) {{{
+if exists('*strchars')
+ " TODO: Why can't I write like this?
+ " let s:strchars = function('strchars')
+ function! s:strchars(str)
+ return strchars(a:str)
+ endfunction
+else
+ function! s:strchars(str)
+ return strlen(substitute(copy(a:str), '.', 'x', 'g'))
+ endfunction
+endif "}}}
+
+" Remove last character from a:str.
+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 "}}}
+
+
let &cpo = s:save_cpo
@@ -0,0 +1,70 @@
+" "Callable thing" in vital.
+
+let s:save_cpo = &cpo
+set cpo&vim
+
+
+" The same arguments as call()
+" 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.
+" NOTE: `s:wrap(callable).do` must be Funcref value.
+let s:TYPE_STRING = type("")
+let s:TYPE_FUNCREF = type(function('tr'))
+let s:TYPE_DICT = type({})
+function! s:wrap(callable)
+ if type(a:callable) ==# s:TYPE_FUNCREF
+ return {'do': a:callable}
+ elseif type(a:callable) ==# s:TYPE_STRING
+ return {'do': function(a:callable)}
+ elseif type(a:callable) ==# s:TYPE_DICT
+ \ && has_key(a:callable, 'do')
+ if type(a:callable.do) ==# s:TYPE_FUNCREF
+ return a:callable
+ elseif type(a:callable.do) ==# s:TYPE_STRING
+ return extend(a:callable, {
+ \ 'do': function(a:callable),
+ \}, 'force')
+ endif
+ endif
+ throw 'vital: Functor.wrap(): '
+ \ . 'a:callable is not callable!'
+endfunction
+
+" Bind a:this to a:callable's `self`.
+function! s:bind(callable, this)
+ let this = copy(a:this)
+ let this.do = s:wrap(a:callable).do
+ return this
+endfunction
+
+" Curry a:callable's 1st argument with a:v.
+function! s:curry(callable, v)
+ return {
+ \ 'do': s:localfunc('__curry_stub', s:__sid()),
+ \ '__functor': s:wrap(a:callable),
+ \ '__value': a:v,
+ \}
+endfunction
+function! s:__curry_stub(...) dict
+ return s:call(self.__functor, [self.__value] + a:000)
+endfunction
+function! s:__sid()
+ return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze___sid$')
+endfunction
+
+" Convert script-local function to globally callable function.
+function! s:localfunc(funcname, sid)
+ return function(printf('<SNR>%d_%s', a:sid, a:funcname))
+endfunction
+
+
+let &cpo = s:save_cpo
@@ -64,7 +64,7 @@ endfunction"}}}
if v:version >= 703
" Use builtin function.
function! s:wcswidth(str)"{{{
- return strdisplaywidth(a:str)
+ return strwidth(a:str)
endfunction"}}}
function! s:wcwidth(str)"{{{
return strwidth(a:str)
@@ -0,0 +1,87 @@
+" Utilities for file copy/move/mkdir/etc.
+
+let s:save_cpo = &cpo
+set cpo&vim
+
+
+
+" Move a file.
+" Dispatch s:move_file_exe() or s:move_file_pure().
+function! s:move_file(src, dest) "{{{
+ if executable('mv')
+ return s:move_file_exe(a:src, a:dest)
+ else
+ return s:move_file_pure(a:src, a:dest)
+ endif
+endfunction "}}}
+
+" Move a file.
+" Implemented by 'mv' executable.
+" TODO: Support non-*nix like system.
+function! s:move_file_exe(src, dest)
+ if !executable('mv') | return 0 | endif
+ silent execute '!mv' shellescape(a:src) shellescape(a:dest)
+ if v:shell_error
+ return 0
+ endif
+ return 1
+endfunction
+
+" Move a file.
+" Implemented by pure vimscript.
+function! s:move_file_pure(src, dest) "{{{
+ let copy_success = s:copy_file(a:src, a:dest)
+ let remove_success = delete(a:src) == 0
+ if copy_success && remove_success
+ return 1
+ else
+ return 0
+ endif
+endfunction "}}}
+
+" Copy a file.
+" Dispatch s:copy_file_exe() or s:copy_file_pure().
+function! s:copy_file(src, dest) "{{{
+ if executable('cp')
+ return s:copy_file_exe(a:src, a:dest)
+ else
+ return s:copy_file_pure(a:src, a:dest)
+ endif
+endfunction "}}}
+
+" Copy a file.
+" Implemented by 'cp' executable.
+" TODO: Support non-*nix like system.
+function! s:copy_file_exe(src, dest)
+ if !executable('cp') | return 0 | endif
+ silent execute '!cp' shellescape(a:src) shellescape(a:dest)
+ if v:shell_error
+ return 0
+ endif
+ return 1
+endfunction
+
+" Copy a file.
+" Implemented by pure vimscript.
+function! s:copy_file_pure(src, dest) "{{{
+ let ret = writefile(readfile(a:src, "b"), a:dest, "b")
+ if ret == -1
+ return 0
+ endif
+ return 1
+endfunction "}}}
+
+" mkdir() but does not throw an exception.
+" Returns true if success.
+" Returns false if failure.
+function! s:mkdir_nothrow(...) "{{{
+ try
+ call call('mkdir', a:000)
+ return 1
+ catch
+ return 0
+ endtry
+endfunction "}}}
+
+
+let &cpo = s:save_cpo
@@ -1 +1 @@
-b3fc32
+6190cd

0 comments on commit 58c9e6a

Please sign in to comment.