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

@ -40,7 +40,6 @@ fu! s:opts()
unl! g:ctrlp_max_history unl! g:ctrlp_max_history
" Note: wildignore is ignored when using ** " Note: wildignore is ignored when using **
let s:glob = s:dotfiles ? '.*\|*' : '*' 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:maxdepth = min([s:maxdepth, 100])
let g:ctrlp_builtins = 2 let g:ctrlp_builtins = 2
if !empty(s:extensions) | for each in s:extensions if !empty(s:extensions) | for each in s:extensions
@ -781,6 +780,17 @@ fu! s:comptime(s1, s2)
retu time1 == time2 ? 0 : time1 < time2 ? 1 : -1 retu time1 == time2 ? 0 : time1 < time2 ? 1 : -1
endf 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, ...) fu! s:matchlens(str, pat, ...)
if empty(a:pat) || index(['^','$'], a:pat) >= 0 | retu {} | en if empty(a:pat) || index(['^','$'], a:pat) >= 0 | retu {} | en
let st = exists('a:1') ? a:1 : 0 let st = exists('a:1') ? a:1 : 0
@ -811,9 +821,13 @@ endf
fu! s:mixedsort(s1, s2) fu! s:mixedsort(s1, s2)
let [cml, cln] = [s:compmatlen(a:s1, a:s2), s:complen(a:s1, a: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 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)] let [ctm, wrd] = [s:comptime(a:s1, a:s2), s:compword(a:s1, a:s2)]
retu 6 * cml + 3 * ctm + 2 * cln + wrd retu 12 * cml + 6 * par + 3 * ctm + 2 * cln + wrd
en
retu 3 * cml + 2 * par + cln
en en
retu 2 * cml + cln retu 2 * cml + cln
endf endf
@ -864,10 +878,18 @@ fu! s:dirfilter(val)
endf endf
fu! s:parentdir(curr) 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 if parent != a:curr | cal s:setdir(parent) | en
endf 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) fu! s:createparentdirs(arr)
for each in a:arr for each in a:arr
let curr = exists('curr') ? curr.s:lash.each : each let curr = exists('curr') ? curr.s:lash.each : each
@ -877,7 +899,7 @@ fu! s:createparentdirs(arr)
endf endf
fu! s:listdirs(path, parent) 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)') for entry in filter(dirs, 'isdirectory(v:val)')
let str .= a:parent . split(entry, '[\/]')[-1] . "\n" let str .= a:parent . split(entry, '[\/]')[-1] . "\n"
endfo endfo
@ -902,7 +924,7 @@ fu! s:findroot(curr, mark, depth, type)
let s:foundroot = 1 let s:foundroot = 1
en en
el 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 if parent != a:curr | cal s:findroot(parent, a:mark, depth, a:type) | en
en en
endf endf
@ -1158,7 +1180,7 @@ fu! s:walker(max, pos, dir)
endf endf
fu! s:matchsubstr(item, pat) fu! s:matchsubstr(item, pat)
retu match(split(a:item, '[\/]\ze[^\/]\+$')[-1], a:pat) retu match(split(a:item, '[\/]\ze[^\/:]\+$')[-1], a:pat)
endf endf
fu! s:maxfiles(len) fu! s:maxfiles(len)

View File

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

View File

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