2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
18 #include <lib-lib/mem.h>
19 #include <lib-lib/ascii.h>
20 #include <lib-lib/str.h>
21 #include <lib-lib/macros.h>
22 #include <lib-lib/buffer.h>
23 #include <lib-lib/mapping.h>
24 #include <lib-lib/debug.h>
33 int ColorDefs[MT_COLOR_MAX];
34 COLOR_LINE *ColorHdrList = NULL;
35 COLOR_LINE *ColorBodyList = NULL;
36 COLOR_LINE *ColorIndexList = NULL;
38 /* local to this file */
39 static int ColorQuoteSize;
43 #define COLOR_DEFAULT (-2)
45 typedef struct color_list {
50 struct color_list *next;
53 static COLOR_LIST *ColorList = NULL;
54 static int UserColors = 0;
56 static struct mapping_t Colors[] = {
57 {"black", COLOR_BLACK},
60 {"green", COLOR_GREEN},
61 {"magenta", COLOR_MAGENTA},
63 {"white", COLOR_WHITE},
64 {"yellow", COLOR_YELLOW},
65 #if defined (USE_SLANG_CURSES) || defined (HAVE_USE_DEFAULT_COLORS)
66 {"default", COLOR_DEFAULT},
71 #endif /* HAVE_COLOR */
73 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},
93 {"sidebar", MT_COLOR_SIDEBAR},
94 {"sidebar_flagged", MT_COLOR_FLAGGED},
98 #define COLOR_QUOTE_INIT 8
100 static COLOR_LINE *mutt_new_color_line (void)
102 COLOR_LINE *p = p_new(COLOR_LINE, 1);
109 static void mutt_free_color_line (COLOR_LINE ** l, int free_colors)
119 if (free_colors && tmp->fg != -1 && tmp->bg != -1)
120 mutt_free_color (tmp->fg, tmp->bg);
123 /* we should really introduce a container
124 * type for regular expressions.
128 mutt_pattern_free (&tmp->color_pattern);
129 p_delete(&tmp->pattern);
133 void ci_start_color (void)
135 memset(ColorDefs, A_NORMAL, sizeof(int) * MT_COLOR_MAX);
136 ColorQuote = p_new(int, COLOR_QUOTE_INIT);
137 memset(ColorQuote, A_NORMAL, sizeof(int) * COLOR_QUOTE_INIT);
138 ColorQuoteSize = COLOR_QUOTE_INIT;
141 /* set some defaults */
142 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
143 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
144 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
145 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
146 /* special meaning: toggle the relevant attribute */
147 ColorDefs[MT_COLOR_BOLD] = 0;
148 ColorDefs[MT_COLOR_UNDERLINE] = 0;
157 #ifdef USE_SLANG_CURSES
158 static char *get_color_name (char *dest, size_t destlen, int val)
160 static char *missing[3] = { "brown", "lightgray", "default" };
165 m_strcpy(dest, destlen, missing[0]);
169 m_strcpy(dest, destlen, missing[1]);
173 m_strcpy(dest, destlen, missing[2]);
177 for (i = 0; Colors[i].name; i++) {
178 if (Colors[i].value == val) {
179 m_strcpy(dest, destlen, Colors[i].name);
184 /* Sigh. If we got this far, the color is of the form 'colorN'
185 * Slang can handle this itself, so just return 'colorN'
188 snprintf (dest, destlen, "color%d", val);
193 int mutt_alloc_color (int fg, int bg)
195 COLOR_LIST *p = ColorList;
198 #if defined (USE_SLANG_CURSES)
199 char fgc[SHORT_STRING], bgc[SHORT_STRING];
202 /* check to see if this color is already allocated to save space */
204 if (p->fg == fg && p->bg == bg) {
206 return (COLOR_PAIR (p->index));
211 /* check to see if there are colors left */
212 if (++UserColors > COLOR_PAIRS)
215 /* find the smallest available index (object) */
229 p = p_new(COLOR_LIST, 1);
238 #if defined (USE_SLANG_CURSES)
239 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
240 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg),
241 get_color_name (bgc, sizeof (bgc), bg));
243 #elif defined (HAVE_USE_DEFAULT_COLORS)
244 if (fg == COLOR_DEFAULT)
246 if (bg == COLOR_DEFAULT)
250 init_pair (i, fg, bg);
252 debug_print (1, ("Color pairs used so far: %d\n", UserColors));
254 return (COLOR_PAIR (p->index));
257 void mutt_free_color (int fg, int bg)
263 if (p->fg == fg && p->bg == bg) {
269 debug_print (1, ("Color pairs used so far: %d\n", UserColors));
271 if (p == ColorList) {
272 ColorList = ColorList->next;
291 #endif /* HAVE_COLOR */
297 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER * err)
301 if (m_strncasecmp(s, "bright", 6) == 0) {
306 /* allow aliases for xterm color resources */
307 if (m_strncasecmp(s, "color", 5) == 0) {
309 *col = strtol (s, &eptr, 10);
310 if (!*s || *eptr || *col < 0 ||
311 (*col >= COLORS && !option (OPTNOCURSES) && has_colors ())) {
312 snprintf (err->data, err->dsize, _("%s: color not supported by term"),
317 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1) {
318 snprintf (err->data, err->dsize, _("%s: no such color"), s);
328 /* usage: uncolor index pattern [pattern...]
329 * unmono index pattern [pattern...]
333 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
334 BUFFER * err, short parse_uncolor);
339 int mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
342 return _mutt_parse_uncolor (buf, s, data, err, 1);
347 int mutt_parse_unmono (BUFFER * buf, BUFFER * s, unsigned long data,
350 return _mutt_parse_uncolor (buf, s, data, err, 0);
354 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
355 BUFFER * err, short parse_uncolor)
357 int object = 0, do_cache = 0;
358 COLOR_LINE *tmp, *last = NULL;
360 mutt_extract_token (buf, s, 0);
362 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1) {
363 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
367 if (m_strncmp(buf->data, "index", 5) != 0) {
368 snprintf (err->data, err->dsize,
369 _("%s: command valid only for index object"),
370 parse_uncolor ? "uncolor" : "unmono");
375 snprintf (err->data, err->dsize,
376 _("%s: too few arguments"),
377 parse_uncolor ? "uncolor" : "unmono");
383 /* we're running without curses */
385 || /* we're parsing an uncolor command, and have no colors */
386 (parse_uncolor && !has_colors ())
387 /* we're parsing an unmono command, and have colors */
388 || (!parse_uncolor && has_colors ())
390 /* We don't even have colors compiled in */
394 /* just eat the command, but don't do anything real about it */
396 mutt_extract_token (buf, s, 0);
397 while (MoreArgs (s));
404 mutt_extract_token (buf, s, 0);
405 if (!m_strcmp("*", buf->data)) {
406 for (tmp = ColorIndexList; tmp;) {
411 mutt_free_color_line (&last, parse_uncolor);
413 ColorIndexList = NULL;
416 for (last = NULL, tmp = ColorIndexList; tmp;
417 last = tmp, tmp = tmp->next) {
418 if (!m_strcmp(buf->data, tmp->pattern)) {
421 debug_print (1, ("Freeing pattern \"%s\" from ColorIndexList\n", tmp->pattern));
423 last->next = tmp->next;
425 ColorIndexList = tmp->next;
426 mutt_free_color_line (&tmp, parse_uncolor);
432 while (MoreArgs (s));
435 if (do_cache && !option (OPTNOCURSES)) {
438 set_option (OPTFORCEREDRAWINDEX);
439 /* force re-caching of index colors */
440 for (i = 0; Context && i < Context->msgcount; i++)
441 Context->hdrs[i]->pair = 0;
448 add_pattern (COLOR_LINE ** top, const char *s, int sensitive,
449 int fg, int bg, int attr, BUFFER * err, int is_index)
452 /* is_index used to store compiled pattern
453 * only for `index' color object
454 * when called from mutt_parse_color() */
456 COLOR_LINE *tmp = *top;
460 if (m_strcmp(s, tmp->pattern) == 0)
464 if (m_strcasecmp(s, tmp->pattern) == 0)
472 if (fg != -1 && bg != -1) {
473 if (tmp->fg != fg || tmp->bg != bg) {
474 mutt_free_color (tmp->fg, tmp->bg);
477 attr |= mutt_alloc_color (fg, bg);
480 attr |= (tmp->pair & ~A_BOLD);
482 #endif /* HAVE_COLOR */
489 tmp = mutt_new_color_line ();
493 m_strcpy(buf, sizeof(buf), NONULL(s));
494 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
495 if ((tmp->color_pattern =
496 mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL) {
497 mutt_free_color_line (&tmp, 1);
500 /* force re-caching of index colors */
501 for (i = 0; Context && i < Context->msgcount; i++)
502 Context->hdrs[i]->pair = 0;
506 REGCOMP (&tmp->rx, s,
507 (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0) {
508 regerror (r, &tmp->rx, err->data, err->dsize);
509 mutt_free_color_line (&tmp, 1);
513 tmp->pattern = m_strdup(s);
515 if (fg != -1 && bg != -1) {
518 attr |= mutt_alloc_color (fg, bg);
529 parse_object (BUFFER * buf, BUFFER * s, int *o, int *ql, BUFFER * err)
535 m_strcpy(err->data, err->dsize, _("Missing arguments."));
539 mutt_extract_token (buf, s, 0);
540 if (!m_strncmp(buf->data, "quoted", 6)) {
542 *ql = strtol (buf->data + 6, &eptr, 10);
543 if (*eptr || q_level < 0) {
544 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
551 *o = MT_COLOR_QUOTED;
553 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1) {
554 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
561 typedef int (*parser_callback_t) (BUFFER *, BUFFER *, int *, int *, int *,
567 parse_color_pair (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
571 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
575 mutt_extract_token (buf, s, 0);
577 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
581 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
585 mutt_extract_token (buf, s, 0);
587 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
596 parse_attr_spec (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
606 m_strcpy(err->data, err->dsize, _("mono: too few arguments"));
610 mutt_extract_token (buf, s, 0);
612 if (ascii_strcasecmp ("bold", buf->data) == 0)
614 else if (ascii_strcasecmp ("underline", buf->data) == 0)
615 *attr |= A_UNDERLINE;
616 else if (ascii_strcasecmp ("none", buf->data) == 0)
618 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
620 else if (ascii_strcasecmp ("standout", buf->data) == 0)
622 else if (ascii_strcasecmp ("normal", buf->data) == 0)
623 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
625 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
632 static int fgbgattr_to_color (int fg, int bg, int attr)
635 if (fg != -1 && bg != -1)
636 return attr | mutt_alloc_color (fg, bg);
642 /* usage: color <object> <fg> <bg> [ <regexp> ]
643 * mono <object> <attr> [ <regexp> ]
647 _mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
648 parser_callback_t callback, short dry_run)
650 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
653 if (parse_object (buf, s, &object, &q_level, err) == -1)
656 if (callback (buf, s, &fg, &bg, &attr, err) == -1)
659 /* extract a regular expression if needed */
661 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY
662 || object == MT_COLOR_INDEX) {
664 m_strcpy(err->data, err->dsize, _("too few arguments"));
668 mutt_extract_token (buf, s, 0);
672 m_strcpy(err->data, err->dsize, _("too many arguments"));
683 # ifdef HAVE_USE_DEFAULT_COLORS
684 if (!option (OPTNOCURSES) && has_colors ()
685 /* delay use_default_colors() until needed, since it initializes things */
686 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
687 && use_default_colors () != OK) {
688 m_strcpy(err->data, err->dsize, _("default colors not supported"));
691 # endif /* HAVE_USE_DEFAULT_COLORS */
694 if (object == MT_COLOR_HEADER)
695 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err, 0);
696 else if (object == MT_COLOR_BODY)
697 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
698 else if (object == MT_COLOR_INDEX) {
699 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
700 set_option (OPTFORCEREDRAWINDEX);
702 else if (object == MT_COLOR_QUOTED) {
703 if (q_level >= ColorQuoteSize) {
704 p_realloc(&ColorQuote, ColorQuoteSize += 2);
705 ColorQuote[ColorQuoteSize - 2] = ColorDefs[MT_COLOR_QUOTED];
706 ColorQuote[ColorQuoteSize - 1] = ColorDefs[MT_COLOR_QUOTED];
708 if (q_level >= ColorQuoteUsed)
709 ColorQuoteUsed = q_level + 1;
711 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color (fg, bg, attr);
713 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
714 for (q_level = 1; q_level < ColorQuoteUsed; q_level++) {
715 if (ColorQuote[q_level] == A_NORMAL)
716 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
720 ColorQuote[q_level] = fgbgattr_to_color (fg, bg, attr);
723 ColorDefs[object] = fgbgattr_to_color (fg, bg, attr);
727 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors ())
728 BKGDSET (MT_COLOR_NORMAL);
737 int mutt_parse_color (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
742 if (option (OPTNOCURSES) || !has_colors ())
745 return _mutt_parse_color (buff, s, err, parse_color_pair, dry_run);
750 int mutt_parse_mono (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
756 if (option (OPTNOCURSES) || has_colors ())
759 if (option (OPTNOCURSES))
763 return _mutt_parse_color (buff, s, err, parse_attr_spec, dry_run);