From db1218712b39348f03925878963a24a7573ef83f Mon Sep 17 00:00:00 2001
From: Gregory P. Smith <greg@electricrain.com>
Date: Wed, 29 Nov 2006 23:47:38 -0800
Subject: ircII goes reverse color when switching colour to on in xterms

Bug-Debian: http://bugs.debian.org/61008
---
 include/config.h.dist |    4 +-
 include/screen.h      |    2 +
 source/screen.c       |   87 ++++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 80 insertions(+), 13 deletions(-)

diff --git a/include/config.h.dist b/include/config.h.dist
index f01da3b..e6d23c5 100644
--- a/include/config.h.dist
+++ b/include/config.h.dist
@@ -113,7 +113,7 @@
 #define DEFAULT_ALWAYS_SPLIT_BIGGEST 1
 #define DEFAULT_AUTO_UNMARK_AWAY 0
 #define DEFAULT_AUTO_WHOWAS 0
-#define DEFAULT_BACKGROUND_COLOUR 1
+#define DEFAULT_BACKGROUND_COLOUR 16
 #define DEFAULT_BEEP 1
 #define DEFAULT_BEEP_MAX 3
 #define DEFAULT_BEEP_ON_MSG "NONE"
@@ -143,7 +143,7 @@
 #define DEFAULT_FLOOD_RATE 3
 #define DEFAULT_FLOOD_USERS 3
 #define DEFAULT_FLOOD_WARNING 0
-#define DEFAULT_FOREGROUND_COLOUR 15
+#define DEFAULT_FOREGROUND_COLOUR 16
 #define DEFAULT_FULL_STATUS_LINE 1
 #define DEFAULT_HELP_PAGER 1
 #define DEFAULT_HELP_PROMPT 1
diff --git a/include/screen.h b/include/screen.h
index c840d52..c6d61fa 100644
--- a/include/screen.h
+++ b/include/screen.h
@@ -42,6 +42,8 @@
 #define WAIT_PROMPT_LINE	0x01
 #define WAIT_PROMPT_KEY		0x02
 
+#define COLOUR_DEFAULT	16
+
 /* Stuff for the screen/xterm junk */
 
 #define ST_NOTHING	-1
diff --git a/source/screen.c b/source/screen.c
index 6baaacc..782c9cf 100644
--- a/source/screen.c
+++ b/source/screen.c
@@ -471,6 +471,19 @@ display_colours(fgcolour, bgcolour)
 	int fgcolour;
 	int bgcolour;
 {
+	static int current_fg = COLOUR_DEFAULT;
+	static int current_bg = COLOUR_DEFAULT;
+	static int current_bold = 0;
+	static int current_blink = 0;
+
+	int emit_code = 0;
+	int desired_bold, desired_blink;
+
+	if (fgcolour < 0)
+		fgcolour = COLOUR_DEFAULT;
+	if (bgcolour < 0)
+		bgcolour = COLOUR_DEFAULT;
+
 	if (get_int_var(COLOUR_VAR))
 	{
 		/* Some people will say that I should use termcap values but
@@ -486,6 +499,10 @@ display_colours(fgcolour, bgcolour)
 		 */
 		 
 		/* Written by Bisqwit (bisqwit@iki.fi) */
+		/* Reworked to support default terminal color, allowing use of
+		 * color on non-black background terminals.  20061129
+		 *    -- Gregory P. Smith (greg@krypto.org)
+		 */
 		 
 		/* mirc colours -> iso 6469 colours translation tables */
 		static const u_char trans[] = "7042115332664507";
@@ -494,14 +511,63 @@ display_colours(fgcolour, bgcolour)
 		
 		u_char iso[15]; /* long enough for "e[0;1;5;37;40m" */
 		
-		snprintf(CP(iso), sizeof iso, "\33[0;");
-		if (bolds[fgcolour] == '1')
-			my_strcat(iso, "1;");
-		if (bolds[bgcolour] == '1')
-			my_strcat(iso, "5;");
-		snprintf(CP(my_index(iso, 0)), 7, "3%c;4%cm", trans[fgcolour&15], trans[bgcolour&15]);
-		
-		fwrite(CP(iso), my_strlen(iso), 1, current_screen->fpout);
+		snprintf(CP(iso), sizeof iso, "\33[");
+		if (fgcolour != COLOUR_DEFAULT)
+			desired_bold = bolds[fgcolour&15] - '0';
+		else
+			desired_bold = current_bold;
+
+		if (bgcolour != COLOUR_DEFAULT)
+			desired_blink = bolds[bgcolour&15] - '0';
+		else
+			desired_blink = current_blink;
+
+		/* to turn off bold or blink we have to issue a '0' for reset */
+		if (current_bold != desired_bold || current_blink != desired_blink)
+		{
+			my_strcat(iso, "0");
+			emit_code = 1;
+			current_bold = 0;
+			current_blink = 0;
+			current_bg = COLOUR_DEFAULT;	/* XXX is this needed? */
+			current_fg = COLOUR_DEFAULT;	/* XXX is this needed? */
+		}
+		if (desired_bold)
+		{
+			if (emit_code)
+				my_strcat(iso, ";");
+			my_strcat(iso, "1");
+			emit_code = 1;
+			current_bold = 1;
+		}
+		if (desired_blink)
+		{
+			if (emit_code)
+				my_strcat(iso, ";");
+			my_strcat(iso, "5");
+			emit_code = 1;
+			current_blink = 1;
+		}
+
+		if (fgcolour != current_fg)
+		{
+			if (emit_code)
+				my_strcat(iso, ";");
+			snprintf(CP(my_index(iso, 0)), 3, "3%c", trans[fgcolour&15]);
+			emit_code = 1;
+		}
+		if (bgcolour != current_bg)
+		{
+			if (emit_code)
+				my_strcat(iso, ";");
+			snprintf(CP(my_index(iso, 0)), 3, "4%c", trans[bgcolour&15]);
+			emit_code = 1;
+		}
+		if (emit_code)
+		{
+			my_strcat(iso, "m");
+			fwrite(CP(iso), my_strlen(iso), 1, current_screen->fpout);
+		}
 	}
 }
 
@@ -663,15 +729,14 @@ output_line(str, startpos)
 		bgcolour_user = get_int_var(BACKGROUND_COLOUR_VAR);
 	static	int	high = OFF,
 			bold = OFF,
-			fgcolour = -1,
-			bgcolour = -1;
+			fgcolour = COLOUR_DEFAULT,
+			bgcolour = COLOUR_DEFAULT;
 	int	rev_tog, und_tog, bld_tog, all_off;
 	int     dobeep = 0;
 	int     written = 0;
 
 	display_highlight(high);
 	display_bold(bold);
-	display_colours(fgcolour, bgcolour);
 	/* do processing on the string, handle inverse and bells */
 	display_nonshift();
 	while (*str)
