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

Re: How to “namespace” an autoloaded function?



On 10/25/21, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Sun, Oct 24, 2021 at 2:17 AM Marlon Richert <marlon.richert@xxxxxxxxx>
> 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`?




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