2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 #include <lib-lib/lib-lib.h>
20 int ColorDefs[MT_COLOR_MAX];
21 COLOR_LINE *ColorHdrList = NULL;
22 COLOR_LINE *ColorBodyList = NULL;
23 COLOR_LINE *ColorIndexList = NULL;
25 /* local to this file */
26 static int ColorQuoteSize;
28 #define COLOR_DEFAULT (-2)
30 typedef struct color_list {
35 struct color_list *next;
38 static COLOR_LIST *ColorList = NULL;
39 static int UserColors = 0;
41 static struct mapping_t Colors[] = {
42 {"black", COLOR_BLACK},
45 {"green", COLOR_GREEN},
46 {"magenta", COLOR_MAGENTA},
48 {"white", COLOR_WHITE},
49 {"yellow", COLOR_YELLOW},
50 #if defined (USE_SLANG_CURSES) || defined (HAVE_USE_DEFAULT_COLORS)
51 {"default", COLOR_DEFAULT},
56 static struct mapping_t Fields[] = {
57 {"hdrdefault", MT_COLOR_HDEFAULT},
58 {"quoted", MT_COLOR_QUOTED},
59 {"signature", MT_COLOR_SIGNATURE},
60 {"indicator", MT_COLOR_INDICATOR},
61 {"status", MT_COLOR_STATUS},
62 {"tree", MT_COLOR_TREE},
63 {"error", MT_COLOR_ERROR},
64 {"normal", MT_COLOR_NORMAL},
65 {"tilde", MT_COLOR_TILDE},
66 {"markers", MT_COLOR_MARKERS},
67 {"header", MT_COLOR_HEADER},
68 {"body", MT_COLOR_BODY},
69 {"message", MT_COLOR_MESSAGE},
70 {"attachment", MT_COLOR_ATTACHMENT},
71 {"search", MT_COLOR_SEARCH},
72 {"bold", MT_COLOR_BOLD},
73 {"underline", MT_COLOR_UNDERLINE},
74 {"index", MT_COLOR_INDEX},
75 {"sidebar_new", MT_COLOR_NEW},
76 {"sidebar", MT_COLOR_SIDEBAR},
77 {"sidebar_flagged", MT_COLOR_FLAGGED},
81 #define COLOR_QUOTE_INIT 8
83 static COLOR_LINE *mutt_new_color_line (void)
85 COLOR_LINE *p = p_new(COLOR_LINE, 1);
92 static void mutt_free_color_line (COLOR_LINE ** l, int free_colors)
101 if (free_colors && tmp->fg != -1 && tmp->bg != -1)
102 mutt_free_color (tmp->fg, tmp->bg);
104 /* we should really use the container type for regular expressions. */
107 pattern_list_wipe(&tmp->color_pattern);
108 p_delete(&tmp->pattern);
112 void ci_start_color (void)
114 memset(ColorDefs, A_NORMAL, sizeof(int) * MT_COLOR_MAX);
115 ColorQuote = p_new(int, COLOR_QUOTE_INIT);
116 memset(ColorQuote, A_NORMAL, sizeof(int) * COLOR_QUOTE_INIT);
117 ColorQuoteSize = COLOR_QUOTE_INIT;
120 /* set some defaults */
121 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
122 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
123 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
124 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
125 /* special meaning: toggle the relevant attribute */
126 ColorDefs[MT_COLOR_BOLD] = 0;
127 ColorDefs[MT_COLOR_UNDERLINE] = 0;
132 #ifdef USE_SLANG_CURSES
133 static char *get_color_name (char *dest, size_t destlen, int val)
135 static char *missing[3] = { "brown", "lightgray", "default" };
140 m_strcpy(dest, destlen, missing[0]);
144 m_strcpy(dest, destlen, missing[1]);
148 m_strcpy(dest, destlen, missing[2]);
152 for (i = 0; Colors[i].name; i++) {
153 if (Colors[i].value == val) {
154 m_strcpy(dest, destlen, Colors[i].name);
159 /* Sigh. If we got this far, the color is of the form 'colorN'
160 * Slang can handle this itself, so just return 'colorN'
163 snprintf (dest, destlen, "color%d", val);
168 int mutt_alloc_color (int fg, int bg)
170 COLOR_LIST *p = ColorList;
173 #if defined (USE_SLANG_CURSES)
174 char fgc[STRING], bgc[STRING];
177 /* check to see if this color is already allocated to save space */
179 if (p->fg == fg && p->bg == bg) {
181 return (COLOR_PAIR (p->index));
186 /* check to see if there are colors left */
187 if (++UserColors > COLOR_PAIRS)
190 /* find the smallest available index (object) */
204 p = p_new(COLOR_LIST, 1);
213 #if defined (USE_SLANG_CURSES)
214 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
215 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg),
216 get_color_name (bgc, sizeof (bgc), bg));
218 #elif defined (HAVE_USE_DEFAULT_COLORS)
219 if (fg == COLOR_DEFAULT)
221 if (bg == COLOR_DEFAULT)
225 init_pair (i, fg, bg);
227 return (COLOR_PAIR (p->index));
230 void mutt_free_color (int fg, int bg)
236 if (p->fg == fg && p->bg == bg) {
243 if (p == ColorList) {
244 ColorList = ColorList->next;
264 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER * err)
268 if (m_strncasecmp(s, "bright", 6) == 0) {
273 /* allow aliases for xterm color resources */
274 if (m_strncasecmp(s, "color", 5) == 0) {
276 *col = strtol (s, &eptr, 10);
277 if (!*s || *eptr || *col < 0 ||
278 (*col >= COLORS && !option (OPTNOCURSES) && has_colors ())) {
279 snprintf (err->data, err->dsize, _("%s: color not supported by term"),
284 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1) {
285 snprintf (err->data, err->dsize, _("%s: no such color"), s);
292 /* usage: uncolor index pattern [pattern...]
293 * unmono index pattern [pattern...]
297 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
298 BUFFER * err, short parse_uncolor);
300 int mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
303 return _mutt_parse_uncolor (buf, s, data, err, 1);
306 int mutt_parse_unmono (BUFFER * buf, BUFFER * s, unsigned long data,
309 return _mutt_parse_uncolor (buf, s, data, err, 0);
313 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
314 BUFFER * err, short parse_uncolor)
316 int object = 0, do_cache = 0;
317 COLOR_LINE *tmp, *last = NULL;
319 mutt_extract_token (buf, s, 0);
321 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1) {
322 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
326 if (m_strncmp(buf->data, "index", 5) != 0) {
327 snprintf (err->data, err->dsize,
328 _("%s: command valid only for index object"),
329 parse_uncolor ? "uncolor" : "unmono");
334 snprintf (err->data, err->dsize,
335 _("%s: too few arguments"),
336 parse_uncolor ? "uncolor" : "unmono");
340 if (option (OPTNOCURSES) || (parse_uncolor != has_colors())) {
341 /* just eat the command, but don't do anything real about it */
343 mutt_extract_token (buf, s, 0);
344 } while (MoreArgs(s));
349 mutt_extract_token (buf, s, 0);
350 if (!m_strcmp("*", buf->data)) {
351 for (tmp = ColorIndexList; tmp;) {
356 mutt_free_color_line (&last, parse_uncolor);
358 ColorIndexList = NULL;
361 for (last = NULL, tmp = ColorIndexList; tmp;
362 last = tmp, tmp = tmp->next) {
363 if (!m_strcmp(buf->data, tmp->pattern)) {
367 last->next = tmp->next;
369 ColorIndexList = tmp->next;
370 mutt_free_color_line (&tmp, parse_uncolor);
376 while (MoreArgs (s));
379 if (do_cache && !option (OPTNOCURSES)) {
382 set_option (OPTFORCEREDRAWINDEX);
383 /* force re-caching of index colors */
384 for (i = 0; Context && i < Context->msgcount; i++)
385 Context->hdrs[i]->pair = 0;
392 add_pattern (COLOR_LINE ** top, const char *s, int sensitive,
393 int fg, int bg, int attr, BUFFER * err, int is_index)
396 /* is_index used to store compiled pattern
397 * only for `index' color object
398 * when called from mutt_parse_color() */
400 COLOR_LINE *tmp = *top;
404 if (m_strcmp(s, tmp->pattern) == 0)
408 if (m_strcasecmp(s, tmp->pattern) == 0)
415 if (fg != -1 && bg != -1) {
416 if (tmp->fg != fg || tmp->bg != bg) {
417 mutt_free_color (tmp->fg, tmp->bg);
420 attr |= mutt_alloc_color (fg, bg);
423 attr |= (tmp->pair & ~A_BOLD);
430 tmp = mutt_new_color_line ();
434 m_strcpy(buf, sizeof(buf), NONULL(s));
435 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
436 if ((tmp->color_pattern =
437 mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL) {
438 mutt_free_color_line (&tmp, 1);
441 /* force re-caching of index colors */
442 for (i = 0; Context && i < Context->msgcount; i++)
443 Context->hdrs[i]->pair = 0;
447 REGCOMP (&tmp->rx, s,
448 (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0) {
449 regerror (r, &tmp->rx, err->data, err->dsize);
450 mutt_free_color_line (&tmp, 1);
454 tmp->pattern = m_strdup(s);
455 if (fg != -1 && bg != -1) {
458 attr |= mutt_alloc_color (fg, bg);
468 parse_object (BUFFER * buf, BUFFER * s, int *o, int *ql, BUFFER * err)
474 m_strcpy(err->data, err->dsize, _("Missing arguments."));
478 mutt_extract_token (buf, s, 0);
479 if (!m_strncmp(buf->data, "quoted", 6)) {
481 *ql = strtol (buf->data + 6, &eptr, 10);
482 if (*eptr || q_level < 0) {
483 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
490 *o = MT_COLOR_QUOTED;
492 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1) {
493 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
500 typedef int (*parser_callback_t) (BUFFER *, BUFFER *, int *, int *, int *,
504 parse_color_pair (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
508 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
512 mutt_extract_token (buf, s, 0);
514 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
518 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
522 mutt_extract_token (buf, s, 0);
524 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
531 parse_attr_spec (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
541 m_strcpy(err->data, err->dsize, _("mono: too few arguments"));
545 mutt_extract_token (buf, s, 0);
547 if (ascii_strcasecmp ("bold", buf->data) == 0)
549 else if (ascii_strcasecmp ("underline", buf->data) == 0)
550 *attr |= A_UNDERLINE;
551 else if (ascii_strcasecmp ("none", buf->data) == 0)
553 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
555 else if (ascii_strcasecmp ("standout", buf->data) == 0)
557 else if (ascii_strcasecmp ("normal", buf->data) == 0)
558 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
560 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
567 static int fgbgattr_to_color (int fg, int bg, int attr)
569 if (fg != -1 && bg != -1)
570 return attr | mutt_alloc_color (fg, bg);
575 /* usage: color <object> <fg> <bg> [ <regexp> ]
576 * mono <object> <attr> [ <regexp> ]
580 _mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
581 parser_callback_t callback, short dry_run)
583 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
586 if (parse_object (buf, s, &object, &q_level, err) == -1)
589 if (callback (buf, s, &fg, &bg, &attr, err) == -1)
592 /* extract a regular expression if needed */
594 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY
595 || object == MT_COLOR_INDEX) {
597 m_strcpy(err->data, err->dsize, _("too few arguments"));
601 mutt_extract_token (buf, s, 0);
605 m_strcpy(err->data, err->dsize, _("too many arguments"));
615 #ifdef HAVE_USE_DEFAULT_COLORS
616 if (!option (OPTNOCURSES) && has_colors ()
617 /* delay use_default_colors() until needed, since it initializes things */
618 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
619 && use_default_colors () != OK) {
620 m_strcpy(err->data, err->dsize, _("default colors not supported"));
623 #endif /* HAVE_USE_DEFAULT_COLORS */
625 if (object == MT_COLOR_HEADER)
626 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err, 0);
627 else if (object == MT_COLOR_BODY)
628 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
629 else if (object == MT_COLOR_INDEX) {
630 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
631 set_option (OPTFORCEREDRAWINDEX);
633 else if (object == MT_COLOR_QUOTED) {
634 if (q_level >= ColorQuoteSize) {
635 p_realloc(&ColorQuote, ColorQuoteSize += 2);
636 ColorQuote[ColorQuoteSize - 2] = ColorDefs[MT_COLOR_QUOTED];
637 ColorQuote[ColorQuoteSize - 1] = ColorDefs[MT_COLOR_QUOTED];
639 if (q_level >= ColorQuoteUsed)
640 ColorQuoteUsed = q_level + 1;
642 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color (fg, bg, attr);
644 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
645 for (q_level = 1; q_level < ColorQuoteUsed; q_level++) {
646 if (ColorQuote[q_level] == A_NORMAL)
647 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
651 ColorQuote[q_level] = fgbgattr_to_color (fg, bg, attr);
654 ColorDefs[object] = fgbgattr_to_color (fg, bg, attr);
657 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors ())
658 BKGDSET (MT_COLOR_NORMAL);
664 int mutt_parse_color (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
669 if (option (OPTNOCURSES) || !has_colors ())
672 return _mutt_parse_color (buff, s, err, parse_color_pair, dry_run);
675 int mutt_parse_mono (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
680 if (option (OPTNOCURSES) || has_colors ())
683 return _mutt_parse_color (buff, s, err, parse_attr_spec, dry_run);