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 },
92 { "sidebar_new", MT_COLOR_NEW },
96 #define COLOR_QUOTE_INIT 8
98 static COLOR_LINE *mutt_new_color_line (void)
100 COLOR_LINE *p = safe_calloc (1, sizeof (COLOR_LINE));
107 static void mutt_free_color_line(COLOR_LINE **l,
118 if(free_colors && tmp->fg != -1 && tmp->bg != -1)
119 mutt_free_color(tmp->fg, tmp->bg);
122 /* we should really introduce a container
123 * type for regular expressions.
127 mutt_pattern_free(&tmp->color_pattern);
128 FREE (&tmp->pattern);
132 void ci_start_color (void)
134 memset (ColorDefs, A_NORMAL, sizeof (int) * MT_COLOR_MAX);
135 ColorQuote = (int *) safe_malloc (COLOR_QUOTE_INIT * sizeof (int));
136 memset (ColorQuote, A_NORMAL, sizeof (int) * COLOR_QUOTE_INIT);
137 ColorQuoteSize = COLOR_QUOTE_INIT;
140 /* set some defaults */
141 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
142 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
143 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
144 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
145 /* special meaning: toggle the relevant attribute */
146 ColorDefs[MT_COLOR_BOLD] = 0;
147 ColorDefs[MT_COLOR_UNDERLINE] = 0;
156 #ifdef USE_SLANG_CURSES
157 static char *get_color_name (char *dest, size_t destlen, int val)
159 static char * missing[3] = {"brown", "lightgray", ""};
165 strfcpy (dest, missing[0], destlen);
169 strfcpy (dest, missing[1], destlen);
173 strfcpy (dest, missing[2], destlen);
177 for (i = 0; Colors[i].name; i++)
179 if (Colors[i].value == val)
181 strfcpy (dest, Colors[i].name, destlen);
186 /* Sigh. If we got this far, the color is of the form 'colorN'
187 * Slang can handle this itself, so just return 'colorN'
190 snprintf (dest, destlen, "color%d", val);
195 int mutt_alloc_color (int fg, int bg)
197 COLOR_LIST *p = ColorList;
200 #if defined (USE_SLANG_CURSES)
201 char fgc[SHORT_STRING], bgc[SHORT_STRING];
204 /* check to see if this color is already allocated to save space */
207 if (p->fg == fg && p->bg == bg)
210 return (COLOR_PAIR (p->index));
215 /* check to see if there are colors left */
216 if (++UserColors > COLOR_PAIRS) return (A_NORMAL);
218 /* find the smallest available index (object) */
225 if (p->index == i) break;
228 if (p == NULL) break;
232 p = (COLOR_LIST *) safe_malloc (sizeof (COLOR_LIST));
241 #if defined (USE_SLANG_CURSES)
242 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
243 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg), get_color_name (bgc, sizeof (bgc), bg));
245 #elif defined (HAVE_USE_DEFAULT_COLORS)
246 if (fg == COLOR_DEFAULT)
248 if (bg == COLOR_DEFAULT)
252 init_pair(i, fg, bg);
254 dprint(1,(debugfile,"mutt_alloc_color(): Color pairs used so far: %d\n",
257 return (COLOR_PAIR (p->index));
260 void mutt_free_color (int fg, int bg)
267 if (p->fg == fg && p->bg == bg)
270 if (p->count > 0) return;
273 dprint(1,(debugfile,"mutt_free_color(): Color pairs used so far: %d\n",
278 ColorList = ColorList->next;
299 #endif /* HAVE_COLOR */
305 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER *err)
309 if (mutt_strncasecmp (s, "bright", 6) == 0)
315 /* allow aliases for xterm color resources */
316 if (mutt_strncasecmp (s, "color", 5) == 0)
319 *col = strtol (s, &eptr, 10);
320 if (!*s || *eptr || *col < 0 ||
321 (*col >= COLORS && !option(OPTNOCURSES) && has_colors()))
323 snprintf (err->data, err->dsize, _("%s: color not supported by term"), s);
327 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1)
329 snprintf (err->data, err->dsize, _("%s: no such color"), s);
339 /* usage: uncolor index pattern [pattern...]
340 * unmono index pattern [pattern...]
344 _mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err,
345 short parse_uncolor);
350 int mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data,
353 return _mutt_parse_uncolor(buf, s, data, err, 1);
358 int mutt_parse_unmono (BUFFER *buf, BUFFER *s, unsigned long data,
361 return _mutt_parse_uncolor(buf, s, data, err, 0);
365 _mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err,
368 int object = 0, do_cache = 0;
369 COLOR_LINE *tmp, *last = NULL;
371 mutt_extract_token (buf, s, 0);
373 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1)
375 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
379 if (mutt_strncmp (buf->data, "index", 5) != 0)
381 snprintf (err->data, err->dsize,
382 _("%s: command valid only for index object"),
383 parse_uncolor ? "uncolor" : "unmono");
389 snprintf (err->data, err->dsize,
390 _("%s: too few arguments"), parse_uncolor ? "uncolor" : "unmono");
396 /* we're running without curses */
398 || /* we're parsing an uncolor command, and have no colors */
399 (parse_uncolor && !has_colors())
400 /* we're parsing an unmono command, and have colors */
401 || (!parse_uncolor && has_colors())
403 /* We don't even have colors compiled in */
408 /* just eat the command, but don't do anything real about it */
410 mutt_extract_token (buf, s, 0);
411 while (MoreArgs (s));
419 mutt_extract_token (buf, s, 0);
420 if (!mutt_strcmp ("*", buf->data))
422 for (tmp = ColorIndexList; tmp; )
428 mutt_free_color_line(&last, parse_uncolor);
430 ColorIndexList = NULL;
434 for (last = NULL, tmp = ColorIndexList; tmp; last = tmp, tmp = tmp->next)
436 if (!mutt_strcmp (buf->data, tmp->pattern))
440 dprint(1,(debugfile,"Freeing pattern \"%s\" from ColorIndexList\n",
443 last->next = tmp->next;
445 ColorIndexList = tmp->next;
446 mutt_free_color_line(&tmp, parse_uncolor);
452 while (MoreArgs (s));
455 if (do_cache && !option (OPTNOCURSES))
458 set_option (OPTFORCEREDRAWINDEX);
459 /* force re-caching of index colors */
460 for (i = 0; Context && i < Context->msgcount; i++)
461 Context->hdrs[i]->pair = 0;
468 add_pattern (COLOR_LINE **top, const char *s, int sensitive,
469 int fg, int bg, int attr, BUFFER *err,
473 /* is_index used to store compiled pattern
474 * only for `index' color object
475 * when called from mutt_parse_color() */
477 COLOR_LINE *tmp = *top;
483 if (mutt_strcmp (s, tmp->pattern) == 0)
488 if (mutt_strcasecmp (s, tmp->pattern) == 0)
497 if (fg != -1 && bg != -1)
499 if (tmp->fg != fg || tmp->bg != bg)
501 mutt_free_color (tmp->fg, tmp->bg);
504 attr |= mutt_alloc_color (fg, bg);
507 attr |= (tmp->pair & ~A_BOLD);
509 #endif /* HAVE_COLOR */
517 tmp = mutt_new_color_line ();
522 strfcpy(buf, NONULL(s), sizeof(buf));
523 mutt_check_simple (buf, sizeof (buf), NONULL(SimpleSearch));
524 if((tmp->color_pattern = mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL)
526 mutt_free_color_line(&tmp, 1);
529 /* force re-caching of index colors */
530 for (i = 0; Context && i < Context->msgcount; i++)
531 Context->hdrs[i]->pair = 0;
533 else if ((r = REGCOMP (&tmp->rx, s, (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0)
535 regerror (r, &tmp->rx, err->data, err->dsize);
536 mutt_free_color_line(&tmp, 1);
540 tmp->pattern = safe_strdup (s);
542 if(fg != -1 && bg != -1)
546 attr |= mutt_alloc_color (fg, bg);
557 parse_object(BUFFER *buf, BUFFER *s, int *o, int *ql, BUFFER *err)
564 strfcpy(err->data, _("Missing arguments."), err->dsize);
568 mutt_extract_token(buf, s, 0);
569 if(!mutt_strncmp(buf->data, "quoted", 6))
573 *ql = strtol(buf->data + 6, &eptr, 10);
574 if(*eptr || q_level < 0)
576 snprintf(err->data, err->dsize, _("%s: no such object"), buf->data);
583 *o = MT_COLOR_QUOTED;
585 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1)
587 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
594 typedef int (*parser_callback_t)(BUFFER *, BUFFER *, int *, int *, int *, BUFFER *);
599 parse_color_pair(BUFFER *buf, BUFFER *s, int *fg, int *bg, int *attr, BUFFER *err)
603 strfcpy (err->data, _("color: too few arguments"), err->dsize);
607 mutt_extract_token (buf, s, 0);
609 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
614 strfcpy (err->data, _("color: too few arguments"), err->dsize);
618 mutt_extract_token (buf, s, 0);
620 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
629 parse_attr_spec(BUFFER *buf, BUFFER *s, int *fg, int *bg, int *attr, BUFFER *err)
637 strfcpy (err->data, _("mono: too few arguments"), err->dsize);
641 mutt_extract_token (buf, s, 0);
643 if (ascii_strcasecmp ("bold", buf->data) == 0)
645 else if (ascii_strcasecmp ("underline", buf->data) == 0)
646 *attr |= A_UNDERLINE;
647 else if (ascii_strcasecmp ("none", buf->data) == 0)
649 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
651 else if (ascii_strcasecmp ("standout", buf->data) == 0)
653 else if (ascii_strcasecmp ("normal", buf->data) == 0)
654 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
657 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
664 static int fgbgattr_to_color(int fg, int bg, int attr)
667 if(fg != -1 && bg != -1)
668 return attr | mutt_alloc_color(fg, bg);
674 /* usage: color <object> <fg> <bg> [ <regexp> ]
675 * mono <object> <attr> [ <regexp> ]
679 _mutt_parse_color (BUFFER *buf, BUFFER *s, BUFFER *err,
680 parser_callback_t callback, short dry_run)
682 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
685 if(parse_object(buf, s, &object, &q_level, err) == -1)
688 if(callback(buf, s, &fg, &bg, &attr, err) == -1)
691 /* extract a regular expression if needed */
693 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY || object == MT_COLOR_INDEX)
697 strfcpy (err->data, _("too few arguments"), err->dsize);
701 mutt_extract_token (buf, s, 0);
706 strfcpy (err->data, _("too many arguments"), err->dsize);
712 if(dry_run) return 0;
716 # ifdef HAVE_USE_DEFAULT_COLORS
717 if (!option (OPTNOCURSES) && has_colors()
718 /* delay use_default_colors() until needed, since it initializes things */
719 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
720 && use_default_colors () != OK)
722 strfcpy (err->data, _("default colors not supported"), err->dsize);
725 # endif /* HAVE_USE_DEFAULT_COLORS */
728 if (object == MT_COLOR_HEADER)
729 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err,0);
730 else if (object == MT_COLOR_BODY)
731 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
732 else if (object == MT_COLOR_INDEX)
734 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
735 set_option (OPTFORCEREDRAWINDEX);
737 else if (object == MT_COLOR_QUOTED)
739 if (q_level >= ColorQuoteSize)
741 safe_realloc (&ColorQuote, (ColorQuoteSize += 2) * sizeof (int));
742 ColorQuote[ColorQuoteSize-2] = ColorDefs[MT_COLOR_QUOTED];
743 ColorQuote[ColorQuoteSize-1] = ColorDefs[MT_COLOR_QUOTED];
745 if (q_level >= ColorQuoteUsed)
746 ColorQuoteUsed = q_level + 1;
749 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color(fg, bg, attr);
751 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
752 for (q_level = 1; q_level < ColorQuoteUsed; q_level++)
754 if (ColorQuote[q_level] == A_NORMAL)
755 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
759 ColorQuote[q_level] = fgbgattr_to_color(fg, bg, attr);
762 ColorDefs[object] = fgbgattr_to_color(fg, bg, attr);
766 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors())
767 BKGDSET (MT_COLOR_NORMAL);
776 int mutt_parse_color(BUFFER *buff, BUFFER *s, unsigned long data, BUFFER *err)
780 if(option(OPTNOCURSES) || !has_colors())
783 return _mutt_parse_color(buff, s, err, parse_color_pair, dry_run);
788 int mutt_parse_mono(BUFFER *buff, BUFFER *s, unsigned long data, BUFFER *err)
793 if(option(OPTNOCURSES) || has_colors())
796 if(option(OPTNOCURSES))
800 return _mutt_parse_color(buff, s, err, parse_attr_spec, dry_run);