zsh-users
 help / color / mirror / code / Atom feed
From: Marlon Richert <marlon.richert@gmail.com>
To: Bart Schaefer <schaefer@brasslantern.com>, Zsh Users <zsh-users@zsh.org>
Subject: Re: How to “namespace” an autoloaded function?
Date: Mon, 25 Oct 2021 10:36:30 +0300	[thread overview]
Message-ID: <CAHLkEDs-Tq7VBe7O_dHqqWEZmM7oBJFiQ6BO8Wxi9ySaV5R_wA@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7Y9fFLUFZcWQHxkQTEiM2kwsVRwuhTa+UPxZk0p7NN64A@mail.gmail.com>

On 10/25/21, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Sun, Oct 24, 2021 at 2:17 AM Marlon Richert <marlon.richert@gmail.com>
> wrote:
>>
>> What would be the canonical way to make the body of function 'foo.bar' be
>> autoloaded from '/some/path/to/bar'?
>
> There isn't a "canonical" way, really.  The autoload mechanism is
> entirely dependent on the file name and the function name being the
> same.  I can suggest a way to accomplish it, but there is probably
> more than one way.
>
> The most flexible way I can think of is to take advantage of the
> "suffix alias" mechanism.
>
> function namespacedfunction {
>   if [[ $1 = *.* ]] && {
>     [[ -n $functions[$1:e] ]] ||
>     autoload +X -R /some/path/to/$1:e
>   }
>   then
>     functions -c $1:e $1
>     "$@"
>   else
>     return 1
>   fi
> }

Can you think of another way to do this than to autoload the function
and copy it to a new function? Given a function file 'bar', which I
want to load as 'foo.bar', there might already be a loaded function
'bar', which I don’t want to overwrite. The point of adding a
namespace to the function is to not overwrite an otherwise same-named
function, if any, which this doesn’t accomplish.

Also, what would be the best way to do this in batch form? That is, I
want to autoload _all_ function files from a particular dir as
foo.<filename> instead of just <filename>.


> Now
>
> alias -s bar='namespacedfunction'
>
> will cause "foo.bar" to invoke "namedspacedfunction" for any "foo",
> link it to "bar", and then run it.

Would this work when calling foo.bar from inside a function that was
loaded with `autoload -U`?


  reply	other threads:[~2021-10-25  7:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-24  9:17 Marlon Richert
2021-10-24 23:44 ` Bart Schaefer
2021-10-25  7:36   ` Marlon Richert [this message]
2021-10-25 16:49     ` 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=CAHLkEDs-Tq7VBe7O_dHqqWEZmM7oBJFiQ6BO8Wxi9ySaV5R_wA@mail.gmail.com \
    --to=marlon.richert@gmail.com \
    --cc=schaefer@brasslantern.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).