utils.vim 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. let s:lash = ctrlp#utils#lash()
  11. fu! s:lash(...)
  12. retu match(a:0 ? a:1 : getcwd(), '[\/]$') < 0 ? s:lash : ''
  13. endf
  14. fu! ctrlp#utils#opts()
  15. let usrhome = $HOME.s:lash($HOME)
  16. let cahome = exists('$XDG_CACHE_HOME') ? $XDG_CACHE_HOME : usrhome.'.cache'
  17. let s:cache_dir = isdirectory(usrhome.'.ctrlp_cache')
  18. \ ? usrhome.'.ctrlp_cache' : cahome.s:lash(cahome).'ctrlp'
  19. if exists('g:ctrlp_cache_dir')
  20. let s:cache_dir = expand(g:ctrlp_cache_dir, 1)
  21. if isdirectory(s:cache_dir.s:lash(s:cache_dir).'.ctrlp_cache')
  22. let s:cache_dir = s:cache_dir.s:lash(s:cache_dir).'.ctrlp_cache'
  23. en
  24. en
  25. endf
  26. cal ctrlp#utils#opts()
  27. " Files and Directories {{{1
  28. fu! ctrlp#utils#cachedir()
  29. retu s:cache_dir
  30. endf
  31. fu! ctrlp#utils#cachefile(...)
  32. let [tail, dir] = [a:0 == 1 ? '.'.a:1 : '', a:0 == 2 ? a:1 : getcwd()]
  33. let cache_file = substitute(dir, '\([\/]\|^\a\zs:\)', '%', 'g').tail.'.txt'
  34. retu a:0 == 1 ? cache_file : s:cache_dir.s:lash(s:cache_dir).cache_file
  35. endf
  36. fu! ctrlp#utils#readfile(file)
  37. if filereadable(a:file)
  38. let data = readfile(a:file)
  39. if empty(data) || type(data) != 3
  40. unl data
  41. let data = []
  42. en
  43. retu data
  44. en
  45. retu []
  46. endf
  47. fu! ctrlp#utils#mkdir(dir)
  48. if exists('*mkdir') && !isdirectory(a:dir)
  49. sil! cal mkdir(a:dir, 'p')
  50. en
  51. retu a:dir
  52. endf
  53. fu! ctrlp#utils#writecache(lines, ...)
  54. if isdirectory(ctrlp#utils#mkdir(a:0 ? a:1 : s:cache_dir))
  55. sil! cal writefile(a:lines, a:0 >= 2 ? a:2 : ctrlp#utils#cachefile())
  56. en
  57. endf
  58. fu! ctrlp#utils#glob(...)
  59. let cond = v:version > 702 || ( v:version == 702 && has('patch051') )
  60. retu call('glob', cond ? a:000 : [a:1])
  61. endf
  62. "}}}
  63. " vim:fen:fdm=marker:fmr={{{,}}}:fdl=0:fdc=1:ts=2:sw=2:sts=2