NeoMutt  2025-09-05-43-g177ed6
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 "notify2.h"
45#include "regex4.h"
46
47// clang-format off
48struct RegexColorList AttachList;
49struct RegexColorList BodyList;
50struct RegexColorList HeaderList;
51struct RegexColorList IndexAuthorList;
52struct RegexColorList IndexCollapsedList;
53struct RegexColorList IndexDateList;
54struct RegexColorList IndexFlagsList;
55struct RegexColorList IndexLabelList;
56struct RegexColorList IndexList;
57struct RegexColorList IndexNumberList;
58struct RegexColorList IndexSizeList;
59struct RegexColorList IndexSubjectList;
60struct RegexColorList IndexTagList;
61struct RegexColorList IndexTagsList;
62struct RegexColorList StatusList;
63// clang-format on
64
87
110
115{
117}
118
126{
127 if (!rcol)
128 return;
129
130 rcol->match = 0;
131 rcol->stop_matching = false;
132
134 FREE(&rcol->pattern);
135 regfree(&rcol->regex);
137}
138
143void regex_color_free(struct RegexColor **ptr)
144{
145 if (!ptr || !*ptr)
146 return;
147
148 struct RegexColor *rcol = *ptr;
149 regex_color_clear(rcol);
150
151 FREE(ptr);
152}
153
159{
160 return MUTT_MEM_CALLOC(1, struct RegexColor);
161}
162
167struct RegexColorList *regex_color_list_new(void)
168{
169 struct RegexColorList *rcl = MUTT_MEM_CALLOC(1, struct RegexColorList);
170
171 STAILQ_INIT(rcl);
172
173 struct RegexColor *rcol = regex_color_new();
174 STAILQ_INSERT_TAIL(rcl, rcol, entries);
175
176 return rcl;
177}
178
187void regex_color_list_clear(struct RegexColorList *rcl)
188{
189 if (!rcl)
190 return;
191
192 struct RegexColor *np = NULL, *tmp = NULL;
193 STAILQ_FOREACH_SAFE(np, rcl, entries, tmp)
194 {
195 STAILQ_REMOVE(rcl, np, RegexColor, entries);
196 regex_color_free(&np);
197 }
198}
199
205struct RegexColorList *regex_colors_get_list(enum ColorId cid)
206{
207 switch (cid)
208 {
210 return &AttachList;
211 case MT_COLOR_BODY:
212 return &BodyList;
213 case MT_COLOR_HEADER:
214 return &HeaderList;
215 case MT_COLOR_INDEX:
216 return &IndexList;
218 return &IndexAuthorList;
220 return &IndexCollapsedList;
222 return &IndexDateList;
224 return &IndexFlagsList;
226 return &IndexLabelList;
228 return &IndexNumberList;
230 return &IndexSizeList;
232 return &IndexSubjectList;
234 return &IndexTagList;
236 return &IndexTagsList;
237 case MT_COLOR_STATUS:
238 return &StatusList;
239 default:
240 return NULL;
241 }
242}
243
257static enum CommandResult add_pattern(struct RegexColorList *rcl, const char *s,
258 struct AttrColor *ac_val,
259 struct Buffer *err, bool is_index, int match)
260{
261 struct RegexColor *rcol = NULL;
262
263 STAILQ_FOREACH(rcol, rcl, entries)
264 {
265 if (mutt_str_equal(s, rcol->pattern))
266 break;
267 }
268
269 if (rcol) // found a matching regex
270 {
271 struct AttrColor *ac = &rcol->attr_color;
272 attr_color_overwrite(ac, ac_val);
273 }
274 else
275 {
276 rcol = regex_color_new();
277 if (is_index)
278 {
279 struct Buffer *buf = buf_pool_get();
280 buf_strcpy(buf, s);
281 const char *const c_simple_search = cs_subset_string(NeoMutt->sub, "simple_search");
282 mutt_check_simple(buf, NONULL(c_simple_search));
283 struct MailboxView *mv_cur = get_current_mailbox_view();
285 buf_pool_release(&buf);
286 if (!rcol->color_pattern)
287 {
288 regex_color_free(&rcol);
289 return MUTT_CMD_ERROR;
290 }
291 }
292 else
293 {
294 // Smart case matching
295 uint16_t flags = mutt_mb_is_lower(s) ? REG_ICASE : 0;
296
297 const int r = REG_COMP(&rcol->regex, s, flags);
298 if (r != 0)
299 {
300 regerror(r, &rcol->regex, err->data, err->dsize);
301 regex_color_free(&rcol);
302 return MUTT_CMD_ERROR;
303 }
304 }
305 rcol->pattern = mutt_str_dup(s);
306 rcol->match = match;
307
308 struct AttrColor *ac = &rcol->attr_color;
309
310 attr_color_overwrite(ac, ac_val);
311
312 STAILQ_INSERT_TAIL(rcl, rcol, entries);
313 }
314
315 if (is_index)
316 {
317 /* force re-caching of index colors */
318 struct EventColor ev_c = { MT_COLOR_INDEX, NULL };
320 }
321
322 return MUTT_CMD_SUCCESS;
323}
324
336bool regex_colors_parse_color_list(enum ColorId cid, const char *pat,
337 struct AttrColor *ac, int *rc, struct Buffer *err)
338
339{
340 if (cid == MT_COLOR_STATUS)
341 return false;
342
343 struct RegexColorList *rcl = regex_colors_get_list(cid);
344 if (!rcl)
345 return false;
346
347 bool is_index = false;
348 switch (cid)
349 {
351 case MT_COLOR_BODY:
352 break;
353 case MT_COLOR_HEADER:
354 break;
355 case MT_COLOR_INDEX:
366 is_index = true;
367 break;
368 default:
369 return false;
370 }
371
372 *rc = add_pattern(rcl, pat, ac, err, is_index, 0);
373
374 struct Buffer *buf = buf_pool_get();
375 get_colorid_name(cid, buf);
376 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf_string(buf));
377 buf_pool_release(&buf);
378
379 if (!is_index) // else it will be logged in add_pattern()
380 {
381 struct EventColor ev_c = { cid, NULL };
383 }
384
385 return true;
386}
387
398 struct AttrColor *ac, int match, struct Buffer *err)
399{
400 if (cid != MT_COLOR_STATUS)
401 return MUTT_CMD_ERROR;
402
403 int rc = add_pattern(&StatusList, pat, ac, err, false, match);
404 if (rc != MUTT_CMD_SUCCESS)
405 return rc;
406
407 struct Buffer *buf = buf_pool_get();
408 get_colorid_name(cid, buf);
409 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf_string(buf));
410 buf_pool_release(&buf);
411
412 struct EventColor ev_c = { cid, NULL };
414
415 return rc;
416}
417
424bool regex_colors_parse_uncolor(enum ColorId cid, const char *pat)
425{
426 struct RegexColorList *cl = regex_colors_get_list(cid);
427 if (!cl)
428 return false;
429
430 if (!pat) // Reset all patterns
431 {
432 if (STAILQ_EMPTY(cl))
433 return true;
434
435 mutt_debug(LL_NOTIFY, "NT_COLOR_RESET: [ALL]\n");
436 struct EventColor ev_c = { cid, NULL };
438
440 return true;
441 }
442
443 bool rc = false;
444 struct RegexColor *np = NULL, *prev = NULL;
445 prev = NULL;
446 STAILQ_FOREACH(np, cl, entries)
447 {
448 if (mutt_str_equal(pat, np->pattern))
449 {
450 rc = true;
451
452 color_debug(LL_DEBUG1, "Freeing pattern \"%s\" from XXX\n", pat);
453 if (prev)
454 STAILQ_REMOVE_AFTER(cl, prev, entries);
455 else
456 STAILQ_REMOVE_HEAD(cl, entries);
457
458 mutt_debug(LL_NOTIFY, "NT_COLOR_RESET: XXX\n");
459 struct EventColor ev_c = { cid, &np->attr_color };
461
462 regex_color_free(&np);
463 break;
464 }
465 prev = np;
466 }
467
468 return rc;
469}
void attr_color_clear(struct AttrColor *ac)
Free the contents of an AttrColor.
Definition attr.c:48
void attr_color_overwrite(struct AttrColor *ac_old, const struct AttrColor *ac_new)
Update an AttrColor in-place.
Definition attr.c:395
Colour and attributes.
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:395
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.
struct Notify * ColorsNotify
Notifications: ColorId, EventColor.
Definition notify.c:36
struct RegexColorList IndexCollapsedList
List of colours applied to a collapsed thread in the index.
Definition regex.c:52
struct RegexColorList IndexFlagsList
List of colours applied to the flags in the index.
Definition regex.c:54
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:257
struct RegexColorList IndexAuthorList
List of colours applied to the author in the index.
Definition regex.c:51
struct RegexColorList IndexSubjectList
List of colours applied to the subject in the index.
Definition regex.c:59
struct RegexColorList IndexLabelList
List of colours applied to the label in the index.
Definition regex.c:55
struct RegexColorList * regex_colors_get_list(enum ColorId cid)
Return the RegexColorList for a Colour ID.
Definition regex.c:205
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:397
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:336
struct RegexColorList StatusList
List of colours applied to the status bar.
Definition regex.c:62
void regex_colors_init(void)
Initialise the Regex colours.
Definition regex.c:68
struct RegexColorList IndexList
List of default colours applied to the index.
Definition regex.c:56
struct RegexColorList IndexTagList
List of colours applied to tags in the index.
Definition regex.c:60
struct RegexColorList BodyList
List of colours applied to the email body.
Definition regex.c:49
struct RegexColorList IndexTagsList
List of colours applied to the tags in the index.
Definition regex.c:61
void regex_colors_reset(void)
Reset the Regex colours.
Definition regex.c:91
void regex_color_free(struct RegexColor **ptr)
Free a Regex colour.
Definition regex.c:143
bool regex_colors_parse_uncolor(enum ColorId cid, const char *pat)
Parse a Regex 'uncolor' command.
Definition regex.c:424
struct RegexColorList HeaderList
List of colours applied to the email headers.
Definition regex.c:50
struct RegexColorList AttachList
List of colours applied to the attachment headers.
Definition regex.c:48
void regex_colors_cleanup(void)
Cleanup the Regex colours.
Definition regex.c:114
struct RegexColorList IndexDateList
List of colours applied to the date in the index.
Definition regex.c:53
struct RegexColor * regex_color_new(void)
Create a new RegexColor.
Definition regex.c:158
struct RegexColorList IndexNumberList
List of colours applied to the message number in the index.
Definition regex.c:57
void regex_color_list_clear(struct RegexColorList *rcl)
Free the contents of a RegexColorList.
Definition regex.c:187
struct RegexColorList IndexSizeList
List of colours applied to the size in the index.
Definition regex.c:58
struct RegexColorList * regex_color_list_new(void)
Create a new RegexColorList.
Definition regex.c:167
void regex_color_clear(struct RegexColor *rcol)
Free the contents of a Regex colour.
Definition regex.c:125
Color and attribute parsing.
ColorId
List of all coloured objects.
Definition color.h:36
@ MT_COLOR_INDEX_AUTHOR
Index: author field.
Definition color.h:88
@ MT_COLOR_HEADER
Message headers (takes a pattern)
Definition color.h:49
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition color.h:79
@ MT_COLOR_INDEX_SIZE
Index: size field.
Definition color.h:94
@ MT_COLOR_INDEX_TAGS
Index: tags field (g, J)
Definition color.h:97
@ MT_COLOR_INDEX_SUBJECT
Index: subject field.
Definition color.h:95
@ MT_COLOR_BODY
Pager: highlight body of message (takes a pattern)
Definition color.h:40
@ MT_COLOR_INDEX_DATE
Index: date field.
Definition color.h:90
@ MT_COLOR_INDEX_TAG
Index: tag field (G)
Definition color.h:96
@ MT_COLOR_ATTACH_HEADERS
MIME attachment test (takes a pattern)
Definition color.h:39
@ MT_COLOR_INDEX_LABEL
Index: label field.
Definition color.h:92
@ MT_COLOR_INDEX
Index: default colour.
Definition color.h:87
@ MT_COLOR_INDEX_NUMBER
Index: index number.
Definition color.h:93
@ MT_COLOR_INDEX_FLAGS
Index: flags field.
Definition color.h:91
@ MT_COLOR_INDEX_COLLAPSED
Index: number of messages in collapsed thread.
Definition color.h:89
CommandResult
Error codes for command_t parse functions.
Definition command.h:35
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition command.h:38
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition command.h:36
struct PatternList * mutt_pattern_comp(struct MailboxView *mv, const char *s, PatternCompFlags flags, struct Buffer *err)
Create a Pattern.
Definition compile.c:902
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition compile.c:774
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:52
#define mutt_debug(LEVEL,...)
Definition logging2.h:90
GUI manage the main index (list of emails)
struct MailboxView * get_current_mailbox_view(void)
Get the current Mailbox view.
Definition index.c:689
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:48
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:44
@ LL_NOTIFY
Log of notifications.
Definition logging2.h:49
bool mutt_mb_is_lower(const char *s)
Does a multi-byte string contain only lowercase characters?
Definition mbyte.c:354
#define FREE(x)
Definition memory.h:62
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:47
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:255
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:660
Colour notifications.
@ NT_COLOR_RESET
Color has been reset/removed.
Definition notify2.h:44
@ NT_COLOR_SET
Color has been set.
Definition notify2.h:43
@ NT_COLOR
Colour has changed, NotifyColor, EventColor.
Definition notify_type.h:41
Match patterns to emails.
#define MUTT_PC_FULL_MSG
Enable body and header matching.
Definition lib.h:69
void mutt_check_simple(struct Buffer *s, const char *simple)
Convert a simple search into a real request.
Definition pattern.c:109
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
#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:50
Regex Colour.
#define NONULL(x)
Definition string2.h:43
A curses colour and its attributes.
Definition attr.h:66
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
An Event that happened to a Colour.
Definition notify2.h:55
enum ColorId cid
Colour ID that has changed.
Definition notify2.h:56
View of a Mailbox.
Definition mview.h:40
Container for Accounts, Notifications.
Definition neomutt.h:43
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:47
A regular expression and a color to highlight a line.
Definition regex4.h:36
regex_t regex
Compiled regex.
Definition regex4.h:39
struct PatternList * color_pattern
Compiled pattern to speed up index color calculation.
Definition regex4.h:41
struct AttrColor attr_color
Colour and attributes to apply.
Definition regex4.h:37
char * pattern
Pattern to match.
Definition regex4.h:38
bool stop_matching
Used by the pager for body patterns, to prevent the color from being retried once it fails.
Definition regex4.h:43
int match
Substring to match, 0 for old behaviour.
Definition regex4.h:40