From bcd5476c6d7e61d20b1decd8c45c4bc3ff3b5928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Schaerer?= Date: Wed, 18 Feb 2009 15:13:29 +0100 Subject: [PATCH] merge autojump_completion into autojump.sh ; fix atomic write ; avoid risk of extreme slowdown by multiple autojumps in prompt command --- autojump | 11 +++++++---- autojump.sh | 11 ++++++++++- autojump_completion | 9 --------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/autojump b/autojump index f0114bc..c97c00f 100755 --- a/autojump +++ b/autojump @@ -31,15 +31,18 @@ def match(path,pattern,path_dict,re_flags=0): del path_dict[path] return False +def save(path_dict,dic_file): + 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 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 + save(path_dict,dic_file) def find_matches(dirs,pattern,path_dict,result_list,re_flags): for path,count in dirs: @@ -63,7 +66,7 @@ except IOError: if ('-a','') in optlist: dicadd(path_dict,args[-1]) - cPickle.dump(path_dict,open(dic_file,'w'),-1) + save(path_dict,dic_file) elif ('--stat','') in optlist: a=path_dict.items() a.sort(key=lambda e:e[1]) diff --git a/autojump.sh b/autojump.sh index 81ec11f..a04e222 100755 --- a/autojump.sh +++ b/autojump.sh @@ -1,7 +1,16 @@ #This shell snippet sets the prompt command and the necessary aliases #Only excecute if the shell is bash and it is interactive if [ $SHELL = "/bin/bash" ] && [ -n "$PS1" ]; then - export PROMPT_COMMAND='autojump -a "$(pwd -P)";'"$PROMPT_COMMAND" + _autojump() + { + local cur + COMPREPLY=() + cur=${COMP_WORDS[1]} + IFS=$'\n' read -d '' -a COMPREPLY < <(autojump --completion "$cur") + return 0 + } + complete -F _autojump j + export PROMPT_COMMAND='autojump -a "$(pwd -P)"' alias jumpstat="autojump --stat" function j { new_path="$(autojump $@)";if [ -n "$new_path" ]; then echo -e "\\033[31m${new_path}\\033[0m"; echo; cd "$new_path";fi } fi diff --git a/autojump_completion b/autojump_completion index e3dd1a5..fb198b0 100644 --- a/autojump_completion +++ b/autojump_completion @@ -2,12 +2,3 @@ # # $Id: unrar,v 1.4 2004/07/05 23:37:47 ianmacd Exp $ -_autojump() -{ - local cur - COMPREPLY=() - cur=${COMP_WORDS[1]} - IFS=$'\n' read -d '' -a COMPREPLY < <(autojump --completion "$cur") - return 0 -} -complete -F _autojump j