From 0547e23411755e3366eacf5d3385025df8833d9b Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 18 Dec 2012 11:32:58 +0100 Subject: [PATCH 1/4] Avoid calls to stat() in find_matches Instead of stating every directory to check if its realpath is the current directory, only do so for any potential matches. --- bin/autojump | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bin/autojump b/bin/autojump index 91ef235..c2f3a22 100755 --- a/bin/autojump +++ b/bin/autojump @@ -368,10 +368,6 @@ def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False): return [] for path, _ in dirs: - # avoid jumping to current directory - if current_dir == decode(os.path.realpath(path)) : - continue - found, tmp = True, path for n, p in enumerate(patterns): # for single/last pattern, only check end of path @@ -382,6 +378,11 @@ def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False): if not found: break if found and (os.path.exists(path) or TESTING): + # avoid jumping to current directory + # (call out to realpath this late to not stat all dirs) + if current_dir == decode(os.path.realpath(path)) : + continue + if path not in results: results.append(path) if len(results) >= max_matches: From 6d98cf886deb26fc7923461d46c293500bebc6c8 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 18 Dec 2012 11:52:57 +0100 Subject: [PATCH 2/4] Do not decode os.path.realpath / path `path` is decoded already (coming from `db`) and this caused the following error: Traceback (most recent call last): File "/home/user/.autojump/bin/autojump", line 460, in if not shell_utility(): sys.exit(1) File "/home/user/.autojump/bin/autojump", line 429, in shell_utility results = find_matches(db, patterns, max_matches, False) File "/home/user/.autojump/bin/autojump", line 374, in find_matches if current_dir == decode(os.path.realpath(path)) : File "/home/user/.autojump/bin/autojump", line 277, in decode return text.decode(encoding, errors) File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeEncodeError: 'ascii' codec can't encode character u'\xb4' in position 52: ordinal not in range(128) --- bin/autojump | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/autojump b/bin/autojump index c2f3a22..2fb54c3 100755 --- a/bin/autojump +++ b/bin/autojump @@ -380,7 +380,7 @@ def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False): if found and (os.path.exists(path) or TESTING): # avoid jumping to current directory # (call out to realpath this late to not stat all dirs) - if current_dir == decode(os.path.realpath(path)) : + if current_dir == os.path.realpath(path): continue if path not in results: From 586ce0deeead5cac87aae3f3d294e3f8fe38d348 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 18 Dec 2012 15:46:24 +0100 Subject: [PATCH 3/4] Skip current_dir in find_matches for fuzzy=False, too --- bin/autojump | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bin/autojump b/bin/autojump index 2fb54c3..0c1a440 100755 --- a/bin/autojump +++ b/bin/autojump @@ -356,6 +356,9 @@ def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False): # collisions: ignore lower weight paths if end not in end_dirs and (os.path.exists(d[0]) or TESTING): + # avoid jumping to current directory + if current_dir == os.path.realpath(d[0]): + continue end_dirs[end] = d[0] # find the first match (heighest weight) From 10a8a3f785f1c9c18734d3714e80957dda0b26e0 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 18 Dec 2012 17:14:53 +0100 Subject: [PATCH 4/4] Avoid file stats in find_matches for fuzzy=True, too --- bin/autojump | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/bin/autojump b/bin/autojump index 0c1a440..17cca82 100755 --- a/bin/autojump +++ b/bin/autojump @@ -355,14 +355,21 @@ def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False): end = d[0].split('/')[-1] # collisions: ignore lower weight paths - if end not in end_dirs and (os.path.exists(d[0]) or TESTING): - # avoid jumping to current directory - if current_dir == os.path.realpath(d[0]): - continue + if end not in end_dirs: end_dirs[end] = d[0] # find the first match (heighest weight) - found = get_close_matches(patterns[-1], end_dirs, 1, .6) + while True: + found = get_close_matches(patterns[-1], end_dirs, n=1, cutoff=.6) + if not found: + break + # avoid jumping to current directory + if (os.path.exists(found[0]) or TESTING) and \ + current_dir != os.path.realpath(found[0]): + break + # continue with the last found directory removed + del end_dirs[found[0]] + if found: found = found[0] results.append(end_dirs[found])