zsh-users
 help / color / mirror / code / Atom feed
From: Philippe Altherr <philippe.altherr@gmail.com>
To: Thomas Lauer <thomas.lauer@virgin.net>
Cc: Zsh-Users List <zsh-users@zsh.org>
Subject: Re: execute or print one-liner
Date: Wed, 30 Nov 2022 17:18:28 +0100	[thread overview]
Message-ID: <CAGdYchsLQRL6Dq0rUDf_hXuVVF_TypKWazBHmrx4atdbCOgpUw@mail.gmail.com> (raw)
In-Reply-To: <uqneoh9765lvqo5p9o53n0rftushp024k7@tlc.com>

[-- Attachment #1: Type: text/plain, Size: 1689 bytes --]

Here is an idea:

# Helper function to define expandable functions
exec-or-expand() { if [[ -n $EXPAND ]]; then print -z "$@"; else "$@"; fi;
}

# Command to force the expansion of a function defined with exec-or-expand
expand() { EXPAND=1 "$@"; }

# Example of expandable function
f1() { exec-or-expand echo blahblah -o1 -o2 $1 -o3 $2 $3 }

% f1 a b c
blahblah -o1 -o2 a -o3 b c
% expand f1 a b c
% echo blahblah -o1 -o2 a -o3 b c
blahblah -o1 -o2 a -o3 b c

Caveat: probably won't do what's desired if redirections are used.

Philippe


On Wed, Nov 30, 2022 at 3:10 PM Thomas Lauer <thomas.lauer@virgin.net>
wrote:

> Hi folks,
>
> I have dozens, if not hundreds, of one-liners, many as aliases and many
> others as simple one-line functions (used whenever some parameters need
> a little massaging before execution). That's working very well.
>
> I have early on stumbled across a zsh function that would expand an
> alias on the command line so that I can edit or add stuff and I use that
> for many aliases. However, I just can't find a similar thing for
> one-line functions, ie a function that will just print the one-line
> function body to the command line but not execute it. Given
>
> f1() { blahblah -o1 -o2 $1 -o3 $2 $3 }
>
> executing f1() with three parameters is simple:
>
> f1 a b c
>
> What I'm looking for is a way to have f1's body injected into the
> command line, ie something akin to
>
> print -z blahblah -o1 -o2 -o3
>
> ($1-$3 will be empty as they are undefined.)
>
> I can write a second function print-z-ing the required string but that
> feels redundant... although I have no idea how to do this w/o such a
> function.
>
> Anyone got an idea?
>
> Thanks T
>
>

[-- Attachment #2: Type: text/html, Size: 2374 bytes --]

  reply	other threads:[~2022-11-30 16:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-30 14:10 Thomas Lauer
2022-11-30 16:18 ` Philippe Altherr [this message]
2022-11-30 16:38 ` Peter Stephenson
2022-11-30 17:32   ` Thomas Lauer

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=CAGdYchsLQRL6Dq0rUDf_hXuVVF_TypKWazBHmrx4atdbCOgpUw@mail.gmail.com \
    --to=philippe.altherr@gmail.com \
    --cc=thomas.lauer@virgin.net \
    --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).