Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: array with newlines preserved as literal text

On Tue, Feb 2, 2021 at 3:31 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
> Complicated.  It goes back to my first bitch with zsh, namely not being
> able to capture the literal invocation of a function (along with it's
> tail) unexpanded, just as you'd see in history.

I'm not entirely sure I understand what that means, but maybe it's this?

_fn2hist() {
  emulate -L zsh
  local cmd=$1
  if [[ -n $functions[$cmd] ]]
    print -rS "${$(functions -x 2 $cmd)#$cmd} ${${(qq)@}}"
  $cmd "$@"

Intended to be used like this:

myfn() { : do whatever myfn does }
alias myfn='_fn2hist myfn'

What this does is turn the body of "myfn" into an anonymous function,
and stuff that into the history followed by the original arguments,
and then run the function.

At this point if you up-history, you'll get something like

ubuntu%  () {
  : do whatever myfn does
} 'original' 'arguments'

That could be a very long history entry, if "myfn" is complicated ...
but if you hit "enter", it should behave just like "myfn" did.

With one caveat:  If the function depends on getting its own name from
$0 or some similar trick involving $funcstack, etc., that will fail.

Messages sorted by: Reverse Date, Date, Thread, Author