Hello, I thought that I share a way of writing runnable objects in such a way that they can be both invoked as a script and as a function. The main contribution is a concise method of getting the directory path where the object is located: # Run as script? ZSH_SCRIPT is a Zsh 5.3 addition if [[ $0 != example-script || -n $ZSH_SCRIPT ]]; then 0=${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}} 0=${${(M)0##/*}:-$PWD/$0} # Such global variable is expected to be typeset'd -g # in the plugin.zsh file. Here it's restored in case # of the function being ran as a script typeset MY_PLUGIN_DIR=${0:h} fi The code is also available at Zplugin's repo – it is being added as an example object when creating a plugin with the `create' subcommand: https://github.com/zdharma/zplugin/blob/master/doc/example-script I wonder if one could get the directory where the function is being located directly, without the global *_DIR parameter? Without use of the $*trace parameters (I don't trust them;). -- Sebastian Gniazdowski News: https://twitter.com/ZdharmaI IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin Blog: http://zdharma.org
On Mon, 25 Nov 2019 at 15:22, Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote: > I wonder if one could get the directory where the function is being > located directly, without the global *_DIR parameter? Without use of > the $*trace parameters (I don't trust them;). I've had a thought: it shouldn't be an uncomfortable thing to use shell's features → if the object is being run as a function, then it has access to the in-session parameters – and why hesitate to leverage this? So in short, the PLUGIN_NAME_DIR parameter is a good solution, there's no need for querying the autoload's storage directory. -- Sebastian Gniazdowski News: https://twitter.com/ZdharmaI IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin Blog: http://zdharma.org