Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Automatic completion on <return>



I already have a "smart" cd-script that allows to "cd" into files:

cd with 0 parameters:

  Unchanged.

cd with 1 parameters:

  If the parameter is a directory name, cd into it.
  If the parameter is a file name, cd into the directory containing
  the file.
  If the parameter is a file name that ends with .tar.gz, .tgz etc.
  and a directory with the same name without the suffix exists, cd
  into that (i.e. cd into extracted archives).

cd with 2 parameters:

  I've forgotten what this does.  That stuff does not look like my
  coding style.  :-)

Now I wonder how this logic could be extended to work with the autocd
option.  Is it possible to augment command execution with a script.  Is
there any "hook" function that is automatically called if a command
from the command line cannot be executed because it does not exist?

I've another idea how to extend this, but I'll write another message
for that.

-- BEGIN custom cd
function cd ()
{
        case $# in
            0)
                builtin cd
                ;;
            1)
                VAR="$1"
                DIR=`dirname "$1"`
                if [[ -d "$1" ]]; then
                    builtin cd "$1"
                elif STRIP="$DIR/`basename \"$VAR\" :`" &&
                        [[ -d "$STRIP" ]]; then
                    builtin cd "$STRIP"
                elif STRIP="$DIR/`basename \"$VAR\" .tar.gz`" &&
                        [[ -d "$STRIP" ]]; then
                    builtin cd "$STRIP"
                elif STRIP="$DIR/`basename \"$VAR\" .tar.bz2`" &&
                        [[ -d "$STRIP" ]]; then
                    builtin cd "$STRIP"
                elif STRIP="$DIR/`basename \"$VAR\" .tgz`" &&
                        [[ -d "$STRIP" ]]; then
                    builtin cd "$STRIP"
                elif STRIP="$DIR/`basename \"$VAR\" .TGZ`" &&
                        [[ -d "$STRIP" ]]; then
                    builtin cd "$STRIP"
                elif STRIP="$DIR/`basename \"$VAR\" .zip`" &&
                        [[ -d "$STRIP" ]]; then
                    builtin cd "$STRIP"
                elif STRIP="$DIR/`basename \"$VAR\" .ZIP`" &&
                        [[ -d "$STRIP" ]]; then
                    builtin cd "$STRIP"
                elif [[ -f "$1" ]]; then
                    builtin cd "$DIR"
                elif STRIP="$VAR[$#VAR]" && [[ -f "$STRIP" ]]; then
                    builtin cd "$DIR"
                else
                    builtin cd "$1"
                fi
                ;;
            2)
                integer i=0
                local target="$1"
                if ((ARGC > 1)); then
                        while ((++i)) &&
                                [[ "$PWD" != "${(SI-$i-)PWD#${(q)1}}" ]]
                        do
                                target="${(SI-$i-)PWD/${(q)1}/$2}"
                                [[ -d "$target" && -r "$target" ]] && break
                                target="${(SI-$i-)PWD//${(q)1}/$2}"
                                [[ -d "$target" && -r "$target" ]] && break
                                target=
                        done
                fi
                builtin cd "${target:-${PWD/${(q)1}/$2}}"
                ;;
            **)
                builtin cd "$@"
                ;;
        esac
}
-- END custom cd

Ciao

Dominik ^_^  ^_^



Messages sorted by: Reverse Date, Date, Thread, Author