help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: Load autoloadable function, but only in enclosing function scope
Date: Thu, 24 Mar 2022 10:51:12 -0700	[thread overview]
Message-ID: <CAH+w=7Yxm-s7jFFW4MLZ5NzRUeJ1tF5R=YSUjQs1M8FX-J-6Ow@mail.gmail.com> (raw)
In-Reply-To: <306931674.638268.1648141537809@mail2.virginmedia.com>

On Thu, Mar 24, 2022 at 10:06 AM Peter Stephenson
<p.w.stephenson@ntlworld.com> wrote:
> With that key limitation --- if you've already got functions with the
> names of the ones you're creating, you're stuffed --- you can automate
> it like this.
> fn() {
>   local -a oldfuncs=(${(k)functions})

It actually can be even shorter than that, depending on how carefully
the context needs to be preserved.  Peter's example keeps the
definitions of any functions that were previously defined, even if
those definitions were replaced (by autoload execution or explicit
redefinition).  The example below restores all the function
definitions to their previous state, even when replaced.  (Native zsh
mode semantics assumed below.)

foo() { print original foo }
bar() {
  local funcs=(${(kv)functions}
    foo() { print the bar foo }
  } always {

% foo; bar; foo
original foo
the bar foo
original foo

This does have side-effects for autoloaded functions; they become
defined with "builtin autoload -X" as the body, which doesn't always
work the same way as a true autoload.

  reply	other threads:[~2022-03-24 17:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-24 16:12 Zach Riggle
2022-03-24 16:29 ` Mikael Magnusson
2022-03-24 17:05   ` Peter Stephenson
2022-03-24 17:51     ` Bart Schaefer [this message]
2022-03-24 18:34       ` Bart Schaefer
2022-03-24 19:52       ` Peter Stephenson
2022-03-25  0:27         ` Bart Schaefer

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:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7Yxm-s7jFFW4MLZ5NzRUeJ1tF5R=YSUjQs1M8FX-J-6Ow@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \


* 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


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