pathogen.vim 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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() or an absolute path to invoke pathogen#surround().
  17. " Curly braces are expanded with pathogen#expand(): "bundle/{}" finds all
  18. " subdirectories inside "bundle" inside all directories in the runtime path.
  19. " If no arguments are given, defaults "bundle/{}", and also "pack/{}/start/{}"
  20. " on versions of Vim without native package support.
  21. function! pathogen#infect(...) abort
  22. if a:0
  23. let paths = filter(reverse(copy(a:000)), 'type(v:val) == type("")')
  24. else
  25. let paths = ['bundle/{}', 'pack/{}/start/{}']
  26. endif
  27. if has('packages')
  28. call filter(paths, 'v:val !~# "^pack/[^/]*/start/[^/]*$"')
  29. endif
  30. let static = '^\%([$~\\/]\|\w:[\\/]\)[^{}*]*$'
  31. for path in filter(copy(paths), 'v:val =~# static')
  32. call pathogen#surround(path)
  33. endfor
  34. for path in filter(copy(paths), 'v:val !~# static')
  35. if path =~# '^\%([$~\\/]\|\w:[\\/]\)'
  36. call pathogen#surround(path)
  37. else
  38. call pathogen#interpose(path)
  39. endif
  40. endfor
  41. call pathogen#cycle_filetype()
  42. if pathogen#is_disabled($MYVIMRC)
  43. return 'finish'
  44. endif
  45. return ''
  46. endfunction
  47. " Split a path into a list.
  48. function! pathogen#split(path) abort
  49. if type(a:path) == type([]) | return a:path | endif
  50. if empty(a:path) | return [] | endif
  51. let split = split(a:path,'\\\@<!\%(\\\\\)*\zs,')
  52. return map(split,'substitute(v:val,''\\\([\\,]\)'',''\1'',"g")')
  53. endfunction
  54. " Convert a list to a path.
  55. function! pathogen#join(...) abort
  56. if type(a:1) == type(1) && a:1
  57. let i = 1
  58. let space = ' '
  59. else
  60. let i = 0
  61. let space = ''
  62. endif
  63. let path = ""
  64. while i < a:0
  65. if type(a:000[i]) == type([])
  66. let list = a:000[i]
  67. let j = 0
  68. while j < len(list)
  69. let escaped = substitute(list[j],'[,'.space.']\|\\[\,'.space.']\@=','\\&','g')
  70. let path .= ',' . escaped
  71. let j += 1
  72. endwhile
  73. else
  74. let path .= "," . a:000[i]
  75. endif
  76. let i += 1
  77. endwhile
  78. return substitute(path,'^,','','')
  79. endfunction
  80. " Convert a list to a path with escaped spaces for 'path', 'tag', etc.
  81. function! pathogen#legacyjoin(...) abort
  82. return call('pathogen#join',[1] + a:000)
  83. endfunction
  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')
  87. filetype off
  88. filetype on
  89. endif
  90. endfunction
  91. " Check if a bundle is disabled. A bundle is considered disabled if its
  92. " basename or full name is included in the list g:pathogen_blacklist or the
  93. " comma delimited environment variable $VIMBLACKLIST.
  94. function! pathogen#is_disabled(path) abort
  95. if a:path =~# '\~$'
  96. return 1
  97. endif
  98. let sep = pathogen#slash()
  99. let blacklist = get(g:, 'pathogen_blacklist', get(g:, 'pathogen_disabled', [])) + pathogen#split($VIMBLACKLIST)
  100. if !empty(blacklist)
  101. call map(blacklist, 'substitute(v:val, "[\\/]$", "", "")')
  102. endif
  103. return index(blacklist, fnamemodify(a:path, ':t')) != -1 || index(blacklist, a:path) != -1
  104. endfunction
  105. " Prepend the given directory to the runtime path and append its corresponding
  106. " after directory. Curly braces are expanded with pathogen#expand().
  107. function! pathogen#surround(path) abort
  108. let sep = pathogen#slash()
  109. let rtp = pathogen#split(&rtp)
  110. let path = fnamemodify(a:path, ':s?[\\/]\=$??')
  111. let before = filter(pathogen#expand(path), '!pathogen#is_disabled(v:val)')
  112. let after = filter(reverse(pathogen#expand(path, sep.'after')), '!pathogen#is_disabled(v:val[0:-7])')
  113. call filter(rtp, 'index(before + after, v:val) == -1')
  114. let &rtp = pathogen#join(before, rtp, after)
  115. return &rtp
  116. endfunction
  117. " For each directory in the runtime path, add a second entry with the given
  118. " argument appended. Curly braces are expanded with pathogen#expand().
  119. function! pathogen#interpose(name) abort
  120. let sep = pathogen#slash()
  121. let name = a:name
  122. if has_key(s:done_bundles, name)
  123. return ""
  124. endif
  125. let s:done_bundles[name] = 1
  126. let list = []
  127. for dir in pathogen#split(&rtp)
  128. if dir =~# '\<after$'
  129. let list += reverse(filter(pathogen#expand(dir[0:-6].name, sep.'after'), '!pathogen#is_disabled(v:val[0:-7])')) + [dir]
  130. else
  131. let list += [dir] + filter(pathogen#expand(dir.sep.name), '!pathogen#is_disabled(v:val)')
  132. endif
  133. endfor
  134. let &rtp = pathogen#join(pathogen#uniq(list))
  135. return 1
  136. endfunction
  137. let s:done_bundles = {}
  138. " Invoke :helptags on all non-$VIM doc directories in runtimepath.
  139. function! pathogen#helptags() abort
  140. let sep = pathogen#slash()
  141. for glob in pathogen#split(&rtp)
  142. for dir in map(split(glob(glob), "\n"), 'v:val.sep."/doc/".sep')
  143. if (dir)[0 : strlen($VIMRUNTIME)] !=# $VIMRUNTIME.sep && filewritable(dir) == 2 && !empty(split(glob(dir.'*.txt'))) && (!filereadable(dir.'tags') || filewritable(dir.'tags'))
  144. silent! execute 'helptags' pathogen#fnameescape(dir)
  145. endif
  146. endfor
  147. endfor
  148. endfunction
  149. command! -bar Helptags :call pathogen#helptags()
  150. " Execute the given command. This is basically a backdoor for --remote-expr.
  151. function! pathogen#execute(...) abort
  152. for command in a:000
  153. execute command
  154. endfor
  155. return ''
  156. endfunction
  157. " Section: Unofficial
  158. function! pathogen#is_absolute(path) abort
  159. return a:path =~# (has('win32') ? '^\%([\\/]\|\w:\)[\\/]\|^[~$]' : '^[/~$]')
  160. endfunction
  161. " Given a string, returns all possible permutations of comma delimited braced
  162. " alternatives of that string. pathogen#expand('/{a,b}/{c,d}') yields
  163. " ['/a/c', '/a/d', '/b/c', '/b/d']. Empty braces are treated as a wildcard
  164. " and globbed. Actual globs are preserved.
  165. function! pathogen#expand(pattern, ...) abort
  166. let after = a:0 ? a:1 : ''
  167. let pattern = substitute(a:pattern, '^[~$][^\/]*', '\=expand(submatch(0))', '')
  168. if pattern =~# '{[^{}]\+}'
  169. let [pre, pat, post] = split(substitute(pattern, '\(.\{-\}\){\([^{}]\+\)}\(.*\)', "\\1\001\\2\001\\3", ''), "\001", 1)
  170. let found = map(split(pat, ',', 1), 'pre.v:val.post')
  171. let results = []
  172. for pattern in found
  173. call extend(results, pathogen#expand(pattern))
  174. endfor
  175. elseif pattern =~# '{}'
  176. let pat = matchstr(pattern, '^.*{}[^*]*\%($\|[\\/]\)')
  177. let post = pattern[strlen(pat) : -1]
  178. let results = map(split(glob(substitute(pat, '{}', '*', 'g')), "\n"), 'v:val.post')
  179. else
  180. let results = [pattern]
  181. endif
  182. let vf = pathogen#slash() . 'vimfiles'
  183. call map(results, 'v:val =~# "\\*" ? v:val.after : isdirectory(v:val.vf.after) ? v:val.vf.after : isdirectory(v:val.after) ? v:val.after : ""')
  184. return filter(results, '!empty(v:val)')
  185. endfunction
  186. " \ on Windows unless shellslash is set, / everywhere else.
  187. function! pathogen#slash() abort
  188. return !exists("+shellslash") || &shellslash ? '/' : '\'
  189. endfunction
  190. function! pathogen#separator() abort
  191. return pathogen#slash()
  192. endfunction
  193. " Convenience wrapper around glob() which returns a list.
  194. function! pathogen#glob(pattern) abort
  195. let files = split(glob(a:pattern),"\n")
  196. return map(files,'substitute(v:val,"[".pathogen#slash()."/]$","","")')
  197. endfunction
  198. " Like pathogen#glob(), only limit the results to directories.
  199. function! pathogen#glob_directories(pattern) abort
  200. return filter(pathogen#glob(a:pattern),'isdirectory(v:val)')
  201. endfunction
  202. " Remove duplicates from a list.
  203. function! pathogen#uniq(list) abort
  204. let i = 0
  205. let seen = {}
  206. while i < len(a:list)
  207. if (a:list[i] ==# '' && exists('empty')) || has_key(seen,a:list[i])
  208. call remove(a:list,i)
  209. elseif a:list[i] ==# ''
  210. let i += 1
  211. let empty = 1
  212. else
  213. let seen[a:list[i]] = 1
  214. let i += 1
  215. endif
  216. endwhile
  217. return a:list
  218. endfunction
  219. " Backport of fnameescape().
  220. function! pathogen#fnameescape(string) abort
  221. if exists('*fnameescape')
  222. return fnameescape(a:string)
  223. elseif a:string ==# '-'
  224. return '\-'
  225. else
  226. return substitute(escape(a:string," \t\n*?[{`$\\%#'\"|!<"),'^[+>]','\\&','')
  227. endif
  228. endfunction
  229. " Like findfile(), but hardcoded to use the runtimepath.
  230. function! pathogen#runtime_findfile(file,count) abort
  231. let rtp = pathogen#join(1,pathogen#split(&rtp))
  232. let file = findfile(a:file,rtp,a:count)
  233. if file ==# ''
  234. return ''
  235. else
  236. return fnamemodify(file,':p')
  237. endif
  238. endfunction
  239. " vim:set et sw=2 foldmethod=expr foldexpr=getline(v\:lnum)=~'^\"\ Section\:'?'>1'\:getline(v\:lnum)=~#'^fu'?'a1'\:getline(v\:lnum)=~#'^endf'?'s1'\:'=':