Skip to content

Instantly share code, notes, and snippets.

@sgur
Created June 18, 2013 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgur/5806782 to your computer and use it in GitHub Desktop.
Save sgur/5806782 to your computer and use it in GitHub Desktop.
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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment