comparison vim/vimfiles/autoload/ctrlp.vim @ 0:48859d9c82c5

Initial commit, based on settings used at work (minus NERD tree & some tagging plugins).
author Brian Neal <bgneal@gmail.com>
date Tue, 03 Jan 2012 20:53:13 -0600
parents
children 92af3257a261
comparison
equal deleted inserted replaced
-1:000000000000 0:48859d9c82c5
1 " =============================================================================
2 " File: autoload/ctrlp.vim
3 " Description: Fuzzy file, buffer, mru and tag finder.
4 " Author: Kien Nguyen <github.com/kien>
5 " Version: 1.6.4
6 " =============================================================================
7
8 " Static variables {{{1
9 fu! s:opts()
10 let hst = exists('+hi') ? &hi : 20
11 let opts = {
12 \ 'g:ctrlp_by_filename': ['s:byfname', 0],
13 \ 'g:ctrlp_clear_cache_on_exit': ['s:clrex', 1],
14 \ 'g:ctrlp_dont_split': ['s:nosplit', ''],
15 \ 'g:ctrlp_dotfiles': ['s:dotfiles', 1],
16 \ 'g:ctrlp_extensions': ['s:extensions', []],
17 \ 'g:ctrlp_follow_symlinks': ['s:folsym', 0],
18 \ 'g:ctrlp_highlight_match': ['s:mathi', [1, 'Identifier']],
19 \ 'g:ctrlp_lazy_update': ['s:lazy', 0],
20 \ 'g:ctrlp_jump_to_buffer': ['s:jmptobuf', 1],
21 \ 'g:ctrlp_match_window_bottom': ['s:mwbottom', 1],
22 \ 'g:ctrlp_match_window_reversed': ['s:mwreverse', 1],
23 \ 'g:ctrlp_max_depth': ['s:maxdepth', 40],
24 \ 'g:ctrlp_max_files': ['s:maxfiles', 20000],
25 \ 'g:ctrlp_max_height': ['s:mxheight', 10],
26 \ 'g:ctrlp_max_history': ['s:maxhst', hst],
27 \ 'g:ctrlp_open_multi': ['s:opmul', '1v'],
28 \ 'g:ctrlp_open_new_file': ['s:newfop', 3],
29 \ 'g:ctrlp_prompt_mappings': ['s:urprtmaps', 0],
30 \ 'g:ctrlp_regexp_search': ['s:regexp', 0],
31 \ 'g:ctrlp_root_markers': ['s:rmarkers', []],
32 \ 'g:ctrlp_split_window': ['s:splitwin', 0],
33 \ 'g:ctrlp_use_caching': ['s:caching', 1],
34 \ 'g:ctrlp_use_migemo': ['s:migemo', 0],
35 \ 'g:ctrlp_user_command': ['s:usrcmd', ''],
36 \ 'g:ctrlp_working_path_mode': ['s:pathmode', 2],
37 \ }
38 for [ke, va] in items(opts)
39 exe 'let' va[0] '=' string(exists(ke) ? eval(ke) : va[1])
40 endfo
41 if !exists('g:ctrlp_newcache') | let g:ctrlp_newcache = 0 | en
42 let s:glob = s:dotfiles ? '.*\|*' : '*'
43 let s:maxdepth = min([s:maxdepth, 100])
44 let g:ctrlp_builtins = 2
45 if !empty(s:extensions) | for each in s:extensions
46 exe 'ru autoload/ctrlp/'.each.'.vim'
47 endfo | en
48 endf
49 cal s:opts()
50
51 let s:lash = ctrlp#utils#lash()
52
53 " Global options
54 let s:glbs = { 'magic': 1, 'to': 1, 'tm': 0, 'sb': 1, 'hls': 0, 'im': 0,
55 \ 'report': 9999, 'sc': 0, 'ss': 0, 'siso': 0, 'mfd': 200, 'mouse': 'n',
56 \ 'gcr': 'a:block-PmenuSel-blinkon0' }
57
58 if s:lazy
59 cal extend(s:glbs, { 'ut': ( s:lazy > 1 ? s:lazy : 250 ) })
60 en
61
62 " Limiters
63 let [s:compare_lim, s:nocache_lim, s:mltipats_lim] = [3000, 4000, 2000]
64 " * Open & Close {{{1
65 fu! s:Open()
66 let [s:cwd, s:winres] = [getcwd(), winrestcmd()]
67 let [s:crfile, s:crfpath] = [expand('%:p', 1), expand('%:p:h', 1)]
68 let [s:crword, s:crline] = [expand('<cword>'), getline('.')]
69 let [s:tagfiles, s:crcursor] = [s:tagfiles(), getpos('.')]
70 let [s:crbufnr, s:crvisual] = [bufnr('%'), s:lastvisual()]
71 let s:currwin = s:mwbottom ? winnr() : winnr() + has('autocmd')
72 sil! exe s:mwbottom ? 'bo' : 'to' '1new ControlP'
73 let [s:bufnr, s:prompt] = [bufnr('%'), ['', '', '']]
74 abc <buffer>
75 if !exists('s:hstry')
76 let hst = filereadable(s:gethistloc()[1]) ? s:gethistdata() : ['']
77 let s:hstry = empty(hst) || !s:maxhst ? [''] : hst
78 en
79 for [ke, va] in items(s:glbs)
80 sil! exe 'let s:glb_'.ke.' = &'.ke.' | let &'.ke.' = '.string(va)
81 endfo
82 if s:opmul && has('signs')
83 sign define ctrlpmark text=+> texthl=Search
84 en
85 cal s:setupblank()
86 endf
87
88 fu! s:Close()
89 try | bun! | cat | clo! | endt
90 cal s:unmarksigns()
91 for key in keys(s:glbs)
92 sil! exe 'let &'.key.' = s:glb_'.key
93 endfo
94 if exists('s:glb_acd') | let &acd = s:glb_acd | en
95 let [g:ctrlp_lines, g:ctrlp_allfiles] = [[], []]
96 exe s:winres
97 unl! s:focus s:hisidx s:hstgot s:marked s:statypes s:cline s:init s:savestr
98 \ s:crfile s:crfpath s:crword s:crvisual s:tagfiles s:crline s:crcursor
99 \ g:ctrlp_nolimit s:crbufnr
100 cal ctrlp#recordhist()
101 ec
102 endf
103 " * Clear caches {{{1
104 fu! ctrlp#clr(...)
105 exe 'let g:ctrlp_new'.( exists('a:1') ? a:1 : 'cache' ).' = 1'
106 endf
107
108 fu! ctrlp#clra(...)
109 if !exists('a:1') && ( has('dialog_gui') || has('dialog_con') ) &&
110 \ confirm("Delete all cache files?", "&OK\n&Cancel") != 1 | retu | en
111 let cache_dir = ctrlp#utils#cachedir()
112 if isdirectory(cache_dir)
113 let cache_files = split(s:glbpath(cache_dir, '**', 1), "\n")
114 cal filter(cache_files, '!isdirectory(v:val) && v:val !~ ''\<cache\.txt$''')
115 sil! cal map(cache_files, 'delete(v:val)')
116 en
117 cal ctrlp#clr()
118 endf
119
120 fu! ctrlp#reset()
121 if ( has('dialog_gui') || has('dialog_con') ) &&
122 \ confirm("Reset and apply new options?", "&OK\n&Cancel") != 1 | retu | en
123 cal s:opts()
124 cal ctrlp#utils#opts()
125 cal ctrlp#mrufiles#opts()
126 unl! s:cline
127 endf
128 " * Files() {{{1
129 fu! s:GlobPath(dirs, allfiles, depth)
130 let entries = split(globpath(a:dirs, s:glob), "\n")
131 if !s:folsym
132 let entries = filter(entries, 'getftype(v:val) != "link"')
133 en
134 let g:ctrlp_allfiles = filter(copy(entries), '!isdirectory(v:val)')
135 let ftrfunc = s:dotfiles ? 'ctrlp#dirfilter(v:val)' : 'isdirectory(v:val)'
136 let alldirs = filter(entries, ftrfunc)
137 cal extend(g:ctrlp_allfiles, a:allfiles, 0)
138 let depth = a:depth + 1
139 if !empty(alldirs) && !s:maxfiles(len(g:ctrlp_allfiles)) && depth <= s:maxdepth
140 sil! cal ctrlp#progress(len(g:ctrlp_allfiles))
141 cal s:GlobPath(join(alldirs, ','), g:ctrlp_allfiles, depth)
142 en
143 endf
144
145 fu! s:UserCommand(path, lscmd)
146 let path = a:path
147 if exists('+ssl') && &ssl
148 let [ssl, &ssl, path] = [&ssl, 0, tr(path, '/', '\')]
149 en
150 let path = exists('*shellescape') ? shellescape(path) : path
151 let g:ctrlp_allfiles = split(system(printf(a:lscmd, path)), "\n")
152 if exists('+ssl') && exists('ssl')
153 let &ssl = ssl
154 cal map(g:ctrlp_allfiles, 'tr(v:val, "\\", "/")')
155 en
156 if exists('s:vcscmd') && s:vcscmd
157 cal map(g:ctrlp_allfiles, 'tr(v:val, "/", "\\")')
158 en
159 endf
160
161 fu! s:Files()
162 let [cwd, cache_file] = [getcwd(), ctrlp#utils#cachefile()]
163 if g:ctrlp_newcache || !filereadable(cache_file) || !s:caching
164 let lscmd = s:lscommand()
165 " Get the list of files
166 if empty(lscmd)
167 cal s:GlobPath(cwd, [], 0)
168 el
169 sil! cal ctrlp#progress('Waiting...')
170 try | cal s:UserCommand(cwd, lscmd) | cat | retu [] | endt
171 en
172 " Remove base directory
173 cal ctrlp#rmbasedir(g:ctrlp_allfiles)
174 let read_cache = 0
175 el
176 let g:ctrlp_allfiles = ctrlp#utils#readfile(cache_file)
177 let read_cache = 1
178 en
179 if len(g:ctrlp_allfiles) <= s:compare_lim
180 cal sort(g:ctrlp_allfiles, 'ctrlp#complen')
181 en
182 cal s:writecache(read_cache, cache_file)
183 retu g:ctrlp_allfiles
184 endf
185 fu! s:Buffers() "{{{1
186 let allbufs = []
187 for each in range(1, bufnr('$'))
188 if getbufvar(each, '&bl') && each != bufnr('#')
189 let bufname = bufname(each)
190 if strlen(bufname) && getbufvar(each, '&ma') && bufname != 'ControlP'
191 cal add(allbufs, fnamemodify(bufname, ':p'))
192 en
193 en
194 endfo
195 retu allbufs
196 endf
197 " * MatchedItems() {{{1
198 fu! s:MatchIt(items, pat, limit, ispathitem)
199 let [items, pat, limit, newitems] = [a:items, a:pat, a:limit, []]
200 let mfunc = s:byfname && a:ispathitem ? 's:matchfname'
201 \ : s:itemtype > 2 && len(items) < 30000 && !a:ispathitem ? 's:matchtab'
202 \ : 'match'
203 for item in items
204 if call(mfunc, [item, pat]) >= 0 | cal add(newitems, item) | en
205 if limit > 0 && len(newitems) >= limit | brea | en
206 endfo
207 retu newitems
208 endf
209
210 fu! s:MatchedItems(items, pats, limit)
211 let [items, pats, limit, ipt] = [a:items, a:pats, a:limit, s:ispathitem()]
212 " If items is longer than s:mltipats_lim, use only the last pattern
213 if len(items) >= s:mltipats_lim | let pats = [pats[-1]] | en
214 cal map(pats, 'substitute(v:val, "\\\~", "\\\\\\~", "g")')
215 if !s:regexp | cal map(pats, 'escape(v:val, ".")') | en
216 " Loop through the patterns
217 for each in pats
218 " If newitems is small, set it as items to search in
219 if exists('newitems') && len(newitems) < limit
220 let items = copy(newitems)
221 en
222 if empty(items) " End here
223 retu exists('newitems') ? newitems : []
224 el " Start here, go back up if have 2 or more in pats
225 " Loop through the items
226 let newitems = s:MatchIt(items, each, limit, ipt)
227 en
228 endfo
229 let s:matches = len(newitems)
230 retu newitems
231 endf
232 fu! s:SplitPattern(str, ...) "{{{1
233 let str = s:sanstail(a:str)
234 if s:migemo && s:regexp && len(str) > 0 && executable('cmigemo')
235 let dict = s:glbpath(&rtp, printf("dict/%s/migemo-dict", &encoding), 1)
236 if !len(dict)
237 let dict = s:glbpath(&rtp, "dict/migemo-dict", 1)
238 en
239 if len(dict)
240 let [tokens, str, cmd] = [split(str, '\s'), '', 'cmigemo -v -w %s -d %s']
241 for token in tokens
242 let rtn = system(printf(cmd, shellescape(token), shellescape(dict)))
243 let str .= !v:shell_error && len(rtn) > 0 ? '.*'.rtn : token
244 endfo
245 en
246 en
247 let s:savestr = str
248 if s:regexp || match(str, '\\\(zs\|ze\|<\|>\)\|[*|]') >= 0
249 let array = [s:regexfilter(str)]
250 el
251 let array = split(str, '\zs')
252 if exists('+ssl') && !&ssl
253 cal map(array, 'substitute(v:val, "\\", "\\\\\\", "g")')
254 en
255 " Literal ^ and $
256 for each in ['^', '$']
257 cal map(array, 'substitute(v:val, "\\\'.each.'", "\\\\\\'.each.'", "g")')
258 endfo
259 en
260 " Build the new pattern
261 let nitem = !empty(array) ? array[0] : ''
262 let newpats = [nitem]
263 if len(array) > 1
264 for item in range(1, len(array) - 1)
265 " Separator
266 let sep = exists('a:1') ? a:1 : '[^'.array[item-1].']\{-}'
267 let nitem .= sep.array[item]
268 cal add(newpats, nitem)
269 endfo
270 en
271 retu newpats
272 endf
273 " * BuildPrompt() {{{1
274 fu! s:Render(lines, pat)
275 let lines = a:lines
276 " Setup the match window
277 let s:height = min([len(lines), s:mxheight])
278 sil! exe '%d _ | res' s:height
279 " Print the new items
280 if empty(lines)
281 setl nocul
282 cal setline(1, ' == NO ENTRIES ==')
283 cal s:unmarksigns()
284 if s:dohighlight() | cal clearmatches() | en
285 retu
286 en
287 setl cul
288 " Sort if not MRU
289 if ( s:itemtype != 2 && !exists('g:ctrlp_nolimit') )
290 \ || !empty(join(s:prompt, ''))
291 let s:compat = a:pat
292 cal sort(lines, 's:mixedsort')
293 unl s:compat
294 en
295 if s:mwreverse | cal reverse(lines) | en
296 let s:matched = copy(lines)
297 cal map(lines, 'substitute(v:val, "^", "> ", "")')
298 cal setline(1, lines)
299 exe 'keepj norm!' s:mwreverse ? 'G' : 'gg'
300 keepj norm! 1|
301 cal s:unmarksigns()
302 cal s:remarksigns()
303 if exists('s:cline') | cal cursor(s:cline, 1) | en
304 " Highlighting
305 if s:dohighlight()
306 cal s:highlight(a:pat, empty(s:mathi[1]) ? 'Identifier' : s:mathi[1])
307 en
308 endf
309
310 fu! s:Update(str)
311 " Get the previous string if existed
312 let oldstr = exists('s:savestr') ? s:savestr : ''
313 let pats = s:SplitPattern(a:str)
314 " Get the new string sans tail
315 let notail = substitute(a:str, ':\([^:]\|\\:\)*$', '', 'g')
316 " Stop if the string's unchanged
317 if notail == oldstr && !empty(notail) && !exists('s:force')
318 retu
319 en
320 let lines = exists('g:ctrlp_nolimit') && empty(notail) ? copy(g:ctrlp_lines)
321 \ : s:MatchedItems(g:ctrlp_lines, pats, s:mxheight)
322 cal s:Render(lines, pats[-1])
323 endf
324
325 fu! s:ForceUpdate()
326 let [estr, prt] = ['"\', copy(s:prompt)]
327 cal map(prt, 'escape(v:val, estr)')
328 cal s:Update(join(prt, ''))
329 endf
330
331 fu! s:BuildPrompt(upd, ...)
332 let base = ( s:regexp ? 'r' : '>' ).( s:byfname ? 'd' : '>' ).'> '
333 let [estr, prt] = ['"\', copy(s:prompt)]
334 cal map(prt, 'escape(v:val, estr)')
335 let str = join(prt, '')
336 let lazy = empty(str) || exists('s:force') || !has('autocmd') ? 0 : s:lazy
337 if a:upd && ( s:matches || s:regexp || match(str, '[*|]') >= 0 ) && !lazy
338 sil! cal s:Update(str)
339 en
340 sil! cal ctrlp#statusline()
341 " Toggling
342 let [hiactive, hicursor, base] = exists('a:1') && !a:1
343 \ ? ['Comment', 'Comment', tr(base, '>', '-')]
344 \ : ['Normal', 'Constant', base]
345 let hibase = 'Comment'
346 " Build it
347 redr
348 exe 'echoh' hibase '| echon "'.base.'"
349 \ | echoh' hiactive '| echon "'.prt[0].'"
350 \ | echoh' hicursor '| echon "'.prt[1].'"
351 \ | echoh' hiactive '| echon "'.prt[2].'" | echoh None'
352 " Append the cursor at the end
353 if empty(prt[1]) && ( !exists('a:1') || ( exists('a:1') && a:1 ) )
354 exe 'echoh' hibase '| echon "_" | echoh None'
355 en
356 endf
357 " ** Prt Actions {{{1
358 " Editing {{{2
359 fu! s:PrtClear()
360 unl! s:hstgot
361 let [s:prompt, s:matches] = [['', '', ''], 1]
362 cal s:BuildPrompt(1)
363 endf
364
365 fu! s:PrtAdd(char)
366 unl! s:hstgot
367 let s:prompt[0] = s:prompt[0] . a:char
368 cal s:BuildPrompt(1)
369 endf
370
371 fu! s:PrtBS()
372 unl! s:hstgot
373 let [prt, s:matches] = [s:prompt, 1]
374 let prt[0] = strpart(prt[0], -1, strlen(prt[0]))
375 cal s:BuildPrompt(1)
376 endf
377
378 fu! s:PrtDelete()
379 unl! s:hstgot
380 let [prt, s:matches] = [s:prompt, 1]
381 let prt[1] = strpart(prt[2], 0, 1)
382 let prt[2] = strpart(prt[2], 1)
383 cal s:BuildPrompt(1)
384 endf
385
386 fu! s:PrtDeleteWord()
387 unl! s:hstgot
388 let [str, s:matches] = [s:prompt[0], 1]
389 let str = match(str, '\W\w\+$') >= 0 ? matchstr(str, '^.\+\W\ze\w\+$')
390 \ : match(str, '\w\W\+$') >= 0 ? matchstr(str, '^.\+\w\ze\W\+$')
391 \ : match(str, '\s\+$') >= 0 ? matchstr(str, '^.*[^ \t]\+\ze\s\+$')
392 \ : match(str, ' ') <= 0 ? '' : str
393 let s:prompt[0] = str
394 cal s:BuildPrompt(1)
395 endf
396
397 fu! s:PrtInsert(type)
398 unl! s:hstgot
399 " Insert current word, search register, last visual and clipboard
400 let s:prompt[0] .= a:type == 'w' ? s:crword
401 \ : a:type == 's' ? getreg('/')
402 \ : a:type == 'v' ? s:crvisual
403 \ : a:type == '+' ? substitute(getreg('+'), '\n', '\\n', 'g') : s:prompt[0]
404 cal s:BuildPrompt(1)
405 endf
406 " Movement {{{2
407 fu! s:PrtCurLeft()
408 if !empty(s:prompt[0])
409 let prt = s:prompt
410 let prt[2] = prt[1] . prt[2]
411 let prt[1] = strpart(prt[0], strlen(prt[0]) - 1)
412 let prt[0] = strpart(prt[0], -1, strlen(prt[0]))
413 en
414 cal s:BuildPrompt(0)
415 endf
416
417 fu! s:PrtCurRight()
418 let prt = s:prompt
419 let prt[0] = prt[0] . prt[1]
420 let prt[1] = strpart(prt[2], 0, 1)
421 let prt[2] = strpart(prt[2], 1)
422 cal s:BuildPrompt(0)
423 endf
424
425 fu! s:PrtCurStart()
426 let prt = s:prompt
427 let str = join(prt, '')
428 let [prt[0], prt[1], prt[2]] = ['', strpart(str, 0, 1), strpart(str, 1)]
429 cal s:BuildPrompt(0)
430 endf
431
432 fu! s:PrtCurEnd()
433 let prt = s:prompt
434 let [prt[0], prt[1], prt[2]] = [join(prt, ''), '', '']
435 cal s:BuildPrompt(0)
436 endf
437
438 fu! s:PrtSelectMove(dir)
439 exe 'norm!' a:dir
440 let s:cline = line('.')
441 if line('$') > winheight(0) | cal s:BuildPrompt(0, s:Focus()) | en
442 endf
443
444 fu! s:PrtSelectJump(char, ...)
445 let lines = copy(s:matched)
446 if exists('a:1')
447 cal map(lines, 'split(v:val, ''[\/]\ze[^\/]\+$'')[-1]')
448 en
449 " Cycle through matches, use s:jmpchr to store last jump
450 let chr = escape(a:char, '.~')
451 if match(lines, '\c^'.chr) >= 0
452 " If not exists or does but not for the same char
453 let pos = match(lines, '\c^'.chr)
454 if !exists('s:jmpchr') || ( exists('s:jmpchr') && s:jmpchr[0] != chr )
455 let [jmpln, s:jmpchr] = [pos, [chr, pos]]
456 elsei exists('s:jmpchr') && s:jmpchr[0] == chr
457 " Start of lines
458 if s:jmpchr[1] == -1 | let s:jmpchr[1] = pos | en
459 let npos = match(lines, '\c^'.chr, s:jmpchr[1] + 1)
460 let [jmpln, s:jmpchr] = [npos == -1 ? pos : npos, [chr, npos]]
461 en
462 keepj exe jmpln + 1
463 let s:cline = line('.')
464 if line('$') > winheight(0) | cal s:BuildPrompt(0, s:Focus()) | en
465 en
466 endf
467 " Misc {{{2
468 fu! s:PrtClearCache()
469 if s:itemtype == 1 | retu | en
470 if s:itemtype == 0
471 cal ctrlp#clr()
472 elsei s:itemtype > 2
473 cal ctrlp#clr(s:statypes[s:itemtype][1])
474 en
475 if s:itemtype == 2
476 let g:ctrlp_lines = ctrlp#mrufiles#list(-1, 1)
477 el
478 cal s:SetLines(s:itemtype)
479 en
480 let s:force = 1
481 cal s:BuildPrompt(1)
482 unl s:force
483 endf
484
485 fu! s:PrtDeleteMRU()
486 if s:itemtype == 2
487 let s:force = 1
488 let g:ctrlp_lines = ctrlp#mrufiles#list(-1, 2)
489 cal s:BuildPrompt(1)
490 unl s:force
491 en
492 endf
493
494 fu! s:PrtExit()
495 if !has('autocmd') | cal s:Close() | en
496 exe s:currwin.'winc w'
497 endf
498
499 fu! s:PrtHistory(...)
500 if !s:maxhst | retu | en
501 let [str, hst, s:matches] = [join(s:prompt, ''), s:hstry, 1]
502 " Save to history if not saved before
503 let [hst[0], hslen] = [exists('s:hstgot') ? hst[0] : str, len(hst)]
504 let idx = exists('s:hisidx') ? s:hisidx + a:1 : a:1
505 " Limit idx within 0 and hslen
506 let idx = idx < 0 ? 0 : idx >= hslen ? hslen > 1 ? hslen - 1 : 0 : idx
507 let s:prompt = [hst[idx], '', '']
508 let [s:hisidx, s:hstgot, s:force] = [idx, 1, 1]
509 cal s:BuildPrompt(1)
510 unl s:force
511 endf
512 "}}}1
513 " * MapKeys() {{{1
514 fu! s:MapKeys(...)
515 " Normal keys
516 let pfunc = exists('a:1') && !a:1 ? 'PrtSelectJump' : 'PrtAdd'
517 let dojmp = s:byfname && pfunc == 'PrtSelectJump' ? ', 1' : ''
518 for each in range(32, 126)
519 let cmd = "nn \<buffer> \<silent> \<char-%d> :\<c-u>cal \<SID>%s(\"%s\"%s)\<cr>"
520 exe printf(cmd, each, pfunc, escape(nr2char(each), '"|\'), dojmp)
521 endfo
522 if exists('a:2') | retu | en
523 " Special keys
524 cal call('s:MapSpecs', exists('a:1') && !a:1 ? [1] : [])
525 endf
526
527 fu! s:MapSpecs(...)
528 let [lcmap, prtmaps] = ['nn <buffer> <silent>', {
529 \ 'PrtBS()': ['<bs>'],
530 \ 'PrtDelete()': ['<del>'],
531 \ 'PrtDeleteWord()': ['<c-w>'],
532 \ 'PrtClear()': ['<c-u>'],
533 \ 'PrtSelectMove("j")': ['<c-j>', '<down>'],
534 \ 'PrtSelectMove("k")': ['<c-k>', '<up>'],
535 \ 'PrtHistory(-1)': ['<c-n>'],
536 \ 'PrtHistory(1)': ['<c-p>'],
537 \ 'AcceptSelection("e")': ['<cr>', '<2-LeftMouse>'],
538 \ 'AcceptSelection("h")': ['<c-x>', '<c-cr>', '<c-s>'],
539 \ 'AcceptSelection("t")': ['<c-t>', '<MiddleMouse>'],
540 \ 'AcceptSelection("v")': ['<c-v>', '<c-q>', '<RightMouse>'],
541 \ 'ToggleFocus()': ['<tab>'],
542 \ 'ToggleRegex()': ['<c-r>'],
543 \ 'ToggleByFname()': ['<c-d>'],
544 \ 'ToggleType(1)': ['<c-f>', '<c-up'],
545 \ 'ToggleType(-1)': ['<c-b>', '<c-down>'],
546 \ 'PrtInsert("w")': ['<F2>'],
547 \ 'PrtInsert("s")': ['<F3>'],
548 \ 'PrtInsert("v")': ['<F4>'],
549 \ 'PrtInsert("+")': ['<F6>'],
550 \ 'PrtCurStart()': ['<c-a>'],
551 \ 'PrtCurEnd()': ['<c-e>'],
552 \ 'PrtCurLeft()': ['<c-h>', '<left>'],
553 \ 'PrtCurRight()': ['<c-l>', '<right>'],
554 \ 'PrtClearCache()': ['<F5>'],
555 \ 'PrtDeleteMRU()': ['<F7>'],
556 \ 'CreateNewFile()': ['<c-y>'],
557 \ 'MarkToOpen()': ['<c-z>'],
558 \ 'OpenMulti()': ['<c-o>'],
559 \ 'PrtExit()': ['<esc>', '<c-c>', '<c-g>'],
560 \ }]
561 if type(s:urprtmaps) == 4
562 cal extend(prtmaps, s:urprtmaps)
563 en
564 " Correct arrow keys in terminal
565 if ( has('termresponse') && !empty(v:termresponse) )
566 \ || &term =~? 'xterm\|\<k\?vt\|gnome\|screen'
567 for each in ['\A <up>','\B <down>','\C <right>','\D <left>']
568 exe lcmap.' <esc>['.each
569 endfo
570 en
571 if exists('a:1')
572 let prtunmaps = [
573 \ 'PrtBS()',
574 \ 'PrtDelete()',
575 \ 'PrtDeleteWord()',
576 \ 'PrtClear()',
577 \ 'PrtCurStart()',
578 \ 'PrtCurEnd()',
579 \ 'PrtCurLeft()',
580 \ 'PrtCurRight()',
581 \ 'PrtHistory(-1)',
582 \ 'PrtHistory(1)',
583 \ 'PrtInsert("w")',
584 \ 'PrtInsert("s")',
585 \ 'PrtInsert("v")',
586 \ 'PrtInsert("+")',
587 \ ]
588 for ke in prtunmaps | for kp in prtmaps[ke]
589 exe lcmap kp '<Nop>'
590 endfo | endfo
591 el
592 for [ke, va] in items(prtmaps) | for kp in va
593 exe lcmap kp ':<c-u>cal <SID>'.ke.'<cr>'
594 endfo | endfo
595 en
596 endf
597 " * Toggling {{{1
598 fu! s:Focus()
599 retu !exists('s:focus') ? 1 : s:focus
600 endf
601
602 fu! s:ToggleFocus()
603 let s:focus = !exists('s:focus') || s:focus ? 0 : 1
604 cal s:MapKeys(s:focus)
605 cal s:BuildPrompt(0, s:focus)
606 endf
607
608 fu! s:ToggleRegex()
609 let s:regexp = s:regexp ? 0 : 1
610 cal s:PrtSwitcher()
611 endf
612
613 fu! s:ToggleByFname()
614 if s:ispathitem()
615 let s:byfname = s:byfname ? 0 : 1
616 cal s:MapKeys(s:Focus(), 1)
617 cal s:PrtSwitcher()
618 en
619 endf
620
621 fu! s:ToggleType(dir)
622 let ext = exists('g:ctrlp_ext_vars') ? len(g:ctrlp_ext_vars) : 0
623 let s:itemtype = s:walker(g:ctrlp_builtins + ext, s:itemtype, a:dir)
624 let s:extid = s:itemtype - ( g:ctrlp_builtins + 1 )
625 unl! g:ctrlp_nolimit
626 cal s:SetLines(s:itemtype)
627 cal s:PrtSwitcher()
628 if s:itemtype > 2
629 if exists('g:ctrlp_ext_vars['.s:extid.'][4][0]')
630 let g:ctrlp_nolimit = g:ctrlp_ext_vars[s:extid][4][0]
631 en
632 el
633 cal s:syntax()
634 en
635 endf
636
637 fu! s:PrtSwitcher()
638 let [s:force, s:matches] = [1, 1]
639 cal s:BuildPrompt(1, s:Focus())
640 unl s:force
641 endf
642 fu! s:SetWD(...) "{{{1
643 let pathmode = s:pathmode
644 if exists('a:1') && len(a:1) | if type(a:1)
645 cal ctrlp#setdir(a:1) | retu
646 el
647 let pathmode = a:1
648 en | en
649 if !exists('a:2')
650 if match(s:crfile, '^\<.\+\>://.*') >= 0 || !pathmode | retu | en
651 if exists('+acd') | let [s:glb_acd, &acd] = [&acd, 0] | en
652 cal ctrlp#setdir(s:crfpath)
653 en
654 if pathmode == 1 | retu | en
655 let markers = ['root.dir','.git/','.hg/','.vimprojects','_darcs/','.bzr/']
656 if type(s:rmarkers) == 3 && !empty(s:rmarkers)
657 cal extend(markers, s:rmarkers, 0)
658 en
659 for marker in markers
660 cal s:findroot(getcwd(), marker, 0, 0)
661 if exists('s:foundroot') | brea | en
662 endfo
663 unl! s:foundroot
664 endf
665 " * AcceptSelection() {{{1
666 fu! ctrlp#acceptfile(mode, matchstr, ...)
667 let [md, matchstr] = [a:mode, a:matchstr]
668 " Get the full path
669 let filpath = s:itemtype ? matchstr : getcwd().s:lash.matchstr
670 cal s:PrtExit()
671 let bufnum = bufnr(filpath)
672 if s:jmptobuf && bufnum > 0 && md == 'e'
673 let [jmpb, bufwinnr] = [1, bufwinnr(bufnum)]
674 let buftab = s:jmptobuf > 1 ? s:buftab(bufnum) : [0, 0]
675 let j2l = a:0 ? a:1 : str2nr(matchstr(s:tail(), '^ +\zs\d\+$'))
676 en
677 " Switch to existing buffer or open new one
678 if exists('jmpb') && buftab[0]
679 exe 'tabn' buftab[1]
680 exe buftab[0].'winc w'
681 if j2l | cal s:j2l(j2l) | en
682 elsei exists('jmpb') && bufwinnr > 0
683 exe bufwinnr.'winc w'
684 if j2l | cal s:j2l(j2l) | en
685 el
686 " Determine the command to use
687 let cmd = md == 't' || s:splitwin == 1 ? 'tabe'
688 \ : md == 'h' || s:splitwin == 2 ? 'new'
689 \ : md == 'v' || s:splitwin == 3 ? 'vne' : ctrlp#normcmd('e')
690 " Open new window/buffer
691 cal call('s:openfile', a:0 ? [cmd, filpath, ' +'.a:1] : [cmd, filpath])
692 en
693 endf
694
695 fu! s:AcceptSelection(mode)
696 if a:mode == 'e' | if s:specinputs() | retu | en | en
697 " Get the selected line
698 let matchstr = matchstr(getline('.'), '^> \zs.\+\ze\t*$')
699 if empty(matchstr) | retu | en
700 " Do something with it
701 let actfunc = s:itemtype =~ '0\|1\|2' ? 'ctrlp#acceptfile'
702 \ : g:ctrlp_ext_vars[s:itemtype - ( g:ctrlp_builtins + 1 )][1]
703 cal call(actfunc, [a:mode, matchstr])
704 endf
705 fu! s:CreateNewFile() "{{{1
706 let str = join(s:prompt, '')
707 if empty(str) | retu | en
708 let str = s:sanstail(str)
709 let arr = split(str, '[\/]')
710 let fname = remove(arr, -1)
711 if len(arr) | if isdirectory(s:createparentdirs(arr))
712 let optyp = str
713 en | el
714 let optyp = fname
715 en
716 if exists('optyp')
717 let filpath = getcwd().s:lash.optyp
718 cal s:insertcache(str)
719 cal s:PrtExit()
720 let cmd = s:newfop == 1 ? 'tabe'
721 \ : s:newfop == 2 ? 'new'
722 \ : s:newfop == 3 ? 'vne' : ctrlp#normcmd('e')
723 cal s:openfile(cmd, filpath)
724 en
725 endf
726 " * OpenMulti() {{{1
727 fu! s:MarkToOpen()
728 if s:bufnr <= 0 || !s:opmul || s:itemtype > g:ctrlp_builtins | retu | en
729 let matchstr = matchstr(getline('.'), '^> \zs.\+\ze\t*$')
730 if empty(matchstr) | retu | en
731 let filpath = s:itemtype ? matchstr : getcwd().s:lash.matchstr
732 if exists('s:marked') && s:dictindex(s:marked, filpath) > 0
733 " Unmark and remove the file from s:marked
734 let key = s:dictindex(s:marked, filpath)
735 cal remove(s:marked, key)
736 if empty(s:marked) | unl! s:marked | en
737 if has('signs')
738 exe 'sign unplace' key 'buffer='.s:bufnr
739 en
740 el
741 " Add to s:marked and place a new sign
742 if exists('s:marked')
743 let vac = s:vacantdict(s:marked)
744 let key = empty(vac) ? len(s:marked) + 1 : vac[0]
745 let s:marked = extend(s:marked, { key : filpath })
746 el
747 let [key, s:marked] = [1, { 1 : filpath }]
748 en
749 if has('signs')
750 exe 'sign place' key 'line='.line('.').' name=ctrlpmark buffer='.s:bufnr
751 en
752 en
753 sil! cal ctrlp#statusline()
754 endf
755
756 fu! s:OpenMulti()
757 if !exists('s:marked') || !s:opmul
758 cal s:AcceptSelection('e')
759 retu
760 en
761 let mkd = s:marked
762 cal s:PrtExit()
763 " Try not to open a new tab
764 let [ntab, norwins] = [0, s:normbuf()]
765 if empty(norwins) | let ntab = 1 | el
766 for each in norwins
767 let bufnr = winbufnr(each)
768 if !empty(bufname(bufnr)) && !empty(getbufvar(bufnr, '&ft'))
769 \ && bufname(bufnr) != 'ControlP'
770 let ntab = 1
771 en
772 endfo
773 if !ntab | let wnr = min(norwins) | en
774 en
775 if ntab | tabnew | en
776 let [ic, wnr] = [1, exists('wnr') ? wnr : 1]
777 let cmds = { 'v': 'vne', 'h': 'new', 't': 'tabe' }
778 let spt = len(s:opmul) > 1 ? cmds[matchstr(s:opmul, '\w$')] : 'vne'
779 let nr = matchstr(s:opmul, '^\d\+')
780 exe wnr.'winc w'
781 for [ke, va] in items(mkd)
782 let cmd = ic == 1 ? 'e' : spt
783 cal s:openfile(cmd, va)
784 if nr > 1 && nr < ic | clo! | el | let ic += 1 | en
785 endfo
786 endf
787 " ** Helper functions {{{1
788 " Sorting {{{2
789 fu! ctrlp#complen(s1, s2)
790 " By length
791 let [len1, len2] = [strlen(a:s1), strlen(a:s2)]
792 retu len1 == len2 ? 0 : len1 > len2 ? 1 : -1
793 endf
794
795 fu! s:compmatlen(s1, s2)
796 " By match length
797 let mln1 = s:shortest(s:matchlens(a:s1, s:compat))
798 let mln2 = s:shortest(s:matchlens(a:s2, s:compat))
799 retu mln1 == mln2 ? 0 : mln1 > mln2 ? 1 : -1
800 endf
801
802 fu! s:comptime(s1, s2)
803 " By last modified time
804 let [time1, time2] = [getftime(a:s1), getftime(a:s2)]
805 retu time1 == time2 ? 0 : time1 < time2 ? 1 : -1
806 endf
807
808 fu! s:comparent(s1, s2)
809 " By same parent dir
810 if match(s:crfpath, escape(getcwd(), '.^$*\')) >= 0
811 let [as1, as2] = [getcwd().s:lash.a:s1, getcwd().s:lash.a:s2]
812 let [loc1, loc2] = [s:getparent(as1), s:getparent(as2)]
813 if loc1 == s:crfpath && loc2 != s:crfpath | retu -1 | en
814 if loc2 == s:crfpath && loc1 != s:crfpath | retu 1 | en
815 retu 0
816 en
817 retu 0
818 endf
819
820 fu! s:matchlens(str, pat, ...)
821 if empty(a:pat) || index(['^','$'], a:pat) >= 0 | retu {} | en
822 let st = exists('a:1') ? a:1 : 0
823 let lens = exists('a:2') ? a:2 : {}
824 let nr = exists('a:3') ? a:3 : 0
825 if match(a:str, a:pat, st) != -1
826 let [mst, mnd] = [matchstr(a:str, a:pat, st), matchend(a:str, a:pat, st)]
827 let lens = extend(lens, { nr : [len(mst), mst] })
828 let lens = s:matchlens(a:str, a:pat, mnd, lens, nr + 1)
829 en
830 retu lens
831 endf
832
833 fu! s:shortest(lens)
834 retu min(map(values(a:lens), 'v:val[0]'))
835 endf
836
837 fu! s:mixedsort(s1, s2)
838 let [cml, cln] = [s:compmatlen(a:s1, a:s2), ctrlp#complen(a:s1, a:s2)]
839 if s:itemtype < 3 && s:height < 51
840 let par = s:comparent(a:s1, a:s2)
841 if s:height < 21
842 retu 6 * cml + 3 * par + 2 * s:comptime(a:s1, a:s2) + cln
843 en
844 retu 3 * cml + 2 * par + cln
845 en
846 retu 2 * cml + cln
847 endf
848 " Statusline {{{2
849 fu! ctrlp#statusline(...)
850 if !exists('s:statypes')
851 let s:statypes = [
852 \ ['files', 'fil'],
853 \ ['buffers', 'buf'],
854 \ ['mru files', 'mru'],
855 \ ]
856 if exists('g:ctrlp_ext_vars')
857 cal map(copy(g:ctrlp_ext_vars), 'add(s:statypes, [ v:val[2], v:val[3] ])')
858 en
859 en
860 let tps = s:statypes
861 let max = len(tps) - 1
862 let nxt = tps[s:walker(max, s:itemtype, 1)][1]
863 let prv = tps[s:walker(max, s:itemtype, -1)][1]
864 let item = tps[s:itemtype][0]
865 let focus = s:Focus() ? 'prt' : 'win'
866 let byfname = s:byfname ? 'file' : 'path'
867 let regex = s:regexp ? '%#LineNr# regex %*' : ''
868 let focus = '%#LineNr# '.focus.' %*'
869 let byfname = '%#Character# '.byfname.' %*'
870 let item = '%#Character# '.item.' %*'
871 let slider = ' <'.prv.'>={'.item.'}=<'.nxt.'>'
872 let dir = ' %=%<%#LineNr# '.getcwd().' %*'
873 let marked = s:opmul ? exists('s:marked') ? ' <'.s:dismrk().'>' : ' <+>' : ''
874 let &l:stl = focus.byfname.regex.slider.marked.dir
875 endf
876
877 fu! s:dismrk()
878 retu has('signs') ? '+'.len(s:marked) :
879 \ '%<'.join(values(map(copy(s:marked), 'split(v:val, "[\\/]")[-1]')), ', ')
880 endf
881
882 fu! ctrlp#progress(len)
883 if has('macunix') || has('mac') | sl 1m | en
884 let &l:stl = '%#Function# '.a:len.' %* %=%<%#LineNr# '.getcwd().' %*'
885 redr
886 endf
887 " Paths {{{2
888 fu! ctrlp#dirfilter(val)
889 retu isdirectory(a:val) && match(a:val, '[\/]\.\{,2}$') < 0 ? 1 : 0
890 endf
891
892 fu! s:ispathitem()
893 let ext = s:itemtype - ( g:ctrlp_builtins + 1 )
894 if s:itemtype < 3 || ( s:itemtype > 2 && g:ctrlp_ext_vars[ext][3] == 'dir' )
895 retu 1
896 en
897 retu 0
898 endf
899
900 fu! ctrlp#rmbasedir(items)
901 let path = &ssl || !exists('+ssl') ? getcwd().'/' :
902 \ substitute(getcwd(), '\\', '\\\\', 'g').'\\'
903 retu map(a:items, 'substitute(v:val, path, "", "g")')
904 endf
905
906 fu! s:parentdir(curr)
907 let parent = s:getparent(a:curr)
908 if parent != a:curr | cal ctrlp#setdir(parent) | en
909 endf
910
911 fu! s:getparent(item)
912 retu split(a:item, '[\/]\ze[^\/]\+[\/:]\?$')[0]
913 endf
914
915 fu! s:getgrand(item)
916 retu split(a:item, '[\/]\ze[^\/]\+[\/][^\/]\+[\/:]\?$')[0]
917 endf
918
919 fu! s:createparentdirs(arr)
920 for each in a:arr
921 let curr = exists('curr') ? curr.s:lash.each : each
922 cal ctrlp#utils#mkdir(curr)
923 endfo
924 retu curr
925 endf
926
927 fu! s:listdirs(path, parent)
928 let [str, dirs] = ['', split(s:glbpath(a:path, '*', 1), "\n")]
929 for entry in filter(dirs, 'isdirectory(v:val)')
930 let str .= a:parent . split(entry, '[\/]')[-1] . "\n"
931 endfo
932 retu str
933 endf
934
935 fu! ctrlp#cpl(A, L, P)
936 let haslash = match(a:A, '[\/]')
937 let parent = substitute(a:A, '[^\/]*$', '', 'g')
938 let path = !haslash ? parent : haslash > 0 ? getcwd().s:lash.parent : getcwd()
939 retu s:listdirs(path, parent)
940 endf
941
942 fu! s:findroot(curr, mark, depth, type)
943 let [depth, notfound] = [a:depth + 1, empty(s:glbpath(a:curr, a:mark, 1))]
944 if !notfound || depth > s:maxdepth
945 if notfound | cal ctrlp#setdir(s:cwd) | en
946 if a:type
947 let s:vcsroot = depth <= s:maxdepth ? a:curr : ''
948 el
949 cal ctrlp#setdir(a:curr)
950 let s:foundroot = 1
951 en
952 el
953 let parent = s:getparent(a:curr)
954 if parent != a:curr | cal s:findroot(parent, a:mark, depth, a:type) | en
955 en
956 endf
957
958 fu! s:glbpath(...)
959 retu call('globpath', v:version > 701 ? a:000 : a:000[:1])
960 endf
961
962 fu! ctrlp#fnesc(path)
963 retu exists('*fnameescape') ? fnameescape(a:path) : escape(a:path, " %#*?|<\"\n")
964 endf
965
966 fu! ctrlp#setdir(path, ...)
967 let cmd = exists('a:1') ? a:1 : 'lc!'
968 try
969 exe cmd.' '.ctrlp#fnesc(a:path)
970 cat
971 cal ctrlp#msg("Can't change working dir. Directory not exists.")
972 endt
973 endf
974 " Highlighting {{{2
975 fu! s:syntax()
976 sy match CtrlPNoEntries '^ == NO ENTRIES ==$'
977 sy match CtrlPLineMarker '^>'
978 hi link CtrlPNoEntries Error
979 hi CtrlPLineMarker guifg=bg
980 endf
981
982 fu! s:highlight(pat, grp)
983 cal clearmatches()
984 if !empty(a:pat) && a:pat != '..' && s:itemtype < 3
985 let pat = substitute(a:pat, '\~', '\\~', 'g')
986 if !s:regexp | let pat = escape(pat, '.') | en
987 " Match only filename
988 if s:byfname
989 let pat = substitute(pat, '\[\^\(.\{-}\)\]\\{-}', '[^\\/\1]\\{-}', 'g')
990 let pat = substitute(pat, '$', '\\ze[^\\/]*$', 'g')
991 en
992 cal matchadd(a:grp, '\c'.pat)
993 cal matchadd('CtrlPLineMarker', '^>')
994 en
995 endf
996
997 fu! s:dohighlight()
998 retu type(s:mathi) == 3 && len(s:mathi) == 2 && s:mathi[0]
999 \ && exists('*clearmatches')
1000 endf
1001 " Prompt history {{{2
1002 fu! s:gethistloc()
1003 let cache_dir = ctrlp#utils#cachedir().s:lash.'hist'
1004 retu [cache_dir, cache_dir.s:lash.'cache.txt']
1005 endf
1006
1007 fu! s:gethistdata()
1008 retu ctrlp#utils#readfile(s:gethistloc()[1])
1009 endf
1010
1011 fu! ctrlp#recordhist()
1012 let str = join(s:prompt, '')
1013 if empty(str) || !s:maxhst | retu | en
1014 let hst = s:hstry
1015 if len(hst) > 1 && hst[1] == str | retu | en
1016 cal extend(hst, [str], 1)
1017 if len(hst) > s:maxhst | cal remove(hst, s:maxhst, -1) | en
1018 endf
1019 " Signs {{{2
1020 fu! s:unmarksigns()
1021 if !s:dosigns() | retu | en
1022 for key in keys(s:marked)
1023 exe 'sign unplace' key 'buffer='.s:bufnr
1024 endfo
1025 endf
1026
1027 fu! s:remarksigns()
1028 if !s:dosigns() | retu | en
1029 let nls = s:matched
1030 for ic in range(1, len(nls))
1031 let filpath = s:itemtype ? nls[ic - 1] : getcwd().s:lash.nls[ic - 1]
1032 let key = s:dictindex(s:marked, filpath)
1033 if key > 0
1034 exe 'sign place' key 'line='.ic.' name=ctrlpmark buffer='.s:bufnr
1035 en
1036 endfo
1037 endf
1038
1039 fu! s:dosigns()
1040 retu exists('s:marked') && s:bufnr > 0 && s:opmul && has('signs')
1041 endf
1042 " Dictionaries {{{2
1043 fu! s:dictindex(dict, expr)
1044 for key in keys(a:dict)
1045 if a:dict[key] == a:expr | retu key | en
1046 endfo
1047 retu -1
1048 endf
1049
1050 fu! s:vacantdict(dict)
1051 retu filter(range(1, max(keys(a:dict))), '!has_key(a:dict, v:val)')
1052 endf
1053 " Buffers {{{2
1054 fu! s:buftab(bufnum)
1055 for nr in range(1, tabpagenr('$'))
1056 let buflist = tabpagebuflist(nr)
1057 if match(buflist, a:bufnum) >= 0
1058 let [buftabnr, tabwinnrs] = [nr, tabpagewinnr(nr, '$')]
1059 for ewin in range(1, tabwinnrs)
1060 if buflist[ewin - 1] == a:bufnum
1061 retu [ewin, buftabnr]
1062 en
1063 endfo
1064 en
1065 endfo
1066 retu [0, 0]
1067 endf
1068
1069 fu! s:normbuf()
1070 let winnrs = []
1071 for each in range(1, winnr('$'))
1072 let bufnr = winbufnr(each)
1073 if getbufvar(bufnr, '&bl') && empty(getbufvar(bufnr, '&bt'))
1074 \ && getbufvar(bufnr, '&ma')
1075 cal add(winnrs, each)
1076 en
1077 endfo
1078 retu winnrs
1079 endf
1080
1081 fu! ctrlp#normcmd(cmd)
1082 if !empty(s:nosplit) && match([bufname('%'), &l:ft], s:nosplit) >= 0
1083 retu a:cmd
1084 en
1085 " Find a regular buffer
1086 let norwins = s:normbuf()
1087 let norwin = empty(norwins) ? 0 : norwins[0]
1088 if norwin
1089 if index(norwins, winnr()) < 0
1090 exe norwin.'winc w'
1091 en
1092 retu a:cmd
1093 en
1094 retu 'bo vne'
1095 endf
1096
1097 fu! s:setupblank()
1098 setl noswf nobl nonu nowrap nolist nospell nocuc wfh
1099 setl fdc=0 fdl=99 tw=0 bt=nofile bh=unload
1100 if v:version >= 703
1101 setl nornu noudf cc=0
1102 en
1103 endf
1104
1105 fu! s:leavepre()
1106 if s:clrex && ( !has('clientserver') ||
1107 \ ( has('clientserver') && len(split(serverlist(), "\n")) == 1 ) )
1108 cal ctrlp#clra(1)
1109 en
1110 cal ctrlp#utils#writecache(s:hstry, s:gethistloc()[0], s:gethistloc()[1])
1111 endf
1112
1113 fu! s:checkbuf()
1114 if exists('s:init') | retu | en
1115 if exists('s:bufnr') && s:bufnr > 0
1116 exe s:bufnr.'bw!'
1117 en
1118 endf
1119 " Arguments {{{2
1120 fu! s:tail()
1121 if exists('s:optail') && !empty('s:optail')
1122 let tailpref = match(s:optail, '^\s*+') < 0 ? ' +' : ' '
1123 retu tailpref.s:optail
1124 en
1125 retu ''
1126 endf
1127
1128 fu! s:sanstail(str)
1129 " Restore the number of backslashes
1130 let str = substitute(a:str, '\\\\', '\', 'g')
1131 unl! s:optail
1132 if match(str, ':\([^:]\|\\:\)*$') >= 0
1133 let s:optail = matchstr(str, ':\zs\([^:]\|\\:\)*$')
1134 retu substitute(str, ':\([^:]\|\\:\)*$', '', 'g')
1135 en
1136 retu str
1137 endf
1138 " Misc {{{2
1139 fu! s:specinputs()
1140 let str = join(s:prompt, '')
1141 let type = s:itemtype > 2 ?
1142 \ g:ctrlp_ext_vars[s:itemtype - ( g:ctrlp_builtins + 1 )][3] : s:itemtype
1143 if str == '..' && type =~ '0\|dir'
1144 cal s:parentdir(getcwd())
1145 cal s:SetLines(s:itemtype)
1146 cal s:PrtClear()
1147 retu 1
1148 elsei ( str == '/' || str == '\' ) && type =~ '0\|dir'
1149 cal s:SetWD(2, 0)
1150 cal s:SetLines(s:itemtype)
1151 cal s:PrtClear()
1152 retu 1
1153 elsei str == '?'
1154 cal s:PrtExit()
1155 let hlpwin = &columns > 159 ? '| vert res 80' : ''
1156 sil! exe 'bo vert h ctrlp-mappings' hlpwin '| norm! 0'
1157 retu 1
1158 en
1159 retu 0
1160 endf
1161
1162 fu! s:lastvisual()
1163 let cview = winsaveview()
1164 let [ovreg, ovtype] = [getreg('v'), getregtype('v')]
1165 let [oureg, outype] = [getreg('"'), getregtype('"')]
1166 sil! norm! gv"vy
1167 let selected = substitute(getreg('v'), '\n', '\\n', 'g')
1168 cal setreg('v', ovreg, ovtype)
1169 cal setreg('"', oureg, outype)
1170 cal winrestview(cview)
1171 retu selected
1172 endf
1173
1174 fu! ctrlp#msg(msg)
1175 echoh Identifier | echon "CtrlP: ".a:msg | echoh None
1176 endf
1177
1178 fu! s:openfile(cmd, filpath, ...)
1179 let cmd = a:cmd == 'e' && &modified ? 'hid e' : a:cmd
1180 let tail = a:0 ? a:1 : s:tail()
1181 try
1182 exe cmd.tail.' '.ctrlp#fnesc(a:filpath)
1183 cat
1184 cal ctrlp#msg("Operation can't be completed. Make sure filename is valid.")
1185 fina
1186 if !empty(tail)
1187 sil! norm! zOzz
1188 en
1189 endt
1190 endf
1191
1192 fu! s:writecache(read_cache, cache_file)
1193 if !a:read_cache && ( ( g:ctrlp_newcache || !filereadable(a:cache_file) )
1194 \ && s:caching || len(g:ctrlp_allfiles) > s:nocache_lim )
1195 if len(g:ctrlp_allfiles) > s:nocache_lim | let s:caching = 1 | en
1196 cal ctrlp#utils#writecache(g:ctrlp_allfiles)
1197 en
1198 endf
1199
1200 fu! s:j2l(nr)
1201 exe a:nr
1202 sil! norm! zOzz
1203 endf
1204
1205 fu! s:regexfilter(str)
1206 let str = a:str
1207 let pats = {
1208 \ '^\(\\|\)\|\(\\|\)$': '\\|',
1209 \ '^\\\(zs\|ze\|<\|>\)': '^\\\(zs\|ze\|<\|>\)',
1210 \ '^\S\*$': '\*',
1211 \ '^\S\\?$': '\\?',
1212 \ }
1213 for key in keys(pats) | if match(str, key) >= 0
1214 let str = substitute(str, pats[key], '', 'g')
1215 en | endfo
1216 retu str
1217 endf
1218
1219 fu! s:walker(max, pos, dir)
1220 retu a:dir > 0 ? a:pos < a:max ? a:pos + 1 : 0 : a:pos > 0 ? a:pos - 1 : a:max
1221 endf
1222
1223 fu! s:matchfname(item, pat)
1224 retu match(split(a:item, '[\/]\ze[^\/]\+$')[-1], a:pat)
1225 endf
1226
1227 fu! s:matchtab(item, pat)
1228 retu match(split(a:item, '\t\+[^\t]\+$')[0], a:pat)
1229 endf
1230
1231 fu! s:maxfiles(len)
1232 retu s:maxfiles && a:len > s:maxfiles ? 1 : 0
1233 endf
1234
1235 fu! s:insertcache(str)
1236 let [data, g:ctrlp_newcache, str] = [g:ctrlp_allfiles, 1, a:str]
1237 if strlen(str) <= strlen(data[0])
1238 let pos = 0
1239 elsei strlen(str) >= strlen(data[-1])
1240 let pos = len(data) - 1
1241 el
1242 let pos = 0
1243 for each in data
1244 if strlen(each) > strlen(str) | brea | en
1245 let pos += 1
1246 endfo
1247 en
1248 cal insert(data, str, pos)
1249 cal s:writecache(0, ctrlp#utils#cachefile())
1250 endf
1251
1252 fu! s:lscommand()
1253 let cmd = s:usrcmd
1254 if type(cmd) == 1
1255 retu cmd
1256 elsei type(cmd) == 3 && len(cmd) >= 2 && !empty(cmd[0]) && !empty(cmd[1])
1257 let rmarker = cmd[0]
1258 " Find a repo root
1259 cal s:findroot(getcwd(), rmarker, 0, 1)
1260 if !exists('s:vcsroot') || ( exists('s:vcsroot') && empty(s:vcsroot) )
1261 " Try the secondary_command
1262 retu len(cmd) == 3 ? cmd[2] : ''
1263 en
1264 let s:vcscmd = s:lash == '\' ? 1 : 0
1265 retu cmd[1]
1266 en
1267 endf
1268 " Extensions {{{2
1269 fu! s:tagfiles()
1270 retu filter(map(tagfiles(), 'fnamemodify(v:val, ":p")'), 'filereadable(v:val)')
1271 endf
1272
1273 fu! ctrlp#exit()
1274 cal s:PrtExit()
1275 endf
1276
1277 fu! ctrlp#prtclear()
1278 cal s:PrtClear()
1279 endf
1280
1281 fu! ctrlp#setlines(type)
1282 cal s:SetLines(a:type)
1283 endf
1284 "}}}1
1285 " * Initialization {{{1
1286 fu! s:SetLines(type)
1287 let s:itemtype = a:type
1288 let types = [
1289 \ 's:Files()',
1290 \ 's:Buffers()',
1291 \ 'ctrlp#mrufiles#list(-1)',
1292 \ ]
1293 if exists('g:ctrlp_ext_vars')
1294 cal map(copy(g:ctrlp_ext_vars), 'add(types, v:val[0])')
1295 en
1296 let g:ctrlp_lines = eval(types[a:type])
1297 endf
1298
1299 fu! ctrlp#init(type, ...)
1300 if exists('s:init') | retu | en
1301 let [s:matches, s:init] = [1, 1]
1302 cal s:Open()
1303 cal s:SetWD(exists('a:1') ? a:1 : '')
1304 cal s:MapKeys()
1305 cal s:SetLines(a:type)
1306 cal s:BuildPrompt(1)
1307 if has('syntax') && exists('g:syntax_on')
1308 cal s:syntax()
1309 en
1310 endf
1311 if has('autocmd') "{{{1
1312 aug CtrlPAug
1313 au!
1314 au BufEnter ControlP cal s:checkbuf()
1315 au BufLeave ControlP cal s:Close()
1316 au VimLeavePre * cal s:leavepre()
1317 if s:lazy
1318 au CursorHold ControlP cal s:ForceUpdate()
1319 en
1320 aug END
1321 en "}}}
1322
1323 " vim:fen:fdm=marker:fmr={{{,}}}:fdl=0:fdc=1:ts=2:sw=2:sts=2