1N/A#
1N/A# DIRECTORY MANIPULATION FUNCTIONS PUSHD, POPD AND DIRS
1N/A#
1N/A# Uses global parameters _push_max _push_top _push_stack
1N/Ainteger _push_max=100 _push_top=100
1N/A# Display directory stack -- $HOME displayed as ~
1N/Afunction dirs
1N/A{
1N/A typeset dir="${PWD#$HOME/}"
1N/A case $dir in
1N/A $HOME)
1N/A dir=\~
1N/A ;;
1N/A /*) ;;
1N/A *) dir=\~/$dir
1N/A esac
1N/A print -r - "$dir ${_push_stack[@]}"
1N/A}
1N/A
1N/A# Change directory and put directory on front of stack
1N/Afunction pushd
1N/A{
1N/A typeset dir= type=0
1N/A integer i
1N/A case $1 in
1N/A "") # pushd
1N/A if ((_push_top >= _push_max))
1N/A then print pushd: No other directory.
1N/A return 1
1N/A fi
1N/A type=1 dir=${_push_stack[_push_top]}
1N/A ;;
1N/A +[1-9]|+[1-9][0-9]) # pushd +n
1N/A integer i=_push_top$1-1
1N/A if ((i >= _push_max))
1N/A then print pushd: Directory stack not that deep.
1N/A return 1
1N/A fi
1N/A type=2 dir=${_push_stack[i]}
1N/A ;;
1N/A *) if ((_push_top <= 0))
1N/A then print pushd: Directory stack overflow.
1N/A return 1
1N/A fi
1N/A esac
1N/A case $dir in
1N/A \~*) dir=$HOME${dir#\~}
1N/A esac
1N/A cd "${dir:-$1}" > /dev/null || return 1
1N/A dir=${OLDPWD#$HOME/}
1N/A case $dir in
1N/A $HOME)
1N/A dir=\~
1N/A ;;
1N/A /*) ;;
1N/A *) dir=\~/$dir
1N/A esac
1N/A case $type in
1N/A 0) # pushd name
1N/A _push_stack[_push_top=_push_top-1]=$dir
1N/A ;;
1N/A 1) # pushd
1N/A _push_stack[_push_top]=$dir
1N/A ;;
1N/A 2) # push +n
1N/A type=${1#+} i=_push_top-1
1N/A set -- "${_push_stack[@]}" "$dir" "${_push_stack[@]}"
1N/A shift $type
1N/A for dir
1N/A do (((i=i+1) < _push_max)) || break
1N/A _push_stack[i]=$dir
1N/A done
1N/A esac
1N/A dirs
1N/A}
1N/A
1N/A# Pops the top directory
1N/Afunction popd
1N/A{
1N/A typeset dir
1N/A if ((_push_top >= _push_max))
1N/A then print popd: Nothing to pop.
1N/A return 1
1N/A fi
1N/A case $1 in
1N/A "")
1N/A dir=${_push_stack[_push_top]}
1N/A case $dir in
1N/A \~*) dir=$HOME${dir#\~}
1N/A esac
1N/A cd "$dir" || return 1
1N/A ;;
1N/A +[1-9]|+[1-9][0-9])
1N/A typeset savedir
1N/A integer i=_push_top$1-1
1N/A if ((i >= _push_max))
1N/A then print pushd: Directory stack not that deep.
1N/A return 1
1N/A fi
1N/A while ((i > _push_top))
1N/A do _push_stack[i]=${_push_stack[i-1]}
1N/A i=i-1
1N/A done
1N/A ;;
1N/A *) print pushd: Bad directory.
1N/A return 1
1N/A esac
1N/A unset '_push_stack[_push_top]'
1N/A _push_top=_push_top+1
1N/A dirs
1N/A}