1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00
This commit is contained in:
Joel Schaerer 2010-04-26 00:20:55 +02:00
parent 3b5b52d610
commit 35836ada8f

View File

@ -35,15 +35,16 @@ def uniqadd(list,key):
def dicadd(dic,key,increment=1): def dicadd(dic,key,increment=1):
dic[key]=dic.get(key,0.)+increment dic[key]=dic.get(key,0.)+increment
def match(path,pattern,path_dict,re_flags=0): def match(path,pattern,path_dict,ignore_case=False):
try: try:
if os.path.realpath(os.curdir)==path : return False if os.path.realpath(os.curdir)==path : return False
except OSError: #sometimes the current path doesn't exist anymore. In that case, jump if possible. except OSError: #sometimes the current path doesn't exist anymore. In that case, jump if possible.
pass pass
match_string = "/".join(path.split('/')[-1-pattern.count('/'):]) match_string = "/".join(path.split('/')[-1-pattern.count('/'):])
#import re #import re
#if re.search(pattern,match_string),re_flags) is None: #if re.search(pattern,match_string),re.IGNORECASE if ignore_case else 0) is None:
if match_string.find(pattern) == -1: match=match_string.lower().find(pattern.lower()) == -1 if ignore_case else match_string.find(pattern) == -1
if match:
return False return False
else: else:
if os.path.exists(path) : return True if os.path.exists(path) : return True
@ -76,11 +77,11 @@ def forget(path_dict,dic_file):
path_dict[k]*=0.9*max_keyweight/keyweight path_dict[k]*=0.9*max_keyweight/keyweight
save(path_dict,dic_file) save(path_dict,dic_file)
def find_matches(dirs,pattern,path_dict,result_list,re_flags,max_matches): def find_matches(dirs,pattern,path_dict,result_list,ignore_case,max_matches):
"""Find max_matches paths that match the pattern, and add them to the result_list""" """Find max_matches paths that match the pattern, and add them to the result_list"""
for path,count in dirs: for path,count in dirs:
if len(result_list) >= max_matches : break if len(result_list) >= max_matches : break
if match(path,pattern,path_dict,re_flags): if match(path,pattern,path_dict,ignore_case):
uniqadd(result_list,path) uniqadd(result_list,path)
def open_dic(dic_file,error_recovery=False): def open_dic(dic_file,error_recovery=False):
@ -145,11 +146,11 @@ else:
dirs=path_dict.items() dirs=path_dict.items()
dirs.sort(key=lambda e:e[1],reverse=True) dirs.sort(key=lambda e:e[1],reverse=True)
find_matches(dirs,pattern,path_dict,results,re_flags=0,max_matches=9) find_matches(dirs,pattern,path_dict,results,False,max_matches=9)
dirs=path_dict.items() #we need to recreate the list since the first iteration potentially deletes paths dirs=path_dict.items() #we need to recreate the list since the first iteration potentially deletes paths
dirs.sort(key=lambda e:e[1],reverse=True) dirs.sort(key=lambda e:e[1],reverse=True)
if completion or not results: #if not found, try ignoring case. On completion always show all results if completion or not results: #if not found, try ignoring case. On completion always show all results
find_matches(dirs,pattern,path_dict,results,re_flags=re.IGNORECASE,max_matches=9) find_matches(dirs,pattern,path_dict,results,ignore_case=True,max_matches=9)
if dead_dirs and not completion: #save the dict if there were some non-existent directories in the database if dead_dirs and not completion: #save the dict if there were some non-existent directories in the database
save(path_dict,dic_file) save(path_dict,dic_file)