annotate vim/vimfiles/autoload/ctrlp.vim @ 3:92af3257a261

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