From e51f59501895b72530c77f9d0ea97a3e99c44b4b Mon Sep 17 00:00:00 2001 From: William Ting Date: Tue, 17 Dec 2013 10:54:55 -0600 Subject: [PATCH] match end for single arguments finished --- bin/autojump | 24 ++++++++++++++++++------ bin/utils.py | 9 +++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/bin/autojump b/bin/autojump index 8daca9d..ff3c2bc 100755 --- a/bin/autojump +++ b/bin/autojump @@ -43,6 +43,7 @@ from utils import first from utils import has_uppercase from utils import is_osx from utils import print_entry +from utils import second VERSION = 'release-v21.8.0' Entry = namedtuple('Entry', ['path', 'weight']) @@ -201,16 +202,27 @@ def find_matches(config, needles, count=1): if not needles: return first(data).path - ignore_case = detect_smartcase(needles) sanitize = lambda x: decode(x).rstrip(os.sep) - needles = imap(sanitize, needles) + needles = map(sanitize, needles) + ignore_case = detect_smartcase(needles) - exact_matches = match_exact_regex(needles, data, ignore_case) + if len(needles) == 1: + # if one needle, match only against the last directory of each path + exact_matches = match_end(first(needles), data, ignore_case) + else: + exact_matches = match_regex(needles, data, ignore_case) return first(exact_matches).path -def match_exact_regex(needles, haystack, ignore_case=False): +def match_end(needle, haystack, ignore_case=False): + """Matches needle against the last directory of path.""" + get_last_dir = lambda entry: second(os.path.split(entry.path)) + has_needle = lambda entry: needle in get_last_dir(entry) + return ifilter(has_needle, haystack) + + +def match_regex(needles, haystack, ignore_case=False): """ Performs an exact match by combining all arguments into a single regex expression and finding matches. @@ -226,8 +238,8 @@ def match_exact_regex(needles, haystack, ignore_case=False): """ regex_needle = '.*' + '.*'.join(needles) + '.*' regex_flags = re.IGNORECASE | re.UNICODE if ignore_case else re.UNICODE - find = lambda haystack: re.search(regex_needle, haystack.path, flags=regex_flags) - return ifilter(find, haystack) + has_needle = lambda haystack: re.search(regex_needle, haystack.path, flags=regex_flags) + return ifilter(has_needle, haystack) def purge_missing_paths(config): diff --git a/bin/utils.py b/bin/utils.py index dc616a7..add0085 100644 --- a/bin/utils.py +++ b/bin/utils.py @@ -88,6 +88,15 @@ def print_entry(path, weight): print(encode_local("%.1f:\t%s" % (weight, path))) +def second(xs): + it = iter(xs) + try: + it.next() + return it.next() + except StopIteration: + return None + + def take(n, iterable): """Return first n items of an iterable.""" return islice(iterable, n)