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

major refactoring to make completion work better

This commit is contained in:
Joël Schaerer 2009-02-18 14:12:13 +01:00
parent 6026f24747
commit 0f457b436d

View File

@ -7,20 +7,12 @@ import getopt
from sys import argv,exit from sys import argv,exit
import os import os
import signal import signal
max_keyweight=1000
def signal_handler(arg1,arg2): def signal_handler(arg1,arg2):
print "Received SIGINT, trying to continue" print "Received SIGINT, trying to continue"
signal.signal(signal.SIGINT,signal_handler) #Don't break on sigint signal.signal(signal.SIGINT,signal_handler) #Don't break on sigint
#add the following to your .bashrc:
"""
PROMPT_COMMAND='autojump.py -a $(pwd)'
function j { cd "$(autojump.py $1)"; }
"""
max_keyweight=1000
def uniqadd(list,key): def uniqadd(list,key):
if key not in list: if key not in list:
list.append(key) list.append(key)
@ -39,6 +31,22 @@ def match(path,pattern,path_dict,re_flags=0):
del path_dict[path] del path_dict[path]
return False return False
def forget(path_dict,dic_file):
"""Gradually forget about directories. Only call from the actual jump since it can take time"""
keyweight=sum(path_dict.values()) #Gradually forget about old directories
if keyweight>max_keyweight:
for k in path_dict.keys():
path_dict[k]*=0.9*max_keyweight/keyweight
cPickle.dump(path_dict,open(dic_file+".tmp",'w'),-1)
import shutil
shutil.copy(dic_file+".tmp",dic_file) #cPickle.dump doesn't seem to be atomic, so this is more secure
def find_matches(dirs,pattern,path_dict,result_list,re_flags):
for path,count in dirs:
if match(path,pattern,path_dict,re_flags):
uniqadd(result_list,path)
#Main code
try: try:
optlist, args = getopt.getopt(argv[1:], 'a',['stat','import','completion']) optlist, args = getopt.getopt(argv[1:], 'a',['stat','import','completion'])
except getopt.GetoptError, e: except getopt.GetoptError, e:
@ -70,52 +78,36 @@ else:
import re import re
completion=False completion=False
userchoice=-1 #3 if the pattern is of the form __pattern__3, otherwise -1 userchoice=-1 #3 if the pattern is of the form __pattern__3, otherwise -1
results=[]
if ('--completion','') in optlist: if ('--completion','') in optlist:
completion=True completion=True
results=[] else:
keyweight=sum(path_dict.values()) #Gradually forget about old directories forget(path_dict,dic_file) #gradually forget about old directories
if keyweight>max_keyweight: if not args: pattern=""
for k in path_dict.keys(): else: pattern=args[-1]
path_dict[k]*=0.9*max_keyweight/keyweight
if not args: args=[''] if len(pattern)>0 and pattern[0]=="/": #if pattern is a full path, jump there
if completion: if not completion : print pattern
argument=args[-1] else:
endmatch=re.search("__([0-9]+)$",argument) endmatch=re.search("__([0-9]+)",pattern)
if endmatch: if endmatch:
userchoice=int(endmatch.group(1)) userchoice=int(endmatch.group(1))
argument=argument[2:] pattern=re.sub("__[0-9]+.*","",pattern)
argument=re.sub("__[0-9]+$","",argument)
else: else:
endmatch=re.match("__(.*)__",argument) endmatch=re.match("(.*)__",pattern)
if endmatch: argument=endmatch.group(1) if endmatch: pattern=endmatch.group(1)
else:
argument=re.sub("^__.*__[0-9]+__","",args[-1]) 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)
found=False dirs=path_dict.items() #we need to recreate the list since the first iteration potentially deletes paths
for path,count in dirs: dirs.sort(key=lambda e:e[1],reverse=True)
if match(path,argument,path_dict): #First look for case-matching path if completion or not results: #if not found, try ignoring case. On completion always show all results
if not completion: find_matches(dirs,pattern,path_dict,results,re_flags=re.IGNORECASE)
print path
found=True
break
else:
uniqadd(results,path)
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)
if not found:
for path,count in dirs:
if match(path,argument,path_dict,re.IGNORECASE): #Then try to ignore case
if not completion:
print path
break
else:
uniqadd(results,path)
if completion:
if userchoice!=-1: if userchoice!=-1:
print results[userchoice] if len(results) > userchoice-1 : print results[userchoice-1]
elif len(results) > 1 and completion:
print " ".join(("%s__%d__%s" % (pattern,n+1,r) for n,r in enumerate(results[:8])))
else: else:
print " ".join(("__%s__%d__%s" % (argument,n,r) for n,r in enumerate(results))) if results : print results[0]
cPickle.dump(path_dict,open(dic_file+".tmp",'w'),-1)
import shutil
shutil.copy(dic_file+".tmp",dic_file) #cPickle.dump doesn't seem to be atomic, so this is more secure