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.
18 #include <lib-lib/lib-lib.h>
27 int ColorDefs[MT_COLOR_MAX];
28 COLOR_LINE *ColorHdrList = NULL;
29 COLOR_LINE *ColorBodyList = NULL;
30 COLOR_LINE *ColorIndexList = NULL;
32 /* local to this file */
33 static int ColorQuoteSize;
37 #define COLOR_DEFAULT (-2)
39 typedef struct color_list {
44 struct color_list *next;
47 static COLOR_LIST *ColorList = NULL;
48 static int UserColors = 0;
50 static struct mapping_t Colors[] = {
51 {"black", COLOR_BLACK},
54 {"green", COLOR_GREEN},
55 {"magenta", COLOR_MAGENTA},
57 {"white", COLOR_WHITE},
58 {"yellow", COLOR_YELLOW},
59 #if defined (USE_SLANG_CURSES) || defined (HAVE_USE_DEFAULT_COLORS)
60 {"default", COLOR_DEFAULT},
65 #endif /* HAVE_COLOR */
67 static struct mapping_t Fields[] = {
68 {"hdrdefault", MT_COLOR_HDEFAULT},
69 {"quoted", MT_COLOR_QUOTED},
70 {"signature", MT_COLOR_SIGNATURE},
71 {"indicator", MT_COLOR_INDICATOR},
72 {"status", MT_COLOR_STATUS},
73 {"tree", MT_COLOR_TREE},
74 {"error", MT_COLOR_ERROR},
75 {"normal", MT_COLOR_NORMAL},
76 {"tilde", MT_COLOR_TILDE},
77 {"markers", MT_COLOR_MARKERS},
78 {"header", MT_COLOR_HEADER},
79 {"body", MT_COLOR_BODY},
80 {"message", MT_COLOR_MESSAGE},
81 {"attachment", MT_COLOR_ATTACHMENT},
82 {"search", MT_COLOR_SEARCH},
83 {"bold", MT_COLOR_BOLD},
84 {"underline", MT_COLOR_UNDERLINE},
85 {"index", MT_COLOR_INDEX},
86 {"sidebar_new", MT_COLOR_NEW},
87 {"sidebar", MT_COLOR_SIDEBAR},
88 {"sidebar_flagged", MT_COLOR_FLAGGED},
92 #define COLOR_QUOTE_INIT 8
94 static COLOR_LINE *mutt_new_color_line (void)
96 COLOR_LINE *p = p_new(COLOR_LINE, 1);
103 static void mutt_free_color_line (COLOR_LINE ** l, int free_colors)
113 if (free_colors && tmp->fg != -1 && tmp->bg != -1)
114 mutt_free_color (tmp->fg, tmp->bg);
117 /* we should really introduce a container
118 * type for regular expressions.
122 mutt_pattern_free (&tmp->color_pattern);
123 p_delete(&tmp->pattern);
127 void ci_start_color (void)
129 memset(ColorDefs, A_NORMAL, sizeof(int) * MT_COLOR_MAX);
130 ColorQuote = p_new(int, COLOR_QUOTE_INIT);
131 memset(ColorQuote, A_NORMAL, sizeof(int) * COLOR_QUOTE_INIT);
132 ColorQuoteSize = COLOR_QUOTE_INIT;
135 /* set some defaults */
136 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
137 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
138 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
139 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
140 /* special meaning: toggle the relevant attribute */
141 ColorDefs[MT_COLOR_BOLD] = 0;
142 ColorDefs[MT_COLOR_UNDERLINE] = 0;
151 #ifdef USE_SLANG_CURSES
152 static char *get_color_name (char *dest, size_t destlen, int val)
154 static char *missing[3] = { "brown", "lightgray", "default" };
159 m_strcpy(dest, destlen, missing[0]);
163 m_strcpy(dest, destlen, missing[1]);
167 m_strcpy(dest, destlen, missing[2]);
171 for (i = 0; Colors[i].name; i++) {
172 if (Colors[i].value == val) {
173 m_strcpy(dest, destlen, Colors[i].name);
178 /* Sigh. If we got this far, the color is of the form 'colorN'
179 * Slang can handle this itself, so just return 'colorN'
182 snprintf (dest, destlen, "color%d", val);
187 int mutt_alloc_color (int fg, int bg)
189 COLOR_LIST *p = ColorList;
192 #if defined (USE_SLANG_CURSES)
193 char fgc[SHORT_STRING], bgc[SHORT_STRING];
196 /* check to see if this color is already allocated to save space */
198 if (p->fg == fg && p->bg == bg) {
200 return (COLOR_PAIR (p->index));
205 /* check to see if there are colors left */
206 if (++UserColors > COLOR_PAIRS)
209 /* find the smallest available index (object) */
223 p = p_new(COLOR_LIST, 1);
232 #if defined (USE_SLANG_CURSES)
233 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
234 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg),
235 get_color_name (bgc, sizeof (bgc), bg));
237 #elif defined (HAVE_USE_DEFAULT_COLORS)
238 if (fg == COLOR_DEFAULT)
240 if (bg == COLOR_DEFAULT)
244 init_pair (i, fg, bg);
246 return (COLOR_PAIR (p->index));
249 void mutt_free_color (int fg, int bg)
255 if (p->fg == fg && p->bg == bg) {
262 if (p == ColorList) {
263 ColorList = ColorList->next;
282 #endif /* HAVE_COLOR */
288 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER * err)
292 if (m_strncasecmp(s, "bright", 6) == 0) {
297 /* allow aliases for xterm color resources */
298 if (m_strncasecmp(s, "color", 5) == 0) {
300 *col = strtol (s, &eptr, 10);
301 if (!*s || *eptr || *col < 0 ||
302 (*col >= COLORS && !option (OPTNOCURSES) && has_colors ())) {
303 snprintf (err->data, err->dsize, _("%s: color not supported by term"),
308 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1) {
309 snprintf (err->data, err->dsize, _("%s: no such color"), s);
319 /* usage: uncolor index pattern [pattern...]
320 * unmono index pattern [pattern...]
324 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
325 BUFFER * err, short parse_uncolor);
330 int mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
333 return _mutt_parse_uncolor (buf, s, data, err, 1);
338 int mutt_parse_unmono (BUFFER * buf, BUFFER * s, unsigned long data,
341 return _mutt_parse_uncolor (buf, s, data, err, 0);
345 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
346 BUFFER * err, short parse_uncolor)
348 int object = 0, do_cache = 0;
349 COLOR_LINE *tmp, *last = NULL;
351 mutt_extract_token (buf, s, 0);
353 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1) {
354 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
358 if (m_strncmp(buf->data, "index", 5) != 0) {
359 snprintf (err->data, err->dsize,
360 _("%s: command valid only for index object"),
361 parse_uncolor ? "uncolor" : "unmono");
366 snprintf (err->data, err->dsize,
367 _("%s: too few arguments"),
368 parse_uncolor ? "uncolor" : "unmono");
374 /* we're running without curses */
376 || /* we're parsing an uncolor command, and have no colors */
377 (parse_uncolor && !has_colors ())
378 /* we're parsing an unmono command, and have colors */
379 || (!parse_uncolor && has_colors ())
381 /* We don't even have colors compiled in */
385 /* just eat the command, but don't do anything real about it */
387 mutt_extract_token (buf, s, 0);
388 while (MoreArgs (s));
395 mutt_extract_token (buf, s, 0);
396 if (!m_strcmp("*", buf->data)) {
397 for (tmp = ColorIndexList; tmp;) {
402 mutt_free_color_line (&last, parse_uncolor);
404 ColorIndexList = NULL;
407 for (last = NULL, tmp = ColorIndexList; tmp;
408 last = tmp, tmp = tmp->next) {
409 if (!m_strcmp(buf->data, tmp->pattern)) {
413 last->next = tmp->next;
415 ColorIndexList = tmp->next;
416 mutt_free_color_line (&tmp, parse_uncolor);
422 while (MoreArgs (s));
425 if (do_cache && !option (OPTNOCURSES)) {
428 set_option (OPTFORCEREDRAWINDEX);
429 /* force re-caching of index colors */
430 for (i = 0; Context && i < Context->msgcount; i++)
431 Context->hdrs[i]->pair = 0;
438 add_pattern (COLOR_LINE ** top, const char *s, int sensitive,
439 int fg, int bg, int attr, BUFFER * err, int is_index)
442 /* is_index used to store compiled pattern
443 * only for `index' color object
444 * when called from mutt_parse_color() */
446 COLOR_LINE *tmp = *top;
450 if (m_strcmp(s, tmp->pattern) == 0)
454 if (m_strcasecmp(s, tmp->pattern) == 0)
462 if (fg != -1 && bg != -1) {
463 if (tmp->fg != fg || tmp->bg != bg) {
464 mutt_free_color (tmp->fg, tmp->bg);
467 attr |= mutt_alloc_color (fg, bg);
470 attr |= (tmp->pair & ~A_BOLD);
472 #endif /* HAVE_COLOR */
479 tmp = mutt_new_color_line ();
483 m_strcpy(buf, sizeof(buf), NONULL(s));
484 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
485 if ((tmp->color_pattern =
486 mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL) {
487 mutt_free_color_line (&tmp, 1);
490 /* force re-caching of index colors */
491 for (i = 0; Context && i < Context->msgcount; i++)
492 Context->hdrs[i]->pair = 0;
496 REGCOMP (&tmp->rx, s,
497 (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0) {
498 regerror (r, &tmp->rx, err->data, err->dsize);
499 mutt_free_color_line (&tmp, 1);
503 tmp->pattern = m_strdup(s);
505 if (fg != -1 && bg != -1) {
508 attr |= mutt_alloc_color (fg, bg);
519 parse_object (BUFFER * buf, BUFFER * s, int *o, int *ql, BUFFER * err)
525 m_strcpy(err->data, err->dsize, _("Missing arguments."));
529 mutt_extract_token (buf, s, 0);
530 if (!m_strncmp(buf->data, "quoted", 6)) {
532 *ql = strtol (buf->data + 6, &eptr, 10);
533 if (*eptr || q_level < 0) {
534 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
541 *o = MT_COLOR_QUOTED;
543 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1) {
544 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
551 typedef int (*parser_callback_t) (BUFFER *, BUFFER *, int *, int *, int *,
557 parse_color_pair (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
561 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
565 mutt_extract_token (buf, s, 0);
567 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
571 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
575 mutt_extract_token (buf, s, 0);
577 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
586 parse_attr_spec (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
596 m_strcpy(err->data, err->dsize, _("mono: too few arguments"));
600 mutt_extract_token (buf, s, 0);
602 if (ascii_strcasecmp ("bold", buf->data) == 0)
604 else if (ascii_strcasecmp ("underline", buf->data) == 0)
605 *attr |= A_UNDERLINE;
606 else if (ascii_strcasecmp ("none", buf->data) == 0)
608 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
610 else if (ascii_strcasecmp ("standout", buf->data) == 0)
612 else if (ascii_strcasecmp ("normal", buf->data) == 0)
613 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
615 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
622 static int fgbgattr_to_color (int fg, int bg, int attr)
625 if (fg != -1 && bg != -1)
626 return attr | mutt_alloc_color (fg, bg);
632 /* usage: color <object> <fg> <bg> [ <regexp> ]
633 * mono <object> <attr> [ <regexp> ]
637 _mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
638 parser_callback_t callback, short dry_run)
640 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
643 if (parse_object (buf, s, &object, &q_level, err) == -1)
646 if (callback (buf, s, &fg, &bg, &attr, err) == -1)
649 /* extract a regular expression if needed */
651 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY
652 || object == MT_COLOR_INDEX) {
654 m_strcpy(err->data, err->dsize, _("too few arguments"));
658 mutt_extract_token (buf, s, 0);
662 m_strcpy(err->data, err->dsize, _("too many arguments"));
673 # ifdef HAVE_USE_DEFAULT_COLORS
674 if (!option (OPTNOCURSES) && has_colors ()
675 /* delay use_default_colors() until needed, since it initializes things */
676 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
677 && use_default_colors () != OK) {
678 m_strcpy(err->data, err->dsize, _("default colors not supported"));
681 # endif /* HAVE_USE_DEFAULT_COLORS */
684 if (object == MT_COLOR_HEADER)
685 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err, 0);
686 else if (object == MT_COLOR_BODY)
687 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
688 else if (object == MT_COLOR_INDEX) {
689 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
690 set_option (OPTFORCEREDRAWINDEX);
692 else if (object == MT_COLOR_QUOTED) {
693 if (q_level >= ColorQuoteSize) {
694 p_realloc(&ColorQuote, ColorQuoteSize += 2);
695 ColorQuote[ColorQuoteSize - 2] = ColorDefs[MT_COLOR_QUOTED];
696 ColorQuote[ColorQuoteSize - 1] = ColorDefs[MT_COLOR_QUOTED];
698 if (q_level >= ColorQuoteUsed)
699 ColorQuoteUsed = q_level + 1;
701 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color (fg, bg, attr);
703 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
704 for (q_level = 1; q_level < ColorQuoteUsed; q_level++) {
705 if (ColorQuote[q_level] == A_NORMAL)
706 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
710 ColorQuote[q_level] = fgbgattr_to_color (fg, bg, attr);
713 ColorDefs[object] = fgbgattr_to_color (fg, bg, attr);
717 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors ())
718 BKGDSET (MT_COLOR_NORMAL);
727 int mutt_parse_color (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
732 if (option (OPTNOCURSES) || !has_colors ())
735 return _mutt_parse_color (buff, s, err, parse_color_pair, dry_run);
740 int mutt_parse_mono (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
746 if (option (OPTNOCURSES) || has_colors ())
749 if (option (OPTNOCURSES))
753 return _mutt_parse_color (buff, s, err, parse_attr_spec, dry_run);