1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00

"Eat" paths to avoid multiple patterns matching the same part of the string

example:

if you have /tmp/vv/vv and want to specifically target it, you can now type
j vv vv
and it won't match /tmp/vv.
This commit is contained in:
Joël Schaerer 2011-09-09 13:04:21 +02:00
parent e1ee172437
commit b2985f3255

View File

@ -93,36 +93,49 @@ def clean_dict(sorted_dirs, path_dict):
else: return False else: return False
def match(path, pattern, ignore_case=False, only_end=False): def match(path, pattern, ignore_case=False, only_end=False):
"""Check whether a path matches a particular pattern""" """Check whether a path matches a particular pattern, and return
try: the remaning part of the string"""
if os.path.realpath(os.curdir) == path :
return False
#Sometimes the current path doesn't exist anymore.
#In that case, jump if possible.
except OSError:
pass
if only_end: if only_end:
match_string = "/".join(path.split('/')[-1-pattern.count('/'):]) match_string = "/".join(path.split('/')[-1-pattern.count('/'):])
else: else:
match_string = path match_string = path
if ignore_case: if ignore_case:
does_match = (match_string.lower().find(pattern.lower()) != -1) find_idx = match_string.lower().find(pattern.lower())
else: else:
does_match = (match_string.find(pattern) != -1) find_idx = match_string.find(pattern)
#return True if there is a match and the path exists does_match = (find_idx != -1)
#(useful in the case of external drives, for example) # Eat the path to avoid two patterns matching the same part of the string
return does_match and os.path.exists(path) if does_match:
eaten_path = path[find_idx+len(pattern):]
else:
eaten_path = path
return (does_match, eaten_path)
def find_matches(dirs, patterns, result_list, ignore_case, max_matches): def find_matches(dirs, patterns, result_list, ignore_case, max_matches):
"""Find max_matches paths that match the pattern, """Find max_matches paths that match the pattern,
and add them to the result_list""" and add them to the result_list"""
for path, count in dirs: for path, count in dirs:
if len(result_list) >= max_matches : # Don't jump to where we alread are
break try:
#For the last pattern, only match the end of the pattern if os.path.realpath(os.curdir) == path :
if all(match(path, p, ignore_case, continue
only_end=(n == len(patterns)-1)) for n, p in enumerate(patterns)): #Sometimes the current path doesn't exist anymore.
#In that case, jump if possible.
except OSError:
pass
#If a path doesn't exist, don't jump there
#We still keep it in db in case it's from a removable drive
if not os.path.exists(path):
continue
does_match, eaten_path = True, path
for n,p in enumerate(patterns):
#For the last pattern, only match the end of the pattern
does_match, eaten_path = match(eaten_path, p, ignore_case, only_end=(n == len(patterns)-1))
if not does_match: break
if does_match:
uniqadd(result_list, path) uniqadd(result_list, path)
if len(result_list) >= max_matches :
break
def open_dic(dic_file, error_recovery=False): def open_dic(dic_file, error_recovery=False):
"""Try hard to open the database file, recovering """Try hard to open the database file, recovering