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

Re: copy-prev-word doesn't respect universal argument



Vin Shelton <acs@xxxxxxxxxxxxxxxxxxxx> wrote:
> Why don't copy-prev-word and copy-prev-shell-word respect the
> universal argument?
> 
> Given the following:
> 
>   bindkey '^XA' copy-prev-word
>   echo abc def ghi <ESC>2^XA
> 
> I would expect the result to be
> 
>   echo abc def ghi def
> 
> but zle seems always to copy the most recent word, ('ghi' in this
> case), yielding:
> 
>   echo abc def ghi ghi
> 
> no matter what the prefix arg is.  I'm surprised I'm the first person
> to request this.

I think it's just a question of "when are you going to get around to
putting up those shelves".

I didn't bother handling negative prefix arguments.  Once, I would have
been more zealous.

> Is there an alternate way to copy an arbitrary word
> from earlier in the same command, or is the !#:<n> syntax sufficient
> for most people?

Actually, there is: see the supplied function copy-earlier-word which can
cycle through earlier words in the same way as insert-last-word cycles back
through the history.  In fact, copy-earlier-word works in combination with
insert-last-word so that you can copy earlier words on previous command
lines, too.  Note, however, that the sense of the digit argument is
different; N copies the Nth word from the start of the line.  I'm not sure
why I did it that way (and althought it's correctly documented it disagrees
with a comment in the function).

Index: Src/Zle/zle_misc.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_misc.c,v
retrieving revision 1.32
diff -u -r1.32 zle_misc.c
--- Src/Zle/zle_misc.c	2 Nov 2005 10:11:33 -0000	1.32
+++ Src/Zle/zle_misc.c	2 Nov 2005 10:51:51 -0000
@@ -616,17 +616,28 @@
 int
 copyprevword(UNUSED(char **args))
 {
-    int len, t0;
+    int len, t0 = zlecs, t1;
 
-    for (t0 = zlecs - 1; t0 >= 0; t0--)
-	if (ZC_iword(zleline[t0]))
-	    break;
-    for (; t0 >= 0; t0--)
-	if (!ZC_iword(zleline[t0]))
-	    break;
-    if (t0)
-	t0++;
-    len = zlecs - t0;
+    if (zmult > 0) {
+	int count = zmult;
+
+	for (;;) {
+	    t1 = t0;
+
+	    while (t0 && !ZC_iword(zleline[t0-1]))
+		t0--;
+	    while (t0 && ZC_iword(zleline[t0-1]))
+		t0--;
+
+	    if (!--count)
+		break;
+	    if (t0 == 0)
+		return 1;
+	}
+    }
+    else
+	return 1;
+    len = t1 - t0;
     spaceinline(len);
     ZS_memcpy(zleline + zlecs, zleline + t0, len);
     zlecs += len;
@@ -642,12 +653,19 @@
     int i;
     unsigned char *p = NULL;
 
-    if ((l = bufferwords(NULL, NULL, &i)))
+    if (zmult <= 0)
+	return 1;
+
+    if ((l = bufferwords(NULL, NULL, &i))) {
+	i -= (zmult-1);
+	if (i < 0)
+	    return 1;
         for (n = firstnode(l); n; incnode(n))
             if (!i--) {
                 p = (unsigned char *)getdata(n);
                 break;
             }
+    }
 
     if (p) {
 	int len;
-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com



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