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

Re: PATH: autoload with explicit path



On Sun, 29 Jan 2017 08:11:04 -0800 (PST)
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> Consider for example that several of the autoloadable functions in the
> zsh distribution rely on "autoload colors".
> 
> If autoload behaved the way you suggest, this would break, because the
> colors function would not be found at the absolute path from which the
> calling function was loaded -- unless the writer of the calling function
> has used "autoload -d", of course.  But if we're now relying on the
> author to use the correct options for autoload, we're right back where
> we started -- might as well rely on the author to make his function
> suite "relocatable" by way of $function_source as well, and that is
> now possible.
> 
> There's no magic solution that automatically covers all the variations
> without cooperation from both the writer and the installer of the suite.

That's roughly my thinking --- any attempt to magic it gets very messy
very quickly --- and you don't need to.  Here's how it can work.  I
think it puts the onus to do the clever stuff on the function code
writer, which is the right place.

1. User gets a heap of functions and is instructed to put them all in the
same directory (or under the same directory, we have the flexiblity to
make it work with a set of subdirectories).  That's not controversial
anyway, I don't think.

2. User puts:

autoload -Uz /wherever/they/put/it/mainfunc

in their start up.  Note it'll still work if they rely on $fpath ---
mainfunc is resolved when loaded, so the code below still works.  So you
are protected against the user doing anything as long as it's some valid
autoload that works in the version of the shell in us.  No special
instructions are needed.

3. mainfunc contains:

zmodload zsh/parameter
if (( ${+functions_source} )); then
  # If that's available, load by absolute path is available.
  # The -R barfs immediately if the function can't be found
  # (optional extra to simplify error handling).
  # You can add -d if you want to search $fpath, too.
  autoload -RUz ${functions_source[mainfunc]:h}/{other1,other2,other3}
  # ... or whatever.
else
  # We're stuck with old-fashioned fpath.  Add warning, check, whatever.
  autoload other1 other2 other3
fi

This can be tailored to suit.  Any half-baked magic in the shell C
implementation can't.  To repeat, I'm not in any case interested in
doing half-baked magic in the shell C implementation. so let's not
go down that route again.

pws



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