Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Global Aliases, but as a function?
- X-seq: zsh-users 27434
- From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Cc: Zach Riggle <zachriggle@xxxxxxxxx>
- Subject: Re: Global Aliases, but as a function?
- Date: Wed, 22 Dec 2021 20:14:39 +0000
- Archived-at: <https://zsh.org/users/27434>
- In-reply-to: <CAH+w=7aLPhTT5znrLxNv9gHZWZGCpGfpHNXR4Fuu2j1tug2sMQ@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- References: <CAMP9c5mpNVRiKMQmHBD9P+S3aUQXXHTYUeWKjU1joN9xcNJNCg@mail.gmail.com> <CAH+w=7ZFOXk39PiinFqarip+_GfwZ090eVSoy_L1JEX1Wx-x6A@mail.gmail.com> <64ee9965-6517-481f-9fb5-683bd48cf220@www.fastmail.com> <CAMP9c5nQS7nwx5LD5WjjSc4f6-jE9xUP+ZDvWPaTMtm=fqzQPA@mail.gmail.com> <CAH+w=7aLPhTT5znrLxNv9gHZWZGCpGfpHNXR4Fuu2j1tug2sMQ@mail.gmail.com>
Bart Schaefer wrote on Mon, Dec 20, 2021 at 17:55:15 -0800:
> You'd be looking at the third argument passed to preexec ("... the
> full text that is being executed") which you will have to take apart
> with something like cmdline=(${(z)3}) to figure out whether it's a
> simple command and whether one of the arguments is "--help".
>
If INTERACTIVE_COMMENTS is set then the taking apart should use ${(zZ+c+)}.
> Assuming it is just one command and has that argument, you've got the
> command name in $cmdline[1] (in my foregoing example) so you're going
> to create function with that name that ends by deleting itself; e.g.
> (naively again, ignoring the "is this a simple command" test):
>
> preexec() {
> local cmdline=(${(z)3})
> if [[ -n $cmdline[(R)--help] ]]; then
> function $cmdline[1] {
> $cmdline[1] "$@" | $LESS
> unfunction $cmdline[1]
The parameter «cmdline» will have been undefined by the time the inner
function runs, and the first line of the inner function would infinite
loop. Therefore:
.
eval "function ${(qq)cmdline[1]} {
unfunction -- ${(q)cmdline[1]}
${(q)cmdline[1]} "\$@" | …
}"
>
For the "is this a simple command?" test, my first sketch would be:
- No command separator tokens (';', '|', '||', etc.)
- First word's «type» is «command» [this rules out reserved words,
assignments, aliases, functions, parameter expansions, history
expansions…]
> }
> fi
> }
>
> And you're done. This works as long as $cmdline[1] isn't already
> defined as a different function, in which case you have to figure out
> how to save and restore that.
«functions -c»?
Cheers,
Daniel
Messages sorted by:
Reverse Date,
Date,
Thread,
Author