bgneal@7: " ============================================================================= bgneal@7: " File: autoload/ctrlp/mrufiles.vim bgneal@7: " Description: Most Recently Used Files extension bgneal@7: " Author: Kien Nguyen bgneal@7: " ============================================================================= bgneal@7: bgneal@7: " Static variables {{{1 bgneal@7: let [s:mrbs, s:mrufs] = [[], []] bgneal@7: bgneal@7: fu! ctrlp#mrufiles#opts() bgneal@7: let [pref, opts] = ['g:ctrlp_mruf_', { bgneal@7: \ 'max': ['s:max', 250], bgneal@7: \ 'include': ['s:in', ''], bgneal@7: \ 'exclude': ['s:ex', ''], bgneal@7: \ 'case_sensitive': ['s:cseno', 1], bgneal@7: \ 'relative': ['s:re', 0], bgneal@7: \ }] bgneal@7: for [ke, va] in items(opts) bgneal@7: let [{va[0]}, {pref.ke}] = [pref.ke, exists(pref.ke) ? {pref.ke} : va[1]] bgneal@7: endfo bgneal@7: endf bgneal@7: cal ctrlp#mrufiles#opts() bgneal@7: " Utilities {{{1 bgneal@7: fu! s:excl(fn) bgneal@7: retu !empty({s:ex}) && a:fn =~# {s:ex} bgneal@7: endf bgneal@7: bgneal@7: fu! s:mergelists() bgneal@7: let diskmrufs = ctrlp#utils#readfile(ctrlp#mrufiles#cachefile()) bgneal@7: cal filter(diskmrufs, 'index(s:mrufs, v:val) < 0') bgneal@7: let mrufs = s:mrufs + diskmrufs bgneal@7: retu s:chop(mrufs) bgneal@7: endf bgneal@7: bgneal@7: fu! s:chop(mrufs) bgneal@7: if len(a:mrufs) > {s:max} | cal remove(a:mrufs, {s:max}, -1) | en bgneal@7: retu a:mrufs bgneal@7: endf bgneal@7: bgneal@7: fu! s:reformat(mrufs) bgneal@7: if {s:re} bgneal@7: let cwd = exists('+ssl') ? tr(getcwd(), '/', '\') : getcwd() bgneal@7: cal filter(a:mrufs, '!stridx(v:val, cwd)') bgneal@7: en bgneal@7: retu map(a:mrufs, 'fnamemodify(v:val, ":.")') bgneal@7: endf bgneal@7: bgneal@7: fu! s:record(bufnr) bgneal@7: if s:locked | retu | en bgneal@7: let bufnr = a:bufnr + 0 bgneal@7: if bufnr <= 0 | retu | en bgneal@7: let bufname = bufname(bufnr) bgneal@7: if empty(bufname) | retu | en bgneal@7: let fn = fnamemodify(bufname, ':p') bgneal@7: let fn = exists('+ssl') ? tr(fn, '/', '\') : fn bgneal@7: cal filter(s:mrbs, 'v:val != bufnr') bgneal@7: cal insert(s:mrbs, bufnr) bgneal@7: if ( !empty({s:in}) && fn !~# {s:in} ) || ( !empty({s:ex}) && fn =~# {s:ex} ) bgneal@7: \ || !empty(&bt) || !filereadable(fn) | retu bgneal@7: en bgneal@7: cal filter(s:mrufs, 'v:val !='.( {s:cseno} ? '#' : '?' ).' fn') bgneal@7: cal insert(s:mrufs, fn) bgneal@7: endf bgneal@7: bgneal@7: fu! s:savetofile(mrufs) bgneal@7: cal ctrlp#utils#writecache(a:mrufs, s:cadir, s:cafile) bgneal@7: endf bgneal@7: " Public {{{1 bgneal@7: fu! ctrlp#mrufiles#refresh(...) bgneal@7: let s:mrufs = s:mergelists() bgneal@7: cal filter(s:mrufs, '!empty(ctrlp#utils#glob(v:val, 1)) && !s:excl(v:val)') bgneal@7: if exists('+ssl') bgneal@7: cal map(s:mrufs, 'tr(v:val, "/", "\\")') bgneal@7: cal filter(s:mrufs, 'count(s:mrufs, v:val) == 1') bgneal@7: en bgneal@7: cal s:savetofile(s:mrufs) bgneal@7: retu a:0 && a:1 == 'raw' ? [] : s:reformat(copy(s:mrufs)) bgneal@7: endf bgneal@7: bgneal@7: fu! ctrlp#mrufiles#remove(files) bgneal@7: let s:mrufs = [] bgneal@7: if a:files != [] bgneal@7: let s:mrufs = s:mergelists() bgneal@7: cal filter(s:mrufs, 'index(a:files, v:val, 0, '.(!{s:cseno}).') < 0') bgneal@7: en bgneal@7: cal s:savetofile(s:mrufs) bgneal@7: retu s:reformat(copy(s:mrufs)) bgneal@7: endf bgneal@7: bgneal@7: fu! ctrlp#mrufiles#list(...) bgneal@7: retu a:0 ? a:1 == 'raw' ? s:mergelists() : 0 : s:reformat(s:mergelists()) bgneal@7: endf bgneal@7: bgneal@7: fu! ctrlp#mrufiles#bufs() bgneal@7: retu s:mrbs bgneal@7: endf bgneal@7: bgneal@7: fu! ctrlp#mrufiles#cachefile() bgneal@7: if !exists('s:cadir') || !exists('s:cafile') bgneal@7: let s:cadir = ctrlp#utils#cachedir().ctrlp#utils#lash().'mru' bgneal@7: let s:cafile = s:cadir.ctrlp#utils#lash().'cache.txt' bgneal@7: en bgneal@7: retu s:cafile bgneal@7: endf bgneal@7: bgneal@7: fu! ctrlp#mrufiles#init() bgneal@7: if !has('autocmd') | retu | en bgneal@7: let s:locked = 0 bgneal@7: aug CtrlPMRUF bgneal@7: au! bgneal@7: au BufAdd,BufEnter,BufLeave,BufUnload * cal s:record(expand('', 1)) bgneal@7: au QuickFixCmdPre *vimgrep* let s:locked = 1 bgneal@7: au QuickFixCmdPost *vimgrep* let s:locked = 0 bgneal@7: au VimLeavePre * cal s:savetofile(s:mergelists()) bgneal@7: aug END bgneal@7: endf bgneal@7: "}}} bgneal@7: bgneal@7: " vim:fen:fdm=marker:fmr={{{,}}}:fdl=0:fdc=1:ts=2:sw=2:sts=2