"============================================================================= " FILE: util.vim " AUTHOR: Shougo Matsushita " License: MIT license {{{ " Permission is hereby granted, free of charge, to any person obtaining " a copy of this software and associated documentation files (the " "Software"), to deal in the Software without restriction, including " without limitation the rights to use, copy, modify, merge, publish, " distribute, sublicense, and/or sell copies of the Software, and to " permit persons to whom the Software is furnished to do so, subject to " the following conditions: " " The above copyright notice and this permission notice shall be included " in all copies or substantial portions of the Software. " " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS " OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF " MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. " IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY " CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, " TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE " SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. " }}} "============================================================================= let s:save_cpo = &cpo set cpo&vim let s:is_windows = has('win32') let s:is_cygwin = has('win32unix') let s:is_mac = !s:is_windows && !s:is_cygwin \ && (has('mac') || has('macunix') || has('gui_macvim') || \ (!isdirectory('/proc') && executable('sw_vers'))) function! neobundle#util#substitute_path_separator(path) abort "{{{ return (s:is_windows && a:path =~ '\\') ? \ tr(a:path, '\', '/') : a:path endfunction"}}} function! neobundle#util#expand(path) abort "{{{ let path = (a:path =~ '^\~') ? fnamemodify(a:path, ':p') : \ (a:path =~ '^\$\h\w*') ? substitute(a:path, \ '^\$\h\w*', '\=eval(submatch(0))', '') : \ a:path return (s:is_windows && path =~ '\\') ? \ neobundle#util#substitute_path_separator(path) : path endfunction"}}} function! neobundle#util#join_paths(path1, path2) abort "{{{ " Joins two paths together, handling the case where the second path " is an absolute path. if s:is_absolute(a:path2) return a:path2 endif if a:path1 =~ (s:is_windows ? '[\\/]$' : '/$') || \ a:path2 =~ (s:is_windows ? '^[\\/]' : '^/') " the appropriate separator already exists return a:path1 . a:path2 else " note: I'm assuming here that '/' is always valid as a directory " separator on Windows. I know Windows has paths that start with \\?\ that " diasble behavior like that, but I don't know how Vim deals with that. return a:path1 . '/' . a:path2 endif endfunction "}}} if s:is_windows function! s:is_absolute(path) abort "{{{ return a:path =~ '^[\\/]\|^\a:' endfunction "}}} else function! s:is_absolute(path) abort "{{{ return a:path =~ "^/" endfunction "}}} endif function! neobundle#util#is_windows() abort "{{{ return s:is_windows endfunction"}}} function! neobundle#util#is_mac() abort "{{{ return s:is_mac endfunction"}}} function! neobundle#util#is_cygwin() abort "{{{ return s:is_cygwin endfunction"}}} " Sudo check. function! neobundle#util#is_sudo() abort "{{{ return $SUDO_USER != '' && $USER !=# $SUDO_USER \ && $HOME !=# expand('~'.$USER) \ && $HOME ==# expand('~'.$SUDO_USER) endfunction"}}} " Check vimproc. "{{{ function! neobundle#util#has_vimproc() abort "{{{ if !exists('*vimproc#version') try call vimproc#version() catch endtry endif return exists('*vimproc#version') endfunction"}}} "}}} " iconv() wrapper for safety. function! s:iconv(expr, from, to) abort "{{{ 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"}}} function! neobundle#util#system(str, ...) abort "{{{ 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 = neobundle#util#has_vimproc() ? \ vimproc#system(command) : system(command, "\") elseif a:0 == 1 let output = neobundle#util#has_vimproc() ? \ vimproc#system(command, input) : system(command, input) else " ignores 3rd argument unless you have vimproc. let output = neobundle#util#has_vimproc() ? \ vimproc#system(command, input, a:2) : system(command, input) endif let output = s:iconv(output, 'char', &encoding) return substitute(output, '\n$', '', '') endfunction"}}} function! neobundle#util#get_last_status() abort "{{{ return neobundle#util#has_vimproc() ? \ vimproc#get_last_status() : v:shell_error endfunction"}}} " Split a comma separated string to a list. function! neobundle#util#split_rtp(runtimepath) abort "{{{ if stridx(a:runtimepath, '\,') < 0 return split(a:runtimepath, ',') endif let split = split(a:runtimepath, '\\\@# a:b[1] ? 1 : -1'), 'v:val[0]') endfunction"}}} " Executes a command and returns its output. " This wraps Vim's `:redir`, and makes sure that the `verbose` settings have " no influence. function! neobundle#util#redir(cmd) abort "{{{ let [save_verbose, save_verbosefile] = [&verbose, &verbosefile] set verbose=0 verbosefile= redir => res silent! execute a:cmd redir END let [&verbose, &verbosefile] = [save_verbose, save_verbosefile] return res 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) abort "{{{ 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"}}} function! s:_compare(a, b) abort return eval(s:expr) endfunction function! neobundle#util#print_bundles(bundles) abort "{{{ echomsg string(map(copy(a:bundles), 'v:val.name')) endfunction"}}} function! neobundle#util#sort_human(filenames) abort "{{{ return sort(a:filenames, 's:compare_filename') endfunction"}}} " Compare filename by human order. "{{{ function! s:compare_filename(i1, i2) abort let words_1 = s:get_words(a:i1) let words_2 = s:get_words(a:i2) let words_1_len = len(words_1) let words_2_len = len(words_2) for i in range(0, min([words_1_len, words_2_len])-1) if words_1[i] >? words_2[i] return 1 elseif words_1[i] a:width let char = matchstr(ret, '.$') let ret = ret[: -1 - len(char)] let width -= s:wcswidth(char) endwhile return ret endfunction"}}} function! s:strwidthpart_reverse(str, width) abort "{{{ 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 endfunction"}}} function! s:wcswidth(str) abort "{{{ return v:version >= 704 ? strwidth(a:str) : strlen(a:str) endfunction"}}} let &cpo = s:save_cpo unlet s:save_cpo " vim: foldmethod=marker