diff --git a/autoload/vundle/compat.vim b/autoload/vundle/compat.vim new file mode 100644 index 0000000..ea9b69a --- /dev/null +++ b/autoload/vundle/compat.vim @@ -0,0 +1,155 @@ +" vundle - vim compatibility support module + +" this abstracts the operation: +" exec 'set runtimepath-='.vundle#compat#fnameescape(LIST) +" +" notes: +" * it attempts to do the "real thing", if it can; +" * otherwise, it has a number of fallback scenarios, ordered by +" reliability; +" * special case: if a:dirs contains a ',' (multiple entries), this function +" attempts the +" exec 'set runtimepath-='.vundle#compat#fnameescape(LIST) +" first. +" if that does not alter &runtimepath, then it tries to remove a:dirs +" elements individually; +" +" args: +" dirs [string]: described as 'LIST' above; +" +" for now, it does not produce an explicit return value +" +func! vundle#compat#rtp_rm_entry(dirs) + " debug: echomsg '[debug] vundle#compat#rtp_rm_entry(): entered. rtp: ' . string(&rtp) . '; dirs: ' . string(a:dirs) + " optimisation: there seems to be a few of these cases + if empty(a:dirs) + return 0 + endif + let l:runtimepath_orig = &rtp + let l:processed_flag = 0 + let l:elem_separator = ',' + for l:process_stage in range(1, 4) + if ( l:process_stage == 1 ) + exec 'set rtp-='.vundle#compat#fnameescape(a:dirs) + " if &rtp was altered, or a:dirs had only one element, we won't keep + " trying + let l:processed_flag = ( ( &rtp != l:runtimepath_orig ) || ( stridx(a:dirs, l:elem_separator) < 0 ) ) + elseif ( l:process_stage == 2 ) + if exists('*split') && exists('*filter') && exists('*join') + " a bit costly, but more compatible + let l:runtimepath_list = split(&rtp, l:elem_separator, 1) + + for l:entry_dir in split(a:dirs, l:elem_separator, 1) + let l:runtimepath_list = filter(l:runtimepath_list, 'v:val != l:entry_dir') + endfor + " assemble the runtime variable from the list + let &rtp = join(l:runtimepath_list, l:elem_separator) + let l:processed_flag = 1 + endif + elseif ( l:process_stage == 3 ) + if exists('*escape') + " from fnameescape() documentation: + " it escapes: " \t\n*?[{`$\\%#'\"|!<" + " plus, depending on 'isfname', other characters. + " (TODO: add those cases, if needed) + exec 'set rtp-='.escape(a:dirs, " \t\n*?[{`$\\%#'\"|!<") + let l:processed_flag = 1 + endif + elseif ( l:process_stage == 4 ) + " cheap and cheerful (but no escaping) + exec 'set rtp-='.a:dirs + let l:processed_flag = 1 + endif + if l:processed_flag + break + endif + endfor + " debug: echomsg '[debug] vundle#compat#rtp_rm_entry(): exiting. rtp: ' . string(&rtp) +endf + +" abstracts the following operations: +" exec 'set runtimepath^='.vundle#compat#fnameescape(LIST) +" exec 'set runtimepath+='.vundle#compat#fnameescape(LIST) +" exec 'set runtimepath='.vundle#compat#fnameescape(LIST) +" +" args: +" dirs [string]: described as 'LIST' above; +" addset_operator [string]: one of: +" '^': prepend ('set runtimepath^='); +" '+': append ('set runtimepath+='); +" '': set ('set runtimepath='); +" +" for now, it does not produce an explicit return value +" +func! vundle#compat#rtp_addset_entry(dirs, addset_operator) + " debug: echomsg '[debug] vundle#compat#rtp_addset_entry(): entered. rtp: ' . string(&rtp) . '; dirs: ' . string(a:dirs) . '; operator: ' . string( a:addset_operator ) + if exists('*fnameescape') + exec 'set rtp'.a:addset_operator.'='.fnameescape(a:dirs) + else + " a bit costly, but more compatible + + if ( a:addset_operator == '' ) && empty(&rtp) + " this should be quick + let &rtp = a:dirs + else + let l:elem_separator = ',' + + " only add the elements not already present in &runtimepath + if exists('*split') && exists('*filter') && exists('*join') + " a bit costly, but more compatible + let l:dirs_list = split(a:dirs, l:elem_separator, 1) + for l:entry_dir in split(&rtp, l:elem_separator, 1) + let l:dirs_list = filter(l:dirs_list, 'v:val != l:entry_dir') + endfor + let l:dirs = join(l:dirs_list, l:elem_separator) + else + " nothing we can do about this: we'll take the input as is + let l:dirs = a:dirs + endif + + if !empty(l:dirs) + if a:addset_operator == '^' + let &rtp = l:dirs . l:elem_separator . &rtp + elseif a:addset_operator == '+' + let &rtp = &rtp . l:elem_separator . l:dirs + else + " FIXME: report internal error + endif + endif + endif + endif + " debug: echomsg '[debug] vundle#compat#rtp_addset_entry(): exiting. rtp: ' . string(&rtp) +endf + +func! vundle#compat#has_dos_windows_paths() + return exists('+shellslash') +endf + +" provides the same functionality as shellescape() +" (even on vim versions that don't support it) +" +" note: behaviour taken from vim-7.1 documentation +func! vundle#compat#shellescape(string_value) + if vundle#compat#has_dos_windows_paths() + if &shellslash + " return as is? + return a:string_value + else + " double all double quotes within string, + " and enclose result in double quotes + return '"' . substitute(a:string_value,'"','""','g') . '"' + endif + else + " replace all "'" with "'\''" + return "'" . substitute(a:string_value,"'","'".'\\'."''",'g') . "'" + endif +endf + +" (first appeared in a major release in vim-7.2) +let s:vundle_compat_has_fnameescape = exists( '*fnameescape' ) +function vundle#compat#fnameescape( fname ) + " from documentation: it escapes: " \t\n*?[{`$\\%#'\"|!<" + " plus, depending on 'isfname', other characters. + return ( s:vundle_compat_has_fnameescape ? fnameescape( a:fname ) : escape( a:fname, " \t\n*?[{`$\\%#'\"|!<" ) ) +endfunction + diff --git a/autoload/vundle/config.vim b/autoload/vundle/config.vim index c665d4d..173e97b 100644 --- a/autoload/vundle/config.vim +++ b/autoload/vundle/config.vim @@ -172,7 +172,7 @@ func! s:rtp_add_defaults() let &rtp = current let default_rtp_items = split(default, ',') if !empty(default_rtp_items) - let first_item = fnameescape(default_rtp_items[0]) + let first_item = vundle#compat#fnameescape(default_rtp_items[0]) exec 'set rtp-=' . first_item exec 'set rtp^=' . first_item endif @@ -185,10 +185,12 @@ endf " --------------------------------------------------------------------------- func! s:rtp_rm_a() let paths = map(copy(g:vundle#bundles), 'v:val.rtpath') - let prepends = join(paths, ',') - let appends = join(paths, '/after,').'/after' - exec 'set rtp-='.fnameescape(prepends) - exec 'set rtp-='.fnameescape(appends) + if !empty(paths) + let prepends = join(paths, ',') + let appends = join(paths, '/after,').'/after' + call vundle#compat#rtp_rm_entry(prepends) + call vundle#compat#rtp_rm_entry(appends) + endif endf @@ -198,10 +200,12 @@ endf " --------------------------------------------------------------------------- func! s:rtp_add_a() let paths = map(copy(g:vundle#bundles), 'v:val.rtpath') - let prepends = join(paths, ',') - let appends = join(paths, '/after,').'/after' - exec 'set rtp^='.fnameescape(prepends) - exec 'set rtp+='.fnameescape(appends) + if !empty(paths) + let prepends = join(paths, ',') + let appends = join(paths, '/after,').'/after' + call vundle#compat#rtp_addset_entry(prepends,'^') + call vundle#compat#rtp_addset_entry(appends,'+') + endif endf @@ -212,8 +216,10 @@ endf " 'after' directory will also be removed. " --------------------------------------------------------------------------- func! s:rtp_rm(dir) abort - exec 'set rtp-='.fnameescape(expand(a:dir, 1)) - exec 'set rtp-='.fnameescape(expand(a:dir.'/after', 1)) + if !empty(a:dir) + call vundle#compat#rtp_rm_entry(expand(a:dir, 1)) + call vundle#compat#rtp_rm_entry(expand(a:dir.'/after', 1)) + endif endf @@ -224,8 +230,10 @@ endf " 'after' directory will also be added. " --------------------------------------------------------------------------- func! s:rtp_add(dir) abort - exec 'set rtp^='.fnameescape(expand(a:dir, 1)) - exec 'set rtp+='.fnameescape(expand(a:dir.'/after', 1)) + if !empty(a:dir) + call vundle#compat#rtp_addset_entry(expand(a:dir, 1), '^') + call vundle#compat#rtp_addset_entry(expand(a:dir.'/after', 1), '+') + endif endf diff --git a/autoload/vundle/installer.vim b/autoload/vundle/installer.vim index e05cff2..7859807 100644 --- a/autoload/vundle/installer.vim +++ b/autoload/vundle/installer.vim @@ -482,7 +482,7 @@ func! vundle#installer#shellesc(cmd) abort if ((has('win32') || has('win64')) && empty(matchstr(&shell, 'sh'))) return '"' . substitute(a:cmd, '"', '\\"', 'g') . '"' endif - return shellescape(a:cmd) + return vundle#compat#shellescape(a:cmd) endf diff --git a/test/vimrc b/test/vimrc index d8455a7..984d4c7 100644 --- a/test/vimrc +++ b/test/vimrc @@ -9,6 +9,9 @@ let bundle_dir = '/tmp/vundle-test/bundles/' " Vundle Options " let g:vundle_default_git_proto = 'git' +" NOTE: the 'autoload' modules are not available. if they were, +" we could escape the 'bundle_dir' like this: +" vundle#compat#shellescape(bundle_dir) silent execute '!mkdir -p '.bundle_dir silent execute '!ln -f -s ~/.vim/bundle/Vundle.vim '.bundle_dir