zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: "Zsh Hackers' List" <zsh-workers@zsh.org>
Subject: Re: add-zle-hook-widget Re: Next release (5.3)
Date: Sat, 16 Jul 2016 18:51:03 -0700	[thread overview]
Message-ID: <160716185103.ZM5258@torch.brasslantern.com> (raw)
In-Reply-To: <160713171152.ZM22376@torch.brasslantern.com>

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


  reply	other threads:[~2016-07-17  1:50 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-04 10:40 Peter Stephenson
2016-07-04 15:04 ` Bart Schaefer
2016-07-05  6:00   ` Sebastian Gniazdowski
2016-07-05  6:33     ` Bart Schaefer
2016-07-05  8:33   ` Peter Stephenson
2016-07-05 11:48     ` Peter Stephenson
2016-07-05 16:29       ` Bart Schaefer
2016-07-07  2:00   ` Daniel Shahaf
2016-07-13  5:00     ` add-zle-hook-widget " Daniel Shahaf
2016-07-14  0:11       ` Bart Schaefer
2016-07-17  1:51         ` Bart Schaefer [this message]
2016-07-17 15:00           ` [PATCH] _add-zle-hook-widget: New completion Daniel Shahaf
2016-07-17 19:21             ` Bart Schaefer
2016-07-17 20:40               ` Bart Schaefer
2016-07-17 21:57             ` Bart Schaefer
2016-07-18  9:47             ` Oliver Kiddle
2016-07-18 15:30               ` Bart Schaefer
2016-07-19 10:30                 ` Oliver Kiddle
2016-07-19 17:58                   ` Bart Schaefer
2016-07-20  6:54               ` [PATCH v2] " Daniel Shahaf
2016-07-21 15:28                 ` Oliver Kiddle
2016-07-22  6:22                   ` Daniel Shahaf
2016-07-22 18:21                     ` Bart Schaefer
2016-07-22 18:45                     ` Oliver Kiddle
2016-07-23 18:03                       ` Daniel Shahaf
2016-07-17 14:59         ` add-zle-hook-widget Re: Next release (5.3) Daniel Shahaf
2016-07-17 18:48           ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=160716185103.ZM5258@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).