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.
14 #include <lib-lib/mem.h>
19 #include "mutt_curses.h"
25 #include "lib/debug.h"
34 int ColorDefs[MT_COLOR_MAX];
35 COLOR_LINE *ColorHdrList = NULL;
36 COLOR_LINE *ColorBodyList = NULL;
37 COLOR_LINE *ColorIndexList = NULL;
39 /* local to this file */
40 static int ColorQuoteSize;
44 #define COLOR_DEFAULT (-2)
46 typedef struct color_list {
51 struct color_list *next;
54 static COLOR_LIST *ColorList = NULL;
55 static int UserColors = 0;
57 static struct mapping_t Colors[] = {
58 {"black", COLOR_BLACK},
61 {"green", COLOR_GREEN},
62 {"magenta", COLOR_MAGENTA},
64 {"white", COLOR_WHITE},
65 {"yellow", COLOR_YELLOW},
66 #if defined (USE_SLANG_CURSES) || defined (HAVE_USE_DEFAULT_COLORS)
67 {"default", COLOR_DEFAULT},
72 #endif /* HAVE_COLOR */
74 static struct mapping_t Fields[] = {
75 {"hdrdefault", MT_COLOR_HDEFAULT},
76 {"quoted", MT_COLOR_QUOTED},
77 {"signature", MT_COLOR_SIGNATURE},
78 {"indicator", MT_COLOR_INDICATOR},
79 {"status", MT_COLOR_STATUS},
80 {"tree", MT_COLOR_TREE},
81 {"error", MT_COLOR_ERROR},
82 {"normal", MT_COLOR_NORMAL},
83 {"tilde", MT_COLOR_TILDE},
84 {"markers", MT_COLOR_MARKERS},
85 {"header", MT_COLOR_HEADER},
86 {"body", MT_COLOR_BODY},
87 {"message", MT_COLOR_MESSAGE},
88 {"attachment", MT_COLOR_ATTACHMENT},
89 {"search", MT_COLOR_SEARCH},
90 {"bold", MT_COLOR_BOLD},
91 {"underline", MT_COLOR_UNDERLINE},
92 {"index", MT_COLOR_INDEX},
93 {"sidebar_new", MT_COLOR_NEW},
94 {"sidebar", MT_COLOR_SIDEBAR},
95 {"sidebar_flagged", MT_COLOR_FLAGGED},
99 #define COLOR_QUOTE_INIT 8
101 static COLOR_LINE *mutt_new_color_line (void)
103 COLOR_LINE *p = p_new(COLOR_LINE, 1);
110 static void mutt_free_color_line (COLOR_LINE ** l, int free_colors)
120 if (free_colors && tmp->fg != -1 && tmp->bg != -1)
121 mutt_free_color (tmp->fg, tmp->bg);
124 /* we should really introduce a container
125 * type for regular expressions.
129 mutt_pattern_free (&tmp->color_pattern);
130 p_delete(&tmp->pattern);
134 void ci_start_color (void)
136 memset (ColorDefs, A_NORMAL, sizeof (int) * MT_COLOR_MAX);
137 ColorQuote = p_new(int, COLOR_QUOTE_INIT);
138 memset (ColorQuote, A_NORMAL, sizeof (int) * COLOR_QUOTE_INIT);
139 ColorQuoteSize = COLOR_QUOTE_INIT;
142 /* set some defaults */
143 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
144 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
145 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
146 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
147 /* special meaning: toggle the relevant attribute */
148 ColorDefs[MT_COLOR_BOLD] = 0;
149 ColorDefs[MT_COLOR_UNDERLINE] = 0;
158 #ifdef USE_SLANG_CURSES
159 static char *get_color_name (char *dest, size_t destlen, int val)
161 static char *missing[3] = { "brown", "lightgray", "default" };
166 strfcpy (dest, missing[0], destlen);
170 strfcpy (dest, missing[1], destlen);
174 strfcpy (dest, missing[2], destlen);
178 for (i = 0; Colors[i].name; i++) {
179 if (Colors[i].value == val) {
180 strfcpy (dest, Colors[i].name, destlen);
185 /* Sigh. If we got this far, the color is of the form 'colorN'
186 * Slang can handle this itself, so just return 'colorN'
189 snprintf (dest, destlen, "color%d", val);
194 int mutt_alloc_color (int fg, int bg)
196 COLOR_LIST *p = ColorList;
199 #if defined (USE_SLANG_CURSES)
200 char fgc[SHORT_STRING], bgc[SHORT_STRING];
203 /* check to see if this color is already allocated to save space */
205 if (p->fg == fg && p->bg == bg) {
207 return (COLOR_PAIR (p->index));
212 /* check to see if there are colors left */
213 if (++UserColors > COLOR_PAIRS)
216 /* find the smallest available index (object) */
230 p = p_new(COLOR_LIST, 1);
239 #if defined (USE_SLANG_CURSES)
240 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
241 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg),
242 get_color_name (bgc, sizeof (bgc), bg));
244 #elif defined (HAVE_USE_DEFAULT_COLORS)
245 if (fg == COLOR_DEFAULT)
247 if (bg == COLOR_DEFAULT)
251 init_pair (i, fg, bg);
253 debug_print (1, ("Color pairs used so far: %d\n", UserColors));
255 return (COLOR_PAIR (p->index));
258 void mutt_free_color (int fg, int bg)
264 if (p->fg == fg && p->bg == bg) {
270 debug_print (1, ("Color pairs used so far: %d\n", UserColors));
272 if (p == ColorList) {
273 ColorList = ColorList->next;
292 #endif /* HAVE_COLOR */
298 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER * err)
302 if (str_ncasecmp (s, "bright", 6) == 0) {
307 /* allow aliases for xterm color resources */
308 if (str_ncasecmp (s, "color", 5) == 0) {
310 *col = strtol (s, &eptr, 10);
311 if (!*s || *eptr || *col < 0 ||
312 (*col >= COLORS && !option (OPTNOCURSES) && has_colors ())) {
313 snprintf (err->data, err->dsize, _("%s: color not supported by term"),
318 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1) {
319 snprintf (err->data, err->dsize, _("%s: no such color"), s);
329 /* usage: uncolor index pattern [pattern...]
330 * unmono index pattern [pattern...]
334 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
335 BUFFER * err, short parse_uncolor);
340 int mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
343 return _mutt_parse_uncolor (buf, s, data, err, 1);
348 int mutt_parse_unmono (BUFFER * buf, BUFFER * s, unsigned long data,
351 return _mutt_parse_uncolor (buf, s, data, err, 0);
355 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
356 BUFFER * err, short parse_uncolor)
358 int object = 0, do_cache = 0;
359 COLOR_LINE *tmp, *last = NULL;
361 mutt_extract_token (buf, s, 0);
363 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1) {
364 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
368 if (str_ncmp (buf->data, "index", 5) != 0) {
369 snprintf (err->data, err->dsize,
370 _("%s: command valid only for index object"),
371 parse_uncolor ? "uncolor" : "unmono");
376 snprintf (err->data, err->dsize,
377 _("%s: too few arguments"),
378 parse_uncolor ? "uncolor" : "unmono");
384 /* we're running without curses */
386 || /* we're parsing an uncolor command, and have no colors */
387 (parse_uncolor && !has_colors ())
388 /* we're parsing an unmono command, and have colors */
389 || (!parse_uncolor && has_colors ())
391 /* We don't even have colors compiled in */
395 /* just eat the command, but don't do anything real about it */
397 mutt_extract_token (buf, s, 0);
398 while (MoreArgs (s));
405 mutt_extract_token (buf, s, 0);
406 if (!str_cmp ("*", buf->data)) {
407 for (tmp = ColorIndexList; tmp;) {
412 mutt_free_color_line (&last, parse_uncolor);
414 ColorIndexList = NULL;
417 for (last = NULL, tmp = ColorIndexList; tmp;
418 last = tmp, tmp = tmp->next) {
419 if (!str_cmp (buf->data, tmp->pattern)) {
422 debug_print (1, ("Freeing pattern \"%s\" from ColorIndexList\n", tmp->pattern));
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 (str_cmp (s, tmp->pattern) == 0)
465 if (str_casecmp (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 = str_dup (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 (!str_ncmp (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 mem_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);