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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 },
99 #define COLOR_QUOTE_INIT 8
101 static COLOR_LINE *mutt_new_color_line (void)
103 COLOR_LINE *p = safe_calloc (1, sizeof (COLOR_LINE));
110 static void mutt_free_color_line(COLOR_LINE **l,
121 if(free_colors && tmp->fg != -1 && tmp->bg != -1)
122 mutt_free_color(tmp->fg, tmp->bg);
125 /* we should really introduce a container
126 * type for regular expressions.
130 mutt_pattern_free(&tmp->color_pattern);
131 FREE (&tmp->pattern);
132 FREE (l); /* __FREE_CHECKED__ */
135 void ci_start_color (void)
137 memset (ColorDefs, A_NORMAL, sizeof (int) * MT_COLOR_MAX);
138 ColorQuote = (int *) safe_malloc (COLOR_QUOTE_INIT * sizeof (int));
139 memset (ColorQuote, A_NORMAL, sizeof (int) * COLOR_QUOTE_INIT);
140 ColorQuoteSize = COLOR_QUOTE_INIT;
143 /* set some defaults */
144 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
145 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
146 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
147 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
148 /* special meaning: toggle the relevant attribute */
149 ColorDefs[MT_COLOR_BOLD] = 0;
150 ColorDefs[MT_COLOR_UNDERLINE] = 0;
159 #ifdef USE_SLANG_CURSES
160 static char *get_color_name (char *dest, size_t destlen, int val)
162 static char * missing[3] = {"brown", "lightgray", "default"};
168 strfcpy (dest, missing[0], destlen);
172 strfcpy (dest, missing[1], destlen);
176 strfcpy (dest, missing[2], destlen);
180 for (i = 0; Colors[i].name; i++)
182 if (Colors[i].value == val)
184 strfcpy (dest, Colors[i].name, destlen);
189 /* Sigh. If we got this far, the color is of the form 'colorN'
190 * Slang can handle this itself, so just return 'colorN'
193 snprintf (dest, destlen, "color%d", val);
198 int mutt_alloc_color (int fg, int bg)
200 COLOR_LIST *p = ColorList;
203 #if defined (USE_SLANG_CURSES)
204 char fgc[SHORT_STRING], bgc[SHORT_STRING];
207 /* check to see if this color is already allocated to save space */
210 if (p->fg == fg && p->bg == bg)
213 return (COLOR_PAIR (p->index));
218 /* check to see if there are colors left */
219 if (++UserColors > COLOR_PAIRS) return (A_NORMAL);
221 /* find the smallest available index (object) */
228 if (p->index == i) break;
231 if (p == NULL) break;
235 p = (COLOR_LIST *) safe_malloc (sizeof (COLOR_LIST));
244 #if defined (USE_SLANG_CURSES)
245 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
246 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg), get_color_name (bgc, sizeof (bgc), bg));
248 #elif defined (HAVE_USE_DEFAULT_COLORS)
249 if (fg == COLOR_DEFAULT)
251 if (bg == COLOR_DEFAULT)
255 init_pair(i, fg, bg);
257 dprint (3, (debugfile,"mutt_alloc_color(): Color pairs used so far: %d\n",
260 return (COLOR_PAIR (p->index));
263 void mutt_free_color (int fg, int bg)
270 if (p->fg == fg && p->bg == bg)
273 if (p->count > 0) return;
276 dprint(1,(debugfile,"mutt_free_color(): Color pairs used so far: %d\n",
281 ColorList = ColorList->next;
302 #endif /* HAVE_COLOR */
308 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER *err)
312 if (ascii_strncasecmp (s, "bright", 6) == 0)
318 /* allow aliases for xterm color resources */
319 if (ascii_strncasecmp (s, "color", 5) == 0)
322 *col = strtol (s, &eptr, 10);
323 if (!*s || *eptr || *col < 0 ||
324 (*col >= COLORS && !option(OPTNOCURSES) && has_colors()))
326 snprintf (err->data, err->dsize, _("%s: color not supported by term"), s);
330 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1)
332 snprintf (err->data, err->dsize, _("%s: no such color"), s);
342 /* usage: uncolor index pattern [pattern...]
343 * unmono index pattern [pattern...]
347 _mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err,
348 short parse_uncolor);
353 int mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data,
356 return _mutt_parse_uncolor(buf, s, data, err, 1);
361 int mutt_parse_unmono (BUFFER *buf, BUFFER *s, unsigned long data,
364 return _mutt_parse_uncolor(buf, s, data, err, 0);
368 _mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err,
371 int object = 0, do_cache = 0;
372 COLOR_LINE *tmp, *last = NULL;
374 mutt_extract_token (buf, s, 0);
376 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1)
378 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
382 if (mutt_strncmp (buf->data, "index", 5) != 0)
384 snprintf (err->data, err->dsize,
385 _("%s: command valid only for index object"),
386 parse_uncolor ? "uncolor" : "unmono");
392 snprintf (err->data, err->dsize,
393 _("%s: too few arguments"), parse_uncolor ? "uncolor" : "unmono");
399 /* we're running without curses */
401 || /* we're parsing an uncolor command, and have no colors */
402 (parse_uncolor && !has_colors())
403 /* we're parsing an unmono command, and have colors */
404 || (!parse_uncolor && has_colors())
406 /* We don't even have colors compiled in */
411 /* just eat the command, but don't do anything real about it */
413 mutt_extract_token (buf, s, 0);
414 while (MoreArgs (s));
422 mutt_extract_token (buf, s, 0);
423 if (!mutt_strcmp ("*", buf->data))
425 for (tmp = ColorIndexList; tmp; )
431 mutt_free_color_line(&last, parse_uncolor);
433 ColorIndexList = NULL;
437 for (last = NULL, tmp = ColorIndexList; tmp; last = tmp, tmp = tmp->next)
439 if (!mutt_strcmp (buf->data, tmp->pattern))
443 dprint(1,(debugfile,"Freeing pattern \"%s\" from ColorIndexList\n",
446 last->next = tmp->next;
448 ColorIndexList = tmp->next;
449 mutt_free_color_line(&tmp, parse_uncolor);
455 while (MoreArgs (s));
458 if (do_cache && !option (OPTNOCURSES))
461 set_option (OPTFORCEREDRAWINDEX);
462 /* force re-caching of index colors */
463 for (i = 0; Context && i < Context->msgcount; i++)
464 Context->hdrs[i]->pair = 0;
471 add_pattern (COLOR_LINE **top, const char *s, int sensitive,
472 int fg, int bg, int attr, BUFFER *err,
476 /* is_index used to store compiled pattern
477 * only for `index' color object
478 * when called from mutt_parse_color() */
480 COLOR_LINE *tmp = *top;
486 if (mutt_strcmp (s, tmp->pattern) == 0)
491 if (mutt_strcasecmp (s, tmp->pattern) == 0)
500 if (fg != -1 && bg != -1)
502 if (tmp->fg != fg || tmp->bg != bg)
504 mutt_free_color (tmp->fg, tmp->bg);
507 attr |= mutt_alloc_color (fg, bg);
510 attr |= (tmp->pair & ~A_BOLD);
512 #endif /* HAVE_COLOR */
518 char buf[LONG_STRING];
520 tmp = mutt_new_color_line ();
525 strfcpy(buf, NONULL(s), sizeof(buf));
526 mutt_check_simple (buf, sizeof (buf), NONULL(SimpleSearch));
527 if((tmp->color_pattern = mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL)
529 mutt_free_color_line(&tmp, 1);
532 /* force re-caching of index colors */
533 for (i = 0; Context && i < Context->msgcount; i++)
534 Context->hdrs[i]->pair = 0;
536 else if ((r = REGCOMP (&tmp->rx, s, (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0)
538 regerror (r, &tmp->rx, err->data, err->dsize);
539 mutt_free_color_line(&tmp, 1);
543 tmp->pattern = safe_strdup (s);
545 if(fg != -1 && bg != -1)
549 attr |= mutt_alloc_color (fg, bg);
560 parse_object(BUFFER *buf, BUFFER *s, int *o, int *ql, BUFFER *err)
567 strfcpy(err->data, _("Missing arguments."), err->dsize);
571 mutt_extract_token(buf, s, 0);
572 if(!mutt_strncmp(buf->data, "quoted", 6))
576 *ql = strtol(buf->data + 6, &eptr, 10);
577 if(*eptr || q_level < 0)
579 snprintf(err->data, err->dsize, _("%s: no such object"), buf->data);
586 *o = MT_COLOR_QUOTED;
588 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1)
590 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
597 typedef int (*parser_callback_t)(BUFFER *, BUFFER *, int *, int *, int *, BUFFER *);
602 parse_color_pair(BUFFER *buf, BUFFER *s, int *fg, int *bg, int *attr, BUFFER *err)
606 strfcpy (err->data, _("color: too few arguments"), err->dsize);
610 mutt_extract_token (buf, s, 0);
612 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
617 strfcpy (err->data, _("color: too few arguments"), err->dsize);
621 mutt_extract_token (buf, s, 0);
623 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
632 parse_attr_spec(BUFFER *buf, BUFFER *s, int *fg, int *bg, int *attr, BUFFER *err)
640 strfcpy (err->data, _("mono: too few arguments"), err->dsize);
644 mutt_extract_token (buf, s, 0);
646 if (ascii_strcasecmp ("bold", buf->data) == 0)
648 else if (ascii_strcasecmp ("underline", buf->data) == 0)
649 *attr |= A_UNDERLINE;
650 else if (ascii_strcasecmp ("none", buf->data) == 0)
652 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
654 else if (ascii_strcasecmp ("standout", buf->data) == 0)
656 else if (ascii_strcasecmp ("normal", buf->data) == 0)
657 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
660 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
667 static int fgbgattr_to_color(int fg, int bg, int attr)
670 if(fg != -1 && bg != -1)
671 return attr | mutt_alloc_color(fg, bg);
677 /* usage: color <object> <fg> <bg> [ <regexp> ]
678 * mono <object> <attr> [ <regexp> ]
682 _mutt_parse_color (BUFFER *buf, BUFFER *s, BUFFER *err,
683 parser_callback_t callback, short dry_run)
685 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
688 if(parse_object(buf, s, &object, &q_level, err) == -1)
691 if(callback(buf, s, &fg, &bg, &attr, err) == -1)
694 /* extract a regular expression if needed */
696 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY || object == MT_COLOR_INDEX)
700 strfcpy (err->data, _("too few arguments"), err->dsize);
704 mutt_extract_token (buf, s, 0);
709 strfcpy (err->data, _("too many arguments"), err->dsize);
715 if(dry_run) return 0;
719 # ifdef HAVE_USE_DEFAULT_COLORS
720 if (!option (OPTNOCURSES) && has_colors()
721 /* delay use_default_colors() until needed, since it initializes things */
722 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
723 && use_default_colors () != OK)
725 strfcpy (err->data, _("default colors not supported"), err->dsize);
728 # endif /* HAVE_USE_DEFAULT_COLORS */
731 if (object == MT_COLOR_HEADER)
732 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err,0);
733 else if (object == MT_COLOR_BODY)
734 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
735 else if (object == MT_COLOR_INDEX)
737 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
738 set_option (OPTFORCEREDRAWINDEX);
740 else if (object == MT_COLOR_QUOTED)
742 if (q_level >= ColorQuoteSize)
744 safe_realloc (&ColorQuote, (ColorQuoteSize += 2) * sizeof (int));
745 ColorQuote[ColorQuoteSize-2] = ColorDefs[MT_COLOR_QUOTED];
746 ColorQuote[ColorQuoteSize-1] = ColorDefs[MT_COLOR_QUOTED];
748 if (q_level >= ColorQuoteUsed)
749 ColorQuoteUsed = q_level + 1;
752 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color(fg, bg, attr);
754 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
755 for (q_level = 1; q_level < ColorQuoteUsed; q_level++)
757 if (ColorQuote[q_level] == A_NORMAL)
758 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
762 ColorQuote[q_level] = fgbgattr_to_color(fg, bg, attr);
765 ColorDefs[object] = fgbgattr_to_color(fg, bg, attr);
769 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors())
770 BKGDSET (MT_COLOR_NORMAL);
779 int mutt_parse_color(BUFFER *buff, BUFFER *s, unsigned long data, BUFFER *err)
783 if(option(OPTNOCURSES) || !has_colors())
786 return _mutt_parse_color(buff, s, err, parse_color_pair, dry_run);
791 int mutt_parse_mono(BUFFER *buff, BUFFER *s, unsigned long data, BUFFER *err)
796 if(option(OPTNOCURSES) || has_colors())
799 if(option(OPTNOCURSES))
803 return _mutt_parse_color(buff, s, err, parse_attr_spec, dry_run);