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

Re: How to “namespace” an autoloaded function?



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
}

Now

alias -s bar='namespacedfunction'

will cause "foo.bar" to invoke "namedspacedfunction" for any "foo",
link it to "bar", and then run it.




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