diff --git a/autojump b/autojump index 8fe7618..502b917 100755 --- a/autojump +++ b/autojump @@ -34,7 +34,11 @@ import os MAX_KEYWEIGHT = 1000 MAX_STORED_PATHS = 600 COMPLETION_SEPARATOR = '__' -CONFIG_DIR = os.environ.get("AUTOJUMP_DATA_DIR", os.path.expanduser("~")) +if "AUTOJUMP_DATA_DIR" in os.environ: + CONFIG_DIR = os.environ.get("AUTOJUMP_DATA_DIR") +else: + xdg_data_dir = os.environ.get('XDG_DATA_HOME') or os.path.join(os.environ['HOME'], '.local', 'share') + CONFIG_DIR=os.path.join(xdg_data_dir, 'autojump') def uniqadd(collection, key): """Adds a key to a list only if it is not already present""" diff --git a/autojump.bash b/autojump.bash index f29b92f..aad91a8 100644 --- a/autojump.bash +++ b/autojump.bash @@ -25,23 +25,24 @@ _autojump() done < <(autojump --bash --completion $cur) } complete -F _autojump j -#data_dir=${XDG_DATA_HOME:-$([ -e ~/.local/share ] && echo ~/.local/share || echo ~)} -data_dir=$([ -e ~/.local/share ] && echo ~/.local/share || echo ~) -export AUTOJUMP_HOME=${HOME} -if [[ "$data_dir" = "${HOME}" ]] + +#determine the data directory according to the XDG Base Directory Specification +if [ -n "$XDG_DATA_HOME" ] then - export AUTOJUMP_DATA_DIR=${data_dir} + export AUTOJUMP_DATA_DIR="$XDG_DATA_HOME/autojump" else - export AUTOJUMP_DATA_DIR=${data_dir}/autojump + export AUTOJUMP_DATA_DIR=~/.local/share/autojump fi + if [ ! -e "${AUTOJUMP_DATA_DIR}" ] then - mkdir "${AUTOJUMP_DATA_DIR}" + mkdir -p "${AUTOJUMP_DATA_DIR}" mv ~/.autojump_py "${AUTOJUMP_DATA_DIR}/autojump_py" 2>>/dev/null #migration mv ~/.autojump_py.bak "${AUTOJUMP_DATA_DIR}/autojump_py.bak" 2>>/dev/null mv ~/.autojump_errors "${AUTOJUMP_DATA_DIR}/autojump_errors" 2>>/dev/null fi +export AUTOJUMP_HOME=${HOME} AUTOJUMP='{ [[ "$AUTOJUMP_HOME" == "$HOME" ]] && (autojump -a "$(pwd -P)"&)>/dev/null 2>>${AUTOJUMP_DATA_DIR}/.autojump_errors;} 2>/dev/null' if [[ ! $PROMPT_COMMAND =~ autojump ]]; then export PROMPT_COMMAND="$AUTOJUMP ; ${PROMPT_COMMAND:-:}" diff --git a/autojump.zsh b/autojump.zsh index f7a170d..1f32269 100644 --- a/autojump.zsh +++ b/autojump.zsh @@ -14,17 +14,17 @@ #You should have received a copy of the GNU General Public License #along with autojump. If not, see . -#local data_dir=${XDG_DATA_HOME:-$([ -e ~/.local/share ] && echo ~/.local/share || echo ~)} -local data_dir=$([ -e ~/.local/share ] && echo ~/.local/share || echo ~) -if [[ "$data_dir" = "${HOME}" ]] +#determine the data directory according to the XDG Base Directory Specification +if [ -n "$XDG_DATA_HOME" ] then - export AUTOJUMP_DATA_DIR=${data_dir} + export AUTOJUMP_DATA_DIR="$XDG_DATA_HOME/autojump" else - export AUTOJUMP_DATA_DIR=${data_dir}/autojump + export AUTOJUMP_DATA_DIR=~/.local/share/autojump fi + if [ ! -e "${AUTOJUMP_DATA_DIR}" ] then - mkdir "${AUTOJUMP_DATA_DIR}" + mkdir -p "${AUTOJUMP_DATA_DIR}" mv ~/.autojump_py "${AUTOJUMP_DATA_DIR}/autojump_py" 2>>/dev/null #migration mv ~/.autojump_py.bak "${AUTOJUMP_DATA_DIR}/autojump_py.bak" 2>>/dev/null mv ~/.autojump_errors "${AUTOJUMP_DATA_DIR}/autojump_errors" 2>>/dev/null diff --git a/jumpapplet b/jumpapplet index a4dfc2e..7290b17 100755 --- a/jumpapplet +++ b/jumpapplet @@ -29,6 +29,15 @@ from autojump import open_dic,get_dic_file defaults={} actions={} +#directory finding helpers, conforming to the XDG Base Directory Specification +def data_dir(): + xdg_data_dir = os.environ.get('XDG_DATA_HOME') or os.path.join(os.environ['HOME'], '.local', 'share') + return os.path.join(xdg_data_dir, 'autojump') + +def config_dir(): + xdg_config_dir = os.environ.get('XDG_CONFIG_HOME') or os.path.join(os.environ['HOME'], '.config') + return os.path.join(xdg_config_dir, 'autojump') + #decorator truff def action(validator,name=None): if name is None: @@ -57,17 +66,22 @@ def has_child_file(filename): return os.path.isfile(os.path.join(path,filename)) return wrapper -#do the work def load_paths(maxpath=10): path_dict=open_dic(get_dic_file()) path_dict=path_dict.items() path_dict.sort(key=lambda x: x[1],reverse=True) return [path for path,score in path_dict[:maxpath]] -def load_settings_file(filename="~/.jumpapplet_py"): +def load_settings_file(): + filename = os.path.join(config_dir(), 'jumpapplet_py') + #migration from old location + old_filename = os.path.join(os.environ['HOME'], '.jumpapplet_py') + if not os.path.exists(filename) and os.path.exists(old_filename): + os.rename(old_filename, filename) + print "loading settings" global defaults - dic_file=os.path.expanduser(filename) + dic_file = filename try: aj_file=open(dic_file,'r') @@ -85,9 +99,16 @@ def load_settings_file(filename="~/.jumpapplet_py"): create_actions() -def save_settings_file(filename="~/.jumpapplet_py"): +def save_settings_file(filename=None): + directory = config_dir() + if not filename: + filename = os.path.join(directory, 'jumpapplet_py') + + if not os.path.exists(directory): + os.makedirs(directory) + print "saving settings" - dic_file=os.path.expanduser(filename) + dic_file= filename aj_file=open(dic_file,'w') cPickle.dump(defaults,aj_file)