NeoMutt  2021-02-05
Teaching an old dog new tricks
DOXYGEN
color.h File Reference

Color and attribute parsing. More...

#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include "mutt/lib.h"
#include "mutt_commands.h"
+ Include dependency graph for color.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ColorLine
 A regular expression and a color to highlight a line. More...
 
struct  ColorList
 A set of colors. More...
 
struct  Colors
 
struct  EventColor
 An Event that happened to a Colour. More...
 

Enumerations

enum  ColorId {
  MT_COLOR_NONE = 50, MT_COLOR_ATTACHMENT, MT_COLOR_ATTACH_HEADERS, MT_COLOR_BODY,
  MT_COLOR_BOLD, MT_COLOR_COMPOSE_HEADER, MT_COLOR_COMPOSE_SECURITY_BOTH, MT_COLOR_COMPOSE_SECURITY_ENCRYPT,
  MT_COLOR_COMPOSE_SECURITY_NONE, MT_COLOR_COMPOSE_SECURITY_SIGN, MT_COLOR_ERROR, MT_COLOR_HDRDEFAULT,
  MT_COLOR_HEADER, MT_COLOR_INDICATOR, MT_COLOR_MARKERS, MT_COLOR_MESSAGE,
  MT_COLOR_MESSAGE_LOG, MT_COLOR_NORMAL, MT_COLOR_OPTIONS, MT_COLOR_PROGRESS,
  MT_COLOR_PROMPT, MT_COLOR_QUOTED, MT_COLOR_SEARCH, MT_COLOR_SIDEBAR_DIVIDER,
  MT_COLOR_SIDEBAR_FLAGGED, MT_COLOR_SIDEBAR_HIGHLIGHT, MT_COLOR_SIDEBAR_INDICATOR, MT_COLOR_SIDEBAR_NEW,
  MT_COLOR_SIDEBAR_ORDINARY, MT_COLOR_SIDEBAR_SPOOLFILE, MT_COLOR_SIDEBAR_UNREAD, MT_COLOR_SIGNATURE,
  MT_COLOR_STATUS, MT_COLOR_TILDE, MT_COLOR_TREE, MT_COLOR_UNDERLINE,
  MT_COLOR_WARNING, MT_COLOR_INDEX, MT_COLOR_INDEX_AUTHOR, MT_COLOR_INDEX_FLAGS,
  MT_COLOR_INDEX_SUBJECT, MT_COLOR_INDEX_TAG, MT_COLOR_INDEX_COLLAPSED, MT_COLOR_INDEX_DATE,
  MT_COLOR_INDEX_LABEL, MT_COLOR_INDEX_NUMBER, MT_COLOR_INDEX_SIZE, MT_COLOR_INDEX_TAGS,
  MT_COLOR_MAX
}
 List of all colored objects. More...
 
enum  NotifyColor { NT_COLOR_SET = 1, NT_COLOR_RESET }
 Types of Color Event. More...
 

Functions

 STAILQ_HEAD (ColorLineList, ColorLine)
 
int mutt_color_alloc (struct Colors *c, uint32_t fg, uint32_t bg)
 Allocate a colour pair. More...
 
int mutt_color_combine (struct Colors *c, uint32_t fg_attr, uint32_t bg_attr)
 Combine two colours. More...
 
void mutt_color_free (struct Colors *c, uint32_t fg, uint32_t bg)
 Free a colour. More...
 
struct Colorsmutt_colors_new (void)
 Create new colours. More...
 
void mutt_colors_free (struct Colors **ptr)
 Free all the colours. More...
 
enum CommandResult mutt_parse_color (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'color' command - Implements Command::parse() More...
 
enum CommandResult mutt_parse_mono (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'mono' command - Implements Command::parse() More...
 
enum CommandResult mutt_parse_uncolor (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'uncolor' command - Implements Command::parse() More...
 
enum CommandResult mutt_parse_unmono (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unmono' command - Implements Command::parse() More...
 

Detailed Description

Color and attribute parsing.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file color.h.

Enumeration Type Documentation

◆ ColorId

enum ColorId

List of all colored objects.

This enumeration starts at 50 to avoid any of the values being 37 (ASCII %). Inserting colour codes into expando strings, when one of the colour codes was '', was causing formatting problems.

Enumerator
MT_COLOR_NONE 
MT_COLOR_ATTACHMENT 

MIME attachments text (entire line)

MT_COLOR_ATTACH_HEADERS 

MIME attachment test (takes a pattern)

MT_COLOR_BODY 

Pager: highlight body of message (takes a pattern)

MT_COLOR_BOLD 

Bold text.

MT_COLOR_COMPOSE_HEADER 

Header labels, e.g. From:

MT_COLOR_COMPOSE_SECURITY_BOTH 

Mail will be encrypted and signed.

MT_COLOR_COMPOSE_SECURITY_ENCRYPT 

Mail will be encrypted.

MT_COLOR_COMPOSE_SECURITY_NONE 

Mail will not be encrypted or signed.

MT_COLOR_COMPOSE_SECURITY_SIGN 

Mail will be signed.

MT_COLOR_ERROR 

Error message.

MT_COLOR_HDRDEFAULT 

Header default colour.

MT_COLOR_HEADER 

Message headers (takes a pattern)

MT_COLOR_INDICATOR 

Selected item in list.

MT_COLOR_MARKERS 

Pager: markers, line continuation.

MT_COLOR_MESSAGE 

Informational message.

MT_COLOR_MESSAGE_LOG 

Menu showing log messages.

MT_COLOR_NORMAL 

Plain text.

MT_COLOR_OPTIONS 

Options in prompt.

MT_COLOR_PROGRESS 

Progress bar.

MT_COLOR_PROMPT 

Question/user input.

MT_COLOR_QUOTED 

Pager: quoted text.

MT_COLOR_SEARCH 

Pager: search matches.

MT_COLOR_SIDEBAR_DIVIDER 

Line dividing sidebar from the index/pager.

MT_COLOR_SIDEBAR_FLAGGED 

Mailbox with flagged messages.

MT_COLOR_SIDEBAR_HIGHLIGHT 

Select cursor.

MT_COLOR_SIDEBAR_INDICATOR 

Current open mailbox.

MT_COLOR_SIDEBAR_NEW 

Mailbox with new mail.

MT_COLOR_SIDEBAR_ORDINARY 

Mailbox with no new or flagged messages.

MT_COLOR_SIDEBAR_SPOOLFILE 

$spoolfile (Spool mailbox)

MT_COLOR_SIDEBAR_UNREAD 

Mailbox with unread mail.

MT_COLOR_SIGNATURE 

Pager: signature lines.

MT_COLOR_STATUS 

Status bar (takes a pattern)

MT_COLOR_TILDE 

Pager: empty lines after message.

MT_COLOR_TREE 

Index: tree-drawing characters.

MT_COLOR_UNDERLINE 

Underlined text.

MT_COLOR_WARNING 

Warning messages.

MT_COLOR_INDEX 

Index: default colour (takes a pattern)

MT_COLOR_INDEX_AUTHOR 

Index: author field (takes a pattern)

MT_COLOR_INDEX_FLAGS 

Index: flags field (takes a pattern)

MT_COLOR_INDEX_SUBJECT 

Index: subject field (takes a pattern)

MT_COLOR_INDEX_TAG 

Index: tag field (g, takes a pattern)

MT_COLOR_INDEX_COLLAPSED 

Index: number of messages in collapsed thread.

MT_COLOR_INDEX_DATE 

Index: date field.

MT_COLOR_INDEX_LABEL 

Index: label field.

MT_COLOR_INDEX_NUMBER 

Index: index number.

MT_COLOR_INDEX_SIZE 

Index: size field.

MT_COLOR_INDEX_TAGS 

Index: tags field (g, J)

MT_COLOR_MAX 

Definition at line 58 of file color.h.

59 {
60  MT_COLOR_NONE = 50,
83 #ifdef USE_SIDEBAR
92 #endif
99  /* please no non-MT_COLOR_INDEX objects after this point */
105  /* below here - only index coloring stuff that doesn't have a pattern */
112  MT_COLOR_MAX,
113 };
Mail will be encrypted.
Definition: color.h:67
MIME attachment test (takes a pattern)
Definition: color.h:62
Bold text.
Definition: color.h:64
Index: index number.
Definition: color.h:109
Header default colour.
Definition: color.h:71
Underlined text.
Definition: color.h:97
Progress bar.
Definition: color.h:79
Index: tags field (g, J)
Definition: color.h:111
Informational message.
Definition: color.h:75
Pager: highlight body of message (takes a pattern)
Definition: color.h:63
Mailbox with no new or flagged messages.
Definition: color.h:89
Line dividing sidebar from the index/pager.
Definition: color.h:84
Pager: signature lines.
Definition: color.h:93
Select cursor.
Definition: color.h:86
Pager: quoted text.
Definition: color.h:81
Index: number of messages in collapsed thread.
Definition: color.h:106
Mailbox with new mail.
Definition: color.h:88
Current open mailbox.
Definition: color.h:87
Menu showing log messages.
Definition: color.h:76
Index: author field (takes a pattern)
Definition: color.h:101
Pager: empty lines after message.
Definition: color.h:95
Message headers (takes a pattern)
Definition: color.h:72
Index: tag field (g, takes a pattern)
Definition: color.h:104
Index: tree-drawing characters.
Definition: color.h:96
Pager: markers, line continuation.
Definition: color.h:74
Plain text.
Definition: color.h:77
Index: date field.
Definition: color.h:107
Index: size field.
Definition: color.h:110
Status bar (takes a pattern)
Definition: color.h:94
Header labels, e.g. From:
Definition: color.h:65
Selected item in list.
Definition: color.h:73
Mail will be signed.
Definition: color.h:69
Mail will be encrypted and signed.
Definition: color.h:66
Index: label field.
Definition: color.h:108
Index: subject field (takes a pattern)
Definition: color.h:103
Error message.
Definition: color.h:70
MIME attachments text (entire line)
Definition: color.h:61
Mailbox with unread mail.
Definition: color.h:91
Pager: search matches.
Definition: color.h:82
Mail will not be encrypted or signed.
Definition: color.h:68
Options in prompt.
Definition: color.h:78
Index: default colour (takes a pattern)
Definition: color.h:100
Question/user input.
Definition: color.h:80
Mailbox with flagged messages.
Definition: color.h:85
Warning messages.
Definition: color.h:98
Index: flags field (takes a pattern)
Definition: color.h:102
$spoolfile (Spool mailbox)
Definition: color.h:90

◆ NotifyColor

Types of Color Event.

Observers of NT_COLOR will be passed an EventColor.

Enumerator
NT_COLOR_SET 

A new Color has been set.

NT_COLOR_RESET 

Color has been reset/removed.

Definition at line 169 of file color.h.

170 {
171  NT_COLOR_SET = 1,
173 };
A new Color has been set.
Definition: color.h:171
Color has been reset/removed.
Definition: color.h:172

Function Documentation

◆ STAILQ_HEAD()

STAILQ_HEAD ( ColorLineList  ,
ColorLine   
)

◆ mutt_color_alloc()

int mutt_color_alloc ( struct Colors c,
uint32_t  fg,
uint32_t  bg 
)

Allocate a colour pair.

Parameters
cColours
fgForeground colour ID
bgBackground colour ID
Return values
numCombined colour pair

Definition at line 472 of file color.c.

473 {
474 #ifdef USE_SLANG_CURSES
475  char fgc[128], bgc[128];
476 #endif
477  struct ColorList *p = c->user_colors;
478 
479  /* check to see if this color is already allocated to save space */
480  while (p)
481  {
482  if ((p->fg == fg) && (p->bg == bg))
483  {
484  (p->count)++;
485  return COLOR_PAIR(p->index);
486  }
487  p = p->next;
488  }
489 
490  /* check to see if there are colors left */
491  if (++c->num_user_colors > COLOR_PAIRS)
492  return A_NORMAL;
493 
494  /* find the smallest available index (object) */
495  int i = 1;
496  while (true)
497  {
498  p = c->user_colors;
499  while (p)
500  {
501  if (p->index == i)
502  break;
503  p = p->next;
504  }
505  if (!p)
506  break;
507  i++;
508  }
509 
510  p = mutt_mem_malloc(sizeof(struct ColorList));
511  p->next = c->user_colors;
512  c->user_colors = p;
513 
514  p->index = i;
515  p->count = 1;
516  p->bg = bg;
517  p->fg = fg;
518 
519 #ifdef USE_SLANG_CURSES
520  /*
521  * If using s-lang always use SLtt_set_color which allows using truecolor
522  * values. Note that I couldn't figure out if s-lang somehow reports
523  * truecolor support.
524  */
525  SLtt_set_color(i, NULL, get_color_name(fgc, sizeof(fgc), fg),
526  get_color_name(bgc, sizeof(bgc), bg));
527 #else
528 #ifdef HAVE_USE_DEFAULT_COLORS
529  if (fg == COLOR_DEFAULT)
530  fg = COLOR_UNSET;
531  if (bg == COLOR_DEFAULT)
532  bg = COLOR_UNSET;
533 #endif
534  init_pair(i, fg, bg);
535 #endif
536 
537  mutt_debug(LL_DEBUG3, "Color pairs used so far: %d\n", c->num_user_colors);
538 
539  return COLOR_PAIR(p->index);
540 }
#define COLOR_UNSET
Definition: color.c:65
#define COLOR_DEFAULT
Definition: color.c:71
int num_user_colors
Definition: color.h:147
short count
Definition: color.h:125
short index
Definition: color.h:124
uint32_t bg
Definition: color.h:123
struct ColorList * next
Definition: color.h:126
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
struct ColorList * user_colors
Definition: color.h:146
uint32_t fg
Definition: color.h:122
A set of colors.
Definition: color.h:118
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
Log at debug level 3.
Definition: logging.h:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_color_combine()

int mutt_color_combine ( struct Colors c,
uint32_t  fg_attr,
uint32_t  bg_attr 
)

Combine two colours.

Parameters
cColours
fg_attrColour pair of foreground to use
bg_attrColour pair of background to use
Return values
numColour pair of combined colour

Definition at line 577 of file color.c.

578 {
579  uint32_t fg = COLOR_DEFAULT;
580  uint32_t bg = COLOR_DEFAULT;
581 
582  mutt_lookup_color(c, fg_attr, &fg, NULL);
583  mutt_lookup_color(c, bg_attr, NULL, &bg);
584 
585  if ((fg == COLOR_DEFAULT) && (bg == COLOR_DEFAULT))
586  return A_NORMAL;
587  return mutt_color_alloc(c, fg, bg);
588 }
#define COLOR_DEFAULT
Definition: color.c:71
static int mutt_lookup_color(struct Colors *c, short pair, uint32_t *fg, uint32_t *bg)
Get the colours from a colour pair.
Definition: color.c:551
uint32_t bg
Definition: color.h:123
uint32_t fg
Definition: color.h:122
int mutt_color_alloc(struct Colors *c, uint32_t fg, uint32_t bg)
Allocate a colour pair.
Definition: color.c:472
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_color_free()

void mutt_color_free ( struct Colors c,
uint32_t  fg,
uint32_t  bg 
)

Free a colour.

Parameters
cColours
fgForeground colour ID
bgBackground colour ID

If there are no more users, the resource will be freed.

Definition at line 252 of file color.c.

253 {
254  struct ColorList *q = NULL;
255 
256  struct ColorList *p = c->user_colors;
257  while (p)
258  {
259  if ((p->fg == fg) && (p->bg == bg))
260  {
261  (p->count)--;
262  if (p->count > 0)
263  return;
264 
265  c->num_user_colors--;
266  mutt_debug(LL_DEBUG1, "Color pairs used so far: %d\n", c->num_user_colors);
267 
268  if (p == c->user_colors)
269  {
270  c->user_colors = c->user_colors->next;
271  FREE(&p);
272  return;
273  }
274  q = c->user_colors;
275  while (q)
276  {
277  if (q->next == p)
278  {
279  q->next = p->next;
280  FREE(&p);
281  return;
282  }
283  q = q->next;
284  }
285  /* can't get here */
286  }
287  p = p->next;
288  }
289 }
int num_user_colors
Definition: color.h:147
short count
Definition: color.h:125
uint32_t bg
Definition: color.h:123
struct ColorList * next
Definition: color.h:126
struct ColorList * user_colors
Definition: color.h:146
uint32_t fg
Definition: color.h:122
Log at debug level 1.
Definition: logging.h:40
A set of colors.
Definition: color.h:118
#define FREE(x)
Definition: memory.h:40
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
+ Here is the caller graph for this function:

◆ mutt_colors_new()

struct Colors* mutt_colors_new ( void  )

Create new colours.

Return values
ptrNew Colors

Definition at line 374 of file color.c.

375 {
376  struct Colors *c = mutt_mem_calloc(1, sizeof(*c));
377  c->notify = notify_new();
378 
379  quotes_init(c);
380  defs_init(c);
381 
383  STAILQ_INIT(&c->body_list);
384  STAILQ_INIT(&c->hdr_list);
387  STAILQ_INIT(&c->index_list);
391 
392 #ifdef HAVE_COLOR
393  start_color();
394 #endif
395 
397  return c;
398 }
struct ColorLineList status_list
List of colours applied to the status bar.
Definition: color.h:141
struct ColorLineList index_tag_list
List of colours applied to tags in the index.
Definition: color.h:140
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
struct Notify * notify
Notifications system.
Definition: color.h:149
struct ColorLineList body_list
List of colours applied to the email body.
Definition: color.h:134
struct ColorLineList index_flags_list
List of colours applied to the flags in the index.
Definition: color.h:137
struct ColorLineList index_subject_list
List of colours applied to the subject in the index.
Definition: color.h:139
Container for Accounts, Notifications.
Definition: neomutt.h:36
#define STAILQ_INIT(head)
Definition: queue.h:369
static void quotes_init(struct Colors *c)
Initialise the quoted-email colours.
Definition: color.c:206
struct ColorLineList hdr_list
List of colours applied to the email headers.
Definition: color.h:135
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:49
struct ColorLineList index_author_list
List of colours applied to the author in the index.
Definition: color.h:136
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
Definition: color.h:129
struct ColorLineList index_list
List of default colours applied to the index.
Definition: color.h:138
struct ColorLineList attach_list
List of colours applied to the attachment headers.
Definition: color.h:133
void notify_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition: notify.c:82
static void defs_init(struct Colors *c)
Initialise the simple colour definitions.
Definition: color.c:169
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_colors_free()

void mutt_colors_free ( struct Colors **  ptr)

Free all the colours.

Parameters
ptrColours

Definition at line 356 of file color.c.

357 {
358  if (!ptr || !*ptr)
359  return;
360 
361  struct Colors *c = *ptr;
362 
363  colors_clear(c);
364  defs_free(c);
365  quotes_free(c);
366  notify_free(&c->notify);
367  FREE(ptr);
368 }
struct Notify * notify
Notifications system.
Definition: color.h:149
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:62
static void colors_clear(struct Colors *c)
Reset all the colours.
Definition: color.c:334
Definition: color.h:129
#define FREE(x)
Definition: memory.h:40
static void quotes_free(struct Colors *c)
Free the quoted-email colours.
Definition: color.c:197
static void defs_free(struct Colors *c)
Free the simple colour definitions.
Definition: color.c:160
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_parse_color()

enum CommandResult mutt_parse_color ( struct Buffer buf,
struct Buffer s,
intptr_t  data,
struct Buffer err 
)

Parse the 'color' command - Implements Command::parse()

Definition at line 1346 of file color.c.

1348 {
1349  bool dry_run = false;
1350 
1351  if (OptNoCurses || !has_colors())
1352  dry_run = true;
1353 
1354  return parse_color(Colors, buf, s, err, parse_color_pair, dry_run, true);
1355 }
static enum CommandResult parse_color(struct Colors *c, struct Buffer *buf, struct Buffer *s, struct Buffer *err, parser_callback_t callback, bool dry_run, bool color)
Parse a &#39;color&#39; command.
Definition: color.c:1172
WHERE bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:48
Definition: color.h:129
static enum CommandResult parse_color_pair(struct Buffer *buf, struct Buffer *s, uint32_t *fg, uint32_t *bg, int *attr, struct Buffer *err)
Parse a pair of colours - Implements parser_callback_t.
Definition: color.c:1053
+ Here is the call graph for this function:

◆ mutt_parse_mono()

enum CommandResult mutt_parse_mono ( struct Buffer buf,
struct Buffer s,
intptr_t  data,
struct Buffer err 
)

Parse the 'mono' command - Implements Command::parse()

Definition at line 1361 of file color.c.

1363 {
1364  bool dry_run = false;
1365 
1366 #ifdef HAVE_COLOR
1367  if (OptNoCurses || has_colors())
1368  dry_run = true;
1369 #else
1370  if (OptNoCurses)
1371  dry_run = true;
1372 #endif
1373 
1374  return parse_color(Colors, buf, s, err, parse_attr_spec, dry_run, false);
1375 }
static enum CommandResult parse_color(struct Colors *c, struct Buffer *buf, struct Buffer *s, struct Buffer *err, parser_callback_t callback, bool dry_run, bool color)
Parse a &#39;color&#39; command.
Definition: color.c:1172
WHERE bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:48
static enum CommandResult parse_attr_spec(struct Buffer *buf, struct Buffer *s, uint32_t *fg, uint32_t *bg, int *attr, struct Buffer *err)
Parse an attribute description - Implements parser_callback_t.
Definition: color.c:1103
Definition: color.h:129
+ Here is the call graph for this function:

◆ mutt_parse_uncolor()

enum CommandResult mutt_parse_uncolor ( struct Buffer buf,
struct Buffer s,
intptr_t  data,
struct Buffer err 
)

Parse the 'uncolor' command - Implements Command::parse()

Definition at line 921 of file color.c.

923 {
924  return parse_uncolor(buf, s, Colors, err, true);
925 }
static enum CommandResult parse_uncolor(struct Buffer *buf, struct Buffer *s, struct Colors *c, struct Buffer *err, bool uncolor)
Parse an &#39;uncolor&#39; command.
Definition: color.c:820
Definition: color.h:129
+ Here is the call graph for this function:

◆ mutt_parse_unmono()

enum CommandResult mutt_parse_unmono ( struct Buffer buf,
struct Buffer s,
intptr_t  data,
struct Buffer err 
)

Parse the 'unmono' command - Implements Command::parse()

Definition at line 931 of file color.c.

933 {
934  return parse_uncolor(buf, s, Colors, err, false);
935 }
static enum CommandResult parse_uncolor(struct Buffer *buf, struct Buffer *s, struct Colors *c, struct Buffer *err, bool uncolor)
Parse an &#39;uncolor&#39; command.
Definition: color.c:820
Definition: color.h:129
+ Here is the call graph for this function: