>From b01a3bfda6b4ce6bf5142d63c5835e9b8d4c3fd8 Mon Sep 17 00:00:00 2001
From: Brand Huntsman
Date: Thu, 18 Jan 2018 20:08:32 -0700
Subject: [PATCH 2/4] index colors
Signed-off-by: Brand Huntsman
---
src/color.c | 34 ++++++++++++++++++++++++++++++++++
src/global.c | 2 ++
src/nano.c | 4 ++++
src/proto.h | 2 ++
src/rcfile.c | 23 +++++++++++++++++++++--
syntax/nanorc.nanorc | 4 ++--
6 files changed, 65 insertions(+), 4 deletions(-)
diff --git a/src/color.c b/src/color.c
index 477582eb..67f9359c 100644
--- a/src/color.c
+++ b/src/color.c
@@ -27,6 +27,7 @@
#endif
#include
#include
+#include
#ifdef ENABLE_COLOR
@@ -430,4 +431,37 @@ void precalc_multicolorinfo(void)
}
}
+#if defined(NCURSES_VERSION_MAJOR) && (NCURSES_VERSION_MAJOR >= 6)
+#define HAS_EXTENDED_COLORS 1
+#else
+#define HAS_EXTENDED_COLORS 0
+#endif
+
+/* Get number of colors supported by terminal and initialize lookup tables. */
+void extended_color_init(void)
+{
+ if (!has_colors())
+ nr_term_colors = 0;
+
+ if (tgetent(NULL, getenv("TERM")) != 1) {
+ nr_term_colors = 0;
+ return;
+ }
+ nr_term_colors = tgetnum("Co");
+
+ if (nr_term_colors > 16 && !HAS_EXTENDED_COLORS) {
+ /* No support for extended colors. */
+ nr_term_colors = 16;
+ } else if (nr_term_colors > 256) {
+ nr_term_colors = 256;
+ } else if (nr_term_colors > 16) {
+ if (nr_term_colors != 256 && nr_term_colors != 88)
+ nr_term_colors = 16;
+ } else if (nr_term_colors > 8) {
+ if (nr_term_colors != 16)
+ nr_term_colors = 8;
+ } else if (nr_term_colors != 8)
+ nr_term_colors = 0;
+}
+
#endif /* ENABLE_COLOR */
diff --git a/src/global.c b/src/global.c
index 677211be..238ba308 100644
--- a/src/global.c
+++ b/src/global.c
@@ -183,6 +183,8 @@ char *syntaxstr = NULL;
/* The color syntax name specified on the command line. */
bool have_palette = FALSE;
/* Whether the colors for the current syntax have been initialized. */
+int nr_term_colors = 0;
+ /* Number of colors supported by terminal. */
#endif
bool refresh_needed = FALSE;
diff --git a/src/nano.c b/src/nano.c
index a21625cf..b8d8c538 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -1985,6 +1985,10 @@ int main(int argc, char **argv)
console = (getenv("DISPLAY") == NULL);
#endif
+#ifdef ENABLE_COLOR
+ extended_color_init();
+#endif
+
/* Back up the terminal settings so that they can be restored. */
tcgetattr(0, &oldterm);
diff --git a/src/proto.h b/src/proto.h
index 32c267b9..87334388 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -144,6 +144,7 @@ extern char *alt_speller;
extern syntaxtype *syntaxes;
extern char *syntaxstr;
extern bool have_palette;
+extern int nr_term_colors;
#endif
extern bool refresh_needed;
@@ -248,6 +249,7 @@ void color_update(void);
void check_the_multis(filestruct *line);
void alloc_multidata_if_needed(filestruct *fileptr);
void precalc_multicolorinfo(void);
+void extended_color_init(void);
#endif
/* Most functions in cut.c. */
diff --git a/src/rcfile.c b/src/rcfile.c
index 3fbdde3f..4d4070bd 100644
--- a/src/rcfile.c
+++ b/src/rcfile.c
@@ -573,13 +573,32 @@ void parse_includes(char *ptr)
* and set bold to TRUE if that color has bright prefix. */
short color_to_short(const char *colorname, bool *bold)
{
+ *bold = FALSE;
+
+ /* Index colors are numeric values from 0 to nr_term_colors-1.
+ * These are are not portable between terminals with different
+ * color counts. */
+ if (*colorname >= '0' && *colorname <= '9') {
+ ssize_t result = -1;
+ parse_num(colorname, &result);
+ if (result < 0) {
+ rcfile_error(N_("Color \"%s\" not understood"), colorname);
+ return -1;
+ }
+ if (result >= nr_term_colors) {
+ rcfile_error(N_("Color index \"%s\" must be 0 to %d"),
+ colorname, nr_term_colors-1);
+ return -1;
+ }
+ return result;
+ }
+
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;
colorname += 6;
- } else
- *bold = FALSE;
+ }
if (strcasecmp(colorname, "green") == 0)
return COLOR_GREEN;
diff --git a/syntax/nanorc.nanorc b/syntax/nanorc.nanorc
index aa5020ba..705d5820 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:]]+((bold|italic|reverse|underline)(,(bold|italic|reverse|underline))*|((bold|italic|reverse|underline),)*((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|italic|reverse|underline)(,(bold|italic|reverse|underline))*|((bold|italic|reverse|underline),)*((bright)?(white|black|red|blue|green|yellow|magenta|cyan)|[0-9]+)?(,(white|black|red|blue|green|yellow|magenta|cyan|[0-9]+))?)\>"
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:]]+((bold|italic|reverse|underline)(,(bold|italic|reverse|underline))*|((bold|italic|reverse|underline),)*((bright)?(white|black|red|blue|green|yellow|magenta|cyan))?(,(white|black|red|blue|green|yellow|magenta|cyan))?)\>"
+icolor yellow "^[[:space:]]*i?color[[:space:]]+((bold|italic|reverse|underline)(,(bold|italic|reverse|underline))*|((bold|italic|reverse|underline),)*((bright)?(white|black|red|blue|green|yellow|magenta|cyan)|[0-9]+)?(,(white|black|red|blue|green|yellow|magenta|cyan|[0-9]+))?)\>"
icolor magenta "^[[:space:]]*i?color\>" "\<(start|end)="
# Comments
--
2.13.6