ctrlp.vim/autoload/ctrlp/mrufiles.vim

126 lines
3.3 KiB
VimL
Raw Normal View History

" =============================================================================
" File: autoload/ctrlp/mrufiles.vim
" Description: Most Recently Used Files extension
" Author: Kien Nguyen <github.com/kien>
" =============================================================================
" Static variables {{{1
fu! ctrlp#mrufiles#opts()
2012-04-04 17:28:37 -04:00
let [pref, opts] = ['g:ctrlp_mruf_', {
\ 'max': ['s:max', 250],
\ 'include': ['s:in', ''],
\ 'exclude': ['s:ex', ''],
\ 'case_sensitive': ['s:csen', 1],
\ 'relative': ['s:re', 0],
\ }]
for [ke, va] in items(opts)
2012-04-04 17:28:37 -04:00
exe 'let' va[0] '=' string(exists(pref.ke) ? eval(pref.ke) : va[1])
endfo
let [s:csen, s:mrbs, s:mrufs] = [s:csen ? '#' : '?', [], []]
if exists('s:locked') | cal ctrlp#mrufiles#init() | en
endf
cal ctrlp#mrufiles#opts()
2012-03-14 21:15:54 -04:00
" Utilities {{{1
fu! s:excl(fn)
retu !empty(s:ex) && a:fn =~# s:ex
endf
fu! s:mergelists()
let diskmrufs = s:readcache()
cal filter(diskmrufs, 'index(s:mrufs, v:val) < 0')
let mrufs = s:mrufs + diskmrufs
if v:version < 702 | cal filter(mrufs, 'count(mrufs, v:val) == 1') | en
retu s:chop(mrufs)
endf
fu! s:chop(mrufs)
if len(a:mrufs) > s:max | cal remove(a:mrufs, s:max, -1) | en
retu a:mrufs
endf
2012-03-14 21:15:54 -04:00
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
2012-03-14 21:15:54 -04:00
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
retu map(a:mrufs, 'fnamemodify(v:val, ":.")')
2012-03-14 21:15:54 -04:00
endf
fu! s:record(bufnr)
2012-03-14 21:15:54 -04:00
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)
retu
en
cal filter(s:mrufs, 'v:val !='.s:csen.' fn')
cal insert(s:mrufs, fn)
let s:mrufs = s:chop(s:mrufs)
endf
fu! s:savetofile(mrufs)
cal ctrlp#utils#writecache(a:mrufs, s:cadir, s:cafile)
2012-03-14 21:15:54 -04:00
endf
" Public {{{1
fu! ctrlp#mrufiles#refresh(...)
2012-04-04 18:15:03 -04:00
let mrufs = s:mergelists()
cal filter(mrufs, '!empty(ctrlp#utils#glob(v:val, 1)) && !s:excl(v:val)')
2012-03-14 21:15:54 -04:00
if exists('+ssl')
2012-04-04 18:15:03 -04:00
cal map(mrufs, 'tr(v:val, "/", "\\")')
cal filter(mrufs, 'count(mrufs, v:val) == 1')
2012-03-14 21:15:54 -04:00
en
2012-04-04 18:15:03 -04:00
cal s:savetofile(mrufs)
retu a:0 && a:1 == 'raw' ? [] : s:reformat(mrufs)
2012-03-14 21:15:54 -04:00
endf
fu! ctrlp#mrufiles#remove(files)
2012-04-04 18:15:03 -04:00
let mrufs = []
2012-03-14 21:15:54 -04:00
if a:files != []
2012-04-04 18:15:03 -04:00
let mrufs = s:mergelists()
cal filter(mrufs, 'index(a:files, v:val) < 0')
2012-03-14 21:15:54 -04:00
en
2012-04-04 18:15:03 -04:00
cal s:savetofile(mrufs)
retu s:reformat(mrufs)
2012-03-14 21:15:54 -04:00
endf
2012-03-15 16:11:55 -04:00
fu! ctrlp#mrufiles#list(...)
if a:0 && a:1 != 'raw' | retu | en
retu a:0 && a:1 == 'raw' ? s:mergelists() : s:reformat(s:mergelists())
2012-03-14 21:15:54 -04:00
endf
fu! ctrlp#mrufiles#bufs()
retu s:mrbs
endf
fu! ctrlp#mrufiles#mrufs()
retu s:mrufs
endf
2012-03-14 21:15:54 -04:00
fu! ctrlp#mrufiles#init()
if !has('autocmd') | retu | en
let s:locked = 0
aug CtrlPMRUF
au!
au BufAdd,BufEnter,BufUnload * cal s:record(expand('<abuf>', 1))
au QuickFixCmdPre *vimgrep* let s:locked = 1
au QuickFixCmdPost *vimgrep* let s:locked = 0
au VimLeavePre * cal s:savetofile(s:mergelists())
aug END
endf
"}}}
" vim:fen:fdm=marker:fmr={{{,}}}:fdl=0:fdc=1:ts=2:sw=2:sts=2