From 283fcbb1d61f54fb283cf2852ac01fa44be2a636 Mon Sep 17 00:00:00 2001 From: Sebastian Gniazdowski Date: Wed, 26 Jul 2023 14:58:24 -0500 Subject: [PATCH] =?UTF-8?q?feat:=20v1.1.7=20=E2=80=93=20new=200=3D"?= =?UTF-8?q?=E2=80=A6"=20snippet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zsh-Plugin-Standard.adoc | 42 +++++++++++++++------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/Zsh-Plugin-Standard.adoc b/Zsh-Plugin-Standard.adoc index d1d8aa9..e355c82 100644 --- a/Zsh-Plugin-Standard.adoc +++ b/Zsh-Plugin-Standard.adoc @@ -1,6 +1,6 @@ # Zsh Plugin Standard -Sebastian Gniazdowski -v1.0, 11/22/2019 +Sebastian Gniazdowski +v1.1.7, 07/26/2023 :source-highlighter: rouge :toc: @@ -53,8 +53,7 @@ cover the information of how to write a Zsh plugin. + To get the plugin’s location, plugins should do: ```shell -0="${ZERO:-${${0:#$ZSH_ARGZERO}:-${(%):-%N}}}" -0="${${(M)0:#/*}:-$PWD/$0}" +0=${${ZERO:-${(%):-%x}}:A} # Then ${0:h} to get plugin’s directory ``` @@ -73,28 +72,23 @@ The one-line code above will: (link:http://www.zsh.org/mla/workers/2017/msg01827.html[comparison] note that it’s not for a compiled script). -3. Use `$0` if it doesn’t contain the path to the Zsh binary, +3. Use `%x` expansion otherwise. - - plugin manager will still be able to set `$0`, although more difficultly - (requires `unsetopt function_argzero` before sourcing plugin script, and - `0=…​` assignment), + - it is an universal expansion that returns source file path even for + autoloaded functions (and also of course for sourced scripts, like + the plugin script), so you can use it also in the auto-funs source + files (tested by author) or hash-bang scripts (tested, too), - `unsetopt function_argzero` will be detected (it causes `$0` not to - contain plugin-script path, but path to Zsh binary, if not overwritten - by a `0=…​` assignment), - - - `setopt posix_argzero` will be detected (as above). - -4. Use `%N` prompt expansion flag, which always gives absolute path to script, + contain plugin-script path, but path to Zsh binary, like `/bin/zsh`), - - plugin manager cannot alter this (no advanced loading of plugin - is possible), but simple plugin-file sourcing (without a plugin - manager) will be saved from breaking caused by the mentioned - `*_argzero` options, so this is a very good last-resort - fallback. + - `setopt posix_argzero` will be detected (as above). -5. Finally, in the second line, it will ensure that `$0` contains an absolute - path by prepending it with `$PWD` if necessary. +5. Finally, the `:A` modifier will be applied in to resolve any + symbolic links and unneeded `../` components, plus make the path absolute. + This step is taken in order to provide the same trailing dir for different + symbolic paths, because some plugins use it, e.g.: for location of plugin + configuration in `~/.config/DIR` or similar. The goal is flexibility, with essential motivation to support `eval "$(