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

Re: Load autoloadable function, but only in enclosing function scope



On Thu, Mar 24, 2022 at 12:52 PM Peter Stephenson
<p.w.stephenson@xxxxxxxxxxxx> wrote:
>
> 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.

Extending your previous suggestion, then:

local -a oldfuncs=(${(k)functions})
local autoloads=(${(k)functions[(R)builtin autoload -X*]})
local funcsrc=(${(kv)functions_source})
{
  : ...
} always {
  local funcs=(${(k)functions:|oldfuncs})
  funcs=(${funcs:|autoloads})
  (( $#autoloads )) && unfunction $autoloads
  (( ${#funcs} )) && unfunction $funcs
  local func src
  for func src in $funcsrc; do
    [[ $src = */$func && $functions[$func] == '' ]] &&
      autoload -r $src
  done
}

Handling .zwc files and unusual autoload flags left as an exercise.
Particularly difficult is autoloads from "sticky" option scopes.




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