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
master
Chris Allegretta 2001-12-02 04:55:44 +00:00
parent 86c9676f7a
commit 2fa11b836d
4 changed files with 125 additions and 162 deletions

34
color.c
View File

@ -98,8 +98,8 @@ void colorinit_one(int colortoset, short fg, short bg, int bold)
int do_colorinit(void) int do_colorinit(void)
{ {
int i, fg, bg; int i;
colortype *tmpcolor = NULL; colortype *tmpcolor = NULL, *beforenow = NULL;
int defok = 0; int defok = 0;
if (has_colors()) { if (has_colors()) {
@ -115,24 +115,26 @@ int do_colorinit(void)
for (tmpcolor = colorstrings; tmpcolor != NULL; for (tmpcolor = colorstrings; tmpcolor != NULL;
tmpcolor = tmpcolor->next) { tmpcolor = tmpcolor->next) {
if (tmpcolor->fg > 8) for (beforenow = colorstrings; beforenow != NULL
fg = tmpcolor->fg - 8; && beforenow != tmpcolor &&
else (beforenow->fg != tmpcolor->fg || beforenow->bg != tmpcolor->bg
fg = tmpcolor->fg; || beforenow->bright != tmpcolor->bright);
beforenow = beforenow->next)
;
if (tmpcolor->bg > 8) if (beforenow != NULL && beforenow != tmpcolor) {
bg = tmpcolor->bg - 8; tmpcolor->pairnum = beforenow->pairnum;
else continue;
bg = tmpcolor->bg; }
if (defok && bg == -1) if (defok && tmpcolor->bg == -1)
init_pair(i, fg, -1); init_pair(i, tmpcolor->fg, -1);
else if (bg == -1) else if (tmpcolor->bg == -1)
init_pair(i, fg, COLOR_BLACK); init_pair(i, tmpcolor->fg, COLOR_BLACK);
else /* They picked a fg and bg color */ 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; tmpcolor->pairnum = i;
i++; i++;

1
nano.h
View File

@ -129,6 +129,7 @@ typedef struct colorstr {
typedef struct colortype { typedef struct colortype {
int fg; int fg;
int bg; int bg;
int bright;
int pairnum; int pairnum;
colorstr *str; colorstr *str;
struct colortype *next; struct colortype *next;

View File

@ -122,7 +122,7 @@ char *parse_next_word(char *ptr)
return ptr; return ptr;
} }
int colortoint(char *colorname, char *filename, int *lineno) int colortoint(char *colorname, int *bright, char *filename, int *lineno)
{ {
int mcolor = 0; int mcolor = 0;
@ -130,7 +130,7 @@ int colortoint(char *colorname, char *filename, int *lineno)
return -1; return -1;
if (strcasestr(colorname, "bright")) { if (strcasestr(colorname, "bright")) {
mcolor += 8; *bright = 1;
colorname += 6; colorname += 6;
} }
@ -167,7 +167,7 @@ int colortoint(char *colorname, char *filename, int *lineno)
/* Parse the color stuff into the colorstrings array */ /* Parse the color stuff into the colorstrings array */
void parse_colors(FILE *rcstream, char *filename, int *lineno, char *buf, char *ptr) 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 prev = '\\';
char *tmp = NULL, *beginning, *fgstr, *bgstr; char *tmp = NULL, *beginning, *fgstr, *bgstr;
colortype *tmpcolor = NULL; colortype *tmpcolor = NULL;
@ -188,8 +188,8 @@ void parse_colors(FILE *rcstream, char *filename, int *lineno, char *buf, char *
} else } else
bgstr = NULL; bgstr = NULL;
fg = colortoint(fgstr, filename, lineno); fg = colortoint(fgstr, &bright, filename, lineno);
bg = colortoint(bgstr, filename, lineno); bg = colortoint(bgstr, &bright, filename, lineno);
/* Now the fun part, start adding regexps to individual strings /* Now the fun part, start adding regexps to individual strings
in the colorstrings array, woo! */ in the colorstrings array, woo! */
@ -219,33 +219,16 @@ void parse_colors(FILE *rcstream, char *filename, int *lineno, char *buf, char *
colorstrings = nmalloc(sizeof(colortype)); colorstrings = nmalloc(sizeof(colortype));
colorstrings->fg = fg; colorstrings->fg = fg;
colorstrings->bg = bg; colorstrings->bg = bg;
colorstrings->bright = bright;
colorstrings->str = NULL; colorstrings->str = NULL;
colorstrings->str = nmalloc(sizeof(colorstr)); colorstrings->str = nmalloc(sizeof(colorstr));
colorstrings->str->val = tmp; colorstrings->str->val = tmp;
colorstrings->str->next = NULL; colorstrings->str->next = NULL;
colorstrings->next = NULL; colorstrings->next = NULL;
} else { } else {
for (tmpcolor = colorstrings; for (tmpcolor = colorstrings; tmpcolor->next != NULL;
tmpcolor->next != NULL && !(tmpcolor->fg == fg tmpcolor = tmpcolor->next)
&& tmpcolor->bg == bg); 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 #ifdef DEBUG
fprintf(stderr, "Adding new entry for fg %d bg %d\n", fg, bg); fprintf(stderr, "Adding new entry for fg %d bg %d\n", fg, bg);
#endif #endif
@ -253,12 +236,12 @@ void parse_colors(FILE *rcstream, char *filename, int *lineno, char *buf, char *
tmpcolor->next = nmalloc(sizeof(colortype)); tmpcolor->next = nmalloc(sizeof(colortype));
tmpcolor->next->fg = fg; tmpcolor->next->fg = fg;
tmpcolor->next->bg = bg; tmpcolor->next->bg = bg;
tmpcolor->next->bright = bright;
tmpcolor->next->str = nmalloc(sizeof(colorstr)); tmpcolor->next->str = nmalloc(sizeof(colorstr));
tmpcolor->next->str->val = tmp; tmpcolor->next->str->val = tmp;
tmpcolor->next->str->next = NULL; tmpcolor->next->str->next = NULL;
tmpcolor->next->next = NULL; tmpcolor->next->next = NULL;
} }
}
i = 0; i = 0;
beginning = ptr; beginning = ptr;

173
winio.c
View File

@ -741,9 +741,6 @@ void add_marked_sameline(int begin, int end, filestruct * fileptr, int y,
sel_data_len = end - begin; sel_data_len = end - begin;
post_data_len = this_page_end - end; 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 #ifdef ENABLE_COLOR
color_on(edit, COLOR_MARKER); color_on(edit, COLOR_MARKER);
#else #else
@ -759,8 +756,6 @@ void add_marked_sameline(int begin, int end, filestruct * fileptr, int y,
wattroff(edit, A_REVERSE); wattroff(edit, A_REVERSE);
#endif /* ENABLE_COLOR */ #endif /* ENABLE_COLOR */
mvwaddnstr(edit, y, end - this_page_start,
&fileptr->data[end], post_data_len);
} }
#endif #endif
@ -774,10 +769,65 @@ void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
{ {
#ifndef NANO_SMALL #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 /* There are quite a few cases that could take place; we'll deal
* with them each in turn */ * with them each in turn */
if (ISSET(MARK_ISSET) if (ISSET(MARK_ISSET) &&
&& !((fileptr->lineno > mark_beginbuf->lineno !((fileptr->lineno > mark_beginbuf->lineno
&& fileptr->lineno > current->lineno) && fileptr->lineno > current->lineno)
|| (fileptr->lineno < mark_beginbuf->lineno || (fileptr->lineno < mark_beginbuf->lineno
&& fileptr->lineno < current->lineno))) { && fileptr->lineno < current->lineno))) {
@ -832,41 +882,36 @@ void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
#else #else
wattron(edit, A_REVERSE); wattron(edit, A_REVERSE);
#endif /* ENABLE_COLOR */ #endif /* ENABLE_COLOR */
}
target = target =
(virt_mark_beginx < (virt_mark_beginx < COLS - 1) ? virt_mark_beginx : COLS - 1;
COLS - 1) ? virt_mark_beginx : COLS - 1;
mvwaddnstr(edit, yval, 0, fileptr->data, target); mvwaddnstr(edit, yval, 0, fileptr->data, target);
if (mark_beginbuf->lineno < current->lineno) {
#ifdef ENABLE_COLOR
color_on(edit, COLOR_MARKER);
#else
wattron(edit, A_REVERSE);
#endif /* ENABLE_COLOR */
} else {
#ifdef ENABLE_COLOR #ifdef ENABLE_COLOR
color_off(edit, COLOR_MARKER); color_off(edit, COLOR_MARKER);
#else #else
wattroff(edit, A_REVERSE); wattroff(edit, A_REVERSE);
#endif /* ENABLE_COLOR */ #endif /* ENABLE_COLOR */
} }
if (mark_beginbuf->lineno < current->lineno) {
#ifdef ENABLE_COLOR
color_on(edit, COLOR_MARKER);
#else
wattron(edit, A_REVERSE);
#endif /* ENABLE_COLOR */
target = (COLS - 1) - virt_mark_beginx; target = (COLS - 1) - virt_mark_beginx;
if (target < 0) if (target < 0)
target = 0; target = 0;
mvwaddnstr(edit, yval, virt_mark_beginx, mvwaddnstr(edit, yval, virt_mark_beginx,
&fileptr->data[virt_mark_beginx], target); &fileptr->data[virt_mark_beginx], target);
if (mark_beginbuf->lineno < current->lineno) {
#ifdef ENABLE_COLOR #ifdef ENABLE_COLOR
color_off(edit, COLOR_MARKER); color_off(edit, COLOR_MARKER);
#else #else
@ -889,14 +934,19 @@ void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
wattron(edit, A_REVERSE); wattron(edit, A_REVERSE);
#endif /* ENABLE_COLOR */ #endif /* ENABLE_COLOR */
}
if (virt_cur_x > COLS - 2) { if (virt_cur_x > COLS - 2) {
mvwaddnstr(edit, yval, 0, mvwaddnstr(edit, yval, 0,
&fileptr->data[this_page_start], &fileptr->data[this_page_start],
virt_cur_x - this_page_start); virt_cur_x - this_page_start);
} else { } else
mvwaddnstr(edit, yval, 0, fileptr->data, virt_cur_x); mvwaddnstr(edit, yval, 0, fileptr->data, virt_cur_x);
#ifdef ENABLE_COLOR
color_off(edit, COLOR_MARKER);
#else
wattroff(edit, A_REVERSE);
#endif /* ENABLE_COLOR */
} }
if (mark_beginbuf->lineno > current->lineno) { if (mark_beginbuf->lineno > current->lineno) {
@ -907,16 +957,6 @@ void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
wattron(edit, A_REVERSE); wattron(edit, A_REVERSE);
#endif /* ENABLE_COLOR */ #endif /* ENABLE_COLOR */
} else {
#ifdef ENABLE_COLOR
color_off(edit, COLOR_MARKER);
#else
wattroff(edit, A_REVERSE);
#endif /* ENABLE_COLOR */
}
if (virt_cur_x > COLS - 2) if (virt_cur_x > COLS - 2)
mvwaddnstr(edit, yval, virt_cur_x - this_page_start, mvwaddnstr(edit, yval, virt_cur_x - this_page_start,
&fileptr->data[virt_cur_x], &fileptr->data[virt_cur_x],
@ -925,8 +965,6 @@ void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x,
mvwaddnstr(edit, yval, virt_cur_x, mvwaddnstr(edit, yval, virt_cur_x,
&fileptr->data[virt_cur_x], COLS - virt_cur_x); &fileptr->data[virt_cur_x], COLS - virt_cur_x);
if (mark_beginbuf->lineno > current->lineno) {
#ifdef ENABLE_COLOR #ifdef ENABLE_COLOR
color_off(edit, COLOR_MARKER); color_off(edit, COLOR_MARKER);
#else #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 #endif
} }