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