1 " pathogen.vim - path option manipulation
2 " Maintainer: Tim Pope <http://tpo.pe/>
5 " Install in ~/.vim/autoload (or ~\vimfiles\autoload).
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.
11 " The API is documented inline below.
13 if exists("g:loaded_pathogen") || &cp
16 let g:loaded_pathogen = 1
18 " Point of entry for basic default usage. Give a relative path to invoke
19 " pathogen#interpose() (defaults to "bundle/{}"), or an absolute path to invoke
20 " pathogen#surround(). Curly braces are expanded with pathogen#expand():
21 " "bundle/{}" finds all subdirectories inside "bundle" inside all directories
22 " in the runtime path.
23 function! pathogen#infect(...) abort
24 for path in a:0 ? filter(reverse(copy(a:000)), 'type(v:val) == type("")') : ['bundle/{}']
25 if path =~# '^\%({\=[$~\\/]\|{\=\w:[\\/]\).*[{}*]'
26 call pathogen#surround(path)
27 elseif path =~# '^\%([$~\\/]\|\w:[\\/]\)'
28 call s:warn('Change pathogen#infect('.string(path).') to pathogen#infect('.string(path.'/{}').')')
29 call pathogen#surround(path . '/{}')
30 elseif path =~# '[{}*]'
31 call pathogen#interpose(path)
33 call s:warn('Change pathogen#infect('.string(path).') to pathogen#infect('.string(path.'/{}').')')
34 call pathogen#interpose(path . '/{}')
37 call pathogen#cycle_filetype()
38 if pathogen#is_disabled($MYVIMRC)
44 " Split a path into a list.
45 function! pathogen#split(path) abort
46 if type(a:path) == type([]) | return a:path | endif
47 if empty(a:path) | return [] | endif
48 let split = split(a:path,'\\\@<!\%(\\\\\)*\zs,')
49 return map(split,'substitute(v:val,''\\\([\\,]\)'',''\1'',"g")')
52 " Convert a list to a path.
53 function! pathogen#join(...) abort
54 if type(a:1) == type(1) && a:1
63 if type(a:000[i]) == type([])
67 let escaped = substitute(list[j],'[,'.space.']\|\\[\,'.space.']\@=','\\&','g')
68 let path .= ',' . escaped
72 let path .= "," . a:000[i]
76 return substitute(path,'^,','','')
79 " Convert a list to a path with escaped spaces for 'path', 'tag', etc.
80 function! pathogen#legacyjoin(...) abort
81 return call('pathogen#join',[1] + a:000)
84 " Turn filetype detection off and back on again if it was already enabled.
85 function! pathogen#cycle_filetype() abort
86 if exists('g:did_load_filetypes')
92 " Check if a bundle is disabled. A bundle is considered disabled if its
93 " basename or full name is included in the list g:pathogen_disabled.
94 function! pathogen#is_disabled(path) abort
98 let sep = pathogen#slash()
100 \ get(g:, 'pathogen_blacklist', get(g:, 'pathogen_disabled', [])) +
101 \ pathogen#split($VIMBLACKLIST),
102 \ 'substitute(v:val, "[\\/]$", "", "")')
103 return index(blacklist, fnamemodify(a:path, ':t')) != -1 || index(blacklist, a:path) != -1
106 " Prepend the given directory to the runtime path and append its corresponding
107 " after directory. Curly braces are expanded with pathogen#expand().
108 function! pathogen#surround(path) abort
109 let sep = pathogen#slash()
110 let rtp = pathogen#split(&rtp)
111 let path = fnamemodify(a:path, ':p:?[\\/]\=$??')
112 let before = filter(pathogen#expand(path), '!pathogen#is_disabled(v:val)')
113 let after = filter(reverse(pathogen#expand(path.sep.'after')), '!pathogen#is_disabled(v:val[0:-7])')
114 call filter(rtp, 'index(before + after, v:val) == -1')
115 let &rtp = pathogen#join(before, rtp, after)
119 " For each directory in the runtime path, add a second entry with the given
120 " argument appended. Curly braces are expanded with pathogen#expand().
121 function! pathogen#interpose(name) abort
122 let sep = pathogen#slash()
124 if has_key(s:done_bundles, name)
127 let s:done_bundles[name] = 1
129 for dir in pathogen#split(&rtp)
130 if dir =~# '\<after$'
131 let list += reverse(filter(pathogen#expand(dir[0:-6].name.sep.'after'), '!pathogen#is_disabled(v:val[0:-7])')) + [dir]
133 let list += [dir] + filter(pathogen#expand(dir.sep.name), '!pathogen#is_disabled(v:val)')
136 let &rtp = pathogen#join(pathogen#uniq(list))
140 let s:done_bundles = {}
142 " Invoke :helptags on all non-$VIM doc directories in runtimepath.
143 function! pathogen#helptags() abort
144 let sep = pathogen#slash()
145 for glob in pathogen#split(&rtp)
146 for dir in map(split(glob(glob), "\n"), 'v:val.sep."/doc/".sep')
147 if (dir)[0 : strlen($VIMRUNTIME)] !=# $VIMRUNTIME.sep && filewritable(dir) == 2 && !empty(split(glob(dir.'*.txt'))) && (!filereadable(dir.'tags') || filewritable(dir.'tags'))
148 silent! execute 'helptags' pathogen#fnameescape(dir)
154 command! -bar Helptags :call pathogen#helptags()
156 " Execute the given command. This is basically a backdoor for --remote-expr.
157 function! pathogen#execute(...) abort
164 " Section: Unofficial
166 function! pathogen#is_absolute(path) abort
167 return a:path =~# (has('win32') ? '^\%([\\/]\|\w:\)[\\/]\|^[~$]' : '^[/~$]')
170 " Given a string, returns all possible permutations of comma delimited braced
171 " alternatives of that string. pathogen#expand('/{a,b}/{c,d}') yields
172 " ['/a/c', '/a/d', '/b/c', '/b/d']. Empty braces are treated as a wildcard
173 " and globbed. Actual globs are preserved.
174 function! pathogen#expand(pattern) abort
175 if a:pattern =~# '{[^{}]\+}'
176 let [pre, pat, post] = split(substitute(a:pattern, '\(.\{-\}\){\([^{}]\+\)}\(.*\)', "\\1\001\\2\001\\3", ''), "\001", 1)
177 let found = map(split(pat, ',', 1), 'pre.v:val.post')
180 call extend(results, pathogen#expand(pattern))
183 elseif a:pattern =~# '{}'
184 let pat = matchstr(a:pattern, '^.*{}[^*]*\%($\|[\\/]\)')
185 let post = a:pattern[strlen(pat) : -1]
186 return map(split(glob(substitute(pat, '{}', '*', 'g')), "\n"), 'v:val.post')
192 " \ on Windows unless shellslash is set, / everywhere else.
193 function! pathogen#slash() abort
194 return !exists("+shellslash") || &shellslash ? '/' : '\'
197 function! pathogen#separator() abort
198 return pathogen#slash()
201 " Convenience wrapper around glob() which returns a list.
202 function! pathogen#glob(pattern) abort
203 let files = split(glob(a:pattern),"\n")
204 return map(files,'substitute(v:val,"[".pathogen#slash()."/]$","","")')
207 " Like pathogen#glob(), only limit the results to directories.
208 function! pathogen#glob_directories(pattern) abort
209 return filter(pathogen#glob(a:pattern),'isdirectory(v:val)')
212 " Remove duplicates from a list.
213 function! pathogen#uniq(list) abort
216 while i < len(a:list)
217 if (a:list[i] ==# '' && exists('empty')) || has_key(seen,a:list[i])
218 call remove(a:list,i)
219 elseif a:list[i] ==# ''
223 let seen[a:list[i]] = 1
230 " Backport of fnameescape().
231 function! pathogen#fnameescape(string) abort
232 if exists('*fnameescape')
233 return fnameescape(a:string)
234 elseif a:string ==# '-'
237 return substitute(escape(a:string," \t\n*?[{`$\\%#'\"|!<"),'^[+>]','\\&','')
241 " Like findfile(), but hardcoded to use the runtimepath.
242 function! pathogen#runtime_findfile(file,count) abort "{{{1
243 let rtp = pathogen#join(1,pathogen#split(&rtp))
244 let file = findfile(a:file,rtp,a:count)
248 return fnamemodify(file,':p')
252 " Section: Deprecated
254 function! s:warn(msg) abort
260 " Prepend all subdirectories of path to the rtp, and append all 'after'
261 " directories in those subdirectories. Deprecated.
262 function! pathogen#runtime_prepend_subdirectories(path) abort
263 call s:warn('Change pathogen#runtime_prepend_subdirectories('.string(a:path).') to pathogen#infect('.string(a:path.'/{}').')')
264 return pathogen#surround(a:path . pathogen#slash() . '{}')
267 function! pathogen#incubate(...) abort
268 let name = a:0 ? a:1 : 'bundle/{}'
269 call s:warn('Change pathogen#incubate('.(a:0 ? string(a:1) : '').') to pathogen#infect('.string(name).')')
270 return pathogen#interpose(name)
273 " Deprecated alias for pathogen#interpose().
274 function! pathogen#runtime_append_all_bundles(...) abort
276 call s:warn('Change pathogen#runtime_append_all_bundles('.string(a:1).') to pathogen#infect('.string(a:1.'/{}').')')
278 call s:warn('Change pathogen#runtime_append_all_bundles() to pathogen#infect()')
280 return pathogen#interpose(a:0 ? a:1 . '/{}' : 'bundle/{}')
287 let s:vopen_warning = 0
289 function! s:find(count,cmd,file,lcd)
290 let rtp = pathogen#join(1,pathogen#split(&runtimepath))
291 let file = pathogen#runtime_findfile(a:file,a:count)
293 return "echoerr 'E345: Can''t find file \"".a:file."\" in runtimepath'"
296 let s:vopen_warning = 1
297 let warning = '|echohl WarningMsg|echo "Install scriptease.vim to continue using :V'.a:cmd.'"|echohl NONE'
302 let path = file[0:-strlen(a:file)-2]
303 execute 'lcd `=path`'
304 return a:cmd.' '.pathogen#fnameescape(a:file) . warning
306 return a:cmd.' '.pathogen#fnameescape(file) . warning
310 function! s:Findcomplete(A,L,P)
311 let sep = pathogen#slash()
319 if a:A =~# '^\w[\\/]' && has_key(cheats,a:A[0])
320 let request = cheats[a:A[0]].a:A[1:-1]
324 let pattern = substitute(request,'/\|\'.sep,'*'.sep,'g').'*'
326 for path in pathogen#split(&runtimepath)
327 let path = expand(path, ':p')
328 let matches = split(glob(path.sep.pattern),"\n")
329 call map(matches,'isdirectory(v:val) ? v:val.sep : v:val')
330 call map(matches,'expand(v:val, ":p")[strlen(path)+1:-1]')
335 return sort(keys(found))
338 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Ve :execute s:find(<count>,'edit<bang>',<q-args>,0)
339 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vedit :execute s:find(<count>,'edit<bang>',<q-args>,0)
340 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vopen :execute s:find(<count>,'edit<bang>',<q-args>,1)
341 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vsplit :execute s:find(<count>,'split',<q-args>,<bang>1)
342 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vvsplit :execute s:find(<count>,'vsplit',<q-args>,<bang>1)
343 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vtabedit :execute s:find(<count>,'tabedit',<q-args>,<bang>1)
344 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vpedit :execute s:find(<count>,'pedit',<q-args>,<bang>1)
345 command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vread :execute s:find(<count>,'read',<q-args>,<bang>1)
347 " vim:set et sw=2 foldmethod=expr foldexpr=getline(v\:lnum)=~'^\"\ Section\:'?'>1'\:getline(v\:lnum)=~#'^fu'?'a1'\:getline(v\:lnum)=~#'^endf'?'s1'\:'=':