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