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

PATCH: colour sanity checks



Some playtesting revealed odd things could happen with colours out of
the termcap range; I've performed the sanity checking earlier.

In addition, I've made all numeric colours on terminals with appropriate
termcap sequences use those for consistency.  To generate ANSI colour
sequences you need to use the names.

Index: Doc/Zsh/zle.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/zle.yo,v
retrieving revision 1.70
diff -u -r1.70 zle.yo
--- Doc/Zsh/zle.yo	6 May 2008 20:56:50 -0000	1.70
+++ Doc/Zsh/zle.yo	6 May 2008 21:24:29 -0000
@@ -2180,18 +2180,16 @@
 
 Not all terminals support this and, of those that do, not all provide
 facilities to test the support, hence the user should decide based on the
-terminal type.  Most terminals with colour support accept the numbers 0 to
-7, and may generate additional colours if the tt(bold) attributes is also
-present.  Most terminals also have a standard range of colours for those
-numbers (though the interpretation of the colour can vary); these colours
-can be set by one of the names tt(black), tt(red), tt(green), tt(yellow),
-tt(blue), tt(magenta), tt(cyan) and tt(white).  Abbreviations are
-allowed; tt(b) or tt(bl) selects black.
+terminal type.  Most terminals support the colours tt(black), tt(red),
+tt(green), tt(yellow), tt(blue), tt(magenta), tt(cyan) and tt(white),
+which can be set by name.  Abbreviations are allowed; tt(b) or tt(bl)
+selects black.  Some terminals may generate additional colours if the
+tt(bold) attribute is also present.
 
 On recent terminals and on systems with an up-to-date terminal database the
 number of colours supported may be tested by with the command `tt(echotc
 Co)'; if this succeeds, it indicates a limit on the number of colours which
-will be enforced by the line editor.  The number of colours is in case
+will be enforced by the line editor.  The number of colours is in any case
 limited to 256 (i.e. the range 0 to 255).
 
 Colour is also known as color.
Index: Src/Zle/zle_refresh.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_refresh.c,v
retrieving revision 1.67
diff -u -r1.67 zle_refresh.c
--- Src/Zle/zle_refresh.c	6 May 2008 18:19:15 -0000	1.67
+++ Src/Zle/zle_refresh.c	6 May 2008 21:24:29 -0000
@@ -460,10 +460,10 @@
 	found = 0;
 	if (strpfx("fg=", teststr) || strpfx("bg=", teststr)) {
 	    int is_fg = (teststr[0] == 'f');
-	    int colour, shft, on;
+	    int colour, shft, on, named, tc;
 
 	    teststr += 3;
-	    if (ialpha(*teststr))
+	    if ((named = ialpha(*teststr)))
 		colour = match_colour(&teststr);
 	    else
 		colour = (int)zstrtol(teststr, (char **)&teststr, 10);
@@ -478,9 +478,33 @@
 	    if (is_fg) {
 		shft = TXT_ATTR_FG_COL_SHIFT;
 		on = TXTFGCOLOUR;
+		tc = TCFGCOLOUR;
 	    } else {
 		shft = TXT_ATTR_BG_COL_SHIFT;
 		on = TXTBGCOLOUR;
+		tc = TCBGCOLOUR;
+	    }
+	    /*
+	     * Try termcap for numbered characters if posible.
+	     * Don't for named characters, since our best bet
+	     * of getting the names right is with ANSI sequences.
+	     */
+	    if (!named && tccan(tc)) {
+		if (tccolours >= 0 && colour >= tccolours) {
+		    /*
+		     * Out of range of termcap colours.
+		     * Can we assume ANSI colours work?
+		     */
+		    if (colour > 7)
+			continue; /* No. */
+		} else {
+		    /*
+		     * We can handle termcap colours and the number
+		     * is in range, so use termcap.
+		     */
+		    *on_var |= is_fg ? TXT_ATTR_FG_TERMCAP :
+			TXT_ATTR_BG_TERMCAP;
+		}
 	    }
 	    *on_var |= on | (colour << shft);
 	} else {
@@ -1137,8 +1161,11 @@
     /*
      * If we're not restoring the default, and either have a
      * colour value that is too large for ANSI, or have been told
-     * to use the termcap sequence (which at the time of writing
-     * we never are), try to use the termcap sequence.
+     * to use the termcap sequence, try to use the termcap sequence.
+     *
+     * We have already sanitised the values we allow from the
+     * highlighting variables, so much of this shouldn't be
+     * necessary at this point, but we might as well be safe.
      */
     if (!def && (colour > 7 || use_termcap)) {
 	/*


-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



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