[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Nano-devel] [PATCH 2/2] colors: separate bold from bright, as far as po
From: |
Benno Schulenberg |
Subject: |
[Nano-devel] [PATCH 2/2] colors: separate bold from bright, as far as possible |
Date: |
Fri, 9 Feb 2018 11:31:57 +0100 |
From: Brand Huntsman <address@hidden>
Allow the user to specify "bold" in their nanorc file to make
colored characters bold (when the terminal supports this), and
change the bright prefix to mean only bright (when the terminal
is capable of it).
If you want bright colors to always be bold, use the following
command to convert your syntax files:
sed -i 's/color bright/color bold,/' *.nanorc
Signed-off-by: Brand Huntsman <address@hidden>
---
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 aaac83db..426692a3 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] = hilite_attribute;
@@ -89,7 +89,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)
@@ -97,8 +98,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 44d71949..af351377 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 b14192d5..2ac9a796 100644
--- a/src/rcfile.c
+++ b/src/rcfile.c
@@ -578,7 +578,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) {
@@ -614,7 +614,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) {
@@ -630,7 +630,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') {
@@ -677,7 +677,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);
@@ -735,30 +735,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;
}
@@ -767,7 +810,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 9882d6b0..9896d77e 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:]]+((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:]]+((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|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.14.3