pathogen.vim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. " pathogen.vim - path option manipulation
  2. " Maintainer: Tim Pope <http://tpo.pe/>
  3. " Version: 2.3
  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_disabled.
  86. function! pathogen#is_disabled(path) abort
  87. if a:path =~# '\~$'
  88. return 1
  89. endif
  90. let sep = pathogen#slash()
  91. let blacklist = get(g:, 'pathogen_blacklist', get(g:, 'pathogen_disabled', [])) + pathogen#split($VIMBLACKLIST)
  92. return index(blacklist, fnamemodify(a:path, ':t')) != -1 || index(blacklist, a:path) != -1
  93. endfunction "}}}1
  94. " Prepend the given directory to the runtime path and append its corresponding
  95. " after directory. Curly braces are expanded with pathogen#expand().
  96. function! pathogen#surround(path) abort
  97. let sep = pathogen#slash()
  98. let rtp = pathogen#split(&rtp)
  99. let path = fnamemodify(a:path, ':p:?[\\/]\=$??')
  100. let before = filter(pathogen#expand(path), '!pathogen#is_disabled(v:val)')
  101. let after = filter(reverse(pathogen#expand(path.sep.'after')), '!pathogen#is_disabled(v:val[0:-7])')
  102. call filter(rtp, 'index(before + after, v:val) == -1')
  103. let &rtp = pathogen#join(before, rtp, after)
  104. return &rtp
  105. endfunction
  106. " For each directory in the runtime path, add a second entry with the given
  107. " argument appended. Curly braces are expanded with pathogen#expand().
  108. function! pathogen#interpose(name) abort
  109. let sep = pathogen#slash()
  110. let name = a:name
  111. if has_key(s:done_bundles, name)
  112. return ""
  113. endif
  114. let s:done_bundles[name] = 1
  115. let list = []
  116. for dir in pathogen#split(&rtp)
  117. if dir =~# '\<after$'
  118. let list += reverse(filter(pathogen#expand(dir[0:-6].name.sep.'after'), '!pathogen#is_disabled(v:val[0:-7])')) + [dir]
  119. else
  120. let list += [dir] + filter(pathogen#expand(dir.sep.name), '!pathogen#is_disabled(v:val)')
  121. endif
  122. endfor
  123. let &rtp = pathogen#join(pathogen#uniq(list))
  124. return 1
  125. endfunction
  126. let s:done_bundles = {}
  127. " Invoke :helptags on all non-$VIM doc directories in runtimepath.
  128. function! pathogen#helptags() abort
  129. let sep = pathogen#slash()
  130. for glob in pathogen#split(&rtp)
  131. for dir in map(split(glob(glob), "\n"), 'v:val.sep."/doc/".sep')
  132. if (dir)[0 : strlen($VIMRUNTIME)] !=# $VIMRUNTIME.sep && filewritable(dir) == 2 && !empty(split(glob(dir.'*.txt'))) && (!filereadable(dir.'tags') || filewritable(dir.'tags'))
  133. silent! execute 'helptags' pathogen#fnameescape(dir)
  134. endif
  135. endfor
  136. endfor
  137. endfunction
  138. command! -bar Helptags :call pathogen#helptags()
  139. " Execute the given command. This is basically a backdoor for --remote-expr.
  140. function! pathogen#execute(...) abort
  141. for command in a:000
  142. execute command
  143. endfor
  144. return ''
  145. endfunction
  146. " Section: Unofficial
  147. function! pathogen#is_absolute(path) abort
  148. return a:path =~# (has('win32') ? '^\%([\\/]\|\w:\)[\\/]\|^[~$]' : '^[/~$]')
  149. endfunction
  150. " Given a string, returns all possible permutations of comma delimited braced
  151. " alternatives of that string. pathogen#expand('/{a,b}/{c,d}') yields
  152. " ['/a/c', '/a/d', '/b/c', '/b/d']. Empty braces are treated as a wildcard
  153. " and globbed. Actual globs are preserved.
  154. function! pathogen#expand(pattern) abort
  155. if a:pattern =~# '{[^{}]\+}'
  156. let [pre, pat, post] = split(substitute(a:pattern, '\(.\{-\}\){\([^{}]\+\)}\(.*\)', "\\1\001\\2\001\\3", ''), "\001", 1)
  157. let found = map(split(pat, ',', 1), 'pre.v:val.post')
  158. let results = []
  159. for pattern in found
  160. call extend(results, pathogen#expand(pattern))
  161. endfor
  162. return results
  163. elseif a:pattern =~# '{}'
  164. let pat = matchstr(a:pattern, '^.*{}[^*]*\%($\|[\\/]\)')
  165. let post = a:pattern[strlen(pat) : -1]
  166. return map(split(glob(substitute(pat, '{}', '*', 'g')), "\n"), 'v:val.post')
  167. else
  168. return [a:pattern]
  169. endif
  170. endfunction
  171. " \ on Windows unless shellslash is set, / everywhere else.
  172. function! pathogen#slash() abort
  173. return !exists("+shellslash") || &shellslash ? '/' : '\'
  174. endfunction
  175. function! pathogen#separator() abort
  176. return pathogen#slash()
  177. endfunction
  178. " Convenience wrapper around glob() which returns a list.
  179. function! pathogen#glob(pattern) abort
  180. let files = split(glob(a:pattern),"\n")
  181. return map(files,'substitute(v:val,"[".pathogen#slash()."/]$","","")')
  182. endfunction "}}}1
  183. " Like pathogen#glob(), only limit the results to directories.
  184. function! pathogen#glob_directories(pattern) abort
  185. return filter(pathogen#glob(a:pattern),'isdirectory(v:val)')
  186. endfunction "}}}1
  187. " Remove duplicates from a list.
  188. function! pathogen#uniq(list) abort
  189. let i = 0
  190. let seen = {}
  191. while i < len(a:list)
  192. if (a:list[i] ==# '' && exists('empty')) || has_key(seen,a:list[i])
  193. call remove(a:list,i)
  194. elseif a:list[i] ==# ''
  195. let i += 1
  196. let empty = 1
  197. else
  198. let seen[a:list[i]] = 1
  199. let i += 1
  200. endif
  201. endwhile
  202. return a:list
  203. endfunction
  204. " Backport of fnameescape().
  205. function! pathogen#fnameescape(string) abort
  206. if exists('*fnameescape')
  207. return fnameescape(a:string)
  208. elseif a:string ==# '-'
  209. return '\-'
  210. else
  211. return substitute(escape(a:string," \t\n*?[{`$\\%#'\"|!<"),'^[+>]','\\&','')
  212. endif
  213. endfunction
  214. " Like findfile(), but hardcoded to use the runtimepath.
  215. function! pathogen#runtime_findfile(file,count) abort "{{{1
  216. let rtp = pathogen#join(1,pathogen#split(&rtp))
  217. let file = findfile(a:file,rtp,a:count)
  218. if file ==# ''
  219. return ''
  220. else
  221. return fnamemodify(file,':p')
  222. endif
  223. endfunction
  224. " Section: Deprecated
  225. function! s:warn(msg) abort
  226. echohl WarningMsg
  227. echomsg a:msg
  228. echohl NONE
  229. endfunction
  230. " Prepend all subdirectories of path to the rtp, and append all 'after'
  231. " directories in those subdirectories. Deprecated.
  232. function! pathogen#runtime_prepend_subdirectories(path) abort
  233. call s:warn('Change pathogen#runtime_prepend_subdirectories('.string(a:path).') to pathogen#infect('.string(a:path.'/{}').')')
  234. return pathogen#surround(a:path . pathogen#slash() . '{}')
  235. endfunction
  236. function! pathogen#incubate(...) abort
  237. let name = a:0 ? a:1 : 'bundle/{}'
  238. call s:warn('Change pathogen#incubate('.(a:0 ? string(a:1) : '').') to pathogen#infect('.string(name).')')
  239. return pathogen#interpose(name)
  240. endfunction
  241. " Deprecated alias for pathogen#interpose().
  242. function! pathogen#runtime_append_all_bundles(...) abort
  243. if a:0
  244. call s:warn('Change pathogen#runtime_append_all_bundles('.string(a:1).') to pathogen#infect('.string(a:1.'/{}').')')
  245. else
  246. call s:warn('Change pathogen#runtime_append_all_bundles() to pathogen#infect()')
  247. endif
  248. return pathogen#interpose(a:0 ? a:1 . '/{}' : 'bundle/{}')
  249. endfunction
  250. if exists(':Vedit')
  251. finish
  252. endif
  253. let s:vopen_warning = 0
  254. function! s:find(count,cmd,file,lcd)
  255. let rtp = pathogen#join(1,pathogen#split(&runtimepath))
  256. let file = pathogen#runtime_findfile(a:file,a:count)
  257. if file ==# ''
  258. return "echoerr 'E345: Can''t find file \"".a:file."\" in runtimepath'"
  259. endif
  260. if !s:vopen_warning
  261. let s:vopen_warning = 1
  262. let warning = '|echohl WarningMsg|echo "Install scriptease.vim to continue using :V'.a:cmd.'"|echohl NONE'
  263. else
  264. let warning = ''
  265. endif
  266. if a:lcd
  267. let path = file[0:-strlen(a:file)-2]
  268. execute 'lcd `=path`'
  269. return a:cmd.' '.pathogen#fnameescape(a:file) . warning
  270. else
  271. return a:cmd.' '.pathogen#fnameescape(file) . warning
  272. endif
  273. endfunction
  274. function! s:Findcomplete(A,L,P)
  275. let sep = pathogen#slash()
  276. let cheats = {
  277. \'a': 'autoload',
  278. \'d': 'doc',
  279. \'f': 'ftplugin',
  280. \'i': 'indent',
  281. \'p': 'plugin',
  282. \'s': 'syntax'}
  283. if a:A =~# '^\w[\\/]' && has_key(cheats,a:A[0])
  284. let request = cheats[a:A[0]].a:A[1:-1]
  285. else
  286. let request = a:A
  287. endif
  288. let pattern = substitute(request,'/\|\'.sep,'*'.sep,'g').'*'
  289. let found = {}
  290. for path in pathogen#split(&runtimepath)
  291. let path = expand(path, ':p')
  292. let matches = split(glob(path.sep.pattern),"\n")
  293. call map(matches,'isdirectory(v:val) ? v:val.sep : v:val')
  294. call map(matches,'expand(v:val, ":p")[strlen(path)+1:-1]')
  295. for match in matches
  296. let found[match] = 1
  297. endfor
  298. endfor
  299. return sort(keys(found))
  300. endfunction
  301. command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Ve :execute s:find(<count>,'edit<bang>',<q-args>,0)
  302. command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vedit :execute s:find(<count>,'edit<bang>',<q-args>,0)
  303. command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vopen :execute s:find(<count>,'edit<bang>',<q-args>,1)
  304. command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vsplit :execute s:find(<count>,'split',<q-args>,<bang>1)
  305. command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vvsplit :execute s:find(<count>,'vsplit',<q-args>,<bang>1)
  306. command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vtabedit :execute s:find(<count>,'tabedit',<q-args>,<bang>1)
  307. command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vpedit :execute s:find(<count>,'pedit',<q-args>,<bang>1)
  308. command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Findcomplete Vread :execute s:find(<count>,'read',<q-args>,<bang>1)
  309. " vim:set et sw=2 foldmethod=expr foldexpr=getline(v\:lnum)=~'^\"\ Section\:'?'>1'\:getline(v\:lnum)=~#'^fu'?'a1'\:getline(v\:lnum)=~#'^endf'?'s1'\:'=':