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

Re: PATCH: make kill ring work in vi mode



Peter wrote:
> 
> Nothing here's a real problem, though if it's possible to refactor it
> more neatly that's nice to have.

Patch below refactors and adds tests. I left yank() alone which you
might question.

I think I'll ignore the undo issue until I've used it in practice
awhile. At least the current behaviour avoids the need for a
reverse-yank-pop.

Oliver

diff --git a/Src/Zle/zle_misc.c b/Src/Zle/zle_misc.c
index 10bd71c..3d4a9bb 100644
--- a/Src/Zle/zle_misc.c
+++ b/Src/Zle/zle_misc.c
@@ -507,6 +507,40 @@ yank(UNUSED(char **args))
     return 0;
 }
 
+static void pastebuf(Cutbuffer buf, int mult, int after)
+{
+    int cc;
+    if (buf->flags & CUTBUFFER_LINE) {
+	if (after) {
+	    yankb = zlecs = findeol();
+	    spaceinline(buf->len + 1);
+	    zleline[zlecs++] = ZWC('\n');
+	    yanke = zlecs + buf->len;
+	    ZS_memcpy(zleline + zlecs, buf->buf, buf->len);
+	} else {
+	    yankb = zlecs = findbol();
+	    spaceinline(buf->len + 1);
+	    ZS_memcpy(zleline + zlecs, buf->buf, buf->len);
+	    yanke = zlecs + buf->len + 1;
+	    zleline[zlecs + buf->len] = ZWC('\n');
+	}
+	vifirstnonblank(zlenoargs);
+    } else {
+	if (after && zlecs != findeol())
+	    INCCS();
+	yankb = zlecs;
+	cc = buf->len;
+	while (mult--) {
+	    spaceinline(cc);
+	    ZS_memcpy(zleline + zlecs, buf->buf, cc);
+	    zlecs += cc;
+	}
+	yanke = zlecs;
+	if (zlecs)
+	    DECCS();
+    }
+}
+
 /**/
 int
 viputbefore(UNUSED(char **args))
@@ -524,24 +558,7 @@ viputbefore(UNUSED(char **args))
 	return 1;
     kct = -1;
     yankcs = zlecs;
-    if (kctbuf->flags & CUTBUFFER_LINE) {
-	yankb = zlecs = findbol();
-	spaceinline(kctbuf->len + 1);
-	ZS_memcpy(zleline + zlecs, kctbuf->buf, kctbuf->len);
-	yanke = zlecs + kctbuf->len + 1;
-	zleline[zlecs + kctbuf->len] = ZWC('\n');
-	vifirstnonblank(zlenoargs);
-    } else {
-	yankb = zlecs;
-	while (n--) {
-	    spaceinline(kctbuf->len);
-	    ZS_memcpy(zleline + zlecs, kctbuf->buf, kctbuf->len);
-	    zlecs += kctbuf->len;
-	}
-	yanke = zlecs;
-	if (zlecs)
-	    DECCS();
-    }
+    pastebuf(kctbuf, n, 0);
     return 0;
 }
 
@@ -562,26 +579,7 @@ viputafter(UNUSED(char **args))
 	return 1;
     kct = -1;
     yankcs = zlecs;
-    if (kctbuf->flags & CUTBUFFER_LINE) {
-	yankb = zlecs = findeol();
-	spaceinline(kctbuf->len + 1);
-	zleline[zlecs++] = ZWC('\n');
-	yanke = zlecs + kctbuf->len;
-	ZS_memcpy(zleline + zlecs, kctbuf->buf, kctbuf->len);
-	vifirstnonblank(zlenoargs);
-    } else {
-	if (zlecs != findeol())
-	    INCCS();
-	yankb = zlecs;
-	while (n--) {
-	    spaceinline(kctbuf->len);
-	    ZS_memcpy(zleline + zlecs, kctbuf->buf, kctbuf->len);
-	    zlecs += kctbuf->len;
-	}
-	yanke = zlecs;
-	if (zlecs)
-	    DECCS();
-    }
+    pastebuf(kctbuf, n, 1);
     return 0;
 }
 
@@ -589,7 +587,7 @@ viputafter(UNUSED(char **args))
 int
 yankpop(UNUSED(char **args))
 {
-    int cc, kctstart = kct;
+    int kctstart = kct;
     Cutbuffer buf;
 
     if (!(lastcmd & ZLE_YANK) || !kring || !kctbuf) {
@@ -637,32 +635,7 @@ yankpop(UNUSED(char **args))
     zlecs = yankb;
     foredel(yanke - yankb, CUT_RAW);
     zlecs = yankcs;
-
-    if (buf->flags & CUTBUFFER_LINE) {
-	if (lastcmd & ZLE_YANKBEFORE) {
-	    yankb = zlecs = findbol();
-	    spaceinline(buf->len + 1);
-	    ZS_memcpy(zleline + zlecs, buf->buf, buf->len);
-	    yanke = zlecs + buf->len + 1;
-	    zleline[zlecs + buf->len] = ZWC('\n');
-	} else {
-	    yankb = zlecs = findeol();
-	    spaceinline(buf->len + 1);
-	    zleline[zlecs++] = ZWC('\n');
-	    yanke = zlecs + buf->len;
-	    ZS_memcpy(zleline + zlecs, buf->buf, buf->len);
-	}
-	vifirstnonblank(zlenoargs);
-    } else {
-	if ((lastcmd & ZLE_YANKAFTER) && zlecs != findeol())
-	    INCCS();
-	yankb = zlecs;
-	cc = buf->len;
-	spaceinline(cc);
-	ZS_memcpy(zleline + zlecs, buf->buf, cc);
-	zlecs += cc;
-	yanke = zlecs;
-    }
+    pastebuf(buf, 1, lastcmd & ZLE_YANKAFTER);
     return 0;
 }
 
diff --git a/Test/X02zlevi.ztst b/Test/X02zlevi.ztst
index 2af6f06..185980b 100644
--- a/Test/X02zlevi.ztst
+++ b/Test/X02zlevi.ztst
@@ -116,6 +116,34 @@
 >BUFFER: stnwararart
 >CURSOR: 9
 
+  zpty_run 'bindkey -a "^P" yank-pop'
+  zletest $'word\C-wline\eddiSE\eP\C-P'
+0:line based put before followed by character based yank-pop
+>BUFFER: SwordE
+>CURSOR: 4
+
+  zletest $'line\eddiword\C-w\eiSE\eP\C-P'
+0:character based put before followed by line based yank-pop
+>BUFFER: line
+>SE
+>CURSOR: 0
+
+  zletest $'one two three\C-w\C-w\C-wSE\e0p\C-P\C-P'
+0:put after cycled twice with yank-pop
+>BUFFER: SthreeE
+>CURSOR: 5
+
+  zletest $'word\C-wline\eddiSE\ehp\C-P'
+0:line based put after followed by character based yank-pop
+>BUFFER: SwordE
+>CURSOR: 5
+
+  zletest $'line\eddiword\C-w\eiSE\ehp\C-P'
+0:character based after before followed by line based yank-pop
+>BUFFER: SE
+>line
+>CURSOR: 3
+
   zletest $'word\euaend'
 0:undo initial change
 >BUFFER: end



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