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.
15 #include "mutt_curses.h"
21 #include "lib/debug.h"
30 int ColorDefs[MT_COLOR_MAX];
31 COLOR_LINE *ColorHdrList = NULL;
32 COLOR_LINE *ColorBodyList = NULL;
33 COLOR_LINE *ColorIndexList = NULL;
35 /* local to this file */
36 static int ColorQuoteSize;
40 #define COLOR_DEFAULT (-2)
42 typedef struct color_list {
47 struct color_list *next;
50 static COLOR_LIST *ColorList = NULL;
51 static int UserColors = 0;
53 static struct mapping_t Colors[] = {
54 {"black", COLOR_BLACK},
57 {"green", COLOR_GREEN},
58 {"magenta", COLOR_MAGENTA},
60 {"white", COLOR_WHITE},
61 {"yellow", COLOR_YELLOW},
62 #if defined (USE_SLANG_CURSES) || defined (HAVE_USE_DEFAULT_COLORS)
63 {"default", COLOR_DEFAULT},
68 #endif /* HAVE_COLOR */
70 static struct mapping_t Fields[] = {
71 {"hdrdefault", MT_COLOR_HDEFAULT},
72 {"quoted", MT_COLOR_QUOTED},
73 {"signature", MT_COLOR_SIGNATURE},
74 {"indicator", MT_COLOR_INDICATOR},
75 {"status", MT_COLOR_STATUS},
76 {"tree", MT_COLOR_TREE},
77 {"error", MT_COLOR_ERROR},
78 {"normal", MT_COLOR_NORMAL},
79 {"tilde", MT_COLOR_TILDE},
80 {"markers", MT_COLOR_MARKERS},
81 {"header", MT_COLOR_HEADER},
82 {"body", MT_COLOR_BODY},
83 {"message", MT_COLOR_MESSAGE},
84 {"attachment", MT_COLOR_ATTACHMENT},
85 {"search", MT_COLOR_SEARCH},
86 {"bold", MT_COLOR_BOLD},
87 {"underline", MT_COLOR_UNDERLINE},
88 {"index", MT_COLOR_INDEX},
89 {"sidebar_new", MT_COLOR_NEW},
90 {"sidebar", MT_COLOR_SIDEBAR},
91 {"sidebar_flagged", MT_COLOR_FLAGGED},
95 #define COLOR_QUOTE_INIT 8
97 static COLOR_LINE *mutt_new_color_line (void)
99 COLOR_LINE *p = safe_calloc (1, sizeof (COLOR_LINE));
106 static void mutt_free_color_line (COLOR_LINE ** l, int free_colors)
116 if (free_colors && tmp->fg != -1 && tmp->bg != -1)
117 mutt_free_color (tmp->fg, tmp->bg);
120 /* we should really introduce a container
121 * type for regular expressions.
125 mutt_pattern_free (&tmp->color_pattern);
126 FREE (&tmp->pattern);
130 void ci_start_color (void)
132 memset (ColorDefs, A_NORMAL, sizeof (int) * MT_COLOR_MAX);
133 ColorQuote = (int *) safe_malloc (COLOR_QUOTE_INIT * sizeof (int));
134 memset (ColorQuote, A_NORMAL, sizeof (int) * COLOR_QUOTE_INIT);
135 ColorQuoteSize = COLOR_QUOTE_INIT;
138 /* set some defaults */
139 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
140 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
141 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
142 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
143 /* special meaning: toggle the relevant attribute */
144 ColorDefs[MT_COLOR_BOLD] = 0;
145 ColorDefs[MT_COLOR_UNDERLINE] = 0;
154 #ifdef USE_SLANG_CURSES
155 static char *get_color_name (char *dest, size_t destlen, int val)
157 static char *missing[3] = { "brown", "lightgray", "default" };
162 strfcpy (dest, missing[0], destlen);
166 strfcpy (dest, missing[1], destlen);
170 strfcpy (dest, missing[2], destlen);
174 for (i = 0; Colors[i].name; i++) {
175 if (Colors[i].value == val) {
176 strfcpy (dest, Colors[i].name, destlen);
181 /* Sigh. If we got this far, the color is of the form 'colorN'
182 * Slang can handle this itself, so just return 'colorN'
185 snprintf (dest, destlen, "color%d", val);
190 int mutt_alloc_color (int fg, int bg)
192 COLOR_LIST *p = ColorList;
195 #if defined (USE_SLANG_CURSES)
196 char fgc[SHORT_STRING], bgc[SHORT_STRING];
199 /* check to see if this color is already allocated to save space */
201 if (p->fg == fg && p->bg == bg) {
203 return (COLOR_PAIR (p->index));
208 /* check to see if there are colors left */
209 if (++UserColors > COLOR_PAIRS)
212 /* find the smallest available index (object) */
226 p = (COLOR_LIST *) safe_malloc (sizeof (COLOR_LIST));
235 #if defined (USE_SLANG_CURSES)
236 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
237 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg),
238 get_color_name (bgc, sizeof (bgc), bg));
240 #elif defined (HAVE_USE_DEFAULT_COLORS)
241 if (fg == COLOR_DEFAULT)
243 if (bg == COLOR_DEFAULT)
247 init_pair (i, fg, bg);
249 debug_print (1, ("Color pairs used so far: %d\n", UserColors));
251 return (COLOR_PAIR (p->index));
254 void mutt_free_color (int fg, int bg)
260 if (p->fg == fg && p->bg == bg) {
266 debug_print (1, ("Color pairs used so far: %d\n", UserColors));
268 if (p == ColorList) {
269 ColorList = ColorList->next;
288 #endif /* HAVE_COLOR */
294 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER * err)
298 if (safe_strncasecmp (s, "bright", 6) == 0) {
303 /* allow aliases for xterm color resources */
304 if (safe_strncasecmp (s, "color", 5) == 0) {
306 *col = strtol (s, &eptr, 10);
307 if (!*s || *eptr || *col < 0 ||
308 (*col >= COLORS && !option (OPTNOCURSES) && has_colors ())) {
309 snprintf (err->data, err->dsize, _("%s: color not supported by term"),
314 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1) {
315 snprintf (err->data, err->dsize, _("%s: no such color"), s);
325 /* usage: uncolor index pattern [pattern...]
326 * unmono index pattern [pattern...]
330 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
331 BUFFER * err, short parse_uncolor);
336 int mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
339 return _mutt_parse_uncolor (buf, s, data, err, 1);
344 int mutt_parse_unmono (BUFFER * buf, BUFFER * s, unsigned long data,
347 return _mutt_parse_uncolor (buf, s, data, err, 0);
351 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
352 BUFFER * err, short parse_uncolor)
354 int object = 0, do_cache = 0;
355 COLOR_LINE *tmp, *last = NULL;
357 mutt_extract_token (buf, s, 0);
359 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1) {
360 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
364 if (safe_strncmp (buf->data, "index", 5) != 0) {
365 snprintf (err->data, err->dsize,
366 _("%s: command valid only for index object"),
367 parse_uncolor ? "uncolor" : "unmono");
372 snprintf (err->data, err->dsize,
373 _("%s: too few arguments"),
374 parse_uncolor ? "uncolor" : "unmono");
380 /* we're running without curses */
382 || /* we're parsing an uncolor command, and have no colors */
383 (parse_uncolor && !has_colors ())
384 /* we're parsing an unmono command, and have colors */
385 || (!parse_uncolor && has_colors ())
387 /* We don't even have colors compiled in */
391 /* just eat the command, but don't do anything real about it */
393 mutt_extract_token (buf, s, 0);
394 while (MoreArgs (s));
401 mutt_extract_token (buf, s, 0);
402 if (!safe_strcmp ("*", buf->data)) {
403 for (tmp = ColorIndexList; tmp;) {
408 mutt_free_color_line (&last, parse_uncolor);
410 ColorIndexList = NULL;
413 for (last = NULL, tmp = ColorIndexList; tmp;
414 last = tmp, tmp = tmp->next) {
415 if (!safe_strcmp (buf->data, tmp->pattern)) {
418 debug_print (1, ("Freeing pattern \"%s\" from ColorIndexList\n", tmp->pattern));
420 last->next = tmp->next;
422 ColorIndexList = tmp->next;
423 mutt_free_color_line (&tmp, parse_uncolor);
429 while (MoreArgs (s));
432 if (do_cache && !option (OPTNOCURSES)) {
435 set_option (OPTFORCEREDRAWINDEX);
436 /* force re-caching of index colors */
437 for (i = 0; Context && i < Context->msgcount; i++)
438 Context->hdrs[i]->pair = 0;
445 add_pattern (COLOR_LINE ** top, const char *s, int sensitive,
446 int fg, int bg, int attr, BUFFER * err, int is_index)
449 /* is_index used to store compiled pattern
450 * only for `index' color object
451 * when called from mutt_parse_color() */
453 COLOR_LINE *tmp = *top;
457 if (safe_strcmp (s, tmp->pattern) == 0)
461 if (safe_strcasecmp (s, tmp->pattern) == 0)
469 if (fg != -1 && bg != -1) {
470 if (tmp->fg != fg || tmp->bg != bg) {
471 mutt_free_color (tmp->fg, tmp->bg);
474 attr |= mutt_alloc_color (fg, bg);
477 attr |= (tmp->pair & ~A_BOLD);
479 #endif /* HAVE_COLOR */
486 tmp = mutt_new_color_line ();
490 strfcpy (buf, NONULL (s), sizeof (buf));
491 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
492 if ((tmp->color_pattern =
493 mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL) {
494 mutt_free_color_line (&tmp, 1);
497 /* force re-caching of index colors */
498 for (i = 0; Context && i < Context->msgcount; i++)
499 Context->hdrs[i]->pair = 0;
503 REGCOMP (&tmp->rx, s,
504 (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0) {
505 regerror (r, &tmp->rx, err->data, err->dsize);
506 mutt_free_color_line (&tmp, 1);
510 tmp->pattern = safe_strdup (s);
512 if (fg != -1 && bg != -1) {
515 attr |= mutt_alloc_color (fg, bg);
526 parse_object (BUFFER * buf, BUFFER * s, int *o, int *ql, BUFFER * err)
532 strfcpy (err->data, _("Missing arguments."), err->dsize);
536 mutt_extract_token (buf, s, 0);
537 if (!safe_strncmp (buf->data, "quoted", 6)) {
539 *ql = strtol (buf->data + 6, &eptr, 10);
540 if (*eptr || q_level < 0) {
541 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
548 *o = MT_COLOR_QUOTED;
550 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1) {
551 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
558 typedef int (*parser_callback_t) (BUFFER *, BUFFER *, int *, int *, int *,
564 parse_color_pair (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
568 strfcpy (err->data, _("color: too few arguments"), err->dsize);
572 mutt_extract_token (buf, s, 0);
574 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
578 strfcpy (err->data, _("color: too few arguments"), err->dsize);
582 mutt_extract_token (buf, s, 0);
584 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
593 parse_attr_spec (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
603 strfcpy (err->data, _("mono: too few arguments"), err->dsize);
607 mutt_extract_token (buf, s, 0);
609 if (ascii_strcasecmp ("bold", buf->data) == 0)
611 else if (ascii_strcasecmp ("underline", buf->data) == 0)
612 *attr |= A_UNDERLINE;
613 else if (ascii_strcasecmp ("none", buf->data) == 0)
615 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
617 else if (ascii_strcasecmp ("standout", buf->data) == 0)
619 else if (ascii_strcasecmp ("normal", buf->data) == 0)
620 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
622 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
629 static int fgbgattr_to_color (int fg, int bg, int attr)
632 if (fg != -1 && bg != -1)
633 return attr | mutt_alloc_color (fg, bg);
639 /* usage: color <object> <fg> <bg> [ <regexp> ]
640 * mono <object> <attr> [ <regexp> ]
644 _mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
645 parser_callback_t callback, short dry_run)
647 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
650 if (parse_object (buf, s, &object, &q_level, err) == -1)
653 if (callback (buf, s, &fg, &bg, &attr, err) == -1)
656 /* extract a regular expression if needed */
658 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY
659 || object == MT_COLOR_INDEX) {
661 strfcpy (err->data, _("too few arguments"), err->dsize);
665 mutt_extract_token (buf, s, 0);
669 strfcpy (err->data, _("too many arguments"), err->dsize);
680 # ifdef HAVE_USE_DEFAULT_COLORS
681 if (!option (OPTNOCURSES) && has_colors ()
682 /* delay use_default_colors() until needed, since it initializes things */
683 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
684 && use_default_colors () != OK) {
685 strfcpy (err->data, _("default colors not supported"), err->dsize);
688 # endif /* HAVE_USE_DEFAULT_COLORS */
691 if (object == MT_COLOR_HEADER)
692 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err, 0);
693 else if (object == MT_COLOR_BODY)
694 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
695 else if (object == MT_COLOR_INDEX) {
696 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
697 set_option (OPTFORCEREDRAWINDEX);
699 else if (object == MT_COLOR_QUOTED) {
700 if (q_level >= ColorQuoteSize) {
701 safe_realloc (&ColorQuote, (ColorQuoteSize += 2) * sizeof (int));
702 ColorQuote[ColorQuoteSize - 2] = ColorDefs[MT_COLOR_QUOTED];
703 ColorQuote[ColorQuoteSize - 1] = ColorDefs[MT_COLOR_QUOTED];
705 if (q_level >= ColorQuoteUsed)
706 ColorQuoteUsed = q_level + 1;
708 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color (fg, bg, attr);
710 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
711 for (q_level = 1; q_level < ColorQuoteUsed; q_level++) {
712 if (ColorQuote[q_level] == A_NORMAL)
713 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
717 ColorQuote[q_level] = fgbgattr_to_color (fg, bg, attr);
720 ColorDefs[object] = fgbgattr_to_color (fg, bg, attr);
724 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors ())
725 BKGDSET (MT_COLOR_NORMAL);
734 int mutt_parse_color (BUFFER * buff, BUFFER * s, unsigned long data,
739 if (option (OPTNOCURSES) || !has_colors ())
742 return _mutt_parse_color (buff, s, err, parse_color_pair, dry_run);
747 int mutt_parse_mono (BUFFER * buff, BUFFER * s, unsigned long data,
753 if (option (OPTNOCURSES) || has_colors ())
756 if (option (OPTNOCURSES))
760 return _mutt_parse_color (buff, s, err, parse_attr_spec, dry_run);