Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm
Precedence: bulk
X-No-Archive: yes
List-Id: Zsh Workers List <zsh-workers.zsh.org>
List-Post: <mailto:zsh-workers@zsh.org>
List-Help: <mailto:zsh-workers-help@zsh.org>
X-Qmail-Scanner-Diagnostics: from mail-pf0-f194.google.com by f.primenet.com.au (envelope-from <schaefer@brasslantern.com>, uid 7791) with qmail-scanner-2.11 
 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1.  
 Clear:RC:0(209.85.192.194):SA:0(0.0/5.0):. 
 Processed in 0.255874 secs); 17 Jul 2016 01:50:43 -0000
X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au
X-Spam-Level: 
X-Spam-Status: No, score=0.0 required=5.0 tests=T_DKIM_INVALID
	autolearn=unavailable autolearn_force=no version=3.4.1
X-Envelope-From: schaefer@brasslantern.com
X-Qmail-Scanner-Mime-Attachments: |
X-Qmail-Scanner-Zip-Files: |
Received-SPF: none (ns1.primenet.com.au: domain at brasslantern.com does not designate permitted sender hosts)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=brasslantern-com.20150623.gappssmtp.com; s=20150623;
        h=from:message-id:date:in-reply-to:comments:references:to:subject
         :mime-version;
        bh=D25m5/Q21NVvG4waB7sojB484PIr5DGYuCTrb5C29bo=;
        b=oaqpOIrM/vxk0wQ2UFOvziY+ZBKANOv8CTC/YoT+si+BgeviI7WeyAxNm7u4bWPV1D
         Ew53g7A6saM+vQArSG6dUK/pboAqHbsR8pTLFa/9c7d2fjfN7rV+5KxQyckA+VJ0ufDD
         1nThAygSTpfwcYWVeVuQOss1weQTu+edkWLyzEjdoyqJ0RNYmAI3Is1DQisLsbJsUFGO
         YlvlSO7S7wA/NsJwkyRy/peAci/CE4o4LjKW3i9R5/AXKT6rh8WjUVPsor5arsZiRl86
         X5nRdlykJQx0TxtDhJXSjskRL8SqYErCeqXM/OWD2a/XSNAdOA/vrTqgETkESSkhalgX
         gHFQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:from:message-id:date:in-reply-to:comments
         :references:to:subject:mime-version;
        bh=D25m5/Q21NVvG4waB7sojB484PIr5DGYuCTrb5C29bo=;
        b=FcZ91caxdexCtGEyZ7OxeWRYwflHabP4F/DITvbGrTo0SAJYiCP1xByAUU9VZAyyGr
         Ug2fwN6DefAk9lxBdThPbOXc8VHozapdDxS4jm3PKgxKA9arphqlkRrrKvWM+Nk699F5
         RBzHrT5BKeKYDsR8jK54nfPYgUmmAmc9NwOq/bWEToqHcealUWwiANf3e1XUsd0mW8RF
         WbV88je5WsfBXbyx86nZxdZOKdy7g/P9vcj5L7EfnoAQci7mQiko9mkcSU+itIkBktJ9
         meTq8gkNr0s8oZR7ItNaloqUNyoTOLLvbjlhKr2nEi5y8hNh0KHy3J9n74yyn7OJA+Pd
         +aeA==
X-Gm-Message-State: ALyK8tIY+Ea22SogqyCcyE4jKaSRDd+yCiiiqh+xL50TJo2KG2F5VsPhdFHueYLPjrQgvQ==
X-Received: by 10.98.86.85 with SMTP id k82mr34574291pfb.82.1468720234625;
        Sat, 16 Jul 2016 18:50:34 -0700 (PDT)
From: Bart Schaefer <schaefer@brasslantern.com>
Message-Id: <160716185103.ZM5258@torch.brasslantern.com>
Date: Sat, 16 Jul 2016 18:51:03 -0700
In-Reply-To: <160713171152.ZM22376@torch.brasslantern.com>
Comments: In reply to Bart Schaefer <schaefer@brasslantern.com>
        "Re: add-zle-hook-widget Re: Next release (5.3)" (Jul 13,  5:11pm)
References: <20160704114016.190f48be@pwslap01u.europe.root.pri> 
	<160704080424.ZM12848@torch.brasslantern.com> 
	<20160707020021.GB6364@tarsus.local2> 
	<20160713050011.GA18173@tarsus.local2> 
	<160713171152.ZM22376@torch.brasslantern.com>
X-Mailer: OpenZMail Classic (0.9.2 24April2005)
To: "Zsh Hackers' List" <zsh-workers@zsh.org>
Subject: Re: add-zle-hook-widget Re: Next release (5.3)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Seq: zsh-workers 38866

On Jul 13,  5:11pm, Bart Schaefer wrote:
}
} The contrib.yo file needs to be updated, I wanted first to see whether
} anyone spots any obvious problems with this rendition of the function.

Nobody has commented, so ...

The patch below fixes a couple of edge cases, notably:
- the case where "bindkey" has created an undefined slot in $widgets
- attempting to add a special widget to its own hook list
- a bug when checking whether to alias an existing hookd widget

It also switches from defining the hooks functions in a loop to defining
them with multifuncdef syntax, and updates contrib.yo.


diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index d4a4538..1d2b7ca 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -340,23 +340,20 @@ those additional widgets.
 var(hook) is one of tt(isearch-exit), tt(isearch-update),
 tt(line-pre-redraw), tt(line-init), tt(line-finish), tt(history-line-set),
 or tt(keymap-select), corresponding to each of the special widgets
-tt(zle-isearch-exit), etc.
+tt(zle-isearch-exit), etc.  The special widget names are also accepted
+as the var(hook) argument.
 
 var(widgetname) is the name of a ZLE widget.  If no options are given this
 is added to the array of widgets to be invoked in the given hook context.
 Note that the hooks are called as widgets, that is, with
 example(tt(zle )var(widgetname)tt( -Nw "$@"))
-rather than as a function call.
 
-In typical usage, var(widgetname) has the form var(index)tt(:)var(name).
-In this case var(index) is an integer which determines the order in which
-the widget var(name) will be called relative to other widgets in the
-array.  Widgets having the same var(index) are called in unspecified
-order.  However, var(widgetname) may omit the index, in which case an
-index is computed for it to arrange for it to be called in the order
-in which it was added to the array.
+vindex(WIDGET, in hooks)
+Note that this means that the `tt(WIDGET)' special parameter tracks the
+var(widgetname) when the widget function is called, rather than tracking
+the name of the corresponding special hook widget.
 
-If the option tt(-d) is given, the var(widgename) is removed from
+If the option tt(-d) is given, the var(widgetname) is removed from
 the array of widgets to be executed.
 
 If the option tt(-D) is given, the var(widgetname) is treated as a pattern
@@ -370,7 +367,6 @@ passed as arguments to tt(autoload) as with tt(add-zsh-hook).  The
 widget is also created with `tt(zle -N )var(widgetname)' to cause the
 corresponding function to be loaded the first time the hook is called.
 
-
 The arrays of var(widgetname) are currently maintained in tt(zstyle)
 contexts, one for each var(hook) context, with a style of `tt(widgets)'.
 If the tt(-L) option is given, this set of styles is listed with
diff --git a/Functions/Zle/add-zle-hook-widget b/Functions/Zle/add-zle-hook-widget
index 760e26d..cc2d54a 100644
--- a/Functions/Zle/add-zle-hook-widget
+++ b/Functions/Zle/add-zle-hook-widget
@@ -35,25 +35,23 @@ local -a hooktypes=( zle-isearch-exit zle-isearch-update
 # Stash in zstyle to make it global
 zstyle zle-hook types ${hooktypes#zle-}
 
-for hook in $hooktypes
-do
-  function azhw:$hook {
-      local -a hook_widgets
-      local hook
-      # Values of these styles look like number:name
-      # and we run them in number order
-      zstyle -a $WIDGET widgets hook_widgets
-      for hook in "${(@)${(@on)hook_widgets[@]}#<->:}"; do
-	  if [[ "$hook" = user:* ]]; then
-	      # Preserve $WIDGET within the renamed widget
-	      zle "$hook" -N "$@"
-	  else
-	      zle "$hook" -Nw "$@"
-	  fi || return
-      done
-      return 0
-  }
-done
+# Relying on multifuncdef option here
+function azhw:${^hooktypes} {
+    local -a hook_widgets
+    local hook
+    # Values of these styles look like number:name
+    # and we run them in number order
+    zstyle -a $WIDGET widgets hook_widgets
+    for hook in "${(@)${(@on)hook_widgets[@]}#<->:}"; do
+	if [[ "$hook" = user:* ]]; then
+	    # Preserve $WIDGET within the renamed widget
+	    zle "$hook" -N "$@"
+	else
+	    zle "$hook" -Nw "$@"
+	fi || return
+    done
+    return 0
+}
 
 # Redefine ourself with the setup left out
 
@@ -127,12 +125,25 @@ function add-zle-hook-widget {
 	    fi
 	fi
     else
+	# Check whether attempting to add a widget named for the hook
+	if [[ "$fn" = "$hook" ]]; then
+	    if [[ -n "${widgets[$fn]}" ]]; then
+		print -u2 "Cannot hook $fn to itself"
+		return 1
+	    fi
+	    # No point in building the array until another is added
+	    autoload "${autoopts[@]}" -- "$fn"
+	    zle -N "$fn"
+	    return 0
+	fi
 	integer i=${#options[ksharrays]}-2
 	zstyle -g extant_hooks "$hook" widgets
         # Check for an existing widget, add it as the first hook
 	if [[ ${widgets[$hook]} != "user:azhw:$hook" ]]; then
-	    zle -A "$hook" "${widgets[$hook]}"
-	    extant_hooks=(0:"${widgets[$hook]}" "${extant_hooks[@]}")
+	    if [[ -n ${widgets[$hook]} ]]; then
+		zle -A "$hook" "${widgets[$hook]}"
+		extant_hooks=(0:"${widgets[$hook]}" "${extant_hooks[@]}")
+	    fi
 	    zle -N "$hook" azhw:"$hook"
 	fi
 	# Add new widget only if not already in the hook list

