help / color / mirror / Atom feed
* A way to write type-agnostic script/function runnables
@ 2019-11-25 14:22 Sebastian Gniazdowski
  2019-12-05 11:15 ` Sebastian Gniazdowski
  0 siblings, 1 reply; 2+ messages in thread
From: Sebastian Gniazdowski @ 2019-11-25 14:22 UTC (permalink / raw)
  To: Zsh Users

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

    # 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}

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:


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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-12-05 11:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-25 14:22 A way to write type-agnostic script/function runnables Sebastian Gniazdowski
2019-12-05 11:15 ` Sebastian Gniazdowski


This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-users

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-users zsh-users/ http://inbox.vuxu.org/zsh-users \
	public-inbox-index zsh-users

Example config snippet for mirrors.
Newsgroup available over NNTP:

code repositories for the project(s) associated with this inbox:


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git