NeoMutt  2025-01-09-41-g086358
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 "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 Colour ID.
 
static enum CommandResult parse_object (struct Buffer *buf, struct Buffer *s, enum ColorId *cid, 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.
 

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 Colour ID.

Parameters
cidColour, e.g. MT_COLOR_HEADER
bufBuffer for result

Definition at line 128 of file command.c.

129{
130 const char *name = mutt_map_get_name(cid, ColorFields);
131 if (name)
132 buf_addstr(buf, name);
133 else
134 buf_printf(buf, "UNKNOWN %d", cid);
135}
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 ColorFields[]
Mapping of colour names to their IDs.
Definition: command.c:55
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,
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]errBuffer for error messages
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

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

Definition at line 147 of file command.c.

149{
150 if (mutt_istr_equal(buf->data, "compose"))
151 {
152 if (!MoreArgs(s))
153 {
154 buf_printf(err, _("%s: too few arguments"), "color");
155 return MUTT_CMD_WARNING;
156 }
157
158 struct Buffer *tmp = buf_pool_get();
160 buf_fix_dptr(buf);
161 buf_add_printf(buf, "_%s", buf_string(tmp));
162 buf_pool_release(&tmp);
163 }
164
165 int rc = mutt_map_get_value(buf->data, ColorFields);
166 if (rc == -1)
167 {
168 buf_printf(err, _("%s: no such object"), buf->data);
169 return MUTT_CMD_WARNING;
170 }
171 else
172 {
173 color_debug(LL_DEBUG5, "object: %s\n", mutt_map_get_name(rc, ColorFields));
174 }
175
176 *cid = rc;
177 return MUTT_CMD_SUCCESS;
178}
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:204
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition: buffer.c:182
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
@ 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
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
String manipulation buffer.
Definition: buffer.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 192 of file command.c.

194{
195 if (OptNoCurses) // No GUI, so quietly discard the command
196 {
197 while (MoreArgs(s))
198 {
200 }
201 return MUTT_CMD_SUCCESS;
202 }
203
205
206 if (mutt_str_equal(buf->data, "*"))
207 {
208 colors_reset();
209 return MUTT_CMD_SUCCESS;
210 }
211
212 unsigned int cid = MT_COLOR_NONE;
213 color_debug(LL_DEBUG5, "uncolor: %s\n", buf_string(buf));
214 enum CommandResult rc = parse_object(buf, s, &cid, err);
215 if (rc != MUTT_CMD_SUCCESS)
216 return rc;
217
218 if (cid == -1)
219 {
220 buf_printf(err, _("%s: no such object"), buf->data);
221 return MUTT_CMD_ERROR;
222 }
223
224 if ((cid == MT_COLOR_STATUS) && !MoreArgs(s))
225 {
226 color_debug(LL_DEBUG5, "simple\n");
227 simple_color_reset(cid); // default colour for the status bar
228 return MUTT_CMD_SUCCESS;
229 }
230
231 if (!mutt_color_has_pattern(cid))
232 {
233 color_debug(LL_DEBUG5, "simple\n");
235 return MUTT_CMD_SUCCESS;
236 }
237
238 if (!MoreArgs(s))
239 {
240 if (regex_colors_parse_uncolor(cid, NULL, uncolor))
241 return MUTT_CMD_SUCCESS;
242 else
243 return MUTT_CMD_ERROR;
244 }
245
246 do
247 {
249 if (mutt_str_equal("*", buf->data))
250 {
251 if (regex_colors_parse_uncolor(cid, NULL, uncolor))
252 return MUTT_CMD_SUCCESS;
253 else
254 return MUTT_CMD_ERROR;
255 }
256
257 regex_colors_parse_uncolor(cid, buf->data, uncolor);
258
259 } while (MoreArgs(s));
260
261 return MUTT_CMD_SUCCESS;
262}
static enum CommandResult parse_object(struct Buffer *buf, struct Buffer *s, enum ColorId *cid, struct Buffer *err)
Identify a colour object.
Definition: command.c:147
bool regex_colors_parse_uncolor(enum ColorId cid, const char *pat, bool uncolor)
Parse a Regex 'uncolor' command.
Definition: regex.c:411
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:80
@ 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
+ 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 277 of file command.c.

280{
281 unsigned int match = 0;
282 enum ColorId cid = MT_COLOR_NONE;
284 struct AttrColor *ac = NULL;
285
286 if (!MoreArgs(s))
287 {
288 if (StartupComplete)
289 {
290 color_dump();
291 rc = MUTT_CMD_SUCCESS;
292 }
293 else
294 {
295 buf_printf(err, _("%s: too few arguments"), color ? "color" : "mono");
296 rc = MUTT_CMD_WARNING;
297 }
298
299 goto done;
300 }
301
303 color_debug(LL_DEBUG5, "color: %s\n", buf_string(buf));
304
305 rc = parse_object(buf, s, &cid, err);
306 if (rc != MUTT_CMD_SUCCESS)
307 goto done;
308
309 ac = attr_color_new();
310 rc = callback(buf, s, ac, err);
311 if (rc != MUTT_CMD_SUCCESS)
312 goto done;
313
314 //------------------------------------------------------------------
315 // Business Logic
316
317 if ((ac->fg.type == CT_RGB) || (ac->bg.type == CT_RGB))
318 {
319#ifndef NEOMUTT_DIRECT_COLORS
320 buf_printf(err, _("Direct colors support not compiled in: %s"), buf_string(s));
321 return MUTT_CMD_ERROR;
322#endif
323
324 const bool c_color_directcolor = cs_subset_bool(NeoMutt->sub, "color_directcolor");
325 if (!c_color_directcolor)
326 {
327 buf_printf(err, _("Direct colors support disabled: %s"), buf_string(s));
328 return MUTT_CMD_ERROR;
329 }
330 }
331
332 if ((ac->fg.color >= COLORS) || (ac->bg.color >= COLORS))
333 {
334 buf_printf(err, _("%s: color not supported by term"), buf_string(s));
335 return MUTT_CMD_ERROR;
336 }
337
338 //------------------------------------------------------------------
339
340 /* extract a regular expression if needed */
341
342 if (mutt_color_has_pattern(cid) && (cid != MT_COLOR_STATUS))
343 {
344 color_debug(LL_DEBUG5, "regex needed\n");
345 if (MoreArgs(s))
346 {
348 }
349 else
350 {
351 buf_strcpy(buf, ".*");
352 }
353 }
354
355 if (MoreArgs(s) && (cid != MT_COLOR_STATUS))
356 {
357 buf_printf(err, _("%s: too many arguments"), color ? "color" : "mono");
358 rc = MUTT_CMD_WARNING;
359 goto done;
360 }
361
362 if (regex_colors_parse_color_list(cid, buf->data, ac, &rc, err))
363 {
364 color_debug(LL_DEBUG5, "regex_colors_parse_color_list done\n");
365 goto done;
366 // do nothing
367 }
368 else if ((cid == MT_COLOR_STATUS) && MoreArgs(s))
369 {
370 color_debug(LL_DEBUG5, "status\n");
371 /* 'color status fg bg' can have up to 2 arguments:
372 * 0 arguments: sets the default status color (handled below by else part)
373 * 1 argument : colorize pattern on match
374 * 2 arguments: colorize nth submatch of pattern */
376
377 if (MoreArgs(s))
378 {
379 struct Buffer *tmp = buf_pool_get();
381 if (!mutt_str_atoui_full(tmp->data, &match))
382 {
383 buf_printf(err, _("%s: invalid number: %s"), color ? "color" : "mono", tmp->data);
384 buf_pool_release(&tmp);
385 rc = MUTT_CMD_WARNING;
386 goto done;
387 }
388 buf_pool_release(&tmp);
389 }
390
391 if (MoreArgs(s))
392 {
393 buf_printf(err, _("%s: too many arguments"), color ? "color" : "mono");
394 rc = MUTT_CMD_WARNING;
395 goto done;
396 }
397
398 rc = regex_colors_parse_status_list(cid, buf->data, ac, match, err);
399 goto done;
400 }
401 else // Remaining simple colours
402 {
403 color_debug(LL_DEBUG5, "simple\n");
404 if (simple_color_set(cid, ac))
405 rc = MUTT_CMD_SUCCESS;
406 else
407 rc = MUTT_CMD_ERROR;
408 }
409
410 if (rc == MUTT_CMD_SUCCESS)
411 {
412 get_colorid_name(cid, buf);
413 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf->data);
414 struct EventColor ev_c = { cid, NULL };
416 }
417
418done:
419 attr_color_free(&ac);
420 return rc;
421}
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 Colour ID.
Definition: command.c:128
void color_dump(void)
Display all the colours in the Pager.
Definition: dump.c:450
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:383
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:322
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
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
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 55 of file command.c.