Add sorting by parent dir

* Add a simple sorting algo giving more weight to files
  with same parent dir. Close #31.
* Change g:ctrlp_cache_dir to accept full path. Close #32.
* Fix a bug with listdirs() introduced in previous commit.
This commit is contained in:
Kien N 2011-11-14 13:58:09 +07:00
parent 0af1cdc791
commit 9da5b08e04
3 changed files with 50 additions and 31 deletions

View File

@ -39,9 +39,8 @@ fu! s:opts()
\ : exists('+hi') ? &hi : 20
unl! g:ctrlp_max_history
" Note: wildignore is ignored when using **
let s:glob = s:dotfiles ? '.*\|*' : '*'
let s:cache_dir = exists('g:ctrlp_cache_dir') ? g:ctrlp_cache_dir : $HOME
let s:maxdepth = min([s:maxdepth, 100])
let s:glob = s:dotfiles ? '.*\|*' : '*'
let s:maxdepth = min([s:maxdepth, 100])
let g:ctrlp_builtins = 2
if !empty(s:extensions) | for each in s:extensions
exe 'ru autoload/ctrlp/'.each.'.vim'
@ -781,6 +780,17 @@ fu! s:comptime(s1, s2)
retu time1 == time2 ? 0 : time1 < time2 ? 1 : -1
endf
fu! s:comparent(s1, s2)
" By same parent dir
if match(s:crfpath, escape(getcwd(), '.^$*\')) >= 0
let [as1, as2] = [getcwd().s:lash.a:s1, getcwd().s:lash.a:s2]
let [loc1, loc2] = [s:getparent(as1), s:getparent(as2)]
if loc1 == s:crfpath && loc2 != s:crfpath | retu -1 | en
if loc2 == s:crfpath && loc1 != s:crfpath | retu 1 | en
en
retu 0
endf
fu! s:matchlens(str, pat, ...)
if empty(a:pat) || index(['^','$'], a:pat) >= 0 | retu {} | en
let st = exists('a:1') ? a:1 : 0
@ -811,9 +821,13 @@ endf
fu! s:mixedsort(s1, s2)
let [cml, cln] = [s:compmatlen(a:s1, a:s2), s:complen(a:s1, a:s2)]
if s:itemtype =~ '0\|1\|2' && s:height < 21
let [ctm, wrd] = [s:comptime(a:s1, a:s2), s:compword(a:s1, a:s2)]
retu 6 * cml + 3 * ctm + 2 * cln + wrd
if s:itemtype < 3 && s:height < 51
let par = s:comparent(a:s1, a:s2)
if s:height < 21
let [ctm, wrd] = [s:comptime(a:s1, a:s2), s:compword(a:s1, a:s2)]
retu 12 * cml + 6 * par + 3 * ctm + 2 * cln + wrd
en
retu 3 * cml + 2 * par + cln
en
retu 2 * cml + cln
endf
@ -864,10 +878,18 @@ fu! s:dirfilter(val)
endf
fu! s:parentdir(curr)
let parent = substitute(a:curr, '[\/]\zs[^\/]\+[\/]\?$', '', '')
let parent = s:getparent(a:curr)
if parent != a:curr | cal s:setdir(parent) | en
endf
fu! s:getparent(item)
retu split(a:item, '[\/]\ze[^\/]\+[\/:]\?$')[0]
endf
fu! s:getgrand(item)
retu split(a:item, '[\/]\ze[^\/]\+[\/][^\/]\+[\/:]\?$')[0]
endf
fu! s:createparentdirs(arr)
for each in a:arr
let curr = exists('curr') ? curr.s:lash.each : each
@ -877,7 +899,7 @@ fu! s:createparentdirs(arr)
endf
fu! s:listdirs(path, parent)
let [str, dirs] = [split(s:glbpath(a:path, '*', 1), "\n"), '']
let [str, dirs] = ['', split(s:glbpath(a:path, '*', 1), "\n")]
for entry in filter(dirs, 'isdirectory(v:val)')
let str .= a:parent . split(entry, '[\/]')[-1] . "\n"
endfo
@ -902,7 +924,7 @@ fu! s:findroot(curr, mark, depth, type)
let s:foundroot = 1
en
el
let parent = substitute(a:curr, '[\/]\zs[^\/]\+[\/:]\?$', '', '')
let parent = s:getparent(a:curr)
if parent != a:curr | cal s:findroot(parent, a:mark, depth, a:type) | en
en
endf
@ -1158,7 +1180,7 @@ fu! s:walker(max, pos, dir)
endf
fu! s:matchsubstr(item, pat)
retu match(split(a:item, '[\/]\ze[^\/]\+$')[-1], a:pat)
retu match(split(a:item, '[\/]\ze[^\/:]\+$')[-1], a:pat)
endf
fu! s:maxfiles(len)

View File

@ -1,58 +1,55 @@
" =============================================================================
" File: autoload/ctrlp/utils.vim
" Description: Utility functions
" Description: Utilities
" Author: Kien Nguyen <github.com/kien>
" =============================================================================
" Static variables {{{
fu! ctrlp#utils#lash()
retu &ssl || !exists('+ssl') ? '/' : '\'
endf
let s:lash = ctrlp#utils#lash()
fu! ctrlp#utils#opts()
let s:cache_dir = exists('g:ctrlp_cache_dir') ? g:ctrlp_cache_dir : $HOME
let s:cache_dir = exists('g:ctrlp_cache_dir') ?
\ isdirectory(g:ctrlp_cache_dir.s:lash.'.ctrlp_cache')
\ ? g:ctrlp_cache_dir.s:lash.'.ctrlp_cache'
\ : g:ctrlp_cache_dir : $HOME.s:lash.'.ctrlp_cache'
endf
cal ctrlp#utils#opts()
"}}}
" Files and Directories {{{
fu! ctrlp#utils#cachedir()
retu exists('*mkdir') ? s:cache_dir.ctrlp#utils#lash().'.ctrlp_cache' : s:cache_dir
retu s:cache_dir
endf
fu! ctrlp#utils#cachefile()
let cache_file = substitute(getcwd(), '\([\/]\|^\a\zs:\)', '%', 'g').'.txt'
retu ctrlp#utils#cachedir().ctrlp#utils#lash().cache_file
retu s:cache_dir.s:lash.cache_file
endf
fu! ctrlp#utils#readfile(file)
if filereadable(a:file)
let data = readfile(a:file)
if empty(data) || type(data) != 3
unl data | let data = []
en
if empty(data) || type(data) != 3 | unl data | let data = [] | en
retu data
en
retu []
endf
fu! ctrlp#utils#mkdir(dir)
if exists('*mkdir') && !isdirectory(a:dir)
sil! cal mkdir(a:dir)
en
if exists('*mkdir') && !isdirectory(a:dir) | sil! cal mkdir(a:dir) | en
endf
fu! ctrlp#utils#writecache(lines, ...)
let cache_dir = exists('a:1') ? a:1 : ctrlp#utils#cachedir()
let cache_dir = exists('a:1') ? a:1 : s:cache_dir
cal ctrlp#utils#mkdir(cache_dir)
" write cache
if isdirectory(cache_dir)
sil! cal writefile(a:lines, exists('a:2') ? a:2 : ctrlp#utils#cachefile())
if !exists('a:1') || !exists('a:2')
let g:ctrlp_newcache = 0
en
if !exists('a:1') || !exists('a:2') | let g:ctrlp_newcache = 0 | en
en
endf
fu! ctrlp#utils#lash()
retu &ssl || !exists('+ssl') ? '/' : '\'
endf
fu! ctrlp#utils#glob(...)
retu call('glob', v:version > 701 ? [a:1, a:2] : [a:1])
endf

View File

@ -111,8 +111,8 @@ Set this to 0 to enable cross-sessions caching: >
<
*'g:ctrlp_cache_dir'*
Set the parent directory for the '.ctrlp_cache' directory: >
let g:ctrlp_cache_dir = $HOME
Set the directory to store the cache files: >
let g:ctrlp_cache_dir = $HOME.'/.ctrlp_cache'
<
*'g:ctrlp_prompt_mappings'*