# In zsh, emulate the behavior of pushd with tcsh's dextract option # Version 1.0, written by Timothy R. Eliseo # Don't need the function def if native option exists if ! setopt pushd_extract 2> /dev/null; then pushd() { setopt local_options extended_glob auto_pushd cd_silent zmodload zsh/parameter # Need $dirstack[] # Gather any option flags, duplicating builtin behavior of only recognizing # certain options, and only before non-option arguments. local -a opts while [[ $# -gt 0 && $1 == -[qsLP]## ]]; do opts+=("$1") [[ $1 != *q* ]] || setopt pushd_silent shift done # The chdir/cd builtin, with one argument in [+|-]n form and with # the auto_pushd option set, has the desired stack extract behavior, # instead of pushd's stack rotation. For better error output, we also # check if the index is in the range that would have different behavior than # pushd. This range check is the same regardless of +/- because pushing # either the first or last entry has the same result with extraction or # rotation. if [[ $# -eq 1 && ! -o posix_cd && $1 == [+-][0-9]## && $1 -ne 0 && ${1:1} -lt ${#dirstack[@]} ]]; then # Use chdir to pushd with extract. cd_silent suppresses its normal # output, and then we execute dirs, as pushd would, if appropriate. builtin chdir "${opts[@]}" "$@" && { [[ ! -o interactive || -o pushd_silent ]] || builtin dirs; } else # Otherwise just execute pushd with original args builtin pushd "${opts[@]}" "$@" fi } fi