summary refs log tree commit diff
path: root/Functions
diff options
context:
space:
mode:
authorBarton E. Schaefer <schaefer@zsh.org>2016-07-17 12:04:48 -0700
committerBarton E. Schaefer <schaefer@zsh.org>2016-07-17 12:04:48 -0700
commite3884c60ed1a247af1e1578710629f2ca79630d8 (patch)
tree32d75fe47edad94dd932ce19c714944396825cfa /Functions
parent18d676ff199d7ddc8c9ff3e18a4764ca1a4b25a4 (diff)
downloadzsh-e3884c60ed1a247af1e1578710629f2ca79630d8.tar.gz
zsh-e3884c60ed1a247af1e1578710629f2ca79630d8.tar.xz
zsh-e3884c60ed1a247af1e1578710629f2ca79630d8.zip
38866: update add-zle-hook-widget doc for 38850, bug fixes
Edge case handling, wrap in anonymous function for kshautoload management.
Diffstat (limited to 'Functions')
-rw-r--r--Functions/Zle/add-zle-hook-widget57
1 files changed, 36 insertions, 21 deletions
diff --git a/Functions/Zle/add-zle-hook-widget b/Functions/Zle/add-zle-hook-widget
index 760e26d29..04be50478 100644
--- a/Functions/Zle/add-zle-hook-widget
+++ b/Functions/Zle/add-zle-hook-widget
@@ -18,6 +18,8 @@
 #
 # The -L option lists the hooks and their associated widgets.
 
+() { # Preserve caller global option settings
+
 emulate -L zsh
 
 # This is probably more safeguarding than necessary
@@ -35,25 +37,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 +127,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
@@ -155,6 +168,8 @@ function add-zle-hook-widget {
     fi
 }
 
+} "$@" # Resume caller global options
+
 # Handle zsh autoloading conventions:
 # - "file" appears last in zsh_eval_context when "source"-ing
 # - "evalautofunc" appears with kshautoload set or autoload -k