NeoMutt  2025-12-11-949-g4870ee
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
regex.c
Go to the documentation of this file.
1
23
30
31#include "config.h"
32#include <stdbool.h>
33#include <stddef.h>
34#include <stdint.h>
35#include "mutt/lib.h"
36#include "config/lib.h"
37#include "core/lib.h"
38#include "index/lib.h"
39#include "pattern/lib.h"
40#include "attr.h"
41#include "color.h"
42#include "commands.h"
43#include "debug.h"
44#include "module_data.h"
45#include "notify2.h"
46#include "regex4.h"
47
52void regex_colors_init(struct ColorModuleData *mod_data)
53{
54 color_debug(LL_DEBUG5, "init AttachList, BodyList, etc\n");
55 STAILQ_INIT(&mod_data->attach_list);
56 STAILQ_INIT(&mod_data->body_list);
57 STAILQ_INIT(&mod_data->header_list);
60 STAILQ_INIT(&mod_data->index_date_list);
61 STAILQ_INIT(&mod_data->index_label_list);
63 STAILQ_INIT(&mod_data->index_size_list);
64 STAILQ_INIT(&mod_data->index_tags_list);
65 STAILQ_INIT(&mod_data->index_flags_list);
66 STAILQ_INIT(&mod_data->index_list);
68 STAILQ_INIT(&mod_data->index_tag_list);
69 STAILQ_INIT(&mod_data->status_list);
70}
71
95
101{
102 regex_colors_reset(mod_data);
103}
104
112{
113 if (!rcol)
114 return;
115
116 rcol->match = 0;
117 rcol->stop_matching = false;
118
120 FREE(&rcol->pattern);
121 regfree(&rcol->regex);
123}
124
129void regex_color_free(struct RegexColor **ptr)
130{
131 if (!ptr || !*ptr)
132 return;
133
134 struct RegexColor *rcol = *ptr;
135 regex_color_clear(rcol);
136
137 FREE(ptr);
138}
139
145{
146 return MUTT_MEM_CALLOC(1, struct RegexColor);
147}
148
153struct RegexColorList *regex_color_list_new(void)
154{
155 struct RegexColorList *rcl = MUTT_MEM_CALLOC(1, struct RegexColorList);
156
157 STAILQ_INIT(rcl);
158
159 struct RegexColor *rcol = regex_color_new();
160 STAILQ_INSERT_TAIL(rcl, rcol, entries);
161
162 return rcl;
163}
164
173void regex_color_list_clear(struct RegexColorList *rcl)
174{
175 if (!rcl)
176 return;
177
178 struct RegexColor *np = NULL, *tmp = NULL;
179 STAILQ_FOREACH_SAFE(np, rcl, entries, tmp)
180 {
181 STAILQ_REMOVE(rcl, np, RegexColor, entries);
182 regex_color_free(&np);
183 }
184}
185
191struct RegexColorList *regex_colors_get_list(enum ColorId cid)
192{
194 switch (cid)
195 {
197 return &mod_data->attach_list;
198 case MT_COLOR_BODY:
199 return &mod_data->body_list;
200 case MT_COLOR_HEADER:
201 return &mod_data->header_list;
202 case MT_COLOR_INDEX:
203 return &mod_data->index_list;
205 return &mod_data->index_author_list;
207 return &mod_data->index_collapsed_list;
209 return &mod_data->index_date_list;
211 return &mod_data->index_flags_list;
213 return &mod_data->index_label_list;
215 return &mod_data->index_number_list;
217 return &mod_data->index_size_list;
219 return &mod_data->index_subject_list;
221 return &mod_data->index_tag_list;
223 return &mod_data->index_tags_list;
224 case MT_COLOR_STATUS:
225 return &mod_data->status_list;
226 default:
227 return NULL;
228 }
229}
230
244static enum CommandResult add_pattern(struct RegexColorList *rcl, const char *s,
245 struct AttrColor *ac_val,
246 struct Buffer *err, bool is_index, int match)
247{
248 struct RegexColor *rcol = NULL;
249
250 STAILQ_FOREACH(rcol, rcl, entries)
251 {
252 if (mutt_str_equal(s, rcol->pattern))
253 break;
254 }
255
256 if (rcol) // found a matching regex
257 {
258 struct AttrColor *ac = &rcol->attr_color;
259 attr_color_overwrite(ac, ac_val);
260 }
261 else
262 {
263 rcol = regex_color_new();
264 if (is_index)
265 {
266 struct Buffer *buf = buf_pool_get();
267 buf_strcpy(buf, s);
268 const char *const c_simple_search = cs_subset_string(NeoMutt->sub, "simple_search");
269 mutt_check_simple(buf, NONULL(c_simple_search));
270 struct MailboxView *mv_cur = get_current_mailbox_view();
272 buf_pool_release(&buf);
273 if (!rcol->color_pattern)
274 {
275 regex_color_free(&rcol);
276 return MUTT_CMD_ERROR;
277 }
278 }
279 else
280 {
281 // Smart case matching
282 uint16_t flags = mutt_mb_is_lower(s) ? REG_ICASE : 0;
283
284 const int r = REG_COMP(&rcol->regex, s, flags);
285 if (r != 0)
286 {
287 regerror(r, &rcol->regex, err->data, err->dsize);
288 buf_fix_dptr(err);
289 regex_color_free(&rcol);
290 return MUTT_CMD_ERROR;
291 }
292 }
293 rcol->pattern = mutt_str_dup(s);
294 rcol->match = match;
295
296 struct AttrColor *ac = &rcol->attr_color;
297
298 attr_color_overwrite(ac, ac_val);
299
300 STAILQ_INSERT_TAIL(rcl, rcol, entries);
301 }
302
303 if (is_index)
304 {
305 /* force re-caching of index colors */
307 struct EventColor ev_c = { MT_COLOR_INDEX, NULL };
309 }
310
311 return MUTT_CMD_SUCCESS;
312}
313
325bool regex_colors_parse_color_list(enum ColorId cid, const char *pat,
326 struct AttrColor *ac, int *rc, struct Buffer *err)
327
328{
329 if (cid == MT_COLOR_STATUS)
330 return false;
331
332 struct RegexColorList *rcl = regex_colors_get_list(cid);
333 if (!rcl)
334 return false;
335
336 bool is_index = false;
337 switch (cid)
338 {
340 case MT_COLOR_BODY:
341 break;
342 case MT_COLOR_HEADER:
343 break;
344 case MT_COLOR_INDEX:
355 is_index = true;
356 break;
357 default:
358 return false;
359 }
360
361 *rc = add_pattern(rcl, pat, ac, err, is_index, 0);
362
363 struct Buffer *buf = buf_pool_get();
364 get_colorid_name(cid, buf);
365 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf_string(buf));
366 buf_pool_release(&buf);
367
368 if (!is_index) // else it will be logged in add_pattern()
369 {
371 struct EventColor ev_c = { cid, NULL };
373 }
374
375 return true;
376}
377
388 struct AttrColor *ac, int match, struct Buffer *err)
389{
390 if (cid != MT_COLOR_STATUS)
391 return MUTT_CMD_ERROR;
392
394 int rc = add_pattern(&mod_data->status_list, pat, ac, err, false, match);
395 if (rc != MUTT_CMD_SUCCESS)
396 return rc;
397
398 struct Buffer *buf = buf_pool_get();
399 get_colorid_name(cid, buf);
400 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf_string(buf));
401 buf_pool_release(&buf);
402
403 struct EventColor ev_c = { cid, NULL };
405
406 return rc;
407}
408
415bool regex_colors_parse_uncolor(enum ColorId cid, const char *pat)
416{
417 struct RegexColorList *cl = regex_colors_get_list(cid);
418 if (!cl)
419 return false;
420
422
423 if (!pat) // Reset all patterns
424 {
425 if (STAILQ_EMPTY(cl))
426 return true;
427
428 mutt_debug(LL_NOTIFY, "NT_COLOR_RESET: [ALL]\n");
429 struct EventColor ev_c = { cid, NULL };
431
433 return true;
434 }
435
436 bool rc = false;
437 struct RegexColor *np = NULL, *prev = NULL;
438 prev = NULL;
439 STAILQ_FOREACH(np, cl, entries)
440 {
441 if (mutt_str_equal(pat, np->pattern))
442 {
443 rc = true;
444
445 color_debug(LL_DEBUG1, "Freeing pattern \"%s\" from XXX\n", pat);
446 if (prev)
447 STAILQ_REMOVE_AFTER(cl, prev, entries);
448 else
449 STAILQ_REMOVE_HEAD(cl, entries);
450
451 mutt_debug(LL_NOTIFY, "NT_COLOR_RESET: XXX\n");
452 struct EventColor ev_c = { cid, &np->attr_color };
454
455 regex_color_free(&np);
456 break;
457 }
458 prev = np;
459 }
460
461 return rc;
462}
void attr_color_clear(struct AttrColor *ac)
Free the contents of an AttrColor.
Definition attr.c:47
void attr_color_overwrite(struct AttrColor *ac_old, const struct AttrColor *ac_new)
Update an AttrColor in-place.
Definition attr.c:394
Colour and attributes.
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition buffer.c:189
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:401
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
void get_colorid_name(unsigned int cid, struct Buffer *buf)
Get the name of a Colour ID.
Definition commands.c:128
Parse colour commands.
Color private Module data.
static enum CommandResult add_pattern(struct RegexColorList *rcl, const char *s, struct AttrColor *ac_val, struct Buffer *err, bool is_index, int match)
Associate a colour to a pattern.
Definition regex.c:244
struct RegexColorList * regex_colors_get_list(enum ColorId cid)
Return the RegexColorList for a Colour ID.
Definition regex.c:191
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:387
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:325
void regex_color_free(struct RegexColor **ptr)
Free a Regex colour.
Definition regex.c:129
bool regex_colors_parse_uncolor(enum ColorId cid, const char *pat)
Parse a Regex 'uncolor' command.
Definition regex.c:415
void regex_colors_cleanup(struct ColorModuleData *mod_data)
Cleanup the Regex colours.
Definition regex.c:100
void regex_colors_reset(struct ColorModuleData *mod_data)
Reset the Regex colours.
Definition regex.c:76
struct RegexColor * regex_color_new(void)
Create a new RegexColor.
Definition regex.c:144
void regex_colors_init(struct ColorModuleData *mod_data)
Initialise the Regex colours.
Definition regex.c:52
void regex_color_list_clear(struct RegexColorList *rcl)
Free the contents of a RegexColorList.
Definition regex.c:173
struct RegexColorList * regex_color_list_new(void)
Create a new RegexColorList.
Definition regex.c:153
void regex_color_clear(struct RegexColor *rcol)
Free the contents of a Regex colour.
Definition regex.c:111
Color and attribute parsing.
ColorId
List of all coloured objects.
Definition color.h:35
@ MT_COLOR_INDEX_AUTHOR
Index: author field.
Definition color.h:87
@ MT_COLOR_HEADER
Message headers (takes a pattern)
Definition color.h:48
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition color.h:78
@ MT_COLOR_INDEX_SIZE
Index: size field.
Definition color.h:93
@ MT_COLOR_INDEX_TAGS
Index: tags field (g, J)
Definition color.h:96
@ MT_COLOR_INDEX_SUBJECT
Index: subject field.
Definition color.h:94
@ MT_COLOR_BODY
Pager: highlight body of message (takes a pattern)
Definition color.h:39
@ MT_COLOR_INDEX_DATE
Index: date field.
Definition color.h:89
@ MT_COLOR_INDEX_TAG
Index: tag field (G)
Definition color.h:95
@ MT_COLOR_ATTACH_HEADERS
MIME attachment test (takes a pattern)
Definition color.h:38
@ MT_COLOR_INDEX_LABEL
Index: label field.
Definition color.h:91
@ MT_COLOR_INDEX
Index: default colour.
Definition color.h:86
@ MT_COLOR_INDEX_NUMBER
Index: index number.
Definition color.h:92
@ MT_COLOR_INDEX_FLAGS
Index: flags field.
Definition color.h:90
@ MT_COLOR_INDEX_COLLAPSED
Index: number of messages in collapsed thread.
Definition color.h:88
CommandResult
Error codes for command_t parse functions.
Definition command.h:37
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition command.h:40
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition command.h:38
struct PatternList * mutt_pattern_comp(struct MailboxView *mv, const char *s, PatternCompFlags flags, struct Buffer *err)
Create a Pattern.
Definition compile.c:964
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition compile.c:836
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
Colour Debugging.
static int color_debug(enum LogLevel level, const char *format,...)
Definition debug.h:51
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
GUI manage the main index (list of emails)
struct MailboxView * get_current_mailbox_view(void)
Get the current Mailbox view.
Definition index.c:692
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
@ LL_NOTIFY
Log of notifications.
Definition logging2.h:50
bool mutt_mb_is_lower(const char *s)
Does a multi-byte string contain only lowercase characters?
Definition mbyte.c:355
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
@ MODULE_ID_COLOR
ModuleColor, Color
Definition module_api.h:53
Convenience wrapper for the library headers.
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
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:665
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:666
Colour notifications.
@ NT_COLOR_RESET
Color has been reset/removed.
Definition notify2.h:41
@ NT_COLOR_SET
Color has been set.
Definition notify2.h:40
@ NT_COLOR
Colour has changed, NotifyColor, EventColor.
Definition notify_type.h:41
Match patterns to emails.
void mutt_check_simple(struct Buffer *s, const char *simple)
Convert a simple search into a real request.
Definition pattern.c:91
@ MUTT_PC_FULL_MSG
Enable body and header matching.
Definition lib.h:74
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
#define STAILQ_REMOVE_HEAD(head, field)
Definition queue.h:461
#define STAILQ_REMOVE(head, elm, type, field)
Definition queue.h:441
#define STAILQ_INIT(head)
Definition queue.h:410
#define STAILQ_REMOVE_AFTER(head, elm, field)
Definition queue.h:455
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:427
#define STAILQ_EMPTY(head)
Definition queue.h:382
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition queue.h:400
#define REG_COMP(preg, regex, cflags)
Compile a regular expression.
Definition regex3.h:49
Regex Colour.
#define NONULL(x)
Definition string2.h:44
A curses colour and its attributes.
Definition attr.h:65
String manipulation buffer.
Definition buffer.h:36
size_t dsize
Length of data.
Definition buffer.h:39
char * data
Pointer to data.
Definition buffer.h:37
Color private Module data.
Definition module_data.h:35
struct RegexColorList index_collapsed_list
List of colours applied to a collapsed thread in the index.
Definition module_data.h:47
struct RegexColorList index_subject_list
List of colours applied to the subject in the index.
Definition module_data.h:54
struct RegexColorList attach_list
List of colours applied to the attachment headers.
Definition module_data.h:43
struct RegexColorList body_list
List of colours applied to the email body.
Definition module_data.h:44
struct RegexColorList index_flags_list
List of colours applied to the flags in the index.
Definition module_data.h:49
struct RegexColorList index_label_list
List of colours applied to the label in the index.
Definition module_data.h:50
struct RegexColorList index_tags_list
List of colours applied to the tags in the index.
Definition module_data.h:56
struct RegexColorList index_size_list
List of colours applied to the size in the index.
Definition module_data.h:53
struct RegexColorList index_list
List of default colours applied to the index.
Definition module_data.h:51
struct Notify * colors_notify
Notifications: ColorId, EventColor.
Definition module_data.h:40
struct RegexColorList index_author_list
List of colours applied to the author in the index.
Definition module_data.h:46
struct RegexColorList header_list
List of colours applied to the email headers.
Definition module_data.h:45
struct RegexColorList index_date_list
List of colours applied to the date in the index.
Definition module_data.h:48
struct RegexColorList index_number_list
List of colours applied to the message number in the index.
Definition module_data.h:52
struct RegexColorList status_list
List of colours applied to the status bar.
Definition module_data.h:57
struct RegexColorList index_tag_list
List of colours applied to tags in the index.
Definition module_data.h:55
An Event that happened to a Colour.
Definition notify2.h:52
enum ColorId cid
Colour ID that has changed.
Definition notify2.h:53
View of a Mailbox.
Definition mview.h:40
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
A regular expression and a color to highlight a line.
Definition regex4.h:37
regex_t regex
Compiled regex.
Definition regex4.h:40
struct PatternList * color_pattern
Compiled pattern to speed up index color calculation.
Definition regex4.h:42
struct AttrColor attr_color
Colour and attributes to apply.
Definition regex4.h:38
char * pattern
Pattern to match.
Definition regex4.h:39
bool stop_matching
Used by the pager for body patterns, to prevent the color from being retried once it fails.
Definition regex4.h:44
int match
Substring to match, 0 for old behaviour.
Definition regex4.h:41