* Command not found handler for non-searched commands?
@ 2012-04-29 16:22 Benjamin R. Haskell
2012-04-29 16:55 ` Benjamin R. Haskell
0 siblings, 1 reply; 5+ messages in thread
From: Benjamin R. Haskell @ 2012-04-29 16:22 UTC (permalink / raw)
To: Zsh Users
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:
command_not_found_handler () {
local dir create
echo HEREIAM >&2
(( $# == 1 )) || return 1
[[ $1 == */* ]] || return 1
dir=$1
read -q "create?Create $dir [y/N]? " || return 1
mkdir -p $dir || return 1
cd $dir
}
But, it doesn't get called when I need it:
## when I don't need it:
$ fake-command-here
HEREIAM
zsh: command not found: fake-command-here
## when I do need it:
$ ~/tmp/one-off-project
zsh: no such file or directory: /home/bhaskell/tmp/one-off-project
I see from the description of how commands are found that it won't get
called when there's a slash in the command. Is there a way to force it?
Otherwise, is there another, easy way to accomplish my goal?
--
Best,
Ben
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Command not found handler for non-searched commands?
2012-04-29 16:22 Command not found handler for non-searched commands? 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
0 siblings, 2 replies; 5+ messages in thread
From: Benjamin R. Haskell @ 2012-04-29 16:55 UTC (permalink / raw)
To: Zsh Users
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:
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
Now $dir ends up containing '~/tmp/one-off-project', with the '~'
unexpanded. Seeing as how the directory doesn't yet exist, I can't just
glob it. Is there a function to do just named-directory expansion?
--
Best,
Ben
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Command not found handler for non-searched commands?
2012-04-29 16:55 ` Benjamin R. Haskell
@ 2012-04-29 17:00 ` Benjamin R. Haskell
2012-04-29 17:01 ` Benjamin R. Haskell
1 sibling, 0 replies; 5+ messages in thread
From: Benjamin R. Haskell @ 2012-04-29 17:00 UTC (permalink / raw)
To: Zsh Users
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:
>
> 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
>
> Now $dir ends up containing '~/tmp/one-off-project', with the '~'
> unexpanded. Seeing as how the directory doesn't yet exist, I can't
> just glob it. Is there a function to do just named-directory
> expansion?
Answered my own question, kind of... I found the '~' parameter
expansion flag. Still don't 100% understand why pattern expansion
(terminology?) is different than globbing w.r.t. non-existent files.
Final solution ended up as the following (Difference is the 'dir=${~1}'
line):
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
--
Best,
Ben
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Command not found handler for non-searched commands?
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
1 sibling, 1 reply; 5+ messages in thread
From: Benjamin R. Haskell @ 2012-04-29 17:01 UTC (permalink / raw)
To: Zsh Users
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:
>
> 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
>
> Now $dir ends up containing '~/tmp/one-off-project', with the '~' unexpanded.
> Seeing as how the directory doesn't yet exist, I can't just glob it. Is there
> a function to do just named-directory expansion?
Answered my own question, kind of... I found the '~' parameter expansion flag.
Still don't 100% understand why pattern expansion (terminology?) is different
than globbing w.r.t. non-existent files. Final solution ended up as the
following (Difference is the 'dir=${~1}' line):
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
--
Best,
Ben
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Command not found handler for non-searched commands?
2012-04-29 17:01 ` Benjamin R. Haskell
@ 2012-04-30 1:34 ` Benjamin R. Haskell
0 siblings, 0 replies; 5+ messages in thread
From: Benjamin R. Haskell @ 2012-04-30 1:34 UTC (permalink / raw)
To: Zsh Users
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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2012-04-30 1:34 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-29 16:22 Command not found handler for non-searched commands? 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
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).