zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zach Riggle <zachriggle@gmail.com>
Cc: Daniel Shahaf <d.s@daniel.shahaf.name>, Zsh Users <zsh-users@zsh.org>
Subject: Re: Global Aliases, but as a function?
Date: Mon, 20 Dec 2021 17:55:15 -0800	[thread overview]
Message-ID: <CAH+w=7aLPhTT5znrLxNv9gHZWZGCpGfpHNXR4Fuu2j1tug2sMQ@mail.gmail.com> (raw)
In-Reply-To: <CAMP9c5nQS7nwx5LD5WjjSc4f6-jE9xUP+ZDvWPaTMtm=fqzQPA@mail.gmail.com>

On Mon, Dec 20, 2021 at 4:56 PM Zach Riggle <zachriggle@gmail.com> wrote:
>
> Where can I read in the documentation about the preexec wrapper and
> specifically how to modify the command being executed?

Very little of this is just going to fall out of the documentation for you.

The preexec function (and the preexec_functions array) are described
in 9.3.1 Hook Functions, which is in "man zshmisc".  There is also doc
about the add-zsh-hook helper function in 26.2.5 Manipulating Hook
Functions (man zshcontrib).

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

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]
    }
  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.


  reply	other threads:[~2021-12-21  1:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-18 10:38 Zach Riggle
2021-12-18 22:40 ` Bart Schaefer
2021-12-20 18:42   ` Daniel Shahaf
2021-12-20 20:04     ` Bart Schaefer
2021-12-20 20:11       ` Roman Perepelitsa
2021-12-20 20:24         ` Bart Schaefer
2021-12-21  0:57           ` Zach Riggle
2021-12-21  0:56     ` Zach Riggle
2021-12-21  1:55       ` Bart Schaefer [this message]
2021-12-22 20:14         ` Daniel Shahaf

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='CAH+w=7aLPhTT5znrLxNv9gHZWZGCpGfpHNXR4Fuu2j1tug2sMQ@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=d.s@daniel.shahaf.name \
    --cc=zachriggle@gmail.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).