NeoMutt  2024-03-23-147-g885fbc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pattern.c File Reference

Match patterns to emails. More...

#include "config.h"
#include <stddef.h>
#include <stdbool.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 "mview.h"
#include "mx.h"
#include "protos.h"
#include "search_state.h"
#include <sys/stat.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, 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 86 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 94 of file pattern.c.

95{
96 buf_reset(buf);
97 buf_addch(buf, '"');
98 while (*str)
99 {
100 if ((*str == '\\') || (*str == '"'))
101 buf_addch(buf, '\\');
102 buf_addch(buf, *str++);
103 }
104 buf_addch(buf, '"');
105}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:75
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:240
+ 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 112 of file pattern.c.

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

Perform some Pattern matching for Alias.

Parameters
promptPrompt to show the user
mdataMenu data holding Aliases
menuCurrent menu
Return values
0Success
-1Failure

Definition at line 190 of file pattern.c.

191{
192 int rc = -1;
193 struct Progress *progress = NULL;
194 struct Buffer *buf = buf_pool_get();
195
196 buf_strcpy(buf, mdata->limit);
197 if (prompt)
198 {
199 if ((mw_get_field(prompt, buf, MUTT_COMP_CLEAR, HC_PATTERN, &CompletePatternOps, NULL) != 0) ||
200 buf_is_empty(buf))
201 {
202 buf_pool_release(&buf);
203 return -1;
204 }
205 }
206
207 mutt_message(_("Compiling search pattern..."));
208
209 bool match_all = false;
210 struct PatternList *pat = NULL;
211 char *simple = buf_strdup(buf);
212 if (simple)
213 {
215 const char *pbuf = buf->data;
216 while (*pbuf == ' ')
217 pbuf++;
218 match_all = mutt_str_equal(pbuf, "~A");
219
220 struct Buffer *err = buf_pool_get();
221 pat = mutt_pattern_comp(NULL, menu, buf->data, MUTT_PC_FULL_MSG, err);
222 if (!pat)
223 {
224 mutt_error("%s", buf_string(err));
225 buf_pool_release(&err);
226 goto bail;
227 }
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 int vcounter = 0;
238 struct AliasView *avp = NULL;
239 ARRAY_FOREACH(avp, &mdata->ava)
240 {
241 progress_update(progress, ARRAY_FOREACH_IDX, -1);
242
243 if (match_all ||
245 {
246 avp->is_visible = true;
247 vcounter++;
248 }
249 else
250 {
251 avp->is_visible = false;
252 }
253 }
254 progress_free(&progress);
255
256 FREE(&mdata->limit);
257 if (!match_all)
258 {
259 mdata->limit = simple;
260 simple = NULL;
261 }
262
263 if (menu)
264 {
265 menu->max = vcounter;
266 menu_set_index(menu, 0);
267 }
268
270
271 rc = 0;
272
273bail:
274 buf_pool_release(&buf);
275 FREE(&simple);
276 mutt_pattern_free(&pat);
277
278 return rc;
279}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
#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:290
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:570
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:905
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:777
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:1160
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:274
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_message(...)
Definition: logging2.h:91
@ HC_PATTERN
Patterns.
Definition: lib.h:55
#define FREE(x)
Definition: memory.h:45
MenuRedrawFlags menu_set_index(struct Menu *menu, int index)
Set the current selection in the Menu.
Definition: menu.c:174
#define _(a)
Definition: message.h:28
#define MUTT_COMP_CLEAR
Clear input if printable character is pressed.
Definition: mutt.h:57
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:74
const struct CompleteOps CompletePatternOps
Auto-Completion of Patterns.
Definition: complete.c:82
#define MUTT_PC_FULL_MSG
Enable body and header matching.
Definition: lib.h:69
#define MUTT_MATCH_FULL_ADDRESS
Match the full address.
Definition: lib.h:106
#define MUTT_ALIAS_SIMPLESEARCH
Definition: lib.h:63
void mutt_check_simple(struct Buffer *buf, const char *simple)
Convert a simple search into a real request.
Definition: pattern.c:112
@ MUTT_PROGRESS_READ
Progress tracks elements, according to $read_inc
Definition: lib.h:82
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:229
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
char * data
Pointer to data.
Definition: buffer.h:37
int max
Number of entries in the menu.
Definition: lib.h:81
+ 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 289 of file pattern.c.

290{
291 if (!mv || !mv->mailbox)
292 return -1;
293
294 struct Mailbox *m = mv->mailbox;
295
296 struct Buffer *err = NULL;
297 int rc = -1;
298 struct Progress *progress = NULL;
299 struct Buffer *buf = buf_pool_get();
300 bool interrupted = false;
301
302 buf_strcpy(buf, mv->pattern);
303 if (prompt || (op != MUTT_LIMIT))
304 {
305 if ((mw_get_field(prompt, buf, MUTT_COMP_CLEAR, HC_PATTERN, &CompletePatternOps, NULL) != 0) ||
306 buf_is_empty(buf))
307 {
308 buf_pool_release(&buf);
309 return -1;
310 }
311 }
312
313 mutt_message(_("Compiling search pattern..."));
314
315 char *simple = buf_strdup(buf);
316 const char *const c_simple_search = cs_subset_string(NeoMutt->sub, "simple_search");
317 mutt_check_simple(buf, NONULL(c_simple_search));
318 const char *pbuf = buf->data;
319 while (*pbuf == ' ')
320 pbuf++;
321 const bool match_all = mutt_str_equal(pbuf, "~A");
322
323 err = buf_pool_get();
324 struct PatternList *pat = mutt_pattern_comp(mv, mv->menu, buf->data, MUTT_PC_FULL_MSG, err);
325 if (!pat)
326 {
327 mutt_error("%s", buf_string(err));
328 goto bail;
329 }
330
331 if ((m->type == MUTT_IMAP) && (!imap_search(m, pat)))
332 goto bail;
333
334 progress = progress_new(MUTT_PROGRESS_READ, (op == MUTT_LIMIT) ? m->msg_count : m->vcount);
335 progress_set_message(progress, _("Executing command on matching messages..."));
336
337 if (op == MUTT_LIMIT)
338 {
339 m->vcount = 0;
340 mv->vsize = 0;
341 mv->collapsed = false;
342 int padding = mx_msg_padding_size(m);
343
344 for (int i = 0; i < m->msg_count; i++)
345 {
346 struct Email *e = m->emails[i];
347 if (!e)
348 break;
349
350 if (SigInt)
351 {
352 interrupted = true;
353 SigInt = false;
354 break;
355 }
356 progress_update(progress, i, -1);
357 /* new limit pattern implicitly uncollapses all threads */
358 e->vnum = -1;
359 e->visible = false;
360 e->limit_visited = true;
361 e->collapsed = false;
362 e->num_hidden = 0;
363
364 if (match_all ||
366 {
367 e->vnum = m->vcount;
368 e->visible = true;
369 m->v2r[m->vcount] = i;
370 m->vcount++;
371 struct Body *b = e->body;
372 mv->vsize += b->length + b->offset - b->hdr_offset + padding;
373 }
374 }
375 }
376 else
377 {
378 for (int i = 0; i < m->vcount; i++)
379 {
380 struct Email *e = mutt_get_virt_email(m, i);
381 if (!e)
382 continue;
383
384 if (SigInt)
385 {
386 interrupted = true;
387 SigInt = false;
388 break;
389 }
390 progress_update(progress, i, -1);
392 {
393 switch (op)
394 {
395 case MUTT_UNDELETE:
396 mutt_set_flag(m, e, MUTT_PURGE, false, true);
398
399 case MUTT_DELETE:
400 mutt_set_flag(m, e, MUTT_DELETE, (op == MUTT_DELETE), true);
401 break;
402 case MUTT_TAG:
403 case MUTT_UNTAG:
404 mutt_set_flag(m, e, MUTT_TAG, (op == MUTT_TAG), true);
405 break;
406 }
407 }
408 }
409 }
410 progress_free(&progress);
411
413
414 if (op == MUTT_LIMIT)
415 {
416 /* drop previous limit pattern */
417 FREE(&mv->pattern);
419
420 if (m->msg_count && !m->vcount)
421 mutt_error(_("No messages matched criteria"));
422
423 /* record new limit pattern, unless match all */
424 if (!match_all)
425 {
426 mv->pattern = simple;
427 simple = NULL; /* don't clobber it */
428 mv->limit_pattern = mutt_pattern_comp(mv, mv->menu, buf->data, MUTT_PC_FULL_MSG, err);
429 }
430 }
431
432 if (interrupted)
433 mutt_error(_("Search interrupted"));
434
435 rc = 0;
436
437bail:
438 buf_pool_release(&buf);
439 buf_pool_release(&err);
440 FREE(&simple);
441 mutt_pattern_free(&pat);
442
443 return rc;
444}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:292
@ MUTT_IMAP
'IMAP' Mailbox type
Definition: mailbox.h:50
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:1133
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:57
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:111
@ MUTT_UNDELETE
Messages to be un-deleted.
Definition: mutt.h:76
@ MUTT_LIMIT
Messages in limited view.
Definition: mutt.h:82
@ MUTT_UNTAG
Messages to be un-tagged.
Definition: mutt.h:81
@ MUTT_PURGE
Messages to be purged (bypass trash)
Definition: mutt.h:77
@ MUTT_TAG
Tagged messages.
Definition: mutt.h:80
@ MUTT_DELETE
Messages to be deleted.
Definition: mutt.h:75
struct Email * mutt_get_virt_email(struct Mailbox *m, int vnum)
Get a virtual Email.
Definition: mview.c:418
int mx_msg_padding_size(struct Mailbox *m)
Bytes of padding between messages - Wrapper for MxOps::msg_padding_size()
Definition: mx.c:1503
volatile sig_atomic_t SigInt
true after SIGINT is received
Definition: signal.c:63
#define NONULL(x)
Definition: string2.h:37
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:80
The envelope/body of an email.
Definition: email.h:39
bool visible
Is this message part of the view?
Definition: email.h:124
bool limit_visited
Has the limit pattern been applied to this message?
Definition: email.h:125
bool collapsed
Is this message part of a collapsed thread?
Definition: email.h:123
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:126
int vnum
Virtual message number.
Definition: email.h:117
bool collapsed
Are all threads collapsed?
Definition: mview.h:49
struct Menu * menu
Needed for pattern compilation.
Definition: mview.h:47
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:79
int vcount
The number of virtual messages.
Definition: mailbox.h:99
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:98
int msg_count
Total number of messages.
Definition: mailbox.h:88
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
struct Email ** emails
Array of Emails.
Definition: mailbox.h:96
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
+ 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 456 of file pattern.c.

458{
459 struct Progress *progress = NULL;
460 int rc = -1;
461 struct Mailbox *m = mv ? mv->mailbox : NULL;
462 if (!m)
463 return -1;
464 bool pattern_changed = false;
465
466 if (buf_is_empty(state->string) || (flags & SEARCH_PROMPT))
467 {
468 if ((mw_get_field((state->reverse) ? _("Reverse search for: ") : _("Search for: "),
470 &CompletePatternOps, NULL) != 0) ||
471 buf_is_empty(state->string))
472 {
473 goto done;
474 }
475
476 /* compare the *expanded* version of the search pattern in case
477 * $simple_search has changed while we were searching */
478 struct Buffer *tmp = buf_pool_get();
479 buf_copy(tmp, state->string);
480 const char *const c_simple_search = cs_subset_string(NeoMutt->sub, "simple_search");
481 mutt_check_simple(tmp, NONULL(c_simple_search));
482 if (!buf_str_equal(tmp, state->string_expn))
483 {
484 mutt_pattern_free(&state->pattern);
485 buf_copy(state->string_expn, tmp);
486 buf_pool_release(&tmp);
487 }
488 }
489
490 if (!state->pattern)
491 {
492 mutt_message(_("Compiling search pattern..."));
493 mutt_pattern_free(&state->pattern);
494 struct Buffer *err = buf_pool_get();
495 state->pattern = mutt_pattern_comp(mv, menu, state->string_expn->data,
496 MUTT_PC_FULL_MSG, err);
497 pattern_changed = true;
498 if (!state->pattern)
499 {
500 mutt_error("%s", buf_string(err));
501 buf_free(&err);
502 buf_reset(state->string);
503 buf_reset(state->string_expn);
504 return -1;
505 }
506 buf_free(&err);
508 }
509
510 if (pattern_changed)
511 {
512 for (int i = 0; i < m->msg_count; i++)
513 m->emails[i]->searched = false;
514 if ((m->type == MUTT_IMAP) && (!imap_search(m, state->pattern)))
515 return -1;
516 }
517
518 int incr = state->reverse ? -1 : 1;
519 if (flags & SEARCH_OPPOSITE)
520 incr = -incr;
521
522 progress = progress_new(MUTT_PROGRESS_READ, m->vcount);
523 progress_set_message(progress, _("Searching..."));
524
525 const bool c_wrap_search = cs_subset_bool(NeoMutt->sub, "wrap_search");
526 for (int i = cur + incr, j = 0; j != m->vcount; j++)
527 {
528 const char *msg = NULL;
529 progress_update(progress, j, -1);
530 if (i > m->vcount - 1)
531 {
532 i = 0;
533 if (c_wrap_search)
534 {
535 msg = _("Search wrapped to top");
536 }
537 else
538 {
539 mutt_message(_("Search hit bottom without finding match"));
540 goto done;
541 }
542 }
543 else if (i < 0)
544 {
545 i = m->vcount - 1;
546 if (c_wrap_search)
547 {
548 msg = _("Search wrapped to bottom");
549 }
550 else
551 {
552 mutt_message(_("Search hit top without finding match"));
553 goto done;
554 }
555 }
556
557 struct Email *e = mutt_get_virt_email(m, i);
558 if (!e)
559 goto done;
560
561 if (e->searched)
562 {
563 /* if we've already evaluated this message, use the cached value */
564 if (e->matched)
565 {
567 if (msg && *msg)
568 mutt_message("%s", msg);
569 rc = i;
570 goto done;
571 }
572 }
573 else
574 {
575 /* remember that we've already searched this message */
576 e->searched = true;
578 MUTT_MATCH_FULL_ADDRESS, m, e, NULL);
579 if (e->matched)
580 {
582 if (msg && *msg)
583 mutt_message("%s", msg);
584 rc = i;
585 goto done;
586 }
587 }
588
589 if (SigInt)
590 {
591 mutt_error(_("Search interrupted"));
592 SigInt = false;
593 goto done;
594 }
595
596 i += incr;
597 }
598
599 mutt_error(_("Not found"));
600done:
601 progress_free(&progress);
602 return rc;
603}
void buf_free(struct Buffer **ptr)
Deallocates a buffer.
Definition: buffer.c:318
bool buf_str_equal(const struct Buffer *a, const struct Buffer *b)
Return if two buffers are equal.
Definition: buffer.c:684
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition: buffer.c:600
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
#define SEARCH_OPPOSITE
Search in the opposite direction.
Definition: search_state.h:46
#define SEARCH_PROMPT
Ask for search input.
Definition: search_state.h:45
bool searched
Email has been searched.
Definition: email.h:108
bool matched
Search matches this Email.
Definition: email.h:105
struct Buffer * string
search string
Definition: search_state.h:38
struct Buffer * string_expn
expanded search string
Definition: search_state.h:39
bool reverse
search backwards
Definition: search_state.h:40
struct PatternList * pattern
compiled search pattern
Definition: search_state.h:37
+ 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 614 of file pattern.c.

616{
617 struct Progress *progress = NULL;
618 const struct AliasMenuData *mdata = menu->mdata;
619 const struct AliasViewArray *ava = &mdata->ava;
620 int rc = -1;
621 bool pattern_changed = false;
622
623 if (buf_is_empty(state->string) || flags & SEARCH_PROMPT)
624 {
625 if ((mw_get_field((flags & OP_SEARCH_REVERSE) ? _("Reverse search for: ") : _("Search for: "),
627 &CompletePatternOps, NULL) != 0) ||
628 buf_is_empty(state->string))
629 {
630 goto done;
631 }
632
633 /* compare the *expanded* version of the search pattern in case
634 * $simple_search has changed while we were searching */
635 struct Buffer *tmp = buf_pool_get();
636 buf_copy(tmp, state->string);
638 if (!buf_str_equal(tmp, state->string_expn))
639 {
640 mutt_pattern_free(&state->pattern);
641 buf_copy(state->string_expn, tmp);
642 buf_pool_release(&tmp);
643 }
644 }
645
646 if (!state->pattern)
647 {
648 mutt_message(_("Compiling search pattern..."));
649 struct Buffer *err = buf_pool_get();
650 state->pattern = mutt_pattern_comp(NULL, menu, state->string_expn->data,
651 MUTT_PC_FULL_MSG, err);
652 pattern_changed = true;
653 if (!state->pattern)
654 {
655 mutt_error("%s", buf_string(err));
656 buf_free(&err);
657 buf_reset(state->string);
658 buf_reset(state->string_expn);
659 return -1;
660 }
661 buf_free(&err);
663 }
664
665 if (pattern_changed)
666 {
667 struct AliasView *av = NULL;
668 ARRAY_FOREACH(av, ava)
669 {
670 av->is_searched = false;
671 }
672 }
673
674 int incr = state->reverse ? -1 : 1;
675 if (flags & SEARCH_OPPOSITE)
676 incr = -incr;
677
679 progress_set_message(progress, _("Searching..."));
680
681 const bool c_wrap_search = cs_subset_bool(NeoMutt->sub, "wrap_search");
682 for (int i = cur + incr, j = 0; j != ARRAY_SIZE(ava); j++)
683 {
684 const char *msg = NULL;
685 progress_update(progress, j, -1);
686 if (i > ARRAY_SIZE(ava) - 1)
687 {
688 i = 0;
689 if (c_wrap_search)
690 {
691 msg = _("Search wrapped to top");
692 }
693 else
694 {
695 mutt_message(_("Search hit bottom without finding match"));
696 goto done;
697 }
698 }
699 else if (i < 0)
700 {
701 i = ARRAY_SIZE(ava) - 1;
702 if (c_wrap_search)
703 {
704 msg = _("Search wrapped to bottom");
705 }
706 else
707 {
708 mutt_message(_("Search hit top without finding match"));
709 goto done;
710 }
711 }
712
713 struct AliasView *av = ARRAY_GET(ava, i);
714 if (av->is_searched)
715 {
716 /* if we've already evaluated this message, use the cached value */
717 if (av->is_matched)
718 {
720 if (msg && *msg)
721 mutt_message("%s", msg);
722 rc = i;
723 goto done;
724 }
725 }
726 else
727 {
728 /* remember that we've already searched this message */
729 av->is_searched = true;
731 MUTT_MATCH_FULL_ADDRESS, av, NULL);
732 if (av->is_matched)
733 {
735 if (msg && *msg)
736 mutt_message("%s", msg);
737 rc = i;
738 goto done;
739 }
740 }
741
742 if (SigInt)
743 {
744 mutt_error(_("Search interrupted"));
745 SigInt = false;
746 goto done;
747 }
748
749 i += incr;
750 }
751
752 mutt_error(_("Not found"));
753done:
754 progress_free(&progress);
755 return rc;
756}
#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:147
+ 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
Definition: private.h:121
#define RANGE_ABS_RX
Definition: private.h:117
#define RANGE_LT_RX
Definition: private.h:120
#define RANGE_BARE_RX
Definition: private.h:124
#define RANGE_REL_RX
Definition: private.h:113
@ RANGE_K_REL
Relative range.
Definition: private.h:101
@ RANGE_K_ABS
Absolute range.
Definition: private.h:102
@ RANGE_K_LT
Less-than range.
Definition: private.h:103
@ RANGE_K_BARE
Single symbol.
Definition: private.h:105
@ RANGE_K_GT
Greater-than range.
Definition: private.h:104

Set of Regexes for various range types.

This array, will also contain the compiled regexes.

Definition at line 65 of file pattern.c.