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

Re: PATCH: function copy



On Tue, Jul 16, 2019 at 1:56 AM Peter Stephenson
<p.stephenson@xxxxxxxxxxx> wrote:
>
> On Mon, 2019-07-15 at 14:42 -0700, Bart Schaefer wrote:
>
> > "around" you need a way to say "call the original function HERE" which
> > you can then embed in another function that becomes the "around" (and
> > which is called in place of the original everywhere except HERE).
>
> I think you're implying you'd rather not have an additional function
> name to deal with, i.e. the HERE is indicated in some other fashion.
> That's quite hard to fit into zsh syntax.

There doesn't need to be any special syntax, just some special name
linking, e.g.:

_my_fn() {
  # do stuff here
  _orig_fn "$@"
  # do stuff here
}
advice-around _std_fn _my_fn _orig_fn
advice-around _other_fn _my_fn _orig_fn

and "advice-around" would arrange that any time either _std_fn or
_other_fn was called, the name _orig_fn would temporary be linked to
either _std_fn or _other_fn (as appropriate to whichever one was being
replaced), and then _my_fn would be invoked.  At the end of _my_fn the
temporary name _orig_fn would evaporate.  This allows the same advice
to be linked to multiple functions without having to copy all of them,
etc.

For ordinary functions this is the same as doing something like

_my_fn() {
  local _orig_fn=$1
  shift
  # do stuff here
  $_orig_fn "$@"
  #do stuff here
}
alias _std_fn="_my_fn _std_fn"
alias _other_fn="_my_fn _other_fn"

except you don't have to worry about whether aliases are going to
expand or what order to define them or having the extra argument to
shift off, etc.  For widget functions, though, there's a lot more to
do to be able to invoke a widget properly in the middle of _my_fn,
especially if the original widget is a builtin.



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