1
0

utils.vim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. " =============================================================================
  2. " File: autoload/ctrlp/utils.vim
  3. " Description: Utilities
  4. " Author: Kien Nguyen <github.com/kien>
  5. " =============================================================================
  6. " Static variables {{{1
  7. fu! ctrlp#utils#lash()
  8. retu &ssl || !exists('+ssl') ? '/' : '\'
  9. endf
  10. fu! s:lash(...)
  11. retu ( a:0 ? a:1 : getcwd() ) !~ '[\/]$' ? s:lash : ''
  12. endf
  13. fu! ctrlp#utils#opts()
  14. let s:lash = ctrlp#utils#lash()
  15. let usrhome = $HOME . s:lash( $HOME )
  16. let cahome = exists('$XDG_CACHE_HOME') ? $XDG_CACHE_HOME : usrhome.'.cache'
  17. let cadir = isdirectory(usrhome.'.ctrlp_cache')
  18. \ ? usrhome.'.ctrlp_cache' : cahome.s:lash(cahome).'ctrlp'
  19. if exists('g:ctrlp_cache_dir')
  20. let cadir = expand(g:ctrlp_cache_dir, 1)
  21. if isdirectory(cadir.s:lash(cadir).'.ctrlp_cache')
  22. let cadir = cadir.s:lash(cadir).'.ctrlp_cache'
  23. en
  24. en
  25. let s:cache_dir = cadir
  26. endf
  27. cal ctrlp#utils#opts()
  28. let s:wig_cond = v:version > 702 || ( v:version == 702 && has('patch051') )
  29. " Files and Directories {{{1
  30. fu! ctrlp#utils#cachedir()
  31. retu s:cache_dir
  32. endf
  33. fu! ctrlp#utils#cachefile(...)
  34. let [tail, dir] = [a:0 == 1 ? '.'.a:1 : '', a:0 == 2 ? a:1 : getcwd()]
  35. let cache_file = substitute(dir, '\([\/]\|^\a\zs:\)', '%', 'g').tail.'.txt'
  36. retu a:0 == 1 ? cache_file : s:cache_dir.s:lash(s:cache_dir).cache_file
  37. endf
  38. fu! ctrlp#utils#readfile(file)
  39. if filereadable(a:file)
  40. let data = readfile(a:file)
  41. if empty(data) || type(data) != 3
  42. unl data
  43. let data = []
  44. en
  45. retu data
  46. en
  47. retu []
  48. endf
  49. fu! ctrlp#utils#mkdir(dir)
  50. if exists('*mkdir') && !isdirectory(a:dir)
  51. sil! cal mkdir(a:dir, 'p')
  52. en
  53. retu a:dir
  54. endf
  55. fu! ctrlp#utils#writecache(lines, ...)
  56. if isdirectory(ctrlp#utils#mkdir(a:0 ? a:1 : s:cache_dir))
  57. sil! cal writefile(a:lines, a:0 >= 2 ? a:2 : ctrlp#utils#cachefile())
  58. en
  59. endf
  60. fu! ctrlp#utils#glob(...)
  61. let path = ctrlp#utils#fnesc(a:1, 'g')
  62. retu s:wig_cond ? glob(path, a:2) : glob(path)
  63. endf
  64. fu! ctrlp#utils#globpath(...)
  65. retu call('globpath', s:wig_cond ? a:000 : a:000[:1])
  66. endf
  67. fu! ctrlp#utils#fnesc(path, type, ...)
  68. if exists('*fnameescape')
  69. if exists('+ssl')
  70. if a:type == 'c'
  71. let path = escape(a:path, '%#')
  72. elsei a:type == 'f'
  73. let path = fnameescape(a:path)
  74. elsei a:type == 'g'
  75. let path = escape(a:path, '?*')
  76. en
  77. let path = substitute(path, '[', '[[]', 'g')
  78. el
  79. let path = fnameescape(a:path)
  80. en
  81. el
  82. if exists('+ssl')
  83. if a:type == 'c'
  84. let path = escape(a:path, '%#')
  85. elsei a:type == 'f'
  86. let path = escape(a:path, " \t\n%#*?|<\"")
  87. elsei a:type == 'g'
  88. let path = escape(a:path, '?*')
  89. en
  90. let path = substitute(path, '[', '[[]', 'g')
  91. el
  92. let path = escape(a:path, " \t\n*?[{`$\\%#'\"|!<")
  93. en
  94. en
  95. retu a:0 ? escape(path, a:1) : path
  96. endf
  97. "}}}
  98. " vim:fen:fdm=marker:fmr={{{,}}}:fdl=0:fdc=1:ts=2:sw=2:sts=2