NeoMutt  2025-12-11-1009-ga75d9e
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
regex.c File Reference

Regex Colour. 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 "index/lib.h"
#include "pattern/lib.h"
#include "attr.h"
#include "color.h"
#include "commands.h"
#include "debug.h"
#include "module_data.h"
#include "notify2.h"
#include "regex4.h"
Include dependency graph for regex.c:

Go to the source code of this file.

Functions

void regex_colors_init (struct ColorModuleData *mod_data)
 Initialise the Regex colours.
void regex_colors_reset (struct ColorModuleData *mod_data)
 Reset the Regex colours.
void regex_colors_cleanup (struct ColorModuleData *mod_data)
 Cleanup the Regex colours.
void regex_color_clear (struct RegexColor *rcol)
 Free the contents of a Regex colour.
void regex_color_free (struct RegexColor **ptr)
 Free a Regex colour.
struct RegexColorregex_color_new (void)
 Create a new RegexColor.
struct RegexColorList * regex_color_list_new (void)
 Create a new RegexColorList.
void regex_color_list_clear (struct RegexColorList *rcl)
 Free the contents of a RegexColorList.
struct RegexColorList * regex_colors_get_list (enum ColorId cid)
 Return the RegexColorList for a Colour ID.
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.
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.
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.
bool regex_colors_parse_uncolor (enum ColorId cid, const char *pat)
 Parse a Regex 'uncolor' command.

Detailed Description

Regex Colour.

Authors
  • Richard Russon
  • Pietro Cerutti

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 regex.c.

Function Documentation

◆ regex_colors_init()

void regex_colors_init ( struct ColorModuleData * mod_data)

Initialise the Regex colours.

Parameters
mod_dataColor module data

Definition at line 52 of file regex.c.

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}
static int color_debug(enum LogLevel level, const char *format,...)
Definition debug.h:51
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
#define STAILQ_INIT(head)
Definition queue.h:410
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 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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ regex_colors_reset()

void regex_colors_reset ( struct ColorModuleData * mod_data)

Reset the Regex colours.

Parameters
mod_dataColor module data

Definition at line 76 of file regex.c.

77{
78 color_debug(LL_DEBUG5, "reset regex\n");
94}
void regex_color_list_clear(struct RegexColorList *rcl)
Free the contents of a RegexColorList.
Definition regex.c:173
Here is the call graph for this function:
Here is the caller graph for this function:

◆ regex_colors_cleanup()

void regex_colors_cleanup ( struct ColorModuleData * mod_data)

Cleanup the Regex colours.

Parameters
mod_dataColor module data

Definition at line 100 of file regex.c.

101{
102 regex_colors_reset(mod_data);
103}
void regex_colors_reset(struct ColorModuleData *mod_data)
Reset the Regex colours.
Definition regex.c:76
Here is the call graph for this function:
Here is the caller graph for this function:

◆ regex_color_clear()

void regex_color_clear ( struct RegexColor * rcol)

Free the contents of a Regex colour.

Parameters
rcolRegexColor to empty
Note
The RegexColor object isn't freed

Definition at line 111 of file regex.c.

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}
void attr_color_clear(struct AttrColor *ac)
Free the contents of an AttrColor.
Definition attr.c:47
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition compile.c:836
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ regex_color_free()

void regex_color_free ( struct RegexColor ** ptr)

Free a Regex colour.

Parameters
ptrRegexColor to free

Definition at line 129 of file regex.c.

130{
131 if (!ptr || !*ptr)
132 return;
133
134 struct RegexColor *rcol = *ptr;
135 regex_color_clear(rcol);
136
137 FREE(ptr);
138}
void regex_color_clear(struct RegexColor *rcol)
Free the contents of a Regex colour.
Definition regex.c:111
A regular expression and a color to highlight a line.
Definition regex4.h:37
Here is the call graph for this function:
Here is the caller graph for this function:

◆ regex_color_new()

struct RegexColor * regex_color_new ( void )

Create a new RegexColor.

Return values
ptrNew RegexColor

Definition at line 144 of file regex.c.

145{
146 return MUTT_MEM_CALLOC(1, struct RegexColor);
147}
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
Here is the caller graph for this function:

◆ regex_color_list_new()

struct RegexColorList * regex_color_list_new ( void )

Create a new RegexColorList.

Return values
ptrNew RegexColorList

Definition at line 153 of file regex.c.

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}
struct RegexColor * regex_color_new(void)
Create a new RegexColor.
Definition regex.c:144
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:427
Here is the call graph for this function:

◆ regex_color_list_clear()

void regex_color_list_clear ( struct RegexColorList * rcl)

Free the contents of a RegexColorList.

Parameters
rclList to clear

Free each of the RegexColorList in a list.

Note
The list object isn't freed, only emptied

Definition at line 173 of file regex.c.

174{
175 if (!rcl)
176 return;
177
178 struct RegexColor *np = NULL;
179 struct RegexColor *tmp = NULL;
180 STAILQ_FOREACH_SAFE(np, rcl, entries, tmp)
181 {
182 STAILQ_REMOVE(rcl, np, RegexColor, entries);
183 regex_color_free(&np);
184 }
185}
void regex_color_free(struct RegexColor **ptr)
Free a Regex colour.
Definition regex.c:129
#define STAILQ_REMOVE(head, elm, type, field)
Definition queue.h:441
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition queue.h:400
Here is the call graph for this function:
Here is the caller graph for this function:

◆ regex_colors_get_list()

struct RegexColorList * regex_colors_get_list ( enum ColorId cid)

Return the RegexColorList for a Colour ID.

Parameters
cidColour ID, e.g. MT_COLOR_BODY
Return values
ptrRegexColorList

Definition at line 192 of file regex.c.

193{
195 switch (cid)
196 {
198 return &mod_data->attach_list;
199 case MT_COLOR_BODY:
200 return &mod_data->body_list;
201 case MT_COLOR_HEADER:
202 return &mod_data->header_list;
203 case MT_COLOR_INDEX:
204 return &mod_data->index_list;
206 return &mod_data->index_author_list;
208 return &mod_data->index_collapsed_list;
210 return &mod_data->index_date_list;
212 return &mod_data->index_flags_list;
214 return &mod_data->index_label_list;
216 return &mod_data->index_number_list;
218 return &mod_data->index_size_list;
220 return &mod_data->index_subject_list;
222 return &mod_data->index_tag_list;
224 return &mod_data->index_tags_list;
225 case MT_COLOR_STATUS:
226 return &mod_data->status_list;
227 default:
228 return NULL;
229 }
230}
@ 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
@ MODULE_ID_COLOR
ModuleColor, Color
Definition module_api.h:53
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:666
Color private Module data.
Definition module_data.h:35
Container for Accounts, Notifications.
Definition neomutt.h:41
Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_pattern()

enum CommandResult add_pattern ( struct RegexColorList * rcl,
const char * s,
struct AttrColor * ac_val,
struct Buffer * err,
bool is_index,
int match )
static

Associate a colour to a pattern.

Parameters
rclList of existing colours
sString to match
ac_valColour value to use
errBuffer for error messages
is_indextrue of this is for the index
matchNumber of regex subexpression to match (0 for entire pattern)
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

is_index used to store compiled pattern only for 'index' color object when called from parse_color()

Definition at line 245 of file regex.c.

248{
249 struct RegexColor *rcol = NULL;
250
251 STAILQ_FOREACH(rcol, rcl, entries)
252 {
253 if (mutt_str_equal(s, rcol->pattern))
254 break;
255 }
256
257 if (rcol) // found a matching regex
258 {
259 struct AttrColor *ac = &rcol->attr_color;
260 attr_color_overwrite(ac, ac_val);
261 }
262 else
263 {
264 rcol = regex_color_new();
265 if (is_index)
266 {
267 struct Buffer *buf = buf_pool_get();
268 buf_strcpy(buf, s);
269 const char *const c_simple_search = cs_subset_string(NeoMutt->sub, "simple_search");
270 mutt_check_simple(buf, NONULL(c_simple_search));
271 struct MailboxView *mv_cur = get_current_mailbox_view();
273 buf_pool_release(&buf);
274 if (!rcol->color_pattern)
275 {
276 regex_color_free(&rcol);
277 return MUTT_CMD_ERROR;
278 }
279 }
280 else
281 {
282 // Smart case matching
283 uint16_t flags = mutt_mb_is_lower(s) ? REG_ICASE : 0;
284
285 const int r = REG_COMP(&rcol->regex, s, flags);
286 if (r != 0)
287 {
288 regerror(r, &rcol->regex, err->data, err->dsize);
289 buf_fix_dptr(err);
290 regex_color_free(&rcol);
291 return MUTT_CMD_ERROR;
292 }
293 }
294 rcol->pattern = mutt_str_dup(s);
295 rcol->match = match;
296
297 struct AttrColor *ac = &rcol->attr_color;
298
299 attr_color_overwrite(ac, ac_val);
300
301 STAILQ_INSERT_TAIL(rcl, rcol, entries);
302 }
303
304 if (is_index)
305 {
306 /* force re-caching of index colors */
308 struct EventColor ev_c = { MT_COLOR_INDEX, NULL };
310 }
311
312 return MUTT_CMD_SUCCESS;
313}
void attr_color_overwrite(struct AttrColor *ac_old, const struct AttrColor *ac_new)
Update an AttrColor in-place.
Definition attr.c:394
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
@ 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
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
struct MailboxView * get_current_mailbox_view(void)
Get the current Mailbox view.
Definition index.c:692
bool mutt_mb_is_lower(const char *s)
Does a multi-byte string contain only lowercase characters?
Definition mbyte.c:355
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:666
@ NT_COLOR_SET
Color has been set.
Definition notify2.h:40
@ NT_COLOR
Colour has changed, NotifyColor, EventColor.
Definition notify_type.h:41
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_FOREACH(var, head, field)
Definition queue.h:390
#define REG_COMP(preg, regex, cflags)
Compile a regular expression.
Definition regex3.h:49
#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
struct Notify * colors_notify
Notifications: ColorId, EventColor.
Definition module_data.h:40
An Event that happened to a Colour.
Definition notify2.h:52
View of a Mailbox.
Definition mview.h:40
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
Here is the call graph for this function:
Here is the caller graph for this function:

◆ regex_colors_parse_color_list()

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.

Parameters
cidColour ID, should be MT_COLOR_STATUS
patRegex pattern
acColour value to use
rcReturn code, e.g. MUTT_CMD_SUCCESS
errBuffer for error messages
Return values
trueColour was parsed

Parse a Regex 'color' command, e.g. "color index green default pattern"

Definition at line 326 of file regex.c.

329{
330 if (cid == MT_COLOR_STATUS)
331 return false;
332
333 struct RegexColorList *rcl = regex_colors_get_list(cid);
334 if (!rcl)
335 return false;
336
337 bool is_index = false;
338 switch (cid)
339 {
341 case MT_COLOR_BODY:
342 break;
343 case MT_COLOR_HEADER:
344 break;
345 case MT_COLOR_INDEX:
356 is_index = true;
357 break;
358 default:
359 return false;
360 }
361
362 *rc = add_pattern(rcl, pat, ac, err, is_index, 0);
363
364 struct Buffer *buf = buf_pool_get();
365 get_colorid_name(cid, buf);
366 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf_string(buf));
367 buf_pool_release(&buf);
368
369 if (!is_index) // else it will be logged in add_pattern()
370 {
372 struct EventColor ev_c = { cid, NULL };
374 }
375
376 return true;
377}
void get_colorid_name(unsigned int cid, struct Buffer *buf)
Get the name of a Colour ID.
Definition commands.c:128
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:245
struct RegexColorList * regex_colors_get_list(enum ColorId cid)
Return the RegexColorList for a Colour ID.
Definition regex.c:192
enum ColorId cid
Colour ID that has changed.
Definition notify2.h:53
Here is the call graph for this function:
Here is the caller graph for this function:

◆ regex_colors_parse_status_list()

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.

Parameters
cidColour ID, should be MT_COLOR_STATUS
patRegex pattern
acColour value to use
matchUse the nth regex submatch
errBuffer for error messages
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

Definition at line 388 of file regex.c.

390{
391 if (cid != MT_COLOR_STATUS)
392 return MUTT_CMD_ERROR;
393
395 int rc = add_pattern(&mod_data->status_list, pat, ac, err, false, match);
396 if (rc != MUTT_CMD_SUCCESS)
397 return rc;
398
399 struct Buffer *buf = buf_pool_get();
400 get_colorid_name(cid, buf);
401 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf_string(buf));
402 buf_pool_release(&buf);
403
404 struct EventColor ev_c = { cid, NULL };
406
407 return rc;
408}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ regex_colors_parse_uncolor()

bool regex_colors_parse_uncolor ( enum ColorId cid,
const char * pat )

Parse a Regex 'uncolor' command.

Parameters
cidColour ID, e.g. MT_COLOR_STATUS
patPattern to remove (NULL to remove all)
Return values
trueIf colours were unset

Definition at line 416 of file regex.c.

417{
418 struct RegexColorList *cl = regex_colors_get_list(cid);
419 if (!cl)
420 return false;
421
423
424 if (!pat) // Reset all patterns
425 {
426 if (STAILQ_EMPTY(cl))
427 return true;
428
429 mutt_debug(LL_NOTIFY, "NT_COLOR_RESET: [ALL]\n");
430 struct EventColor ev_c = { cid, NULL };
432
434 return true;
435 }
436
437 bool rc = false;
438 struct RegexColor *np = NULL;
439 struct RegexColor *prev = NULL;
440 prev = NULL;
441 STAILQ_FOREACH(np, cl, entries)
442 {
443 if (mutt_str_equal(pat, np->pattern))
444 {
445 rc = true;
446
447 color_debug(LL_DEBUG1, "Freeing pattern \"%s\" from XXX\n", pat);
448 if (prev)
449 STAILQ_REMOVE_AFTER(cl, prev, entries);
450 else
451 STAILQ_REMOVE_HEAD(cl, entries);
452
453 mutt_debug(LL_NOTIFY, "NT_COLOR_RESET: XXX\n");
454 struct EventColor ev_c = { cid, &np->attr_color };
456
457 regex_color_free(&np);
458 break;
459 }
460 prev = np;
461 }
462
463 return rc;
464}
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
@ LL_NOTIFY
Log of notifications.
Definition logging2.h:50
@ NT_COLOR_RESET
Color has been reset/removed.
Definition notify2.h:41
#define STAILQ_REMOVE_HEAD(head, field)
Definition queue.h:461
#define STAILQ_REMOVE_AFTER(head, elm, field)
Definition queue.h:455
#define STAILQ_EMPTY(head)
Definition queue.h:382
Here is the call graph for this function:
Here is the caller graph for this function: