pathogen.vim 12 KB

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