NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pattern.c File Reference

Match patterns to emails. More...

#include "config.h"
#include <stdbool.h>
#include <stddef.h>
#include "private.h"
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "alias/gui.h"
#include "alias/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "lib.h"
#include "editor/lib.h"
#include "history/lib.h"
#include "imap/lib.h"
#include "menu/lib.h"
#include "progress/lib.h"
#include "mutt_logging.h"
#include "mx.h"
#include "search_state.h"
+ Include dependency graph for pattern.c:

Go to the source code of this file.

Typedefs

typedef bool(* eat_arg_t) (struct Pattern *pat, PatternCompFlags flags, struct Buffer *s, struct Buffer *err)
 

Functions

static void quote_simple (const char *str, struct Buffer *buf)
 Apply simple quoting to a string.
 
void mutt_check_simple (struct Buffer *buf, const char *simple)
 Convert a simple search into a real request.
 
int mutt_pattern_alias_func (char *prompt, struct AliasMenuData *mdata, enum PatternAlias action, struct Menu *menu)
 Perform some Pattern matching for Alias.
 
int mutt_pattern_func (struct MailboxView *mv, int op, char *prompt)
 Perform some Pattern matching.
 
int mutt_search_command (struct MailboxView *mv, struct Menu *menu, int cur, struct SearchState *state, SearchFlags flags)
 Perform a search.
 
int mutt_search_alias_command (struct Menu *menu, int cur, struct SearchState *state, SearchFlags flags)
 Perform a search.
 

Variables

struct RangeRegex RangeRegexes []
 Set of Regexes for various range types.
 

Detailed Description

Match patterns to emails.

Authors
  • Pietro Cerutti
  • R Primus
  • Romeu Vieira
  • Richard Russon
  • Dennis Schön

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

Typedef Documentation

◆ eat_arg_t

typedef bool(* eat_arg_t) (struct Pattern *pat, PatternCompFlags flags, struct Buffer *s, struct Buffer *err)

Definition at line 81 of file pattern.c.

Function Documentation

◆ quote_simple()

static void quote_simple ( const char * str,
struct Buffer * buf )
static

Apply simple quoting to a string.

Parameters
strString to quote
bufBuffer for the result

Definition at line 89 of file pattern.c.

90{
91 buf_reset(buf);
92 buf_addch(buf, '"');
93 while (*str)
94 {
95 if ((*str == '\\') || (*str == '"'))
96 buf_addch(buf, '\\');
97 buf_addch(buf, *str++);
98 }
99 buf_addch(buf, '"');
100}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_check_simple()

void mutt_check_simple ( struct Buffer * buf,
const char * simple )

Convert a simple search into a real request.

Parameters
bufBuffer for the result
simpleSearch string to convert

Definition at line 107 of file pattern.c.

108{
109 bool do_simple = true;
110
111 for (const char *p = buf_string(buf); p && (p[0] != '\0'); p++)
112 {
113 if ((p[0] == '\\') && (p[1] != '\0'))
114 {
115 p++;
116 }
117 else if ((p[0] == '~') || (p[0] == '=') || (p[0] == '%'))
118 {
119 do_simple = false;
120 break;
121 }
122 }
123
124 /* XXX - is mutt_istr_cmp() right here, or should we use locale's
125 * equivalences? */
126
127 if (do_simple) /* yup, so spoof a real request */
128 {
129 /* convert old tokens into the new format */
130 if (mutt_istr_equal("all", buf_string(buf)) || mutt_str_equal("^", buf_string(buf)) ||
131 mutt_str_equal(".", buf_string(buf))) /* ~A is more efficient */
132 {
133 buf_strcpy(buf, "~A");
134 }
135 else if (mutt_istr_equal("del", buf_string(buf)))
136 {
137 buf_strcpy(buf, "~D");
138 }
139 else if (mutt_istr_equal("flag", buf_string(buf)))
140 {
141 buf_strcpy(buf, "~F");
142 }
143 else if (mutt_istr_equal("new", buf_string(buf)))
144 {
145 buf_strcpy(buf, "~N");
146 }
147 else if (mutt_istr_equal("old", buf_string(buf)))
148 {
149 buf_strcpy(buf, "~O");
150 }
151 else if (mutt_istr_equal("repl", buf_string(buf)))
152 {
153 buf_strcpy(buf, "~Q");
154 }
155 else if (mutt_istr_equal("read", buf_string(buf)))
156 {
157 buf_strcpy(buf, "~R");
158 }
159 else if (mutt_istr_equal("tag", buf_string(buf)))
160 {
161 buf_strcpy(buf, "~T");
162 }
163 else if (mutt_istr_equal("unread", buf_string(buf)))
164 {
165 buf_strcpy(buf, "~U");
166 }
167 else
168 {
169 struct Buffer *tmp = buf_pool_get();
170 quote_simple(buf_string(buf), tmp);
171 mutt_file_expand_fmt(buf, simple, buf_string(tmp));
172 buf_pool_release(&tmp);
173 }
174 }
175}
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 mutt_file_expand_fmt(struct Buffer *dest, const char *fmt, const char *src)
Replace s in a string with a filename.
Definition file.c:1366
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:677
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:665
static void quote_simple(const char *str, struct Buffer *buf)
Apply simple quoting to a string.
Definition pattern.c:89
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
String manipulation buffer.
Definition buffer.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_pattern_alias_func()

int mutt_pattern_alias_func ( char * prompt,
struct AliasMenuData * mdata,
enum PatternAlias action,
struct Menu * menu )

Perform some Pattern matching for Alias.

Parameters
promptPrompt to show the user
mdataMenu data holding Aliases
actionWhat to do with the results, e.g. PAA_TAG
menuCurrent menu
Return values
0Success
-1Failure

Definition at line 186 of file pattern.c.

188{
189 int rc = -1;
190 struct Progress *progress = NULL;
191 struct Buffer *buf = buf_pool_get();
192
193 buf_strcpy(buf, mdata->limit);
194 if (prompt)
195 {
196 if ((mw_get_field(prompt, buf, MUTT_COMP_CLEAR, HC_PATTERN, &CompletePatternOps, NULL) != 0) ||
197 buf_is_empty(buf))
198 {
199 buf_pool_release(&buf);
200 return -1;
201 }
202 }
203
204 mutt_message(_("Compiling search pattern..."));
205
206 /* Compile the pattern string; "~A" means match-all. If the simple string
207 * was expanded into a pattern, compile it for evaluation. */
208 bool match_all = false;
209 struct PatternList *pat = NULL;
210 char *simple = buf_strdup(buf);
211 if (simple)
212 {
214 const char *pbuf = buf->data;
215 while (*pbuf == ' ')
216 pbuf++;
217 match_all = mutt_str_equal(pbuf, "~A");
218
219 struct Buffer *err = buf_pool_get();
220 pat = mutt_pattern_comp(NULL, buf->data, MUTT_PC_FULL_MSG, err);
221 if (!pat)
222 {
223 mutt_error("%s", buf_string(err));
224 buf_pool_release(&err);
225 goto bail;
226 }
227 buf_pool_release(&err);
228 }
229 else
230 {
231 match_all = true;
232 }
233
234 progress = progress_new(MUTT_PROGRESS_READ, ARRAY_SIZE(&mdata->ava));
235 progress_set_message(progress, _("Executing command on matching messages..."));
236
237 /* Apply the action (tag, untag, or filter visibility) to each alias
238 * that matches the compiled pattern */
239 int vcounter = 0;
240 struct AliasView *avp = NULL;
241 ARRAY_FOREACH(avp, &mdata->ava)
242 {
243 progress_update(progress, ARRAY_FOREACH_IDX_avp, -1);
244
245 if (match_all ||
247 {
248 switch (action)
249 {
250 case PAA_TAG:
251 avp->is_tagged = true;
252 break;
253 case PAA_UNTAG:
254 avp->is_tagged = false;
255 break;
256 case PAA_VISIBLE:
257 avp->is_visible = true;
258 vcounter++;
259 break;
260 }
261 }
262 else
263 {
264 switch (action)
265 {
266 case PAA_TAG:
267 case PAA_UNTAG:
268 // Do nothing
269 break;
270 case PAA_VISIBLE:
271 avp->is_visible = false;
272 break;
273 }
274 }
275 }
276 progress_free(&progress);
277
278 FREE(&mdata->limit);
279 if (!match_all)
280 {
281 mdata->limit = simple;
282 simple = NULL;
283 }
284
285 if (menu && (action == PAA_VISIBLE))
286 {
287 menu->max = vcounter;
288 menu_set_index(menu, 0);
289 }
290
292
293 rc = 0;
294
295bail:
296 buf_pool_release(&buf);
297 FREE(&simple);
298 mutt_pattern_free(&pat);
299
300 return rc;
301}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition buffer.c:291
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
struct PatternList * mutt_pattern_comp(struct MailboxView *mv, const char *s, PatternCompFlags flags, struct Buffer *err)
Create a Pattern.
Definition compile.c:954
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition compile.c:826
#define MUTT_COMP_CLEAR
Clear input if printable character is pressed.
Definition wdata.h:43
int mw_get_field(const char *prompt, struct Buffer *buf, CompletionFlags complete, enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
Ask the user for a string -.
Definition window.c:463
#define mutt_error(...)
Definition logging2.h:94
#define mutt_message(...)
Definition logging2.h:93
@ HC_PATTERN
Patterns.
Definition lib.h:59
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
MenuRedrawFlags menu_set_index(struct Menu *menu, int index)
Set the current selection in the Menu.
Definition menu.c:178
#define _(a)
Definition message.h:28
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
const struct CompleteOps CompletePatternOps
Auto-Completion of Patterns.
Definition complete.c:98
bool mutt_pattern_alias_exec(struct Pattern *pat, PatternExecFlags flags, struct AliasView *av, struct PatternCache *cache)
Match a pattern against an alias.
Definition exec.c:1178
#define MUTT_PC_FULL_MSG
Enable body and header matching.
Definition lib.h:70
@ PAA_VISIBLE
Set AliasView.is_visible and hide the rest.
Definition lib.h:191
@ PAA_TAG
Set AliasView.is_tagged, but don't touch the others.
Definition lib.h:189
@ PAA_UNTAG
Unset AliasView.is_tagged, but don't touch the others.
Definition lib.h:190
#define MUTT_MATCH_FULL_ADDRESS
Match the full address.
Definition lib.h:107
#define MUTT_ALIAS_SIMPLESEARCH
Simple search pattern for aliases (from/to/cc fields)
Definition lib.h:64
void mutt_check_simple(struct Buffer *buf, const char *simple)
Convert a simple search into a real request.
Definition pattern.c:107
@ MUTT_PROGRESS_READ
Progress tracks elements, according to $read_inc
Definition lib.h:84
struct Progress * progress_new(enum ProgressType type, size_t size)
Create a new Progress Bar.
Definition progress.c:139
void progress_free(struct Progress **ptr)
Free a Progress Bar.
Definition progress.c:110
void progress_set_message(struct Progress *progress, const char *fmt,...) __attribute__((__format__(__printf__
bool progress_update(struct Progress *progress, size_t pos, int percent)
Update the state of the progress bar.
Definition progress.c:80
#define SLIST_FIRST(head)
Definition queue.h:227
char * limit
Limit being used.
Definition gui.h:60
struct AliasViewArray ava
All Aliases/Queries.
Definition gui.h:55
GUI data wrapping an Alias.
Definition gui.h:38
bool is_visible
Is visible?
Definition gui.h:45
bool is_tagged
Is it tagged?
Definition gui.h:43
char * data
Pointer to data.
Definition buffer.h:37
int max
Number of entries in the menu.
Definition lib.h:82
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_pattern_func()

int mutt_pattern_func ( struct MailboxView * mv,
int op,
char * prompt )

Perform some Pattern matching.

Parameters
mvMailbox View
opOperation to perform, e.g. MUTT_LIMIT
promptPrompt to show the user
Return values
0Success
-1Failure

Definition at line 311 of file pattern.c.

312{
313 if (!mv || !mv->mailbox)
314 return -1;
315
316 struct Mailbox *m = mv->mailbox;
317
318 struct Buffer *err = NULL;
319 int rc = -1;
320 struct Progress *progress = NULL;
321 struct Buffer *buf = buf_pool_get();
322 bool interrupted = false;
323
324 buf_strcpy(buf, mv->pattern);
325 if (prompt || (op != MUTT_LIMIT))
326 {
327 if ((mw_get_field(prompt, buf, MUTT_COMP_CLEAR, HC_PATTERN, &CompletePatternOps, NULL) != 0) ||
328 buf_is_empty(buf))
329 {
330 buf_pool_release(&buf);
331 return -1;
332 }
333 }
334
335 mutt_message(_("Compiling search pattern..."));
336
337 char *simple = buf_strdup(buf);
338 const char *const c_simple_search = cs_subset_string(NeoMutt->sub, "simple_search");
339 mutt_check_simple(buf, NONULL(c_simple_search));
340 const char *pbuf = buf->data;
341 while (*pbuf == ' ')
342 pbuf++;
343 const bool match_all = mutt_str_equal(pbuf, "~A");
344
345 err = buf_pool_get();
346 struct PatternList *pat = mutt_pattern_comp(mv, buf->data, MUTT_PC_FULL_MSG, err);
347 if (!pat)
348 {
349 mutt_error("%s", buf_string(err));
350 goto bail;
351 }
352
353 if ((m->type == MUTT_IMAP) && (!imap_search(m, pat)))
354 goto bail;
355
356 progress = progress_new(MUTT_PROGRESS_READ, (op == MUTT_LIMIT) ? m->msg_count : m->vcount);
357 progress_set_message(progress, _("Executing command on matching messages..."));
358
359 if (op == MUTT_LIMIT)
360 {
361 m->vcount = 0;
362 mv->vsize = 0;
363 mv->collapsed = false;
364 int padding = mx_msg_padding_size(m);
365
366 for (int i = 0; i < m->msg_count; i++)
367 {
368 struct Email *e = m->emails[i];
369 if (!e)
370 break;
371
372 if (SigInt)
373 {
374 interrupted = true;
375 SigInt = false;
376 break;
377 }
378 progress_update(progress, i, -1);
379 /* new limit pattern implicitly uncollapses all threads */
380 e->vnum = -1;
381 e->visible = false;
382 e->limit_visited = true;
383 e->collapsed = false;
384 e->num_hidden = 0;
385
386 if (match_all ||
388 {
389 e->vnum = m->vcount;
390 e->visible = true;
391 m->v2r[m->vcount] = i;
392 m->vcount++;
393 struct Body *b = e->body;
394 mv->vsize += b->length + b->offset - b->hdr_offset + padding;
395 }
396 }
397 }
398 else
399 {
400 for (int i = 0; i < m->vcount; i++)
401 {
402 struct Email *e = mutt_get_virt_email(m, i);
403 if (!e)
404 continue;
405
406 if (SigInt)
407 {
408 interrupted = true;
409 SigInt = false;
410 break;
411 }
412 progress_update(progress, i, -1);
414 {
415 switch (op)
416 {
417 case MUTT_UNDELETE:
418 mutt_set_flag(m, e, MUTT_PURGE, false, true);
420
421 case MUTT_DELETE:
422 mutt_set_flag(m, e, MUTT_DELETE, (op == MUTT_DELETE), true);
423 break;
424 case MUTT_TAG:
425 case MUTT_UNTAG:
426 mutt_set_flag(m, e, MUTT_TAG, (op == MUTT_TAG), true);
427 break;
428 }
429 }
430 }
431 }
432 progress_free(&progress);
433
435
436 if (op == MUTT_LIMIT)
437 {
438 /* drop previous limit pattern */
439 FREE(&mv->pattern);
441
442 if (m->msg_count && !m->vcount)
443 mutt_error(_("No messages matched criteria"));
444
445 /* record new limit pattern, unless match all */
446 if (!match_all)
447 {
448 mv->pattern = simple;
449 simple = NULL; /* don't clobber it */
451 }
452 }
453
454 if (interrupted)
455 mutt_error(_("Search interrupted"));
456
457 rc = 0;
458
459bail:
460 buf_pool_release(&buf);
461 buf_pool_release(&err);
462 FREE(&simple);
463 mutt_pattern_free(&pat);
464
465 return rc;
466}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
@ MUTT_IMAP
'IMAP' Mailbox type
Definition mailbox.h:49
void mutt_set_flag(struct Mailbox *m, struct Email *e, enum MessageType flag, bool bf, bool upd_mbox)
Set a flag on an email.
Definition flags.c:54
bool imap_search(struct Mailbox *m, const struct PatternList *pat)
Find messages in mailbox matching a pattern.
Definition search.c:227
#define FALLTHROUGH
Definition lib.h:117
@ MUTT_UNDELETE
Messages to be un-deleted.
Definition mutt.h:95
@ MUTT_LIMIT
Messages in limited view.
Definition mutt.h:101
@ MUTT_UNTAG
Messages to be un-tagged.
Definition mutt.h:100
@ MUTT_PURGE
Messages to be purged (bypass trash)
Definition mutt.h:96
@ MUTT_TAG
Tagged messages.
Definition mutt.h:99
@ MUTT_DELETE
Messages to be deleted.
Definition mutt.h:94
struct Email * mutt_get_virt_email(struct Mailbox *m, int vnum)
Get a virtual Email.
Definition mview.c:415
int mx_msg_padding_size(struct Mailbox *m)
Bytes of padding between messages - Wrapper for MxOps::msg_padding_size()
Definition mx.c:1507
bool mutt_pattern_exec(struct Pattern *pat, PatternExecFlags flags, struct Mailbox *m, struct Email *e, struct PatternCache *cache)
Match a pattern against an email header.
Definition exec.c:1151
volatile sig_atomic_t SigInt
true after SIGINT is received
Definition signal.c:68
#define NONULL(x)
Definition string2.h:44
The body of an email.
Definition body.h:36
LOFF_T offset
offset where the actual data begins
Definition body.h:52
LOFF_T length
length (in bytes) of attachment
Definition body.h:53
long hdr_offset
Offset in stream where the headers begin.
Definition body.h:81
The envelope/body of an email.
Definition email.h:39
bool visible
Is this message part of the view?
Definition email.h:121
bool limit_visited
Has the limit pattern been applied to this message?
Definition email.h:122
bool collapsed
Is this message part of a collapsed thread?
Definition email.h:120
struct Body * body
List of MIME parts.
Definition email.h:69
size_t num_hidden
Number of hidden messages in this view (only valid when collapsed is set)
Definition email.h:123
int vnum
Virtual message number.
Definition email.h:114
bool collapsed
Are all threads collapsed?
Definition mview.h:49
off_t vsize
Size (in bytes) of the messages shown.
Definition mview.h:41
struct PatternList * limit_pattern
Compiled limit pattern.
Definition mview.h:43
struct Mailbox * mailbox
Current Mailbox.
Definition mview.h:51
char * pattern
Limit pattern string.
Definition mview.h:42
A mailbox.
Definition mailbox.h:78
int vcount
The number of virtual messages.
Definition mailbox.h:98
int * v2r
Mapping from virtual to real msgno.
Definition mailbox.h:97
int msg_count
Total number of messages.
Definition mailbox.h:87
enum MailboxType type
Mailbox type.
Definition mailbox.h:101
struct Email ** emails
Array of Emails.
Definition mailbox.h:95
Container for Accounts, Notifications.
Definition neomutt.h:41
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:

◆ mutt_search_command()

int mutt_search_command ( struct MailboxView * mv,
struct Menu * menu,
int cur,
struct SearchState * state,
SearchFlags flags )

Perform a search.

Parameters
mvMailbox view to search through
menuCurrent Menu
curIndex number of current email
stateCurrent search state
flagsSearch flags, e.g. SEARCH_PROMPT
Return values
>=0Index of matching email
-1No match, or error

Definition at line 478 of file pattern.c.

480{
481 struct Progress *progress = NULL;
482 int rc = -1;
483 struct Mailbox *m = mv ? mv->mailbox : NULL;
484 if (!m)
485 return -1;
486 bool pattern_changed = false;
487
488 if (buf_is_empty(state->string) || (flags & SEARCH_PROMPT))
489 {
490 if ((mw_get_field((state->reverse) ? _("Reverse search for: ") : _("Search for: "),
492 &CompletePatternOps, NULL) != 0) ||
493 buf_is_empty(state->string))
494 {
495 goto done;
496 }
497
498 /* compare the *expanded* version of the search pattern in case
499 * $simple_search has changed while we were searching */
500 struct Buffer *tmp = buf_pool_get();
501 buf_copy(tmp, state->string);
502 const char *const c_simple_search = cs_subset_string(NeoMutt->sub, "simple_search");
503 mutt_check_simple(tmp, NONULL(c_simple_search));
504 if (!buf_str_equal(tmp, state->string_expn))
505 {
506 mutt_pattern_free(&state->pattern);
507 buf_copy(state->string_expn, tmp);
508 buf_pool_release(&tmp);
509 }
510 }
511
512 if (!state->pattern)
513 {
514 mutt_message(_("Compiling search pattern..."));
515 mutt_pattern_free(&state->pattern);
516 struct Buffer *err = buf_pool_get();
517 state->pattern = mutt_pattern_comp(mv, state->string_expn->data, MUTT_PC_FULL_MSG, err);
518 pattern_changed = true;
519 if (!state->pattern)
520 {
521 mutt_error("%s", buf_string(err));
522 buf_free(&err);
523 buf_reset(state->string);
524 buf_reset(state->string_expn);
525 return -1;
526 }
527 buf_free(&err);
529 }
530
531 if (pattern_changed)
532 {
533 for (int i = 0; i < m->msg_count; i++)
534 m->emails[i]->searched = false;
535 if ((m->type == MUTT_IMAP) && (!imap_search(m, state->pattern)))
536 return -1;
537 }
538
539 int incr = state->reverse ? -1 : 1;
540 if (flags & SEARCH_OPPOSITE)
541 incr = -incr;
542
543 progress = progress_new(MUTT_PROGRESS_READ, m->vcount);
544 progress_set_message(progress, _("Searching..."));
545
546 const bool c_wrap_search = cs_subset_bool(NeoMutt->sub, "wrap_search");
547 for (int i = cur + incr, j = 0; j != m->vcount; j++)
548 {
549 const char *msg = NULL;
550 progress_update(progress, j, -1);
551 if (i > m->vcount - 1)
552 {
553 i = 0;
554 if (c_wrap_search)
555 {
556 msg = _("Search wrapped to top");
557 }
558 else
559 {
560 mutt_message(_("Search hit bottom without finding match"));
561 goto done;
562 }
563 }
564 else if (i < 0)
565 {
566 i = m->vcount - 1;
567 if (c_wrap_search)
568 {
569 msg = _("Search wrapped to bottom");
570 }
571 else
572 {
573 mutt_message(_("Search hit top without finding match"));
574 goto done;
575 }
576 }
577
578 struct Email *e = mutt_get_virt_email(m, i);
579 if (!e)
580 goto done;
581
582 if (e->searched)
583 {
584 /* if we've already evaluated this message, use the cached value */
585 if (e->matched)
586 {
588 if (msg && *msg)
589 mutt_message("%s", msg);
590 rc = i;
591 goto done;
592 }
593 }
594 else
595 {
596 /* remember that we've already searched this message */
597 e->searched = true;
599 MUTT_MATCH_FULL_ADDRESS, m, e, NULL);
600 if (e->matched)
601 {
603 if (msg && *msg)
604 mutt_message("%s", msg);
605 rc = i;
606 goto done;
607 }
608 }
609
610 if (SigInt)
611 {
612 mutt_error(_("Search interrupted"));
613 SigInt = false;
614 goto done;
615 }
616
617 i += incr;
618 }
619
620 mutt_error(_("Not found"));
621done:
622 progress_free(&progress);
623 return rc;
624}
void buf_free(struct Buffer **ptr)
Deallocates a buffer.
Definition buffer.c:319
bool buf_str_equal(const struct Buffer *a, const struct Buffer *b)
Return if two buffers are equal.
Definition buffer.c:683
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition buffer.c:601
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
#define SEARCH_OPPOSITE
Search in the opposite direction.
#define SEARCH_PROMPT
Ask for search input.
bool searched
Email has been searched.
Definition email.h:105
bool matched
Search matches this Email.
Definition email.h:102
struct Buffer * string
search string
struct Buffer * string_expn
expanded search string
bool reverse
search backwards
struct PatternList * pattern
compiled search pattern
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_search_alias_command()

int mutt_search_alias_command ( struct Menu * menu,
int cur,
struct SearchState * state,
SearchFlags flags )

Perform a search.

Parameters
menuMenu to search through
curIndex number of current email
stateCurrent search state
flagsSearch flags, e.g. SEARCH_PROMPT
Return values
>=0Index of matching alias
-1No match, or error

Definition at line 635 of file pattern.c.

637{
638 struct Progress *progress = NULL;
639 const struct AliasMenuData *mdata = menu->mdata;
640 const struct AliasViewArray *ava = &mdata->ava;
641 int rc = -1;
642 bool pattern_changed = false;
643
644 if (buf_is_empty(state->string) || flags & SEARCH_PROMPT)
645 {
646 if ((mw_get_field(state->reverse ? _("Reverse search for: ") : _("Search for: "),
648 &CompletePatternOps, NULL) != 0) ||
649 buf_is_empty(state->string))
650 {
651 goto done;
652 }
653
654 /* compare the *expanded* version of the search pattern in case
655 * $simple_search has changed while we were searching */
656 struct Buffer *tmp = buf_pool_get();
657 buf_copy(tmp, state->string);
659 if (!buf_str_equal(tmp, state->string_expn))
660 {
661 mutt_pattern_free(&state->pattern);
662 buf_copy(state->string_expn, tmp);
663 buf_pool_release(&tmp);
664 }
665 }
666
667 if (!state->pattern)
668 {
669 mutt_message(_("Compiling search pattern..."));
670 struct Buffer *err = buf_pool_get();
671 state->pattern = mutt_pattern_comp(NULL, state->string_expn->data, MUTT_PC_FULL_MSG, err);
672 pattern_changed = true;
673 if (!state->pattern)
674 {
675 mutt_error("%s", buf_string(err));
676 buf_free(&err);
677 buf_reset(state->string);
678 buf_reset(state->string_expn);
679 return -1;
680 }
681 buf_free(&err);
683 }
684
685 if (pattern_changed)
686 {
687 struct AliasView *av = NULL;
688 ARRAY_FOREACH(av, ava)
689 {
690 av->is_searched = false;
691 }
692 }
693
694 int incr = state->reverse ? -1 : 1;
695 if (flags & SEARCH_OPPOSITE)
696 incr = -incr;
697
699 progress_set_message(progress, _("Searching..."));
700
701 const bool c_wrap_search = cs_subset_bool(NeoMutt->sub, "wrap_search");
702 for (int i = cur + incr, j = 0; j != ARRAY_SIZE(ava); j++)
703 {
704 const char *msg = NULL;
705 progress_update(progress, j, -1);
706 if (i > ARRAY_SIZE(ava) - 1)
707 {
708 i = 0;
709 if (c_wrap_search)
710 {
711 msg = _("Search wrapped to top");
712 }
713 else
714 {
715 mutt_message(_("Search hit bottom without finding match"));
716 goto done;
717 }
718 }
719 else if (i < 0)
720 {
721 i = ARRAY_SIZE(ava) - 1;
722 if (c_wrap_search)
723 {
724 msg = _("Search wrapped to bottom");
725 }
726 else
727 {
728 mutt_message(_("Search hit top without finding match"));
729 goto done;
730 }
731 }
732
733 struct AliasView *av = ARRAY_GET(ava, i);
734 if (av->is_searched)
735 {
736 /* if we've already evaluated this message, use the cached value */
737 if (av->is_matched)
738 {
740 if (msg && *msg)
741 mutt_message("%s", msg);
742 rc = i;
743 goto done;
744 }
745 }
746 else
747 {
748 /* remember that we've already searched this message */
749 av->is_searched = true;
751 MUTT_MATCH_FULL_ADDRESS, av, NULL);
752 if (av->is_matched)
753 {
755 if (msg && *msg)
756 mutt_message("%s", msg);
757 rc = i;
758 goto done;
759 }
760 }
761
762 if (SigInt)
763 {
764 mutt_error(_("Search interrupted"));
765 SigInt = false;
766 goto done;
767 }
768
769 i += incr;
770 }
771
772 mutt_error(_("Not found"));
773done:
774 progress_free(&progress);
775 return rc;
776}
#define ARRAY_GET(head, idx)
Return the element at index.
Definition array.h:109
AliasView array wrapper with Pattern information -.
Definition gui.h:54
struct Menu * menu
Menu.
Definition gui.h:58
bool is_matched
Search matches this Alias.
Definition gui.h:42
bool is_searched
Alias has been searched.
Definition gui.h:41
void * mdata
Private data.
Definition lib.h:149
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ RangeRegexes

struct RangeRegex RangeRegexes[]
Initial value:
= {
[RANGE_K_REL] = { RANGE_REL_RX, 1, 3, 0, { 0 } },
[RANGE_K_ABS] = { RANGE_ABS_RX, 1, 3, 0, { 0 } },
[RANGE_K_LT] = { RANGE_LT_RX, 1, 2, 0, { 0 } },
[RANGE_K_GT] = { RANGE_GT_RX, 2, 1, 0, { 0 } },
[RANGE_K_BARE] = { RANGE_BARE_RX, 1, 1, 0, { 0 } },
}
#define RANGE_GT_RX
Regex for greater-than range (e.g., ">50")
Definition private.h:117
#define RANGE_ABS_RX
Regex for absolute range (e.g., "1-5")
Definition private.h:112
#define RANGE_LT_RX
Regex for less-than range (e.g., "<100")
Definition private.h:115
#define RANGE_BARE_RX
Regex for bare number range.
Definition private.h:120
#define RANGE_REL_RX
Regex for relative range (e.g., "1,5" or "-3,.")
Definition private.h:107
@ RANGE_K_REL
Relative range.
Definition private.h:93
@ RANGE_K_ABS
Absolute range.
Definition private.h:94
@ RANGE_K_LT
Less-than range.
Definition private.h:95
@ RANGE_K_BARE
Single symbol.
Definition private.h:97
@ RANGE_K_GT
Greater-than range.
Definition private.h:96

Set of Regexes for various range types.

This array, will also contain the compiled regexes.

Definition at line 60 of file pattern.c.

60 {
61 // clang-format off
62 [RANGE_K_REL] = { RANGE_REL_RX, 1, 3, 0, { 0 } },
63 [RANGE_K_ABS] = { RANGE_ABS_RX, 1, 3, 0, { 0 } },
64 [RANGE_K_LT] = { RANGE_LT_RX, 1, 2, 0, { 0 } },
65 [RANGE_K_GT] = { RANGE_GT_RX, 2, 1, 0, { 0 } },
66 [RANGE_K_BARE] = { RANGE_BARE_RX, 1, 1, 0, { 0 } },
67 // clang-format on
68};