2019-09-15 06:31:32 +00:00
|
|
|
if [[ -z $AUTOJUMP_SOURCED ]]; then
|
|
|
|
export AUTOJUMP_SOURCED=1
|
2014-10-05 02:41:41 +00:00
|
|
|
|
2019-09-15 06:31:32 +00:00
|
|
|
# Script include
|
|
|
|
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
|
|
|
|
# set user installation paths
|
|
|
|
export PATH="${SCRIPT_DIR}:${PATH}"
|
2013-12-31 15:48:42 +00:00
|
|
|
|
|
|
|
|
2019-09-15 06:31:32 +00:00
|
|
|
# set error file location
|
|
|
|
if [[ "$(uname)" == "Darwin" ]]; then
|
2014-01-08 04:13:01 +00:00
|
|
|
export AUTOJUMP_ERROR_PATH=~/Library/autojump/errors.log
|
2019-09-15 06:31:32 +00:00
|
|
|
elif [[ -n "${XDG_DATA_HOME}" ]]; then
|
2014-01-08 04:13:01 +00:00
|
|
|
export AUTOJUMP_ERROR_PATH="${XDG_DATA_HOME}/autojump/errors.log"
|
2019-09-15 06:31:32 +00:00
|
|
|
else
|
2014-01-08 04:13:01 +00:00
|
|
|
export AUTOJUMP_ERROR_PATH=~/.local/share/autojump/errors.log
|
2019-09-15 06:31:32 +00:00
|
|
|
fi
|
2014-01-08 04:13:01 +00:00
|
|
|
|
2019-09-15 06:31:32 +00:00
|
|
|
if [[ ! -d "$(dirname ${AUTOJUMP_ERROR_PATH})" ]]; then
|
2014-01-11 12:58:07 +00:00
|
|
|
mkdir -p "$(dirname ${AUTOJUMP_ERROR_PATH})"
|
2019-09-15 06:31:32 +00:00
|
|
|
fi
|
2014-01-11 12:58:07 +00:00
|
|
|
|
2014-01-08 04:13:01 +00:00
|
|
|
|
2019-09-15 06:31:32 +00:00
|
|
|
# enable tab completion
|
|
|
|
_autojump() {
|
|
|
|
local cur
|
|
|
|
cur=${COMP_WORDS[*]:1}
|
|
|
|
comps=$(autojump --complete $cur)
|
|
|
|
echo "$comps" | while read i; do
|
|
|
|
COMPREPLY=("${COMPREPLY[@]}" "${i}")
|
|
|
|
done
|
|
|
|
}
|
|
|
|
complete -F _autojump j
|
2011-07-24 19:08:56 +00:00
|
|
|
|
2012-02-08 21:27:43 +00:00
|
|
|
|
2019-09-15 06:31:32 +00:00
|
|
|
# change pwd hook
|
|
|
|
autojump_add_to_database() {
|
2014-01-11 12:58:07 +00:00
|
|
|
if [[ -f "${AUTOJUMP_ERROR_PATH}" ]]; then
|
2019-09-15 06:31:32 +00:00
|
|
|
(autojump --add "$(pwd)" >/dev/null 2>>${AUTOJUMP_ERROR_PATH} &) &>/dev/null
|
2014-01-11 12:58:07 +00:00
|
|
|
else
|
2019-09-15 06:31:32 +00:00
|
|
|
(autojump --add "$(pwd)" >/dev/null &) &>/dev/null
|
2014-01-11 12:58:07 +00:00
|
|
|
fi
|
2019-09-15 06:31:32 +00:00
|
|
|
}
|
2011-09-14 05:56:30 +00:00
|
|
|
|
2019-09-15 06:31:32 +00:00
|
|
|
case $PROMPT_COMMAND in
|
2013-02-01 20:59:34 +00:00
|
|
|
*autojump*)
|
2019-09-15 06:31:32 +00:00
|
|
|
;;
|
2013-02-01 20:59:34 +00:00
|
|
|
*)
|
2019-09-15 06:31:32 +00:00
|
|
|
PROMPT_COMMAND="${PROMPT_COMMAND:+$(echo "${PROMPT_COMMAND}" | awk '{gsub(/; *$/,"")}1') ; }autojump_add_to_database"
|
|
|
|
;;
|
|
|
|
esac
|
2011-09-14 05:56:30 +00:00
|
|
|
|
2013-12-31 15:48:42 +00:00
|
|
|
|
2019-09-15 06:31:32 +00:00
|
|
|
# default autojump command
|
|
|
|
j() {
|
2014-08-23 01:07:27 +00:00
|
|
|
if [[ ${1} == -* ]] && [[ ${1} != "--" ]]; then
|
2019-09-15 06:31:32 +00:00
|
|
|
autojump ${@}
|
|
|
|
return
|
2012-05-27 21:24:49 +00:00
|
|
|
fi
|
2012-04-02 12:58:43 +00:00
|
|
|
|
2014-10-05 02:41:41 +00:00
|
|
|
output="$(autojump ${@})"
|
|
|
|
if [[ -d "${output}" ]]; then
|
2019-09-15 06:31:32 +00:00
|
|
|
if [ -t 1 ]; then # if stdout is a terminal, use colors
|
|
|
|
echo -e "\\033[31m${output}\\033[0m"
|
|
|
|
else
|
|
|
|
echo -e "${output}"
|
|
|
|
fi
|
|
|
|
cd "${output}"
|
2012-04-02 12:58:43 +00:00
|
|
|
else
|
2019-09-15 06:31:32 +00:00
|
|
|
echo "autojump: directory '${@}' not found"
|
|
|
|
echo "\n${output}\n"
|
|
|
|
echo "Try \`autojump --help\` for more information."
|
|
|
|
false
|
2012-04-02 12:58:43 +00:00
|
|
|
fi
|
2019-09-15 06:31:32 +00:00
|
|
|
}
|
2012-12-15 04:58:20 +00:00
|
|
|
|
2013-12-31 15:48:42 +00:00
|
|
|
|
2019-09-15 06:31:32 +00:00
|
|
|
# jump to child directory (subdirectory of current path)
|
|
|
|
jc() {
|
2014-08-23 01:07:27 +00:00
|
|
|
if [[ ${1} == -* ]] && [[ ${1} != "--" ]]; then
|
2019-09-15 06:31:32 +00:00
|
|
|
autojump ${@}
|
|
|
|
return
|
2012-12-15 04:58:20 +00:00
|
|
|
else
|
2019-09-15 06:31:32 +00:00
|
|
|
j $(pwd) ${@}
|
2012-12-15 04:58:20 +00:00
|
|
|
fi
|
2019-09-15 06:31:32 +00:00
|
|
|
}
|
2013-02-01 19:50:52 +00:00
|
|
|
|
2013-12-31 15:48:42 +00:00
|
|
|
|
2019-09-15 06:31:32 +00:00
|
|
|
# open autojump results in file browser
|
|
|
|
jo() {
|
2014-08-23 01:07:27 +00:00
|
|
|
if [[ ${1} == -* ]] && [[ ${1} != "--" ]]; then
|
2019-09-15 06:31:32 +00:00
|
|
|
autojump ${@}
|
|
|
|
return
|
2013-12-31 14:39:14 +00:00
|
|
|
fi
|
|
|
|
|
2014-10-05 02:41:41 +00:00
|
|
|
output="$(autojump ${@})"
|
|
|
|
if [[ -d "${output}" ]]; then
|
2019-09-15 06:31:32 +00:00
|
|
|
case ${OSTYPE} in
|
|
|
|
linux*)
|
|
|
|
xdg-open "${output}"
|
|
|
|
;;
|
|
|
|
darwin*)
|
|
|
|
open "${output}"
|
|
|
|
;;
|
|
|
|
cygwin)
|
|
|
|
cygstart "" $(cygpath -w -a ${output})
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unknown operating system: ${OSTYPE}." 1>&2
|
|
|
|
;;
|
|
|
|
esac
|
2013-12-31 14:39:14 +00:00
|
|
|
else
|
2019-09-15 06:31:32 +00:00
|
|
|
echo "autojump: directory '${@}' not found"
|
|
|
|
echo "\n${output}\n"
|
|
|
|
echo "Try \`autojump --help\` for more information."
|
|
|
|
false
|
2013-02-14 18:59:01 +00:00
|
|
|
fi
|
2019-09-15 06:31:32 +00:00
|
|
|
}
|
2013-02-01 19:50:52 +00:00
|
|
|
|
2013-12-31 15:48:42 +00:00
|
|
|
|
2019-09-15 06:31:32 +00:00
|
|
|
# open autojump results (child directory) in file browser
|
|
|
|
jco() {
|
2014-08-23 01:07:27 +00:00
|
|
|
if [[ ${1} == -* ]] && [[ ${1} != "--" ]]; then
|
2019-09-15 06:31:32 +00:00
|
|
|
autojump ${@}
|
|
|
|
return
|
2013-02-01 19:50:52 +00:00
|
|
|
else
|
2019-09-15 06:31:32 +00:00
|
|
|
jo $(pwd) ${@}
|
2013-02-01 19:50:52 +00:00
|
|
|
fi
|
2019-09-15 06:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Jump around a git repo
|
|
|
|
g() {
|
|
|
|
REPO_ROOT=`git rev-parse --show-toplevel`
|
|
|
|
j "$REPO_ROOT" "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
echo "ERROR: autojump was sourced twice"
|
|
|
|
fi
|