NeoMutt  2025-09-05-43-g177ed6
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
lib.h File Reference

Match patterns to emails. More...

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

Go to the source code of this file.

Data Structures

struct  Pattern
 A simple (non-regex) pattern. More...
 
struct  PatternCache
 Cache commonly-used patterns. More...
 

Macros

#define MUTT_ALIAS_SIMPLESEARCH   "~f %s | ~t %s | ~c %s"
 
#define MUTT_PC_NO_FLAGS   0
 No flags are set.
 
#define MUTT_PC_FULL_MSG   (1 << 0)
 Enable body and header matching.
 
#define MUTT_PC_PATTERN_DYNAMIC   (1 << 1)
 Enable runtime date range evaluation.
 
#define MUTT_PC_SEND_MODE_SEARCH   (1 << 2)
 Allow send-mode body searching.
 
#define MUTT_PAT_EXEC_NO_FLAGS   0
 No flags are set.
 
#define MUTT_MATCH_FULL_ADDRESS   (1 << 0)
 Match the full address.
 

Typedefs

typedef uint8_t PatternCompFlags
 Flags for mutt_pattern_comp(), e.g. MUTT_PC_FULL_MSG.
 
typedef uint8_t PatternExecFlags
 Flags for mutt_pattern_exec(), e.g. MUTT_MATCH_FULL_ADDRESS.
 

Enumerations

enum  PatternType {
  MUTT_PAT_AND = MUTT_MT_MAX , MUTT_PAT_OR , MUTT_PAT_THREAD , MUTT_PAT_PARENT ,
  MUTT_PAT_CHILDREN , MUTT_PAT_TO , MUTT_PAT_CC , MUTT_PAT_BCC ,
  MUTT_PAT_COLLAPSED , MUTT_PAT_SUBJECT , MUTT_PAT_FROM , MUTT_PAT_DATE ,
  MUTT_PAT_DATE_RECEIVED , MUTT_PAT_DUPLICATED , MUTT_PAT_UNREFERENCED , MUTT_PAT_BROKEN ,
  MUTT_PAT_ID , MUTT_PAT_ID_EXTERNAL , MUTT_PAT_BODY , MUTT_PAT_HEADER ,
  MUTT_PAT_HORMEL , MUTT_PAT_WHOLE_MSG , MUTT_PAT_SENDER , MUTT_PAT_MESSAGE ,
  MUTT_PAT_SCORE , MUTT_PAT_SIZE , MUTT_PAT_REFERENCE , MUTT_PAT_RECIPIENT ,
  MUTT_PAT_LIST , MUTT_PAT_SUBSCRIBED_LIST , MUTT_PAT_PERSONAL_RECIP , MUTT_PAT_PERSONAL_FROM ,
  MUTT_PAT_ADDRESS , MUTT_PAT_CRYPT_SIGN , MUTT_PAT_CRYPT_VERIFIED , MUTT_PAT_CRYPT_ENCRYPT ,
  MUTT_PAT_PGP_KEY , MUTT_PAT_XLABEL , MUTT_PAT_SERVERSEARCH , MUTT_PAT_DRIVER_TAGS ,
  MUTT_PAT_MIMEATTACH , MUTT_PAT_MIMETYPE , MUTT_PAT_NEWSGROUPS , MUTT_PAT_MAX
}
 Types of pattern to match. More...
 
enum  PatternAlias { PAA_TAG , PAA_UNTAG , PAA_VISIBLE }
 What to do with the matching Aliases. More...
 

Functions

 SLIST_HEAD (PatternList, Pattern)
 
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.
 
bool mutt_pattern_alias_exec (struct Pattern *pat, PatternExecFlags flags, struct AliasView *av, struct PatternCache *cache)
 Match a pattern against an alias.
 
struct PatternList * mutt_pattern_comp (struct MailboxView *mv, const char *s, PatternCompFlags flags, struct Buffer *err)
 Create a Pattern.
 
void mutt_check_simple (struct Buffer *s, const char *simple)
 Convert a simple search into a real request.
 
void mutt_pattern_free (struct PatternList **pat)
 Free a Pattern.
 
bool dlg_pattern (struct Buffer *buf)
 Show menu to select a Pattern -.
 
bool mutt_is_list_recipient (bool all_addr, struct Envelope *env)
 Matches known mailing lists.
 
bool mutt_is_subscribed_list_recipient (bool all_addr, struct Envelope *env)
 Matches subscribed mailing lists.
 
int mutt_pattern_func (struct MailboxView *mv, int op, char *prompt)
 Perform some Pattern matching.
 
int mutt_pattern_alias_func (char *prompt, struct AliasMenuData *mdata, enum PatternAlias action, struct Menu *menu)
 Perform some Pattern matching for Alias.
 
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

const struct CompleteOps CompletePatternOps
 Auto-Completion of Patterns.
 

Detailed Description

Match patterns to emails.

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

Macro Definition Documentation

◆ MUTT_ALIAS_SIMPLESEARCH

#define MUTT_ALIAS_SIMPLESEARCH   "~f %s | ~t %s | ~c %s"

Definition at line 63 of file lib.h.

◆ MUTT_PC_NO_FLAGS

#define MUTT_PC_NO_FLAGS   0

No flags are set.

Definition at line 68 of file lib.h.

◆ MUTT_PC_FULL_MSG

#define MUTT_PC_FULL_MSG   (1 << 0)

Enable body and header matching.

Definition at line 69 of file lib.h.

◆ MUTT_PC_PATTERN_DYNAMIC

#define MUTT_PC_PATTERN_DYNAMIC   (1 << 1)

Enable runtime date range evaluation.

Definition at line 70 of file lib.h.

◆ MUTT_PC_SEND_MODE_SEARCH

#define MUTT_PC_SEND_MODE_SEARCH   (1 << 2)

Allow send-mode body searching.

Definition at line 71 of file lib.h.

◆ MUTT_PAT_EXEC_NO_FLAGS

#define MUTT_PAT_EXEC_NO_FLAGS   0

No flags are set.

Definition at line 105 of file lib.h.

◆ MUTT_MATCH_FULL_ADDRESS

#define MUTT_MATCH_FULL_ADDRESS   (1 << 0)

Match the full address.

Definition at line 106 of file lib.h.

Typedef Documentation

◆ PatternCompFlags

typedef uint8_t PatternCompFlags

Flags for mutt_pattern_comp(), e.g. MUTT_PC_FULL_MSG.

Definition at line 67 of file lib.h.

◆ PatternExecFlags

typedef uint8_t PatternExecFlags

Flags for mutt_pattern_exec(), e.g. MUTT_MATCH_FULL_ADDRESS.

Definition at line 104 of file lib.h.

Enumeration Type Documentation

◆ PatternType

Types of pattern to match.

Note
This enum piggy-backs on top of MessageType
See also
mutt_pattern_comp(), mutt_pattern_exec()
Enumerator
MUTT_PAT_AND 

Both patterns must match.

MUTT_PAT_OR 

Either pattern can match.

MUTT_PAT_THREAD 

Pattern matches email thread.

MUTT_PAT_PARENT 

Pattern matches parent.

MUTT_PAT_CHILDREN 

Pattern matches a child email.

MUTT_PAT_TO 

Pattern matches 'To:' field.

MUTT_PAT_CC 

Pattern matches 'Cc:' field.

MUTT_PAT_BCC 

Pattern matches 'Bcc:' field.

MUTT_PAT_COLLAPSED 

Thread is collapsed.

MUTT_PAT_SUBJECT 

Pattern matches 'Subject:' field.

MUTT_PAT_FROM 

Pattern matches 'From:' field.

MUTT_PAT_DATE 

Pattern matches 'Date:' field.

MUTT_PAT_DATE_RECEIVED 

Pattern matches date received.

MUTT_PAT_DUPLICATED 

Duplicate message.

MUTT_PAT_UNREFERENCED 

Message is unreferenced in the thread.

MUTT_PAT_BROKEN 

Message is part of a broken thread.

MUTT_PAT_ID 

Pattern matches email's Message-Id.

MUTT_PAT_ID_EXTERNAL 

Message-Id is among results from an external query.

MUTT_PAT_BODY 

Pattern matches email's body.

MUTT_PAT_HEADER 

Pattern matches email's header.

MUTT_PAT_HORMEL 

Pattern matches email's spam score.

MUTT_PAT_WHOLE_MSG 

Pattern matches raw email text.

MUTT_PAT_SENDER 

Pattern matches sender.

MUTT_PAT_MESSAGE 

Pattern matches message number.

MUTT_PAT_SCORE 

Pattern matches email's score.

MUTT_PAT_SIZE 

Pattern matches email's size.

MUTT_PAT_REFERENCE 

Pattern matches 'References:' or 'In-Reply-To:' field.

MUTT_PAT_RECIPIENT 

User is a recipient of the email.

MUTT_PAT_LIST 

Email is on mailing list.

MUTT_PAT_SUBSCRIBED_LIST 

Email is on subscribed mailing list.

MUTT_PAT_PERSONAL_RECIP 

Email is addressed to the user.

MUTT_PAT_PERSONAL_FROM 

Email is from the user.

MUTT_PAT_ADDRESS 

Pattern matches any address field.

MUTT_PAT_CRYPT_SIGN 

Message is signed.

MUTT_PAT_CRYPT_VERIFIED 

Message is crypographically verified.

MUTT_PAT_CRYPT_ENCRYPT 

Message is encrypted.

MUTT_PAT_PGP_KEY 

Message has PGP key.

MUTT_PAT_XLABEL 

Pattern matches keyword/label.

MUTT_PAT_SERVERSEARCH 

Server-side pattern matches.

MUTT_PAT_DRIVER_TAGS 

Pattern matches message tags.

MUTT_PAT_MIMEATTACH 

Pattern matches number of attachments.

MUTT_PAT_MIMETYPE 

Pattern matches MIME type.

MUTT_PAT_NEWSGROUPS 

Pattern matches newsgroup.

MUTT_PAT_MAX 

Definition at line 135 of file lib.h.

136{
181};
@ MUTT_MT_MAX
Definition mutt.h:87
@ MUTT_PAT_HEADER
Pattern matches email's header.
Definition lib.h:156
@ MUTT_PAT_WHOLE_MSG
Pattern matches raw email text.
Definition lib.h:158
@ MUTT_PAT_BROKEN
Message is part of a broken thread.
Definition lib.h:152
@ MUTT_PAT_ID_EXTERNAL
Message-Id is among results from an external query.
Definition lib.h:154
@ MUTT_PAT_OR
Either pattern can match.
Definition lib.h:138
@ MUTT_PAT_CHILDREN
Pattern matches a child email.
Definition lib.h:141
@ MUTT_PAT_PARENT
Pattern matches parent.
Definition lib.h:140
@ MUTT_PAT_REFERENCE
Pattern matches 'References:' or 'In-Reply-To:' field.
Definition lib.h:163
@ MUTT_PAT_FROM
Pattern matches 'From:' field.
Definition lib.h:147
@ MUTT_PAT_DRIVER_TAGS
Pattern matches message tags.
Definition lib.h:176
@ MUTT_PAT_COLLAPSED
Thread is collapsed.
Definition lib.h:145
@ MUTT_PAT_CRYPT_VERIFIED
Message is crypographically verified.
Definition lib.h:171
@ MUTT_PAT_HORMEL
Pattern matches email's spam score.
Definition lib.h:157
@ MUTT_PAT_SUBJECT
Pattern matches 'Subject:' field.
Definition lib.h:146
@ MUTT_PAT_LIST
Email is on mailing list.
Definition lib.h:165
@ MUTT_PAT_NEWSGROUPS
Pattern matches newsgroup.
Definition lib.h:179
@ MUTT_PAT_PERSONAL_RECIP
Email is addressed to the user.
Definition lib.h:167
@ MUTT_PAT_CC
Pattern matches 'Cc:' field.
Definition lib.h:143
@ MUTT_PAT_SUBSCRIBED_LIST
Email is on subscribed mailing list.
Definition lib.h:166
@ MUTT_PAT_SERVERSEARCH
Server-side pattern matches.
Definition lib.h:175
@ MUTT_PAT_RECIPIENT
User is a recipient of the email.
Definition lib.h:164
@ MUTT_PAT_CRYPT_ENCRYPT
Message is encrypted.
Definition lib.h:172
@ MUTT_PAT_UNREFERENCED
Message is unreferenced in the thread.
Definition lib.h:151
@ MUTT_PAT_CRYPT_SIGN
Message is signed.
Definition lib.h:170
@ MUTT_PAT_MESSAGE
Pattern matches message number.
Definition lib.h:160
@ MUTT_PAT_AND
Both patterns must match.
Definition lib.h:137
@ MUTT_PAT_DATE
Pattern matches 'Date:' field.
Definition lib.h:148
@ MUTT_PAT_XLABEL
Pattern matches keyword/label.
Definition lib.h:174
@ MUTT_PAT_SCORE
Pattern matches email's score.
Definition lib.h:161
@ MUTT_PAT_MIMEATTACH
Pattern matches number of attachments.
Definition lib.h:177
@ MUTT_PAT_DUPLICATED
Duplicate message.
Definition lib.h:150
@ MUTT_PAT_PERSONAL_FROM
Email is from the user.
Definition lib.h:168
@ MUTT_PAT_TO
Pattern matches 'To:' field.
Definition lib.h:142
@ MUTT_PAT_BCC
Pattern matches 'Bcc:' field.
Definition lib.h:144
@ MUTT_PAT_SENDER
Pattern matches sender.
Definition lib.h:159
@ MUTT_PAT_DATE_RECEIVED
Pattern matches date received.
Definition lib.h:149
@ MUTT_PAT_ADDRESS
Pattern matches any address field.
Definition lib.h:169
@ MUTT_PAT_MAX
Definition lib.h:180
@ MUTT_PAT_MIMETYPE
Pattern matches MIME type.
Definition lib.h:178
@ MUTT_PAT_PGP_KEY
Message has PGP key.
Definition lib.h:173
@ MUTT_PAT_ID
Pattern matches email's Message-Id.
Definition lib.h:153
@ MUTT_PAT_THREAD
Pattern matches email thread.
Definition lib.h:139
@ MUTT_PAT_SIZE
Pattern matches email's size.
Definition lib.h:162
@ MUTT_PAT_BODY
Pattern matches email's body.
Definition lib.h:155

◆ PatternAlias

What to do with the matching Aliases.

Enumerator
PAA_TAG 

Set AliasView.is_tagged, but don't touch the others.

PAA_UNTAG 

Unset AliasView.is_tagged, but don't touch the others.

PAA_VISIBLE 

Set AliasView.is_visible and hide the rest.

Definition at line 186 of file lib.h.

187{
188 PAA_TAG,
189 PAA_UNTAG,
191};
@ PAA_VISIBLE
Set AliasView.is_visible and hide the rest.
Definition lib.h:190
@ PAA_TAG
Set AliasView.is_tagged, but don't touch the others.
Definition lib.h:188
@ PAA_UNTAG
Unset AliasView.is_tagged, but don't touch the others.
Definition lib.h:189

Function Documentation

◆ SLIST_HEAD()

SLIST_HEAD ( PatternList ,
Pattern  )

◆ mutt_pattern_exec()

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.

Parameters
patPattern to match
flagsFlags, e.g. MUTT_MATCH_FULL_ADDRESS
mMailbox
eEmail
cacheCache for common Patterns
Return values
trueSuccess, pattern matched
falsePattern did not match

flags: MUTT_MATCH_FULL_ADDRESS: match both personal and machine address cache: For repeated matches against the same Header, passing in non-NULL will store some of the cacheable pattern matches in this structure.

Definition at line 1148 of file exec.c.

1150{
1151 const bool needs_msg = pattern_needs_msg(m, pat);
1152 struct Message *msg = needs_msg ? mx_msg_open(m, e) : NULL;
1153 if (needs_msg && !msg)
1154 {
1155 return false;
1156 }
1157 const bool matched = pattern_exec(pat, flags, m, e, msg, cache);
1158 mx_msg_close(m, &msg);
1159 return matched;
1160}
static bool pattern_needs_msg(const struct Mailbox *m, const struct Pattern *pat)
Check whether a pattern needs a full message.
Definition exec.c:798
static bool pattern_exec(struct Pattern *pat, PatternExecFlags flags, struct Mailbox *m, struct Email *e, struct Message *msg, struct PatternCache *cache)
Match a pattern against an email header.
Definition exec.c:845
int mx_msg_close(struct Mailbox *m, struct Message **ptr)
Close a message.
Definition mx.c:1185
struct Message * mx_msg_open(struct Mailbox *m, struct Email *e)
Return a stream pointer for a message.
Definition mx.c:1139
A local copy of an email.
Definition message.h:34
struct Message::@264267271004327071125374067057142037276212342100 flags
Flags for the Message.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_pattern_alias_exec()

bool mutt_pattern_alias_exec ( struct Pattern * pat,
PatternExecFlags flags,
struct AliasView * av,
struct PatternCache * cache )

Match a pattern against an alias.

Parameters
patPattern to match
flagsFlags, e.g. MUTT_MATCH_FULL_ADDRESS
avAliasView
cacheCache for common Patterns
Return values
trueSuccess, pattern matched
falsePattern did not match

flags: MUTT_MATCH_FULL_ADDRESS: match both personal and machine address cache: For repeated matches against the same Alias, passing in non-NULL will store some of the cacheable pattern matches in this structure.

Definition at line 1175 of file exec.c.

1177{
1178 switch (pat->op)
1179 {
1180 case MUTT_PAT_FROM: /* alias */
1181 if (!av->alias)
1182 return false;
1183 return pat->pat_not ^ (av->alias->name && patmatch(pat, av->alias->name));
1184 case MUTT_PAT_CC: /* comment */
1185 if (!av->alias)
1186 return false;
1187 return pat->pat_not ^ (av->alias->comment && patmatch(pat, av->alias->comment));
1188 case MUTT_PAT_TO: /* alias address list */
1189 if (!av->alias)
1190 return false;
1191 return pat->pat_not ^ match_addrlist(pat, (flags & MUTT_MATCH_FULL_ADDRESS),
1192 1, &av->alias->addr);
1194 if (!av->alias)
1195 return false;
1196 return match_tags(pat, &av->alias->tags);
1197 case MUTT_PAT_AND:
1198 return pat->pat_not ^ (perform_alias_and(pat->child, flags, av, cache) > 0);
1199 case MUTT_PAT_OR:
1200 return pat->pat_not ^ (perform_alias_or(pat->child, flags, av, cache) > 0);
1201 }
1202
1203 return false;
1204}
static bool perform_alias_and(struct PatternList *pat, PatternExecFlags flags, struct AliasView *av, struct PatternCache *cache)
Perform a logical AND on a set of Patterns.
Definition exec.c:328
static int match_addrlist(struct Pattern *pat, bool match_personal, int n,...)
match a pattern against an address list
Definition exec.c:419
static bool patmatch(const struct Pattern *pat, const char *buf)
Compare a string to a Pattern.
Definition exec.c:72
static int perform_alias_or(struct PatternList *pat, PatternExecFlags flags, struct AliasView *av, struct PatternCache *cache)
Perform a logical OR on a set of Patterns.
Definition exec.c:377
static bool match_tags(struct Pattern *pat, struct TagList *tags)
match a pattern against a tags list
Definition exec.c:398
#define MUTT_MATCH_FULL_ADDRESS
Match the full address.
Definition lib.h:106
struct Alias * alias
Alias.
Definition gui.h:46
struct TagList tags
Tags.
Definition alias.h:39
char * comment
Free-form comment string.
Definition alias.h:38
char * name
Short name.
Definition alias.h:36
struct AddressList addr
List of Addresses the Alias expands to.
Definition alias.h:37
struct PatternList * child
Arguments to logical operation.
Definition lib.h:90
short op
Operation, e.g. MUTT_PAT_SCORE.
Definition lib.h:78
bool pat_not
Pattern should be inverted (not)
Definition lib.h:79
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_pattern_comp()

struct PatternList * mutt_pattern_comp ( struct MailboxView * mv,
const char * s,
PatternCompFlags flags,
struct Buffer * err )

Create a Pattern.

Parameters
mvMailbox view
sPattern string
flagsFlags, e.g. MUTT_PC_FULL_MSG
errBuffer for error messages
Return values
ptrNewly allocated Pattern

Definition at line 902 of file compile.c.

904{
905 /* curlist when assigned will always point to a list containing at least one node
906 * with a Pattern value. */
907 struct PatternList *curlist = NULL;
908 bool pat_not = false;
909 bool all_addr = false;
910 bool pat_or = false;
911 bool implicit = true; /* used to detect logical AND operator */
912 bool is_alias = false;
913 const struct PatternFlags *entry = NULL;
914 char *p = NULL;
915 char *buf = NULL;
916 struct Mailbox *m = mv ? mv->mailbox : NULL;
917
918 if (!s || (s[0] == '\0'))
919 {
920 buf_strcpy(err, _("empty pattern"));
921 return NULL;
922 }
923
924 struct Buffer *ps = buf_pool_get();
925 buf_strcpy(ps, s);
926 buf_seek(ps, 0);
927
928 SKIPWS(ps->dptr);
929 while (*ps->dptr)
930 {
931 switch (*ps->dptr)
932 {
933 case '^':
934 ps->dptr++;
935 all_addr = !all_addr;
936 break;
937 case '!':
938 ps->dptr++;
939 pat_not = !pat_not;
940 break;
941 case '@':
942 ps->dptr++;
943 is_alias = !is_alias;
944 break;
945 case '|':
946 if (!pat_or)
947 {
948 if (!curlist)
949 {
950 buf_printf(err, _("error in pattern at: %s"), ps->dptr);
951 buf_pool_release(&ps);
952 return NULL;
953 }
954
955 struct Pattern *pat = SLIST_FIRST(curlist);
956 if (SLIST_NEXT(pat, entries))
957 {
958 /* A & B | C == (A & B) | C */
959 struct Pattern *root = attach_new_root(&curlist);
960 root->op = MUTT_PAT_AND;
961 }
962
963 pat_or = true;
964 }
965 ps->dptr++;
966 implicit = false;
967 pat_not = false;
968 all_addr = false;
969 is_alias = false;
970 break;
971 case '%':
972 case '=':
973 case '~':
974 {
975 if (ps->dptr[1] == '\0')
976 {
977 buf_printf(err, _("missing pattern: %s"), ps->dptr);
978 goto cleanup;
979 }
980 short thread_op = 0;
981 if (ps->dptr[1] == '(')
982 thread_op = MUTT_PAT_THREAD;
983 else if ((ps->dptr[1] == '<') && (ps->dptr[2] == '('))
984 thread_op = MUTT_PAT_PARENT;
985 else if ((ps->dptr[1] == '>') && (ps->dptr[2] == '('))
986 thread_op = MUTT_PAT_CHILDREN;
987 if (thread_op != 0)
988 {
989 ps->dptr++; /* skip ~ */
990 if ((thread_op == MUTT_PAT_PARENT) || (thread_op == MUTT_PAT_CHILDREN))
991 ps->dptr++;
992 p = find_matching_paren(ps->dptr + 1);
993 if (p[0] != ')')
994 {
995 buf_printf(err, _("mismatched parentheses: %s"), ps->dptr);
996 goto cleanup;
997 }
998 struct Pattern *leaf = attach_new_leaf(&curlist);
999 leaf->op = thread_op;
1000 leaf->pat_not = pat_not;
1001 leaf->all_addr = all_addr;
1002 leaf->is_alias = is_alias;
1003 pat_not = false;
1004 all_addr = false;
1005 is_alias = false;
1006 /* compile the sub-expression */
1007 buf = mutt_strn_dup(ps->dptr + 1, p - (ps->dptr + 1));
1008 leaf->child = mutt_pattern_comp(mv, buf, flags, err);
1009 if (!leaf->child)
1010 {
1011 FREE(&buf);
1012 goto cleanup;
1013 }
1014 FREE(&buf);
1015 ps->dptr = p + 1; /* restore location */
1016 break;
1017 }
1018 if (implicit && pat_or)
1019 {
1020 /* A | B & C == (A | B) & C */
1021 struct Pattern *root = attach_new_root(&curlist);
1022 root->op = MUTT_PAT_OR;
1023 pat_or = false;
1024 }
1025
1026 entry = lookup_tag(ps->dptr[1]);
1027 if (!entry)
1028 {
1029 buf_printf(err, _("%c: invalid pattern modifier"), *ps->dptr);
1030 goto cleanup;
1031 }
1032 if (entry->flags && ((flags & entry->flags) == 0))
1033 {
1034 buf_printf(err, _("%c: not supported in this mode"), *ps->dptr);
1035 goto cleanup;
1036 }
1037
1038 struct Pattern *leaf = attach_new_leaf(&curlist);
1039 leaf->pat_not = pat_not;
1040 leaf->all_addr = all_addr;
1041 leaf->is_alias = is_alias;
1042 leaf->string_match = (ps->dptr[0] == '=');
1043 leaf->group_match = (ps->dptr[0] == '%');
1044 leaf->sendmode = (flags & MUTT_PC_SEND_MODE_SEARCH);
1045 leaf->op = entry->op;
1046 pat_not = false;
1047 all_addr = false;
1048 is_alias = false;
1049
1050 ps->dptr++; /* move past the ~ */
1051 ps->dptr++; /* eat the operator and any optional whitespace */
1052 SKIPWS(ps->dptr);
1053 if (entry->eat_arg)
1054 {
1055 if (ps->dptr[0] == '\0')
1056 {
1057 buf_addstr(err, _("missing parameter"));
1058 goto cleanup;
1059 }
1060 switch (entry->eat_arg)
1061 {
1062 case EAT_REGEX:
1063 if (!eat_regex(leaf, flags, ps, err))
1064 goto cleanup;
1065 break;
1066 case EAT_DATE:
1067 if (!eat_date(leaf, flags, ps, err))
1068 goto cleanup;
1069 break;
1070 case EAT_RANGE:
1071 if (!eat_range(leaf, flags, ps, err))
1072 goto cleanup;
1073 break;
1074 case EAT_MESSAGE_RANGE:
1075 if (!eat_message_range(leaf, flags, ps, err, mv))
1076 goto cleanup;
1077 break;
1078 case EAT_QUERY:
1079 if (!eat_query(leaf, flags, ps, err, m))
1080 goto cleanup;
1081 break;
1082 default:
1083 break;
1084 }
1085 }
1086 implicit = true;
1087 break;
1088 }
1089
1090 case '(':
1091 {
1092 p = find_matching_paren(ps->dptr + 1);
1093 if (p[0] != ')')
1094 {
1095 buf_printf(err, _("mismatched parentheses: %s"), ps->dptr);
1096 goto cleanup;
1097 }
1098 /* compile the sub-expression */
1099 buf = mutt_strn_dup(ps->dptr + 1, p - (ps->dptr + 1));
1100 struct PatternList *sub = mutt_pattern_comp(mv, buf, flags, err);
1101 FREE(&buf);
1102 if (!sub)
1103 goto cleanup;
1104 struct Pattern *leaf = SLIST_FIRST(sub);
1105 if (curlist)
1106 {
1107 attach_leaf(curlist, leaf);
1108 FREE(&sub);
1109 }
1110 else
1111 {
1112 curlist = sub;
1113 }
1114 leaf->pat_not ^= pat_not;
1115 leaf->all_addr |= all_addr;
1116 leaf->is_alias |= is_alias;
1117 pat_not = false;
1118 all_addr = false;
1119 is_alias = false;
1120 ps->dptr = p + 1; /* restore location */
1121 break;
1122 }
1123
1124 default:
1125 buf_printf(err, _("error in pattern at: %s"), ps->dptr);
1126 goto cleanup;
1127 }
1128 SKIPWS(ps->dptr);
1129 }
1130 buf_pool_release(&ps);
1131
1132 if (!curlist)
1133 {
1134 buf_strcpy(err, _("empty pattern"));
1135 return NULL;
1136 }
1137
1138 if (SLIST_NEXT(SLIST_FIRST(curlist), entries))
1139 {
1140 struct Pattern *root = attach_new_root(&curlist);
1141 root->op = pat_or ? MUTT_PAT_OR : MUTT_PAT_AND;
1142 }
1143
1144 return curlist;
1145
1146cleanup:
1147 mutt_pattern_free(&curlist);
1148 buf_pool_release(&ps);
1149 return NULL;
1150}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition buffer.c:622
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:226
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:395
static struct Pattern * attach_new_root(struct PatternList **curlist)
Create a new Pattern as a parent for a List.
Definition compile.c:866
static struct Pattern * attach_new_leaf(struct PatternList **curlist)
Attach a new Pattern to a List.
Definition compile.c:882
struct PatternList * mutt_pattern_comp(struct MailboxView *mv, const char *s, PatternCompFlags flags, struct Buffer *err)
Create a Pattern.
Definition compile.c:902
static struct Pattern * attach_leaf(struct PatternList *list, struct Pattern *leaf)
Attach a Pattern to a Pattern List.
Definition compile.c:844
static char * find_matching_paren(char *s)
Find the matching parenthesis.
Definition compile.c:750
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition compile.c:774
static bool eat_query(struct Pattern *pat, PatternCompFlags flags, struct Buffer *s, struct Buffer *err, struct Mailbox *m)
Parse a query for an external search program - Implements eat_arg_t -.
Definition compile.c:139
bool eat_message_range(struct Pattern *pat, PatternCompFlags flags, struct Buffer *s, struct Buffer *err, struct MailboxView *mv)
Parse a range of message numbers - Implements eat_arg_t -.
Definition message.c:281
static bool eat_range(struct Pattern *pat, PatternCompFlags flags, struct Buffer *s, struct Buffer *err)
Parse a number range - Implements eat_arg_t -.
Definition compile.c:620
static bool eat_date(struct Pattern *pat, PatternCompFlags flags, struct Buffer *s, struct Buffer *err)
Parse a date pattern - Implements eat_arg_t -.
Definition compile.c:711
static bool eat_regex(struct Pattern *pat, PatternCompFlags flags, struct Buffer *s, struct Buffer *err)
Parse a regex - Implements eat_arg_t -.
Definition compile.c:64
#define FREE(x)
Definition memory.h:62
#define _(a)
Definition message.h:28
char * mutt_strn_dup(const char *begin, size_t len)
Duplicate a sub-string.
Definition string.c:382
const struct PatternFlags * lookup_tag(char tag)
Lookup a pattern modifier.
Definition flags.c:198
#define MUTT_PC_SEND_MODE_SEARCH
Allow send-mode body searching.
Definition lib.h:71
@ EAT_RANGE
Process a number (range)
Definition private.h:55
@ EAT_MESSAGE_RANGE
Process a message number (range)
Definition private.h:56
@ EAT_DATE
Process a date (range)
Definition private.h:54
@ EAT_QUERY
Process a query string.
Definition private.h:57
@ EAT_REGEX
Process a regex.
Definition private.h:53
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:96
#define SLIST_NEXT(elm, field)
Definition queue.h:268
#define SLIST_FIRST(head)
Definition queue.h:227
#define SKIPWS(ch)
Definition string2.h:51
String manipulation buffer.
Definition buffer.h:36
char * dptr
Current read/write position.
Definition buffer.h:38
struct Mailbox * mailbox
Current Mailbox.
Definition mview.h:51
A mailbox.
Definition mailbox.h:79
Mapping between user character and internal constant.
Definition private.h:64
enum PatternEat eat_arg
Type of function needed to parse flag, e.g. EAT_DATE.
Definition private.h:69
PatternCompFlags flags
Pattern flags, e.g. MUTT_PC_FULL_MSG.
Definition private.h:67
int op
Operation to perform, e.g. MUTT_PAT_SCORE.
Definition private.h:66
A simple (non-regex) pattern.
Definition lib.h:77
bool group_match
Check a group of Addresses.
Definition lib.h:82
union Pattern::@006112053024257132210207314205210350156165326341 p
bool all_addr
All Addresses in the list must match.
Definition lib.h:80
bool string_match
Check a string for a match.
Definition lib.h:81
bool is_alias
Is there an alias for this Address?
Definition lib.h:84
bool sendmode
Evaluate searches in send-mode.
Definition lib.h:86
+ 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 109 of file pattern.c.

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

◆ mutt_pattern_free()

void mutt_pattern_free ( struct PatternList ** pat)

Free a Pattern.

Parameters
[out]patPattern to free

Definition at line 774 of file compile.c.

775{
776 if (!pat || !*pat)
777 return;
778
779 struct Pattern *np = SLIST_FIRST(*pat);
780 struct Pattern *next = NULL;
781
782 while (np)
783 {
784 next = SLIST_NEXT(np, entries);
785
786 if (np->is_multi)
787 {
789 }
790 else if (np->string_match || np->dynamic)
791 {
792 FREE(&np->p.str);
793 }
794 else if (np->group_match)
795 {
796 np->p.group = NULL;
797 }
798 else if (np->p.regex)
799 {
800 regfree(np->p.regex);
801 FREE(&np->p.regex);
802 }
803
804#ifdef USE_DEBUG_GRAPHVIZ
805 FREE(&np->raw_pattern);
806#endif
808 FREE(&np);
809
810 np = next;
811 }
812
813 FREE(pat);
814}
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition list.c:123
struct Group * group
Address group if group_match is set.
Definition lib.h:93
regex_t * regex
Compiled regex, for non-pattern matching.
Definition lib.h:92
struct ListHead multi_cases
Multiple strings for ~I pattern.
Definition lib.h:95
char * str
String, if string_match is set.
Definition lib.h:94
bool dynamic
Evaluate date ranges at run time.
Definition lib.h:85
bool is_multi
Multiple case (only for ~I pattern now)
Definition lib.h:87
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_is_list_recipient()

bool mutt_is_list_recipient ( bool all_addr,
struct Envelope * env )

Matches known mailing lists.

Parameters
all_addrIf true, ALL Addresses must be mailing lists
envEnvelope
Return values
true
  • One Address is a mailing list (all_addr is false)
  • All the Addresses are mailing lists (all_addr is true)

Definition at line 509 of file exec.c.

510{
511 return mutt_is_predicate_recipient(all_addr, env, &mutt_is_mail_list);
512}
static bool mutt_is_predicate_recipient(bool all_addr, struct Envelope *env, addr_predicate_t p)
Test an Envelopes Addresses using a predicate function.
Definition exec.c:472
bool mutt_is_mail_list(const struct Address *addr)
Is this the email address of a mailing list?
Definition maillist.c:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_is_subscribed_list_recipient()

bool mutt_is_subscribed_list_recipient ( bool all_addr,
struct Envelope * env )

Matches subscribed mailing lists.

Parameters
all_addrIf true, ALL Addresses must be on the subscribed list
envEnvelope
Return values
true
  • One Address is subscribed (all_addr is false)
  • All the Addresses are subscribed (all_addr is true)

Definition at line 496 of file exec.c.

497{
499}
bool mutt_is_subscribed_list(const struct Address *addr)
Is this the email address of a user-subscribed mailing list?
Definition maillist.c:56
+ 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 308 of file pattern.c.

309{
310 if (!mv || !mv->mailbox)
311 return -1;
312
313 struct Mailbox *m = mv->mailbox;
314
315 struct Buffer *err = NULL;
316 int rc = -1;
317 struct Progress *progress = NULL;
318 struct Buffer *buf = buf_pool_get();
319 bool interrupted = false;
320
321 buf_strcpy(buf, mv->pattern);
322 if (prompt || (op != MUTT_LIMIT))
323 {
324 if ((mw_get_field(prompt, buf, MUTT_COMP_CLEAR, HC_PATTERN, &CompletePatternOps, NULL) != 0) ||
325 buf_is_empty(buf))
326 {
327 buf_pool_release(&buf);
328 return -1;
329 }
330 }
331
332 mutt_message(_("Compiling search pattern..."));
333
334 char *simple = buf_strdup(buf);
335 const char *const c_simple_search = cs_subset_string(NeoMutt->sub, "simple_search");
336 mutt_check_simple(buf, NONULL(c_simple_search));
337 const char *pbuf = buf->data;
338 while (*pbuf == ' ')
339 pbuf++;
340 const bool match_all = mutt_str_equal(pbuf, "~A");
341
342 err = buf_pool_get();
343 struct PatternList *pat = mutt_pattern_comp(mv, buf->data, MUTT_PC_FULL_MSG, err);
344 if (!pat)
345 {
346 mutt_error("%s", buf_string(err));
347 goto bail;
348 }
349
350 if ((m->type == MUTT_IMAP) && (!imap_search(m, pat)))
351 goto bail;
352
353 progress = progress_new(MUTT_PROGRESS_READ, (op == MUTT_LIMIT) ? m->msg_count : m->vcount);
354 progress_set_message(progress, _("Executing command on matching messages..."));
355
356 if (op == MUTT_LIMIT)
357 {
358 m->vcount = 0;
359 mv->vsize = 0;
360 mv->collapsed = false;
361 int padding = mx_msg_padding_size(m);
362
363 for (int i = 0; i < m->msg_count; i++)
364 {
365 struct Email *e = m->emails[i];
366 if (!e)
367 break;
368
369 if (SigInt)
370 {
371 interrupted = true;
372 SigInt = false;
373 break;
374 }
375 progress_update(progress, i, -1);
376 /* new limit pattern implicitly uncollapses all threads */
377 e->vnum = -1;
378 e->visible = false;
379 e->limit_visited = true;
380 e->collapsed = false;
381 e->num_hidden = 0;
382
383 if (match_all ||
385 {
386 e->vnum = m->vcount;
387 e->visible = true;
388 m->v2r[m->vcount] = i;
389 m->vcount++;
390 struct Body *b = e->body;
391 mv->vsize += b->length + b->offset - b->hdr_offset + padding;
392 }
393 }
394 }
395 else
396 {
397 for (int i = 0; i < m->vcount; i++)
398 {
399 struct Email *e = mutt_get_virt_email(m, i);
400 if (!e)
401 continue;
402
403 if (SigInt)
404 {
405 interrupted = true;
406 SigInt = false;
407 break;
408 }
409 progress_update(progress, i, -1);
411 {
412 switch (op)
413 {
414 case MUTT_UNDELETE:
415 mutt_set_flag(m, e, MUTT_PURGE, false, true);
417
418 case MUTT_DELETE:
419 mutt_set_flag(m, e, MUTT_DELETE, (op == MUTT_DELETE), true);
420 break;
421 case MUTT_TAG:
422 case MUTT_UNTAG:
423 mutt_set_flag(m, e, MUTT_TAG, (op == MUTT_TAG), true);
424 break;
425 }
426 }
427 }
428 }
429 progress_free(&progress);
430
432
433 if (op == MUTT_LIMIT)
434 {
435 /* drop previous limit pattern */
436 FREE(&mv->pattern);
438
439 if (m->msg_count && !m->vcount)
440 mutt_error(_("No messages matched criteria"));
441
442 /* record new limit pattern, unless match all */
443 if (!match_all)
444 {
445 mv->pattern = simple;
446 simple = NULL; /* don't clobber it */
448 }
449 }
450
451 if (interrupted)
452 mutt_error(_("Search interrupted"));
453
454 rc = 0;
455
456bail:
457 buf_pool_release(&buf);
458 buf_pool_release(&err);
459 FREE(&simple);
460 mutt_pattern_free(&pat);
461
462 return rc;
463}
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
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: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:1148
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
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:273
#define mutt_error(...)
Definition logging2.h:93
#define mutt_message(...)
Definition logging2.h:92
@ HC_PATTERN
Patterns.
Definition lib.h:58
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:113
@ 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
#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)
struct Email * mutt_get_virt_email(struct Mailbox *m, int vnum)
Get a virtual Email.
Definition mview.c:417
int mx_msg_padding_size(struct Mailbox *m)
Bytes of padding between messages - Wrapper for MxOps::msg_padding_size()
Definition mx.c:1510
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
void mutt_check_simple(struct Buffer *buf, const char *simple)
Convert a simple search into a real request.
Definition pattern.c:109
@ MUTT_PROGRESS_READ
Progress tracks elements, according to $read_inc
Definition lib.h:83
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
volatile sig_atomic_t SigInt
true after SIGINT is received
Definition signal.c:69
#define NONULL(x)
Definition string2.h:43
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
char * data
Pointer to data.
Definition buffer.h:37
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
char * pattern
Limit pattern string.
Definition mview.h:42
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:43
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:47
+ 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 188 of file pattern.c.

190{
191 int rc = -1;
192 struct Progress *progress = NULL;
193 struct Buffer *buf = buf_pool_get();
194
195 buf_strcpy(buf, mdata->limit);
196 if (prompt)
197 {
198 if ((mw_get_field(prompt, buf, MUTT_COMP_CLEAR, HC_PATTERN, &CompletePatternOps, NULL) != 0) ||
199 buf_is_empty(buf))
200 {
201 buf_pool_release(&buf);
202 return -1;
203 }
204 }
205
206 mutt_message(_("Compiling search pattern..."));
207
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 }
228 else
229 {
230 match_all = true;
231 }
232
233 progress = progress_new(MUTT_PROGRESS_READ, ARRAY_SIZE(&mdata->ava));
234 progress_set_message(progress, _("Executing command on matching messages..."));
235
236 int vcounter = 0;
237 struct AliasView *avp = NULL;
238 ARRAY_FOREACH(avp, &mdata->ava)
239 {
240 progress_update(progress, ARRAY_FOREACH_IDX_avp, -1);
241
242 if (match_all ||
244 {
245 switch (action)
246 {
247 case PAA_TAG:
248 avp->is_tagged = true;
249 break;
250 case PAA_UNTAG:
251 avp->is_tagged = false;
252 break;
253 case PAA_VISIBLE:
254 avp->is_visible = true;
255 vcounter++;
256 break;
257 }
258 }
259 else
260 {
261 switch (action)
262 {
263 case PAA_TAG:
264 case PAA_UNTAG:
265 // Do nothing
266 break;
267 case PAA_VISIBLE:
268 avp->is_visible = false;
269 break;
270 }
271 }
272 }
273 progress_free(&progress);
274
275 FREE(&mdata->limit);
276 if (!match_all)
277 {
278 mdata->limit = simple;
279 simple = NULL;
280 }
281
282 if (menu && (action == PAA_VISIBLE))
283 {
284 menu->max = vcounter;
285 menu_set_index(menu, 0);
286 }
287
289
290 rc = 0;
291
292bail:
293 buf_pool_release(&buf);
294 FREE(&simple);
295 mutt_pattern_free(&pat);
296
297 return rc;
298}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:214
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
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:1175
MenuRedrawFlags menu_set_index(struct Menu *menu, int index)
Set the current selection in the Menu.
Definition menu.c:174
#define MUTT_ALIAS_SIMPLESEARCH
Definition lib.h:63
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
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_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 475 of file pattern.c.

477{
478 struct Progress *progress = NULL;
479 int rc = -1;
480 struct Mailbox *m = mv ? mv->mailbox : NULL;
481 if (!m)
482 return -1;
483 bool pattern_changed = false;
484
485 if (buf_is_empty(state->string) || (flags & SEARCH_PROMPT))
486 {
487 if ((mw_get_field((state->reverse) ? _("Reverse search for: ") : _("Search for: "),
489 &CompletePatternOps, NULL) != 0) ||
490 buf_is_empty(state->string))
491 {
492 goto done;
493 }
494
495 /* compare the *expanded* version of the search pattern in case
496 * $simple_search has changed while we were searching */
497 struct Buffer *tmp = buf_pool_get();
498 buf_copy(tmp, state->string);
499 const char *const c_simple_search = cs_subset_string(NeoMutt->sub, "simple_search");
500 mutt_check_simple(tmp, NONULL(c_simple_search));
501 if (!buf_str_equal(tmp, state->string_expn))
502 {
503 mutt_pattern_free(&state->pattern);
504 buf_copy(state->string_expn, tmp);
505 buf_pool_release(&tmp);
506 }
507 }
508
509 if (!state->pattern)
510 {
511 mutt_message(_("Compiling search pattern..."));
512 mutt_pattern_free(&state->pattern);
513 struct Buffer *err = buf_pool_get();
514 state->pattern = mutt_pattern_comp(mv, state->string_expn->data, MUTT_PC_FULL_MSG, err);
515 pattern_changed = true;
516 if (!state->pattern)
517 {
518 mutt_error("%s", buf_string(err));
519 buf_free(&err);
520 buf_reset(state->string);
521 buf_reset(state->string_expn);
522 return -1;
523 }
524 buf_free(&err);
526 }
527
528 if (pattern_changed)
529 {
530 for (int i = 0; i < m->msg_count; i++)
531 m->emails[i]->searched = false;
532 if ((m->type == MUTT_IMAP) && (!imap_search(m, state->pattern)))
533 return -1;
534 }
535
536 int incr = state->reverse ? -1 : 1;
537 if (flags & SEARCH_OPPOSITE)
538 incr = -incr;
539
540 progress = progress_new(MUTT_PROGRESS_READ, m->vcount);
541 progress_set_message(progress, _("Searching..."));
542
543 const bool c_wrap_search = cs_subset_bool(NeoMutt->sub, "wrap_search");
544 for (int i = cur + incr, j = 0; j != m->vcount; j++)
545 {
546 const char *msg = NULL;
547 progress_update(progress, j, -1);
548 if (i > m->vcount - 1)
549 {
550 i = 0;
551 if (c_wrap_search)
552 {
553 msg = _("Search wrapped to top");
554 }
555 else
556 {
557 mutt_message(_("Search hit bottom without finding match"));
558 goto done;
559 }
560 }
561 else if (i < 0)
562 {
563 i = m->vcount - 1;
564 if (c_wrap_search)
565 {
566 msg = _("Search wrapped to bottom");
567 }
568 else
569 {
570 mutt_message(_("Search hit top without finding match"));
571 goto done;
572 }
573 }
574
575 struct Email *e = mutt_get_virt_email(m, i);
576 if (!e)
577 goto done;
578
579 if (e->searched)
580 {
581 /* if we've already evaluated this message, use the cached value */
582 if (e->matched)
583 {
585 if (msg && *msg)
586 mutt_message("%s", msg);
587 rc = i;
588 goto done;
589 }
590 }
591 else
592 {
593 /* remember that we've already searched this message */
594 e->searched = true;
596 MUTT_MATCH_FULL_ADDRESS, m, e, NULL);
597 if (e->matched)
598 {
600 if (msg && *msg)
601 mutt_message("%s", msg);
602 rc = i;
603 goto done;
604 }
605 }
606
607 if (SigInt)
608 {
609 mutt_error(_("Search interrupted"));
610 SigInt = false;
611 goto done;
612 }
613
614 i += incr;
615 }
616
617 mutt_error(_("Not found"));
618done:
619 progress_free(&progress);
620 return rc;
621}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
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 632 of file pattern.c.

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

◆ CompletePatternOps

const struct CompleteOps CompletePatternOps
extern

Auto-Completion of Patterns.

Definition at line 82 of file complete.c.

82 {
83 .complete = complete_pattern,
84};
static enum FunctionRetval complete_pattern(struct EnterWindowData *wdata, int op)
Complete a NeoMutt Pattern - Implements CompleteOps::complete() -.
Definition complete.c:41