zsh-users
 help / color / mirror / code / Atom feed
* execute or print one-liner
@ 2022-11-30 14:10 Thomas Lauer
  2022-11-30 16:18 ` Philippe Altherr
  2022-11-30 16:38 ` Peter Stephenson
  0 siblings, 2 replies; 4+ messages in thread
From: Thomas Lauer @ 2022-11-30 14:10 UTC (permalink / raw)
  To: Zsh-Users List

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: execute or print one-liner
  2022-11-30 14:10 execute or print one-liner Thomas Lauer
@ 2022-11-30 16:18 ` Philippe Altherr
  2022-11-30 16:38 ` Peter Stephenson
  1 sibling, 0 replies; 4+ messages in thread
From: Philippe Altherr @ 2022-11-30 16:18 UTC (permalink / raw)
  To: Thomas Lauer; +Cc: Zsh-Users List

[-- 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 --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: execute or print one-liner
  2022-11-30 14:10 execute or print one-liner Thomas Lauer
  2022-11-30 16:18 ` Philippe Altherr
@ 2022-11-30 16:38 ` Peter Stephenson
  2022-11-30 17:32   ` Thomas Lauer
  1 sibling, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2022-11-30 16:38 UTC (permalink / raw)
  To: Thomas Lauer, Zsh-Users List

> On 30/11/2022 14:10 Thomas Lauer <thomas.lauer@virgin.net> wrote:
> 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

Not sure how picky you are about having the arguments expanded, but
something like the following (by no means fully debugged) would work
if you don't mind the arguments being at the end.

So run "fline blahblah -o2 -o2 -o3"

pws


fline() {
emulate -L zsh
setopt extendedglob

# In case not yet loaded
local func=$1
shift

case $functions[$func] in
  ('')
  print "Function \`$func' is not defined" >&2
  return 1
  ;;

  ("builtin autoload "[^$'\n']#)
  autoload +X $func
  ;;
esac

local -a args=("() {
$functions[$func]
}"  ${(Q)argv})

print -z "$args"
}


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: execute or print one-liner
  2022-11-30 16:38 ` Peter Stephenson
@ 2022-11-30 17:32   ` Thomas Lauer
  0 siblings, 0 replies; 4+ messages in thread
From: Thomas Lauer @ 2022-11-30 17:32 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh-Users List

From Peter Stephenson <p.w.stephenson@ntlworld.com>:
> Not sure how picky you are about having the arguments expanded, but
> something like the following (by no means fully debugged) would work
> if you don't mind the arguments being at the end.
<snipped>

Yes, that looks pretty good.

I actually prefer a simple replacement w/o any arguments as the whole
point of the exercise is to be able to edit the command in question.

So I adapted your code somewhat and integrated it with the alias
expander I already had (now first checks for a function, then an alias)
so that it will expand both. Thanks, problem solved.

And also thanks to Philippe for his idea.

T


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-11-30 17:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-30 14:10 execute or print one-liner Thomas Lauer
2022-11-30 16:18 ` Philippe Altherr
2022-11-30 16:38 ` Peter Stephenson
2022-11-30 17:32   ` Thomas Lauer

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