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 #if defined (HAVE_USE_DEFAULT_COLORS)
51 {"default", COLOR_DEFAULT},
56 static struct mapping_t Fields[] = {
57 {"hdrdefault", MT_COLOR_HDEFAULT},
58 {"quoted", MT_COLOR_QUOTED},
59 {"signature", MT_COLOR_SIGNATURE},
60 {"indicator", MT_COLOR_INDICATOR},
61 {"status", MT_COLOR_STATUS},
62 {"tree", MT_COLOR_TREE},
63 {"error", MT_COLOR_ERROR},
64 {"normal", MT_COLOR_NORMAL},
65 {"tilde", MT_COLOR_TILDE},
66 {"markers", MT_COLOR_MARKERS},
67 {"header", MT_COLOR_HEADER},
68 {"body", MT_COLOR_BODY},
69 {"message", MT_COLOR_MESSAGE},
70 {"attachment", MT_COLOR_ATTACHMENT},
71 {"search", MT_COLOR_SEARCH},
72 {"bold", MT_COLOR_BOLD},
73 {"underline", MT_COLOR_UNDERLINE},
74 {"index", MT_COLOR_INDEX},
75 {"sidebar_new", MT_COLOR_NEW},
76 {"sidebar", MT_COLOR_SIDEBAR},
77 {"sidebar_flagged", MT_COLOR_FLAGGED},
81 #define COLOR_QUOTE_INIT 8
83 static COLOR_LINE *mutt_new_color_line (void)
85 COLOR_LINE *p = p_new(COLOR_LINE, 1);
92 static void mutt_free_color_line (COLOR_LINE ** l, int free_colors)
101 if (free_colors && tmp->fg != -1 && tmp->bg != -1)
102 mutt_free_color (tmp->fg, tmp->bg);
104 /* we should really use the container type for regular expressions. */
107 pattern_list_wipe(&tmp->color_pattern);
108 p_delete(&tmp->pattern);
112 void ci_start_color (void)
114 memset(ColorDefs, A_NORMAL, sizeof(int) * MT_COLOR_MAX);
115 ColorQuote = p_new(int, COLOR_QUOTE_INIT);
116 memset(ColorQuote, A_NORMAL, sizeof(int) * COLOR_QUOTE_INIT);
117 ColorQuoteSize = COLOR_QUOTE_INIT;
120 /* set some defaults */
121 ColorDefs[MT_COLOR_STATUS] = A_REVERSE;
122 ColorDefs[MT_COLOR_INDICATOR] = A_REVERSE;
123 ColorDefs[MT_COLOR_SEARCH] = A_REVERSE;
124 ColorDefs[MT_COLOR_MARKERS] = A_REVERSE;
125 /* special meaning: toggle the relevant attribute */
126 ColorDefs[MT_COLOR_BOLD] = 0;
127 ColorDefs[MT_COLOR_UNDERLINE] = 0;
132 int mutt_alloc_color (int fg, int bg)
134 COLOR_LIST *p = ColorList;
137 /* check to see if this color is already allocated to save space */
139 if (p->fg == fg && p->bg == bg) {
141 return (COLOR_PAIR (p->index));
146 /* check to see if there are colors left */
147 if (++UserColors > COLOR_PAIRS)
150 /* find the smallest available index (object) */
164 p = p_new(COLOR_LIST, 1);
173 #if defined (HAVE_USE_DEFAULT_COLORS)
174 if (fg == COLOR_DEFAULT)
176 if (bg == COLOR_DEFAULT)
180 init_pair (i, fg, bg);
182 return (COLOR_PAIR (p->index));
185 void mutt_free_color (int fg, int bg)
191 if (p->fg == fg && p->bg == bg) {
198 if (p == ColorList) {
199 ColorList = ColorList->next;
219 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER * err)
223 if (m_strncasecmp(s, "bright", 6) == 0) {
228 /* allow aliases for xterm color resources */
229 if (m_strncasecmp(s, "color", 5) == 0) {
231 *col = strtol (s, &eptr, 10);
232 if (!*s || *eptr || *col < 0 ||
233 (*col >= COLORS && !option (OPTNOCURSES) && has_colors ())) {
234 snprintf (err->data, err->dsize, _("%s: color not supported by term"),
239 else if ((*col = mutt_getvaluebyname (s, Colors)) == -1) {
240 snprintf (err->data, err->dsize, _("%s: no such color"), s);
247 /* usage: uncolor index pattern [pattern...]
248 * unmono index pattern [pattern...]
252 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
253 BUFFER * err, short parse_uncolor);
255 int mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
258 return _mutt_parse_uncolor (buf, s, data, err, 1);
261 int mutt_parse_unmono (BUFFER * buf, BUFFER * s, unsigned long data,
264 return _mutt_parse_uncolor (buf, s, data, err, 0);
268 _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
269 BUFFER * err, short parse_uncolor)
271 int object = 0, do_cache = 0;
272 COLOR_LINE *tmp, *last = NULL;
274 mutt_extract_token (buf, s, 0);
276 if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1) {
277 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
281 if (m_strncmp(buf->data, "index", 5) != 0) {
282 snprintf (err->data, err->dsize,
283 _("%s: command valid only for index object"),
284 parse_uncolor ? "uncolor" : "unmono");
289 snprintf (err->data, err->dsize,
290 _("%s: too few arguments"),
291 parse_uncolor ? "uncolor" : "unmono");
295 if (option (OPTNOCURSES) || (parse_uncolor != has_colors())) {
296 /* just eat the command, but don't do anything real about it */
298 mutt_extract_token (buf, s, 0);
299 } while (MoreArgs(s));
304 mutt_extract_token (buf, s, 0);
305 if (!m_strcmp("*", buf->data)) {
306 for (tmp = ColorIndexList; tmp;) {
311 mutt_free_color_line (&last, parse_uncolor);
313 ColorIndexList = NULL;
316 for (last = NULL, tmp = ColorIndexList; tmp;
317 last = tmp, tmp = tmp->next) {
318 if (!m_strcmp(buf->data, tmp->pattern)) {
322 last->next = tmp->next;
324 ColorIndexList = tmp->next;
325 mutt_free_color_line (&tmp, parse_uncolor);
331 while (MoreArgs (s));
334 if (do_cache && !option (OPTNOCURSES)) {
337 set_option (OPTFORCEREDRAWINDEX);
338 /* force re-caching of index colors */
339 for (i = 0; Context && i < Context->msgcount; i++)
340 Context->hdrs[i]->pair = 0;
347 add_pattern (COLOR_LINE ** top, const char *s, int sensitive,
348 int fg, int bg, int attr, BUFFER * err, int is_index)
351 /* is_index used to store compiled pattern
352 * only for `index' color object
353 * when called from mutt_parse_color() */
355 COLOR_LINE *tmp = *top;
359 if (m_strcmp(s, tmp->pattern) == 0)
363 if (m_strcasecmp(s, tmp->pattern) == 0)
370 if (fg != -1 && bg != -1) {
371 if (tmp->fg != fg || tmp->bg != bg) {
372 mutt_free_color (tmp->fg, tmp->bg);
375 attr |= mutt_alloc_color (fg, bg);
378 attr |= (tmp->pair & ~A_BOLD);
385 tmp = mutt_new_color_line ();
389 m_strcpy(buf, sizeof(buf), NONULL(s));
390 mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
391 if ((tmp->color_pattern =
392 mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL) {
393 mutt_free_color_line (&tmp, 1);
396 /* force re-caching of index colors */
397 for (i = 0; Context && i < Context->msgcount; i++)
398 Context->hdrs[i]->pair = 0;
402 REGCOMP (&tmp->rx, s,
403 (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0) {
404 regerror (r, &tmp->rx, err->data, err->dsize);
405 mutt_free_color_line (&tmp, 1);
409 tmp->pattern = m_strdup(s);
410 if (fg != -1 && bg != -1) {
413 attr |= mutt_alloc_color (fg, bg);
423 parse_object (BUFFER * buf, BUFFER * s, int *o, int *ql, BUFFER * err)
429 m_strcpy(err->data, err->dsize, _("Missing arguments."));
433 mutt_extract_token (buf, s, 0);
434 if (!m_strncmp(buf->data, "quoted", 6)) {
436 *ql = strtol (buf->data + 6, &eptr, 10);
437 if (*eptr || q_level < 0) {
438 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
445 *o = MT_COLOR_QUOTED;
447 else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1) {
448 snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
455 typedef int (*parser_callback_t) (BUFFER *, BUFFER *, int *, int *, int *,
459 parse_color_pair (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
463 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
467 mutt_extract_token (buf, s, 0);
469 if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
473 m_strcpy(err->data, err->dsize, _("color: too few arguments"));
477 mutt_extract_token (buf, s, 0);
479 if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
486 parse_attr_spec (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
496 m_strcpy(err->data, err->dsize, _("mono: too few arguments"));
500 mutt_extract_token (buf, s, 0);
502 if (ascii_strcasecmp ("bold", buf->data) == 0)
504 else if (ascii_strcasecmp ("underline", buf->data) == 0)
505 *attr |= A_UNDERLINE;
506 else if (ascii_strcasecmp ("none", buf->data) == 0)
508 else if (ascii_strcasecmp ("reverse", buf->data) == 0)
510 else if (ascii_strcasecmp ("standout", buf->data) == 0)
512 else if (ascii_strcasecmp ("normal", buf->data) == 0)
513 *attr = A_NORMAL; /* needs use = instead of |= to clear other bits */
515 snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
522 static int fgbgattr_to_color (int fg, int bg, int attr)
524 if (fg != -1 && bg != -1)
525 return attr | mutt_alloc_color (fg, bg);
530 /* usage: color <object> <fg> <bg> [ <regexp> ]
531 * mono <object> <attr> [ <regexp> ]
535 _mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
536 parser_callback_t callback, short dry_run)
538 int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
541 if (parse_object (buf, s, &object, &q_level, err) == -1)
544 if (callback (buf, s, &fg, &bg, &attr, err) == -1)
547 /* extract a regular expression if needed */
549 if (object == MT_COLOR_HEADER || object == MT_COLOR_BODY
550 || object == MT_COLOR_INDEX) {
552 m_strcpy(err->data, err->dsize, _("too few arguments"));
556 mutt_extract_token (buf, s, 0);
560 m_strcpy(err->data, err->dsize, _("too many arguments"));
570 #ifdef HAVE_USE_DEFAULT_COLORS
571 if (!option (OPTNOCURSES) && has_colors ()
572 /* delay use_default_colors() until needed, since it initializes things */
573 && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
574 && use_default_colors () != OK) {
575 m_strcpy(err->data, err->dsize, _("default colors not supported"));
578 #endif /* HAVE_USE_DEFAULT_COLORS */
580 if (object == MT_COLOR_HEADER)
581 r = add_pattern (&ColorHdrList, buf->data, 0, fg, bg, attr, err, 0);
582 else if (object == MT_COLOR_BODY)
583 r = add_pattern (&ColorBodyList, buf->data, 1, fg, bg, attr, err, 0);
584 else if (object == MT_COLOR_INDEX) {
585 r = add_pattern (&ColorIndexList, buf->data, 1, fg, bg, attr, err, 1);
586 set_option (OPTFORCEREDRAWINDEX);
588 else if (object == MT_COLOR_QUOTED) {
589 if (q_level >= ColorQuoteSize) {
590 p_realloc(&ColorQuote, ColorQuoteSize += 2);
591 ColorQuote[ColorQuoteSize - 2] = ColorDefs[MT_COLOR_QUOTED];
592 ColorQuote[ColorQuoteSize - 1] = ColorDefs[MT_COLOR_QUOTED];
594 if (q_level >= ColorQuoteUsed)
595 ColorQuoteUsed = q_level + 1;
597 ColorDefs[MT_COLOR_QUOTED] = fgbgattr_to_color (fg, bg, attr);
599 ColorQuote[0] = ColorDefs[MT_COLOR_QUOTED];
600 for (q_level = 1; q_level < ColorQuoteUsed; q_level++) {
601 if (ColorQuote[q_level] == A_NORMAL)
602 ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
606 ColorQuote[q_level] = fgbgattr_to_color (fg, bg, attr);
609 ColorDefs[object] = fgbgattr_to_color (fg, bg, attr);
612 if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors ())
613 BKGDSET (MT_COLOR_NORMAL);
619 int mutt_parse_color (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
624 if (option (OPTNOCURSES) || !has_colors ())
627 return _mutt_parse_color (buff, s, err, parse_color_pair, dry_run);
630 int mutt_parse_mono (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
635 if (option (OPTNOCURSES) || has_colors ())
638 return _mutt_parse_color (buff, s, err, parse_attr_spec, dry_run);