ctrlp.vim / autoload / ctrlp / mrufiles.vim

" =============================================================================
" File:          autoload/ctrlp/mrufiles.vim
" Description:   Most Recently Used Files extension
" Author:        Kien Nguyen <github.com/kien>
" =============================================================================

" Static variables {{{1
let [s:mrbs, s:mrufs] = [[], []]

fu! ctrlp#mrufiles#opts()
	let [pref, opts] = ['g:ctrlp_mruf_', {
		\ 'max': ['s:max', 250],
		\ 'include': ['s:in', ''],
		\ 'exclude': ['s:ex', ''],
		\ 'case_sensitive': ['s:cseno', 1],
		\ 'relative': ['s:re', 0],
		\ 'save_on_update': ['s:soup', 1],
		\ }]
	for [ke, va] in items(opts)
		let [{va[0]}, {pref.ke}] = [pref.ke, exists(pref.ke) ? {pref.ke} : va[1]]
	endfo
endf
cal ctrlp#mrufiles#opts()
" Utilities {{{1
fu! s:excl(fn)
	retu !empty({s:ex}) && a:fn =~# {s:ex}
endf

fu! s:mergelists()
	let diskmrufs = ctrlp#utils#readfile(ctrlp#mrufiles#cachefile())
	cal filter(diskmrufs, 'index(s:mrufs, v:val) < 0')
	let mrufs = s:mrufs + diskmrufs
	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

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, ":.")')
endf

fu! s:record(bufnr)
	if s:locked | retu | en
	let bufnr = a:bufnr + 0
	let bufname = bufname(bufnr)
	if bufnr > 0 && !empty(bufname)
		cal filter(s:mrbs, 'v:val != bufnr')
		cal insert(s:mrbs, bufnr)
		cal s:addtomrufs(bufname)
	en
endf

fu! s:addtomrufs(fname)
	let fn = fnamemodify(a:fname, ':p')
	let fn = exists('+ssl') ? tr(fn, '/', '\') : fn
	if ( !empty({s:in}) && fn !~# {s:in} ) || ( !empty({s:ex}) && fn =~# {s:ex} )
		\ || !empty(getbufvar('^'.fn.'$', '&bt')) || !filereadable(fn) | retu
	en
	if ( {s:cseno} && get(s:mrufs, 0, '') !=# fn )
		\ || ( !{s:cseno} && get(s:mrufs, 0, '') !=? fn )
		cal filter(s:mrufs, 'v:val !='.( {s:cseno} ? '#' : '?' ).' fn')
		cal insert(s:mrufs, fn)
		if {s:soup}
			cal s:savetofile(s:mergelists())
		en
	en
endf

fu! s:savetofile(mrufs)
	cal ctrlp#utils#writecache(a:mrufs, s:cadir, s:cafile)
endf
" Public {{{1
fu! ctrlp#mrufiles#refresh(...)
	let s:mrufs = s:mergelists()
	cal filter(s:mrufs, '!empty(ctrlp#utils#glob(v:val, 1)) && !s:excl(v:val)')
	if exists('+ssl')
		cal map(s:mrufs, 'tr(v:val, "/", "\\")')
		cal filter(s:mrufs, 'count(s:mrufs, v:val) == 1')
	en
	cal s:savetofile(s:mrufs)
	retu a:0 && a:1 == 'raw' ? [] : s:reformat(copy(s:mrufs))
endf

fu! ctrlp#mrufiles#remove(files)
	let s:mrufs = []
	if a:files != []
		let s:mrufs = s:mergelists()
		cal filter(s:mrufs, 'index(a:files, v:val, 0, '.(!{s:cseno}).') < 0')
	en
	cal s:savetofile(s:mrufs)
	retu s:reformat(copy(s:mrufs))
endf

fu! ctrlp#mrufiles#add(fn)
	if !empty(a:fn)
		cal s:addtomrufs(a:fn)
	en
endf

fu! ctrlp#mrufiles#list(...)
	retu a:0 ? a:1 == 'raw' ? s:mergelists() : 0 : s:reformat(s:mergelists())
endf

fu! ctrlp#mrufiles#bufs()
	retu s:mrbs
endf

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

fu! ctrlp#mrufiles#init()
	if !has('autocmd') | retu | en
	let s:locked = 0
	aug CtrlPMRUF
		au!
		au BufAdd,BufEnter,BufLeave,BufWritePost * 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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.