diff --git a/download-and-install b/download-and-install index 5e14867..1ae48f3 100755 --- a/download-and-install +++ b/download-and-install @@ -144,13 +144,13 @@ if [[ -z $smartcd_previously_installed ]]; then echo echo "To get started, create a few scripts. Its easy! Try this:" echo - echo " echo 'echo hello there from \$(pwd)' | smartcd edit enter" - echo " echo 'echo goodbye from \$(pwd)' | smartcd edit leave" + echo " echo 'echo hello there from \$PWD' | smartcd edit enter" + echo " echo 'echo goodbye from \$PWD' | smartcd edit leave" echo echo "Then simply leave the directory and come back. For a more practical" echo "example, how about tweaking your PATH?" echo - echo " echo \"autostash PATH=\$(pwd)/bin:\\\$PATH\" | smartcd edit enter" + echo " echo \"autostash PATH=\$PWD/bin:\\\$PATH\" | smartcd edit enter" echo echo "(side note: the quoting rules when editing in this fashion can be a bit" echo "awkward, so feel free to run \`smartcd edit\` interactively too!" diff --git a/lib/core/arrays b/lib/core/arrays index 28c5209..3ed6a01 100644 --- a/lib/core/arrays +++ b/lib/core/arrays @@ -139,7 +139,7 @@ function ashift() { fi } -# Bash requires the array be quoted, zsh requires it not +# Bash requires the array to be quoted, zsh does not. function aunshift() { local var=$1; shift diff --git a/lib/core/completion b/lib/core/completion index b53073e..b545f53 100644 --- a/lib/core/completion +++ b/lib/core/completion @@ -6,9 +6,15 @@ # if [[ -n $ZSH_VERSION ]]; then - autoload -U compinit - compinit - autoload -U +X bashcompinit && bashcompinit + # Only load/call compinit once. + if [[ -z $_comp_dumpfile ]]; then + autoload -U compinit + compinit + fi + # Only load/call bashcompinit once. + if [[ -z $functions[complete] ]]; then + autoload -U +X bashcompinit && bashcompinit + fi fi function _smartcd_completion() { diff --git a/lib/core/smartcd b/lib/core/smartcd index 4003057..a24ef08 100644 --- a/lib/core/smartcd +++ b/lib/core/smartcd @@ -1,4 +1,5 @@ ################################################################################ + # smartcd - cd with some oomph # # Copyright (c) 2009,2012 Dave Olszewski @@ -184,7 +185,7 @@ function smartcd() { case $command in -h|help|'') echo $usage;; cd|pushd|popd) shift; _smartcd $command "$@"; return $?;; - reenter) local dir="$(pwd)" + reenter) local dir="$PWD" _smartcd cd .. _smartcd cd "$dir" ;; @@ -203,7 +204,7 @@ function smartcd() { --global) whence="global";; --system) system="true";; --all) whence="all"; system="both";; - *) echo "$opt unknown" + *) echo "$opt unknown. Available: [--host|--global|--all] [--system]." return ;; esac @@ -211,7 +212,6 @@ function smartcd() { done local type=$1 - shift case $type in enter|leave) ;; bash_enter|bash_leave) type="${type#bash_}";; @@ -219,16 +219,19 @@ function smartcd() { return ;; esac + shift [[ -n $type ]] && _smartcd_file_check "$type" "$whence" "$system" "$command" "$@" ;; template) shift; smartcd_template "$@";; - setup) shift; local setup=$1; shift; + setup) shift; local setup=$1 case $setup in cd|pushd|popd) setup_smartcd_function $setup;; prompt-hook|prompt) setup_smartcd_prompt_hook;; chpwd-hook|chpwd) setup_smartcd_chpwd_hook;; exit-hook|exit) setup_smartcd_exit_hook;; completion) _smartcd_load "completion";; + *) echo "Usage: smartcd $command (cd|pushd|popd|prompt[-hook]|chpwd[-hook]|exit[-hook]|completion)" + return ;; esac ;; on-leave) local code=${2//\\/\\\\}; @@ -449,7 +452,7 @@ function _smartcd() { IFS="$_old_ifs" if (( $rv != 0 )); then return $rv; fi - local whereto="$(pwd)" + local whereto="$PWD" _smartcd_last_run_for="x$whereto" # If zsh, turn autopushd off temporarily to avoid extra pushes @@ -497,18 +500,24 @@ function _smartcd() { apop leave_stack >/dev/null done - local host=${SMARTCD_HOSTNAME:-$(hostname)} + local host=${SMARTCD_HOSTNAME:-${HOST:-$(hostname)}} # Run leave_stack in lowest to highest local stack_size=$(alen leave_stack) : ${stack_size:=0} local smartcd_runmode=leave + + # Set vars used in while loops. + if [[ $stack_size -gt 0 ]]; then + local sysdir=$(_smartcd_sysdir) + fi + while (( $stack_size >= 1 )); do # ashift in a sub-shell doesn't work local smartcd_current_dir="$(afirst leave_stack)" ashift leave_stack >/dev/null - # Set this just in case we are not able to actualy chdir to $smartcd_current_dir + # Set this in case we are not able to actually chdir to $smartcd_current_dir. local varstash_dir="$smartcd_current_dir" if [[ -d "$smartcd_current_dir" ]]; then @@ -528,7 +537,6 @@ function _smartcd() { # can check things out # 1) Run system-wide global leave script - local sysdir=$(_smartcd_sysdir) local confdir="$sysdir/scripts$smartcd_current_dir" if [[ -f "$confdir/bash_leave" ]]; then if (( ${SMARTCD_QUIET:-0} == 0 )); then @@ -663,7 +671,6 @@ function _smartcd() { done # 1) Run system-wide global enter script - local sysdir=$(_smartcd_sysdir) local confdir="$sysdir/scripts$smartcd_current_dir" if [[ -f "$confdir/bash_enter" ]]; then if (( ${SMARTCD_QUIET:-0} == 0 )); then @@ -757,7 +764,7 @@ function _smartcd() { } function _smartcd_hook() { - local cwd="$(pwd)" + local cwd="$PWD" # Skip this if we already ran smartcd for this directory if [[ "x$cwd" != "$_smartcd_last_run_for" ]]; then @@ -815,6 +822,9 @@ function setup_smartcd_chpwd_hook() { return fi apush chpwd_functions _smartcd_hook + else + echo "chpwd hook is not supported for your shell." + return fi } @@ -866,13 +876,13 @@ function _smartcd_file() { if [[ -n $ZSH_VERSION ]]; then setopt localoptions && setopt ksharrays local this_file=${funcsourcetrace[0]} - this_file="${this_file%:*}" + # Remove line number and use absolute path. + this_file="${${this_file%:*}:a}" else local this_file="$BASH_SOURCE" fi - if [[ "${this_file%/.smartcd/lib/core/smartcd}" == $HOME ]]; then - this_file=${this_file/#$HOME/\~} - fi + # Replace $HOME with ~ at the beginning. + this_file=${this_file/#$HOME/\~} echo "$this_file" } diff --git a/lib/core/smartcd_edit b/lib/core/smartcd_edit index f960766..d854017 100644 --- a/lib/core/smartcd_edit +++ b/lib/core/smartcd_edit @@ -1,7 +1,7 @@ # wrapper function to preserve legacy usage function smartcd_edit() { local type="$1" - local dir="${2:-$(pwd)}" + local dir="${2:-$PWD}" case $type in bash_enter|bash_leave) local file=$type; type="${type#bash_}";; @@ -169,7 +169,7 @@ function _smartcd_file_check() { return fi else - dir="$(pwd)" + dir="$PWD" fi if [[ -n $system ]]; then @@ -178,7 +178,7 @@ function _smartcd_file_check() { local base=$(_smartcd_base) fi if [[ $whence = "host" ]]; then - local host=${SMARTCD_HOSTNAME:-$(hostname)} + local host=${SMARTCD_HOSTNAME:-${HOST:-$(hostname)}} base="$base/hosts/$host" fi local file="bash_$type" diff --git a/lib/core/smartcd_template b/lib/core/smartcd_template index 38c7874..a323832 100644 --- a/lib/core/smartcd_template +++ b/lib/core/smartcd_template @@ -68,7 +68,7 @@ EOF if [[ -f "$base/templates/$name" ]]; then echo "Template $name already exists, run \"smartcd template edit $name\" to modify it" else - smartcd_template _init "$name" "$(pwd)" + smartcd_template _init "$name" "$PWD" smartcd_template edit "$name" fi else @@ -137,7 +137,7 @@ EOF local name="$1" if [[ -n $name ]]; then if [[ -f "$base/templates/$name" ]]; then - local current_dir=$(pwd) + local current_dir=$PWD local mode= line= while builtin read -r line; do @@ -201,7 +201,7 @@ EOF esac done < "$base/templates/$name" IFS="$_old_ifs" - _smartcd_exec "$contents" "$(pwd)" + _smartcd_exec "$contents" "$PWD" else echo "Template $name not found" fi diff --git a/lib/core/varstash b/lib/core/varstash index 2ad4afa..a12d6fc 100644 --- a/lib/core/varstash +++ b/lib/core/varstash @@ -71,7 +71,7 @@ function stash() { fi if [[ -n $1 ]] && [[ -z $run_from_smartcd ]] && [[ -z $run_from_autostash ]]; then - local working_dir="${varstash_dir:-$(pwd)}" + local working_dir="${varstash_dir:-$PWD}" local smartcd_dir="$(_smartcd_base)/scripts$working_dir" local help_action="stashing a variable" local help_dir=$smartcd_dir @@ -211,7 +211,7 @@ function stash() { function autostash() { if [[ -n $1 ]] && [[ -z $run_from_smartcd ]]; then - local working_dir="${varstash_dir:-$(pwd)}" + local working_dir="${varstash_dir:-$PWD}" local smartcd_dir="$(_smartcd_base)/scripts$working_dir" local help_action="autostashing a variable" local help_dir=$smartcd_dir @@ -241,7 +241,7 @@ function autostash() { function unstash() { if [[ -n $1 ]] && [[ -z $run_from_smartcd ]] && [[ -z $run_from_autounstash ]]; then - local working_dir=${varstash_dir:-$(pwd)} + local working_dir=${varstash_dir:-$PWD} local smartcd_dir="$(_smartcd_base)/scripts$working_dir" local help_action="unstashing a variable" local help_dir=$smartcd_dir diff --git a/t/smartcd.t b/t/smartcd.t index 1496af4..08872ab 100644 --- a/t/smartcd.t +++ b/t/smartcd.t @@ -1,7 +1,7 @@ # Set up smartcd mkdir -p tmphome oldhome=$HOME -export HOME="$(pwd)/tmphome" +export HOME="$PWD/tmphome" # Load testing library @@ -48,17 +48,17 @@ like "${output-_}" "this is a leaving test" "bash_leave executed successfully us echo | smartcd edit enter "$dir" echo | smartcd edit leave "$dir" -linkdest="$(pwd)/$dir/destination" +linkdest="$PWD/$dir/destination" link="$dir/symlink" mkdir -p "$linkdest" ln -s destination "$link" smartcd cd -P $link -is "_$(pwd)" "_$linkdest" "cd -P still works" +is "_$PWD" "_$linkdest" "cd -P still works" smartcd cd ../.. spacedir="dir with a space" mkdir -p "$spacedir" -smartcd_spacedir="$HOME/.smartcd/scripts$(pwd)/$spacedir" +smartcd_spacedir="$HOME/.smartcd/scripts$PWD/$spacedir" mkdir -p "$smartcd_spacedir" echo 'echo -n "1 "' > "$smartcd_spacedir/bash_enter" echo 'echo 2' > "$smartcd_spacedir/bash_leave" @@ -68,7 +68,7 @@ is "${output-_}" "1 2" "could enter and leave a directory with a space" echo 'echo 4' > "$smartcd_spacedir/bash_leave" spacedir2="dir with a space/subdir" mkdir -p "$spacedir2" -smartcd_spacedir2="$HOME/.smartcd/scripts$(pwd)/$spacedir2" +smartcd_spacedir2="$HOME/.smartcd/scripts$PWD/$spacedir2" mkdir -p "$smartcd_spacedir2" echo 'echo -n "2 "' > "$smartcd_spacedir2/bash_enter" echo 'echo -n "3 "' > "$smartcd_spacedir2/bash_leave" diff --git a/t/varstash.t b/t/varstash.t index 284e227..4ca42d3 100644 --- a/t/varstash.t +++ b/t/varstash.t @@ -109,12 +109,12 @@ is "_$(alast thing)" "_three four" "unstashed array" oldhome=$HOME stash HOME mkdir -p tmphome -export HOME=$(pwd)/tmphome +export HOME=$PWD/tmphome VARSTASH_AUTOCONFIG=1 oldshell=$SHELL autostash SHELL -config_file="$HOME/.smartcd/scripts$(pwd)/bash_enter" +config_file="$HOME/.smartcd/scripts$PWD/bash_enter" config_file_exists=$([[ -f $config_file ]] && echo "yes") like "${config_file_exists-_}" "yes" "created smartcd file"