2012-12-25 13:58:04 +00:00
|
|
|
let s:save_cpo = &cpo
|
|
|
|
set cpo&vim
|
2012-09-30 08:06:28 +00:00
|
|
|
|
|
|
|
" glob() wrapper which returns List
|
|
|
|
" and 'wildignore' does not affect
|
|
|
|
" this function's return value.
|
|
|
|
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
|
|
|
|
" globpath() wrapper which returns List
|
|
|
|
" and 'suffixes' and 'wildignore' does not affect
|
|
|
|
" this function's return value.
|
|
|
|
function! s:globpath(path, expr)
|
|
|
|
let R = globpath(a:path, a:expr, 1)
|
|
|
|
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({}),
|
|
|
|
\ has('float') ? type(str2float('0')) : -1
|
|
|
|
\]
|
|
|
|
" __TYPE_FLOAT = -1 when -float
|
|
|
|
" This doesn't match to anything.
|
|
|
|
|
|
|
|
" 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
|
|
|
|
|
2012-12-25 13:58:04 +00:00
|
|
|
function! s:truncate_smart(str, max, footer_width, separator)
|
2012-09-30 08:06:28 +00:00
|
|
|
let width = s:wcswidth(a:str)
|
|
|
|
if width <= a:max
|
|
|
|
let ret = a:str
|
|
|
|
else
|
|
|
|
let header_width = a:max - s:wcswidth(a:separator) - a:footer_width
|
|
|
|
let ret = s:strwidthpart(a:str, header_width) . a:separator
|
|
|
|
\ . s:strwidthpart_reverse(a:str, a:footer_width)
|
|
|
|
endif
|
|
|
|
|
|
|
|
return s:truncate(ret, a:max)
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
2012-09-30 08:06:28 +00:00
|
|
|
|
2012-12-25 13:58:04 +00:00
|
|
|
function! s:truncate(str, width)
|
2012-09-30 08:06:28 +00:00
|
|
|
" Original function is from mattn.
|
|
|
|
" http://github.com/mattn/googlereader-vim/tree/master
|
|
|
|
|
|
|
|
if a:str =~# '^[\x00-\x7f]*$'
|
|
|
|
return len(a:str) < a:width ?
|
|
|
|
\ printf('%-'.a:width.'s', a:str) : strpart(a:str, 0, a:width)
|
|
|
|
endif
|
|
|
|
|
|
|
|
let ret = a:str
|
|
|
|
let width = s:wcswidth(a:str)
|
|
|
|
if width > a:width
|
|
|
|
let ret = s:strwidthpart(ret, a:width)
|
|
|
|
let width = s:wcswidth(ret)
|
|
|
|
endif
|
|
|
|
|
|
|
|
if width < a:width
|
|
|
|
let ret .= repeat(' ', a:width - width)
|
|
|
|
endif
|
|
|
|
|
|
|
|
return ret
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
2012-09-30 08:06:28 +00:00
|
|
|
|
2012-12-25 13:58:04 +00:00
|
|
|
function! s:strchars(str)
|
2012-09-30 08:06:28 +00:00
|
|
|
return len(substitute(a:str, '.', 'x', 'g'))
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
2012-09-30 08:06:28 +00:00
|
|
|
|
2012-12-25 13:58:04 +00:00
|
|
|
function! s:strwidthpart(str, width)
|
2012-09-30 08:06:28 +00:00
|
|
|
if a:width <= 0
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
let ret = a:str
|
|
|
|
let width = s:wcswidth(a:str)
|
|
|
|
while width > a:width
|
|
|
|
let char = matchstr(ret, '.$')
|
|
|
|
let ret = ret[: -1 - len(char)]
|
|
|
|
let width -= s:wcswidth(char)
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
return ret
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
|
|
|
function! s:strwidthpart_reverse(str, width)
|
2012-09-30 08:06:28 +00:00
|
|
|
if a:width <= 0
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
let ret = a:str
|
|
|
|
let width = s:wcswidth(a:str)
|
|
|
|
while width > a:width
|
|
|
|
let char = matchstr(ret, '^.')
|
|
|
|
let ret = ret[len(char) :]
|
|
|
|
let width -= s:wcswidth(char)
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
return ret
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
2012-09-30 08:06:28 +00:00
|
|
|
|
|
|
|
if v:version >= 703
|
|
|
|
" Use builtin function.
|
2012-12-25 13:58:04 +00:00
|
|
|
function! s:wcswidth(str)
|
2012-09-30 08:06:28 +00:00
|
|
|
return strwidth(a:str)
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
2012-09-30 08:06:28 +00:00
|
|
|
else
|
2012-12-25 13:58:04 +00:00
|
|
|
function! s:wcswidth(str)
|
2012-09-30 08:06:28 +00:00
|
|
|
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
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
2012-09-30 08:06:28 +00:00
|
|
|
|
|
|
|
" UTF-8 only.
|
2012-12-25 13:58:04 +00:00
|
|
|
function! s:_wcwidth(ucs)
|
2012-09-30 08:06:28 +00:00
|
|
|
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
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
2012-09-30 08:06:28 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
let s:is_windows = has('win16') || has('win32') || has('win64')
|
|
|
|
let s:is_cygwin = has('win32unix')
|
2012-12-25 13:58:04 +00:00
|
|
|
let s:is_mac = !s:is_windows
|
2012-09-30 08:06:28 +00:00
|
|
|
\ && (has('mac') || has('macunix') || has('gui_macvim') ||
|
2012-12-25 13:58:04 +00:00
|
|
|
\ (!isdirectory('/proc') && executable('sw_vers')))
|
|
|
|
function! s:is_windows()
|
2012-09-30 08:06:28 +00:00
|
|
|
return s:is_windows
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
|
|
|
function! s:is_cygwin()
|
2012-09-30 08:06:28 +00:00
|
|
|
return s:is_cygwin
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
|
|
|
function! s:is_mac()
|
2012-09-30 08:06:28 +00:00
|
|
|
return s:is_mac
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
2012-09-30 08:06:28 +00:00
|
|
|
|
2012-12-25 13:58:04 +00:00
|
|
|
function! s:print_error(message)
|
2012-09-30 08:06:28 +00:00
|
|
|
echohl ErrorMsg
|
|
|
|
for m in split(a:message, "\n")
|
|
|
|
echomsg m
|
|
|
|
endfor
|
|
|
|
echohl None
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
2012-09-30 08:06:28 +00:00
|
|
|
|
2012-12-25 13:58:04 +00:00
|
|
|
function! s:smart_execute_command(action, word)
|
2012-09-30 08:06:28 +00:00
|
|
|
execute a:action . ' ' . (a:word == '' ? '' : '`=a:word`')
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
2012-09-30 08:06:28 +00:00
|
|
|
|
2012-12-25 13:58:04 +00:00
|
|
|
function! s:escape_file_searching(buffer_name)
|
2012-09-30 08:06:28 +00:00
|
|
|
return escape(a:buffer_name, '*[]?{}, ')
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
|
|
|
function! s:escape_pattern(str)
|
2012-09-30 08:06:28 +00:00
|
|
|
return escape(a:str, '~"\.^$[]*')
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
2012-09-30 08:06:28 +00:00
|
|
|
" 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
|
|
|
|
|
2012-12-25 13:58:04 +00:00
|
|
|
function! s:set_default(var, val)
|
2012-09-30 08:06:28 +00:00
|
|
|
if !exists(a:var) || type({a:var}) != type(a:val)
|
|
|
|
let {a:var} = a:val
|
|
|
|
endif
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
|
|
|
function! s:set_dictionary_helper(variable, keys, pattern)
|
2012-09-30 08:06:28 +00:00
|
|
|
for key in split(a:keys, '\s*,\s*')
|
|
|
|
if !has_key(a:variable, key)
|
|
|
|
let a:variable[key] = a:pattern
|
|
|
|
endif
|
|
|
|
endfor
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
|
|
|
function! s:substitute_path_separator(path)
|
2012-09-30 08:06:28 +00:00
|
|
|
return s:is_windows ? substitute(a:path, '\\', '/', 'g') : a:path
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
|
|
|
function! s:path2directory(path)
|
2012-09-30 08:06:28 +00:00
|
|
|
return s:substitute_path_separator(isdirectory(a:path) ? a:path : fnamemodify(a:path, ':p:h'))
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
|
|
|
function! s:path2project_directory(path, ...)
|
2012-09-30 08:06:28 +00:00
|
|
|
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
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
" Search project file.
|
|
|
|
if directory == ''
|
|
|
|
for d in ['build.xml', 'prj.el', '.project', 'pom.xml',
|
|
|
|
\ 'Makefile', 'configure', 'Rakefile', 'NAnt.build', 'tags', 'gtags']
|
|
|
|
let d = findfile(d, s:escape_file_searching(search_directory) . ';')
|
|
|
|
if d != ''
|
|
|
|
let directory = fnamemodify(d, ':p:h')
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
|
|
|
|
if directory == ''
|
|
|
|
" Search /src/ directory.
|
|
|
|
let base = s:substitute_path_separator(search_directory)
|
|
|
|
if base =~# '/src/'
|
|
|
|
let directory = base[: strridx(base, '/src/') + 3]
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
if directory == '' && !is_allow_empty
|
|
|
|
" Use original path.
|
|
|
|
let directory = search_directory
|
|
|
|
endif
|
|
|
|
|
|
|
|
return s:substitute_path_separator(directory)
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
|
|
|
" Check vimproc.
|
|
|
|
function! s:has_vimproc()
|
2012-09-30 08:06:28 +00:00
|
|
|
if !exists('s:exists_vimproc')
|
|
|
|
try
|
|
|
|
call vimproc#version()
|
|
|
|
let s:exists_vimproc = 1
|
|
|
|
catch
|
|
|
|
let s:exists_vimproc = 0
|
|
|
|
endtry
|
|
|
|
endif
|
|
|
|
return s:exists_vimproc
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:system(str, ...)
|
2012-09-30 08:06:28 +00:00
|
|
|
let command = a:str
|
|
|
|
let input = a:0 >= 1 ? a:1 : ''
|
|
|
|
let command = s:iconv(command, &encoding, 'char')
|
|
|
|
let input = s:iconv(input, &encoding, 'char')
|
|
|
|
|
|
|
|
if a:0 == 0
|
|
|
|
let output = s:has_vimproc() ?
|
|
|
|
\ vimproc#system(command) : system(command)
|
|
|
|
elseif a:0 == 1
|
|
|
|
let output = s:has_vimproc() ?
|
|
|
|
\ vimproc#system(command, input) : system(command, input)
|
|
|
|
else
|
|
|
|
" ignores 3rd argument unless you have vimproc.
|
|
|
|
let output = s:has_vimproc() ?
|
|
|
|
\ vimproc#system(command, input, a:2) : system(command, input)
|
|
|
|
endif
|
|
|
|
|
|
|
|
let output = s:iconv(output, 'char', &encoding)
|
|
|
|
|
|
|
|
return output
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
|
|
|
function! s:get_last_status()
|
2012-09-30 08:06:28 +00:00
|
|
|
return s:has_vimproc() ?
|
|
|
|
\ vimproc#get_last_status() : v:shell_error
|
2012-12-25 13:58:04 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
let &cpo = s:save_cpo
|
|
|
|
unlet s:save_cpo
|
2012-09-30 08:06:28 +00:00
|
|
|
|
|
|
|
" vim:set et ts=2 sts=2 sw=2 tw=0:
|