zsh-users
 help / color / mirror / code / Atom feed
* Automatic completion on <return>
@ 2013-04-11  7:12 Dominik Vogt
  2013-04-11 16:51 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Vogt @ 2013-04-11  7:12 UTC (permalink / raw)
  To: zsh-users


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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Automatic completion on <return>
  2013-04-11  7:12 Automatic completion on <return> Dominik Vogt
@ 2013-04-11 16:51 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2013-04-11 16:51 UTC (permalink / raw)
  To: zsh-users

On Apr 11,  9:12am, Dominik Vogt wrote:
} Subject: Automatic completion on <return>
}
} I've another idea how to extend this, but I'll write another message
} for that.

I think your Subject on this message is actually about the other message?

} I already have a "smart" cd-script that allows to "cd" into files:
} 
[...]
} 
} 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?

There is a command_not_found_handler shell function, but you can't use it
for this because it doesn't run until after the parent shell has forked
once to attempt to execute an external command.

You might be able to do something in the zle-line-finish widget:

    zle-line-finish() {
	setopt localoptions no_nomatch no_nullglob no_cshnullglob
	set -- ${(z)BUFFER}
	if (( $# <= 2 )) && [[ ! -x =$1 ]]
	then BUFFER="cd $BUFFER"
	fi
    }
    zle -N zle-line-finish

This won't work for "automatic cd" attempts buried inside multi-line
constructs, etc., and you probably want some additional tests to avoid
having this fire on the first line of a multi-line input or when there
is an unmatched quote.  Once you have it right, you could change

    BUFFER="cd $BUFFER"

to be

    cd "$@" && BUFFER=

but you likely don't want that to excute at the PS2 prompt (for example).

For your enhancement of this idea, you could override the accept-line
widget to do a similar test and invoke completion on the first word
before (or instead of) actually accepting the line.  This would require
something along the lines of

    CURSOR=$#1
    zle complete-word

and then grab the BUFFER contents again and repeat the -x test.

-- 
Barton E. Schaefer


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-04-11 16:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-11  7:12 Automatic completion on <return> Dominik Vogt
2013-04-11 16:51 ` Bart Schaefer

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).