pathogen.vim (11733B)
1 " pathogen.vim - path option manipulation 2 " Maintainer: Tim Pope <http://tpo.pe/> 3 " Version: 2.2 4 5 " Install in ~/.vim/autoload (or ~\vimfiles\autoload). 6 " 7 " For management of individually installed plugins in ~/.vim/bundle (or 8 " ~\vimfiles\bundle), adding `execute pathogen#infect()` to the top of your 9 " .vimrc is the only other setup necessary. 10 " 11 " The API is documented inline below. For maximum ease of reading, 12 " :set foldmethod=marker 13 14 if exists("g:loaded_pathogen") || &cp 15 finish 16 endif 17 let g:loaded_pathogen = 1 18 19 function! s:warn(msg) 20 echohl WarningMsg 21 echomsg a:msg 22 echohl NONE 23 endfunction 24 25 " Point of entry for basic default usage. Give a relative path to invoke 26 " pathogen#incubate() (defaults to "bundle/{}"), or an absolute path to invoke 27 " pathogen#surround(). For backwards compatibility purposes, a full path that 28 " does not end in {} or * is given to pathogen#runtime_prepend_subdirectories() 29 " instead. 30 function! pathogen#infect(...) abort " {{{1 31 for path in a:0 ? reverse(copy(a:000)) : ['bundle/{}'] 32 if path =~# '^[^\\/]\+$' 33 call s:warn('Change pathogen#infect('.string(path).') to pathogen#infect('.string(path.'/{}').')') 34 call pathogen#incubate(path . '/{}') 35 elseif path =~# '^[^\\/]\+[\\/]\%({}\|\*\)$' 36 call pathogen#incubate(path) 37 elseif path =~# '[\\/]\%({}\|\*\)$' 38 call pathogen#surround(path) 39 else 40 call s:warn('Change pathogen#infect('.string(path).') to pathogen#infect('.string(path.'/{}').')') 41 call pathogen#surround(path . '/{}') 42 endif 43 endfor 44 call pathogen#cycle_filetype() 45 return '' 46 endfunction " }}}1 47 48 " Split a path into a list. 49 function! pathogen#split(path) abort " {{{1 50 if type(a:path) == type([]) | return a:path | endif 51 let split = split(a:path,'\\\@<!\%(\\\\\)*\zs,') 52 return map(split,'substitute(v:val,''\\\([\\,]\)'',''\1'',"g")') 53 endfunction " }}}1 54 55 " Convert a list to a path. 56 function! pathogen#join(...) abort " {{{1 57 if type(a:1) == type(1) && a:1 58 let i = 1 59 let space = ' ' 60 else 61 let i = 0 62 let space = '' 63 endif 64 let path = "" 65 while i < a:0 66 if type(a:000[i]) == type([]) 67 let list = a:000[i] 68 let j = 0 69 while j < len(list) 70 let escaped = substitute(list[j],'[,'.space.']\|\\[\,'.space.']\@=','\\&','g') 71 let path .= ',' . escaped 72 let j += 1 73 endwhile 74 else 75 let path .= "," . a:000[i] 76 endif 77 let i += 1 78 endwhile 79 return substitute(path,'^,','','') 80 endfunction " }}}1 81 82 " Convert a list to a path with escaped spaces for 'path', 'tag', etc. 83 function! pathogen#legacyjoin(...) abort " {{{1 84 return call('pathogen#join',[1] + a:000) 85 endfunction " }}}1 86 87 " Remove duplicates from a list. 88 function! pathogen#uniq(list) abort " {{{1 89 let i = 0 90 let seen = {} 91 while i < len(a:list) 92 if (a:list[i] ==# '' && exists('empty')) || has_key(seen,a:list[i]) 93 call remove(a:list,i) 94 elseif a:list[i] ==# '' 95 let i += 1 96 let empty = 1 97 else 98 let seen[a:list[i]] = 1 99 let i += 1 100 endif 101 endwhile 102 return a:list 103 endfunction " }}}1 104 105 " \ on Windows unless shellslash is set, / everywhere else. 106 function! pathogen#separator() abort " {{{1 107 return !exists("+shellslash") || &shellslash ? '/' : '\' 108 endfunction " }}}1 109 110 " Convenience wrapper around glob() which returns a list. 111 function! pathogen#glob(pattern) abort " {{{1 112 let files = split(glob(a:pattern),"\n") 113 return map(files,'substitute(v:val,"[".pathogen#separator()."/]$","","")') 114 endfunction "}}}1 115 116 " Like pathogen#glob(), only limit the results to directories. 117 function! pathogen#glob_directories(pattern) abort " {{{1 118 return filter(pathogen#glob(a:pattern),'isdirectory(v:val)') 119 endfunction "}}}1 120 121 " Turn filetype detection off and back on again if it was already enabled. 122 function! pathogen#cycle_filetype() " {{{1 123 if exists('g:did_load_filetypes') 124 filetype off 125 filetype on 126 endif 127 endfunction " }}}1 128 129 " Check if a bundle is disabled. A bundle is considered disabled if it ends 130 " in a tilde or its basename or full name is included in the list 131 " g:pathogen_disabled. 132 function! pathogen#is_disabled(path) " {{{1 133 if a:path =~# '\~$' 134 return 1 135 elseif !exists("g:pathogen_disabled") 136 return 0 137 endif 138 let sep = pathogen#separator() 139 let blacklist = g:pathogen_disabled 140 return index(blacklist, strpart(a:path, strridx(a:path, sep)+1)) != -1 && index(blacklist, a:path) != 1 141 endfunction "}}}1 142 143 " Prepend the given directory to the runtime path and append its corresponding 144 " after directory. If the directory is already included, move it to the 145 " outermost position. Wildcards are added as is. Ending a path in /{} causes 146 " all subdirectories to be added (except those in g:pathogen_disabled). 147 function! pathogen#surround(path) abort " {{{1 148 let sep = pathogen#separator() 149 let rtp = pathogen#split(&rtp) 150 if a:path =~# '[\\/]{}$' 151 let path = fnamemodify(a:path[0:-4], ':p:s?[\\/]\=$??') 152 let before = filter(pathogen#glob_directories(path.sep.'*'), '!pathogen#is_disabled(v:val)') 153 let after = filter(reverse(pathogen#glob_directories(path.sep."*".sep."after")), '!pathogen#is_disabled(v:val[0:-7])') 154 call filter(rtp,'v:val[0:strlen(path)-1] !=# path') 155 else 156 let path = fnamemodify(a:path, ':p:s?[\\/]\=$??') 157 let before = [path] 158 let after = [path . sep . 'after'] 159 call filter(rtp, 'index(before + after, v:val) == -1') 160 endif 161 let &rtp = pathogen#join(before, rtp, after) 162 return &rtp 163 endfunction " }}}1 164 165 " Prepend all subdirectories of path to the rtp, and append all 'after' 166 " directories in those subdirectories. Deprecated. 167 function! pathogen#runtime_prepend_subdirectories(path) " {{{1 168 call s:warn('Change pathogen#runtime_prepend_subdirectories('.string(a:path).') to pathogen#surround('.string(a:path.'/{}').')') 169 return pathogen#surround(a:path . pathogen#separator() . '{}') 170 endfunction " }}}1 171 172 " For each directory in the runtime path, add a second entry with the given 173 " argument appended. If the argument ends in '/{}', add a separate entry for 174 " each subdirectory. The default argument is 'bundle/{}', which means that 175 " .vim/bundle/*, $VIM/vimfiles/bundle/*, $VIMRUNTIME/bundle/*, 176 " $VIM/vim/files/bundle/*/after, and .vim/bundle/*/after will be added (on 177 " UNIX). 178 function! pathogen#incubate(...) abort " {{{1 179 let sep = pathogen#separator() 180 let name = a:0 ? a:1 : 'bundle/{}' 181 if "\n".s:done_bundles =~# "\\M\n".name."\n" 182 return "" 183 endif 184 let s:done_bundles .= name . "\n" 185 let list = [] 186 for dir in pathogen#split(&rtp) 187 if dir =~# '\<after$' 188 if name =~# '{}$' 189 let list += filter(pathogen#glob_directories(substitute(dir,'after$',name[0:-3],'').'*'.sep.'after'), '!pathogen#is_disabled(v:val[0:-7])') + [dir] 190 else 191 let list += [dir, substitute(dir, 'after$', '', '') . name . sep . 'after'] 192 endif 193 else 194 if name =~# '{}$' 195 let list += [dir] + filter(pathogen#glob_directories(dir.sep.name[0:-3].'*'), '!pathogen#is_disabled(v:val)') 196 else 197 let list += [dir . sep . name, dir] 198 endif 199 endif 200 endfor 201 let &rtp = pathogen#join(pathogen#uniq(list)) 202 return 1 203 endfunction " }}}1 204 205 " Deprecated alias for pathogen#incubate(). 206 function! pathogen#runtime_append_all_bundles(...) abort " {{{1 207 if a:0 208 call s:warn('Change pathogen#runtime_append_all_bundles('.string(a:1).') to pathogen#incubate('.string(a:1.'/{}').')') 209 else 210 call s:warn('Change pathogen#runtime_append_all_bundles() to pathogen#incubate()') 211 endif 212 return call('pathogen#incubate', map(copy(a:000),'v:val . "/{}"')) 213 endfunction 214 215 let s:done_bundles = '' 216 " }}}1 217 218 " Invoke :helptags on all non-$VIM doc directories in runtimepath. 219 function! pathogen#helptags() abort " {{{1 220 let sep = pathogen#separator() 221 for glob in pathogen#split(&rtp) 222 for dir in split(glob(glob), "\n") 223 if (dir.sep)[0 : strlen($VIMRUNTIME)] !=# $VIMRUNTIME.sep && filewritable(dir.sep.'doc') == 2 && !empty(filter(split(glob(dir.sep.'doc'.sep.'*'),"\n>"),'!isdirectory(v:val)')) && (!filereadable(dir.sep.'doc'.sep.'tags') || filewritable(dir.sep.'doc'.sep.'tags')) 224 silent! execute 'helptags' pathogen#fnameescape(dir.'/doc') 225 endif 226 endfor 227 endfor 228 endfunction " }}}1 229 230 command! -bar Helptags :call pathogen#helptags() 231 232 " Execute the given command. This is basically a backdoor for --remote-expr. 233 function! pathogen#execute(...) abort " {{{1 234 for command in a:000 235 execute command 236 endfor 237 return '' 238 endfunction " }}}1 239 240 " Like findfile(), but hardcoded to use the runtimepath. 241 function! pathogen#runtime_findfile(file,count) abort "{{{1 242 let rtp = pathogen#join(1,pathogen#split(&rtp)) 243 let file = findfile(a:file,rtp,a:count) 244 if file ==# '' 245 return '' 246 else 247 return fnamemodify(file,':p') 248 endif 249 endfunction " }}}1 250 251 " Backport of fnameescape(). 252 function! pathogen#fnameescape(string) abort " {{{1 253 if exists('*fnameescape') 254 return fnameescape(a:string) 255 elseif a:string ==# '-' 256 return '\-' 257 else 258 return substitute(escape(a:string," \t\n*?[{`$\\%#'\"|!<"),'^[+>]','\\&','') 259 endif 260 endfunction " }}}1 261 262 if exists(':Vedit') 263 finish 264 endif 265 266 let s:vopen_warning = 0 267 268 function! s:find(count,cmd,file,lcd) " {{{1 269 let rtp = pathogen#join(1,pathogen#split(&runtimepath)) 270 let file = pathogen#runtime_findfile(a:file,a:count) 271 if file ==# '' 272 return "echoerr 'E345: Can''t find file \"".a:file."\" in runtimepath'" 273 endif 274 if !s:vopen_warning 275 let s:vopen_warning = 1 276 let warning = '|echohl WarningMsg|echo "Install scriptease.vim to continue using :V'.a:cmd.'"|echohl NONE' 277 else 278 let warning = '' 279 endif 280 if a:lcd 281 let path = file[0:-strlen(a:file)-2] 282 execute 'lcd `=path`' 283 return a:cmd.' '.pathogen#fnameescape(a:file) . warning 284 else 285 return a:cmd.' '.pathogen#fnameescape(file) . warning 286 endif 287 endfunction " }}}1 288 289 function! s:Findcomplete(A,L,P) " {{{1 290 let sep = pathogen#separator() 291 let cheats = { 292 \'a': 'autoload', 293 \'d': 'doc', 294 \'f': 'ftplugin', 295 \'i': 'indent', 296 \'p': 'plugin', 297 \'s': 'syntax'} 298 if a:A =~# '^\w[\\/]' && has_key(cheats,a:A[0]) 299 let request = cheats[a:A[0]].a:A[1:-1] 300 else 301 let request = a:A 302 endif 303 let pattern = substitute(request,'/\|\'.sep,'*'.sep,'g').'*' 304 let found = {} 305 for path in pathogen#split(&runtimepath) 306 let path = expand(path, ':p') 307 let matches = split(glob(path.sep.pattern),"\n") 308 call map(matches,'isdirectory(v:val) ? v:val.sep : v:val') 309 call map(matches,'expand(v:val, ":p")[strlen(path)+1:-1]') 310 for match in matches 311 let found[match] = 1 312 endfor 313 endfor 314 return sort(keys(found)) 315 endfunction " }}}1 316 317 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Ve :execute s:find(<count>,'edit<bang>',<q-args>,0) 318 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vedit :execute s:find(<count>,'edit<bang>',<q-args>,0) 319 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vopen :execute s:find(<count>,'edit<bang>',<q-args>,1) 320 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vsplit :execute s:find(<count>,'split',<q-args>,<bang>1) 321 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vvsplit :execute s:find(<count>,'vsplit',<q-args>,<bang>1) 322 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vtabedit :execute s:find(<count>,'tabedit',<q-args>,<bang>1) 323 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vpedit :execute s:find(<count>,'pedit',<q-args>,<bang>1) 324 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vread :execute s:find(<count>,'read',<q-args>,<bang>1) 325 326 " vim:set et sw=2: