707
|
1 " =============================================================================
|
|
2 " File: autoload/ctrlp/mrufiles.vim
|
|
3 " Description: Most Recently Used Files extension
|
|
4 " Author: Kien Nguyen <github.com/kien>
|
|
5 " =============================================================================
|
|
6
|
|
7 " Static variables {{{1
|
|
8 let [s:mrbs, s:mrufs] = [[], []]
|
|
9 let s:mruf_map_string = '!stridx(v:val, cwd) ? strpart(v:val, idx) : v:val'
|
|
10
|
|
11 fu! ctrlp#mrufiles#opts()
|
|
12 let [pref, opts] = ['g:ctrlp_mruf_', {
|
|
13 \ 'max': ['s:max', 250],
|
|
14 \ 'include': ['s:in', ''],
|
|
15 \ 'exclude': ['s:ex', ''],
|
|
16 \ 'case_sensitive': ['s:cseno', 1],
|
|
17 \ 'relative': ['s:re', 0],
|
|
18 \ 'save_on_update': ['s:soup', 1],
|
|
19 \ 'map_string': ['g:ctrlp_mruf_map_string', s:mruf_map_string],
|
|
20 \ }]
|
|
21 for [ke, va] in items(opts)
|
|
22 let [{va[0]}, {pref.ke}] = [pref.ke, exists(pref.ke) ? {pref.ke} : va[1]]
|
|
23 endfo
|
|
24 endf
|
|
25 cal ctrlp#mrufiles#opts()
|
|
26 " Utilities {{{1
|
|
27 fu! s:excl(fn)
|
|
28 retu !empty({s:ex}) && a:fn =~# {s:ex}
|
|
29 endf
|
|
30
|
|
31 fu! s:mergelists()
|
|
32 let diskmrufs = ctrlp#utils#readfile(ctrlp#mrufiles#cachefile())
|
|
33 cal filter(diskmrufs, 'index(s:mrufs, v:val) < 0')
|
|
34 let mrufs = s:mrufs + diskmrufs
|
|
35 retu s:chop(mrufs)
|
|
36 endf
|
|
37
|
|
38 fu! s:chop(mrufs)
|
|
39 if len(a:mrufs) > {s:max} | cal remove(a:mrufs, {s:max}, -1) | en
|
|
40 retu a:mrufs
|
|
41 endf
|
|
42
|
|
43 fu! s:reformat(mrufs, ...)
|
|
44 let cwd = getcwd()
|
|
45 let cwd .= cwd !~ '[\/]$' ? ctrlp#utils#lash() : ''
|
|
46 if {s:re}
|
|
47 let cwd = exists('+ssl') ? tr(cwd, '/', '\') : cwd
|
|
48 cal filter(a:mrufs, '!stridx(v:val, cwd)')
|
|
49 en
|
|
50 if a:0 && a:1 == 'raw' | retu a:mrufs | en
|
|
51 let idx = strlen(cwd)
|
|
52 if exists('+ssl') && &ssl
|
|
53 let cwd = tr(cwd, '\', '/')
|
|
54 cal map(a:mrufs, 'tr(v:val, "\\", "/")')
|
|
55 en
|
|
56 retu map(a:mrufs, g:ctrlp_mruf_map_string)
|
|
57 endf
|
|
58
|
|
59 fu! s:record(bufnr)
|
|
60 if s:locked | retu | en
|
|
61 let bufnr = a:bufnr + 0
|
|
62 let bufname = bufname(bufnr)
|
|
63 if bufnr > 0 && !empty(bufname)
|
|
64 cal filter(s:mrbs, 'v:val != bufnr')
|
|
65 cal insert(s:mrbs, bufnr)
|
|
66 cal s:addtomrufs(bufname)
|
|
67 en
|
|
68 endf
|
|
69
|
|
70 fu! s:addtomrufs(fname)
|
|
71 let fn = fnamemodify(a:fname, get(g:, 'ctrlp_tilde_homedir', 0) ? ':p:~' : ':p')
|
|
72 let fn = exists('+ssl') ? tr(fn, '/', '\') : fn
|
|
73 let abs_fn = fnamemodify(fn,':p')
|
|
74 if ( !empty({s:in}) && fn !~# {s:in} ) || ( !empty({s:ex}) && fn =~# {s:ex} )
|
|
75 \ || !empty(getbufvar('^' . abs_fn . '$', '&bt')) || !filereadable(abs_fn)
|
|
76 retu
|
|
77 en
|
|
78 let idx = index(s:mrufs, fn, 0, !{s:cseno})
|
|
79 if idx
|
|
80 cal filter(s:mrufs, 'v:val !='.( {s:cseno} ? '#' : '?' ).' fn')
|
|
81 cal insert(s:mrufs, fn)
|
|
82 if {s:soup} && idx < 0
|
|
83 cal s:savetofile(s:mergelists())
|
|
84 en
|
|
85 en
|
|
86 endf
|
|
87
|
|
88 fu! s:savetofile(mrufs)
|
|
89 cal ctrlp#utils#writecache(a:mrufs, s:cadir, s:cafile)
|
|
90 endf
|
|
91 " Public {{{1
|
|
92 fu! ctrlp#mrufiles#refresh(...)
|
|
93 let mrufs = s:mergelists()
|
|
94 cal filter(mrufs, '!empty(ctrlp#utils#glob(v:val, 1)) && !s:excl(v:val)')
|
|
95 if exists('+ssl')
|
|
96 cal map(mrufs, 'tr(v:val, "/", "\\")')
|
|
97 cal map(s:mrufs, 'tr(v:val, "/", "\\")')
|
|
98 let cond = 'count(mrufs, v:val, !{s:cseno}) == 1'
|
|
99 cal filter(mrufs, cond)
|
|
100 cal filter(s:mrufs, cond)
|
|
101 en
|
|
102 cal s:savetofile(mrufs)
|
|
103 retu a:0 && a:1 == 'raw' ? [] : s:reformat(mrufs)
|
|
104 endf
|
|
105
|
|
106 fu! ctrlp#mrufiles#remove(files)
|
|
107 let mrufs = []
|
|
108 if a:files != []
|
|
109 let mrufs = s:mergelists()
|
|
110 let cond = 'index(a:files, v:val, 0, !{s:cseno}) < 0'
|
|
111 cal filter(mrufs, cond)
|
|
112 cal filter(s:mrufs, cond)
|
|
113 en
|
|
114 cal s:savetofile(mrufs)
|
|
115 retu s:reformat(mrufs)
|
|
116 endf
|
|
117
|
|
118 fu! ctrlp#mrufiles#add(fn)
|
|
119 if !empty(a:fn)
|
|
120 cal s:addtomrufs(a:fn)
|
|
121 en
|
|
122 endf
|
|
123
|
|
124 fu! ctrlp#mrufiles#list(...)
|
|
125 retu a:0 ? a:1 == 'raw' ? s:reformat(s:mergelists(), a:1) : 0
|
|
126 \ : s:reformat(s:mergelists())
|
|
127 endf
|
|
128
|
|
129 fu! ctrlp#mrufiles#bufs()
|
|
130 retu s:mrbs
|
|
131 endf
|
|
132
|
|
133 fu! ctrlp#mrufiles#tgrel()
|
|
134 let {s:re} = !{s:re}
|
|
135 endf
|
|
136
|
|
137 fu! ctrlp#mrufiles#cachefile()
|
|
138 if !exists('s:cadir') || !exists('s:cafile')
|
|
139 let s:cadir = ctrlp#utils#cachedir().ctrlp#utils#lash().'mru'
|
|
140 let s:cafile = s:cadir.ctrlp#utils#lash().'cache.txt'
|
|
141 en
|
|
142 retu s:cafile
|
|
143 endf
|
|
144
|
|
145 fu! ctrlp#mrufiles#init()
|
|
146 if !has('autocmd') | retu | en
|
|
147 let s:locked = 0
|
|
148 aug CtrlPMRUF
|
|
149 au!
|
|
150 au BufWinEnter,BufWinLeave,BufWritePost * cal s:record(expand('<abuf>', 1))
|
|
151 au QuickFixCmdPre *vimgrep* let s:locked = 1
|
|
152 au QuickFixCmdPost *vimgrep* let s:locked = 0
|
|
153 au VimLeavePre * cal s:savetofile(s:mergelists())
|
|
154 aug END
|
|
155 endf
|
|
156 "}}}
|
|
157
|
|
158 " vim:fen:fdm=marker:fmr={{{,}}}:fdl=0:fdc=1:ts=2:sw=2:sts=2
|