From cb86f0f38635fb2b306c01fe26d9bb545672b5a0 Mon Sep 17 00:00:00 2001 From: Kien N Date: Tue, 19 Jun 2012 00:12:54 +0700 Subject: [PATCH] Use the global caching option for extensions --- autoload/ctrlp.vim | 16 ++++++++++------ autoload/ctrlp/dir.vim | 16 +++++++++------- autoload/ctrlp/rtscript.vim | 21 ++++++++++++++------- doc/ctrlp.txt | 7 +++++-- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/autoload/ctrlp.vim b/autoload/ctrlp.vim index dc56267..2402e0b 100644 --- a/autoload/ctrlp.vim +++ b/autoload/ctrlp.vim @@ -138,8 +138,9 @@ en let s:lash = ctrlp#utils#lash() -" Limiters -let [s:compare_lim, s:nocache_lim] = [3000, 4000] +let s:compare_lim = 3000 + +let s:ficounts = {} " Regexp let s:fpats = { @@ -291,7 +292,7 @@ endf " * Files {{{1 fu! ctrlp#files() let cafile = ctrlp#utils#cachefile() - if g:ctrlp_newcache || !filereadable(cafile) || !s:caching + if g:ctrlp_newcache || !filereadable(cafile) || s:nocache() let [lscmd, s:initcwd, g:ctrlp_allfiles] = [s:lsCmd(), s:dyncwd, []] " Get the list of files if empty(lscmd) @@ -313,6 +314,7 @@ fu! ctrlp#files() let g:ctrlp_allfiles = ctrlp#utils#readfile(cafile) en en + cal extend(s:ficounts, { s:dyncwd : len(g:ctrlp_allfiles) }) retu g:ctrlp_allfiles endf @@ -1765,14 +1767,16 @@ fu! s:mmode() endf " Cache {{{2 fu! s:writecache(cache_file) - let fwrite = len(g:ctrlp_allfiles) > s:nocache_lim - if ( g:ctrlp_newcache || !filereadable(a:cache_file) ) && s:caching || fwrite - if fwrite | let s:caching = 1 | en + if ( g:ctrlp_newcache || !filereadable(a:cache_file) ) && !s:nocache() cal ctrlp#utils#writecache(g:ctrlp_allfiles) let g:ctrlp_newcache = 0 en endf +fu! s:nocache() + retu !s:caching || ( s:caching > 1 && get(s:ficounts, s:dyncwd) < s:caching ) +endf + fu! s:insertcache(str) let [data, g:ctrlp_newcache, str] = [g:ctrlp_allfiles, 1, a:str] if strlen(str) <= strlen(data[0]) diff --git a/autoload/ctrlp/dir.vim b/autoload/ctrlp/dir.vim index b775408..e9a6c1c 100644 --- a/autoload/ctrlp/dir.vim +++ b/autoload/ctrlp/dir.vim @@ -10,12 +10,7 @@ if exists('g:loaded_ctrlp_dir') && g:loaded_ctrlp_dir en let [g:loaded_ctrlp_dir, g:ctrlp_newdir] = [1, 0] -let s:ars = [ - \ 's:maxdepth', - \ 's:maxfiles', - \ 's:compare_lim', - \ 's:glob', - \ ] +let s:ars = ['s:maxdepth', 's:maxfiles', 's:compare_lim', 's:glob', 's:caching'] cal add(g:ctrlp_ext_vars, { \ 'init': 'ctrlp#dir#init('.join(s:ars, ', ').')', @@ -27,6 +22,8 @@ cal add(g:ctrlp_ext_vars, { \ }) let s:id = g:ctrlp_builtins + len(g:ctrlp_ext_vars) + +let s:dircounts = {} " Utilities {{{1 fu! s:globdirs(dirs, depth) let entries = split(globpath(a:dirs, s:glob), "\n") @@ -42,6 +39,10 @@ endf fu! s:max(len, max) retu a:max && a:len > a:max ? 1 : 0 endf + +fu! s:nocache() + retu !s:caching || ( s:caching > 1 && get(s:dircounts, s:cwd) < s:caching ) +endf " Public {{{1 fu! ctrlp#dir#init(...) let s:cwd = getcwd() @@ -50,7 +51,7 @@ fu! ctrlp#dir#init(...) endfo let cadir = ctrlp#utils#cachedir().ctrlp#utils#lash().'dir' let cafile = cadir.ctrlp#utils#lash().ctrlp#utils#cachefile('dir') - if g:ctrlp_newdir || !filereadable(cafile) + if g:ctrlp_newdir || s:nocache() || !filereadable(cafile) let [s:initcwd, g:ctrlp_alldirs] = [s:cwd, []] cal s:globdirs(s:cwd, 0) cal ctrlp#rmbasedir(g:ctrlp_alldirs) @@ -65,6 +66,7 @@ fu! ctrlp#dir#init(...) let g:ctrlp_alldirs = ctrlp#utils#readfile(cafile) en en + cal extend(s:dircounts, { s:cwd : len(g:ctrlp_alldirs) }) retu g:ctrlp_alldirs endf diff --git a/autoload/ctrlp/rtscript.vim b/autoload/ctrlp/rtscript.vim index c55889e..cdae9a1 100644 --- a/autoload/ctrlp/rtscript.vim +++ b/autoload/ctrlp/rtscript.vim @@ -11,7 +11,7 @@ en let [g:loaded_ctrlp_rtscript, g:ctrlp_newrts] = [1, 0] cal add(g:ctrlp_ext_vars, { - \ 'init': 'ctrlp#rtscript#init()', + \ 'init': 'ctrlp#rtscript#init(s:caching)', \ 'accept': 'ctrlp#acceptfile', \ 'lname': 'runtime scripts', \ 'sname': 'rts', @@ -20,9 +20,16 @@ cal add(g:ctrlp_ext_vars, { \ }) let s:id = g:ctrlp_builtins + len(g:ctrlp_ext_vars) + +let s:filecounts = {} +" Utilities {{{1 +fu! s:nocache() + retu !s:caching || ( s:caching > 1 && get(s:filecounts, s:cwd) < s:caching ) +endf " Public {{{1 -fu! ctrlp#rtscript#init() - if g:ctrlp_newrts +fu! ctrlp#rtscript#init(caching) + let [s:caching, s:cwd] = [a:caching, getcwd()] + if g:ctrlp_newrts || s:nocache() \ || !( exists('g:ctrlp_rtscache') && g:ctrlp_rtscache[0] == &rtp ) sil! cal ctrlp#progress('Indexing...') let entries = split(globpath(&rtp, '**/*.*'), "\n") @@ -31,13 +38,13 @@ fu! ctrlp#rtscript#init() el let [entries, results] = g:ctrlp_rtscache[2:3] en - let cwd = getcwd() - if g:ctrlp_newrts - \ || !( exists('g:ctrlp_rtscache') && g:ctrlp_rtscache[:1] == [&rtp, cwd] ) + if g:ctrlp_newrts || s:nocache() + \ || !( exists('g:ctrlp_rtscache') && g:ctrlp_rtscache[:1] == [&rtp, s:cwd] ) if !exists('echoed') | sil! cal ctrlp#progress('Processing...') | en let results = map(copy(entries), 'fnamemodify(v:val, '':.'')') en - let [g:ctrlp_rtscache, g:ctrlp_newrts] = [[&rtp, cwd, entries, results], 0] + let [g:ctrlp_rtscache, g:ctrlp_newrts] = [[&rtp, s:cwd, entries, results], 0] + cal extend(s:filecounts, { s:cwd : len(results) }) retu results endf diff --git a/doc/ctrlp.txt b/doc/ctrlp.txt index daa18b4..5f4fc40 100644 --- a/doc/ctrlp.txt +++ b/doc/ctrlp.txt @@ -164,10 +164,13 @@ Use this to set your own root markers in addition to the default ones (.git, < *'g:ctrlp_use_caching'* -Set this to 0 to disable per-session caching. When disabled, caching will still -be enabled for directories that have more than 4000 files: > +Enable/Disable per-session caching: > let g:ctrlp_use_caching = 1 < + 0 - Disable caching. + 1 - Enable caching. + n - When bigger than 1, disable caching and use the number as the limit to + enable caching again. Note: you can quickly purge the cache by pressing while inside CtrlP. *'g:ctrlp_clear_cache_on_exit'*