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>
32 int ColorDefs[MT_COLOR_MAX];
33 COLOR_LINE *ColorHdrList = NULL;
34 COLOR_LINE *ColorBodyList = NULL;
35 COLOR_LINE *ColorIndexList = NULL;
37 /* local to this file */
38 static int ColorQuoteSize;
42 #define COLOR_DEFAULT (-2)
44 typedef struct color_list {
49 struct color_list *next;
52 static COLOR_LIST *ColorList = NULL;
53 static int UserColors = 0;
55 static struct mapping_t Colors[] = {
56 {"black", COLOR_BLACK},
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[] = {
73 {"hdrdefault", MT_COLOR_HDEFAULT},
74 {"quoted", MT_COLOR_QUOTED},
75 {"signature", MT_COLOR_SIGNATURE},
76 {"indicator", MT_COLOR_INDICATOR},
77 {"status", MT_COLOR_STATUS},
78 {"tree", MT_COLOR_TREE},
79 {"error", MT_COLOR_ERROR},
80 {"normal", MT_COLOR_NORMAL},
81 {"tilde", MT_COLOR_TILDE},
82 {"markers", MT_COLOR_MARKERS},
83 {"header", MT_COLOR_HEADER},
84 {"body", MT_COLOR_BODY},
85 {"message", MT_COLOR_MESSAGE},
86 {"attachment", MT_COLOR_ATTACHMENT},
87 {"search", MT_COLOR_SEARCH},
88 {"bold", MT_COLOR_BOLD},
89 {"underline", MT_COLOR_UNDERLINE},
90 {"index", MT_COLOR_INDEX},
91 {"sidebar_new", MT_COLOR_NEW},
92 {"sidebar", MT_COLOR_SIDEBAR},
93 {"sidebar_flagged", MT_COLOR_FLAGGED},
97 #define COLOR_QUOTE_INIT 8
99 static COLOR_LINE *mutt_new_color_line (void)
101 COLOR_LINE *p = p_new(COLOR_LINE, 1);
108 static void mutt_free_color_line (COLOR_LINE ** l, int free_colors)
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 p_delete(&tmp->pattern);
132 void ci_start_color (void)
134 memset(ColorDefs, A_NORMAL, sizeof(int) * MT_COLOR_MAX);
135 ColorQuote = p_new(int, COLOR_QUOTE_INIT);
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", "default" };
164 m_strcpy(dest, destlen, missing[0]);
168 m_strcpy(dest, destlen, missing[1]);
172 m_strcpy(dest, destlen, missing[2]);
176 for (i = 0; Colors[i].name; i++) {
177 if (Colors[i].value == val) {
178 m_strcpy(dest, destlen, Colors[i].name);
183 /* Sigh. If we got this far, the color is of the form 'colorN'
184 * Slang can handle this itself, so just return 'colorN'
187 snprintf (dest, destlen, "color%d", val);
192 int mutt_alloc_color (int fg, int bg)
194 COLOR_LIST *p = ColorList;
197 #if defined (USE_SLANG_CURSES)
198 char fgc[SHORT_STRING], bgc[SHORT_STRING];
201 /* check to see if this color is already allocated to save space */
203 if (p->fg == fg && p->bg == bg) {
205 return (COLOR_PAIR (p->index));
210 /* check to see if there are colors left */
211 if (++UserColors > COLOR_PAIRS)
214 /* find the smallest available index (object) */
228 p = p_new(COLOR_LIST, 1);
237 #if defined (USE_SLANG_CURSES)
238 if (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
239 SLtt_set_color (i, NULL, get_color_name (fgc, sizeof (fgc), fg),
240 get_color_name (bgc, sizeof (bgc), bg));
242 #elif defined (HAVE_USE_DEFAULT_COLORS)
243 if (fg == COLOR_DEFAULT)
245 if (bg == COLOR_DEFAULT)
249 init_pair (i, fg, bg);
251 return (COLOR_PAIR (p->index));
254 void mutt_free_color (int fg, int bg)
260 if (p->fg == fg && p->bg == bg) {
267 if (p == ColorList) {
268 ColorList = ColorList->next;
287 #endif /* HAVE_COLOR */
293 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER * err)
297 if (m_strncasecmp(s, "bright", 6) == 0) {
302 /* allow aliases for xterm color resources */
303 if (m_strncasecmp(s, "color", 5) == 0) {
305 *col = strtol (s, &eptr, 10);
306 if (!*s || *eptr || *col < 0 ||
307 (*col >= COLORS && !option (OPTNOCURSES) && has_colors ())) {
308 snprintf (err->data, err->dsize, _("%s: color not supported by term"),
313 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1) {
314 snprintf (err->data, err->dsize, _("%s: no such color"), s);
324 /* usage: uncolor index pattern [pattern...]
325 * unmono index pattern [pattern...]
329 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
330 BUFFER * err, short parse_uncolor);
335 int mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
338 return _mutt_parse_uncolor (buf, s, data, err, 1);
343 int mutt_parse_unmono (BUFFER * buf, BUFFER * s, unsigned long data,
346 return _mutt_parse_uncolor (buf, s, data, err, 0);
350 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
351 BUFFER * err, short parse_uncolor)
353 int object = 0, do_cache = 0;
354 COLOR_LINE *tmp, *last = NULL;
356 mutt_extract_token (buf, s, 0);
358 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1) {
359 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
363 if (m_strncmp(buf->data, "index", 5) != 0) {
364 snprintf (err->data, err->dsize,
365 _("%s: command valid only for index object"),
366 parse_uncolor ? "uncolor" : "unmono");
371 snprintf (err->data, err->dsize,
372 _("%s: too few arguments"),
373 parse_uncolor ? "uncolor" : "unmono");
379 /* we're running without curses */
381 || /* we're parsing an uncolor command, and have no colors */
382 (parse_uncolor && !has_colors ())
383 /* we're parsing an unmono command, and have colors */
384 || (!parse_uncolor && has_colors ())
386 /* We don't even have colors compiled in */
390 /* just eat the command, but don't do anything real about it */
392 mutt_extract_token (buf, s, 0);
393 while (MoreArgs (s));
400 mutt_extract_token (buf, s, 0);
401 if (!m_strcmp("*", buf->data)) {
402 for (tmp = ColorIndexList; tmp;) {
407 mutt_free_color_line (&last, parse_uncolor);
409 ColorIndexList = NULL;
412 for (last = NULL, tmp = ColorIndexList; tmp;
413 last = tmp, tmp = tmp->next) {
414 if (!m_strcmp(buf->data, tmp->pattern)) {
418 last->next = tmp->next;
420 ColorIndexList = tmp->next;
421 mutt_free_color_line (&tmp, parse_uncolor);
427 while (MoreArgs (s));
430 if (do_cache && !option (OPTNOCURSES)) {
433 set_option (OPTFORCEREDRAWINDEX);
434 /* force re-caching of index colors */
435 for (i = 0; Context && i < Context->msgcount; i++)
436 Context->hdrs[i]->pair = 0;
443 add_pattern (COLOR_LINE ** top, const char *s, int sensitive,
444 int fg, int bg, int attr, BUFFER * err, int is_index)
447 /* is_index used to store compiled pattern
448 * only for `index' color object
449 * when called from mutt_parse_color() */
451 COLOR_LINE *tmp = *top;
455 if (m_strcmp(s, tmp->pattern) == 0)
459 if (m_strcasecmp(s, tmp->pattern) == 0)
467 if (fg != -1 && bg != -1) {
468 if (tmp->fg != fg || tmp->bg != bg) {
469 mutt_free_color (tmp->fg, tmp->bg);
472 attr |= mutt_alloc_color (fg, bg);
475 attr |= (tmp->pair & ~A_BOLD);
477 #endif /* HAVE_COLOR */
484 tmp = mutt_new_color_line ();
488 m_strcpy(buf, sizeof(buf), NONULL(s));
489 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
490 if ((tmp->color_pattern =
491 mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL) {
492 mutt_free_color_line (&tmp, 1);
495 /* force re-caching of index colors */
496 for (i = 0; Context && i < Context->msgcount; i++)
497 Context->hdrs[i]->pair = 0;
501 REGCOMP (&tmp->rx, s,
502 (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0) {
503 regerror (r, &tmp->rx, err->data, err->dsize);
504 mutt_free_color_line (&tmp, 1);
508 tmp->pattern = m_strdup(s);
510 if (fg != -1 && bg != -1) {
513 attr |= mutt_alloc_color (fg, bg);
524 parse_object (BUFFER * buf, BUFFER * s, int *o, int *ql, BUFFER * err)
530 m_strcpy(err->data, err->dsize, _("Missing arguments."));
534 mutt_extract_token (buf, s, 0);
535 if (!m_strncmp(buf->data, "quoted", 6)) {
537 *ql = strtol (buf->data + 6, &eptr, 10);
538 if (*eptr || q_level < 0) {
539 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
546 *o = MT_COLOR_QUOTED;
548 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1) {
549 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
556 typedef int (*parser_callback_t) (BUFFER *, BUFFER *, int *, int *, int *,
562 parse_color_pair (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
566 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
570 mutt_extract_token (buf, s, 0);
572 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
576 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
580 mutt_extract_token (buf, s, 0);
582 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
591 parse_attr_spec (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
601 m_strcpy(err->data, err->dsize, _("mono: too few arguments"));
605 mutt_extract_token (buf, s, 0);
607 if (ascii_strcasecmp ("bold", buf->data) == 0)
609 else if (ascii_strcasecmp ("underline", buf->data) == 0)
610 *attr |= A_UNDERLINE;
611 else if (ascii_strcasecmp ("none", buf->data) == 0)
613 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
615 else if (ascii_strcasecmp ("standout", buf->data) == 0)
617 else if (ascii_strcasecmp ("normal", buf->data) == 0)
618 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
620 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
627 static int fgbgattr_to_color (int fg, int bg, int attr)
630 if (fg != -1 && bg != -1)
631 return attr | mutt_alloc_color (fg, bg);
637 /* usage: color <object> <fg> <bg> [ <regexp> ]
638 * mono <object> <attr> [ <regexp> ]
642 _mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
643 parser_callback_t callback, short dry_run)
645 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
648 if (parse_object (buf, s, &object, &q_level, err) == -1)
651 if (callback (buf, s, &fg, &bg, &attr, err) == -1)
654 /* extract a regular expression if needed */
656 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY
657 || object == MT_COLOR_INDEX) {
659 m_strcpy(err->data, err->dsize, _("too few arguments"));
663 mutt_extract_token (buf, s, 0);
667 m_strcpy(err->data, err->dsize, _("too many arguments"));
678 # ifdef HAVE_USE_DEFAULT_COLORS
679 if (!option (OPTNOCURSES) && has_colors ()
680 /* delay use_default_colors() until needed, since it initializes things */
681 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
682 && use_default_colors () != OK) {
683 m_strcpy(err->data, err->dsize, _("default colors not supported"));
686 # endif /* HAVE_USE_DEFAULT_COLORS */
689 if (object == MT_COLOR_HEADER)
690 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err, 0);
691 else if (object == MT_COLOR_BODY)
692 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
693 else if (object == MT_COLOR_INDEX) {
694 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
695 set_option (OPTFORCEREDRAWINDEX);
697 else if (object == MT_COLOR_QUOTED) {
698 if (q_level >= ColorQuoteSize) {
699 p_realloc(&ColorQuote, ColorQuoteSize += 2);
700 ColorQuote[ColorQuoteSize - 2] = ColorDefs[MT_COLOR_QUOTED];
701 ColorQuote[ColorQuoteSize - 1] = ColorDefs[MT_COLOR_QUOTED];
703 if (q_level >= ColorQuoteUsed)
704 ColorQuoteUsed = q_level + 1;
706 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color (fg, bg, attr);
708 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
709 for (q_level = 1; q_level < ColorQuoteUsed; q_level++) {
710 if (ColorQuote[q_level] == A_NORMAL)
711 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
715 ColorQuote[q_level] = fgbgattr_to_color (fg, bg, attr);
718 ColorDefs[object] = fgbgattr_to_color (fg, bg, attr);
722 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors ())
723 BKGDSET (MT_COLOR_NORMAL);
732 int mutt_parse_color (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
737 if (option (OPTNOCURSES) || !has_colors ())
740 return _mutt_parse_color (buff, s, err, parse_color_pair, dry_run);
745 int mutt_parse_mono (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
751 if (option (OPTNOCURSES) || has_colors ())
754 if (option (OPTNOCURSES))
758 return _mutt_parse_color (buff, s, err, parse_attr_spec, dry_run);