2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
20 #include "mutt_curses.h"
30 int ColorDefs[MT_COLOR_MAX];
31 COLOR_LINE *ColorHdrList = NULL;
32 COLOR_LINE *ColorBodyList = NULL;
33 COLOR_LINE *ColorIndexList = NULL;
35 /* local to this file */
36 static int ColorQuoteSize;
40 #define COLOR_DEFAULT (-2)
42 typedef struct color_list
48 struct color_list *next;
51 static COLOR_LIST *ColorList = NULL;
52 static int UserColors = 0;
54 static struct mapping_t Colors[] =
56 { "black", COLOR_BLACK },
57 { "blue", COLOR_BLUE },
58 { "cyan", COLOR_CYAN },
59 { "green", COLOR_GREEN },
60 { "magenta", COLOR_MAGENTA },
62 { "white", COLOR_WHITE },
63 { "yellow", COLOR_YELLOW },
64 #if defined (USE_SLANG_CURSES) || defined (HAVE_USE_DEFAULT_COLORS)
65 { "default", COLOR_DEFAULT },
70 #endif /* HAVE_COLOR */
72 static struct mapping_t Fields[] =
74 { "hdrdefault", MT_COLOR_HDEFAULT },
75 { "quoted", MT_COLOR_QUOTED },
76 { "signature", MT_COLOR_SIGNATURE },
77 { "indicator", MT_COLOR_INDICATOR },
78 { "status", MT_COLOR_STATUS },
79 { "tree", MT_COLOR_TREE },
80 { "error", MT_COLOR_ERROR },
81 { "normal", MT_COLOR_NORMAL },
82 { "tilde", MT_COLOR_TILDE },
83 { "markers", MT_COLOR_MARKERS },
84 { "header", MT_COLOR_HEADER },
85 { "body", MT_COLOR_BODY },
86 { "message", MT_COLOR_MESSAGE },
87 { "attachment", MT_COLOR_ATTACHMENT },
88 { "search", MT_COLOR_SEARCH },
89 { "bold", MT_COLOR_BOLD },
90 { "underline", MT_COLOR_UNDERLINE },
91 { "index", MT_COLOR_INDEX },
95 #define COLOR_QUOTE_INIT 8
97 static COLOR_LINE *mutt_new_color_line (void)
99 COLOR_LINE *p = safe_calloc (1, sizeof (COLOR_LINE));
106 static void mutt_free_color_line(COLOR_LINE **l,
117 if(free_colors && tmp->fg != -1 && tmp->bg != -1)
118 mutt_free_color(tmp->fg, tmp->bg);
121 /* we should really introduce a container
122 * type for regular expressions.
126 mutt_pattern_free(&tmp->color_pattern);
127 FREE (&tmp->pattern);
131 void ci_start_color (void)
133 memset (ColorDefs, A_NORMAL, sizeof (int) * MT_COLOR_MAX);
134 ColorQuote = (int *) safe_malloc (COLOR_QUOTE_INIT * sizeof (int));
135 memset (ColorQuote, A_NORMAL, sizeof (int) * COLOR_QUOTE_INIT);
136 ColorQuoteSize = COLOR_QUOTE_INIT;
139 /* set some defaults */
140 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
141 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
142 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
143 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
144 /* special meaning: toggle the relevant attribute */
145 ColorDefs[MT_COLOR_BOLD] = 0;
146 ColorDefs[MT_COLOR_UNDERLINE] = 0;
155 #ifdef USE_SLANG_CURSES
156 static char *get_color_name (char *dest, size_t destlen, int val)
158 static char * missing[3] = {"brown", "lightgray", ""};
164 strfcpy (dest, missing[0], destlen);
168 strfcpy (dest, missing[1], destlen);
172 strfcpy (dest, missing[2], destlen);
176 for (i = 0; Colors[i].name; i++)
178 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 */
206 if (p->fg == fg && p->bg == bg)
209 return (COLOR_PAIR (p->index));
214 /* check to see if there are colors left */
215 if (++UserColors > COLOR_PAIRS) return (A_NORMAL);
217 /* find the smallest available index (object) */
224 if (p->index == i) break;
227 if (p == NULL) break;
231 p = (COLOR_LIST *) safe_malloc (sizeof (COLOR_LIST));
240 #if defined (USE_SLANG_CURSES)
241 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
242 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg), 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 dprint(1,(debugfile,"mutt_alloc_color(): Color pairs used so far: %d\n",
256 return (COLOR_PAIR (p->index));
259 void mutt_free_color (int fg, int bg)
266 if (p->fg == fg && p->bg == bg)
269 if (p->count > 0) return;
272 dprint(1,(debugfile,"mutt_free_color(): Color pairs used so far: %d\n",
277 ColorList = ColorList->next;
298 #endif /* HAVE_COLOR */
304 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER *err)
308 if (mutt_strncasecmp (s, "bright", 6) == 0)
314 /* allow aliases for xterm color resources */
315 if (mutt_strncasecmp (s, "color", 5) == 0)
318 *col = strtol (s, &eptr, 10);
319 if (!*s || *eptr || *col < 0 ||
320 (*col >= COLORS && !option(OPTNOCURSES) && has_colors()))
322 snprintf (err->data, err->dsize, _("%s: color not supported by term"), s);
326 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1)
328 snprintf (err->data, err->dsize, _("%s: no such color"), s);
338 /* usage: uncolor index pattern [pattern...]
339 * unmono index pattern [pattern...]
343 _mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err,
344 short parse_uncolor);
349 int mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data,
352 return _mutt_parse_uncolor(buf, s, data, err, 1);
357 int mutt_parse_unmono (BUFFER *buf, BUFFER *s, unsigned long data,
360 return _mutt_parse_uncolor(buf, s, data, err, 0);
364 _mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err,
367 int object = 0, do_cache = 0;
368 COLOR_LINE *tmp, *last = NULL;
370 mutt_extract_token (buf, s, 0);
372 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1)
374 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
378 if (mutt_strncmp (buf->data, "index", 5) != 0)
380 snprintf (err->data, err->dsize,
381 _("%s: command valid only for index object"),
382 parse_uncolor ? "uncolor" : "unmono");
388 snprintf (err->data, err->dsize,
389 _("%s: too few arguments"), parse_uncolor ? "uncolor" : "unmono");
395 /* we're running without curses */
397 || /* we're parsing an uncolor command, and have no colors */
398 (parse_uncolor && !has_colors())
399 /* we're parsing an unmono command, and have colors */
400 || (!parse_uncolor && has_colors())
402 /* We don't even have colors compiled in */
407 /* just eat the command, but don't do anything real about it */
409 mutt_extract_token (buf, s, 0);
410 while (MoreArgs (s));
418 mutt_extract_token (buf, s, 0);
419 if (!mutt_strcmp ("*", buf->data))
421 for (tmp = ColorIndexList; tmp; )
427 mutt_free_color_line(&last, parse_uncolor);
429 ColorIndexList = NULL;
433 for (last = NULL, tmp = ColorIndexList; tmp; last = tmp, tmp = tmp->next)
435 if (!mutt_strcmp (buf->data, tmp->pattern))
439 dprint(1,(debugfile,"Freeing pattern \"%s\" from ColorIndexList\n",
442 last->next = tmp->next;
444 ColorIndexList = tmp->next;
445 mutt_free_color_line(&tmp, parse_uncolor);
451 while (MoreArgs (s));
454 if (do_cache && !option (OPTNOCURSES))
457 set_option (OPTFORCEREDRAWINDEX);
458 /* force re-caching of index colors */
459 for (i = 0; Context && i < Context->msgcount; i++)
460 Context->hdrs[i]->pair = 0;
467 add_pattern (COLOR_LINE **top, const char *s, int sensitive,
468 int fg, int bg, int attr, BUFFER *err,
472 /* is_index used to store compiled pattern
473 * only for `index' color object
474 * when called from mutt_parse_color() */
476 COLOR_LINE *tmp = *top;
482 if (mutt_strcmp (s, tmp->pattern) == 0)
487 if (mutt_strcasecmp (s, tmp->pattern) == 0)
496 if (fg != -1 && bg != -1)
498 if (tmp->fg != fg || tmp->bg != bg)
500 mutt_free_color (tmp->fg, tmp->bg);
503 attr |= mutt_alloc_color (fg, bg);
506 attr |= (tmp->pair & ~A_BOLD);
508 #endif /* HAVE_COLOR */
516 tmp = mutt_new_color_line ();
521 strfcpy(buf, NONULL(s), sizeof(buf));
522 mutt_check_simple (buf, sizeof (buf), NONULL(SimpleSearch));
523 if((tmp->color_pattern = mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL)
525 mutt_free_color_line(&tmp, 1);
528 /* force re-caching of index colors */
529 for (i = 0; Context && i < Context->msgcount; i++)
530 Context->hdrs[i]->pair = 0;
532 else if ((r = REGCOMP (&tmp->rx, s, (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0)
534 regerror (r, &tmp->rx, err->data, err->dsize);
535 mutt_free_color_line(&tmp, 1);
539 tmp->pattern = safe_strdup (s);
541 if(fg != -1 && bg != -1)
545 attr |= mutt_alloc_color (fg, bg);
556 parse_object(BUFFER *buf, BUFFER *s, int *o, int *ql, BUFFER *err)
563 strfcpy(err->data, _("Missing arguments."), err->dsize);
567 mutt_extract_token(buf, s, 0);
568 if(!mutt_strncmp(buf->data, "quoted", 6))
572 *ql = strtol(buf->data + 6, &eptr, 10);
573 if(*eptr || q_level < 0)
575 snprintf(err->data, err->dsize, _("%s: no such object"), buf->data);
582 *o = MT_COLOR_QUOTED;
584 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1)
586 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
593 typedef int (*parser_callback_t)(BUFFER *, BUFFER *, int *, int *, int *, BUFFER *);
598 parse_color_pair(BUFFER *buf, BUFFER *s, int *fg, int *bg, int *attr, BUFFER *err)
602 strfcpy (err->data, _("color: too few arguments"), err->dsize);
606 mutt_extract_token (buf, s, 0);
608 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
613 strfcpy (err->data, _("color: too few arguments"), err->dsize);
617 mutt_extract_token (buf, s, 0);
619 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
628 parse_attr_spec(BUFFER *buf, BUFFER *s, int *fg, int *bg, int *attr, BUFFER *err)
636 strfcpy (err->data, _("mono: too few arguments"), err->dsize);
640 mutt_extract_token (buf, s, 0);
642 if (ascii_strcasecmp ("bold", buf->data) == 0)
644 else if (ascii_strcasecmp ("underline", buf->data) == 0)
645 *attr |= A_UNDERLINE;
646 else if (ascii_strcasecmp ("none", buf->data) == 0)
648 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
650 else if (ascii_strcasecmp ("standout", buf->data) == 0)
652 else if (ascii_strcasecmp ("normal", buf->data) == 0)
653 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
656 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
663 static int fgbgattr_to_color(int fg, int bg, int attr)
666 if(fg != -1 && bg != -1)
667 return attr | mutt_alloc_color(fg, bg);
673 /* usage: color <object> <fg> <bg> [ <regexp> ]
674 * mono <object> <attr> [ <regexp> ]
678 _mutt_parse_color (BUFFER *buf, BUFFER *s, BUFFER *err,
679 parser_callback_t callback, short dry_run)
681 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
684 if(parse_object(buf, s, &object, &q_level, err) == -1)
687 if(callback(buf, s, &fg, &bg, &attr, err) == -1)
690 /* extract a regular expression if needed */
692 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY || object == MT_COLOR_INDEX)
696 strfcpy (err->data, _("too few arguments"), err->dsize);
700 mutt_extract_token (buf, s, 0);
705 strfcpy (err->data, _("too many arguments"), err->dsize);
711 if(dry_run) return 0;
715 # ifdef HAVE_USE_DEFAULT_COLORS
716 if (!option (OPTNOCURSES) && has_colors()
717 /* delay use_default_colors() until needed, since it initializes things */
718 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
719 && use_default_colors () != OK)
721 strfcpy (err->data, _("default colors not supported"), err->dsize);
724 # endif /* HAVE_USE_DEFAULT_COLORS */
727 if (object == MT_COLOR_HEADER)
728 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err,0);
729 else if (object == MT_COLOR_BODY)
730 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
731 else if (object == MT_COLOR_INDEX)
733 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
734 set_option (OPTFORCEREDRAWINDEX);
736 else if (object == MT_COLOR_QUOTED)
738 if (q_level >= ColorQuoteSize)
740 safe_realloc (&ColorQuote, (ColorQuoteSize += 2) * sizeof (int));
741 ColorQuote[ColorQuoteSize-2] = ColorDefs[MT_COLOR_QUOTED];
742 ColorQuote[ColorQuoteSize-1] = ColorDefs[MT_COLOR_QUOTED];
744 if (q_level >= ColorQuoteUsed)
745 ColorQuoteUsed = q_level + 1;
748 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color(fg, bg, attr);
750 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
751 for (q_level = 1; q_level < ColorQuoteUsed; q_level++)
753 if (ColorQuote[q_level] == A_NORMAL)
754 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
758 ColorQuote[q_level] = fgbgattr_to_color(fg, bg, attr);
761 ColorDefs[object] = fgbgattr_to_color(fg, bg, attr);
765 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors())
766 BKGDSET (MT_COLOR_NORMAL);
775 int mutt_parse_color(BUFFER *buff, BUFFER *s, unsigned long data, BUFFER *err)
779 if(option(OPTNOCURSES) || !has_colors())
782 return _mutt_parse_color(buff, s, err, parse_color_pair, dry_run);
787 int mutt_parse_mono(BUFFER *buff, BUFFER *s, unsigned long data, BUFFER *err)
792 if(option(OPTNOCURSES) || has_colors())
795 if(option(OPTNOCURSES))
799 return _mutt_parse_color(buff, s, err, parse_attr_spec, dry_run);