From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 87dc81bb for ; Mon, 25 Nov 2019 14:23:44 +0000 (UTC) Received: (qmail 9345 invoked by alias); 25 Nov 2019 14:23:35 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 24464 Received: (qmail 8010 invoked by uid 1010); 25 Nov 2019 14:23:35 -0000 X-Qmail-Scanner-Diagnostics: from mail-vk1-f178.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.102.0/25642. spamassassin: 3.4.2. Clear:RC:0(209.85.221.178):SA:0(-2.0/5.0):. Processed in 2.35197 secs); 25 Nov 2019 14:23:35 -0000 X-Envelope-From: sgniazdowski@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _netblocks.google.com designates 209.85.221.178 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to :content-transfer-encoding; bh=dIJLxcWOk0cZECJ+V0P9dlkuaKUX5KV+Dld9QhmU3iE=; b=WKsETvnea1jGyfWuPhR8wjc+IlBjfR1gt6ci4Qjt6Pl+7Zj1rEZdyR4MYQiX7LepT0 3CeFwz+g1XhbsnXf/mZTreJr7+49XFH1kw5WFcF0zc+QVKYrv77Mspec57dytlNCenMU nLudNyOFkibCMOLe3orWlVuirsA1dpOrDvo+Q0M9k5fisQdunY9/Od2d7k41iPW3pG3x ePKx1QwR9UaF5T83QgQPua0KVLJCTrY7KxYN9fT/CBV34xLGI5jdSx9LC/u227ckY1b8 7nguoeObOPSDfH0RnpUxDuwcHUBJtdpsx91xvHYJZRRIjcX9hhqahYFefbkueck4pADe 8S9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to :content-transfer-encoding; bh=dIJLxcWOk0cZECJ+V0P9dlkuaKUX5KV+Dld9QhmU3iE=; b=UMMjDeIdgUN94HbmEN08c+jcOc16Q3v8MoP3ztqPeIsTxkpr9Oq8Ywt+qCOVYqDtxf oiXJFTJQuAc6SpEjmBQ5M+M+Srs7/uIM5ve7KXAGe9GLJcYDCk5CTrmA1hyUSmeXVDr4 tG7UYNzk4afpuqUJeMd3FlhQGhg7SZjD8UTbrnHnEgPvrTxjqDkiHFSG2M6EI7E3dC0E RqL8v2trALjdHt+eUGZ4grQoA886CimfAIraP3JaoX++ZTeWXi2JSH/ukLq1xSnnDfn/ SgvrDQK5550afCklNm9GbnlzbLsIp0w9dErOQRzTbau+/rRy7kNrntVq8tVmflo+MImG 59lg== X-Gm-Message-State: APjAAAWEIO4qTiOTopEPGUUiiTmVpgnMNugdibyFgbcLy6uIGFaQbCoB +syC8Xn8l7161MZZ6mCsusYR4a9yz1D1EMrLaxc7FdtQ+v0= X-Google-Smtp-Source: APXvYqxWRMnNde5mRLjJ1ahgT9piJWxUa4v2jhI1yrWawIPjkUKbBwaI/u/WtlP3ZWG/3GcXA/1ERx+1j/nvQZ3mXqE= X-Received: by 2002:a1f:aecb:: with SMTP id x194mr6361668vke.21.1574691778405; Mon, 25 Nov 2019 06:22:58 -0800 (PST) MIME-Version: 1.0 From: Sebastian Gniazdowski Date: Mon, 25 Nov 2019 15:22:53 +0100 Message-ID: Subject: A way to write type-agnostic script/function runnables To: Zsh Users Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 !=3D example-script || -n $ZSH_SCRIPT ]]; then 0=3D${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}} 0=3D${${(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=3D${0:h} fi The code is also available at Zplugin's repo =E2=80=93 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;). --=20 Sebastian Gniazdowski News: https://twitter.com/ZdharmaI IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin Blog: http://zdharma.org