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 },
97 { "sidebar_flagged", MT_COLOR_FLAGGED },
101 #define COLOR_QUOTE_INIT 8
103 static COLOR_LINE *mutt_new_color_line (void)
105 COLOR_LINE *p = safe_calloc (1, sizeof (COLOR_LINE));
112 static void mutt_free_color_line(COLOR_LINE **l,
123 if(free_colors && tmp->fg != -1 && tmp->bg != -1)
124 mutt_free_color(tmp->fg, tmp->bg);
127 /* we should really introduce a container
128 * type for regular expressions.
132 mutt_pattern_free(&tmp->color_pattern);
133 FREE (&tmp->pattern);
137 void ci_start_color (void)
139 memset (ColorDefs, A_NORMAL, sizeof (int) * MT_COLOR_MAX);
140 ColorQuote = (int *) safe_malloc (COLOR_QUOTE_INIT * sizeof (int));
141 memset (ColorQuote, A_NORMAL, sizeof (int) * COLOR_QUOTE_INIT);
142 ColorQuoteSize = COLOR_QUOTE_INIT;
145 /* set some defaults */
146 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
147 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
148 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
149 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
150 /* special meaning: toggle the relevant attribute */
151 ColorDefs[MT_COLOR_BOLD] = 0;
152 ColorDefs[MT_COLOR_UNDERLINE] = 0;
161 #ifdef USE_SLANG_CURSES
162 static char *get_color_name (char *dest, size_t destlen, int val)
164 static char * missing[3] = {"brown", "lightgray", "default"};
170 strfcpy (dest, missing[0], destlen);
174 strfcpy (dest, missing[1], destlen);
178 strfcpy (dest, missing[2], destlen);
182 for (i = 0; Colors[i].name; i++)
184 if (Colors[i].value == val)
186 strfcpy (dest, Colors[i].name, destlen);
191 /* Sigh. If we got this far, the color is of the form 'colorN'
192 * Slang can handle this itself, so just return 'colorN'
195 snprintf (dest, destlen, "color%d", val);
200 int mutt_alloc_color (int fg, int bg)
202 COLOR_LIST *p = ColorList;
205 #if defined (USE_SLANG_CURSES)
206 char fgc[SHORT_STRING], bgc[SHORT_STRING];
209 /* check to see if this color is already allocated to save space */
212 if (p->fg == fg && p->bg == bg)
215 return (COLOR_PAIR (p->index));
220 /* check to see if there are colors left */
221 if (++UserColors > COLOR_PAIRS) return (A_NORMAL);
223 /* find the smallest available index (object) */
230 if (p->index == i) break;
233 if (p == NULL) break;
237 p = (COLOR_LIST *) safe_malloc (sizeof (COLOR_LIST));
246 #if defined (USE_SLANG_CURSES)
247 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
248 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg), get_color_name (bgc, sizeof (bgc), bg));
250 #elif defined (HAVE_USE_DEFAULT_COLORS)
251 if (fg == COLOR_DEFAULT)
253 if (bg == COLOR_DEFAULT)
257 init_pair(i, fg, bg);
259 dprint(1,(debugfile,"mutt_alloc_color(): Color pairs used so far: %d\n",
262 return (COLOR_PAIR (p->index));
265 void mutt_free_color (int fg, int bg)
272 if (p->fg == fg && p->bg == bg)
275 if (p->count > 0) return;
278 dprint(1,(debugfile,"mutt_free_color(): Color pairs used so far: %d\n",
283 ColorList = ColorList->next;
304 #endif /* HAVE_COLOR */
310 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER *err)
314 if (mutt_strncasecmp (s, "bright", 6) == 0)
320 /* allow aliases for xterm color resources */
321 if (mutt_strncasecmp (s, "color", 5) == 0)
324 *col = strtol (s, &eptr, 10);
325 if (!*s || *eptr || *col < 0 ||
326 (*col >= COLORS && !option(OPTNOCURSES) && has_colors()))
328 snprintf (err->data, err->dsize, _("%s: color not supported by term"), s);
332 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1)
334 snprintf (err->data, err->dsize, _("%s: no such color"), s);
344 /* usage: uncolor index pattern [pattern...]
345 * unmono index pattern [pattern...]
349 _mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err,
350 short parse_uncolor);
355 int mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data,
358 return _mutt_parse_uncolor(buf, s, data, err, 1);
363 int mutt_parse_unmono (BUFFER *buf, BUFFER *s, unsigned long data,
366 return _mutt_parse_uncolor(buf, s, data, err, 0);
370 _mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err,
373 int object = 0, do_cache = 0;
374 COLOR_LINE *tmp, *last = NULL;
376 mutt_extract_token (buf, s, 0);
378 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1)
380 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
384 if (mutt_strncmp (buf->data, "index", 5) != 0)
386 snprintf (err->data, err->dsize,
387 _("%s: command valid only for index object"),
388 parse_uncolor ? "uncolor" : "unmono");
394 snprintf (err->data, err->dsize,
395 _("%s: too few arguments"), parse_uncolor ? "uncolor" : "unmono");
401 /* we're running without curses */
403 || /* we're parsing an uncolor command, and have no colors */
404 (parse_uncolor && !has_colors())
405 /* we're parsing an unmono command, and have colors */
406 || (!parse_uncolor && has_colors())
408 /* We don't even have colors compiled in */
413 /* just eat the command, but don't do anything real about it */
415 mutt_extract_token (buf, s, 0);
416 while (MoreArgs (s));
424 mutt_extract_token (buf, s, 0);
425 if (!mutt_strcmp ("*", buf->data))
427 for (tmp = ColorIndexList; tmp; )
433 mutt_free_color_line(&last, parse_uncolor);
435 ColorIndexList = NULL;
439 for (last = NULL, tmp = ColorIndexList; tmp; last = tmp, tmp = tmp->next)
441 if (!mutt_strcmp (buf->data, tmp->pattern))
445 dprint(1,(debugfile,"Freeing pattern \"%s\" from ColorIndexList\n",
448 last->next = tmp->next;
450 ColorIndexList = tmp->next;
451 mutt_free_color_line(&tmp, parse_uncolor);
457 while (MoreArgs (s));
460 if (do_cache && !option (OPTNOCURSES))
463 set_option (OPTFORCEREDRAWINDEX);
464 /* force re-caching of index colors */
465 for (i = 0; Context && i < Context->msgcount; i++)
466 Context->hdrs[i]->pair = 0;
473 add_pattern (COLOR_LINE **top, const char *s, int sensitive,
474 int fg, int bg, int attr, BUFFER *err,
478 /* is_index used to store compiled pattern
479 * only for `index' color object
480 * when called from mutt_parse_color() */
482 COLOR_LINE *tmp = *top;
488 if (mutt_strcmp (s, tmp->pattern) == 0)
493 if (mutt_strcasecmp (s, tmp->pattern) == 0)
502 if (fg != -1 && bg != -1)
504 if (tmp->fg != fg || tmp->bg != bg)
506 mutt_free_color (tmp->fg, tmp->bg);
509 attr |= mutt_alloc_color (fg, bg);
512 attr |= (tmp->pair & ~A_BOLD);
514 #endif /* HAVE_COLOR */
522 tmp = mutt_new_color_line ();
527 strfcpy(buf, NONULL(s), sizeof(buf));
528 mutt_check_simple (buf, sizeof (buf), NONULL(SimpleSearch));
529 if((tmp->color_pattern = mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL)
531 mutt_free_color_line(&tmp, 1);
534 /* force re-caching of index colors */
535 for (i = 0; Context && i < Context->msgcount; i++)
536 Context->hdrs[i]->pair = 0;
538 else if ((r = REGCOMP (&tmp->rx, s, (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0)
540 regerror (r, &tmp->rx, err->data, err->dsize);
541 mutt_free_color_line(&tmp, 1);
545 tmp->pattern = safe_strdup (s);
547 if(fg != -1 && bg != -1)
551 attr |= mutt_alloc_color (fg, bg);
562 parse_object(BUFFER *buf, BUFFER *s, int *o, int *ql, BUFFER *err)
569 strfcpy(err->data, _("Missing arguments."), err->dsize);
573 mutt_extract_token(buf, s, 0);
574 if(!mutt_strncmp(buf->data, "quoted", 6))
578 *ql = strtol(buf->data + 6, &eptr, 10);
579 if(*eptr || q_level < 0)
581 snprintf(err->data, err->dsize, _("%s: no such object"), buf->data);
588 *o = MT_COLOR_QUOTED;
590 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1)
592 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
599 typedef int (*parser_callback_t)(BUFFER *, BUFFER *, int *, int *, int *, BUFFER *);
604 parse_color_pair(BUFFER *buf, BUFFER *s, int *fg, int *bg, int *attr, BUFFER *err)
608 strfcpy (err->data, _("color: too few arguments"), err->dsize);
612 mutt_extract_token (buf, s, 0);
614 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
619 strfcpy (err->data, _("color: too few arguments"), err->dsize);
623 mutt_extract_token (buf, s, 0);
625 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
634 parse_attr_spec(BUFFER *buf, BUFFER *s, int *fg, int *bg, int *attr, BUFFER *err)
642 strfcpy (err->data, _("mono: too few arguments"), err->dsize);
646 mutt_extract_token (buf, s, 0);
648 if (ascii_strcasecmp ("bold", buf->data) == 0)
650 else if (ascii_strcasecmp ("underline", buf->data) == 0)
651 *attr |= A_UNDERLINE;
652 else if (ascii_strcasecmp ("none", buf->data) == 0)
654 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
656 else if (ascii_strcasecmp ("standout", buf->data) == 0)
658 else if (ascii_strcasecmp ("normal", buf->data) == 0)
659 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
662 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
669 static int fgbgattr_to_color(int fg, int bg, int attr)
672 if(fg != -1 && bg != -1)
673 return attr | mutt_alloc_color(fg, bg);
679 /* usage: color <object> <fg> <bg> [ <regexp> ]
680 * mono <object> <attr> [ <regexp> ]
684 _mutt_parse_color (BUFFER *buf, BUFFER *s, BUFFER *err,
685 parser_callback_t callback, short dry_run)
687 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
690 if(parse_object(buf, s, &object, &q_level, err) == -1)
693 if(callback(buf, s, &fg, &bg, &attr, err) == -1)
696 /* extract a regular expression if needed */
698 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY || object == MT_COLOR_INDEX)
702 strfcpy (err->data, _("too few arguments"), err->dsize);
706 mutt_extract_token (buf, s, 0);
711 strfcpy (err->data, _("too many arguments"), err->dsize);
717 if(dry_run) return 0;
721 # ifdef HAVE_USE_DEFAULT_COLORS
722 if (!option (OPTNOCURSES) && has_colors()
723 /* delay use_default_colors() until needed, since it initializes things */
724 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
725 && use_default_colors () != OK)
727 strfcpy (err->data, _("default colors not supported"), err->dsize);
730 # endif /* HAVE_USE_DEFAULT_COLORS */
733 if (object == MT_COLOR_HEADER)
734 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err,0);
735 else if (object == MT_COLOR_BODY)
736 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
737 else if (object == MT_COLOR_INDEX)
739 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
740 set_option (OPTFORCEREDRAWINDEX);
742 else if (object == MT_COLOR_QUOTED)
744 if (q_level >= ColorQuoteSize)
746 safe_realloc (&ColorQuote, (ColorQuoteSize += 2) * sizeof (int));
747 ColorQuote[ColorQuoteSize-2] = ColorDefs[MT_COLOR_QUOTED];
748 ColorQuote[ColorQuoteSize-1] = ColorDefs[MT_COLOR_QUOTED];
750 if (q_level >= ColorQuoteUsed)
751 ColorQuoteUsed = q_level + 1;
754 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color(fg, bg, attr);
756 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
757 for (q_level = 1; q_level < ColorQuoteUsed; q_level++)
759 if (ColorQuote[q_level] == A_NORMAL)
760 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
764 ColorQuote[q_level] = fgbgattr_to_color(fg, bg, attr);
767 ColorDefs[object] = fgbgattr_to_color(fg, bg, attr);
771 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors())
772 BKGDSET (MT_COLOR_NORMAL);
781 int mutt_parse_color(BUFFER *buff, BUFFER *s, unsigned long data, BUFFER *err)
785 if(option(OPTNOCURSES) || !has_colors())
788 return _mutt_parse_color(buff, s, err, parse_color_pair, dry_run);
793 int mutt_parse_mono(BUFFER *buff, BUFFER *s, unsigned long data, BUFFER *err)
798 if(option(OPTNOCURSES) || has_colors())
801 if(option(OPTNOCURSES))
805 return _mutt_parse_color(buff, s, err, parse_attr_spec, dry_run);