NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
regex.c
Go to the documentation of this file.
1
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 "command2.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
69{
70 color_debug(LL_DEBUG5, "init AttachList, BodyList, etc\n");
86}
87
92{
93 color_debug(LL_DEBUG5, "clean up regex\n");
109}
110
118{
119 if (!rcol)
120 return;
121
122 rcol->match = 0;
123 rcol->stop_matching = false;
124
126 FREE(&rcol->pattern);
127 regfree(&rcol->regex);
129}
130
136void regex_color_free(struct RegexColorList *list, struct RegexColor **ptr)
137{
138 if (!ptr || !*ptr)
139 return;
140
141 struct RegexColor *rcol = *ptr;
142 regex_color_clear(rcol);
143
144 FREE(ptr);
145}
146
152{
153 return MUTT_MEM_CALLOC(1, struct RegexColor);
154}
155
164void regex_color_list_clear(struct RegexColorList *rcl)
165{
166 if (!rcl)
167 return;
168
169 struct RegexColor *np = NULL, *tmp = NULL;
170 STAILQ_FOREACH_SAFE(np, rcl, entries, tmp)
171 {
172 STAILQ_REMOVE(rcl, np, RegexColor, entries);
173 regex_color_free(rcl, &np);
174 }
175}
176
182struct RegexColorList *regex_colors_get_list(enum ColorId cid)
183{
184 switch (cid)
185 {
187 return &AttachList;
188 case MT_COLOR_BODY:
189 return &BodyList;
190 case MT_COLOR_HEADER:
191 return &HeaderList;
192 case MT_COLOR_INDEX:
193 return &IndexList;
195 return &IndexAuthorList;
197 return &IndexCollapsedList;
199 return &IndexDateList;
201 return &IndexFlagsList;
203 return &IndexLabelList;
205 return &IndexNumberList;
207 return &IndexSizeList;
209 return &IndexSubjectList;
211 return &IndexTagList;
213 return &IndexTagsList;
214 case MT_COLOR_STATUS:
215 return &StatusList;
216 default:
217 return NULL;
218 }
219}
220
235static enum CommandResult add_pattern(struct RegexColorList *rcl, const char *s,
236 bool sensitive, struct AttrColor *ac_val,
237 struct Buffer *err, bool is_index, int match)
238{
239 struct RegexColor *rcol = NULL;
240
241 STAILQ_FOREACH(rcol, rcl, entries)
242 {
243 if ((sensitive && mutt_str_equal(s, rcol->pattern)) ||
244 (!sensitive && mutt_istr_equal(s, rcol->pattern)))
245 {
246 break;
247 }
248 }
249
250 if (rcol) // found a matching regex
251 {
252 struct AttrColor *ac = &rcol->attr_color;
253 attr_color_overwrite(ac, ac_val);
254 }
255 else
256 {
257 rcol = regex_color_new();
258 if (is_index)
259 {
260 struct Buffer *buf = buf_pool_get();
261 buf_strcpy(buf, s);
262 const char *const c_simple_search = cs_subset_string(NeoMutt->sub, "simple_search");
263 mutt_check_simple(buf, NONULL(c_simple_search));
264 struct MailboxView *mv_cur = get_current_mailbox_view();
265 struct Menu *menu = get_current_menu();
266 rcol->color_pattern = mutt_pattern_comp(mv_cur, menu, buf->data, MUTT_PC_FULL_MSG, err);
267 buf_pool_release(&buf);
268 if (!rcol->color_pattern)
269 {
270 regex_color_free(rcl, &rcol);
271 return MUTT_CMD_ERROR;
272 }
273 }
274 else
275 {
276 uint16_t flags = 0;
277 if (sensitive)
278 flags = mutt_mb_is_lower(s) ? REG_ICASE : 0;
279 else
280 flags = REG_ICASE;
281
282 const int r = REG_COMP(&rcol->regex, s, flags);
283 if (r != 0)
284 {
285 regerror(r, &rcol->regex, err->data, err->dsize);
286 regex_color_free(rcl, &rcol);
287 return MUTT_CMD_ERROR;
288 }
289 }
290 rcol->pattern = mutt_str_dup(s);
291 rcol->match = match;
292
293 struct AttrColor *ac = &rcol->attr_color;
294
295 attr_color_overwrite(ac, ac_val);
296
297 STAILQ_INSERT_TAIL(rcl, rcol, entries);
298 }
299
300 if (is_index)
301 {
302 /* force re-caching of index colors */
303 struct EventColor ev_c = { MT_COLOR_INDEX, NULL };
305 }
306
307 return MUTT_CMD_SUCCESS;
308}
309
321bool regex_colors_parse_color_list(enum ColorId cid, const char *pat,
322 struct AttrColor *ac, int *rc, struct Buffer *err)
323
324{
325 if (cid == MT_COLOR_STATUS)
326 return false;
327
328 struct RegexColorList *rcl = regex_colors_get_list(cid);
329 if (!rcl)
330 return false;
331
332 bool sensitive = false;
333 bool is_index = false;
334 switch (cid)
335 {
337 case MT_COLOR_BODY:
338 sensitive = true;
339 is_index = false;
340 break;
341 case MT_COLOR_HEADER:
342 sensitive = false;
343 is_index = false;
344 break;
345 case MT_COLOR_INDEX:
356 sensitive = true;
357 is_index = true;
358 break;
359 default:
360 return false;
361 }
362
363 *rc = add_pattern(rcl, pat, sensitive, ac, err, is_index, 0);
364
365 struct Buffer *buf = buf_pool_get();
366 get_colorid_name(cid, buf);
367 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf->data);
368 buf_pool_release(&buf);
369
370 if (!is_index) // else it will be logged in add_pattern()
371 {
372 struct EventColor ev_c = { cid, NULL };
374 }
375
376 return true;
377}
378
389 struct AttrColor *ac, int match, struct Buffer *err)
390{
391 if (cid != MT_COLOR_STATUS)
392 return MUTT_CMD_ERROR;
393
394 int rc = add_pattern(&StatusList, pat, true, 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->data);
401 buf_pool_release(&buf);
402
403 struct EventColor ev_c = { cid, NULL };
405
406 return rc;
407}
408
416bool regex_colors_parse_uncolor(enum ColorId cid, const char *pat, bool uncolor)
417{
418 struct RegexColorList *cl = regex_colors_get_list(cid);
419 if (!cl)
420 return false;
421
422 if (!pat) // Reset all patterns
423 {
424 if (STAILQ_EMPTY(cl))
425 return true;
426
427 mutt_debug(LL_NOTIFY, "NT_COLOR_RESET: [ALL]\n");
428 struct EventColor ev_c = { cid, NULL };
430
432 return true;
433 }
434
435 bool rc = false;
436 struct RegexColor *np = NULL, *prev = NULL;
437 prev = NULL;
438 STAILQ_FOREACH(np, cl, entries)
439 {
440 if (mutt_str_equal(pat, np->pattern))
441 {
442 rc = true;
443
444 color_debug(LL_DEBUG1, "Freeing pattern \"%s\" from XXX\n", pat);
445 if (prev)
446 STAILQ_REMOVE_AFTER(cl, prev, entries);
447 else
448 STAILQ_REMOVE_HEAD(cl, entries);
449
450 mutt_debug(LL_NOTIFY, "NT_COLOR_RESET: XXX\n");
451 struct EventColor ev_c = { cid, &np->attr_color };
453
454 regex_color_free(cl, &np);
455 break;
456 }
457 prev = np;
458 }
459
460 return rc;
461}
void attr_color_overwrite(struct AttrColor *ac_old, struct AttrColor *ac_new)
Update an AttrColor in-place.
Definition: attr.c:395
void attr_color_clear(struct AttrColor *ac)
Free the contents of an AttrColor.
Definition: attr.c:48
Colour and attributes.
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:127
struct Notify * ColorsNotify
Notifications: ColorId, EventColor.
Definition: notify.c:35
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
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:182
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:388
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:321
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
void regex_color_free(struct RegexColorList *list, struct RegexColor **ptr)
Free a Regex colour.
Definition: regex.c:136
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
bool regex_colors_parse_uncolor(enum ColorId cid, const char *pat, bool uncolor)
Parse a Regex 'uncolor' command.
Definition: regex.c:416
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)
Clear the Regex colours.
Definition: regex.c:91
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:151
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:164
struct RegexColorList IndexSizeList
List of colours applied to the size in the index.
Definition: regex.c:58
static enum CommandResult add_pattern(struct RegexColorList *rcl, const char *s, bool sensitive, struct AttrColor *ac_val, struct Buffer *err, bool is_index, int match)
Associate a colour to a pattern.
Definition: regex.c:235
void regex_color_clear(struct RegexColor *rcol)
Free the contents of a Regex colour.
Definition: regex.c:117
Color and attribute parsing.
ColorId
List of all colored objects.
Definition: color.h:40
@ MT_COLOR_INDEX_AUTHOR
Index: author field.
Definition: color.h:84
@ MT_COLOR_HEADER
Message headers (takes a pattern)
Definition: color.h:53
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition: color.h:75
@ MT_COLOR_INDEX_SIZE
Index: size field.
Definition: color.h:90
@ MT_COLOR_INDEX_TAGS
Index: tags field (g, J)
Definition: color.h:93
@ MT_COLOR_INDEX_SUBJECT
Index: subject field.
Definition: color.h:91
@ MT_COLOR_BODY
Pager: highlight body of message (takes a pattern)
Definition: color.h:44
@ MT_COLOR_INDEX_DATE
Index: date field.
Definition: color.h:86
@ MT_COLOR_INDEX_TAG
Index: tag field (G)
Definition: color.h:92
@ MT_COLOR_ATTACH_HEADERS
MIME attachment test (takes a pattern)
Definition: color.h:43
@ MT_COLOR_INDEX_LABEL
Index: label field.
Definition: color.h:88
@ MT_COLOR_INDEX
Index: default colour.
Definition: color.h:83
@ MT_COLOR_INDEX_NUMBER
Index: index number.
Definition: color.h:89
@ MT_COLOR_INDEX_FLAGS
Index: flags field.
Definition: color.h:87
@ MT_COLOR_INDEX_COLLAPSED
Index: number of messages in collapsed thread.
Definition: color.h:85
Parse colour commands.
CommandResult
Error codes for command_t parse functions.
Definition: command.h:36
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:39
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:37
struct PatternList * mutt_pattern_comp(struct MailboxView *mv, struct Menu *menu, const char *s, PatternCompFlags flags, struct Buffer *err)
Create a Pattern.
Definition: compile.c:906
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:778
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:89
GUI manage the main index (list of emails)
struct MailboxView * get_current_mailbox_view(void)
Get the current Mailbox view.
Definition: index.c:683
struct Menu * get_current_menu(void)
Get the current Menu.
Definition: index.c:731
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
@ LL_NOTIFY
Log of notifications.
Definition: logging2.h:48
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:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
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
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:672
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
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:42
@ NT_COLOR_SET
Color has been set.
Definition: notify2.h:41
@ 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:68
void mutt_check_simple(struct Buffer *s, const char *simple)
Convert a simple search into a real request.
Definition: pattern.c:112
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
#define STAILQ_REMOVE_HEAD(head, field)
Definition: queue.h:422
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
#define STAILQ_INIT(head)
Definition: queue.h:372
#define STAILQ_REMOVE_AFTER(head, elm, field)
Definition: queue.h:416
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:389
#define STAILQ_EMPTY(head)
Definition: queue.h:348
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:362
#define REG_COMP(preg, regex, cflags)
Compile a regular expression.
Definition: regex3.h:50
Regex Colour.
#define NONULL(x)
Definition: string2.h:37
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:53
enum ColorId cid
Colour ID that has changed.
Definition: notify2.h:54
View of a Mailbox.
Definition: mview.h:40
Definition: lib.h:79
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
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