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"
25 int ColorDefs[MT_COLOR_MAX];
26 COLOR_LINE *ColorHdrList = NULL;
27 COLOR_LINE *ColorBodyList = NULL;
28 COLOR_LINE *ColorIndexList = NULL;
30 /* local to this file */
31 static int ColorQuoteSize;
35 #define COLOR_DEFAULT (-2)
37 typedef struct color_list {
42 struct color_list *next;
45 static COLOR_LIST *ColorList = NULL;
46 static int UserColors = 0;
48 static struct mapping_t Colors[] = {
49 {"black", COLOR_BLACK},
52 {"green", COLOR_GREEN},
53 {"magenta", COLOR_MAGENTA},
55 {"white", COLOR_WHITE},
56 {"yellow", COLOR_YELLOW},
57 #if defined (USE_SLANG_CURSES) || defined (HAVE_USE_DEFAULT_COLORS)
58 {"default", COLOR_DEFAULT},
63 #endif /* HAVE_COLOR */
65 static struct mapping_t Fields[] = {
66 {"hdrdefault", MT_COLOR_HDEFAULT},
67 {"quoted", MT_COLOR_QUOTED},
68 {"signature", MT_COLOR_SIGNATURE},
69 {"indicator", MT_COLOR_INDICATOR},
70 {"status", MT_COLOR_STATUS},
71 {"tree", MT_COLOR_TREE},
72 {"error", MT_COLOR_ERROR},
73 {"normal", MT_COLOR_NORMAL},
74 {"tilde", MT_COLOR_TILDE},
75 {"markers", MT_COLOR_MARKERS},
76 {"header", MT_COLOR_HEADER},
77 {"body", MT_COLOR_BODY},
78 {"message", MT_COLOR_MESSAGE},
79 {"attachment", MT_COLOR_ATTACHMENT},
80 {"search", MT_COLOR_SEARCH},
81 {"bold", MT_COLOR_BOLD},
82 {"underline", MT_COLOR_UNDERLINE},
83 {"index", MT_COLOR_INDEX},
84 {"sidebar_new", MT_COLOR_NEW},
85 {"sidebar", MT_COLOR_SIDEBAR},
86 {"sidebar_flagged", MT_COLOR_FLAGGED},
90 #define COLOR_QUOTE_INIT 8
92 static COLOR_LINE *mutt_new_color_line (void)
94 COLOR_LINE *p = safe_calloc (1, sizeof (COLOR_LINE));
101 static void mutt_free_color_line (COLOR_LINE ** l, int free_colors)
111 if (free_colors && tmp->fg != -1 && tmp->bg != -1)
112 mutt_free_color (tmp->fg, tmp->bg);
115 /* we should really introduce a container
116 * type for regular expressions.
120 mutt_pattern_free (&tmp->color_pattern);
121 FREE (&tmp->pattern);
125 void ci_start_color (void)
127 memset (ColorDefs, A_NORMAL, sizeof (int) * MT_COLOR_MAX);
128 ColorQuote = (int *) safe_malloc (COLOR_QUOTE_INIT * sizeof (int));
129 memset (ColorQuote, A_NORMAL, sizeof (int) * COLOR_QUOTE_INIT);
130 ColorQuoteSize = COLOR_QUOTE_INIT;
133 /* set some defaults */
134 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
135 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
136 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
137 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
138 /* special meaning: toggle the relevant attribute */
139 ColorDefs[MT_COLOR_BOLD] = 0;
140 ColorDefs[MT_COLOR_UNDERLINE] = 0;
149 #ifdef USE_SLANG_CURSES
150 static char *get_color_name (char *dest, size_t destlen, int val)
152 static char *missing[3] = { "brown", "lightgray", "default" };
157 strfcpy (dest, missing[0], destlen);
161 strfcpy (dest, missing[1], destlen);
165 strfcpy (dest, missing[2], destlen);
169 for (i = 0; Colors[i].name; i++) {
170 if (Colors[i].value == val) {
171 strfcpy (dest, Colors[i].name, destlen);
176 /* Sigh. If we got this far, the color is of the form 'colorN'
177 * Slang can handle this itself, so just return 'colorN'
180 snprintf (dest, destlen, "color%d", val);
185 int mutt_alloc_color (int fg, int bg)
187 COLOR_LIST *p = ColorList;
190 #if defined (USE_SLANG_CURSES)
191 char fgc[SHORT_STRING], bgc[SHORT_STRING];
194 /* check to see if this color is already allocated to save space */
196 if (p->fg == fg && p->bg == bg) {
198 return (COLOR_PAIR (p->index));
203 /* check to see if there are colors left */
204 if (++UserColors > COLOR_PAIRS)
207 /* find the smallest available index (object) */
221 p = (COLOR_LIST *) safe_malloc (sizeof (COLOR_LIST));
230 #if defined (USE_SLANG_CURSES)
231 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
232 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg),
233 get_color_name (bgc, sizeof (bgc), bg));
235 #elif defined (HAVE_USE_DEFAULT_COLORS)
236 if (fg == COLOR_DEFAULT)
238 if (bg == COLOR_DEFAULT)
242 init_pair (i, fg, bg);
244 dprint (1, (debugfile, "mutt_alloc_color(): Color pairs used so far: %d\n",
247 return (COLOR_PAIR (p->index));
250 void mutt_free_color (int fg, int bg)
256 if (p->fg == fg && p->bg == bg) {
263 (debugfile, "mutt_free_color(): Color pairs used so far: %d\n",
266 if (p == ColorList) {
267 ColorList = ColorList->next;
286 #endif /* HAVE_COLOR */
292 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER * err)
296 if (mutt_strncasecmp (s, "bright", 6) == 0) {
301 /* allow aliases for xterm color resources */
302 if (mutt_strncasecmp (s, "color", 5) == 0) {
304 *col = strtol (s, &eptr, 10);
305 if (!*s || *eptr || *col < 0 ||
306 (*col >= COLORS && !option (OPTNOCURSES) && has_colors ())) {
307 snprintf (err->data, err->dsize, _("%s: color not supported by term"),
312 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1) {
313 snprintf (err->data, err->dsize, _("%s: no such color"), s);
323 /* usage: uncolor index pattern [pattern...]
324 * unmono index pattern [pattern...]
328 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
329 BUFFER * err, short parse_uncolor);
334 int mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
337 return _mutt_parse_uncolor (buf, s, data, err, 1);
342 int mutt_parse_unmono (BUFFER * buf, BUFFER * s, unsigned long data,
345 return _mutt_parse_uncolor (buf, s, data, err, 0);
349 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
350 BUFFER * err, short parse_uncolor)
352 int object = 0, do_cache = 0;
353 COLOR_LINE *tmp, *last = NULL;
355 mutt_extract_token (buf, s, 0);
357 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1) {
358 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
362 if (mutt_strncmp (buf->data, "index", 5) != 0) {
363 snprintf (err->data, err->dsize,
364 _("%s: command valid only for index object"),
365 parse_uncolor ? "uncolor" : "unmono");
370 snprintf (err->data, err->dsize,
371 _("%s: too few arguments"),
372 parse_uncolor ? "uncolor" : "unmono");
378 /* we're running without curses */
380 || /* we're parsing an uncolor command, and have no colors */
381 (parse_uncolor && !has_colors ())
382 /* we're parsing an unmono command, and have colors */
383 || (!parse_uncolor && has_colors ())
385 /* We don't even have colors compiled in */
389 /* just eat the command, but don't do anything real about it */
391 mutt_extract_token (buf, s, 0);
392 while (MoreArgs (s));
399 mutt_extract_token (buf, s, 0);
400 if (!mutt_strcmp ("*", buf->data)) {
401 for (tmp = ColorIndexList; tmp;) {
406 mutt_free_color_line (&last, parse_uncolor);
408 ColorIndexList = NULL;
411 for (last = NULL, tmp = ColorIndexList; tmp;
412 last = tmp, tmp = tmp->next) {
413 if (!mutt_strcmp (buf->data, tmp->pattern)) {
417 (debugfile, "Freeing pattern \"%s\" from ColorIndexList\n",
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 (mutt_strcmp (s, tmp->pattern) == 0)
461 if (mutt_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 (!mutt_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);