diff --git a/autoload/pymatcher.py b/autoload/pymatcher.py index 6e085b9..c01fa8b 100644 --- a/autoload/pymatcher.py +++ b/autoload/pymatcher.py @@ -1,6 +1,5 @@ import vim, re import heapq -from datetime import datetime def CtrlPPyMatch(): items = vim.eval('a:items') @@ -12,27 +11,23 @@ def CtrlPPyMatch(): rez = vim.eval('s:rez') - specialChars = ['^','$','.','{','}','(',')','[',']','\\','/','+'] + escape = {c : "\\" + c for c in ['^','$','.','{','}','(',')','[',']','\\','/','+']} regex = '' if aregex == 1: regex = astr else: - if len(lowAstr) == 1: - c = lowAstr - if c in specialChars: - c = '\\' + c - regex += c - else: - for c in lowAstr[:-1]: - if c in specialChars: - c = '\\' + c - regex += c + '[^' + c + ']*' - else: - c = lowAstr[-1] - if c in specialChars: - c = '\\' + c - regex += c + # Escape all of the characters as necessary + escaped = [escape.get(c, c) for c in lowAstr] + + # If the string is longer that one character, append a mismatch + # expression to each character (except the last). + if len(lowAstr) > 1: + mismatch = ["[^" + c + "]*" for c in escaped[:-1]] + regex = ''.join([c for pair in zip(escaped[:-1], mismatch) for c in pair]) + + # Append the last character in the string to the regex + regex += escaped[-1] res = [] prog = re.compile(regex) @@ -76,10 +71,7 @@ def path_score(line): else: res = [(path_score(line), line) for line in items] - #rez.extend([line for score, line in heapq.nlargest(limit, res)]) - for score, line in heapq.nlargest(limit, res): - if score != 0: - rez.extend([line]) + rez.extend([line for score, line in heapq.nlargest(limit, res) if score != 0]) # Use double quoted vim strings and escape \ vimrez = ['"' + line.replace('\\', '\\\\').replace('"', '\\"') + '"' for line in rez]