sgur
6/18/2013 - 4:18 PM

Sample code to use vim fucntion with `g:ctrlp_user_command`. https://github.com/sgur/ctrlp.vim/commit/8b645c21f1a1c30d72a5aa0f01175a05150

Sample code to use vim fucntion with g:ctrlp_user_command.

https://github.com/sgur/ctrlp.vim/commit/8b645c21f1a1c30d72a5aa0f01175a05150bc8f2

fu! PyGlob(path)
  let path = escape(a:path, '\')
python << EOF
import vim
import re, os, os.path, 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('g:ctrlp_max_depth'))
wildignore = vim.eval('&wildignore').split(',')

base_dir = vim.eval('path').replace('\\ ', ' ')
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)
EOF
  return pyeval('files_path')
endf

let g:ctrlp_user_command = '*PyGlob'