NeoMutt  2024-03-23-147-g885fbc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
command.c File Reference

Parse colour commands. More...

#include "config.h"
#include <stddef.h>
#include <stdbool.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 127 of file command.c.

128{
129 const char *name = NULL;
130
132 {
134 if (name)
135 {
136 buf_printf(buf, "compose %s", name);
137 return;
138 }
139 }
140
141 name = mutt_map_get_name(cid, ColorFields);
142 if (name)
143 buf_addstr(buf, name);
144 else
145 buf_printf(buf, "UNKNOWN %d", cid);
146}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
const struct Mapping ComposeColorFields[]
Mapping of compose colour names to their IDs.
Definition: command.c:111
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:50
@ MT_COLOR_COMPOSE_HEADER
Header labels, e.g. From:
Definition: color.h:46
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 159 of file command.c.

161{
162 int rc;
163
164 if (mutt_str_startswith(buf->data, "quoted") != 0)
165 {
166 int val = 0;
167 if (buf->data[6] != '\0')
168 {
169 if (!mutt_str_atoi_full(buf->data + 6, &val) || (val > COLOR_QUOTES_MAX))
170 {
171 buf_printf(err, _("%s: no such object"), buf->data);
172 return MUTT_CMD_WARNING;
173 }
174 }
175
176 *ql = val;
177 *cid = MT_COLOR_QUOTED;
178 return MUTT_CMD_SUCCESS;
179 }
180
181 if (mutt_istr_equal(buf->data, "compose"))
182 {
183 if (!MoreArgs(s))
184 {
185 buf_printf(err, _("%s: too few arguments"), "color");
186 return MUTT_CMD_WARNING;
187 }
188
190
192 if (rc == -1)
193 {
194 buf_printf(err, _("%s: no such object"), buf->data);
195 return MUTT_CMD_WARNING;
196 }
197
198 *cid = rc;
199 return MUTT_CMD_SUCCESS;
200 }
201
203 if (rc == -1)
204 {
205 buf_printf(err, _("%s: no such object"), buf->data);
206 return MUTT_CMD_WARNING;
207 }
208 else
209 {
210 color_debug(LL_DEBUG5, "object: %s\n", mutt_map_get_name(rc, ColorFields));
211 }
212
213 *cid = rc;
214 return MUTT_CMD_SUCCESS;
215}
@ MT_COLOR_QUOTED
Pager: quoted text.
Definition: color.h:63
@ 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:53
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:721
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:36
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 229 of file command.c.

231{
232 if (OptNoCurses) // No GUI, so quietly discard the command
233 {
234 while (MoreArgs(s))
235 {
237 }
238 return MUTT_CMD_SUCCESS;
239 }
240
242
243 if (mutt_str_equal(buf->data, "*"))
244 {
246 return MUTT_CMD_SUCCESS;
247 }
248
249 unsigned int cid = MT_COLOR_NONE;
250 int ql = 0;
251 color_debug(LL_DEBUG5, "uncolor: %s\n", buf_string(buf));
252 enum CommandResult rc = parse_object(buf, s, &cid, &ql, err);
253 if (rc != MUTT_CMD_SUCCESS)
254 return rc;
255
256 if (cid == -1)
257 {
258 buf_printf(err, _("%s: no such object"), buf->data);
259 return MUTT_CMD_ERROR;
260 }
261
262 if (cid == MT_COLOR_QUOTED)
263 {
264 color_debug(LL_DEBUG5, "quoted\n");
265 return quoted_colors_parse_uncolor(cid, ql, err);
266 }
267
268 if ((cid == MT_COLOR_STATUS) && !MoreArgs(s))
269 {
270 color_debug(LL_DEBUG5, "simple\n");
271 simple_color_reset(cid); // default colour for the status bar
272 return MUTT_CMD_SUCCESS;
273 }
274
275 if (!mutt_color_has_pattern(cid))
276 {
277 color_debug(LL_DEBUG5, "simple\n");
279 return MUTT_CMD_SUCCESS;
280 }
281
282 if (!MoreArgs(s))
283 {
284 if (regex_colors_parse_uncolor(cid, NULL, uncolor))
285 return MUTT_CMD_SUCCESS;
286 else
287 return MUTT_CMD_ERROR;
288 }
289
290 do
291 {
293 if (mutt_str_equal("*", buf->data))
294 {
295 if (regex_colors_parse_uncolor(cid, NULL, uncolor))
296 return MUTT_CMD_SUCCESS;
297 else
298 return MUTT_CMD_ERROR;
299 }
300
301 regex_colors_parse_uncolor(cid, buf->data, uncolor);
302
303 } while (MoreArgs(s));
304
305 return MUTT_CMD_SUCCESS;
306}
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:159
bool regex_colors_parse_uncolor(enum ColorId cid, const char *pat, bool uncolor)
Parse a Regex 'uncolor' command.
Definition: regex.c:418
void simple_color_reset(enum ColorId cid)
Clear the colour of a simple object.
Definition: simple.c:153
void colors_cleanup(void)
Reset all the simple, quoted and regex colours.
Definition: color.c:49
bool mutt_color_has_pattern(enum ColorId cid)
Check if a color object supports a regex pattern.
Definition: color.c:97
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition: color.h:75
@ MT_COLOR_NONE
Definition: color.h:41
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:72
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:709
enum CommandResult quoted_colors_parse_uncolor(enum ColorId cid, int q_level, struct Buffer *err)
Parse the 'uncolor quoted' command.
Definition: quoted.c:176
+ 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 321 of file command.c.

324{
325 int q_level = 0;
326 unsigned int match = 0;
327 enum ColorId cid = MT_COLOR_NONE;
329 struct AttrColor *ac = NULL;
330
331 if (!MoreArgs(s))
332 {
333 if (StartupComplete)
334 {
335 color_dump();
336 rc = MUTT_CMD_SUCCESS;
337 }
338 else
339 {
340 buf_printf(err, _("%s: too few arguments"), color ? "color" : "mono");
341 rc = MUTT_CMD_WARNING;
342 }
343
344 goto done;
345 }
346
348 color_debug(LL_DEBUG5, "color: %s\n", buf_string(buf));
349
350 rc = parse_object(buf, s, &cid, &q_level, err);
351 if (rc != MUTT_CMD_SUCCESS)
352 goto done;
353
354 ac = attr_color_new();
355 rc = callback(buf, s, ac, err);
356 if (rc != MUTT_CMD_SUCCESS)
357 goto done;
358
359 //------------------------------------------------------------------
360 // Business Logic
361
362 if ((ac->fg.type == CT_RGB) || (ac->bg.type == CT_RGB))
363 {
364#ifndef NEOMUTT_DIRECT_COLORS
365 buf_printf(err, _("Direct colors support not compiled in: %s"), buf_string(s));
366 return MUTT_CMD_ERROR;
367#endif
368
369 const bool c_color_directcolor = cs_subset_bool(NeoMutt->sub, "color_directcolor");
370 if (!c_color_directcolor)
371 {
372 buf_printf(err, _("Direct colors support disabled: %s"), buf_string(s));
373 return MUTT_CMD_ERROR;
374 }
375 }
376
377 if ((ac->fg.color >= COLORS) || (ac->bg.color >= COLORS))
378 {
379 buf_printf(err, _("%s: color not supported by term"), buf_string(s));
380 return MUTT_CMD_ERROR;
381 }
382
383 //------------------------------------------------------------------
384
385 /* extract a regular expression if needed */
386
387 if (mutt_color_has_pattern(cid) && (cid != MT_COLOR_STATUS))
388 {
389 color_debug(LL_DEBUG5, "regex needed\n");
390 if (MoreArgs(s))
391 {
393 }
394 else
395 {
396 buf_strcpy(buf, ".*");
397 }
398 }
399
400 if (MoreArgs(s) && (cid != MT_COLOR_STATUS))
401 {
402 buf_printf(err, _("%s: too many arguments"), color ? "color" : "mono");
403 rc = MUTT_CMD_WARNING;
404 goto done;
405 }
406
407 if (regex_colors_parse_color_list(cid, buf->data, ac, &rc, err))
408 {
409 color_debug(LL_DEBUG5, "regex_colors_parse_color_list done\n");
410 goto done;
411 // do nothing
412 }
413 else if (quoted_colors_parse_color(cid, ac, q_level, &rc, err))
414 {
415 color_debug(LL_DEBUG5, "quoted_colors_parse_color done\n");
416 goto done;
417 // do nothing
418 }
419 else if ((cid == MT_COLOR_STATUS) && MoreArgs(s))
420 {
421 color_debug(LL_DEBUG5, "status\n");
422 /* 'color status fg bg' can have up to 2 arguments:
423 * 0 arguments: sets the default status color (handled below by else part)
424 * 1 argument : colorize pattern on match
425 * 2 arguments: colorize nth submatch of pattern */
427
428 if (MoreArgs(s))
429 {
430 struct Buffer *tmp = buf_pool_get();
432 if (!mutt_str_atoui_full(tmp->data, &match))
433 {
434 buf_printf(err, _("%s: invalid number: %s"), color ? "color" : "mono", tmp->data);
435 buf_pool_release(&tmp);
436 rc = MUTT_CMD_WARNING;
437 goto done;
438 }
439 buf_pool_release(&tmp);
440 }
441
442 if (MoreArgs(s))
443 {
444 buf_printf(err, _("%s: too many arguments"), color ? "color" : "mono");
445 rc = MUTT_CMD_WARNING;
446 goto done;
447 }
448
449 rc = regex_colors_parse_status_list(cid, buf->data, ac, match, err);
450 goto done;
451 }
452 else // Remaining simple colours
453 {
454 color_debug(LL_DEBUG5, "simple\n");
455 if (simple_color_set(cid, ac))
456 rc = MUTT_CMD_SUCCESS;
457 else
458 rc = MUTT_CMD_ERROR;
459 }
460
461 if (rc == MUTT_CMD_SUCCESS)
462 {
463 get_colorid_name(cid, buf);
464 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf->data);
465 struct EventColor ev_c = { cid, NULL };
467 }
468
469done:
470 attr_color_free(&ac);
471 return rc;
472}
struct AttrColor * attr_color_new(void)
Create a new AttrColor.
Definition: attr.c:91
void attr_color_free(struct AttrColor **ptr)
Free an AttrColor.
Definition: attr.c:70
@ 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:394
void get_colorid_name(unsigned int cid, struct Buffer *buf)
Get the name of a color id.
Definition: command.c:127
void color_dump(void)
Display all the colours in the Pager.
Definition: dump.c:450
struct Notify * ColorsNotify
Notifications: ColorId, EventColor.
Definition: notify.c:35
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:390
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:323
struct AttrColor * simple_color_set(enum ColorId cid, struct AttrColor *ac_val)
Set the colour of a simple object.
Definition: simple.c:130
ColorId
List of all colored objects.
Definition: color.h:40
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
bool StartupComplete
When the config has been read.
Definition: main.c:191
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:41
@ 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:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
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:117
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:53
enum ColorId cid
Colour ID that has changed.
Definition: notify2.h:54
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
+ 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:48
@ MT_COLOR_COMPOSE_SECURITY_NONE
Mail will not be encrypted or signed.
Definition: color.h:49
@ MT_COLOR_COMPOSE_SECURITY_BOTH
Mail will be encrypted and signed.
Definition: color.h:47

Mapping of compose colour names to their IDs.

Definition at line 111 of file command.c.