NeoMutt  2023-11-03-85-g512e01
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
regex4.h File Reference

Regex Colour. More...

#include "config.h"
#include <stdbool.h>
#include "mutt/lib.h"
#include "attr.h"
#include "color.h"
+ Include dependency graph for regex4.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  RegexColor
 A regular expression and a color to highlight a line. More...
 

Functions

 STAILQ_HEAD (RegexColorList, RegexColor)
 
void regex_color_clear (struct RegexColor *rcol)
 Free the contents of a Regex colour.
 
void regex_color_free (struct RegexColorList *list, struct RegexColor **ptr)
 Free a Regex colour.
 
struct RegexColorregex_color_new (void)
 Create a new RegexColor.
 
void regex_colors_cleanup (void)
 Clear the Regex colours.
 
struct RegexColorList * regex_colors_get_list (enum ColorId cid)
 Return the RegexColorList for a colour id.
 
void regex_colors_init (void)
 Initialise the Regex colours.
 
void regex_color_list_clear (struct RegexColorList *rcl)
 Free the contents of a RegexColorList.
 
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, bool uncolor)
 Parse a Regex 'uncolor' command.
 

Detailed Description

Regex Colour.

Authors
  • Richard Russon

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 regex4.h.

Function Documentation

◆ STAILQ_HEAD()

STAILQ_HEAD ( RegexColorList  ,
RegexColor   
)

◆ 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 116 of file regex.c.

117{
118 if (!rcol)
119 return;
120
121 rcol->match = 0;
122 rcol->stop_matching = false;
123
125 FREE(&rcol->pattern);
126 regfree(&rcol->regex);
128}
void attr_color_clear(struct AttrColor *ac)
Free the contents of an AttrColor.
Definition: attr.c:49
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:777
#define FREE(x)
Definition: memory.h:45
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
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ regex_color_free()

void regex_color_free ( struct RegexColorList *  list,
struct RegexColor **  ptr 
)

Free a Regex colour.

Parameters
listRegexColorList holding the colour
ptrRegexColor to free

Definition at line 135 of file regex.c.

136{
137 if (!ptr || !*ptr)
138 return;
139
140 struct RegexColor *rcol = *ptr;
141 regex_color_clear(rcol);
142
143 FREE(ptr);
144}
void regex_color_clear(struct RegexColor *rcol)
Free the contents of a Regex colour.
Definition: regex.c:116
A regular expression and a color to highlight a line.
Definition: regex4.h:36
+ 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 150 of file regex.c.

151{
152 struct RegexColor *rcol = mutt_mem_calloc(1, sizeof(*rcol));
153
154 return rcol;
155}
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ regex_colors_cleanup()

void regex_colors_cleanup ( void  )

Clear the Regex colours.

Definition at line 90 of file regex.c.

91{
92 color_debug(LL_DEBUG5, "clean up regex\n");
108}
struct RegexColorList IndexCollapsedList
List of colours applied to a collapsed thread in the index.
Definition: regex.c:51
struct RegexColorList IndexFlagsList
List of colours applied to the flags in the index.
Definition: regex.c:53
struct RegexColorList IndexAuthorList
List of colours applied to the author in the index.
Definition: regex.c:50
struct RegexColorList IndexSubjectList
List of colours applied to the subject in the index.
Definition: regex.c:58
struct RegexColorList IndexLabelList
List of colours applied to the label in the index.
Definition: regex.c:54
struct RegexColorList StatusList
List of colours applied to the status bar.
Definition: regex.c:61
struct RegexColorList IndexList
List of default colours applied to the index.
Definition: regex.c:55
struct RegexColorList IndexTagList
List of colours applied to tags in the index.
Definition: regex.c:59
struct RegexColorList BodyList
List of colours applied to the email body.
Definition: regex.c:48
struct RegexColorList IndexTagsList
List of colours applied to the tags in the index.
Definition: regex.c:60
struct RegexColorList HeaderList
List of colours applied to the email headers.
Definition: regex.c:49
struct RegexColorList AttachList
List of colours applied to the attachment headers.
Definition: regex.c:47
struct RegexColorList IndexDateList
List of colours applied to the date in the index.
Definition: regex.c:52
struct RegexColorList IndexNumberList
List of colours applied to the message number in the index.
Definition: regex.c:56
void regex_color_list_clear(struct RegexColorList *rcl)
Free the contents of a RegexColorList.
Definition: regex.c:165
struct RegexColorList IndexSizeList
List of colours applied to the size in the index.
Definition: regex.c:57
static int color_debug(enum LogLevel level, const char *format,...)
Definition: debug.h:53
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
+ 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 183 of file regex.c.

184{
185 switch (cid)
186 {
188 return &AttachList;
189 case MT_COLOR_BODY:
190 return &BodyList;
191 case MT_COLOR_HEADER:
192 return &HeaderList;
193 case MT_COLOR_INDEX:
194 return &IndexList;
196 return &IndexAuthorList;
198 return &IndexCollapsedList;
200 return &IndexDateList;
202 return &IndexFlagsList;
204 return &IndexLabelList;
206 return &IndexNumberList;
208 return &IndexSizeList;
210 return &IndexSubjectList;
212 return &IndexTagList;
214 return &IndexTagsList;
215 case MT_COLOR_STATUS:
216 return &StatusList;
217 default:
218 return NULL;
219 }
220}
@ MT_COLOR_INDEX_AUTHOR
Index: author field.
Definition: color.h:83
@ MT_COLOR_HEADER
Message headers (takes a pattern)
Definition: color.h:52
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition: color.h:74
@ MT_COLOR_INDEX_SIZE
Index: size field.
Definition: color.h:89
@ MT_COLOR_INDEX_TAGS
Index: tags field (g, J)
Definition: color.h:92
@ MT_COLOR_INDEX_SUBJECT
Index: subject field.
Definition: color.h:90
@ MT_COLOR_BODY
Pager: highlight body of message (takes a pattern)
Definition: color.h:43
@ MT_COLOR_INDEX_DATE
Index: date field.
Definition: color.h:85
@ MT_COLOR_INDEX_TAG
Index: tag field (G)
Definition: color.h:91
@ MT_COLOR_ATTACH_HEADERS
MIME attachment test (takes a pattern)
Definition: color.h:42
@ MT_COLOR_INDEX_LABEL
Index: label field.
Definition: color.h:87
@ MT_COLOR_INDEX
Index: default colour.
Definition: color.h:82
@ MT_COLOR_INDEX_NUMBER
Index: index number.
Definition: color.h:88
@ MT_COLOR_INDEX_FLAGS
Index: flags field.
Definition: color.h:86
@ MT_COLOR_INDEX_COLLAPSED
Index: number of messages in collapsed thread.
Definition: color.h:84
+ Here is the caller graph for this function:

◆ regex_colors_init()

void regex_colors_init ( void  )

Initialise the Regex colours.

Definition at line 67 of file regex.c.

+ Here is the call graph for this function:
+ Here is the caller 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 165 of file regex.c.

166{
167 if (!rcl)
168 return;
169
170 struct RegexColor *np = NULL, *tmp = NULL;
171 STAILQ_FOREACH_SAFE(np, rcl, entries, tmp)
172 {
173 STAILQ_REMOVE(rcl, np, RegexColor, entries);
174 regex_color_free(rcl, &np);
175 }
176}
void regex_color_free(struct RegexColorList *list, struct RegexColor **ptr)
Free a Regex colour.
Definition: regex.c:135
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:362
+ 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_QUOTED
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 322 of file regex.c.

325{
326 if (cid == MT_COLOR_STATUS)
327 return false;
328
329 struct RegexColorList *rcl = regex_colors_get_list(cid);
330 if (!rcl)
331 return false;
332
333 bool sensitive = false;
334 bool is_index = false;
335 switch (cid)
336 {
338 case MT_COLOR_BODY:
339 sensitive = true;
340 is_index = false;
341 break;
342 case MT_COLOR_HEADER:
343 sensitive = false;
344 is_index = false;
345 break;
346 case MT_COLOR_INDEX:
357 sensitive = true;
358 is_index = true;
359 break;
360 default:
361 return false;
362 }
363
364 *rc = add_pattern(rcl, pat, sensitive, ac, err, is_index, 0);
365
366 struct Buffer *buf = buf_pool_get();
367 get_colorid_name(cid, buf);
368 color_debug(LL_DEBUG5, "NT_COLOR_SET: %s\n", buf->data);
369 buf_pool_release(&buf);
370
371 if (!is_index) // else it will be logged in add_pattern()
372 {
373 struct EventColor ev_c = { cid, NULL };
375 }
376
377 return true;
378}
void get_colorid_name(unsigned int cid, struct Buffer *buf)
Get the name of a color id.
Definition: command.c:125
struct Notify * ColorsNotify
Notifications: ColorId, EventColor.
Definition: notify.c:35
struct RegexColorList * regex_colors_get_list(enum ColorId cid)
Return the RegexColorList for a colour id.
Definition: regex.c:183
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:236
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
@ NT_COLOR_SET
Color has been set.
Definition: notify2.h:41
@ NT_COLOR
Colour has changed, NotifyColor, EventColor.
Definition: notify_type.h:41
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
String manipulation buffer.
Definition: buffer.h:34
char * data
Pointer to data.
Definition: buffer.h:35
An Event that happened to a Colour.
Definition: notify2.h:53
enum ColorId cid
Colour ID that has changed.
Definition: notify2.h:54
+ 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_QUOTED
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 389 of file regex.c.

391{
392 if (cid != MT_COLOR_STATUS)
393 return MUTT_CMD_ERROR;
394
395 int rc = add_pattern(&StatusList, pat, true, 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->data);
402 buf_pool_release(&buf);
403
404 struct EventColor ev_c = { cid, NULL };
406
407 return rc;
408}
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:39
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:37
+ 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,
bool  uncolor 
)

Parse a Regex 'uncolor' command.

Parameters
cidColour Id, e.g. MT_COLOR_STATUS
patPattern to remove (NULL to remove all)
uncolortrue if 'uncolor', false if 'unmono'
Return values
trueIf colours were unset

Definition at line 417 of file regex.c.

418{
419 struct RegexColorList *cl = regex_colors_get_list(cid);
420 if (!cl)
421 return false;
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(cl, &np);
456 break;
457 }
458 prev = np;
459 }
460
461 return rc;
462}
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
@ LL_NOTIFY
Log of notifications.
Definition: logging2.h:48
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
@ NT_COLOR_RESET
Color has been reset/removed.
Definition: notify2.h:42
#define STAILQ_REMOVE_HEAD(head, field)
Definition: queue.h:422
#define STAILQ_REMOVE_AFTER(head, elm, field)
Definition: queue.h:416
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_EMPTY(head)
Definition: queue.h:348
+ Here is the call graph for this function:
+ Here is the caller graph for this function: