zsh-workers
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: zsh-workers@zsh.org
Cc: Eric Freese <ericdfreese@gmail.com>
Subject: add-zle-hook-widget and multiple hooks
Date: Sat, 6 Jun 2020 08:40:54 +0000	[thread overview]
Message-ID: <20200606084054.GA31628@tarpaulin.shahaf.local2> (raw)

[-- Attachment #1: Type: text/plain, Size: 861 bytes --]

When two or more zle-line-pre-redraw hooks are registered using
add-zle-hook-widget, the value of $LASTWIDGET when the each hook is
called is the name of the former hook:

    [Eric Freese wrote in https://github.com/zsh-users/zsh-autosuggestions/issues/529#issuecomment-632113840]
    $ zsh -df
    % autoload add-zle-hook-widget
    % f() {}
    % g() { zle -M "$(typeset -p LASTWIDGET)" }
    % add-zle-hook-widget line-pre-redraw f
    % add-zle-hook-widget line-pre-redraw g
    % x<CURSOR>
    typeset -r LASTWIDGET=f

The issue here is that g would like to to know what widget was invoked
immediately before the redraw.  In the example, that'd be self-insert.

I've attached two proofs of concept.  WDYT?

I'll add docs, etc, once an approach is chosen.

Cheers,

Daniel

P.S.  For the latter patch, note that «zle $widget -f» is distinct from
«zle -f».

[-- Attachment #2: azhw-LASTWIDGET-shell-v1.txt --]
[-- Type: text/plain, Size: 514 bytes --]

diff --git a/Functions/Misc/add-zle-hook-widget b/Functions/Misc/add-zle-hook-widget
index 9cc35496f..f5f9b615d 100644
--- a/Functions/Misc/add-zle-hook-widget
+++ b/Functions/Misc/add-zle-hook-widget
@@ -41,6 +41,7 @@ zstyle zle-hook types ${hooktypes#zle-}
 function azhw:${^hooktypes} {
     local -a hook_widgets
     local hook
+    readonly LAST_NONHOOK_WIDGET=$LASTWIDGET
     # Values of these styles look like number:name
     # and we run them in number order
     zstyle -a $WIDGET widgets hook_widgets

[-- Attachment #3: azhw-LASTWIDGET-C-v1.txt --]
[-- Type: text/plain, Size: 2549 bytes --]

diff --git a/Functions/Misc/add-zle-hook-widget b/Functions/Misc/add-zle-hook-widget
index 9cc35496f..4d8049083 100644
--- a/Functions/Misc/add-zle-hook-widget
+++ b/Functions/Misc/add-zle-hook-widget
@@ -47,9 +47,9 @@ function azhw:${^hooktypes} {
     for hook in "${(@)${(@on)hook_widgets[@]}#<->:}"; do
 	if [[ "$hook" = user:* ]]; then
 	    # Preserve $WIDGET within the renamed widget
-	    zle "$hook" -N -- "$@"
+	    zle "$hook" -f "nolast" -N -- "$@"
 	else
-	    zle "$hook" -Nw -- "$@"
+	    zle "$hook" -f "nolast" -Nw -- "$@"
 	fi || return
     done
     return 0
diff --git a/Src/Zle/zle_thingy.c b/Src/Zle/zle_thingy.c
index ce61db27b..7604d5251 100644
--- a/Src/Zle/zle_thingy.c
+++ b/Src/Zle/zle_thingy.c
@@ -678,6 +678,7 @@ bin_zle_flags(char *name, char **args, UNUSED(Options ops), UNUSED(char func))
 		else if (!strcmp(*flag, "keepsuffix"))
 		    w->flags |= ZLE_KEEPSUFFIX;
 		*/
+		/* "nolast" is used in bin_zle_call */
 	        else if (!strcmp(*flag, "vichange")) {
 		    if (invicmdmode()) {
 			startvichange(-1);
@@ -703,7 +704,7 @@ bin_zle_call(char *name, char **args, UNUSED(Options ops), UNUSED(char func))
 {
     Thingy t;
     struct modifier modsave = zmod;
-    int ret, saveflag = 0, setbindk = 0, setlbindk, remetafy;
+    int ret, saveflag = 0, setbindk = 0, setlbindk = 0, remetafy;
     char *wname = *args++, *keymap_restore = NULL, *keymap_tmp;
 
     if (!wname)
@@ -727,12 +728,23 @@ bin_zle_call(char *name, char **args, UNUSED(Options ops), UNUSED(char func))
     while (*args && **args == '-') {
 	char skip_this_arg[2] = "x";
 	char *num;
+	char *flag;
 	if (!args[0][1] || args[0][1] == '-') {
 	    args++;
 	    break;
 	}
 	while (*++(*args)) {
 	    switch (**args) {
+	    case 'f':
+		flag = args[0][1] ? args[0]+1 : args[1];
+		if (strcmp(flag, "nolast")) {
+		    zwarnnam(name, "%s", "'nolast' expected after -f");
+		    if (remetafy)
+			metafy_line();
+		    return 1;
+		}
+		setlbindk = 1;
+		break;
 	    case 'n':
 		num = args[0][1] ? args[0]+1 : args[1];
 		if (!num) {
@@ -787,7 +799,7 @@ bin_zle_call(char *name, char **args, UNUSED(Options ops), UNUSED(char func))
      * a vi range to detect a repeated key */
     setbindk = setbindk ||
 	(t->widget && (t->widget->flags & (WIDGET_INT | ZLE_VIOPER)) == WIDGET_INT);
-    setlbindk = t->widget && (t->widget->flags & ZLE_NOLAST) == ZLE_NOLAST;
+    setlbindk |= t->widget && (t->widget->flags & ZLE_NOLAST) == ZLE_NOLAST;
     ret = execzlefunc(t, args, setbindk, setlbindk);
     unrefthingy(t);
     if (saveflag)

             reply	other threads:[~2020-06-06  8:41 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-06  8:40 Daniel Shahaf [this message]
2020-06-06 11:58 ` Mikael Magnusson
2020-06-08  6:14   ` Daniel Shahaf
2020-06-08 17:52     ` Mikael Magnusson
2020-06-10 13:34       ` Daniel Shahaf
2020-06-18 11:42         ` Daniel Shahaf
2020-06-27  2:54           ` Daniel Shahaf
2020-06-27  3:02             ` Daniel Shahaf

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=20200606084054.GA31628@tarpaulin.shahaf.local2 \
    --to=d.s@daniel.shahaf.name \
    --cc=ericdfreese@gmail.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).