sgur
9/18/2013 - 8:49 AM

ctrlp_glob.py

import vim
import re
import os
import os.path
import fnmatch

custom_ignore = vim.eval('g:ctrlp_custom_ignore')
if custom_ignore['dir'][:2] == '\\v':
    custom_ignore['dir'] = custom_ignore['dir'][2 :]
if custom_ignore['file'][:2] == '\\v':
    custom_ignore['file'] = custom_ignore['file'][2 :]
if custom_ignore['link'][:2] == '\\v':
    custom_ignore['link'] = custom_ignore['link'][2 :]

fs_encoding = vim.eval('&termencoding')
progress_fn = vim.Function('ctrlp#progress')
max_depth = int(vim.eval('exists("b:ctrlp_max_depth") ? b:ctrlp_max_depth : g:ctrlp_max_depth'))
wildignore = vim.eval('&wildignore').split(',')

def glob(base_dir):
    start = base_dir.count(os.sep)
    files_path = []
    for root, dirs, files in os.walk(base_dir, topdown=True):
        files_path += filter((lambda x: re.match(custom_ignore["file"], x) == None), map((lambda x: os.path.join(os.path.normpath(root), x).decode(fs_encoding)), files))
        progress_fn(len(files_path), 1)
        for i in reversed(range(len(dirs))):
            depth = os.path.join(os.path.normpath(root), dirs[i]).count(os.sep)
            if depth - start >= max_depth or dirs[i][0] == '.' \
                    or re.match(custom_ignore["dir"], dirs[i]) != None:
                        del dirs[i]
    for ptn in wildignore:
        files_path = filter((lambda x: not fnmatch.fnmatch(x, ptn)), files_path)
    return files_path

files = glob(sys.argv[0].replace('\\ ', ' '))