NeoMutt  2023-05-17-56-ga67199
Teaching an old dog new tricks
DOXYGEN
command.c File Reference

Parse colour commands. More...

#include "config.h"
#include <stddef.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "lib.h"
#include "pager/lib.h"
#include "parse/lib.h"
#include "globals.h"
+ Include dependency graph for command.c:

Go to the source code of this file.

Enumerations

enum  ColorPrefix { COLOR_PREFIX_NONE , COLOR_PREFIX_ALERT , COLOR_PREFIX_BRIGHT , COLOR_PREFIX_LIGHT }
 Constants for colour prefixes of named colours. More...
 

Functions

static uint32_t color_xterm256_to_24bit (const uint32_t color)
 Convert a xterm color to its RGB value. More...
 
static void modify_color_by_prefix (enum ColorPrefix prefix, bool is_fg, uint32_t *col, int *attrs)
 Modify a colour/attributes based on a prefix, e.g. More...
 
static int parse_color_prefix (const char *s, enum ColorPrefix *prefix)
 Parse a colour prefix, e.g. More...
 
static enum CommandResult parse_color_namedcolor (const char *s, uint32_t *col, int *attrs, bool is_fg, struct Buffer *err)
 Parse a named colour, e.g. More...
 
static enum CommandResult parse_color_colornnn (const char *s, uint32_t *col, int *attrs, bool is_fg, struct Buffer *err)
 Parse a colorNNN, e.g. More...
 
static enum CommandResult parse_color_rrggbb (const char *s, uint32_t *col, int *attrs, bool is_fg, struct Buffer *err)
 Parse an RGB colour, e.g. More...
 
static enum CommandResult parse_color_name (const char *s, uint32_t *col, int *attrs, bool is_fg, struct Buffer *err)
 Parse a colour name. More...
 
static enum CommandResult parse_attr_spec (struct Buffer *buf, struct Buffer *s, uint32_t *fg, uint32_t *bg, int *attrs, struct Buffer *err)
 Parse an attribute description - Implements parser_callback_t -. More...
 
static enum CommandResult parse_color_pair (struct Buffer *buf, struct Buffer *s, uint32_t *fg, uint32_t *bg, int *attrs, struct Buffer *err)
 Parse a pair of colours - Implements parser_callback_t -. More...
 
void get_colorid_name (unsigned int cid, struct Buffer *buf)
 Get the name of a color id. More...
 
static enum CommandResult parse_object (struct Buffer *buf, struct Buffer *s, enum ColorId *cid, int *ql, struct Buffer *err)
 Identify a colour object. More...
 
static enum CommandResult parse_uncolor (struct Buffer *buf, struct Buffer *s, struct Buffer *err, bool uncolor)
 Parse an 'uncolor' command. More...
 
static enum CommandResult parse_color (struct Buffer *buf, struct Buffer *s, struct Buffer *err, parser_callback_t callback, bool dry_run, bool color)
 Parse a 'color' command. 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...
 
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...
 

Variables

const struct Mapping ColorFields []
 Mapping of colour names to their IDs. More...
 
const struct Mapping ComposeColorFields []
 Mapping of compose colour names to their IDs. More...
 

Detailed Description

Parse colour commands.

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 command.c.

Enumeration Type Documentation

◆ ColorPrefix

Constants for colour prefixes of named colours.

Enumerator
COLOR_PREFIX_NONE 

no prefix

COLOR_PREFIX_ALERT 

"alert" colour prefix

COLOR_PREFIX_BRIGHT 

"bright" colour prefix

COLOR_PREFIX_LIGHT 

"light" colour prefix

Definition at line 122 of file command.c.

123{
128};
@ COLOR_PREFIX_NONE
no prefix
Definition: command.c:124
@ COLOR_PREFIX_ALERT
"alert" colour prefix
Definition: command.c:125
@ COLOR_PREFIX_LIGHT
"light" colour prefix
Definition: command.c:127
@ COLOR_PREFIX_BRIGHT
"bright" colour prefix
Definition: command.c:126

Function Documentation

◆ color_xterm256_to_24bit()

static uint32_t color_xterm256_to_24bit ( const uint32_t  color)
static

Convert a xterm color to its RGB value.

Parameters
[in]colorxterm color number to be converted
Return values
numThe color's RGB value as number with value 0xRRGGBB

There are 256 xterm colors numbered 0 to 255.

Caller contract: color must be between 0 and 255.

Xterm Color Codes

Basic and Bright Colors

  • 0-7 correspond to the 8 terminal colours
  • 8-15 are the bright variants of 0-7
0 #000000 1 #800000 2 #008000 3 #808000 4 #000080 5 #800080 6 #008080 7 #c0c0c0
8 #808080 9 #ff0000 10 #00ff00 11 #ffff00 12 #0000ff 13 #ff00ff 14 #00ffff 15 #ffffff

Color palette

16 #000000 17 #00005f 18 #000087 19 #0000af 20 #0000d7 21 #0000ff
22 #005f00 23 #005f5f 24 #005f87 25 #005faf 26 #005fd7 27 #005fff
28 #008700 29 #00875f 30 #008787 31 #0087af 32 #0087d7 33 #0087ff
34 #00af00 35 #00af5f 36 #00af87 37 #00afaf 38 #00afd7 39 #00afff
40 #00d700 41 #00d75f 42 #00d787 43 #00d7af 44 #00d7d7 45 #00d7ff
46 #00ff00 47 #00ff5f 48 #00ff87 49 #00ffaf 50 #00ffd7 51 #00ffff
52 #5f0000 53 #5f005f 54 #5f0087 55 #5f00af 56 #5f00d7 57 #5f00ff
58 #5f5f00 59 #5f5f5f 60 #5f5f87 61 #5f5faf 62 #5f5fd7 63 #5f5fff
64 #5f8700 65 #5f875f 66 #5f8787 67 #5f87af 68 #5f87d7 69 #5f87ff
70 #5faf00 71 #5faf5f 72 #5faf87 73 #5fafaf 74 #5fafd7 75 #5fafff
76 #5fd700 77 #5fd75f 78 #5fd787 79 #5fd7af 80 #5fd7d7 81 #5fd7ff
82 #5fff00 83 #5fff5f 84 #5fff87 85 #5fffaf 86 #5fffd7 87 #5fffff
88 #870000 89 #87005f 90 #870087 91 #8700af 92 #8700d7 93 #8700ff
94 #875f00 95 #875f5f 96 #875f87 97 #875faf 98 #875fd7 99 #875fff
100 #878700 101 #87875f 102 #878787 103 #8787af 104 #8787d7 105 #8787ff
106 #87af00 107 #87af5f 108 #87af87 109 #87afaf 110 #87afd7 111 #87afff
112 #87d700 113 #87d75f 114 #87d787 115 #87d7af 116 #87d7d7 117 #87d7ff
118 #87ff00 119 #87ff5f 120 #87ff87 121 #87ffaf 122 #87ffd7 123 #87ffff
124 #af0000 125 #af005f 126 #af0087 127 #af00af 128 #af00d7 129 #af00ff
130 #af5f00 131 #af5f5f 132 #af5f87 133 #af5faf 134 #af5fd7 135 #af5fff
136 #af8700 137 #af875f 138 #af8787 139 #af87af 140 #af87d7 141 #af87ff
142 #afaf00 143 #afaf5f 144 #afaf87 145 #afafaf 146 #afafd7 147 #afafff
148 #afd700 149 #afd75f 150 #afd787 151 #afd7af 152 #afd7d7 153 #afd7ff
154 #afff00 155 #afff5f 156 #afff87 157 #afffaf 158 #afffd7 159 #afffff
160 #d70000 161 #d7005f 162 #d70087 163 #d700af 164 #d700d7 165 #d700ff
166 #d75f00 167 #d75f5f 168 #d75f87 169 #d75faf 170 #d75fd7 171 #d75fff
172 #d78700 173 #d7875f 174 #d78787 175 #d787af 176 #d787d7 177 #d787ff
178 #d7af00 179 #d7af5f 180 #d7af87 181 #d7afaf 182 #d7afd7 183 #d7afff
184 #d7d700 185 #d7d75f 186 #d7d787 187 #d7d7af 188 #d7d7d7 189 #d7d7ff
190 #d7ff00 191 #d7ff5f 192 #d7ff87 193 #d7ffaf 194 #d7ffd7 195 #d7ffff
196 #ff0000 197 #ff005f 198 #ff0087 199 #ff00af 200 #ff00d7 201 #ff00ff
202 #ff5f00 203 #ff5f5f 204 #ff5f87 205 #ff5faf 206 #ff5fd7 207 #ff5fff
208 #ff8700 209 #ff875f 210 #ff8787 211 #ff87af 212 #ff87d7 213 #ff87ff
214 #ffaf00 215 #ffaf5f 216 #ffaf87 217 #ffafaf 218 #ffafd7 219 #ffafff
220 #ffd700 221 #ffd75f 222 #ffd787 223 #ffd7af 224 #ffd7d7 225 #ffd7ff
226 #ffff00 227 #ffff5f 228 #ffff87 229 #ffffaf 230 #ffffd7 231 #ffffff

Grey Scale Ramp

232 #080808 233 #121212 234 #1c1c1c 235 #262626 236 #303030 237 #3a3a3a 238 #444444 239 #4e4e4e
240 #585858 241 #606060 242 #666666 243 #767676 244 #808080 245 #8a8a8a 246 #949494 247 #9e9e9e
248 #a8a8a8 249 #b2b2b2 250 #bcbcbc 251 #c6c6c6 252 #d0d0d0 253 #dadada 254 #e4e4e4 255 #eeeeee

Definition at line 201 of file command.c.

202{
203 static const uint32_t basic[] = {
204 0x000000, 0x800000, 0x008000, 0x808000, 0x000080, 0x800080,
205 0x008080, 0xc0c0c0, 0x808080, 0xff0000, 0x00ff00, 0xffff00,
206 0x0000ff, 0xff00ff, 0x00ffff, 0xffffff,
207 };
208
209 assert(color < 256);
210
211 if (color < 16)
212 {
213 color_debug(LL_DEBUG5, "Converted color 0-15: %d\n", color);
214 /* The first 16 colours are the "usual" terminal colours */
215 return basic[color];
216 }
217
218 if (color < 232)
219 {
220 /* The Color palette is divided in 6x6x6 colours, i.e. each R, G, B channel
221 * has six values:
222 *
223 * value: 1 2 3 4 5 6
224 * color: 0x00 0x5f 0x87 0xaf 0xd7 0xff
225 *
226 * The steps between the values is 0x28 = 40, the EXCEPT for the first one
227 * where it is 0x5f = 95.
228 *
229 * If we express the xterm color number minus 16 to base 6, i.e.
230 *
231 * color - 16 = vr * 36 + vg * 6 + vb * 1
232 *
233 * with vr, vg, vb integers between 0 and 5, then vr, vg, vb is the channel
234 * value for red, green, and blue, respectively.
235 */
236
237 uint32_t normalised_color = color - 16;
238 uint32_t vr = (normalised_color % 216) / 36; /* 216 = 6*6*6 */
239 uint32_t vg = (normalised_color % 36) / 6;
240 uint32_t vb = (normalised_color % 6) / 1;
241
242 /* First step is wider than the other ones, so add the difference if needed */
243 uint32_t r = vr * 0x28 + ((vr > 0) ? (0x5f - 0x40) : 0);
244 uint32_t g = vg * 0x28 + ((vg > 0) ? (0x5f - 0x40) : 0);
245 uint32_t b = vb * 0x28 + ((vb > 0) ? (0x5f - 0x40) : 0);
246
247 uint32_t rgb = (r << 16) + (g << 8) + (b << 0);
248 color_debug(LL_DEBUG5, "Converted xterm color %d to RGB #%x:\n", color, rgb);
249 return rgb;
250 }
251
252 /* Grey scale starts at 0x08 and adds 0xa = 10 in very step ending in 0xee.
253 * There are a total of 6*4 = 24 grey colors in total. */
254 uint32_t steps = color - 232;
255 uint32_t grey = (steps * 0x0a) + 0x08;
256 uint32_t rgb = (grey << 16) + (grey << 8) + (grey << 0);
257 color_debug(LL_DEBUG5, "Converted xterm color %d to RGB #%x:\n", color, rgb);
258 return rgb;
259}
int color_debug(enum LogLevel level, const char *format,...)
Write to the log file.
Definition: debug.c:44
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modify_color_by_prefix()

static void modify_color_by_prefix ( enum ColorPrefix  prefix,
bool  is_fg,
uint32_t *  col,
int *  attrs 
)
static

Modify a colour/attributes based on a prefix, e.g.

"bright"

Parameters
[in]prefixprefix to apply
[in]is_fgtrue if a foreground colour should be modified
[in,out]colcolour to modify
[in,out]attrsattributes to modify

Definition at line 269 of file command.c.

271{
272 if (prefix == COLOR_PREFIX_NONE)
273 return; // nothing to do here
274
275 if (prefix == COLOR_PREFIX_ALERT)
276 {
277 *attrs |= A_BOLD;
278 *attrs |= A_BLINK;
279 }
280 else if (is_fg)
281 {
282 if ((COLORS >= 16) && (prefix == COLOR_PREFIX_LIGHT))
283 {
284 if (*col <= 7)
285 {
286 /* Advance the color 0-7 by 8 to get the light version */
287 *col += 8;
288 }
289 }
290 else
291 {
292 *attrs |= A_BOLD;
293 }
294 }
295 else
296 {
297 if (COLORS >= 16)
298 {
299 if (*col <= 7)
300 {
301 /* Advance the color 0-7 by 8 to get the light version */
302 *col += 8;
303 }
304 }
305 }
306}
+ Here is the caller graph for this function:

◆ parse_color_prefix()

static int parse_color_prefix ( const char *  s,
enum ColorPrefix prefix 
)
static

Parse a colour prefix, e.g.

"bright"

Parameters
[in]sString to parse
[out]prefixparsed prefix, see ColorPrefix
Return values
numLength of the matched prefix
0No prefix matched

If prefixes should be parsed, but their value is irrelevant, NULL can be passed as 'prefix'.

Definition at line 318 of file command.c.

319{
320 int clen = 0;
321
322 if ((clen = mutt_istr_startswith(s, "bright")))
323 {
324 color_debug(LL_DEBUG5, "bright\n");
325 if (prefix)
326 *prefix = COLOR_PREFIX_BRIGHT;
327 }
328 else if ((clen = mutt_istr_startswith(s, "alert")))
329 {
330 color_debug(LL_DEBUG5, "alert\n");
331 if (prefix)
332 *prefix = COLOR_PREFIX_ALERT;
333 }
334 else if ((clen = mutt_istr_startswith(s, "light")))
335 {
336 color_debug(LL_DEBUG5, "light\n");
337 if (prefix)
338 *prefix = COLOR_PREFIX_LIGHT;
339 }
340
341 return clen;
342}
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition: string.c:240
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_color_namedcolor()

static enum CommandResult parse_color_namedcolor ( const char *  s,
uint32_t *  col,
int *  attrs,
bool  is_fg,
struct Buffer err 
)
static

Parse a named colour, e.g.

"brightred"

Parameters
[in]sString to parse
[out]colNumber for 'colorNNN' colours
[out]attrsAttributes, e.g. A_UNDERLINE
[in]is_fgtrue if this is a foreground colour
[out]errBuffer for error messages
Return values
MUTT_CMD_SUCCESSColour parsed successfully
MUTT_CMD_WARNINGUnknown colour, try other parsers

Definition at line 354 of file command.c.

356{
357 enum ColorPrefix prefix = COLOR_PREFIX_NONE;
358 s += parse_color_prefix(s, &prefix);
359
360 if ((*col = mutt_map_get_value(s, ColorNames)) == -1)
361 return MUTT_CMD_WARNING;
362
363 const char *name = mutt_map_get_name(*col, ColorNames);
364 if (name)
365 color_debug(LL_DEBUG5, "color: %s\n", name);
366
367 modify_color_by_prefix(prefix, is_fg, col, attrs);
368
369#ifdef NEOMUTT_DIRECT_COLORS
370 /* If we are running in direct color mode, we must convert the color
371 * number 0-15 to an RGB value.
372 * The first 16 colours of the xterm palette correspond to the terminal
373 * colours. Note that this replace the colour with a predefined RGB value
374 * and not the RGB value the terminal configured to use.
375 *
376 * Note that some colors are "special" e.g. "default" and do not fall in
377 * the range from 0 to 15. These must not be converted.
378 */
379 const bool c_color_directcolor = cs_subset_bool(NeoMutt->sub, "color_directcolor");
380 if (c_color_directcolor && (*col < 16))
381 {
382 *col = color_xterm256_to_24bit(*col);
383 }
384#endif
385 return MUTT_CMD_SUCCESS;
386}
static int parse_color_prefix(const char *s, enum ColorPrefix *prefix)
Parse a colour prefix, e.g.
Definition: command.c:318
static uint32_t color_xterm256_to_24bit(const uint32_t color)
Convert a xterm color to its RGB value.
Definition: command.c:201
ColorPrefix
Constants for colour prefixes of named colours.
Definition: command.c:123
static void modify_color_by_prefix(enum ColorPrefix prefix, bool is_fg, uint32_t *col, int *attrs)
Modify a colour/attributes based on a prefix, e.g.
Definition: command.c:269
const struct Mapping ColorNames[]
Mapping between a colour name and an ncurses colour.
Definition: color.c:39
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:39
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition: command.h:38
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
int mutt_map_get_value(const char *name, const struct Mapping *map)
Lookup the constant for a string.
Definition: mapping.c:85
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_color_colornnn()

static enum CommandResult parse_color_colornnn ( const char *  s,
uint32_t *  col,
int *  attrs,
bool  is_fg,
struct Buffer err 
)
static

Parse a colorNNN, e.g.

"color123".

Parameters
[in]sString to parse
[out]colNumber for 'colorNNN' colours
[out]attrsAttributes, e.g. A_UNDERLINE
[in]is_fgtrue if this is a foreground colour
[out]errBuffer for error messages
Return values
MUTT_CMD_SUCCESSColour parsed successfully
MUTT_CMD_WARNINGUnknown colour, try other parsers
MUTT_CMD_ERRORError, colour could not be parsed

On MUTT_CMD_ERROR, an error message will be written to err.

Definition at line 401 of file command.c.

403{
404 /* prefixes bright, alert, light are only allowed for named colours and
405 * colorNNN for backwards compatibility. */
406 enum ColorPrefix prefix = COLOR_PREFIX_NONE;
407 s += parse_color_prefix(s, &prefix);
408
409 int clen = 0;
410 /* allow aliases for xterm color resources */
411 if ((clen = mutt_istr_startswith(s, "color")) == 0)
412 return MUTT_CMD_WARNING;
413
414 s += clen;
415 char *eptr = NULL;
416 *col = strtoul(s, &eptr, 10);
417 /* There are only 256 xterm colors. Do not confuse with COLORS which is
418 * the number of colours the terminal supports (usually one of 16, 256,
419 * 16777216 (=24bit)). */
420 if ((*s == '\0') || (*eptr != '\0') || (*col >= 256) || ((*col >= COLORS) && !OptNoCurses))
421 {
422 buf_printf(err, _("%s: color not supported by term"), s);
423 return MUTT_CMD_ERROR;
424 }
425
426 modify_color_by_prefix(prefix, is_fg, col, attrs);
427
428#ifdef NEOMUTT_DIRECT_COLORS
429 const bool c_color_directcolor = cs_subset_bool(NeoMutt->sub, "color_directcolor");
430 if (c_color_directcolor)
431 {
432 /* If we are running in direct color mode, we must convert the xterm
433 * color numbers 0-255 to an RGB value. */
434 *col = color_xterm256_to_24bit(*col);
435 /* FIXME: The color values 0 to 7 (both inclusive) are still occupied by
436 * the default terminal colours. As a workaround we round them up to
437 * #000008 which is the blackest black we can produce. */
438 if (*col < 8)
439 *col = 8;
440 }
441#endif
442 color_debug(LL_DEBUG5, "colorNNN %d\n", *col);
443 return MUTT_CMD_SUCCESS;
444}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:173
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:37
bool OptNoCurses
(pseudo) when sending in batch mode
Definition: globals.c:82
#define _(a)
Definition: message.h:28
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_color_rrggbb()

static enum CommandResult parse_color_rrggbb ( const char *  s,
uint32_t *  col,
int *  attrs,
bool  is_fg,
struct Buffer err 
)
static

Parse an RGB colour, e.g.

"#12FE45"

Parameters
[in]sString to parse
[out]colNumber for 'colorNNN' colours
[out]attrsAttributes, e.g. A_UNDERLINE
[in]is_fgtrue if this is a foreground colour
[out]errBuffer for error messages
Return values
MUTT_CMD_SUCCESSColour parsed successfully
MUTT_CMD_WARNINGUnknown colour, try other parsers
MUTT_CMD_ERRORError, colour could not be parsed

On MUTT_CMD_ERROR, an error message will be written to err.

Definition at line 459 of file command.c.

461{
462 /* parse #RRGGBB colours */
463 if (s[0] != '#')
464 return MUTT_CMD_WARNING;
465
466#ifndef NEOMUTT_DIRECT_COLORS
467 buf_printf(err, _("Direct colors support not compiled in: %s"), s);
468 return MUTT_CMD_ERROR;
469#endif
470 const bool c_color_directcolor = cs_subset_bool(NeoMutt->sub, "color_directcolor");
471 if (!c_color_directcolor)
472 {
473 buf_printf(err, _("Direct colors support disabled: %s"), s);
474 return MUTT_CMD_ERROR;
475 }
476 s++;
477 char *eptr = NULL;
478 *col = strtoul(s, &eptr, 16);
479 if ((*s == '\0') || (*eptr != '\0') || ((*col >= COLORS) && !OptNoCurses))
480 {
481 buf_printf(err, _("%s: color not supported by term"), s);
482 return MUTT_CMD_ERROR;
483 }
484 /* FIXME: The color values 0 to 7 (both inclusive) are still occupied by
485 * the default terminal colours. As a workaround we round them up to
486 * #000008 which is the blackest black we can produce. */
487 if (*col < 8)
488 *col = 8;
489
490 color_debug(LL_DEBUG5, "#RRGGBB: %d\n", *col);
491 return MUTT_CMD_SUCCESS;
492}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_color_name()

static enum CommandResult parse_color_name ( const char *  s,
uint32_t *  col,
int *  attrs,
bool  is_fg,
struct Buffer err 
)
static

Parse a colour name.

Parameters
[in]sString to parse
[out]colNumber for 'colorNNN' colours
[out]attrsAttributes, e.g. A_UNDERLINE
[in]is_fgtrue if this is a foreground colour
[out]errBuffer for error messages
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

Parse a colour name, such as "red", "brightgreen", "color123", "#12FE45"

Definition at line 505 of file command.c.

507{
508 mutt_debug(LL_DEBUG5, "Parsing color name: %s\n", s);
509
510 /* Try the different colour syntaxes. A return value of MUTT_CMD_WARNING
511 * means, we should try the next syntax. */
512 enum CommandResult cr;
513
514 /* #RRGGBB */
515 cr = parse_color_rrggbb(s, col, attrs, is_fg, err);
516 if (cr != MUTT_CMD_WARNING)
517 return cr;
518 /* color123 */
519 cr = parse_color_colornnn(s, col, attrs, is_fg, err);
520 if (cr != MUTT_CMD_WARNING)
521 return cr;
522 /* named color, e.g. "brightred" */
523 cr = parse_color_namedcolor(s, col, attrs, is_fg, err);
524 if (cr != MUTT_CMD_WARNING)
525 return cr;
526
527 buf_printf(err, _("%s: no such color"), s);
528 return MUTT_CMD_WARNING;
529}
static enum CommandResult parse_color_colornnn(const char *s, uint32_t *col, int *attrs, bool is_fg, struct Buffer *err)
Parse a colorNNN, e.g.
Definition: command.c:401
static enum CommandResult parse_color_rrggbb(const char *s, uint32_t *col, int *attrs, bool is_fg, struct Buffer *err)
Parse an RGB colour, e.g.
Definition: command.c:459
static enum CommandResult parse_color_namedcolor(const char *s, uint32_t *col, int *attrs, bool is_fg, struct Buffer *err)
Parse a named colour, e.g.
Definition: command.c:354
CommandResult
Error codes for command_t parse functions.
Definition: command.h:36
#define mutt_debug(LEVEL,...)
Definition: logging2.h:87
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_colorid_name()

void get_colorid_name ( unsigned int  cid,
struct Buffer buf 
)

Get the name of a color id.

Parameters
cidColour, e.g. MT_COLOR_HEADER
bufBuffer for result

Definition at line 667 of file command.c.

668{
669 const char *name = NULL;
670
672 {
674 if (name)
675 {
676 buf_printf(buf, "compose %s", name);
677 return;
678 }
679 }
680
681 name = mutt_map_get_name(cid, ColorFields);
682 if (name)
683 buf_printf(buf, "%s", name);
684 else
685 buf_printf(buf, "UNKNOWN %d", cid);
686}
const struct Mapping ComposeColorFields[]
Mapping of compose colour names to their IDs.
Definition: command.c:108
const struct Mapping ColorFields[]
Mapping of colour names to their IDs.
Definition: command.c:53
@ MT_COLOR_COMPOSE_SECURITY_SIGN
Mail will be signed.
Definition: color.h:48
@ MT_COLOR_COMPOSE_HEADER
Header labels, e.g. From:
Definition: color.h:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_object()

static enum CommandResult parse_object ( struct Buffer buf,
struct Buffer s,
enum ColorId cid,
int *  ql,
struct Buffer err 
)
static

Identify a colour object.

Parameters
[in]bufTemporary Buffer space
[in]sBuffer containing string to be parsed
[out]cidObject type, e.g. MT_COLOR_TILDE
[out]qlQuote level, if type MT_COLOR_QUOTED
[out]errBuffer for error messages
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

Identify a colour object, e.g. "quoted", "compose header"

Definition at line 699 of file command.c.

701{
702 int rc;
703
704 if (mutt_str_startswith(buf->data, "quoted") != 0)
705 {
706 int val = 0;
707 if (buf->data[6] != '\0')
708 {
709 if (!mutt_str_atoi_full(buf->data + 6, &val) || (val > COLOR_QUOTES_MAX))
710 {
711 buf_printf(err, _("%s: no such object"), buf->data);
712 return MUTT_CMD_WARNING;
713 }
714 }
715
716 *ql = val;
717 *cid = MT_COLOR_QUOTED;
718 return MUTT_CMD_SUCCESS;
719 }
720
721 if (mutt_istr_equal(buf->data, "compose"))
722 {
723 if (!MoreArgs(s))
724 {
725 buf_printf(err, _("%s: too few arguments"), "color");
726 return MUTT_CMD_WARNING;
727 }
728
730
732 if (rc == -1)
733 {
734 buf_printf(err, _("%s: no such object"), buf->data);
735 return MUTT_CMD_WARNING;
736 }
737
738 *cid = rc;
739 return MUTT_CMD_SUCCESS;
740 }
741
743 if (rc == -1)
744 {
745 buf_printf(err, _("%s: no such object"), buf->data);
746 return MUTT_CMD_WARNING;
747 }
748 else
749 {
750 color_debug(LL_DEBUG5, "object: %s\n", mutt_map_get_name(rc, ColorFields));
751 }
752
753 *cid = rc;
754 return MUTT_CMD_SUCCESS;
755}
@ MT_COLOR_QUOTED
Pager: quoted text.
Definition: color.h:61
int parse_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: extract.c:47
#define MoreArgs(buf)
Definition: extract.h:30
#define TOKEN_NO_FLAGS
No flags are set.
Definition: extract.h:44
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:810
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:228
#define COLOR_QUOTES_MAX
Ten colours, quoted0..quoted9 (quoted and quoted0 are equivalent)
Definition: quoted.h:37
char * data
Pointer to data.
Definition: buffer.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_uncolor()

static enum CommandResult parse_uncolor ( struct Buffer buf,
struct Buffer s,
struct Buffer err,
bool  uncolor 
)
static

Parse an 'uncolor' command.

Parameters
bufTemporary Buffer space
sBuffer containing string to be parsed
errBuffer for error messages
uncolorIf true, 'uncolor', else 'unmono'
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

Usage:

  • uncolor index pattern [pattern...]
  • unmono index pattern [pattern...]

Definition at line 769 of file command.c.

771{
773
774 if (mutt_str_equal(buf->data, "*"))
775 {
776 colors_clear();
777 return MUTT_CMD_SUCCESS;
778 }
779
780 unsigned int cid = MT_COLOR_NONE;
781 int ql = 0;
782 color_debug(LL_DEBUG5, "uncolor: %s\n", buf_string(buf));
783 enum CommandResult rc = parse_object(buf, s, &cid, &ql, err);
784 if (rc != MUTT_CMD_SUCCESS)
785 return rc;
786
787 if (cid == -1)
788 {
789 buf_printf(err, _("%s: no such object"), buf->data);
790 return MUTT_CMD_ERROR;
791 }
792
793 if (cid == MT_COLOR_QUOTED)
794 {
795 color_debug(LL_DEBUG5, "quoted\n");
796 return quoted_colors_parse_uncolor(cid, ql, err);
797 }
798
799 if ((cid == MT_COLOR_STATUS) && !MoreArgs(s))
800 {
801 color_debug(LL_DEBUG5, "simple\n");
802 simple_color_reset(cid); // default colour for the status bar
803 return MUTT_CMD_SUCCESS;
804 }
805
806 if (!mutt_color_has_pattern(cid))
807 {
808 color_debug(LL_DEBUG5, "simple\n");
810 return MUTT_CMD_SUCCESS;
811 }
812
813 if (OptNoCurses)
814 {
815 do
816 {
817 color_debug(LL_DEBUG5, "do nothing\n");
818 /* just eat the command, but don't do anything real about it */
820 } while (MoreArgs(s));
821
822 return MUTT_CMD_SUCCESS;
823 }
824
825 bool changes = false;
826 if (!MoreArgs(s))
827 {
828 if (regex_colors_parse_uncolor(cid, NULL, uncolor))
829 return MUTT_CMD_SUCCESS;
830 else
831 return MUTT_CMD_ERROR;
832 }
833
834 do
835 {
837 if (mutt_str_equal("*", buf->data))
838 {
839 if (regex_colors_parse_uncolor(cid, NULL, uncolor))
840 return MUTT_CMD_SUCCESS;
841 else
842 return MUTT_CMD_ERROR;
843 }
844
845 changes |= regex_colors_parse_uncolor(cid, buf->data, uncolor);
846
847 } while (MoreArgs(s));
848
849 if (changes)
851
852 return MUTT_CMD_SUCCESS;
853}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:90
static enum CommandResult parse_object(struct Buffer *buf, struct Buffer *s, enum ColorId *cid, int *ql, struct Buffer *err)
Identify a colour object.
Definition: command.c:699
bool regex_colors_parse_uncolor(enum ColorId cid, const char *pat, bool uncolor)
Parse a Regex 'uncolor' command.
Definition: regex.c:439
void simple_color_reset(enum ColorId cid)
Clear the colour of a simple object.
Definition: simple.c:144
bool mutt_color_has_pattern(enum ColorId cid)
Check if a color object supports a regex pattern.
Definition: color.c:103
void colors_clear(void)
Reset all the simple, quoted and regex colours.
Definition: color.c:57
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition: color.h:75
@ MT_COLOR_NONE
Definition: color.h:39
void regex_colors_dump_all(void)
Dump all the Regex colours to the log.
Definition: debug.c:380
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
enum CommandResult quoted_colors_parse_uncolor(enum ColorId cid, int q_level, struct Buffer *err)
Parse the 'uncolor quoted' command.
Definition: quoted.c:163
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_color()

static enum CommandResult parse_color ( struct Buffer buf,
struct Buffer s,
struct Buffer err,
parser_callback_t  callback,
bool  dry_run,
bool  color 
)
static

Parse a 'color' command.

Parameters
bufTemporary Buffer space
sBuffer containing string to be parsed
errBuffer for error messages
callbackFunction to handle command - Implements parser_callback_t
dry_runIf true, test the command, but don't apply it
colorIf true "color", else "mono"
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

Usage:

  • color OBJECT [ ATTRS ] FG BG [ REGEX ]
  • mono OBJECT ATTRS [ REGEX ]

Definition at line 1051 of file command.c.

1054{
1055 int attrs = 0, q_level = 0;
1056 uint32_t fg = 0, bg = 0, match = 0;
1057 enum ColorId cid = MT_COLOR_NONE;
1058 enum CommandResult rc;
1059
1060 if (!MoreArgs(s))
1061 {
1062#ifdef USE_DEBUG_COLOR
1063 if (StartupComplete)
1064 return color_dump(buf, s, 0, err);
1065#endif
1066
1067 buf_printf(err, _("%s: too few arguments"), "color");
1068 return MUTT_CMD_WARNING;
1069 }
1070
1072 color_debug(LL_DEBUG5, "color: %s\n", buf_string(buf));
1073
1074 rc = parse_object(buf, s, &cid, &q_level, err);
1075 if (rc != MUTT_CMD_SUCCESS)
1076 return rc;
1077
1078 rc = callback(buf, s, &fg, &bg, &attrs, err);
1079 if (rc != MUTT_CMD_SUCCESS)
1080 return rc;
1081
1082 /* extract a regular expression if needed */
1083
1084 if (mutt_color_has_pattern(cid) && cid != MT_COLOR_STATUS)
1085 {
1086 color_debug(LL_DEBUG5, "regex needed\n");
1087 if (MoreArgs(s))
1088 {
1090 }
1091 else
1092 {
1093 buf_strcpy(buf, ".*");
1094 }
1095 }
1096
1097 if (MoreArgs(s) && (cid != MT_COLOR_STATUS))
1098 {
1099 buf_printf(err, _("%s: too many arguments"), color ? "color" : "mono");
1100 return MUTT_CMD_WARNING;
1101 }
1102
1103 if (dry_run)
1104 {
1105 color_debug(LL_DEBUG5, "dry_run bailout\n");
1106 *s->dptr = '\0'; /* fake that we're done parsing */
1107 return MUTT_CMD_SUCCESS;
1108 }
1109
1110 /* The case of the tree object is special, because a non-default fg color of
1111 * the tree element may be combined dynamically with the default bg color of
1112 * an index line, not necessarily defined in a rc file. */
1113 if (!OptNoCurses &&
1114 ((fg == COLOR_DEFAULT) || (bg == COLOR_DEFAULT) || (cid == MT_COLOR_TREE)) &&
1115 (use_default_colors() != OK))
1116 {
1117 buf_strcpy(err, _("default colors not supported"));
1118 return MUTT_CMD_ERROR;
1119 }
1120
1121 if (regex_colors_parse_color_list(cid, buf->data, fg, bg, attrs, &rc, err))
1122 {
1123 color_debug(LL_DEBUG5, "regex_colors_parse_color_list done\n");
1124 return rc;
1125 // do nothing
1126 }
1127 else if (quoted_colors_parse_color(cid, fg, bg, attrs, q_level, &rc, err))
1128 {
1129 color_debug(LL_DEBUG5, "quoted_colors_parse_color done\n");
1130 return rc;
1131 // do nothing
1132 }
1133 else if ((cid == MT_COLOR_STATUS) && MoreArgs(s))
1134 {
1135 color_debug(LL_DEBUG5, "status\n");
1136 /* 'color status fg bg' can have up to 2 arguments:
1137 * 0 arguments: sets the default status color (handled below by else part)
1138 * 1 argument : colorize pattern on match
1139 * 2 arguments: colorize nth submatch of pattern */
1141
1142 if (MoreArgs(s))
1143 {
1144 struct Buffer tmp = buf_make(0);
1146 if (!mutt_str_atoui_full(tmp.data, &match))
1147 {
1148 buf_printf(err, _("%s: invalid number: %s"), color ? "color" : "mono", tmp.data);
1149 buf_dealloc(&tmp);
1150 return MUTT_CMD_WARNING;
1151 }
1152 buf_dealloc(&tmp);
1153 }
1154
1155 if (MoreArgs(s))
1156 {
1157 buf_printf(err, _("%s: too many arguments"), color ? "color" : "mono");
1158 return MUTT_CMD_WARNING;
1159 }
1160
1161 rc = regex_colors_parse_status_list(cid, buf->data, fg, bg, attrs, match, err);
1162 return rc;
1163 }
1164 else // Remaining simple colours
1165 {
1166 color_debug(LL_DEBUG5, "simple\n");
1167 if (simple_color_set(cid, fg, bg, attrs))
1168 rc = MUTT_CMD_SUCCESS;
1169 else
1170 rc = MUTT_CMD_ERROR;
1171 }
1172
1173 if (rc == MUTT_CMD_SUCCESS)
1174 {
1175 get_colorid_name(cid, buf);
1176 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf->data);
1177 struct EventColor ev_c = { cid, NULL };
1179 }
1180
1181 return rc;
1182}
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:383
struct Buffer buf_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:70
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:401
void get_colorid_name(unsigned int cid, struct Buffer *buf)
Get the name of a color id.
Definition: command.c:667
struct Notify * ColorsNotify
Notifications: ColorId, EventColor.
Definition: notify.c:35
bool regex_colors_parse_color_list(enum ColorId cid, const char *pat, uint32_t fg, uint32_t bg, int attrs, int *rc, struct Buffer *err)
Parse a Regex 'color' command.
Definition: regex.c:340
int regex_colors_parse_status_list(enum ColorId cid, const char *pat, uint32_t fg, uint32_t bg, int attrs, int match, struct Buffer *err)
Parse a Regex 'color status' command.
Definition: regex.c:410
struct AttrColor * simple_color_set(enum ColorId cid, int fg, int bg, int attrs)
Set the colour of a simple object.
Definition: simple.c:118
#define COLOR_DEFAULT
Definition: color.h:102
ColorId
List of all colored objects.
Definition: color.h:38
@ MT_COLOR_TREE
Index: tree-drawing characters.
Definition: color.h:77
bool StartupComplete
When the config has been read.
Definition: main.c:199
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:171
@ NT_COLOR_SET
Color has been set.
Definition: notify2.h:41
@ NT_COLOR
Colour has changed, NotifyColor, EventColor.
Definition: notify_type.h:41
bool quoted_colors_parse_color(enum ColorId cid, uint32_t fg, uint32_t bg, int attrs, int q_level, int *rc, struct Buffer *err)
Parse the 'color quoted' command.
Definition: quoted.c:99
String manipulation buffer.
Definition: buffer.h:34
char * dptr
Current read/write position.
Definition: buffer.h:36
An Event that happened to a Colour.
Definition: notify2.h:53
enum ColorId cid
Colour ID that has changed.
Definition: notify2.h:54
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ ColorFields

const struct Mapping ColorFields[]

Mapping of colour names to their IDs.

Definition at line 53 of file command.c.

◆ ComposeColorFields

const struct Mapping ComposeColorFields[]
Initial value:
= {
{ "header", MT_COLOR_COMPOSE_HEADER },
{ "security_encrypt", MT_COLOR_COMPOSE_SECURITY_ENCRYPT },
{ "security_sign", MT_COLOR_COMPOSE_SECURITY_SIGN },
{ "security_both", MT_COLOR_COMPOSE_SECURITY_BOTH },
{ "security_none", MT_COLOR_COMPOSE_SECURITY_NONE },
{ NULL, 0 }
}
@ MT_COLOR_COMPOSE_SECURITY_ENCRYPT
Mail will be encrypted.
Definition: color.h:46
@ MT_COLOR_COMPOSE_SECURITY_NONE
Mail will not be encrypted or signed.
Definition: color.h:47
@ MT_COLOR_COMPOSE_SECURITY_BOTH
Mail will be encrypted and signed.
Definition: color.h:45

Mapping of compose colour names to their IDs.

Definition at line 108 of file command.c.