From: "Benjamin R. Haskell" <zsh@benizi.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: Command not found handler for non-searched commands?
Date: Sun, 29 Apr 2012 21:34:02 -0400 (EDT) [thread overview]
Message-ID: <alpine.LNX.2.01.1204292126440.27115@hp.internal> (raw)
In-Reply-To: <alpine.LNX.2.01.1204291301370.27115@hp.internal>
On Sun, 29 Apr 2012, Benjamin R. Haskell wrote:
> On Sun, 29 Apr 2012, Benjamin R. Haskell wrote:
>
>> On Sun, 29 Apr 2012, Benjamin R. Haskell wrote:
>>
>> > I use autocd quite a bit. And often the first thing I want to do
>> > when starting a new project is to create a directory and cd into
>> > it. I tried creating the following command_not_found_handler:
>> >
>> Okay. Different tack, different problem:
>> [...]
> Final solution ended up as the following [...]:
>
> preexec {
> __last_command=$1
> # ... etc.
> }
>
> trap '
> local dir= create=
> set -- ${=__last_command}
> (( $# == 1 )) || return 1
> [[ $1 == */* ]] || return 1
> dir=${~1}
> read -q "create?Create $dir [y/N]? " || return 1
> mkdir -p $dir || return 1
> cd $dir
> ' ZERR
Still talking to myself, but in case someone else follows my lead, I
found fairly quickly that this wreaks havoc on unrelated non-zero
error codes. (e.g. `false ; echo yay` does not echo 'yay').
After reading the section from the manual on:
trap '' ZERR
vs.
TRAPZERR () { }
yet again, I realized I shouldn't be calling 'return' explicitly
(because it returns from the calling context). So, I'm now at:
preexec {
__last_command=$1
# ... etc.
}
trap '
local dir= choose=
set -- ${=__last_command}
if (( $# == 1 )) && [[ $1 == */* ]] ; then
if read -q "choose?Create $1 [y/N]? " ; then
dir=${~1}
if mkdir -p $dir ; then
cd $dir
fi
fi
fi
' ZERR
This still feels substantially heavier-weight than the
command_not_found_handler I'd been hoping for. But, it seems to be
working.
--
Best,
Ben
prev parent reply other threads:[~2012-04-30 1:34 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-29 16:22 Benjamin R. Haskell
2012-04-29 16:55 ` Benjamin R. Haskell
2012-04-29 17:00 ` Benjamin R. Haskell
2012-04-29 17:01 ` Benjamin R. Haskell
2012-04-30 1:34 ` Benjamin R. Haskell [this message]
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=alpine.LNX.2.01.1204292126440.27115@hp.internal \
--to=zsh@benizi.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).