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);
367 static int _mutt_parse_uncolor (BUFFER *buf, BUFFER *s, unsigned long data,
368 BUFFER *err, short parse_uncolor)
370 int object = 0, do_cache = 0;
371 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)
383 list = &ColorIndexList;
384 else if (mutt_strncmp (buf->data, "body", 4) == 0)
385 list = &ColorBodyList;
386 else if (mutt_strncmp (buf->data, "header", 7) == 0)
387 list = &ColorHdrList;
390 snprintf (err->data, err->dsize,
391 _("%s: command valid only for index, body, header objects"),
392 parse_uncolor ? "uncolor" : "unmono");
398 snprintf (err->data, err->dsize,
399 _("%s: too few arguments"), parse_uncolor ? "uncolor" : "unmono");
405 /* we're running without curses */
407 || /* we're parsing an uncolor command, and have no colors */
408 (parse_uncolor && !has_colors())
409 /* we're parsing an unmono command, and have colors */
410 || (!parse_uncolor && has_colors())
412 /* We don't even have colors compiled in */
417 /* just eat the command, but don't do anything real about it */
419 mutt_extract_token (buf, s, 0);
420 while (MoreArgs (s));
427 mutt_extract_token (buf, s, 0);
428 if (!mutt_strcmp ("*", buf->data))
430 for (tmp = *list; tmp; )
436 mutt_free_color_line(&last, parse_uncolor);
442 for (last = NULL, tmp = *list; tmp; last = tmp, tmp = tmp->next)
444 if (!mutt_strcmp (buf->data, tmp->pattern))
448 dprint(1,(debugfile,"Freeing pattern \"%s\" from color list\n",
451 last->next = tmp->next;
454 mutt_free_color_line(&tmp, parse_uncolor);
460 while (MoreArgs (s));
463 if (do_cache && !option (OPTNOCURSES))
466 set_option (OPTFORCEREDRAWINDEX);
467 /* force re-caching of index colors */
468 for (i = 0; Context && i < Context->msgcount; i++)
469 Context->hdrs[i]->pair = 0;
476 add_pattern (COLOR_LINE **top, const char *s, int sensitive,
477 int fg, int bg, int attr, BUFFER *err,
481 /* is_index used to store compiled pattern
482 * only for `index' color object
483 * when called from mutt_parse_color() */
485 COLOR_LINE *tmp = *top;
491 if (mutt_strcmp (s, tmp->pattern) == 0)
496 if (mutt_strcasecmp (s, tmp->pattern) == 0)
505 if (fg != -1 && bg != -1)
507 if (tmp->fg != fg || tmp->bg != bg)
509 mutt_free_color (tmp->fg, tmp->bg);
512 attr |= mutt_alloc_color (fg, bg);
515 attr |= (tmp->pair & ~A_BOLD);
517 #endif /* HAVE_COLOR */
523 char buf[LONG_STRING];
525 tmp = mutt_new_color_line ();
530 strfcpy(buf, NONULL(s), sizeof(buf));
531 mutt_check_simple (buf, sizeof (buf), NONULL(SimpleSearch));
532 if((tmp->color_pattern = mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL)
534 mutt_free_color_line(&tmp, 1);
537 /* force re-caching of index colors */
538 for (i = 0; Context && i < Context->msgcount; i++)
539 Context->hdrs[i]->pair = 0;
541 else if ((r = REGCOMP (&tmp->rx, s, (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0)
543 regerror (r, &tmp->rx, err->data, err->dsize);
544 mutt_free_color_line(&tmp, 1);
548 tmp->pattern = safe_strdup (s);
550 if(fg != -1 && bg != -1)
554 attr |= mutt_alloc_color (fg, bg);
565 parse_object(BUFFER *buf, BUFFER *s, int *o, int *ql, BUFFER *err)
572 strfcpy(err->data, _("Missing arguments."), err->dsize);
576 mutt_extract_token(buf, s, 0);
577 if(!mutt_strncmp(buf->data, "quoted", 6))
581 *ql = strtol(buf->data + 6, &eptr, 10);
582 if(*eptr || q_level < 0)
584 snprintf(err->data, err->dsize, _("%s: no such object"), buf->data);
591 *o = MT_COLOR_QUOTED;
593 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1)
595 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
602 typedef int (*parser_callback_t)(BUFFER *, BUFFER *, int *, int *, int *, BUFFER *);
607 parse_color_pair(BUFFER *buf, BUFFER *s, int *fg, int *bg, int *attr, BUFFER *err)
611 strfcpy (err->data, _("color: too few arguments"), err->dsize);
615 mutt_extract_token (buf, s, 0);
617 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
622 strfcpy (err->data, _("color: too few arguments"), err->dsize);
626 mutt_extract_token (buf, s, 0);
628 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
637 parse_attr_spec(BUFFER *buf, BUFFER *s, int *fg, int *bg, int *attr, BUFFER *err)
645 strfcpy (err->data, _("mono: too few arguments"), err->dsize);
649 mutt_extract_token (buf, s, 0);
651 if (ascii_strcasecmp ("bold", buf->data) == 0)
653 else if (ascii_strcasecmp ("underline", buf->data) == 0)
654 *attr |= A_UNDERLINE;
655 else if (ascii_strcasecmp ("none", buf->data) == 0)
657 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
659 else if (ascii_strcasecmp ("standout", buf->data) == 0)
661 else if (ascii_strcasecmp ("normal", buf->data) == 0)
662 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
665 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
672 static int fgbgattr_to_color(int fg, int bg, int attr)
675 if(fg != -1 && bg != -1)
676 return attr | mutt_alloc_color(fg, bg);
682 /* usage: color <object> <fg> <bg> [ <regexp> ]
683 * mono <object> <attr> [ <regexp> ]
687 _mutt_parse_color (BUFFER *buf, BUFFER *s, BUFFER *err,
688 parser_callback_t callback, short dry_run)
690 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
693 if(parse_object(buf, s, &object, &q_level, err) == -1)
696 if(callback(buf, s, &fg, &bg, &attr, err) == -1)
699 /* extract a regular expression if needed */
701 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY || object == MT_COLOR_INDEX)
705 strfcpy (err->data, _("too few arguments"), err->dsize);
709 mutt_extract_token (buf, s, 0);
714 strfcpy (err->data, _("too many arguments"), err->dsize);
720 if(dry_run) return 0;
724 # ifdef HAVE_USE_DEFAULT_COLORS
725 if (!option (OPTNOCURSES) && has_colors()
726 /* delay use_default_colors() until needed, since it initializes things */
727 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
728 && use_default_colors () != OK)
730 strfcpy (err->data, _("default colors not supported"), err->dsize);
733 # endif /* HAVE_USE_DEFAULT_COLORS */
736 if (object == MT_COLOR_HEADER)
737 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err,0);
738 else if (object == MT_COLOR_BODY)
739 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
740 else if (object == MT_COLOR_INDEX)
742 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
743 set_option (OPTFORCEREDRAWINDEX);
745 else if (object == MT_COLOR_QUOTED)
747 if (q_level >= ColorQuoteSize)
749 safe_realloc (&ColorQuote, (ColorQuoteSize += 2) * sizeof (int));
750 ColorQuote[ColorQuoteSize-2] = ColorDefs[MT_COLOR_QUOTED];
751 ColorQuote[ColorQuoteSize-1] = ColorDefs[MT_COLOR_QUOTED];
753 if (q_level >= ColorQuoteUsed)
754 ColorQuoteUsed = q_level + 1;
757 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color(fg, bg, attr);
759 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
760 for (q_level = 1; q_level < ColorQuoteUsed; q_level++)
762 if (ColorQuote[q_level] == A_NORMAL)
763 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
767 ColorQuote[q_level] = fgbgattr_to_color(fg, bg, attr);
770 ColorDefs[object] = fgbgattr_to_color(fg, bg, attr);
774 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors())
775 BKGDSET (MT_COLOR_NORMAL);
784 int mutt_parse_color(BUFFER *buff, BUFFER *s, unsigned long data, BUFFER *err)
788 if(option(OPTNOCURSES) || !has_colors())
791 return _mutt_parse_color(buff, s, err, parse_color_pair, dry_run);
796 int mutt_parse_mono(BUFFER *buff, BUFFER *s, unsigned long data, BUFFER *err)
801 if(option(OPTNOCURSES) || has_colors())
804 if(option(OPTNOCURSES))
808 return _mutt_parse_color(buff, s, err, parse_attr_spec, dry_run);