NeoMutt  2022-04-29-247-gc6aae8
Teaching an old dog new tricks
DOXYGEN
regex.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stddef.h>
32#include <stdbool.h>
33#include <stdint.h>
34#include "mutt/lib.h"
35#include "config/lib.h"
36#include "core/lib.h"
37#include "index/lib.h"
38#include "pattern/lib.h"
39#include "attr.h"
40#include "color.h"
41#include "command2.h"
42#include "curses2.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 struct RegexColor *rcol = mutt_mem_calloc(1, sizeof(*rcol));
154
155 return rcol;
156}
157
166void regex_color_list_clear(struct RegexColorList *rcl)
167{
168 if (!rcl)
169 return;
170
171 struct RegexColor *np = NULL, *tmp = NULL;
172 STAILQ_FOREACH_SAFE(np, rcl, entries, tmp)
173 {
174 STAILQ_REMOVE(rcl, np, RegexColor, entries);
175 regex_color_free(rcl, &np);
176 }
177}
178
184struct RegexColorList *regex_colors_get_list(enum ColorId cid)
185{
186 switch (cid)
187 {
189 return &AttachList;
190 case MT_COLOR_BODY:
191 return &BodyList;
192 case MT_COLOR_HEADER:
193 return &HeaderList;
194 case MT_COLOR_INDEX:
195 return &IndexList;
197 return &IndexAuthorList;
199 return &IndexCollapsedList;
201 return &IndexDateList;
203 return &IndexFlagsList;
205 return &IndexLabelList;
207 return &IndexNumberList;
209 return &IndexSizeList;
211 return &IndexSubjectList;
213 return &IndexTagList;
215 return &IndexTagsList;
216 case MT_COLOR_STATUS:
217 return &StatusList;
218 default:
219 return NULL;
220 }
221}
222
239static enum CommandResult add_pattern(struct RegexColorList *rcl, const char *s,
240 bool sensitive, uint32_t fg, uint32_t bg, int attrs,
241 struct Buffer *err, bool is_index, int match)
242{
243 struct RegexColor *rcol = NULL;
244
245 STAILQ_FOREACH(rcol, rcl, entries)
246 {
247 if ((sensitive && mutt_str_equal(s, rcol->pattern)) ||
248 (!sensitive && mutt_istr_equal(s, rcol->pattern)))
249 {
250 break;
251 }
252 }
253
254 if (rcol) // found a matching regex
255 {
256 struct AttrColor *ac = &rcol->attr_color;
257 struct CursesColor *cc = ac->curses_color;
258
259 // different colours
260 if (!cc || (cc->fg != fg) || (cc->bg != bg))
261 {
262 cc = curses_color_new(fg, bg);
263 if (cc)
264 {
266 cc->fg = fg;
267 cc->bg = bg;
268 }
269 ac->curses_color = cc;
270 }
271 ac->attrs = attrs;
272 }
273 else
274 {
275 rcol = regex_color_new();
276 if (is_index)
277 {
278 struct Buffer *buf = mutt_buffer_pool_get();
279 mutt_buffer_strcpy(buf, s);
280 const char *const c_simple_search = cs_subset_string(NeoMutt->sub, "simple_search");
281 mutt_check_simple(buf, NONULL(c_simple_search));
282 struct Mailbox *m_cur = get_current_mailbox();
283 struct Menu *menu = get_current_menu();
284 rcol->color_pattern = mutt_pattern_comp(m_cur, menu, buf->data, MUTT_PC_FULL_MSG, err);
286 if (!rcol->color_pattern)
287 {
288 regex_color_free(rcl, &rcol);
289 return MUTT_CMD_ERROR;
290 }
291 }
292 else
293 {
294 uint16_t flags = 0;
295 if (sensitive)
296 flags = mutt_mb_is_lower(s) ? REG_ICASE : 0;
297 else
298 flags = REG_ICASE;
299
300 const int r = REG_COMP(&rcol->regex, s, flags);
301 if (r != 0)
302 {
303 regerror(r, &rcol->regex, err->data, err->dsize);
304 regex_color_free(rcl, &rcol);
305 return MUTT_CMD_ERROR;
306 }
307 }
308 rcol->pattern = mutt_str_dup(s);
309 rcol->match = match;
310 struct CursesColor *cc = curses_color_new(fg, bg);
311 struct AttrColor *ac = &rcol->attr_color;
312 ac->curses_color = cc;
313 ac->attrs = attrs;
314 STAILQ_INSERT_TAIL(rcl, rcol, entries);
315 }
316
317 if (is_index)
318 {
319 /* force re-caching of index colors */
320 struct EventColor ev_c = { MT_COLOR_INDEX, NULL };
322 }
323
324 return MUTT_CMD_SUCCESS;
325}
326
340bool regex_colors_parse_color_list(enum ColorId cid, const char *pat, uint32_t fg,
341 uint32_t bg, int attrs, int *rc, struct Buffer *err)
342
343{
344 if (cid == MT_COLOR_STATUS)
345 return false;
346
347 struct RegexColorList *rcl = regex_colors_get_list(cid);
348 if (!rcl)
349 return false;
350
351 bool sensitive = false;
352 bool is_index = false;
353 switch (cid)
354 {
356 case MT_COLOR_BODY:
357 sensitive = true;
358 is_index = false;
359 break;
360 case MT_COLOR_HEADER:
361 sensitive = false;
362 is_index = false;
363 break;
364 case MT_COLOR_INDEX:
375 sensitive = true;
376 is_index = true;
377 break;
378 default:
379 return false;
380 }
381
382 *rc = add_pattern(rcl, pat, sensitive, fg, bg, attrs, err, is_index, 0);
383
384 struct Buffer *buf = mutt_buffer_pool_get();
385 get_colorid_name(cid, buf);
386 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf->data);
388
389 if (!is_index) // else it will be logged in add_pattern()
390 {
391 struct EventColor ev_c = { cid, NULL };
393 }
394
396 return true;
397}
398
410int regex_colors_parse_status_list(enum ColorId cid, const char *pat, uint32_t fg,
411 uint32_t bg, int attrs, int match, struct Buffer *err)
412{
413 if (cid != MT_COLOR_STATUS)
414 return MUTT_CMD_ERROR;
415
416 int rc = add_pattern(&StatusList, pat, true, fg, bg, attrs, err, false, match);
417 if (rc != MUTT_CMD_SUCCESS)
418 return rc;
419
420 struct Buffer *buf = mutt_buffer_pool_get();
421 get_colorid_name(cid, buf);
422 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf->data);
424
425 struct EventColor ev_c = { cid, NULL };
427
429 return rc;
430}
431
439bool regex_colors_parse_uncolor(enum ColorId cid, const char *pat, bool uncolor)
440{
441 struct RegexColorList *cl = regex_colors_get_list(cid);
442 if (!cl)
443 return false;
444
445 if (!pat) // Reset all patterns
446 {
447 if (STAILQ_EMPTY(cl))
448 return true;
449
450 mutt_debug(LL_NOTIFY, "NT_COLOR_RESET: [ALL]\n");
451 struct EventColor ev_c = { cid, NULL };
453
455 return true;
456 }
457
458 bool rc = false;
459 struct RegexColor *np = NULL, *prev = NULL;
460 prev = NULL;
461 STAILQ_FOREACH(np, cl, entries)
462 {
463 if (mutt_str_equal(pat, np->pattern))
464 {
465 rc = true;
466
467 mutt_debug(LL_DEBUG1, "Freeing pattern \"%s\" from XXX\n", pat);
468 if (prev)
469 STAILQ_REMOVE_AFTER(cl, prev, entries);
470 else
471 STAILQ_REMOVE_HEAD(cl, entries);
472
473 mutt_debug(LL_NOTIFY, "NT_COLOR_RESET: XXX\n");
474 struct EventColor ev_c = { cid, &np->attr_color };
476
477 regex_color_free(cl, &np);
478 break;
479 }
480 prev = np;
481 }
482
483 return rc;
484}
void attr_color_clear(struct AttrColor *ac)
Free the contents of an AttrColor.
Definition: attr.c:44
Colour and attributes.
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:327
void get_colorid_name(unsigned int cid, struct Buffer *buf)
Get the name of a color id.
Definition: command.c:329
struct Notify * ColorsNotify
Notifications: ColorId, EventColor.
Definition: notify.c:34
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:184
void regex_colors_clear(void)
Clear the Regex colours.
Definition: regex.c:91
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:439
static enum CommandResult add_pattern(struct RegexColorList *rcl, const char *s, bool sensitive, uint32_t fg, uint32_t bg, int attrs, struct Buffer *err, bool is_index, int match)
Associate a colour to a pattern.
Definition: regex.c:239
struct RegexColorList HeaderList
List of colours applied to the email headers.
Definition: regex.c:50
bool regex_colors_parse_color_list(enum ColorId cid, const char *pat, uint32_t fg, uint32_t bg, int attrs, int *rc, struct Buffer *err)
Parse a Regex 'color' command.
Definition: regex.c:340
struct RegexColorList AttachList
List of colours applied to the attachment headers.
Definition: regex.c:48
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
int regex_colors_parse_status_list(enum ColorId cid, const char *pat, uint32_t fg, uint32_t bg, int attrs, int match, struct Buffer *err)
Parse a Regex 'color status' command.
Definition: regex.c:410
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:166
struct RegexColorList IndexSizeList
List of colours applied to the size in the index.
Definition: regex.c:58
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:35
@ MT_COLOR_INDEX_AUTHOR
Index: author field.
Definition: color.h:78
@ MT_COLOR_HEADER
Message headers (takes a pattern)
Definition: color.h:48
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition: color.h:71
@ MT_COLOR_INDEX_SIZE
Index: size field.
Definition: color.h:84
@ MT_COLOR_INDEX_TAGS
Index: tags field (g, J)
Definition: color.h:87
@ MT_COLOR_INDEX_SUBJECT
Index: subject field.
Definition: color.h:85
@ 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:80
@ MT_COLOR_INDEX_TAG
Index: tag field (G)
Definition: color.h:86
@ MT_COLOR_ATTACH_HEADERS
MIME attachment test (takes a pattern)
Definition: color.h:38
@ MT_COLOR_INDEX_LABEL
Index: label field.
Definition: color.h:82
@ MT_COLOR_INDEX
Index: default colour.
Definition: color.h:77
@ MT_COLOR_INDEX_NUMBER
Index: index number.
Definition: color.h:83
@ MT_COLOR_INDEX_FLAGS
Index: flags field.
Definition: color.h:81
@ MT_COLOR_INDEX_COLLAPSED
Index: number of messages in collapsed thread.
Definition: color.h:79
Parse colour commands.
CommandResult
Error codes for command_t parse functions.
Definition: command.h:34
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:37
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:35
struct PatternList * mutt_pattern_comp(struct Mailbox *m, struct Menu *menu, const char *s, PatternCompFlags flags, struct Buffer *err)
Create a Pattern.
Definition: compile.c:1160
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:1038
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
Curses Colour.
struct CursesColor * curses_color_new(int fg, int bg)
Create a new CursesColor.
Definition: curses.c:148
void regex_colors_dump_all(void)
Dump all the Regex colours to the log.
Definition: debug.c:380
int color_debug(enum LogLevel level, const char *format,...)
Write to the log file.
Definition: debug.c:44
Colour Debugging.
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
GUI manage the main index (list of emails)
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition: index.c:618
struct Menu * get_current_menu(void)
Get the current Menu.
Definition: index.c:644
@ LL_DEBUG5
Log at debug level 5.
Definition: logging.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
@ LL_NOTIFY
Log of notifications.
Definition: logging.h:45
bool mutt_mb_is_lower(const char *s)
Does a multi-byte string contain only lowercase characters?
Definition: mbyte.c:355
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:43
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:171
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:819
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
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:62
void mutt_check_simple(struct Buffer *s, const char *simple)
Convert a simple search into a real request.
Definition: pattern.c:116
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
#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:53
Regex Colour.
#define NONULL(x)
Definition: string2.h:37
A curses colour and its attributes.
Definition: attr.h:35
int attrs
Text attributes, e.g. A_BOLD.
Definition: attr.h:37
struct CursesColor * curses_color
Underlying Curses colour.
Definition: attr.h:36
String manipulation buffer.
Definition: buffer.h:34
size_t dsize
Length of data.
Definition: buffer.h:37
char * data
Pointer to data.
Definition: buffer.h:35
Colour in the ncurses palette.
Definition: curses2.h:38
uint32_t fg
Foreground colour.
Definition: curses2.h:41
uint32_t bg
Background colour.
Definition: curses2.h:42
An Event that happened to a Colour.
Definition: notify2.h:53
enum ColorId cid
Colour ID that has changed.
Definition: notify2.h:54
A mailbox.
Definition: mailbox.h:79
Definition: lib.h:69
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
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