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"
29 int ColorDefs[MT_COLOR_MAX];
30 COLOR_LINE *ColorHdrList = NULL;
31 COLOR_LINE *ColorBodyList = NULL;
32 COLOR_LINE *ColorIndexList = NULL;
34 /* local to this file */
35 static int ColorQuoteSize;
39 #define COLOR_DEFAULT (-2)
41 typedef struct color_list {
46 struct color_list *next;
49 static COLOR_LIST *ColorList = NULL;
50 static int UserColors = 0;
52 static struct mapping_t Colors[] = {
53 {"black", COLOR_BLACK},
56 {"green", COLOR_GREEN},
57 {"magenta", COLOR_MAGENTA},
59 {"white", COLOR_WHITE},
60 {"yellow", COLOR_YELLOW},
61 #if defined (USE_SLANG_CURSES) || defined (HAVE_USE_DEFAULT_COLORS)
62 {"default", COLOR_DEFAULT},
67 #endif /* HAVE_COLOR */
69 static struct mapping_t Fields[] = {
70 {"hdrdefault", MT_COLOR_HDEFAULT},
71 {"quoted", MT_COLOR_QUOTED},
72 {"signature", MT_COLOR_SIGNATURE},
73 {"indicator", MT_COLOR_INDICATOR},
74 {"status", MT_COLOR_STATUS},
75 {"tree", MT_COLOR_TREE},
76 {"error", MT_COLOR_ERROR},
77 {"normal", MT_COLOR_NORMAL},
78 {"tilde", MT_COLOR_TILDE},
79 {"markers", MT_COLOR_MARKERS},
80 {"header", MT_COLOR_HEADER},
81 {"body", MT_COLOR_BODY},
82 {"message", MT_COLOR_MESSAGE},
83 {"attachment", MT_COLOR_ATTACHMENT},
84 {"search", MT_COLOR_SEARCH},
85 {"bold", MT_COLOR_BOLD},
86 {"underline", MT_COLOR_UNDERLINE},
87 {"index", MT_COLOR_INDEX},
88 {"sidebar_new", MT_COLOR_NEW},
89 {"sidebar", MT_COLOR_SIDEBAR},
90 {"sidebar_flagged", MT_COLOR_FLAGGED},
94 #define COLOR_QUOTE_INIT 8
96 static COLOR_LINE *mutt_new_color_line (void)
98 COLOR_LINE *p = safe_calloc (1, sizeof (COLOR_LINE));
105 static void mutt_free_color_line (COLOR_LINE ** l, int free_colors)
115 if (free_colors && tmp->fg != -1 && tmp->bg != -1)
116 mutt_free_color (tmp->fg, tmp->bg);
119 /* we should really introduce a container
120 * type for regular expressions.
124 mutt_pattern_free (&tmp->color_pattern);
125 FREE (&tmp->pattern);
129 void ci_start_color (void)
131 memset (ColorDefs, A_NORMAL, sizeof (int) * MT_COLOR_MAX);
132 ColorQuote = (int *) safe_malloc (COLOR_QUOTE_INIT * sizeof (int));
133 memset (ColorQuote, A_NORMAL, sizeof (int) * COLOR_QUOTE_INIT);
134 ColorQuoteSize = COLOR_QUOTE_INIT;
137 /* set some defaults */
138 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
139 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
140 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
141 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
142 /* special meaning: toggle the relevant attribute */
143 ColorDefs[MT_COLOR_BOLD] = 0;
144 ColorDefs[MT_COLOR_UNDERLINE] = 0;
153 #ifdef USE_SLANG_CURSES
154 static char *get_color_name (char *dest, size_t destlen, int val)
156 static char *missing[3] = { "brown", "lightgray", "default" };
161 strfcpy (dest, missing[0], destlen);
165 strfcpy (dest, missing[1], destlen);
169 strfcpy (dest, missing[2], destlen);
173 for (i = 0; Colors[i].name; i++) {
174 if (Colors[i].value == val) {
175 strfcpy (dest, Colors[i].name, destlen);
180 /* Sigh. If we got this far, the color is of the form 'colorN'
181 * Slang can handle this itself, so just return 'colorN'
184 snprintf (dest, destlen, "color%d", val);
189 int mutt_alloc_color (int fg, int bg)
191 COLOR_LIST *p = ColorList;
194 #if defined (USE_SLANG_CURSES)
195 char fgc[SHORT_STRING], bgc[SHORT_STRING];
198 /* check to see if this color is already allocated to save space */
200 if (p->fg == fg && p->bg == bg) {
202 return (COLOR_PAIR (p->index));
207 /* check to see if there are colors left */
208 if (++UserColors > COLOR_PAIRS)
211 /* find the smallest available index (object) */
225 p = (COLOR_LIST *) safe_malloc (sizeof (COLOR_LIST));
234 #if defined (USE_SLANG_CURSES)
235 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
236 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg),
237 get_color_name (bgc, sizeof (bgc), bg));
239 #elif defined (HAVE_USE_DEFAULT_COLORS)
240 if (fg == COLOR_DEFAULT)
242 if (bg == COLOR_DEFAULT)
246 init_pair (i, fg, bg);
248 dprint (1, (debugfile, "mutt_alloc_color(): Color pairs used so far: %d\n",
251 return (COLOR_PAIR (p->index));
254 void mutt_free_color (int fg, int bg)
260 if (p->fg == fg && p->bg == bg) {
267 (debugfile, "mutt_free_color(): Color pairs used so far: %d\n",
270 if (p == ColorList) {
271 ColorList = ColorList->next;
290 #endif /* HAVE_COLOR */
296 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER * err)
300 if (mutt_strncasecmp (s, "bright", 6) == 0) {
305 /* allow aliases for xterm color resources */
306 if (mutt_strncasecmp (s, "color", 5) == 0) {
308 *col = strtol (s, &eptr, 10);
309 if (!*s || *eptr || *col < 0 ||
310 (*col >= COLORS && !option (OPTNOCURSES) && has_colors ())) {
311 snprintf (err->data, err->dsize, _("%s: color not supported by term"),
316 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1) {
317 snprintf (err->data, err->dsize, _("%s: no such color"), s);
327 /* usage: uncolor index pattern [pattern...]
328 * unmono index pattern [pattern...]
332 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
333 BUFFER * err, short parse_uncolor);
338 int mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
341 return _mutt_parse_uncolor (buf, s, data, err, 1);
346 int mutt_parse_unmono (BUFFER * buf, BUFFER * s, unsigned long data,
349 return _mutt_parse_uncolor (buf, s, data, err, 0);
353 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
354 BUFFER * err, short parse_uncolor)
356 int object = 0, do_cache = 0;
357 COLOR_LINE *tmp, *last = NULL;
359 mutt_extract_token (buf, s, 0);
361 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1) {
362 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
366 if (mutt_strncmp (buf->data, "index", 5) != 0) {
367 snprintf (err->data, err->dsize,
368 _("%s: command valid only for index object"),
369 parse_uncolor ? "uncolor" : "unmono");
374 snprintf (err->data, err->dsize,
375 _("%s: too few arguments"),
376 parse_uncolor ? "uncolor" : "unmono");
382 /* we're running without curses */
384 || /* we're parsing an uncolor command, and have no colors */
385 (parse_uncolor && !has_colors ())
386 /* we're parsing an unmono command, and have colors */
387 || (!parse_uncolor && has_colors ())
389 /* We don't even have colors compiled in */
393 /* just eat the command, but don't do anything real about it */
395 mutt_extract_token (buf, s, 0);
396 while (MoreArgs (s));
403 mutt_extract_token (buf, s, 0);
404 if (!mutt_strcmp ("*", buf->data)) {
405 for (tmp = ColorIndexList; tmp;) {
410 mutt_free_color_line (&last, parse_uncolor);
412 ColorIndexList = NULL;
415 for (last = NULL, tmp = ColorIndexList; tmp;
416 last = tmp, tmp = tmp->next) {
417 if (!mutt_strcmp (buf->data, tmp->pattern)) {
421 (debugfile, "Freeing pattern \"%s\" from ColorIndexList\n",
424 last->next = tmp->next;
426 ColorIndexList = tmp->next;
427 mutt_free_color_line (&tmp, parse_uncolor);
433 while (MoreArgs (s));
436 if (do_cache && !option (OPTNOCURSES)) {
439 set_option (OPTFORCEREDRAWINDEX);
440 /* force re-caching of index colors */
441 for (i = 0; Context && i < Context->msgcount; i++)
442 Context->hdrs[i]->pair = 0;
449 add_pattern (COLOR_LINE ** top, const char *s, int sensitive,
450 int fg, int bg, int attr, BUFFER * err, int is_index)
453 /* is_index used to store compiled pattern
454 * only for `index' color object
455 * when called from mutt_parse_color() */
457 COLOR_LINE *tmp = *top;
461 if (mutt_strcmp (s, tmp->pattern) == 0)
465 if (mutt_strcasecmp (s, tmp->pattern) == 0)
473 if (fg != -1 && bg != -1) {
474 if (tmp->fg != fg || tmp->bg != bg) {
475 mutt_free_color (tmp->fg, tmp->bg);
478 attr |= mutt_alloc_color (fg, bg);
481 attr |= (tmp->pair & ~A_BOLD);
483 #endif /* HAVE_COLOR */
490 tmp = mutt_new_color_line ();
494 strfcpy (buf, NONULL (s), sizeof (buf));
495 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
496 if ((tmp->color_pattern =
497 mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL) {
498 mutt_free_color_line (&tmp, 1);
501 /* force re-caching of index colors */
502 for (i = 0; Context && i < Context->msgcount; i++)
503 Context->hdrs[i]->pair = 0;
507 REGCOMP (&tmp->rx, s,
508 (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0) {
509 regerror (r, &tmp->rx, err->data, err->dsize);
510 mutt_free_color_line (&tmp, 1);
514 tmp->pattern = safe_strdup (s);
516 if (fg != -1 && bg != -1) {
519 attr |= mutt_alloc_color (fg, bg);
530 parse_object (BUFFER * buf, BUFFER * s, int *o, int *ql, BUFFER * err)
536 strfcpy (err->data, _("Missing arguments."), err->dsize);
540 mutt_extract_token (buf, s, 0);
541 if (!mutt_strncmp (buf->data, "quoted", 6)) {
543 *ql = strtol (buf->data + 6, &eptr, 10);
544 if (*eptr || q_level < 0) {
545 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
552 *o = MT_COLOR_QUOTED;
554 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1) {
555 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
562 typedef int (*parser_callback_t) (BUFFER *, BUFFER *, int *, int *, int *,
568 parse_color_pair (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
572 strfcpy (err->data, _("color: too few arguments"), err->dsize);
576 mutt_extract_token (buf, s, 0);
578 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
582 strfcpy (err->data, _("color: too few arguments"), err->dsize);
586 mutt_extract_token (buf, s, 0);
588 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
597 parse_attr_spec (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
607 strfcpy (err->data, _("mono: too few arguments"), err->dsize);
611 mutt_extract_token (buf, s, 0);
613 if (ascii_strcasecmp ("bold", buf->data) == 0)
615 else if (ascii_strcasecmp ("underline", buf->data) == 0)
616 *attr |= A_UNDERLINE;
617 else if (ascii_strcasecmp ("none", buf->data) == 0)
619 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
621 else if (ascii_strcasecmp ("standout", buf->data) == 0)
623 else if (ascii_strcasecmp ("normal", buf->data) == 0)
624 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
626 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
633 static int fgbgattr_to_color (int fg, int bg, int attr)
636 if (fg != -1 && bg != -1)
637 return attr | mutt_alloc_color (fg, bg);
643 /* usage: color <object> <fg> <bg> [ <regexp> ]
644 * mono <object> <attr> [ <regexp> ]
648 _mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
649 parser_callback_t callback, short dry_run)
651 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
654 if (parse_object (buf, s, &object, &q_level, err) == -1)
657 if (callback (buf, s, &fg, &bg, &attr, err) == -1)
660 /* extract a regular expression if needed */
662 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY
663 || object == MT_COLOR_INDEX) {
665 strfcpy (err->data, _("too few arguments"), err->dsize);
669 mutt_extract_token (buf, s, 0);
673 strfcpy (err->data, _("too many arguments"), err->dsize);
684 # ifdef HAVE_USE_DEFAULT_COLORS
685 if (!option (OPTNOCURSES) && has_colors ()
686 /* delay use_default_colors() until needed, since it initializes things */
687 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
688 && use_default_colors () != OK) {
689 strfcpy (err->data, _("default colors not supported"), err->dsize);
692 # endif /* HAVE_USE_DEFAULT_COLORS */
695 if (object == MT_COLOR_HEADER)
696 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err, 0);
697 else if (object == MT_COLOR_BODY)
698 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
699 else if (object == MT_COLOR_INDEX) {
700 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
701 set_option (OPTFORCEREDRAWINDEX);
703 else if (object == MT_COLOR_QUOTED) {
704 if (q_level >= ColorQuoteSize) {
705 safe_realloc (&ColorQuote, (ColorQuoteSize += 2) * sizeof (int));
706 ColorQuote[ColorQuoteSize - 2] = ColorDefs[MT_COLOR_QUOTED];
707 ColorQuote[ColorQuoteSize - 1] = ColorDefs[MT_COLOR_QUOTED];
709 if (q_level >= ColorQuoteUsed)
710 ColorQuoteUsed = q_level + 1;
712 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color (fg, bg, attr);
714 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
715 for (q_level = 1; q_level < ColorQuoteUsed; q_level++) {
716 if (ColorQuote[q_level] == A_NORMAL)
717 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
721 ColorQuote[q_level] = fgbgattr_to_color (fg, bg, attr);
724 ColorDefs[object] = fgbgattr_to_color (fg, bg, attr);
728 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors ())
729 BKGDSET (MT_COLOR_NORMAL);
738 int mutt_parse_color (BUFFER * buff, BUFFER * s, unsigned long data,
743 if (option (OPTNOCURSES) || !has_colors ())
746 return _mutt_parse_color (buff, s, err, parse_color_pair, dry_run);
751 int mutt_parse_mono (BUFFER * buff, BUFFER * s, unsigned long data,
757 if (option (OPTNOCURSES) || has_colors ())
760 if (option (OPTNOCURSES))
764 return _mutt_parse_color (buff, s, err, parse_attr_spec, dry_run);