Signed-off-by: Brand Huntsman --- src/color.c | 8 +++--- src/nano.h | 2 -- src/proto.h | 2 +- src/rcfile.c | 79 ++++++++++++++++++++++++++++++++++++++++------------ syntax/nanorc.nanorc | 4 +-- 5 files changed, 68 insertions(+), 27 deletions(-) diff --git a/src/color.c b/src/color.c index 7d0c07be..3c68c777 100644 --- a/src/color.c +++ b/src/color.c @@ -67,7 +67,7 @@ void set_colorpairs(void) combo->bg = COLOR_BLACK; init_pair(i + 1, combo->fg, combo->bg); interface_color_pair[i] = COLOR_PAIR(i + 1) | A_BANDAID | - (combo->bright ? A_BOLD : A_NORMAL); + combo->attributes; } else { if (i == FUNCTION_TAG) interface_color_pair[i] = A_NORMAL; @@ -92,7 +92,8 @@ void set_colorpairs(void) while (beforenow != ink && (beforenow->fg != ink->fg || beforenow->bg != ink->bg || - beforenow->bright != ink->bright)) + (beforenow->attributes & 0xFFFF0000) != + ink->attributes)) beforenow = beforenow->next; if (beforenow != ink) @@ -100,8 +101,7 @@ void set_colorpairs(void) else ink->pairnum = new_number++; - ink->attributes = COLOR_PAIR(ink->pairnum) | A_BANDAID | - (ink->bright ? A_BOLD : A_NORMAL); + ink->attributes |= COLOR_PAIR(ink->pairnum) | A_BANDAID; } } } diff --git a/src/nano.h b/src/nano.h index c092d3e1..ca9950ad 100644 --- a/src/nano.h +++ b/src/nano.h @@ -182,8 +182,6 @@ typedef struct colortype { /* This syntax's foreground color. */ short bg; /* This syntax's background color. */ - bool bright; - /* Is this color A_BOLD? */ int pairnum; /* The color pair number used for this foreground color and * background color. */ diff --git a/src/proto.h b/src/proto.h index e656e636..887a1bc8 100644 --- a/src/proto.h +++ b/src/proto.h @@ -475,7 +475,7 @@ int do_yesno_prompt(bool all, const char *msg); /* Most functions in rcfile.c. */ #ifdef ENABLE_NANORC #ifdef ENABLE_COLOR -bool parse_color_names(char *combostr, short *fg, short *bg, bool *bright); +bool parse_color_names(char *combostr, short *fg, short *bg, int *attributes); void grab_and_store(const char *kind, char *ptr, regexlisttype **storage); #endif void parse_rcfile(FILE *rcstream, bool syntax_only); diff --git a/src/rcfile.c b/src/rcfile.c index 82f75a53..a86c9d47 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -584,7 +584,7 @@ void parse_includes(char *ptr) } /* Return the short value corresponding to the color named in colorname, - * and set bright to TRUE if that color is bright. */ + * and set bright to TRUE if that color has bright prefix. */ short color_to_short(const char *colorname, bool *bright) { if (strncasecmp(colorname, "bright", 6) == 0) { @@ -620,7 +620,7 @@ short color_to_short(const char *colorname, bool *bright) void parse_colors(char *ptr, int rex_flags) { short fg, bg; - bool bright; + int attributes; char *item; if (!opensyntax) { @@ -636,7 +636,7 @@ void parse_colors(char *ptr, int rex_flags) item = ptr; ptr = parse_next_word(ptr); - if (!parse_color_names(item, &fg, &bg, &bright)) + if (!parse_color_names(item, &fg, &bg, &attributes)) return; if (*ptr == '\0') { @@ -683,7 +683,7 @@ void parse_colors(char *ptr, int rex_flags) newcolor->fg = fg; newcolor->bg = bg; - newcolor->bright = bright; + newcolor->attributes = attributes; newcolor->rex_flags = rex_flags; newcolor->start_regex = mallocstrcpy(NULL, item); @@ -741,30 +741,73 @@ void parse_colors(char *ptr, int rex_flags) } } +/* Find the first comma in the given text. Return a pointer to the + * character after the comma, or NULL if no comma was found. */ +static char *find_next_comma_word(char *text) +{ + if (text == NULL) + return NULL; + + char *comma = strchr(text, ','); + + if (comma == NULL) + return NULL; + + *comma = '\0'; + return comma + 1; +} + /* Parse the color name, or pair of color names, in combostr. */ -bool parse_color_names(char *combostr, short *fg, short *bg, bool *bright) +bool parse_color_names(char *combostr, short *fg, short *bg, int *attributes) { - char *comma = strchr(combostr, ','); + char *word = combostr; + char *next_word = find_next_comma_word(combostr); + bool bright; - if (comma != NULL) { - *bg = color_to_short(comma + 1, bright); - if (*bright) { - rcfile_error(N_("A background color cannot be bright")); - return FALSE; - } - *comma = '\0'; - } else - *bg = -1; + *attributes = A_NORMAL; - if (comma != combostr) { - *fg = color_to_short(combostr, bright); + /* If the specified color starts with "bold,", set the bold attribute. + * The bold attribute makes named colors bold and bright, or only bright + * on terminals without bold support, or only bold for extended colors. */ + if (word != NULL && strcasecmp(word, "bold") == 0) { + *attributes |= A_BOLD; + + word = next_word; + next_word = find_next_comma_word(word); + } + + /* If it's given and not empty, get the foreground color. */ + if (word != NULL && *word != '\0') { + *fg = color_to_short(word, &bright); /* If the specified foreground color is bad, ignore the regexes. */ if (*fg == -1) return FALSE; + + if (bright) { + /* On an 8-color terminal, use bold to brighten the color. */ + if (nr_term_colors <= 8) + *attributes |= A_BOLD; + else + *fg += 8; + } } else *fg = -1; + word = next_word; + next_word = find_next_comma_word(word); + + /* If it's given and not empty, get the background color. */ + if (word != NULL && *word != '\0') { + *bg = color_to_short(word, &bright); + + if (bright) { + rcfile_error(N_("A background color cannot be bright")); + return FALSE; + } + } else + *bg = -1; + return TRUE; } @@ -773,7 +816,7 @@ colortype *parse_interface_color(char *combostr) { colortype *trio = nmalloc(sizeof(colortype)); - if (parse_color_names(combostr, &trio->fg, &trio->bg, &trio->bright)) { + if (parse_color_names(combostr, &trio->fg, &trio->bg, &trio->attributes)) { free(combostr); return trio; } else { diff --git a/syntax/nanorc.nanorc b/syntax/nanorc.nanorc index 9140c1a3..dde60f8a 100644 --- a/syntax/nanorc.nanorc +++ b/syntax/nanorc.nanorc @@ -8,7 +8,7 @@ icolor brightred "^[[:space:]]*((un)?(bind|set)|include|syntax|header|magic|comm # Keywords icolor brightgreen "^[[:space:]]*(set|unset)[[:space:]]+(allow_insecure_backup|atblanks|autoindent|backup|backwards|boldtext|casesensitive|constantshow|cutfromcursor|fill[[:space:]]+-?[[:digit:]]+|historylog|linenumbers|locking|morespace|mouse|multibuffer|noconvert|nohelp|nopauses|nonewlines|nowrap|positionlog|preserve|quickblank|quiet|rebinddelete|rebindkeypad|regexp|showcursor|smarthome|smooth|softwrap|suspend|tabsize[[:space:]]+[1-9][0-9]*|tabstospaces|tempfile|trimblanks|unix|view|wordbounds)\>" -icolor yellow "^[[:space:]]*set[[:space:]]+((error|function|key|number|selected|status|title)color)[[:space:]]+(bright)?(white|black|red|blue|green|yellow|magenta|cyan)?(,(white|black|red|blue|green|yellow|magenta|cyan))?\>" +icolor yellow "^[[:space:]]*set[[:space:]]+((error|function|key|number|selected|status|title)color)[[:space:]]+(bold|(bold,)?((bright)?(white|black|red|blue|green|yellow|magenta|cyan))?(,(white|black|red|blue|green|yellow|magenta|cyan))?)\>" icolor brightgreen "^[[:space:]]*set[[:space:]]+(backupdir|brackets|errorcolor|functioncolor|keycolor|matchbrackets|numbercolor|operatingdir|punct|quotestr|selectedcolor|speller|statuscolor|titlecolor|whitespace|wordchars)[[:space:]]+" icolor brightgreen "^[[:space:]]*bind[[:space:]]+((\^([[:alpha:]]|[]0-9\^_]|Space)|M-([[:alpha:]]|[]!"#$%&'()*+,./0-9:;<=>address@hidden|}~-]|Space))|F([1-9]|1[0-6])|Ins|Del)[[:space:]]+[[:alpha:]]+[[:space:]]+(all|main|search|replace(with)?|gotoline|writeout|insert|ext(ernal)?cmd|help|spell|linter|browser|whereisfile|gotodir)([[:space:]]+#|[[:space:]]*$)" icolor brightgreen "^[[:space:]]*unbind[[:space:]]+((\^([[:alpha:]]|[]0-9\^_]|Space)|M-([[:alpha:]]|[]!"#$%&'()*+,./0-9:;<=>address@hidden|}~-]|Space))|F([1-9]|1[0-6])|Ins|Del)[[:space:]]+(all|main|search|replace(with)?|gotoline|writeout|insert|ext(ernal)?cmd|help|spell|linter|browser|whereisfile|gotodir)([[:space:]]+#|[[:space:]]*$)" @@ -20,7 +20,7 @@ icolor green "^[[:space:]]*((un)?(bind|set)|include|syntax|header|magic|comment| color brightmagenta "".+"([[:space:]]|$)" # Colors -icolor yellow "^[[:space:]]*i?color[[:space:]]*(bright)?(white|black|red|blue|green|yellow|magenta|cyan)?(,(white|black|red|blue|green|yellow|magenta|cyan))?\>" +icolor yellow "^[[:space:]]*i?color[[:space:]]+(bold|(bold,)?((bright)?(white|black|red|blue|green|yellow|magenta|cyan))?(,(white|black|red|blue|green|yellow|magenta|cyan))?)\>" icolor magenta "^[[:space:]]*i?color\>" "\<(start|end)=" # Comments -- 2.16.1