NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
command.c File Reference

Parse colour commands. More...

#include "config.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "parse/lib.h"
#include "attr.h"
#include "color.h"
#include "command2.h"
#include "debug.h"
#include "dump.h"
#include "globals.h"
#include "notify2.h"
#include "parse_color.h"
#include "quoted.h"
#include "regex4.h"
#include "simple2.h"
+ Include dependency graph for command.c:

Go to the source code of this file.

Functions

void get_colorid_name (unsigned int cid, struct Buffer *buf)
 Get the name of a color id.
 
static enum CommandResult parse_object (struct Buffer *buf, struct Buffer *s, enum ColorId *cid, int *ql, struct Buffer *err)
 Identify a colour object.
 
static enum CommandResult parse_uncolor (struct Buffer *buf, struct Buffer *s, struct Buffer *err, bool uncolor)
 Parse an 'uncolor' command.
 
static enum CommandResult parse_color (struct Buffer *buf, struct Buffer *s, struct Buffer *err, parser_callback_t callback, bool color)
 Parse a 'color' command.
 
enum CommandResult mutt_parse_uncolor (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'uncolor' command - Implements Command::parse() -.
 
enum CommandResult mutt_parse_unmono (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unmono' command - Implements Command::parse() -.
 
enum CommandResult mutt_parse_color (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'color' command - Implements Command::parse() -.
 
enum CommandResult mutt_parse_mono (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'mono' command - Implements Command::parse() -.
 

Variables

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

Detailed Description

Parse colour commands.

Authors
  • Pietro Cerutti
  • Richard Russon
  • Dennis Schön

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.

Function Documentation

◆ 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 128 of file command.c.

129{
130 const char *name = NULL;
131
133 {
135 if (name)
136 {
137 buf_printf(buf, "compose %s", name);
138 return;
139 }
140 }
141
142 name = mutt_map_get_name(cid, ColorFields);
143 if (name)
144 buf_addstr(buf, name);
145 else
146 buf_printf(buf, "UNKNOWN %d", cid);
147}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
const struct Mapping ComposeColorFields[]
Mapping of compose colour names to their IDs.
Definition: command.c:112
const struct Mapping ColorFields[]
Mapping of colour names to their IDs.
Definition: command.c:56
@ MT_COLOR_COMPOSE_SECURITY_SIGN
Mail will be signed.
Definition: color.h:46
@ MT_COLOR_COMPOSE_HEADER
Header labels, e.g. From:
Definition: color.h:42
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
+ 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 160 of file command.c.

162{
163 int rc;
164
165 if (mutt_str_startswith(buf->data, "quoted") != 0)
166 {
167 int val = 0;
168 if (buf->data[6] != '\0')
169 {
170 if (!mutt_str_atoi_full(buf->data + 6, &val) || (val > COLOR_QUOTES_MAX))
171 {
172 buf_printf(err, _("%s: no such object"), buf->data);
173 return MUTT_CMD_WARNING;
174 }
175 }
176
177 *ql = val;
178 *cid = MT_COLOR_QUOTED;
179 return MUTT_CMD_SUCCESS;
180 }
181
182 if (mutt_istr_equal(buf->data, "compose"))
183 {
184 if (!MoreArgs(s))
185 {
186 buf_printf(err, _("%s: too few arguments"), "color");
187 return MUTT_CMD_WARNING;
188 }
189
191
193 if (rc == -1)
194 {
195 buf_printf(err, _("%s: no such object"), buf->data);
196 return MUTT_CMD_WARNING;
197 }
198
199 *cid = rc;
200 return MUTT_CMD_SUCCESS;
201 }
202
204 if (rc == -1)
205 {
206 buf_printf(err, _("%s: no such object"), buf->data);
207 return MUTT_CMD_WARNING;
208 }
209 else
210 {
211 color_debug(LL_DEBUG5, "object: %s\n", mutt_map_get_name(rc, ColorFields));
212 }
213
214 *cid = rc;
215 return MUTT_CMD_SUCCESS;
216}
@ MT_COLOR_QUOTED
Pager: quoted text.
Definition: color.h:59
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:39
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition: command.h:38
static int color_debug(enum LogLevel level, const char *format,...)
Definition: debug.h:52
int parse_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: extract.c:50
#define MoreArgs(buf)
Definition: extract.h:32
#define TOKEN_NO_FLAGS
No flags are set.
Definition: extract.h:46
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
int mutt_map_get_value(const char *name, const struct Mapping *map)
Lookup the constant for a string.
Definition: mapping.c:85
#define _(a)
Definition: message.h:28
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:672
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:230
#define COLOR_QUOTES_MAX
Ten colours, quoted0..quoted9 (quoted and quoted0 are equivalent)
Definition: quoted.h:35
char * data
Pointer to data.
Definition: buffer.h:37
+ 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 230 of file command.c.

232{
233 if (OptNoCurses) // No GUI, so quietly discard the command
234 {
235 while (MoreArgs(s))
236 {
238 }
239 return MUTT_CMD_SUCCESS;
240 }
241
243
244 if (mutt_str_equal(buf->data, "*"))
245 {
246 colors_reset();
247 return MUTT_CMD_SUCCESS;
248 }
249
250 unsigned int cid = MT_COLOR_NONE;
251 int ql = 0;
252 color_debug(LL_DEBUG5, "uncolor: %s\n", buf_string(buf));
253 enum CommandResult rc = parse_object(buf, s, &cid, &ql, err);
254 if (rc != MUTT_CMD_SUCCESS)
255 return rc;
256
257 if (cid == -1)
258 {
259 buf_printf(err, _("%s: no such object"), buf->data);
260 return MUTT_CMD_ERROR;
261 }
262
263 if (COLOR_QUOTED(cid))
264 {
265 color_debug(LL_DEBUG5, "quoted\n");
266 return quoted_colors_parse_uncolor(cid, ql, err);
267 }
268
269 if ((cid == MT_COLOR_STATUS) && !MoreArgs(s))
270 {
271 color_debug(LL_DEBUG5, "simple\n");
272 simple_color_reset(cid); // default colour for the status bar
273 return MUTT_CMD_SUCCESS;
274 }
275
276 if (!mutt_color_has_pattern(cid))
277 {
278 color_debug(LL_DEBUG5, "simple\n");
280 return MUTT_CMD_SUCCESS;
281 }
282
283 if (!MoreArgs(s))
284 {
285 if (regex_colors_parse_uncolor(cid, NULL, uncolor))
286 return MUTT_CMD_SUCCESS;
287 else
288 return MUTT_CMD_ERROR;
289 }
290
291 do
292 {
294 if (mutt_str_equal("*", buf->data))
295 {
296 if (regex_colors_parse_uncolor(cid, NULL, uncolor))
297 return MUTT_CMD_SUCCESS;
298 else
299 return MUTT_CMD_ERROR;
300 }
301
302 regex_colors_parse_uncolor(cid, buf->data, uncolor);
303
304 } while (MoreArgs(s));
305
306 return MUTT_CMD_SUCCESS;
307}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
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:160
bool regex_colors_parse_uncolor(enum ColorId cid, const char *pat, bool uncolor)
Parse a Regex 'uncolor' command.
Definition: regex.c:424
void simple_color_reset(enum ColorId cid)
Clear the colour of a simple object.
Definition: simple.c:150
bool mutt_color_has_pattern(enum ColorId cid)
Check if a color object supports a regex pattern.
Definition: color.c:98
void colors_reset(void)
Reset all the simple, quoted and regex colours.
Definition: color.c:68
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition: color.h:71
@ MT_COLOR_NONE
No colour.
Definition: color.h:37
CommandResult
Error codes for command_t parse functions.
Definition: command.h:36
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:37
bool OptNoCurses
(pseudo) when sending in batch mode
Definition: globals.c:69
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
enum CommandResult quoted_colors_parse_uncolor(enum ColorId cid, int q_level, struct Buffer *err)
Parse the 'uncolor quoted' command.
Definition: quoted.c:184
#define COLOR_QUOTED(cid)
Definition: quoted.h:39
+ 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  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
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 322 of file command.c.

325{
326 int q_level = 0;
327 unsigned int match = 0;
328 enum ColorId cid = MT_COLOR_NONE;
330 struct AttrColor *ac = NULL;
331
332 if (!MoreArgs(s))
333 {
334 if (StartupComplete)
335 {
336 color_dump();
337 rc = MUTT_CMD_SUCCESS;
338 }
339 else
340 {
341 buf_printf(err, _("%s: too few arguments"), color ? "color" : "mono");
342 rc = MUTT_CMD_WARNING;
343 }
344
345 goto done;
346 }
347
349 color_debug(LL_DEBUG5, "color: %s\n", buf_string(buf));
350
351 rc = parse_object(buf, s, &cid, &q_level, err);
352 if (rc != MUTT_CMD_SUCCESS)
353 goto done;
354
355 ac = attr_color_new();
356 rc = callback(buf, s, ac, err);
357 if (rc != MUTT_CMD_SUCCESS)
358 goto done;
359
360 //------------------------------------------------------------------
361 // Business Logic
362
363 if ((ac->fg.type == CT_RGB) || (ac->bg.type == CT_RGB))
364 {
365#ifndef NEOMUTT_DIRECT_COLORS
366 buf_printf(err, _("Direct colors support not compiled in: %s"), buf_string(s));
367 return MUTT_CMD_ERROR;
368#endif
369
370 const bool c_color_directcolor = cs_subset_bool(NeoMutt->sub, "color_directcolor");
371 if (!c_color_directcolor)
372 {
373 buf_printf(err, _("Direct colors support disabled: %s"), buf_string(s));
374 return MUTT_CMD_ERROR;
375 }
376 }
377
378 if ((ac->fg.color >= COLORS) || (ac->bg.color >= COLORS))
379 {
380 buf_printf(err, _("%s: color not supported by term"), buf_string(s));
381 return MUTT_CMD_ERROR;
382 }
383
384 //------------------------------------------------------------------
385
386 /* extract a regular expression if needed */
387
388 if (mutt_color_has_pattern(cid) && (cid != MT_COLOR_STATUS))
389 {
390 color_debug(LL_DEBUG5, "regex needed\n");
391 if (MoreArgs(s))
392 {
394 }
395 else
396 {
397 buf_strcpy(buf, ".*");
398 }
399 }
400
401 if (MoreArgs(s) && (cid != MT_COLOR_STATUS))
402 {
403 buf_printf(err, _("%s: too many arguments"), color ? "color" : "mono");
404 rc = MUTT_CMD_WARNING;
405 goto done;
406 }
407
408 if (regex_colors_parse_color_list(cid, buf->data, ac, &rc, err))
409 {
410 color_debug(LL_DEBUG5, "regex_colors_parse_color_list done\n");
411 goto done;
412 // do nothing
413 }
414 else if (quoted_colors_parse_color(cid, ac, q_level, &rc, err))
415 {
416 color_debug(LL_DEBUG5, "quoted_colors_parse_color done\n");
417 goto done;
418 // do nothing
419 }
420 else if ((cid == MT_COLOR_STATUS) && MoreArgs(s))
421 {
422 color_debug(LL_DEBUG5, "status\n");
423 /* 'color status fg bg' can have up to 2 arguments:
424 * 0 arguments: sets the default status color (handled below by else part)
425 * 1 argument : colorize pattern on match
426 * 2 arguments: colorize nth submatch of pattern */
428
429 if (MoreArgs(s))
430 {
431 struct Buffer *tmp = buf_pool_get();
433 if (!mutt_str_atoui_full(tmp->data, &match))
434 {
435 buf_printf(err, _("%s: invalid number: %s"), color ? "color" : "mono", tmp->data);
436 buf_pool_release(&tmp);
437 rc = MUTT_CMD_WARNING;
438 goto done;
439 }
440 buf_pool_release(&tmp);
441 }
442
443 if (MoreArgs(s))
444 {
445 buf_printf(err, _("%s: too many arguments"), color ? "color" : "mono");
446 rc = MUTT_CMD_WARNING;
447 goto done;
448 }
449
450 rc = regex_colors_parse_status_list(cid, buf->data, ac, match, err);
451 goto done;
452 }
453 else // Remaining simple colours
454 {
455 color_debug(LL_DEBUG5, "simple\n");
456 if (simple_color_set(cid, ac))
457 rc = MUTT_CMD_SUCCESS;
458 else
459 rc = MUTT_CMD_ERROR;
460 }
461
462 if (rc == MUTT_CMD_SUCCESS)
463 {
464 get_colorid_name(cid, buf);
465 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf->data);
466 struct EventColor ev_c = { cid, NULL };
468 }
469
470done:
471 attr_color_free(&ac);
472 return rc;
473}
struct AttrColor * attr_color_new(void)
Create a new AttrColor.
Definition: attr.c:90
void attr_color_free(struct AttrColor **ptr)
Free an AttrColor.
Definition: attr.c:69
@ CT_RGB
True colour, e.g. "#11AAFF".
Definition: attr.h:38
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
void get_colorid_name(unsigned int cid, struct Buffer *buf)
Get the name of a color id.
Definition: command.c:128
void color_dump(void)
Display all the colours in the Pager.
Definition: dump.c:451
struct Notify * ColorsNotify
Notifications: ColorId, EventColor.
Definition: notify.c:36
int regex_colors_parse_status_list(enum ColorId cid, const char *pat, struct AttrColor *ac, int match, struct Buffer *err)
Parse a Regex 'color status' command.
Definition: regex.c:396
bool regex_colors_parse_color_list(enum ColorId cid, const char *pat, struct AttrColor *ac, int *rc, struct Buffer *err)
Parse a Regex 'color' command.
Definition: regex.c:329
struct AttrColor * simple_color_set(enum ColorId cid, struct AttrColor *ac_val)
Set the colour of a simple object.
Definition: simple.c:127
ColorId
List of all coloured objects.
Definition: color.h:36
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
bool StartupComplete
When the config has been read.
Definition: main.c:189
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:173
@ NT_COLOR_SET
Color has been set.
Definition: notify2.h:43
@ NT_COLOR
Colour has changed, NotifyColor, EventColor.
Definition: notify_type.h:41
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
bool quoted_colors_parse_color(enum ColorId cid, struct AttrColor *ac_val, int q_level, int *rc, struct Buffer *err)
Parse the 'color quoted' command.
Definition: quoted.c:125
A curses colour and its attributes.
Definition: attr.h:66
struct ColorElement bg
Background colour.
Definition: attr.h:68
struct ColorElement fg
Foreground colour.
Definition: attr.h:67
String manipulation buffer.
Definition: buffer.h:36
enum ColorType type
Type of Colour.
Definition: attr.h:58
color_t color
Colour.
Definition: attr.h:57
An Event that happened to a Colour.
Definition: notify2.h:55
enum ColorId cid
Colour ID that has changed.
Definition: notify2.h:56
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
+ 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 56 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:44
@ MT_COLOR_COMPOSE_SECURITY_NONE
Mail will not be encrypted or signed.
Definition: color.h:45
@ MT_COLOR_COMPOSE_SECURITY_BOTH
Mail will be encrypted and signed.
Definition: color.h:43

Mapping of compose colour names to their IDs.

Definition at line 112 of file command.c.