2011-09-07 10:41:43 -04:00
|
|
|
" =============================================================================
|
|
|
|
" File: autoload/ctrlp/mrufiles.vim
|
|
|
|
" Description: Most Recently Used Files extension
|
|
|
|
" Author: Kien Nguyen <github.com/kien>
|
|
|
|
" =============================================================================
|
|
|
|
|
2011-12-02 22:54:18 -05:00
|
|
|
" Static variables {{{1
|
2011-11-04 03:29:10 -04:00
|
|
|
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', ''],
|
2012-04-12 22:58:24 -04:00
|
|
|
\ 'case_sensitive': ['s:cseno', 1],
|
2012-04-04 17:28:37 -04:00
|
|
|
\ 'relative': ['s:re', 0],
|
|
|
|
\ }]
|
2011-11-22 01:38:26 -05:00
|
|
|
for [ke, va] in items(opts)
|
2012-04-16 00:40:21 -04:00
|
|
|
let {va[0]} = exists(pref.ke) ? {pref.ke} : va[1]
|
2011-11-04 03:29:10 -04:00
|
|
|
endfo
|
2012-04-12 22:58:24 -04:00
|
|
|
let [s:csen, s:mrbs, s:mrufs] = [s:cseno ? '#' : '?', [], []]
|
2012-04-03 23:35:35 -04:00
|
|
|
if exists('s:locked') | cal ctrlp#mrufiles#init() | en
|
2011-11-04 03:29:10 -04:00
|
|
|
endf
|
2011-09-18 07:33:37 -04:00
|
|
|
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
|
|
|
|
|
2012-04-07 19:33:05 -04:00
|
|
|
fu! s:mergelists()
|
2012-04-05 12:59:54 -04:00
|
|
|
let diskmrufs = ctrlp#utils#readfile(ctrlp#mrufiles#cachefile())
|
2012-04-07 19:33:05 -04:00
|
|
|
cal filter(diskmrufs, 'index(s:mrufs, v:val) < 0')
|
|
|
|
let mrufs = s:mrufs + diskmrufs
|
|
|
|
retu s:chop(mrufs)
|
2012-04-03 23:35:35 -04:00
|
|
|
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:reformat(mrufs)
|
|
|
|
if s:re
|
|
|
|
let cwd = exists('+ssl') ? tr(getcwd(), '/', '\') : getcwd()
|
|
|
|
cal filter(a:mrufs, '!stridx(v:val, cwd)')
|
2011-11-04 03:29:10 -04:00
|
|
|
en
|
2012-04-03 23:35:35 -04:00
|
|
|
retu map(a:mrufs, 'fnamemodify(v:val, ":.")')
|
2012-03-14 21:15:54 -04:00
|
|
|
endf
|
|
|
|
|
2012-04-03 23:35:35 -04:00
|
|
|
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
|
2012-04-06 20:58:44 -04:00
|
|
|
let bufname = bufname(bufnr)
|
|
|
|
if empty(bufname) | retu | en
|
|
|
|
let fn = fnamemodify(bufname, ':p')
|
2012-03-14 21:15:54 -04:00
|
|
|
let fn = exists('+ssl') ? tr(fn, '/', '\') : fn
|
|
|
|
cal filter(s:mrbs, 'v:val !='.s:csen.' fn')
|
|
|
|
cal insert(s:mrbs, fn)
|
2012-04-06 20:58:44 -04:00
|
|
|
if ( !empty(s:in) && fn !~# s:in ) || ( !empty(s:ex) && fn =~# s:ex )
|
2012-04-07 19:33:05 -04:00
|
|
|
\ || !empty(&bt) || !filereadable(fn) | retu
|
2012-01-09 17:58:59 -05:00
|
|
|
en
|
2012-04-03 23:35:35 -04:00
|
|
|
cal filter(s:mrufs, 'v:val !='.s:csen.' fn')
|
|
|
|
cal insert(s:mrufs, fn)
|
|
|
|
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
|
2012-04-03 19:17:11 -04:00
|
|
|
fu! ctrlp#mrufiles#refresh(...)
|
2012-04-07 18:08:35 -04:00
|
|
|
let s:mrufs = s:mergelists()
|
|
|
|
cal filter(s: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-07 18:08:35 -04:00
|
|
|
cal map(s:mrufs, 'tr(v:val, "/", "\\")')
|
|
|
|
cal filter(s:mrufs, 'count(s:mrufs, v:val) == 1')
|
2012-03-14 21:15:54 -04:00
|
|
|
en
|
2012-04-07 18:08:35 -04:00
|
|
|
cal s:savetofile(s:mrufs)
|
|
|
|
retu a:0 && a:1 == 'raw' ? [] : s:reformat(copy(s:mrufs))
|
2012-03-14 21:15:54 -04:00
|
|
|
endf
|
|
|
|
|
|
|
|
fu! ctrlp#mrufiles#remove(files)
|
2012-04-07 18:08:35 -04:00
|
|
|
let s:mrufs = []
|
2012-03-14 21:15:54 -04:00
|
|
|
if a:files != []
|
2012-04-07 18:08:35 -04:00
|
|
|
let s:mrufs = s:mergelists()
|
2012-04-12 22:58:24 -04:00
|
|
|
cal filter(s:mrufs, 'index(a:files, v:val, 0, '.(!s:cseno).') < 0')
|
2012-03-14 21:15:54 -04:00
|
|
|
en
|
2012-04-07 18:08:35 -04:00
|
|
|
cal s:savetofile(s:mrufs)
|
|
|
|
retu s:reformat(copy(s:mrufs))
|
2012-03-14 21:15:54 -04:00
|
|
|
endf
|
|
|
|
|
2012-03-15 16:11:55 -04:00
|
|
|
fu! ctrlp#mrufiles#list(...)
|
2012-04-07 19:33:05 -04:00
|
|
|
retu a:0 ? a:1 == 'raw' ? s:mergelists() : 0 : s:reformat(s:mergelists())
|
2012-03-14 21:15:54 -04:00
|
|
|
endf
|
|
|
|
|
|
|
|
fu! ctrlp#mrufiles#bufs()
|
|
|
|
retu s:mrbs
|
|
|
|
endf
|
|
|
|
|
2012-04-05 12:59:54 -04:00
|
|
|
fu! ctrlp#mrufiles#cachefile()
|
|
|
|
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
|
|
|
|
retu s:cafile
|
|
|
|
endf
|
|
|
|
|
2012-03-14 21:15:54 -04:00
|
|
|
fu! ctrlp#mrufiles#init()
|
|
|
|
if !has('autocmd') | retu | en
|
2011-09-07 10:41:43 -04:00
|
|
|
let s:locked = 0
|
|
|
|
aug CtrlPMRUF
|
|
|
|
au!
|
2012-04-03 23:35:35 -04:00
|
|
|
au BufAdd,BufEnter,BufUnload * cal s:record(expand('<abuf>', 1))
|
2011-11-04 03:29:10 -04:00
|
|
|
au QuickFixCmdPre *vimgrep* let s:locked = 1
|
2011-09-07 10:41:43 -04:00
|
|
|
au QuickFixCmdPost *vimgrep* let s:locked = 0
|
2012-04-03 23:35:35 -04:00
|
|
|
au VimLeavePre * cal s:savetofile(s:mergelists())
|
2011-09-07 10:41:43 -04:00
|
|
|
aug END
|
2011-12-02 22:54:18 -05:00
|
|
|
endf
|
|
|
|
"}}}
|
2011-09-07 10:41:43 -04:00
|
|
|
|
2011-12-02 22:54:18 -05:00
|
|
|
" vim:fen:fdm=marker:fmr={{{,}}}:fdl=0:fdc=1:ts=2:sw=2:sts=2
|