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;
30 #define COLOR_DEFAULT (-2)
32 typedef struct color_list {
37 struct color_list *next;
40 static COLOR_LIST *ColorList = NULL;
41 static int UserColors = 0;
43 static struct mapping_t Colors[] = {
44 {"black", COLOR_BLACK},
47 {"green", COLOR_GREEN},
48 {"magenta", COLOR_MAGENTA},
50 {"white", COLOR_WHITE},
51 {"yellow", COLOR_YELLOW},
52 #if defined (USE_SLANG_CURSES) || defined (HAVE_USE_DEFAULT_COLORS)
53 {"default", COLOR_DEFAULT},
58 #endif /* HAVE_COLOR */
60 static struct mapping_t Fields[] = {
61 {"hdrdefault", MT_COLOR_HDEFAULT},
62 {"quoted", MT_COLOR_QUOTED},
63 {"signature", MT_COLOR_SIGNATURE},
64 {"indicator", MT_COLOR_INDICATOR},
65 {"status", MT_COLOR_STATUS},
66 {"tree", MT_COLOR_TREE},
67 {"error", MT_COLOR_ERROR},
68 {"normal", MT_COLOR_NORMAL},
69 {"tilde", MT_COLOR_TILDE},
70 {"markers", MT_COLOR_MARKERS},
71 {"header", MT_COLOR_HEADER},
72 {"body", MT_COLOR_BODY},
73 {"message", MT_COLOR_MESSAGE},
74 {"attachment", MT_COLOR_ATTACHMENT},
75 {"search", MT_COLOR_SEARCH},
76 {"bold", MT_COLOR_BOLD},
77 {"underline", MT_COLOR_UNDERLINE},
78 {"index", MT_COLOR_INDEX},
79 {"sidebar_new", MT_COLOR_NEW},
80 {"sidebar", MT_COLOR_SIDEBAR},
81 {"sidebar_flagged", MT_COLOR_FLAGGED},
85 #define COLOR_QUOTE_INIT 8
87 static COLOR_LINE *mutt_new_color_line (void)
89 COLOR_LINE *p = p_new(COLOR_LINE, 1);
96 static void mutt_free_color_line (COLOR_LINE ** l, int free_colors)
106 if (free_colors && tmp->fg != -1 && tmp->bg != -1)
107 mutt_free_color (tmp->fg, tmp->bg);
110 /* we should really introduce a container
111 * type for regular expressions.
115 pattern_list_wipe(&tmp->color_pattern);
116 p_delete(&tmp->pattern);
120 void ci_start_color (void)
122 memset(ColorDefs, A_NORMAL, sizeof(int) * MT_COLOR_MAX);
123 ColorQuote = p_new(int, COLOR_QUOTE_INIT);
124 memset(ColorQuote, A_NORMAL, sizeof(int) * COLOR_QUOTE_INIT);
125 ColorQuoteSize = COLOR_QUOTE_INIT;
128 /* set some defaults */
129 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
130 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
131 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
132 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
133 /* special meaning: toggle the relevant attribute */
134 ColorDefs[MT_COLOR_BOLD] = 0;
135 ColorDefs[MT_COLOR_UNDERLINE] = 0;
144 #ifdef USE_SLANG_CURSES
145 static char *get_color_name (char *dest, size_t destlen, int val)
147 static char *missing[3] = { "brown", "lightgray", "default" };
152 m_strcpy(dest, destlen, missing[0]);
156 m_strcpy(dest, destlen, missing[1]);
160 m_strcpy(dest, destlen, missing[2]);
164 for (i = 0; Colors[i].name; i++) {
165 if (Colors[i].value == val) {
166 m_strcpy(dest, destlen, Colors[i].name);
171 /* Sigh. If we got this far, the color is of the form 'colorN'
172 * Slang can handle this itself, so just return 'colorN'
175 snprintf (dest, destlen, "color%d", val);
180 int mutt_alloc_color (int fg, int bg)
182 COLOR_LIST *p = ColorList;
185 #if defined (USE_SLANG_CURSES)
186 char fgc[STRING], bgc[STRING];
189 /* check to see if this color is already allocated to save space */
191 if (p->fg == fg && p->bg == bg) {
193 return (COLOR_PAIR (p->index));
198 /* check to see if there are colors left */
199 if (++UserColors > COLOR_PAIRS)
202 /* find the smallest available index (object) */
216 p = p_new(COLOR_LIST, 1);
225 #if defined (USE_SLANG_CURSES)
226 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
227 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg),
228 get_color_name (bgc, sizeof (bgc), bg));
230 #elif defined (HAVE_USE_DEFAULT_COLORS)
231 if (fg == COLOR_DEFAULT)
233 if (bg == COLOR_DEFAULT)
237 init_pair (i, fg, bg);
239 return (COLOR_PAIR (p->index));
242 void mutt_free_color (int fg, int bg)
248 if (p->fg == fg && p->bg == bg) {
255 if (p == ColorList) {
256 ColorList = ColorList->next;
275 #endif /* HAVE_COLOR */
281 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER * err)
285 if (m_strncasecmp(s, "bright", 6) == 0) {
290 /* allow aliases for xterm color resources */
291 if (m_strncasecmp(s, "color", 5) == 0) {
293 *col = strtol (s, &eptr, 10);
294 if (!*s || *eptr || *col < 0 ||
295 (*col >= COLORS && !option (OPTNOCURSES) && has_colors ())) {
296 snprintf (err->data, err->dsize, _("%s: color not supported by term"),
301 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1) {
302 snprintf (err->data, err->dsize, _("%s: no such color"), s);
312 /* usage: uncolor index pattern [pattern...]
313 * unmono index pattern [pattern...]
317 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
318 BUFFER * err, short parse_uncolor);
323 int mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
326 return _mutt_parse_uncolor (buf, s, data, err, 1);
331 int mutt_parse_unmono (BUFFER * buf, BUFFER * s, unsigned long data,
334 return _mutt_parse_uncolor (buf, s, data, err, 0);
338 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
339 BUFFER * err, short parse_uncolor)
341 int object = 0, do_cache = 0;
342 COLOR_LINE *tmp, *last = NULL;
344 mutt_extract_token (buf, s, 0);
346 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1) {
347 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
351 if (m_strncmp(buf->data, "index", 5) != 0) {
352 snprintf (err->data, err->dsize,
353 _("%s: command valid only for index object"),
354 parse_uncolor ? "uncolor" : "unmono");
359 snprintf (err->data, err->dsize,
360 _("%s: too few arguments"),
361 parse_uncolor ? "uncolor" : "unmono");
367 /* we're running without curses */
369 || /* we're parsing an uncolor command, and have no colors */
370 (parse_uncolor && !has_colors ())
371 /* we're parsing an unmono command, and have colors */
372 || (!parse_uncolor && has_colors ())
374 /* We don't even have colors compiled in */
378 /* just eat the command, but don't do anything real about it */
380 mutt_extract_token (buf, s, 0);
381 while (MoreArgs (s));
388 mutt_extract_token (buf, s, 0);
389 if (!m_strcmp("*", buf->data)) {
390 for (tmp = ColorIndexList; tmp;) {
395 mutt_free_color_line (&last, parse_uncolor);
397 ColorIndexList = NULL;
400 for (last = NULL, tmp = ColorIndexList; tmp;
401 last = tmp, tmp = tmp->next) {
402 if (!m_strcmp(buf->data, tmp->pattern)) {
406 last->next = tmp->next;
408 ColorIndexList = tmp->next;
409 mutt_free_color_line (&tmp, parse_uncolor);
415 while (MoreArgs (s));
418 if (do_cache && !option (OPTNOCURSES)) {
421 set_option (OPTFORCEREDRAWINDEX);
422 /* force re-caching of index colors */
423 for (i = 0; Context && i < Context->msgcount; i++)
424 Context->hdrs[i]->pair = 0;
431 add_pattern (COLOR_LINE ** top, const char *s, int sensitive,
432 int fg, int bg, int attr, BUFFER * err, int is_index)
435 /* is_index used to store compiled pattern
436 * only for `index' color object
437 * when called from mutt_parse_color() */
439 COLOR_LINE *tmp = *top;
443 if (m_strcmp(s, tmp->pattern) == 0)
447 if (m_strcasecmp(s, tmp->pattern) == 0)
455 if (fg != -1 && bg != -1) {
456 if (tmp->fg != fg || tmp->bg != bg) {
457 mutt_free_color (tmp->fg, tmp->bg);
460 attr |= mutt_alloc_color (fg, bg);
463 attr |= (tmp->pair & ~A_BOLD);
465 #endif /* HAVE_COLOR */
472 tmp = mutt_new_color_line ();
476 m_strcpy(buf, sizeof(buf), NONULL(s));
477 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
478 if ((tmp->color_pattern =
479 mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL) {
480 mutt_free_color_line (&tmp, 1);
483 /* force re-caching of index colors */
484 for (i = 0; Context && i < Context->msgcount; i++)
485 Context->hdrs[i]->pair = 0;
489 REGCOMP (&tmp->rx, s,
490 (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0) {
491 regerror (r, &tmp->rx, err->data, err->dsize);
492 mutt_free_color_line (&tmp, 1);
496 tmp->pattern = m_strdup(s);
498 if (fg != -1 && bg != -1) {
501 attr |= mutt_alloc_color (fg, bg);
512 parse_object (BUFFER * buf, BUFFER * s, int *o, int *ql, BUFFER * err)
518 m_strcpy(err->data, err->dsize, _("Missing arguments."));
522 mutt_extract_token (buf, s, 0);
523 if (!m_strncmp(buf->data, "quoted", 6)) {
525 *ql = strtol (buf->data + 6, &eptr, 10);
526 if (*eptr || q_level < 0) {
527 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
534 *o = MT_COLOR_QUOTED;
536 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1) {
537 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
544 typedef int (*parser_callback_t) (BUFFER *, BUFFER *, int *, int *, int *,
550 parse_color_pair (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
554 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
558 mutt_extract_token (buf, s, 0);
560 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
564 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
568 mutt_extract_token (buf, s, 0);
570 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
579 parse_attr_spec (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
589 m_strcpy(err->data, err->dsize, _("mono: too few arguments"));
593 mutt_extract_token (buf, s, 0);
595 if (ascii_strcasecmp ("bold", buf->data) == 0)
597 else if (ascii_strcasecmp ("underline", buf->data) == 0)
598 *attr |= A_UNDERLINE;
599 else if (ascii_strcasecmp ("none", buf->data) == 0)
601 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
603 else if (ascii_strcasecmp ("standout", buf->data) == 0)
605 else if (ascii_strcasecmp ("normal", buf->data) == 0)
606 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
608 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
615 static int fgbgattr_to_color (int fg, int bg, int attr)
618 if (fg != -1 && bg != -1)
619 return attr | mutt_alloc_color (fg, bg);
625 /* usage: color <object> <fg> <bg> [ <regexp> ]
626 * mono <object> <attr> [ <regexp> ]
630 _mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
631 parser_callback_t callback, short dry_run)
633 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
636 if (parse_object (buf, s, &object, &q_level, err) == -1)
639 if (callback (buf, s, &fg, &bg, &attr, err) == -1)
642 /* extract a regular expression if needed */
644 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY
645 || object == MT_COLOR_INDEX) {
647 m_strcpy(err->data, err->dsize, _("too few arguments"));
651 mutt_extract_token (buf, s, 0);
655 m_strcpy(err->data, err->dsize, _("too many arguments"));
666 # ifdef HAVE_USE_DEFAULT_COLORS
667 if (!option (OPTNOCURSES) && has_colors ()
668 /* delay use_default_colors() until needed, since it initializes things */
669 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
670 && use_default_colors () != OK) {
671 m_strcpy(err->data, err->dsize, _("default colors not supported"));
674 # endif /* HAVE_USE_DEFAULT_COLORS */
677 if (object == MT_COLOR_HEADER)
678 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err, 0);
679 else if (object == MT_COLOR_BODY)
680 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
681 else if (object == MT_COLOR_INDEX) {
682 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
683 set_option (OPTFORCEREDRAWINDEX);
685 else if (object == MT_COLOR_QUOTED) {
686 if (q_level >= ColorQuoteSize) {
687 p_realloc(&ColorQuote, ColorQuoteSize += 2);
688 ColorQuote[ColorQuoteSize - 2] = ColorDefs[MT_COLOR_QUOTED];
689 ColorQuote[ColorQuoteSize - 1] = ColorDefs[MT_COLOR_QUOTED];
691 if (q_level >= ColorQuoteUsed)
692 ColorQuoteUsed = q_level + 1;
694 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color (fg, bg, attr);
696 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
697 for (q_level = 1; q_level < ColorQuoteUsed; q_level++) {
698 if (ColorQuote[q_level] == A_NORMAL)
699 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
703 ColorQuote[q_level] = fgbgattr_to_color (fg, bg, attr);
706 ColorDefs[object] = fgbgattr_to_color (fg, bg, attr);
710 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors ())
711 BKGDSET (MT_COLOR_NORMAL);
720 int mutt_parse_color (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
725 if (option (OPTNOCURSES) || !has_colors ())
728 return _mutt_parse_color (buff, s, err, parse_color_pair, dry_run);
733 int mutt_parse_mono (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
739 if (option (OPTNOCURSES) || has_colors ())
742 if (option (OPTNOCURSES))
746 return _mutt_parse_color (buff, s, err, parse_attr_spec, dry_run);