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

Re: %F doesn't work in format style



On Thu, 15 May 2008 22:08:00 +0100
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:
> On Thu, 15 May 2008 21:29:25 +0200
> "Mikael Magnusson" <mikachu@xxxxxxxxx> wrote:
> > zstyle ':completion:*:corrections' format '%B---- %d (errors: %e)%b'
> > works fine
> > zstyle ':completion:*:corrections' format '%F{red}---- %f%d (errors: %e)%b'
> > produces
> > {red}---- corrections (errors: 2)
> > (unformatted)
> > 
> > The manpage claims
> >   This  string  may  also  contain  the  sequences
> >   to specify output attributes, such as `%B', `%S' and `%{...%}'
> > 
> > Is %F such as %B? :) It's listed in the same section anyway.
> 
> This is all handled completely separately in zle_tricky.c for standard
> completion formatting and yet again in complist.c when zsh/complist is
> loaded.

The code's not that big, and there wasn't enough nexus for a joinder (I
don't know what this means either), so I've just added it in both places.

Also fix the non-use of fallthrough in the prompt code (for %F{default},
just to be compatible), and propagate the use of %{ with a number to
indicate width to the completion code.  Possibly the documentation now
needs to be more explict.  (Lucky I didn't grab %d for any of the colour
sequences.)

Index: Src/prompt.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/prompt.c,v
retrieving revision 1.50
diff -u -r1.50 prompt.c
--- Src/prompt.c	12 May 2008 13:50:42 -0000	1.50
+++ Src/prompt.c	16 May 2008 09:28:02 -0000
@@ -471,7 +471,6 @@
 		    break;
 		}
 		/* else FALLTHROUGH */
-		break;
 	    case 'f':
 		txtchangeset(txtchangep, TXTNOFGCOLOUR, TXT_ATTR_FG_ON_MASK);
 		txtunset(TXT_ATTR_FG_ON_MASK);
@@ -493,7 +492,6 @@
 		    break;
 		}
 		/* else FALLTHROUGH */
-		break;
 	    case 'k':
 		txtchangeset(txtchangep, TXTNOBGCOLOUR, TXT_ATTR_BG_ON_MASK);
 		txtunset(TXT_ATTR_BG_ON_MASK);
@@ -1472,7 +1470,7 @@
  */
 
 /**/
-static int
+mod_export int
 match_colour(const char **teststrp, int is_fg, int colour)
 {
     int shft, on, named = 0, tc;
Index: Src/Zle/complist.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/complist.c,v
retrieving revision 1.115
diff -u -r1.115 complist.c
--- Src/Zle/complist.c	12 May 2008 16:49:59 -0000	1.115
+++ Src/Zle/complist.c	16 May 2008 09:28:02 -0000
@@ -1046,6 +1046,8 @@
 	if (doesc && cchar == ZWC('%')) {
 	    p += len;
 	    if (*p) {
+		int arg = 0, is_fg;
+
 		len = MB_METACHARLENCONV(p, &cchar);
 #ifdef MULTIBYTE_SUPPORT
 		if (cchar == WEOF)
@@ -1053,6 +1055,9 @@
 #endif
 		p += len;
 
+		if (idigit(*p))
+		    arg = zstrtol(p, &p, 10);
+
 		m = 0;
 		switch (cchar) {
 		case ZWC('%'):
@@ -1099,7 +1104,32 @@
 		    if (dopr)
 			tcout(TCUNDERLINEEND);
 		    break;
+		case ZWC('F'):
+		case ZWC('K'):
+		    is_fg = (cchar == ZWC('F'));
+		    /* colours must be ASCII */
+		    if (*p == '{') {
+			p++;
+			arg = match_colour((const char **)&p, is_fg, 0);
+			if (*p == '}')
+			    p++;
+		    } else
+			arg = match_colour(NULL, is_fg, arg);
+		    if (arg >= 0 && dopr)
+			set_colour_attribute(arg, is_fg ? COL_SEQ_FG :
+					     COL_SEQ_BG, 0);
+		    break;
+		case ZWC('f'):
+		    if (dopr)
+			set_colour_attribute(TXTNOFGCOLOUR, COL_SEQ_FG, 0);
+		    break;
+		case ZWC('k'):
+		    if (dopr)
+			set_colour_attribute(TXTNOBGCOLOUR, COL_SEQ_BG, 0);
+		    break;
 		case ZWC('{'):
+		    if (arg)
+			cc += arg;
 		    for (; *p && (*p != '%' || p[1] != '}'); p++)
 			if (dopr)
 			    putc(*p == Meta ? *++p ^ 32 : *p, shout);
Index: Src/Zle/zle_tricky.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_tricky.c,v
retrieving revision 1.94
diff -u -r1.94 zle_tricky.c
--- Src/Zle/zle_tricky.c	22 Apr 2008 15:08:14 -0000	1.94
+++ Src/Zle/zle_tricky.c	16 May 2008 09:28:03 -0000
@@ -2272,7 +2272,10 @@
     for (; *p; ) {
 	/* Handle the `%' stuff (%% == %, %n == <number of matches>). */
 	if (doesc && *p == '%') {
-	    if (*++p) {
+	    int arg = 0, is_fg;
+	    if (idigit(*++p))
+		arg = zstrtol(p, &p, 10);
+	    if (*p) {
 		m = 0;
 		switch (*p) {
 		case '%':
@@ -2316,11 +2319,33 @@
 		    if (dopr)
 			tcout(TCUNDERLINEEND);
 		    break;
+		case 'F':
+		case 'K':
+		    is_fg = (*p == 'F');
+		    if (p[1] == '{') {
+			p += 2;
+			arg = match_colour((const char **)&p, is_fg, 0);
+			if (*p != '}')
+			    p--;
+		    } else
+			arg = match_colour(NULL, is_fg, arg);
+		    if (arg >= 0)
+			set_colour_attribute(arg, is_fg ? COL_SEQ_FG :
+					     COL_SEQ_BG, 0);
+		    break;
+		case 'f':
+		    set_colour_attribute(TXTNOFGCOLOUR, COL_SEQ_FG, 0);
+		    break;
+		case 'k':
+		    set_colour_attribute(TXTNOBGCOLOUR, COL_SEQ_BG, 0);
+		    break;
 		case '{':
+		    if (arg)
+			cc += arg;
 		    for (p++; *p && (*p != '%' || p[1] != '}'); p++) {
 			if (*p == Meta) {
 			    p++;
-			    if (dopr) 
+			    if (dopr)
 				putc(*p ^ 32, shout);
 			}
 			else if (dopr)



-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



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