Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions autoload/pymatcher.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import os
import vim, re
import heapq

_escape = dict((c , "\\" + c) for c in ['^','$','.','{','}','(',')','[',']','\\','/','+'])

def CtrlPPyMatch():
items = vim.eval('a:items')
try:
_doCtrlPPyMatch()
except Exception as ex:
import traceback
tb = traceback.format_exc()
vim.command(
'let s:rez = ["Unknown error in matcher", "%s", "%s"]' %
(str(ex), tb))


def _doCtrlPPyMatch():
try:
items = vim.eval('a:items')
except UnicodeDecodeError:
_troubleshootUnicodeInputError()
return

astr = vim.eval('a:str')
lowAstr = astr.lower()
limit = int(vim.eval('a:limit'))
Expand All @@ -17,7 +34,7 @@ def CtrlPPyMatch():
items.remove(crfile)

rez = vim.eval('s:rez')

sep = vim.eval('s:slashsep')

regex = ''
if aregex == 1:
Expand All @@ -43,7 +60,7 @@ def CtrlPPyMatch():

def filename_score(line):
# get filename via reverse find to improve performance
slashPos = line.rfind('/')
slashPos = line.rfind(sep)

if slashPos != -1:
line = line[slashPos + 1:]
Expand Down Expand Up @@ -88,3 +105,16 @@ def path_score(line):
vim.command("let s:regex = '%s'" % regex)
vim.command('let s:rez = [%s]' % ','.join(vimrez))


def _troubleshootUnicodeInputError():
count = vim.eval('len(a:items)')
for i in range(int(count)):
try:
val = vim.eval('a:items[%d]' % i)
except:
vim.command(
'let s:rez = ["Unicode error at item %d: ".a:items[%d],'
'"Line contains invalid characters."]' %
(i, i))
break

46 changes: 28 additions & 18 deletions autoload/pymatcher.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
" Python Matcher

if !has('python') && !has('python3')
echo 'In order to use pymatcher plugin, you need +python or +python3 compiled vim'
echo 'In order to use pymatcher plugin, you need +python or +python3 compiled vim'
endif

let s:plugin_path = escape(expand('<sfile>:p:h'), '\')
Expand All @@ -12,32 +12,42 @@ else
execute 'pyfile ' . s:plugin_path . '/pymatcher.py'
endif

function! pymatcher#PyMatch(items, str, limit, mmode, ispath, crfile, regex)
let s:slashsep = '/'
if has('win32') || has('win64')
if !has('+shellslash') || !&shellslash
let s:slashsep = '\'
endif
endif

call clearmatches()
function! pymatcher#PyMatch(items, str, limit, mmode, ispath, crfile, regex)
call clearmatches()

if a:str == ''
let arr = a:items[0:a:limit]
if !exists('g:ctrlp_match_current_file') && a:ispath && !empty(a:crfile)
call remove(arr, index(arr, a:crfile))
endif
return arr
if a:str == ''
let arr = a:items[0:a:limit]
if !exists('g:ctrlp_match_current_file') && a:ispath && !empty(a:crfile)
call remove(arr, index(arr, a:crfile))
endif
return arr
endif

let s:rez = []
let s:regex = ''
let s:rez = []
let s:regex = ''

try
execute 'python' . (has('python3') ? '3' : '') . ' CtrlPPyMatch()'
catch
return ['Error running matcher', str(v:exception)]
endtry

let s:matchregex = '\v\c'
let s:matchregex = '\v\c'

if a:mmode == 'filename-only'
let s:matchregex .= '[\^\/]*'
endif
if a:mmode == 'filename-only'
let s:matchregex .= '[\^\/\\]*'
endif

let s:matchregex .= s:regex
let s:matchregex .= s:regex

call matchadd('CtrlPMatch', s:matchregex)
call matchadd('CtrlPMatch', s:matchregex)

return s:rez
return s:rez
endfunction