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.
10 #include <lib-lib/lib-lib.h>
20 int ColorDefs[MT_COLOR_MAX];
21 COLOR_LINE *ColorHdrList = NULL;
22 COLOR_LINE *ColorBodyList = NULL;
23 COLOR_LINE *ColorIndexList = NULL;
25 /* local to this file */
26 static int ColorQuoteSize;
28 #define COLOR_DEFAULT (-2)
30 typedef struct color_list {
35 struct color_list *next;
38 static COLOR_LIST *ColorList = NULL;
39 static int UserColors = 0;
41 static struct mapping_t Colors[] = {
42 {"black", COLOR_BLACK},
45 {"green", COLOR_GREEN},
46 {"magenta", COLOR_MAGENTA},
48 {"white", COLOR_WHITE},
49 {"yellow", COLOR_YELLOW},
50 {"default", COLOR_DEFAULT},
54 static struct mapping_t Fields[] = {
55 {"hdrdefault", MT_COLOR_HDEFAULT},
56 {"quoted", MT_COLOR_QUOTED},
57 {"signature", MT_COLOR_SIGNATURE},
58 {"indicator", MT_COLOR_INDICATOR},
59 {"status", MT_COLOR_STATUS},
60 {"tree", MT_COLOR_TREE},
61 {"error", MT_COLOR_ERROR},
62 {"normal", MT_COLOR_NORMAL},
63 {"tilde", MT_COLOR_TILDE},
64 {"markers", MT_COLOR_MARKERS},
65 {"header", MT_COLOR_HEADER},
66 {"body", MT_COLOR_BODY},
67 {"message", MT_COLOR_MESSAGE},
68 {"attachment", MT_COLOR_ATTACHMENT},
69 {"search", MT_COLOR_SEARCH},
70 {"bold", MT_COLOR_BOLD},
71 {"underline", MT_COLOR_UNDERLINE},
72 {"index", MT_COLOR_INDEX},
73 {"sidebar_new", MT_COLOR_NEW},
74 {"sidebar", MT_COLOR_SIDEBAR},
75 {"sidebar_flagged", MT_COLOR_FLAGGED},
79 #define COLOR_QUOTE_INIT 8
81 static COLOR_LINE *mutt_new_color_line (void)
83 COLOR_LINE *p = p_new(COLOR_LINE, 1);
90 static void mutt_free_color_line (COLOR_LINE ** l, int free_colors)
99 if (free_colors && tmp->fg != -1 && tmp->bg != -1)
100 mutt_free_color (tmp->fg, tmp->bg);
102 /* we should really use the container type for regular expressions. */
105 pattern_list_wipe(&tmp->color_pattern);
106 p_delete(&tmp->pattern);
110 void ci_start_color (void)
112 memset(ColorDefs, A_NORMAL, sizeof(int) * MT_COLOR_MAX);
113 ColorQuote = p_new(int, COLOR_QUOTE_INIT);
114 memset(ColorQuote, A_NORMAL, sizeof(int) * COLOR_QUOTE_INIT);
115 ColorQuoteSize = COLOR_QUOTE_INIT;
118 /* set some defaults */
119 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
120 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
121 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
122 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
123 /* special meaning: toggle the relevant attribute */
124 ColorDefs[MT_COLOR_BOLD] = 0;
125 ColorDefs[MT_COLOR_UNDERLINE] = 0;
130 int mutt_alloc_color (int fg, int bg)
132 COLOR_LIST *p = ColorList;
135 /* check to see if this color is already allocated to save space */
137 if (p->fg == fg && p->bg == bg) {
139 return (COLOR_PAIR (p->index));
144 /* check to see if there are colors left */
145 if (++UserColors > COLOR_PAIRS)
148 /* find the smallest available index (object) */
162 p = p_new(COLOR_LIST, 1);
171 if (fg == COLOR_DEFAULT)
173 if (bg == COLOR_DEFAULT)
176 init_pair (i, fg, bg);
178 return (COLOR_PAIR (p->index));
181 void mutt_free_color (int fg, int bg)
187 if (p->fg == fg && p->bg == bg) {
194 if (p == ColorList) {
195 ColorList = ColorList->next;
215 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER * err)
219 if (m_strncasecmp(s, "bright", 6) == 0) {
224 /* allow aliases for xterm color resources */
225 if (m_strncasecmp(s, "color", 5) == 0) {
227 *col = strtol (s, &eptr, 10);
228 if (!*s || *eptr || *col < 0 ||
229 (*col >= COLORS && !option (OPTNOCURSES) && has_colors ())) {
230 snprintf (err->data, err->dsize, _("%s: color not supported by term"),
235 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1) {
236 snprintf (err->data, err->dsize, _("%s: no such color"), s);
243 /* usage: uncolor index pattern [pattern...]
244 * unmono index pattern [pattern...]
248 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
249 BUFFER * err, short parse_uncolor);
251 int mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
254 return _mutt_parse_uncolor (buf, s, data, err, 1);
257 int mutt_parse_unmono (BUFFER * buf, BUFFER * s, unsigned long data,
260 return _mutt_parse_uncolor (buf, s, data, err, 0);
264 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
265 BUFFER * err, short parse_uncolor)
267 int object = 0, do_cache = 0;
268 COLOR_LINE *tmp, *last = NULL;
270 mutt_extract_token (buf, s, 0);
272 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1) {
273 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
277 if (m_strncmp(buf->data, "index", 5) != 0) {
278 snprintf (err->data, err->dsize,
279 _("%s: command valid only for index object"),
280 parse_uncolor ? "uncolor" : "unmono");
285 snprintf (err->data, err->dsize,
286 _("%s: too few arguments"),
287 parse_uncolor ? "uncolor" : "unmono");
291 if (option (OPTNOCURSES) || (parse_uncolor != has_colors())) {
292 /* just eat the command, but don't do anything real about it */
294 mutt_extract_token (buf, s, 0);
295 } while (MoreArgs(s));
300 mutt_extract_token (buf, s, 0);
301 if (!m_strcmp("*", buf->data)) {
302 for (tmp = ColorIndexList; tmp;) {
307 mutt_free_color_line (&last, parse_uncolor);
309 ColorIndexList = NULL;
312 for (last = NULL, tmp = ColorIndexList; tmp;
313 last = tmp, tmp = tmp->next) {
314 if (!m_strcmp(buf->data, tmp->pattern)) {
318 last->next = tmp->next;
320 ColorIndexList = tmp->next;
321 mutt_free_color_line (&tmp, parse_uncolor);
327 while (MoreArgs (s));
330 if (do_cache && !option (OPTNOCURSES)) {
333 set_option (OPTFORCEREDRAWINDEX);
334 /* force re-caching of index colors */
335 for (i = 0; Context && i < Context->msgcount; i++)
336 Context->hdrs[i]->pair = 0;
343 add_pattern (COLOR_LINE ** top, const char *s, int sensitive,
344 int fg, int bg, int attr, BUFFER * err, int is_index)
347 /* is_index used to store compiled pattern
348 * only for `index' color object
349 * when called from mutt_parse_color() */
351 COLOR_LINE *tmp = *top;
355 if (m_strcmp(s, tmp->pattern) == 0)
359 if (m_strcasecmp(s, tmp->pattern) == 0)
366 if (fg != -1 && bg != -1) {
367 if (tmp->fg != fg || tmp->bg != bg) {
368 mutt_free_color (tmp->fg, tmp->bg);
371 attr |= mutt_alloc_color (fg, bg);
374 attr |= (tmp->pair & ~A_BOLD);
381 tmp = mutt_new_color_line ();
385 m_strcpy(buf, sizeof(buf), NONULL(s));
386 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
387 if ((tmp->color_pattern =
388 mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL) {
389 mutt_free_color_line (&tmp, 1);
392 /* force re-caching of index colors */
393 for (i = 0; Context && i < Context->msgcount; i++)
394 Context->hdrs[i]->pair = 0;
398 REGCOMP (&tmp->rx, s,
399 (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0) {
400 regerror (r, &tmp->rx, err->data, err->dsize);
401 mutt_free_color_line (&tmp, 1);
405 tmp->pattern = m_strdup(s);
406 if (fg != -1 && bg != -1) {
409 attr |= mutt_alloc_color (fg, bg);
419 parse_object (BUFFER * buf, BUFFER * s, int *o, int *ql, BUFFER * err)
425 m_strcpy(err->data, err->dsize, _("Missing arguments."));
429 mutt_extract_token (buf, s, 0);
430 if (!m_strncmp(buf->data, "quoted", 6)) {
432 *ql = strtol (buf->data + 6, &eptr, 10);
433 if (*eptr || q_level < 0) {
434 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
441 *o = MT_COLOR_QUOTED;
443 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1) {
444 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
451 typedef int (*parser_callback_t) (BUFFER *, BUFFER *, int *, int *, int *,
455 parse_color_pair (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
459 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
463 mutt_extract_token (buf, s, 0);
465 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
469 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
473 mutt_extract_token (buf, s, 0);
475 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
482 parse_attr_spec (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
492 m_strcpy(err->data, err->dsize, _("mono: too few arguments"));
496 mutt_extract_token (buf, s, 0);
498 if (ascii_strcasecmp ("bold", buf->data) == 0)
500 else if (ascii_strcasecmp ("underline", buf->data) == 0)
501 *attr |= A_UNDERLINE;
502 else if (ascii_strcasecmp ("none", buf->data) == 0)
504 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
506 else if (ascii_strcasecmp ("standout", buf->data) == 0)
508 else if (ascii_strcasecmp ("normal", buf->data) == 0)
509 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
511 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
518 static int fgbgattr_to_color (int fg, int bg, int attr)
520 if (fg != -1 && bg != -1)
521 return attr | mutt_alloc_color (fg, bg);
526 /* usage: color <object> <fg> <bg> [ <regexp> ]
527 * mono <object> <attr> [ <regexp> ]
531 _mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
532 parser_callback_t callback, short dry_run)
534 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
537 if (parse_object (buf, s, &object, &q_level, err) == -1)
540 if (callback (buf, s, &fg, &bg, &attr, err) == -1)
543 /* extract a regular expression if needed */
545 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY
546 || object == MT_COLOR_INDEX) {
548 m_strcpy(err->data, err->dsize, _("too few arguments"));
552 mutt_extract_token (buf, s, 0);
556 m_strcpy(err->data, err->dsize, _("too many arguments"));
566 if (!option (OPTNOCURSES) && has_colors ()
567 /* delay use_default_colors() until needed, since it initializes things */
568 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
569 && use_default_colors () != OK) {
570 m_strcpy(err->data, err->dsize, _("default colors not supported"));
574 if (object == MT_COLOR_HEADER)
575 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err, 0);
576 else if (object == MT_COLOR_BODY)
577 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
578 else if (object == MT_COLOR_INDEX) {
579 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
580 set_option (OPTFORCEREDRAWINDEX);
582 else if (object == MT_COLOR_QUOTED) {
583 if (q_level >= ColorQuoteSize) {
584 p_realloc(&ColorQuote, ColorQuoteSize += 2);
585 ColorQuote[ColorQuoteSize - 2] = ColorDefs[MT_COLOR_QUOTED];
586 ColorQuote[ColorQuoteSize - 1] = ColorDefs[MT_COLOR_QUOTED];
588 if (q_level >= ColorQuoteUsed)
589 ColorQuoteUsed = q_level + 1;
591 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color (fg, bg, attr);
593 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
594 for (q_level = 1; q_level < ColorQuoteUsed; q_level++) {
595 if (ColorQuote[q_level] == A_NORMAL)
596 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
600 ColorQuote[q_level] = fgbgattr_to_color (fg, bg, attr);
603 ColorDefs[object] = fgbgattr_to_color (fg, bg, attr);
605 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors ())
606 BKGDSET (MT_COLOR_NORMAL);
611 int mutt_parse_color (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
616 if (option (OPTNOCURSES) || !has_colors ())
619 return _mutt_parse_color (buff, s, err, parse_color_pair, dry_run);
622 int mutt_parse_mono (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
627 if (option (OPTNOCURSES) || has_colors ())
630 return _mutt_parse_color (buff, s, err, parse_attr_spec, dry_run);