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.
24 #include "mutt_curses.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
52 struct color_list *next;
55 static COLOR_LIST *ColorList = NULL;
56 static int UserColors = 0;
58 static struct mapping_t Colors[] =
60 { "black", COLOR_BLACK },
61 { "blue", COLOR_BLUE },
62 { "cyan", COLOR_CYAN },
63 { "green", COLOR_GREEN },
64 { "magenta", COLOR_MAGENTA },
66 { "white", COLOR_WHITE },
67 { "yellow", COLOR_YELLOW },
68 #if defined (USE_SLANG_CURSES) || defined (HAVE_USE_DEFAULT_COLORS)
69 { "default", COLOR_DEFAULT },
74 #endif /* HAVE_COLOR */
76 static struct mapping_t Fields[] =
78 { "hdrdefault", MT_COLOR_HDEFAULT },
79 { "quoted", MT_COLOR_QUOTED },
80 { "signature", MT_COLOR_SIGNATURE },
81 { "indicator", MT_COLOR_INDICATOR },
82 { "status", MT_COLOR_STATUS },
83 { "tree", MT_COLOR_TREE },
84 { "error", MT_COLOR_ERROR },
85 { "normal", MT_COLOR_NORMAL },
86 { "tilde", MT_COLOR_TILDE },
87 { "markers", MT_COLOR_MARKERS },
88 { "header", MT_COLOR_HEADER },
89 { "body", MT_COLOR_BODY },
90 { "message", MT_COLOR_MESSAGE },
91 { "attachment", MT_COLOR_ATTACHMENT },
92 { "search", MT_COLOR_SEARCH },
93 { "bold", MT_COLOR_BOLD },
94 { "underline", MT_COLOR_UNDERLINE },
95 { "index", MT_COLOR_INDEX },
96 { "sidebar_new", MT_COLOR_NEW },
100 #define COLOR_QUOTE_INIT 8
102 static COLOR_LINE *mutt_new_color_line (void)
104 COLOR_LINE *p = safe_calloc (1, sizeof (COLOR_LINE));
111 static void mutt_free_color_line(COLOR_LINE **l,
122 if(free_colors && tmp->fg != -1 && tmp->bg != -1)
123 mutt_free_color(tmp->fg, tmp->bg);
126 /* we should really introduce a container
127 * type for regular expressions.
131 mutt_pattern_free(&tmp->color_pattern);
132 FREE (&tmp->pattern);
136 void ci_start_color (void)
138 memset (ColorDefs, A_NORMAL, sizeof (int) * MT_COLOR_MAX);
139 ColorQuote = (int *) safe_malloc (COLOR_QUOTE_INIT * sizeof (int));
140 memset (ColorQuote, A_NORMAL, sizeof (int) * COLOR_QUOTE_INIT);
141 ColorQuoteSize = COLOR_QUOTE_INIT;
144 /* set some defaults */
145 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
146 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
147 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
148 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
149 /* special meaning: toggle the relevant attribute */
150 ColorDefs[MT_COLOR_BOLD] = 0;
151 ColorDefs[MT_COLOR_UNDERLINE] = 0;
160 #ifdef USE_SLANG_CURSES
161 static char *get_color_name (char *dest, size_t destlen, int val)
163 static char * missing[3] = {"brown", "lightgray", "default"};
169 strfcpy (dest, missing[0], destlen);
173 strfcpy (dest, missing[1], destlen);
177 strfcpy (dest, missing[2], destlen);
181 for (i = 0; Colors[i].name; i++)
183 if (Colors[i].value == val)
185 strfcpy (dest, Colors[i].name, destlen);
190 /* Sigh. If we got this far, the color is of the form 'colorN'
191 * Slang can handle this itself, so just return 'colorN'
194 snprintf (dest, destlen, "color%d", val);
199 int mutt_alloc_color (int fg, int bg)
201 COLOR_LIST *p = ColorList;
204 #if defined (USE_SLANG_CURSES)
205 char fgc[SHORT_STRING], bgc[SHORT_STRING];
208 /* check to see if this color is already allocated to save space */
211 if (p->fg == fg && p->bg == bg)
214 return (COLOR_PAIR (p->index));
219 /* check to see if there are colors left */
220 if (++UserColors > COLOR_PAIRS) return (A_NORMAL);
222 /* find the smallest available index (object) */
229 if (p->index == i) break;
232 if (p == NULL) break;
236 p = (COLOR_LIST *) safe_malloc (sizeof (COLOR_LIST));
245 #if defined (USE_SLANG_CURSES)
246 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
247 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg), get_color_name (bgc, sizeof (bgc), bg));
249 #elif defined (HAVE_USE_DEFAULT_COLORS)
250 if (fg == COLOR_DEFAULT)
252 if (bg == COLOR_DEFAULT)
256 init_pair(i, fg, bg);
258 dprint(1,(debugfile,"mutt_alloc_color(): Color pairs used so far: %d\n",
261 return (COLOR_PAIR (p->index));
264 void mutt_free_color (int fg, int bg)
271 if (p->fg == fg && p->bg == bg)
274 if (p->count > 0) return;
277 dprint(1,(debugfile,"mutt_free_color(): Color pairs used so far: %d\n",
282 ColorList = ColorList->next;
303 #endif /* HAVE_COLOR */
309 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER *err)
313 if (mutt_strncasecmp (s, "bright", 6) == 0)
319 /* allow aliases for xterm color resources */
320 if (mutt_strncasecmp (s, "color", 5) == 0)
323 *col = strtol (s, &eptr, 10);
324 if (!*s || *eptr || *col < 0 ||
325 (*col >= COLORS && !option(OPTNOCURSES) && has_colors()))
327 snprintf (err->data, err->dsize, _("%s: color not supported by term"), s);
331 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1)
333 snprintf (err->data, err->dsize, _("%s: no such color"), s);
343 /* usage: uncolor index pattern [pattern...]
344 * unmono index pattern [pattern...]
348 _mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err,
349 short parse_uncolor);
354 int mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data,
357 return _mutt_parse_uncolor(buf, s, data, err, 1);
362 int mutt_parse_unmono (BUFFER *buf, BUFFER *s, unsigned long data,
365 return _mutt_parse_uncolor(buf, s, data, err, 0);
369 _mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err,
372 int object = 0, do_cache = 0;
373 COLOR_LINE *tmp, *last = NULL;
375 mutt_extract_token (buf, s, 0);
377 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1)
379 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
383 if (mutt_strncmp (buf->data, "index", 5) != 0)
385 snprintf (err->data, err->dsize,
386 _("%s: command valid only for index object"),
387 parse_uncolor ? "uncolor" : "unmono");
393 snprintf (err->data, err->dsize,
394 _("%s: too few arguments"), parse_uncolor ? "uncolor" : "unmono");
400 /* we're running without curses */
402 || /* we're parsing an uncolor command, and have no colors */
403 (parse_uncolor && !has_colors())
404 /* we're parsing an unmono command, and have colors */
405 || (!parse_uncolor && has_colors())
407 /* We don't even have colors compiled in */
412 /* just eat the command, but don't do anything real about it */
414 mutt_extract_token (buf, s, 0);
415 while (MoreArgs (s));
423 mutt_extract_token (buf, s, 0);
424 if (!mutt_strcmp ("*", buf->data))
426 for (tmp = ColorIndexList; tmp; )
432 mutt_free_color_line(&last, parse_uncolor);
434 ColorIndexList = NULL;
438 for (last = NULL, tmp = ColorIndexList; tmp; last = tmp, tmp = tmp->next)
440 if (!mutt_strcmp (buf->data, tmp->pattern))
444 dprint(1,(debugfile,"Freeing pattern \"%s\" from ColorIndexList\n",
447 last->next = tmp->next;
449 ColorIndexList = tmp->next;
450 mutt_free_color_line(&tmp, parse_uncolor);
456 while (MoreArgs (s));
459 if (do_cache && !option (OPTNOCURSES))
462 set_option (OPTFORCEREDRAWINDEX);
463 /* force re-caching of index colors */
464 for (i = 0; Context && i < Context->msgcount; i++)
465 Context->hdrs[i]->pair = 0;
472 add_pattern (COLOR_LINE **top, const char *s, int sensitive,
473 int fg, int bg, int attr, BUFFER *err,
477 /* is_index used to store compiled pattern
478 * only for `index' color object
479 * when called from mutt_parse_color() */
481 COLOR_LINE *tmp = *top;
487 if (mutt_strcmp (s, tmp->pattern) == 0)
492 if (mutt_strcasecmp (s, tmp->pattern) == 0)
501 if (fg != -1 && bg != -1)
503 if (tmp->fg != fg || tmp->bg != bg)
505 mutt_free_color (tmp->fg, tmp->bg);
508 attr |= mutt_alloc_color (fg, bg);
511 attr |= (tmp->pair & ~A_BOLD);
513 #endif /* HAVE_COLOR */
521 tmp = mutt_new_color_line ();
526 strfcpy(buf, NONULL(s), sizeof(buf));
527 mutt_check_simple (buf, sizeof (buf), NONULL(SimpleSearch));
528 if((tmp->color_pattern = mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL)
530 mutt_free_color_line(&tmp, 1);
533 /* force re-caching of index colors */
534 for (i = 0; Context && i < Context->msgcount; i++)
535 Context->hdrs[i]->pair = 0;
537 else if ((r = REGCOMP (&tmp->rx, s, (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0)
539 regerror (r, &tmp->rx, err->data, err->dsize);
540 mutt_free_color_line(&tmp, 1);
544 tmp->pattern = safe_strdup (s);
546 if(fg != -1 && bg != -1)
550 attr |= mutt_alloc_color (fg, bg);
561 parse_object(BUFFER *buf, BUFFER *s, int *o, int *ql, BUFFER *err)
568 strfcpy(err->data, _("Missing arguments."), err->dsize);
572 mutt_extract_token(buf, s, 0);
573 if(!mutt_strncmp(buf->data, "quoted", 6))
577 *ql = strtol(buf->data + 6, &eptr, 10);
578 if(*eptr || q_level < 0)
580 snprintf(err->data, err->dsize, _("%s: no such object"), buf->data);
587 *o = MT_COLOR_QUOTED;
589 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1)
591 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
598 typedef int (*parser_callback_t)(BUFFER *, BUFFER *, int *, int *, int *, BUFFER *);
603 parse_color_pair(BUFFER *buf, BUFFER *s, int *fg, int *bg, int *attr, BUFFER *err)
607 strfcpy (err->data, _("color: too few arguments"), err->dsize);
611 mutt_extract_token (buf, s, 0);
613 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
618 strfcpy (err->data, _("color: too few arguments"), err->dsize);
622 mutt_extract_token (buf, s, 0);
624 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
633 parse_attr_spec(BUFFER *buf, BUFFER *s, int *fg, int *bg, int *attr, BUFFER *err)
641 strfcpy (err->data, _("mono: too few arguments"), err->dsize);
645 mutt_extract_token (buf, s, 0);
647 if (ascii_strcasecmp ("bold", buf->data) == 0)
649 else if (ascii_strcasecmp ("underline", buf->data) == 0)
650 *attr |= A_UNDERLINE;
651 else if (ascii_strcasecmp ("none", buf->data) == 0)
653 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
655 else if (ascii_strcasecmp ("standout", buf->data) == 0)
657 else if (ascii_strcasecmp ("normal", buf->data) == 0)
658 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
661 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
668 static int fgbgattr_to_color(int fg, int bg, int attr)
671 if(fg != -1 && bg != -1)
672 return attr | mutt_alloc_color(fg, bg);
678 /* usage: color <object> <fg> <bg> [ <regexp> ]
679 * mono <object> <attr> [ <regexp> ]
683 _mutt_parse_color (BUFFER *buf, BUFFER *s, BUFFER *err,
684 parser_callback_t callback, short dry_run)
686 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
689 if(parse_object(buf, s, &object, &q_level, err) == -1)
692 if(callback(buf, s, &fg, &bg, &attr, err) == -1)
695 /* extract a regular expression if needed */
697 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY || object == MT_COLOR_INDEX)
701 strfcpy (err->data, _("too few arguments"), err->dsize);
705 mutt_extract_token (buf, s, 0);
710 strfcpy (err->data, _("too many arguments"), err->dsize);
716 if(dry_run) return 0;
720 # ifdef HAVE_USE_DEFAULT_COLORS
721 if (!option (OPTNOCURSES) && has_colors()
722 /* delay use_default_colors() until needed, since it initializes things */
723 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
724 && use_default_colors () != OK)
726 strfcpy (err->data, _("default colors not supported"), err->dsize);
729 # endif /* HAVE_USE_DEFAULT_COLORS */
732 if (object == MT_COLOR_HEADER)
733 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err,0);
734 else if (object == MT_COLOR_BODY)
735 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
736 else if (object == MT_COLOR_INDEX)
738 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
739 set_option (OPTFORCEREDRAWINDEX);
741 else if (object == MT_COLOR_QUOTED)
743 if (q_level >= ColorQuoteSize)
745 safe_realloc (&ColorQuote, (ColorQuoteSize += 2) * sizeof (int));
746 ColorQuote[ColorQuoteSize-2] = ColorDefs[MT_COLOR_QUOTED];
747 ColorQuote[ColorQuoteSize-1] = ColorDefs[MT_COLOR_QUOTED];
749 if (q_level >= ColorQuoteUsed)
750 ColorQuoteUsed = q_level + 1;
753 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color(fg, bg, attr);
755 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
756 for (q_level = 1; q_level < ColorQuoteUsed; q_level++)
758 if (ColorQuote[q_level] == A_NORMAL)
759 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
763 ColorQuote[q_level] = fgbgattr_to_color(fg, bg, attr);
766 ColorDefs[object] = fgbgattr_to_color(fg, bg, attr);
770 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors())
771 BKGDSET (MT_COLOR_NORMAL);
780 int mutt_parse_color(BUFFER *buff, BUFFER *s, unsigned long data, BUFFER *err)
784 if(option(OPTNOCURSES) || !has_colors())
787 return _mutt_parse_color(buff, s, err, parse_color_pair, dry_run);
792 int mutt_parse_mono(BUFFER *buff, BUFFER *s, unsigned long data, BUFFER *err)
797 if(option(OPTNOCURSES) || has_colors())
800 if(option(OPTNOCURSES))
804 return _mutt_parse_color(buff, s, err, parse_attr_spec, dry_run);