Subject: Re: backward-kill-shell-word widget
From: Oliver Kiddle <[email-masked]>
To: [email-masked]
Date: Fri, 08 Jul 2016 16:19:02 +0200
List-Id: Zsh Workers List <zsh-workers.zsh.org>

On 10 Jan, Bart wrote:
> As of 5.0.8 you can do this with:
>
>     backward-kill-shell-word() {
>       zle select-in-shell-word
>       ((++CURSOR))  # adjust for vi vs. emacs region
>       zle kill-region
>     }
>
> I'm not sure if that CURSOR adjustment is a a bug or just a necessary
> evil because of using vi binding in the emacs keymap.

It ought to have adjusted the cursor internally for emacs mode.
It's hard to say exactly how the widget should behave from insert mode
exactly but this patch also fixes select-in/a-word to select further
words when invoked repeatedly from emacs mode. Note that
select-in-shell-word can't unfortunately do that even from vi mode.
I've also tried to consider weird combinations of
virangeflag, invicmdmode() and region_active but may have missed one.

Note that select-in-shell-word will select forwards in some
circumstances which might be unexpected for a backward-kill- widget.

Sorry for taking so long to reply to this.

Oliver

diff --git a/Src/Zle/textobjects.c b/Src/Zle/textobjects.c
index 9b3277a..3db0781 100644
--- a/Src/Zle/textobjects.c
+++ b/Src/Zle/textobjects.c
@@ -1,5 +1,5 @@
 /*
- * textobjects.c - ZLE module implementing Vim style text objects
+ * textobjects.c - ZLE widgets implementing Vim style text objects
  *
  * This file is part of zsh, the Z shell.
  *
@@ -54,11 +54,7 @@ selectword(UNUSED(char **args))
     int sclass = viclass(zleline[zlecs]);
     int doblanks = all && sclass;
 
-    if (!invicmdmode()) {
-	region_active = 1;
-	mark = zlecs;
-    }
-    if (!region_active || zlecs == mark) {
+    if (!region_active || zlecs == mark || mark == -1) {
 	/* search back to first character of same class as the start position
 	 * also stop at the beginning of the line */
 	mark = zlecs;
@@ -207,8 +203,12 @@ selectword(UNUSED(char **args))
     /* Adjustment: vi operators don't include the cursor position, in insert
      * or emacs mode the region also doesn't but for vi visual mode it is
      * included. */
-    if (zlecs && zlecs > mark && !virangeflag)
-	DECCS();
+    if (!virangeflag) {
+	if (!invicmdmode())
+	    region_active = 1;
+	else if (zlecs && zlecs > mark)
+	    DECCS();
+    }
 
     return 0;
 }
@@ -315,7 +315,7 @@ selectargument(UNUSED(char **args))
     }
 
     /* Adjustment: vi operators don't include the cursor position */
-    if (!virangeflag)
+    if (!virangeflag && invicmdmode())
        DECCS();
 
     return 0;