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

123 lines
2.8 KiB
Bash
Raw Normal View History

export AUTOJUMP_SOURCED=1
# set user installation paths
if [[ -d ~/.autojump/bin ]]; then
2014-01-08 04:13:29 +00:00
path=(~/.autojump/bin ${path})
fi
if [[ -d ~/.autojump/functions ]]; then
fpath=(~/.autojump/functions ${fpath})
fi
# set homebrew installation paths
if command -v brew &>/dev/null && [[ -d "$(brew --prefix)/share/zsh/site-functions" ]]; then
2014-01-08 04:13:29 +00:00
fpath=("$(brew --prefix)/share/zsh/site-functions" ${fpath})
fi
2014-01-08 04:13:01 +00:00
# set error file location
if [[ "$(uname)" == "Darwin" ]]; then
export AUTOJUMP_ERROR_PATH=~/Library/autojump/errors.log
elif [[ -n "${XDG_DATA_HOME}" ]]; then
export AUTOJUMP_ERROR_PATH="${XDG_DATA_HOME}/autojump/errors.log"
else
export AUTOJUMP_ERROR_PATH=~/.local/share/autojump/errors.log
fi
if [[ ! -d ${AUTOJUMP_ERROR_PATH:h} ]]; then
mkdir -p ${AUTOJUMP_ERROR_PATH:h}
fi
# change pwd hook
autojump_chpwd() {
if [[ -f "${AUTOJUMP_ERROR_PATH}" ]]; then
2014-10-05 03:02:41 +00:00
autojump --add "$(pwd)" >/dev/null 2>>${AUTOJUMP_ERROR_PATH} &!
else
autojump --add "$(pwd)" >/dev/null &!
fi
2009-03-30 07:34:24 +00:00
}
typeset -gaU chpwd_functions
chpwd_functions+=autojump_chpwd
2013-12-30 23:44:39 +00:00
# default autojump command
j() {
if [[ ${1} == -* ]] && [[ ${1} != "--" ]]; then
autojump ${@}
return
fi
setopt localoptions noautonamedirs
local output="$(autojump ${@})"
if [[ -d "${output}" ]]; then
2016-05-12 03:40:00 +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}"
else
echo "autojump: directory '${@}' not found"
echo "\n${output}\n"
echo "Try \`autojump --help\` for more information."
false
fi
}
2013-12-30 23:44:39 +00:00
# jump to child directory (subdirectory of current path)
jc() {
if [[ ${1} == -* ]] && [[ ${1} != "--" ]]; then
2013-12-30 23:44:39 +00:00
autojump ${@}
return
2013-12-31 00:01:04 +00:00
else
2013-12-31 14:39:14 +00:00
j $(pwd) ${@}
fi
}
2013-12-30 23:44:39 +00:00
# open autojump results in file browser
jo() {
if [[ ${1} == -* ]] && [[ ${1} != "--" ]]; then
2013-12-30 23:44:39 +00:00
autojump ${@}
2013-12-31 14:39:14 +00:00
return
fi
setopt localoptions noautonamedirs
local output="$(autojump ${@})"
if [[ -d "${output}" ]]; then
2013-12-31 00:01:04 +00:00
case ${OSTYPE} in
linux*)
xdg-open "${output}"
2013-12-31 00:01:04 +00:00
;;
darwin*)
open "${output}"
2013-12-31 00:01:04 +00:00
;;
cygwin)
cygstart "" $(cygpath -w -a ${output})
2013-12-31 00:01:04 +00:00
;;
*)
echo "Unknown operating system: ${OSTYPE}" 1>&2
2013-12-31 00:01:04 +00:00
;;
esac
2013-12-31 14:39:14 +00:00
else
echo "autojump: directory '${@}' not found"
echo "\n${output}\n"
2013-12-31 14:39:14 +00:00
echo "Try \`autojump --help\` for more information."
false
2013-11-24 22:26:23 +00:00
fi
}
2013-12-30 23:44:39 +00:00
# open autojump results (child directory) in file browser
jco() {
if [[ ${1} == -* ]] && [[ ${1} != "--" ]]; then
2013-12-30 23:44:39 +00:00
autojump ${@}
return
2013-12-31 14:39:14 +00:00
else
jo $(pwd) ${@}
fi
}