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

Re: Automatic completion on <return>

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"
    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


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

    zle complete-word

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

Barton E. Schaefer

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