zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-users@zsh.org
Subject: Re: Load autoloadable function, but only in enclosing function scope
Date: Thu, 24 Mar 2022 19:52:08 +0000	[thread overview]
Message-ID: <df3bf8b419fba5fd457ae8868581707b95bb5f44.camel@ntlworld.com> (raw)
In-Reply-To: <CAH+w=7Yxm-s7jFFW4MLZ5NzRUeJ1tF5R=YSUjQs1M8FX-J-6Ow@mail.gmail.com>

On Thu, 2022-03-24 at 10:51 -0700, Bart Schaefer wrote:
> 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 }
>     foo
>   } always {
>     functions=($funcs)
>   }
> }
> 
> 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.

It's got side effects for other fucntions, which is that that the line
numbers recorded no longer match those in the file the function is
autoloaded from (you don't see those unless you're using a debug
prompt or equivalent).

You're also at the mercy of zsh's ability to convert internal structures
into text and back, which is supposed to work, though not particularly
efficiently.

pws



  parent reply	other threads:[~2022-03-24 19:53 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
2022-03-24 18:34       ` Bart Schaefer
2022-03-24 19:52       ` Peter Stephenson [this message]
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:
  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=df3bf8b419fba5fd457ae8868581707b95bb5f44.camel@ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --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).