Use MRE sorting for buffers

Close #156
This commit is contained in:
Kien N 2012-03-15 08:15:54 +07:00
parent 607cc29d81
commit fbc61b0d3e
2 changed files with 97 additions and 59 deletions

View File

@ -585,7 +585,7 @@ fu! s:PrtClearCache()
cal ctrlp#clr(s:statypes[s:itemtype][1])
en
if s:itemtype == 2
let g:ctrlp_lines = ctrlp#mrufiles#list(-1, 1)
let g:ctrlp_lines = ctrlp#mrufiles#refresh()
el
cal ctrlp#setlines(s:itemtype)
en
@ -596,13 +596,13 @@ endf
fu! s:PrtDeleteMRU()
if s:itemtype != 2 | retu | en
let [s:force, ags] = [1, [-1, 2]]
let [s:force, tbrem] = [1, []]
if exists('s:marked')
let ags = [-1, 2, values(s:marked)]
let tbrem = values(s:marked)
cal s:unmarksigns()
unl s:marked
en
let g:ctrlp_lines = call('ctrlp#mrufiles#list', ags)
let g:ctrlp_lines = ctrlp#mrufiles#remove(tbrem)
cal s:BuildPrompt(1)
unl s:force
endf
@ -933,6 +933,15 @@ fu! s:comptime(s1, s2)
retu time1 == time2 ? 0 : time1 < time2 ? 1 : -1
endf
fu! s:compmre(...)
" By last entered time (buffer only)
if !exists('s:mrbs')
let s:mrbs = ctrlp#mrufiles#bufs()
en
let cwd = getcwd()
retu index(s:mrbs, cwd.s:lash().a:1) - index(s:mrbs, cwd.s:lash().a:2)
endf
fu! s:comparent(s1, s2)
" By same parent dir
let cwd = getcwd()
@ -976,8 +985,11 @@ fu! s:mixedsort(s1, s2)
if s:itemtype < 3 && s:height < 51
let [par, cfn] = [s:comparent(a:s1, a:s2), s:compfnlen(a:s1, a:s2)]
if s:height < 21
let ctm = s:comptime(a:s1, a:s2)
retu 12 * cml + 6 * par + 3 * cfn + 2 * ctm + cln
let [muls, ctm] = s:itemtype == 1
\ ? [[6, 3, 2, 12], s:compmre(a:s1, a:s2)]
\ : [[12, 6, 3, 2], s:comptime(a:s1, a:s2)]
unl! s:mrbs
retu muls[0] * cml + muls[1] * par + muls[2] * cfn + muls[3] * ctm + cln
en
retu 6 * cml + 3 * par + 2 * cfn + cln
en
@ -1527,7 +1539,7 @@ fu! ctrlp#setlines(type)
let types = [
\ 'ctrlp#files()',
\ 'ctrlp#buffers()',
\ 'ctrlp#mrufiles#list(-1)',
\ 'ctrlp#mrufiles#list()',
\ ]
if exists('g:ctrlp_ext_vars')
cal map(copy(g:ctrlp_ext_vars), 'add(types, v:val["init"])')

View File

@ -17,76 +17,102 @@ fu! ctrlp#mrufiles#opts()
for [ke, va] in items(opts)
exe 'let' va[0] '=' string(exists(ke) ? eval(ke) : va[1])
endfo
let s:csen = s:csen ? '#' : '?'
let [s:csen, s:mrbs] = [s:csen ? '#' : '?', []]
endf
cal ctrlp#mrufiles#opts()
fu! ctrlp#mrufiles#list(bufnr, ...) "{{{1
if s:locked | retu | en
let bufnr = a:bufnr + 0
if bufnr > 0
let fn = fnamemodify(bufname(bufnr), ':p')
let fn = exists('+ssl') ? tr(fn, '/', '\') : fn
if empty(fn) || !empty(&bt) || ( !empty(s:in) && fn !~# s:in )
\ || ( !empty(s:ex) && fn =~# s:ex ) || !filereadable(fn)
retu
en
en
" Utilities {{{1
fu! s:excl(fn)
retu !empty(s:ex) && a:fn =~# s:ex
endf
fu! s:readcache()
if !exists('s:cadir') || !exists('s:cafile')
let s:cadir = ctrlp#utils#cachedir().ctrlp#utils#lash().'mru'
let s:cafile = s:cadir.ctrlp#utils#lash().'cache.txt'
en
if a:0 && a:1 == 2
let mrufs = []
if a:0 == 2
let mrufs = ctrlp#utils#readfile(s:cafile)
cal filter(mrufs, 'index(a:2, v:val) < 0')
en
cal ctrlp#utils#writecache(mrufs, s:cadir, s:cafile)
retu map(mrufs, 'fnamemodify(v:val, '':.'')')
retu ctrlp#utils#readfile(s:cafile)
endf
fu! s:reformat(mrufs)
if s:re
let cwd = exists('+ssl') ? tr(getcwd(), '/', '\') : getcwd()
cal filter(a:mrufs, '!stridx(v:val, cwd)')
en
" Get the list
let mrufs = ctrlp#utils#readfile(s:cafile)
" Remove non-existent files
if a:0 && a:1 == 1
cal filter(mrufs, '!empty(ctrlp#utils#glob(v:val, 1)) && !s:excl(v:val)')
if exists('+ssl')
cal map(mrufs, 'tr(v:val, ''/'', ''\'')')
cal filter(mrufs, 'count(mrufs, v:val) == 1')
en
cal ctrlp#utils#writecache(mrufs, s:cadir, s:cafile)
retu map(a:mrufs, 'fnamemodify(v:val, '':.'')')
endf
fu! s:record(bufnr, ...)
if s:locked | retu | en
let bufnr = a:bufnr + 0
if bufnr <= 0 | retu | en
let fn = fnamemodify(bufname(bufnr), ':p')
let fn = exists('+ssl') ? tr(fn, '/', '\') : fn
cal filter(s:mrbs, 'v:val !='.s:csen.' fn')
cal insert(s:mrbs, fn)
if empty(fn) || !empty(&bt) || ( !empty(s:in) && fn !~# s:in )
\ || ( !empty(s:ex) && fn =~# s:ex ) || !filereadable(fn)
\ || ( a:0 && a:1 == 1 )
retu
en
" Return the list with the active buffer removed
if bufnr == -1
if s:re
let cwd = exists('+ssl') ? tr(getcwd(), '/', '\') : getcwd()
cal filter(mrufs, '!stridx(v:val, cwd)')
en
retu map(mrufs, 'fnamemodify(v:val, '':.'')')
en
" Remove old entry
let mrufs = s:readcache()
cal filter(mrufs, 'v:val !='.s:csen.' fn')
" Insert new one
cal insert(mrufs, fn)
" Remove oldest entry or entries
if len(mrufs) > s:max | cal remove(mrufs, s:max, -1) | en
cal ctrlp#utils#writecache(mrufs, s:cadir, s:cafile)
endf "}}}
fu! s:excl(fn) "{{{
retu !empty(s:ex) && a:fn =~# s:ex
endf "}}}
fu! ctrlp#mrufiles#init() "{{{1
endf
" Public {{{1
fu! ctrlp#mrufiles#refresh()
let mrufs = s:readcache()
cal filter(mrufs, '!empty(ctrlp#utils#glob(v:val, 1)) && !s:excl(v:val)')
if exists('+ssl')
cal map(mrufs, 'tr(v:val, ''/'', ''\'')')
cal filter(mrufs, 'count(mrufs, v:val) == 1')
en
cal ctrlp#utils#writecache(mrufs, s:cadir, s:cafile)
retu s:reformat(mrufs)
endf
fu! ctrlp#mrufiles#remove(files)
let mrufs = []
if a:files != []
let mrufs = s:readcache()
cal filter(mrufs, 'index(a:files, v:val) < 0')
en
cal ctrlp#utils#writecache(mrufs, s:cadir, s:cafile)
retu map(mrufs, 'fnamemodify(v:val, '':.'')')
endf
fu! ctrlp#mrufiles#list()
if a:0
cal s:record(a:1) | retu
en
retu s:reformat(s:readcache())
endf
fu! ctrlp#mrufiles#bufs()
retu s:mrbs
endf
fu! ctrlp#mrufiles#init()
if !has('autocmd') | retu | en
let s:locked = 0
aug CtrlPMRUF
au!
au BufReadPost,BufNewFile,BufWritePost *
\ cal ctrlp#mrufiles#list(expand('<abuf>', 1))
if s:mre
au BufEnter,BufUnload *
\ cal ctrlp#mrufiles#list(expand('<abuf>', 1))
en
au BufReadPost,BufNewFile,BufWritePost * cal s:record(expand('<abuf>', 1))
au BufEnter,BufUnload * cal s:record(expand('<abuf>', 1), 1)
au QuickFixCmdPre *vimgrep* let s:locked = 1
au QuickFixCmdPost *vimgrep* let s:locked = 0
aug END
if s:mre
aug CtrlPMRE
au!
au BufEnter,BufUnload * cal s:record(expand('<abuf>', 1))
aug END
el
if exists('#CtrlPMRE')
au! CtrlPMRE
en
en
endf
"}}}