From 2fa11b836d587c646c84e5439f2a7e84e2ff58d2 Mon Sep 17 00:00:00 2001
From: Chris Allegretta <chrisa@asty.org>
Date: Sun, 2 Dec 2001 04:55:44 +0000
Subject: [PATCH] Two fixes, create new colorstrings array for each regex,
 allows proper order of highlighting, second make color work with the marker

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@915 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 color.c  |  38 ++++++-----
 nano.h   |   1 +
 rcfile.c |  49 +++++---------
 winio.c  | 195 ++++++++++++++++++++++++-------------------------------
 4 files changed, 123 insertions(+), 160 deletions(-)

diff --git a/color.c b/color.c
index f25eb093..1ebc1e51 100644
--- a/color.c
+++ b/color.c
@@ -98,8 +98,8 @@ void colorinit_one(int colortoset, short fg, short bg, int bold)
 
 int do_colorinit(void)
 {
-    int i, fg, bg;
-    colortype *tmpcolor = NULL;
+    int i;
+    colortype *tmpcolor = NULL, *beforenow = NULL;
     int defok = 0;
 
     if (has_colors()) {
@@ -115,24 +115,26 @@ int do_colorinit(void)
 	for (tmpcolor = colorstrings; tmpcolor != NULL; 
 		tmpcolor = tmpcolor->next) {
 
-	    if (tmpcolor->fg > 8)
-		fg = tmpcolor->fg - 8;
-	    else
-		fg = tmpcolor->fg;
-
-	    if (tmpcolor->bg > 8)
-		bg = tmpcolor->bg - 8;
-	    else
-		bg = tmpcolor->bg;
-
-	    if (defok && bg == -1)
-		init_pair(i, fg, -1);
-            else if (bg == -1)
-		init_pair(i, fg, COLOR_BLACK);
+	    for (beforenow = colorstrings; beforenow != NULL
+		 && beforenow != tmpcolor && 
+		 (beforenow->fg != tmpcolor->fg || beforenow->bg != tmpcolor->bg
+		 || beforenow->bright != tmpcolor->bright);
+		beforenow = beforenow->next)
+		;
+
+	    if (beforenow != NULL && beforenow != tmpcolor) {
+		tmpcolor->pairnum = beforenow->pairnum;
+		continue;
+	    }
+	    
+	    if (defok && tmpcolor->bg == -1)
+		init_pair(i, tmpcolor->fg, -1);
+            else if (tmpcolor->bg == -1)
+		init_pair(i, tmpcolor->fg, COLOR_BLACK);
 	    else /* They picked a fg and bg color */
-		init_pair(i, fg, bg);
+		init_pair(i, tmpcolor->fg, tmpcolor->bg);
 
-	    fprintf(stderr, "Running init_pair with fg = %d and bg = %d\n", fg, bg);
+	    fprintf(stderr, "Running init_pair with fg = %d and bg = %d\n", tmpcolor->fg, tmpcolor->bg);
 
 	    tmpcolor->pairnum = i;
 	    i++;
diff --git a/nano.h b/nano.h
index 546ab870..1a08e521 100644
--- a/nano.h
+++ b/nano.h
@@ -129,6 +129,7 @@ typedef struct colorstr {
 typedef struct colortype {
     int fg;
     int bg;
+    int bright;
     int pairnum;
     colorstr *str;
     struct colortype *next;
diff --git a/rcfile.c b/rcfile.c
index c778a95f..c48b7f79 100644
--- a/rcfile.c
+++ b/rcfile.c
@@ -122,7 +122,7 @@ char *parse_next_word(char *ptr)
     return ptr;
 }
 
-int colortoint(char *colorname, char *filename, int *lineno) 
+int colortoint(char *colorname, int *bright, char *filename, int *lineno) 
 {
     int mcolor = 0;
 
@@ -130,7 +130,7 @@ int colortoint(char *colorname, char *filename, int *lineno)
 	return -1;
 
     if (strcasestr(colorname, "bright")) {
-	mcolor += 8;
+	*bright = 1;
 	colorname += 6;
     }
     
@@ -167,7 +167,7 @@ int colortoint(char *colorname, char *filename, int *lineno)
 /* Parse the color stuff into the colorstrings array */
 void parse_colors(FILE *rcstream, char *filename, int *lineno, char *buf, char *ptr)
 {
-    int i = 0, fg, bg;
+    int i = 0, fg, bg, bright = 0;
     char prev = '\\';
     char *tmp = NULL, *beginning, *fgstr, *bgstr;
     colortype *tmpcolor = NULL;
@@ -188,8 +188,8 @@ void parse_colors(FILE *rcstream, char *filename, int *lineno, char *buf, char *
     } else
 	bgstr = NULL;
 
-    fg = colortoint(fgstr, filename, lineno);
-    bg = colortoint(bgstr, filename, lineno);
+    fg = colortoint(fgstr, &bright, filename, lineno);
+    bg = colortoint(bgstr, &bright, filename, lineno);
 
     /* Now the fun part, start adding regexps to individual strings
 	in the colorstrings array, woo! */
@@ -219,45 +219,28 @@ void parse_colors(FILE *rcstream, char *filename, int *lineno, char *buf, char *
 		    colorstrings = nmalloc(sizeof(colortype));
 		    colorstrings->fg = fg;
 		    colorstrings->bg = bg;
+		    colorstrings->bright = bright;
 		    colorstrings->str = NULL;
 		    colorstrings->str = nmalloc(sizeof(colorstr));
 		    colorstrings->str->val = tmp;
 		    colorstrings->str->next = NULL;
 		    colorstrings->next = NULL;
 		} else {
-		    for (tmpcolor = colorstrings;
-			tmpcolor->next != NULL && !(tmpcolor->fg == fg 
-			&& tmpcolor->bg == bg); tmpcolor = tmpcolor->next)
+		    for (tmpcolor = colorstrings; tmpcolor->next != NULL;
+			  tmpcolor = tmpcolor->next)
 			;
-
-		    /* An entry for this color pair already exists, add it
-			to the str list */
-		    if (tmpcolor->fg == fg && tmpcolor->bg == bg) {
-			for (tmpstr = tmpcolor->str; tmpstr->next != NULL;
-				tmpstr = tmpstr->next)
-			    ;
-
-#ifdef DEBUG
-		    fprintf(stderr, "Adding to existing entry for fg %d bg %d\n", fg, bg);
-#endif
-
-			tmpstr->next = nmalloc (sizeof(colorstr));
-			tmpstr->next->val = tmp;
-			tmpstr->next->next = NULL;
-		    } else {
-
 #ifdef DEBUG
 		    fprintf(stderr, "Adding new entry for fg %d bg %d\n", fg, bg);
 #endif
 
-			tmpcolor->next = nmalloc(sizeof(colortype));
-			tmpcolor->next->fg = fg;
-			tmpcolor->next->bg = bg;
-			tmpcolor->next->str = nmalloc(sizeof(colorstr));
-			tmpcolor->next->str->val = tmp;
-			tmpcolor->next->str->next = NULL;
-			tmpcolor->next->next = NULL;
-		    }
+		    tmpcolor->next = nmalloc(sizeof(colortype));
+		    tmpcolor->next->fg = fg;
+		    tmpcolor->next->bg = bg;
+		    tmpcolor->next->bright = bright;
+		    tmpcolor->next->str = nmalloc(sizeof(colorstr));
+		    tmpcolor->next->str->val = tmp;
+		    tmpcolor->next->str->next = NULL;
+		    tmpcolor->next->next = NULL;
 		}
 
 		i = 0;
diff --git a/winio.c b/winio.c
index d851dab3..b3233f7a 100644
--- a/winio.c
+++ b/winio.c
@@ -741,9 +741,6 @@ void add_marked_sameline(int begin, int end, filestruct * fileptr, int y,
     sel_data_len = end - begin;
     post_data_len = this_page_end - end;
 
-    /* Paint this line! */
-    mvwaddnstr(edit, y, 0, &fileptr->data[this_page_start], pre_data_len);
-
 #ifdef ENABLE_COLOR
     color_on(edit, COLOR_MARKER);
 #else
@@ -759,8 +756,6 @@ void add_marked_sameline(int begin, int end, filestruct * fileptr, int y,
     wattroff(edit, A_REVERSE);
 #endif /* ENABLE_COLOR */
 
-    mvwaddnstr(edit, y, end - this_page_start,
-	       &fileptr->data[end], post_data_len);
 }
 #endif
 
@@ -774,10 +769,65 @@ void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
 {
 
 #ifndef NANO_SMALL
+    colortype *tmpcolor = NULL;
+    colorstr *tmpstr = NULL;
+    int k, paintlen;
+#endif
+
+
+
+    /* Just paint the string in any case (we'll add color or reverse on
+	just the text that needs it */
+    mvwaddnstr(edit, yval, 0, &fileptr->data[start],
+	    get_page_end_virtual(this_page) - start + 1);
+
+#ifndef NANO_SMALL
+    if (colorstrings != NULL)
+	for (tmpcolor = colorstrings; tmpcolor != NULL; tmpcolor = tmpcolor->next) {
+	    for (tmpstr = tmpcolor->str; tmpstr != NULL; tmpstr = tmpstr->next) {
+
+		k = start;
+		regcomp(&search_regexp, tmpstr->val, 0);
+		while (!regexec(&search_regexp, &fileptr->data[k], 1, 
+		    regmatches, 0)) {
+
+#ifdef DEBUG
+		    fprintf(stderr, "Match! (%d chars) \"%s\"\n",
+			regmatches[0].rm_eo - regmatches[0].rm_so,
+			&fileptr->data[k + regmatches[0].rm_so]);
+#endif
+		    if (regmatches[0].rm_so < COLS - 1) {
+			if (tmpcolor->bright)
+			    wattron(edit, A_BOLD);
+			wattron(edit, COLOR_PAIR(tmpcolor->pairnum));
+
+			if (regmatches[0].rm_eo - regmatches[0].rm_so 
+			    + k <= COLS)
+			    paintlen = regmatches[0].rm_eo - regmatches[0].rm_so;
+			else
+			    paintlen = COLS - (regmatches[0].rm_eo 
+					-  regmatches[0].rm_so);
+
+			mvwaddnstr(edit, yval, regmatches[0].rm_so + k,
+			    &fileptr->data[k + regmatches[0].rm_so], 
+			    paintlen);
+
+
+		    }
+
+		    if (tmpcolor->bright)
+			wattroff(edit, A_BOLD);
+		    wattroff(edit, COLOR_PAIR(tmpcolor->pairnum));
+
+		    k += regmatches[0].rm_eo;
+		}
+	    }
+	}
+
     /* There are quite a few cases that could take place; we'll deal
      * with them each in turn */
-    if (ISSET(MARK_ISSET)
-	&& !((fileptr->lineno > mark_beginbuf->lineno
+    if (ISSET(MARK_ISSET) &&  
+	!((fileptr->lineno > mark_beginbuf->lineno
 	      && fileptr->lineno > current->lineno)
 	     || (fileptr->lineno < mark_beginbuf->lineno
 		 && fileptr->lineno < current->lineno))) {
@@ -832,41 +882,36 @@ void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
 #else
 		wattron(edit, A_REVERSE);
 #endif /* ENABLE_COLOR */
-	    }
 
-	    target =
-		(virt_mark_beginx <
-		 COLS - 1) ? virt_mark_beginx : COLS - 1;
+		target =
+		    (virt_mark_beginx < COLS - 1) ? virt_mark_beginx : COLS - 1;
 
-	    mvwaddnstr(edit, yval, 0, fileptr->data, target);
-
-	    if (mark_beginbuf->lineno < current->lineno) {
+		mvwaddnstr(edit, yval, 0, fileptr->data, target);
 
 #ifdef ENABLE_COLOR
-		color_on(edit, COLOR_MARKER);
+		color_off(edit, COLOR_MARKER);
 #else
-		wattron(edit, A_REVERSE);
+		wattroff(edit, A_REVERSE);
 #endif /* ENABLE_COLOR */
 
-	    } else {
 
+	    }
+
+	    if (mark_beginbuf->lineno < current->lineno) {
 #ifdef ENABLE_COLOR
-		color_off(edit, COLOR_MARKER);
+		color_on(edit, COLOR_MARKER);
 #else
-		wattroff(edit, A_REVERSE);
+		wattron(edit, A_REVERSE);
 #endif /* ENABLE_COLOR */
 
-	    }
+		target = (COLS - 1) - virt_mark_beginx;
 
-	    target = (COLS - 1) - virt_mark_beginx;
-	    if (target < 0)
-		target = 0;
+		if (target < 0)
+		    target = 0;
 
-	    mvwaddnstr(edit, yval, virt_mark_beginx,
+		mvwaddnstr(edit, yval, virt_mark_beginx,
 		       &fileptr->data[virt_mark_beginx], target);
 
-	    if (mark_beginbuf->lineno < current->lineno) {
-
 #ifdef ENABLE_COLOR
 		color_off(edit, COLOR_MARKER);
 #else
@@ -889,44 +934,37 @@ void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
 		wattron(edit, A_REVERSE);
 #endif /* ENABLE_COLOR */
 
-	    }
-
-	    if (virt_cur_x > COLS - 2) {
-		mvwaddnstr(edit, yval, 0,
+		if (virt_cur_x > COLS - 2) {
+		    mvwaddnstr(edit, yval, 0,
 			   &fileptr->data[this_page_start],
 			   virt_cur_x - this_page_start);
-	    } else {
-		mvwaddnstr(edit, yval, 0, fileptr->data, virt_cur_x);
-	    }
-
-	    if (mark_beginbuf->lineno > current->lineno) {
+		} else
+		    mvwaddnstr(edit, yval, 0, fileptr->data, virt_cur_x);
 
 #ifdef ENABLE_COLOR
-		color_on(edit, COLOR_MARKER);
+		color_off(edit, COLOR_MARKER);
 #else
-		wattron(edit, A_REVERSE);
+		wattroff(edit, A_REVERSE);
 #endif /* ENABLE_COLOR */
 
-	    } else {
+	    }
+
+	    if (mark_beginbuf->lineno > current->lineno) {
 
 #ifdef ENABLE_COLOR
-		color_off(edit, COLOR_MARKER);
+		color_on(edit, COLOR_MARKER);
 #else
-		wattroff(edit, A_REVERSE);
+		wattron(edit, A_REVERSE);
 #endif /* ENABLE_COLOR */
 
-	    }
-
-	    if (virt_cur_x > COLS - 2)
-		mvwaddnstr(edit, yval, virt_cur_x - this_page_start,
+		if (virt_cur_x > COLS - 2)
+		    mvwaddnstr(edit, yval, virt_cur_x - this_page_start,
 			   &fileptr->data[virt_cur_x],
 			   this_page_end - virt_cur_x);
-	    else
-		mvwaddnstr(edit, yval, virt_cur_x,
+		else
+		    mvwaddnstr(edit, yval, virt_cur_x,
 			   &fileptr->data[virt_cur_x], COLS - virt_cur_x);
 
-	    if (mark_beginbuf->lineno > current->lineno) {
-
 #ifdef ENABLE_COLOR
 		color_off(edit, COLOR_MARKER);
 #else
@@ -935,68 +973,7 @@ void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
 
 	    }
 	}
-
-    } else
-#endif
-	/* Just paint the string (no mark on this line) */
-	mvwaddnstr(edit, yval, 0, &fileptr->data[start],
-		   get_page_end_virtual(this_page) - start + 1);
-
-#ifdef ENABLE_COLOR
-    {
-    colortype *tmpcolor = NULL;
-    colorstr *tmpstr = NULL;
-    int k, paintlen;
-
-    if (colorstrings != NULL)
-    for (tmpcolor = colorstrings; tmpcolor != NULL; tmpcolor = tmpcolor->next) {
-	for (tmpstr = tmpcolor->str; tmpstr != NULL; tmpstr = tmpstr->next) {
-
-	    k = start;
-	    regcomp(&search_regexp, tmpstr->val, 0);
-	    while (!regexec(&search_regexp, &fileptr->data[k], 1, 
-		regmatches, 0)) {
-
-#ifdef DEBUG
-		fprintf(stderr, "Match! (%d chars) \"%s\"\n",
-		    regmatches[0].rm_eo - regmatches[0].rm_so,
-		    &fileptr->data[k + regmatches[0].rm_so]);
-#endif
-		if (regmatches[0].rm_so < COLS - 1) {
-		    if (tmpcolor->fg > 8 || tmpcolor->bg > 8) {
-			wattron(edit, A_BOLD);
-			wattron(edit, COLOR_PAIR(tmpcolor->pairnum));
-		    }
-		    else
-			wattron(edit, COLOR_PAIR(tmpcolor->pairnum));
-
-		    if (regmatches[0].rm_eo - regmatches[0].rm_so 
-			+ k <= COLS)
-			paintlen = regmatches[0].rm_eo - regmatches[0].rm_so;
-		    else
-			paintlen = COLS - (regmatches[0].rm_eo 
-					-  regmatches[0].rm_so);
-
-		    mvwaddnstr(edit, yval, regmatches[0].rm_so + k,
-			&fileptr->data[k + regmatches[0].rm_so], 
-			paintlen);
-
-
-		}
-
-		if (tmpcolor->fg > 8 || tmpcolor->bg > 8) {
-		    wattroff(edit, A_BOLD);
-		    wattroff(edit, COLOR_PAIR(tmpcolor->pairnum));
-		}
-		else
-		    wattroff(edit, COLOR_PAIR(tmpcolor->pairnum));
-
-		k += regmatches[0].rm_eo;
-	    }
-	}
-    }
     }
-
 #endif
 
 }
-- 
GitLab