>From ffb670f95b6b39c2a0815eb7c576f004311b8a24 Mon Sep 17 00:00:00 2001 From: Brand Huntsman Date: Mon, 22 Jan 2018 15:16:54 -0700 Subject: [PATCH] bright-is-not-bold toggle for UI colors and per-syntax Signed-off-by: Brand Huntsman --- src/global.c | 1 + src/nano.h | 1 + src/proto.h | 1 + src/rcfile.c | 51 +++++++++++++++++++++++++++++++++++++++------------ syntax/nanorc.nanorc | 1 + 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/global.c b/src/global.c index 238ba308..7fb9d4fc 100644 --- a/src/global.c +++ b/src/global.c @@ -235,6 +235,7 @@ regmatch_t regmatches[10]; int hilite_attribute = A_REVERSE; /* The curses attribute we use to highlight something. */ #ifdef ENABLE_COLOR +bool bright_is_not_bold = FALSE; colortype* color_combo[] = {NULL}; /* The color combinations for interface elements given in the rcfile. */ #endif diff --git a/src/nano.h b/src/nano.h index ba46f689..baa9c242 100644 --- a/src/nano.h +++ b/src/nano.h @@ -227,6 +227,7 @@ typedef struct syntaxtype { char *comment; /* The line comment prefix (and postfix) for this type of file. */ #endif + bool bright_is_not_bold; colortype *color; /* The colors and their regexes used in this syntax. */ int nmultis; diff --git a/src/proto.h b/src/proto.h index 4a8429e8..04296906 100644 --- a/src/proto.h +++ b/src/proto.h @@ -173,6 +173,7 @@ extern regmatch_t regmatches[10]; extern int hilite_attribute; #ifdef ENABLE_COLOR +extern bool bright_is_not_bold; extern colortype *color_combo[NUMBER_OF_ELEMENTS]; #endif extern int interface_color_pair[NUMBER_OF_ELEMENTS]; diff --git a/src/rcfile.c b/src/rcfile.c index 645bbec2..e0cfd46d 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -301,6 +301,7 @@ void parse_syntax(char *ptr) #ifdef ENABLE_COMMENT live_syntax->comment = mallocstrcpy(NULL, GENERAL_COMMENT_CHARACTER); #endif + live_syntax->bright_is_not_bold = FALSE; live_syntax->color = NULL; lastcolor = NULL; live_syntax->nmultis = 0; @@ -582,11 +583,13 @@ static int hex2int(char c) return -1; } -/* Return the short value corresponding to the color named in colorname, - * and set bold to TRUE if that color has bright prefix. */ -short color_to_short(const char *colorname, bool *bold) +/* Return the short value corresponding to the color named in colorname. + * Set bold to TRUE if color has bright prefix and !brightisnotbold(default). + * Set bright to TRUE if color has bright prefix and brightisnotbold. */ +short color_to_short(const char *colorname, bool *bold, bool *bright) { *bold = FALSE; + *bright = FALSE; /* RGB colors are #RRGGBB or #RGB hex formatted values. */ if (*colorname == '#') { @@ -633,9 +636,14 @@ short color_to_short(const char *colorname, bool *bold) } if (strncasecmp(colorname, "bright", 6) == 0) { - /* The bright foreground prefix makes the color bold and bright, - * or only bright on terminals without bold support. */ - *bold = TRUE; + if (opensyntax ? live_syntax->bright_is_not_bold : bright_is_not_bold) { + /* The bright prefix makes the color bright, without bolding it. */ + *bright = TRUE; + } else { + /* The bright foreground prefix makes the color bold and bright, + * or only bright on terminals without bold support. */ + *bold = TRUE; + } colorname += 6; } @@ -851,8 +859,8 @@ bool parse_color_names(char *combostr, short *fg, short *bg, int *attributes) /* Get foreground color. */ if (word != NULL) { if (*word != '\0') { - bool fg_bold; - *fg = color_to_short(word, &fg_bold); + bool fg_bold, fg_bright; + *fg = color_to_short(word, &fg_bold, &fg_bright); /* If the specified foreground color is bad, ignore the regexes. */ if (*fg == -1) @@ -862,6 +870,14 @@ bool parse_color_names(char *combostr, short *fg, short *bg, int *attributes) if (*attributes & A_BOLD) rcfile_error(N_("A foreground color should not be bright with bold attribute")); *attributes |= A_BOLD; + } else if (fg_bright) { + if (*attributes & A_BOLD) + rcfile_error(N_("A foreground color should not be bright with bold attribute")); + /* A bright foreground on 8-color is A_BOLD to brighten it. */ + if (nr_term_colors > 8) + *fg += 8; + else + *attributes |= A_BOLD; } } else *fg = -1; @@ -871,13 +887,17 @@ bool parse_color_names(char *combostr, short *fg, short *bg, int *attributes) /* Get background color. */ if (word != NULL && *word != '\0') { - bool bg_bold; - *bg = color_to_short(word, &bg_bold); + bool bg_bold, bg_bright; + *bg = color_to_short(word, &bg_bold, &bg_bright); - if (bg_bold) { + if (bg_bold || bg_bright) { rcfile_error(N_("A background color cannot be bright")); return FALSE; } + /* It is possible to have a bright background by adding 8 to it. + * But the syntax will show it as valid, even if !brightisnotbold. + * And "red,brightred" becomes "red,red" on 8-color. + * Use "reverse,brightred,red" for a similar result. */ } else *bg = -1; } else { @@ -1111,7 +1131,14 @@ void parse_rcfile(FILE *rcstream, bool syntax_only) #else ; #endif - else if (strcasecmp(keyword, "color") == 0) + else if (strcasecmp(keyword, "brightisnotbold") == 0) { + /* Change behavior of bright prefix for UI colors or current + * syntax. Using in nanorc does not change it for all syntaxes. */ + if (opensyntax) + live_syntax->bright_is_not_bold = TRUE; + else + bright_is_not_bold = TRUE; + } else if (strcasecmp(keyword, "color") == 0) parse_colors(ptr, NANO_REG_EXTENDED); else if (strcasecmp(keyword, "icolor") == 0) parse_colors(ptr, NANO_REG_EXTENDED | REG_ICASE); diff --git a/syntax/nanorc.nanorc b/syntax/nanorc.nanorc index a6a6be1e..03082476 100644 --- a/syntax/nanorc.nanorc +++ b/syntax/nanorc.nanorc @@ -20,6 +20,7 @@ icolor brightgreen "^[[:space:]]*unbind[[:space:]]+((\^([[:alpha:]]|[]0-9\^_]|Sp icolor brightgreen "^[[:space:]]*extendsyntax[[:space:]]+[[:alpha:]]+[[:space:]]+(i?color|header|magic|comment|linter|formatter)[[:space:]]+.*$" icolor brightgreen "^[[:space:]]*(linter|formatter)[[:space:]]+[[:alpha:]]+" icolor green "^[[:space:]]*((un)?(bind|set)|include|syntax|header|magic|comment|linter|formatter|extendsyntax)\>" +icolor green "^[[:space:]]*brightisnotbold$" # Strings color brightmagenta "".+"([[:space:]]|$)" -- 2.13.6