zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-workers@sunsite.dk (Zsh hackers list)
Subject: PATCH: colour sanity checks
Date: Tue, 06 May 2008 22:26:10 +0100	[thread overview]
Message-ID: <17832.1210109170@pws-pc> (raw)

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@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


                 reply	other threads:[~2008-05-06 21:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=17832.1210109170@pws-pc \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).