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]) cal ctrlp#clr(s:statypes[s:itemtype][1])
en en
if s:itemtype == 2 if s:itemtype == 2
let g:ctrlp_lines = ctrlp#mrufiles#list(-1, 1) let g:ctrlp_lines = ctrlp#mrufiles#refresh()
el el
cal ctrlp#setlines(s:itemtype) cal ctrlp#setlines(s:itemtype)
en en
@ -596,13 +596,13 @@ endf
fu! s:PrtDeleteMRU() fu! s:PrtDeleteMRU()
if s:itemtype != 2 | retu | en if s:itemtype != 2 | retu | en
let [s:force, ags] = [1, [-1, 2]] let [s:force, tbrem] = [1, []]
if exists('s:marked') if exists('s:marked')
let ags = [-1, 2, values(s:marked)] let tbrem = values(s:marked)
cal s:unmarksigns() cal s:unmarksigns()
unl s:marked unl s:marked
en en
let g:ctrlp_lines = call('ctrlp#mrufiles#list', ags) let g:ctrlp_lines = ctrlp#mrufiles#remove(tbrem)
cal s:BuildPrompt(1) cal s:BuildPrompt(1)
unl s:force unl s:force
endf endf
@ -933,6 +933,15 @@ fu! s:comptime(s1, s2)
retu time1 == time2 ? 0 : time1 < time2 ? 1 : -1 retu time1 == time2 ? 0 : time1 < time2 ? 1 : -1
endf 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) fu! s:comparent(s1, s2)
" By same parent dir " By same parent dir
let cwd = getcwd() let cwd = getcwd()
@ -976,8 +985,11 @@ fu! s:mixedsort(s1, s2)
if s:itemtype < 3 && s:height < 51 if s:itemtype < 3 && s:height < 51
let [par, cfn] = [s:comparent(a:s1, a:s2), s:compfnlen(a:s1, a:s2)] let [par, cfn] = [s:comparent(a:s1, a:s2), s:compfnlen(a:s1, a:s2)]
if s:height < 21 if s:height < 21
let ctm = s:comptime(a:s1, a:s2) let [muls, ctm] = s:itemtype == 1
retu 12 * cml + 6 * par + 3 * cfn + 2 * ctm + cln \ ? [[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 en
retu 6 * cml + 3 * par + 2 * cfn + cln retu 6 * cml + 3 * par + 2 * cfn + cln
en en
@ -1527,7 +1539,7 @@ fu! ctrlp#setlines(type)
let types = [ let types = [
\ 'ctrlp#files()', \ 'ctrlp#files()',
\ 'ctrlp#buffers()', \ 'ctrlp#buffers()',
\ 'ctrlp#mrufiles#list(-1)', \ 'ctrlp#mrufiles#list()',
\ ] \ ]
if exists('g:ctrlp_ext_vars') if exists('g:ctrlp_ext_vars')
cal map(copy(g:ctrlp_ext_vars), 'add(types, v:val["init"])') 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) for [ke, va] in items(opts)
exe 'let' va[0] '=' string(exists(ke) ? eval(ke) : va[1]) exe 'let' va[0] '=' string(exists(ke) ? eval(ke) : va[1])
endfo endfo
let s:csen = s:csen ? '#' : '?' let [s:csen, s:mrbs] = [s:csen ? '#' : '?', []]
endf endf
cal ctrlp#mrufiles#opts() cal ctrlp#mrufiles#opts()
fu! ctrlp#mrufiles#list(bufnr, ...) "{{{1 " Utilities {{{1
if s:locked | retu | en fu! s:excl(fn)
let bufnr = a:bufnr + 0 retu !empty(s:ex) && a:fn =~# s:ex
if bufnr > 0 endf
let fn = fnamemodify(bufname(bufnr), ':p')
let fn = exists('+ssl') ? tr(fn, '/', '\') : fn fu! s:readcache()
if empty(fn) || !empty(&bt) || ( !empty(s:in) && fn !~# s:in )
\ || ( !empty(s:ex) && fn =~# s:ex ) || !filereadable(fn)
retu
en
en
if !exists('s:cadir') || !exists('s:cafile') if !exists('s:cadir') || !exists('s:cafile')
let s:cadir = ctrlp#utils#cachedir().ctrlp#utils#lash().'mru' let s:cadir = ctrlp#utils#cachedir().ctrlp#utils#lash().'mru'
let s:cafile = s:cadir.ctrlp#utils#lash().'cache.txt' let s:cafile = s:cadir.ctrlp#utils#lash().'cache.txt'
en en
if a:0 && a:1 == 2 retu ctrlp#utils#readfile(s:cafile)
let mrufs = [] endf
if a:0 == 2
let mrufs = ctrlp#utils#readfile(s:cafile) fu! s:reformat(mrufs)
cal filter(mrufs, 'index(a:2, v:val) < 0') if s:re
let cwd = exists('+ssl') ? tr(getcwd(), '/', '\') : getcwd()
cal filter(a:mrufs, '!stridx(v:val, cwd)')
en en
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
let mrufs = s:readcache()
cal filter(mrufs, 'v:val !='.s:csen.' fn')
cal insert(mrufs, fn)
if len(mrufs) > s:max | cal remove(mrufs, s:max, -1) | en
cal ctrlp#utils#writecache(mrufs, s:cadir, s:cafile) cal ctrlp#utils#writecache(mrufs, s:cadir, s:cafile)
retu map(mrufs, 'fnamemodify(v:val, '':.'')') endf
en " Public {{{1
" Get the list fu! ctrlp#mrufiles#refresh()
let mrufs = ctrlp#utils#readfile(s:cafile) let mrufs = s:readcache()
" Remove non-existent files
if a:0 && a:1 == 1
cal filter(mrufs, '!empty(ctrlp#utils#glob(v:val, 1)) && !s:excl(v:val)') cal filter(mrufs, '!empty(ctrlp#utils#glob(v:val, 1)) && !s:excl(v:val)')
if exists('+ssl') if exists('+ssl')
cal map(mrufs, 'tr(v:val, ''/'', ''\'')') cal map(mrufs, 'tr(v:val, ''/'', ''\'')')
cal filter(mrufs, 'count(mrufs, v:val) == 1') cal filter(mrufs, 'count(mrufs, v:val) == 1')
en en
cal ctrlp#utils#writecache(mrufs, s:cadir, s:cafile) 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 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
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) cal ctrlp#utils#writecache(mrufs, s:cadir, s:cafile)
endf "}}} retu map(mrufs, 'fnamemodify(v:val, '':.'')')
fu! s:excl(fn) "{{{ endf
retu !empty(s:ex) && a:fn =~# s:ex
endf "}}} fu! ctrlp#mrufiles#list()
fu! ctrlp#mrufiles#init() "{{{1 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 let s:locked = 0
aug CtrlPMRUF aug CtrlPMRUF
au! au!
au BufReadPost,BufNewFile,BufWritePost * au BufReadPost,BufNewFile,BufWritePost * cal s:record(expand('<abuf>', 1))
\ cal ctrlp#mrufiles#list(expand('<abuf>', 1)) au BufEnter,BufUnload * cal s:record(expand('<abuf>', 1), 1)
if s:mre
au BufEnter,BufUnload *
\ cal ctrlp#mrufiles#list(expand('<abuf>', 1))
en
au QuickFixCmdPre *vimgrep* let s:locked = 1 au QuickFixCmdPre *vimgrep* let s:locked = 1
au QuickFixCmdPost *vimgrep* let s:locked = 0 au QuickFixCmdPost *vimgrep* let s:locked = 0
aug END 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 endf
"}}} "}}}