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

Re: autoload

On Sep 17, 10:00pm, Ray Andrews wrote:
} I guess the 'line 5 `)' error mystery is best
} forgotten.  Very strange tho.

It's not a mystery.  Try this:

    zsh -nf /usr/share/zsh/functions/Completion/Base.zwc

} $ fpath=( /aWorking/Zsh/Source "${fpath[@]}" )
} $ source miscfunctions
} $ autoload -w /aWorking/Zsh/Source/Source.zwc
} $ l
} [ nothing ]
} $ l
} [ now I get listing ]

The way autoload works is, the name of the file has to be the same as
the name of the function.  If you "source" the file, you execute the
file contents immediately, which in your case defines the functions.
If you "autoload" the function names, you NEITHER define NOR execute
them; you just mark them to be processed later.

Then when you try to execute, zsh looks for a file with the same name
as the function, parses it, and executes the result of the parse.
Except for handling of the positional parameters, this is *almost* the
same as

   eval "$functions[$filename:t]"

With "autoload -z" (the default when no option), zsh assumes the file
*was* the entire function body and does nothing further.

*If* the function was marked "autoload -k", then zsh assumes that the
eval-like step defined THAT function, and calls it by name a second time,
similar to:

   eval "$functions[$filename:t]"

If we now throw zcompile into the mix, the -z / -k options to autoload
are ignored in favor of the -z / -k options to zcompile.  However, the
rule about the file name having to be the same as the function name
doesn't change.  Further "autoload -w" still doesn't define anything;
it just uses the index table in the zcompiled file to grab the list
of names to mark for later processing.

} Strangely there seems to be no trouble with multiple functions in one
} file except in the case of 'miscfunctions' so I have to source it
} separately. (The other multiple functions are all help functions only
} called by the main function in each file. I don't know why that makes
} a difference, but it does.)

Does what I just wrote, above, help?  In the "miscfunctions" case, the
name of the file matches NONE of the names of the functions inside it;
they can't be autoloaded.  The name of the file has to match the name
of AT LEAST ONE function inside the file, *OR* the entire file has to
be that single function (as if it were a script).

} Also, I wonder why $fpath needs to be modified since 'autoload -w' is
} given an explicit path to the .zwc file

The internal implementation of autoloaded functions is unrelated to
hashing and does not retain any file paths.  It keeps only the base
names.  Where that set of names came from does not change that.

This is important to the consistent operation of fpath searching.  If
you have

    fpath=(/tmp/testfunctions ...)

and you create a new file /tmp/testfunctions/tryme, and the function
is marked for autoloading, then that new file has to be found ahead
of anything else, even if you previously used "autoload -w" on a .zwc
file that contains a "tryme" file.  The contents of fpath at the
instant of execution ALWAYS takes precedence; there is no "never get
this function from anywhere except [here]" in the autoload scheme.

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