zsh-users
 help / color / mirror / code / Atom feed
From: Dominik Vogt <VOGT@de.ibm.com>
To: zsh-users@zsh.org
Subject: Automatic completion on <return>
Date: Thu, 11 Apr 2013 09:12:16 +0200	[thread overview]
Message-ID: <OFC3CCF1B0.362C13EF-ONC1257B4A.00267D76-C1257B4A.00279747@de.ibm.com> (raw)


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 ^_^  ^_^


             reply	other threads:[~2013-04-11  7:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-11  7:12 Dominik Vogt [this message]
2013-04-11 16:51 ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=OFC3CCF1B0.362C13EF-ONC1257B4A.00267D76-C1257B4A.00279747@de.ibm.com \
    --to=vogt@de.ibm.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).