Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: highlight pasted text



Bart wrote:
> There are a couple of features that might be useful if we could come up
> with a way to do them.  One would be an indication of whether bracketed
> paste is supported at all, so that no one is taken by surprise when the

That's not possible as far as I know. If it was possible to detect that,
we'd be able to make the sending of the start/end strings conditional.

> shell starts executing what they pasted.  Another is some indication of
> what text was pasted (maybe highlight-color it differently?).

How about the following patch? This adds another special token to
zle_highlight. So you can do, e.g: zle_highlight+=( paste:bg=87 )
This applies also for vi put and emacs yank commands.

We were already tracking start/end positions of a paste for the purposes
of the yank-pop widget. Using those, leads to some further changes:

Text from a bracketed paste is now added to the cutbuffer. As far as
I can tell, this seems to actually also be what the GUI mode of emacs
does. Though emacs is perhaps not putting duplicates in. It'd be good if
some actual emacs users could check that it all makes sense, however.

I had been pondering how to interface bracketed-paste with vi registers,
anyway. This will now also put the bracketed paste in the numbered
registers. If you explicitly name a vi register, the pasted text is now
NOT inserted: it is only assigned to the register. This seems quite
useful if not the most vim compatible. For a middle-mouse click after
naming a register, gvim will paste the named register at the position
under the mouse.

Oliver

diff --git a/Doc/Zsh/zle.yo b/Doc/Zsh/zle.yo
index da8ee47..ef73f4d 100644
--- a/Doc/Zsh/zle.yo
+++ b/Doc/Zsh/zle.yo
@@ -2510,6 +2510,9 @@ a directory name.  Note that suffix removal is configurable; the
 circumstances under which the suffix will be removed may differ
 for different completions.
 )
+item(tt(paste))(
+Following a command to paste text, the characters that were inserted.
+)
 enditem()
 
 tt(zle_highlight) may contain additional fields for controlling how
diff --git a/Src/Zle/iwidgets.list b/Src/Zle/iwidgets.list
index 6a07212..ebcf317 100644
--- a/Src/Zle/iwidgets.list
+++ b/Src/Zle/iwidgets.list
@@ -28,7 +28,7 @@
 "beginning-of-history", beginningofhistory, 0
 "beginning-of-line", beginningofline, 0
 "beginning-of-line-hist", beginningoflinehist, 0
-"bracketed-paste", bracketedpaste, ZLE_MENUCMP | ZLE_KEEPSUFFIX
+"bracketed-paste", bracketedpaste, ZLE_MENUCMP | ZLE_KEEPSUFFIX | ZLE_YANKBEFORE
 "capitalize-word", capitalizeword, 0
 "clear-screen", clearscreen, ZLE_MENUCMP | ZLE_KEEPSUFFIX | ZLE_LASTCOL | ZLE_NOTCOMMAND
 "complete-word", completeword, ZLE_MENUCMP | ZLE_KEEPSUFFIX | ZLE_ISCOMP
diff --git a/Src/Zle/zle.h b/Src/Zle/zle.h
index ab2428e..59f4591 100644
--- a/Src/Zle/zle.h
+++ b/Src/Zle/zle.h
@@ -429,8 +429,9 @@ struct region_highlight {
  * 0: region between point and mark
  * 1: isearch region
  * 2: suffix
+ * 3: pasted text
  */
-#define N_SPECIAL_HIGHLIGHTS	(3)
+#define N_SPECIAL_HIGHLIGHTS	(4)
 
 
 #ifdef MULTIBYTE_SUPPORT
diff --git a/Src/Zle/zle_misc.c b/Src/Zle/zle_misc.c
index d350688..b040b97 100644
--- a/Src/Zle/zle_misc.c
+++ b/Src/Zle/zle_misc.c
@@ -517,10 +517,12 @@ copyregionaskill(char **args)
 
 /*
  * kct: index into kill ring, or -1 for original cutbuffer of yank.
- * yankb, yanke: mark the start and end of last yank in editing buffer.
  * yankcs marks the cursor position preceding the last yank
  */
-static int kct, yankb, yanke, yankcs;
+static int kct, yankcs;
+
+/**/
+int yankb, yanke; /* mark the start and end of last yank in editing buffer. */
 
 /* The original cutbuffer, either cutbuf or one of the vi buffers. */
 static Cutbuffer kctbuf;
@@ -778,10 +780,17 @@ bracketedpaste(char **args)
 	ZLE_STRING_T wpaste;
 	wpaste = stringaszleline((zmult == 1) ? pbuf :
 	    quotestring(pbuf, NULL, QT_BACKSLASH), 0, &n, NULL, NULL);
-	zmult = 1;
-	if (region_active)
-	    killregion(zlenoargs);
-	doinsert(wpaste, n);
+	cuttext(wpaste, n, CUT_REPLACE);
+	if (!(zmod.flags & MOD_VIBUF)) {
+	    kct = -1;
+	    kctbuf = &cutbuf;
+	    zmult = 1;
+	    if (region_active)
+		killregion(zlenoargs);
+	    yankcs = yankb = zlecs;
+	    doinsert(wpaste, n);
+	    yanke = zlecs;
+	}
 	free(pbuf); free(wpaste);
     }
     return 0;
diff --git a/Src/Zle/zle_refresh.c b/Src/Zle/zle_refresh.c
index fe33799..5b3b89b 100644
--- a/Src/Zle/zle_refresh.c
+++ b/Src/Zle/zle_refresh.c
@@ -318,6 +318,7 @@ zle_set_highlight(void)
     int region_atr_on_set = 0;
     int isearch_atr_on_set = 0;
     int suffix_atr_on_set = 0;
+    int paste_atr_on_set = 0;
     struct region_highlight *rhp;
 
     special_atr_on = default_atr_on = 0;
@@ -337,7 +338,8 @@ zle_set_highlight(void)
 	for (; *atrs; atrs++) {
 	    if (!strcmp(*atrs, "none")) {
 		/* reset attributes for consistency... usually unnecessary */
-		special_atr_on = default_atr_on = 0;
+		special_atr_on = default_atr_on =
+		    paste_atr_on_set = 0;
 		special_atr_on_set = region_atr_on_set =
 		    isearch_atr_on_set = suffix_atr_on_set = 1;
 	    } else if (strpfx("default:", *atrs)) {
@@ -354,6 +356,9 @@ zle_set_highlight(void)
 	    } else if (strpfx("suffix:", *atrs)) {
 		match_highlight(*atrs + 7, &(region_highlights[2].atr));
 		suffix_atr_on_set = 1;
+	    } else if (strpfx("paste:", *atrs)) {
+		match_highlight(*atrs + 6, &(region_highlights[3].atr));
+		paste_atr_on_set = 1;
 	    }
 	}
     }
@@ -367,6 +372,7 @@ zle_set_highlight(void)
 	region_highlights[1].atr = TXTUNDERLINE;
     if (!suffix_atr_on_set)
 	region_highlights[2].atr = TXTBOLDFACE;
+        /* paste defaults to 0 */
 
     allocate_colour_buffer();
 }
@@ -1073,6 +1079,14 @@ zrefresh(void)
 	region_highlights[2].start = region_highlights[2].end = -1;
     }
 
+    if (lastcmd & ZLE_YANK) {
+	region_highlights[3].start = yankb;
+	region_highlights[3].end = yanke;
+    } else {
+	region_highlights[3].start = region_highlights[3].end = -1;
+    }
+
+
     if (clearlist && listshown > 0) {
 	if (tccan(TCCLEAREOD)) {
 	    int ovln = vln, ovcs = vcs;



Messages sorted by: Reverse Date, Date, Thread, Author