1NOTMUCH-SEXP-QUERIES(7)             notmuch            NOTMUCH-SEXP-QUERIES(7)
2
3
4

NAME

6       notmuch-sexp-queries - s-expression syntax for notmuch queries
7

SYNOPSIS

9       notmuch subcommand --query=sexp [option ...]  -- '(and (to santa) (date
10       december))'
11

DESCRIPTION

13       Notmuch supports an alternative query syntax based on  S-expressions  .
14       It  can  be selected with the command line --query=sexp or with the ap‐
15       propriate option to the library function notmuch_query_create_with_syn‐
16       tax().   Support for this syntax is currently optional, you can test if
17       your build of notmuch supports it with
18
19          $ notmuch config get built_with.sexp_queries
20
21   S-EXPRESSIONS
22       An s-expression is either an atom,  or  list  of  whitespace  delimited
23       s-expressions inside parentheses. Atoms are either
24
25       basic value
26          A basic value is an unquoted string containing no whitespace, double
27          quotes, or parentheses.
28
29       quoted string
30          Double quotes (") delimit strings possibly containing whitespace  or
31          parentheses.  These  can contain double quote characters by escaping
32          with backslash. E.g. "this is a quote \"".
33
34   S-EXPRESSION QUERIES
35       An s-expression query is either an atom, the empty list, or a  compound
36       query  consisting  of  a  prefix atom (first element) defining a field,
37       logical operation, or modifier, and 0 or more subqueries.
38
39       *
40          "*" matches any non-empty string in the current field.
41
42       ()
43          The empty list matches all messages
44
45       term
46          Match all messages  containing  term,  possibly  after  stemming  or
47          phrase  splitting.  For  discussion  of stemming in notmuch see not‐
48          much-search-terms(7). Stemming only applies to unquoted terms (basic
49          values)  in  s-expression queries.  For information on phrase split‐
50          ting see FIELDS.
51
52       ( field q1 q2 ... qn )
53          Restrict the queries q1 to qn to field, and combine  with  and  (for
54          most fields) or or. See FIELDS for more information.
55
56       ( operator q1 q2 ... qn )
57          Combine queries q1 to qn. Currently supported operators are and, or,
58          and not. (not q1 ... qn ) is equivalent to (and (not q1 )  ...  (not
59          qn )).
60
61       ( modifier q1 q2 ... qn )
62          Combine queries q1 to qn, and reinterpret the result (e.g. as a reg‐
63          ular expression).  See MODIFIERS for more information.
64
65       (macro ( p1 ... pn ) body)
66          Define saved query with parameter substitution. The syntax is recog‐
67          nized  only  in  saved  s-expression  queries  (see squery.* in not‐
68          much-config(1)). Parameter names in body must be prefixed with ,  to
69          be expanded (see MACRO EXAMPLES).  Macros may refer to other macros,
70          but only to their own parameters [1].
71
72   FIELDS
73       Fields [2] correspond to attributes of mail messages. Some are inherent
74       (and  immutable)  like  subject, while others tag and property are set‐
75       table by the user.  Each concrete field in the table below is discussed
76       further  under  "Search  prefixes"  in notmuch-search-terms(7). The row
77       user refers to user defined fields, described in notmuch-config(1).
78
79       Most fields are either phrase fields  [3]  (which  match  sequences  of
80       words),  or  term fields [4] (which match exact strings). Phrase split‐
81       ting breaks the term (basic value or quoted string) into words,  ignore
82       punctuation.  Phrase  splitting  is  applied to terms in phrase (proba‐
83       bilistic) fields. Both phrase splitting  and  stemming  apply  only  in
84       phrase fields.
85
86       Each  term or phrase field has an associated combining operator (and or
87       or) used to combine the queries from each element of the  tail  of  the
88       list.  This  is  generally  or for those fields where a message has one
89       such attribute, and and otherwise.
90
91       Term or phrase fields can contain arbitrarily complex queries  made  up
92       from terms, operators, and modifiers, but not other fields.
93
94   Fields with supported modifiers
95             ┌───────────┬─────────┬────────┬────────┬──────────┬───────┐
96             │field      │ combine │ type   │ expand │ wildcard │ regex │
97             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
98none       │ and     │        │ no     │ yes      │ no    │
99             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
100user       │ and     │ phrase │ no     │ yes      │ no    │
101             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
102             │attachment │ and     │ phrase │ yes    │ yes      │ no    │
103             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
104             │body       │ and     │ phrase │ no     │ no       │ no    │
105             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
106             │date       │         │ range  │ no     │ no       │ no    │
107             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
108             │folder     │ or      │ phrase │ yes    │ yes      │ yes   │
109             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
110             │from       │ and     │ phrase │ yes    │ yes      │ yes   │
111             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
112             │id         │ or      │ term   │ no     │ yes      │ yes   │
113             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
114             │is         │ and     │ term   │ yes    │ yes      │ yes   │
115             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
116             │lastmod    │         │ range  │ no     │ no       │ no    │
117             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
118             │mid        │ or      │ term   │ no     │ yes      │ yes   │
119             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
120             │mimetype   │ or      │ phrase │ yes    │ yes      │ no    │
121             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
122             │path       │ or      │ term   │ no     │ yes      │ yes   │
123             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
124             │property   │ and     │ term   │ yes    │ yes      │ yes   │
125             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
126             │subject    │ and     │ phrase │ yes    │ yes      │ yes   │
127             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
128             │tag        │ and     │ term   │ yes    │ yes      │ yes   │
129             ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
130             │thread     │ or      │ term   │ yes    │ yes      │ yes   │
131             └───────────┴─────────┴────────┴────────┴──────────┴───────┘
132
133             │to         │ and     │ phrase │ yes    │ yes      │ no    │
134             └───────────┴─────────┴────────┴────────┴──────────┴───────┘
135
136   MODIFIERS
137       Modifiers  refer  to  any prefixes (first elements of compound queries)
138       that are neither operators nor fields.
139
140       (infix atom )
141          Interpret   atom   as   an   infix   notmuch   query    (see    not‐
142          much-search-terms(7)). Not supported inside fields.
143
144       (matching q1 q2 ... qn ) (of q1 q2 ... qn  )
145          Match  all  messages  have  the  same values of the current field as
146          those matching all of q1 ... qn.  Supported  in  most  term  [7]  or
147          phrase fields. Most commonly used in the thread field.
148
149       (query atom )
150          Expand  to  the saved query named by atom. See notmuch-config(1) for
151          more. Note that the saved  query  must  be  in  infix  syntax  (not‐
152          much-search-terms(7)). Not supported inside fields.
153
154       (regex atom ) (rx atom )
155          Interpret  atom as a POSIX.2 regular expression (see regex(7)). This
156          applies in term fields and a subset [5] of phrase fields (see Fields
157          with supported modifiers).
158
159       (starts-with subword )
160          Matches  any  term  starting  with  subword.  This applies in either
161          phrase or term fields,  or  outside  of  fields  [6].  Note  that  a
162          starts-with  query  cannot be part of a phrase. The atom * is a syn‐
163          onym for (starts-with "").
164

EXAMPLES

166       Wizard
167          Match all messages containing the word "wizard", ignoring case.
168
169       added
170          Match all messages containing "added",  but  also  those  containing
171          "add", "additional", "Additional", "adds", etc... via stemming.
172
173       (and Bob Marley)
174          Match  messages  containing words "Bob" and "Marley", or their stems
175          The words need not be adjacent.
176
177       (not Bob Marley)
178          Match messages containing neither  "Bob"  nor  "Marley",  nor  their
179          stems,
180
181       "quick fox" quick-fox quick@fox
182          Match the phrase "quick" followed by "fox" in phrase fields (or out‐
183          side a field). Match the literal string in a term field.
184
185       (folder (of (id 1234@invalid)))
186          Match any message in the same folder as the one with  Message-Id  "‐
187          1234@invalid"
188
189       (id 1234@invalid blah@test)
190          Matches Message-Id "1234@invalid" or Message-Id "blah@test"
191
192       (and (infix "date:2009-11-18..2009-11-18") (tag unread))
193          Match messages in the given date range with tag unread.
194
195       (starts-with prelim)
196          Match any words starting with "prelim".
197
198       (subject quick "brown fox")
199          Match  messages  whose  subject contains "quick" (anywhere, stemmed)
200          and the phrase "brown fox".
201
202       (subject (starts-with prelim))
203          Matches any word starting with "prelim", inside a message subject.
204
205       (subject (starts-wih quick) "brown fox")
206          Match messages whose subject contains "quick brown  fox",  but  also
207          "brown fox quicksand".
208
209       (thread (of (id 1234@invalid)))
210          Match  any  message in the same thread as the one with Message-Id "‐
211          1234@invalid"
212
213       (thread (matching (from bob@example.com) (to bob@example.com)))
214          Match any (messages in)  a  thread  containing  a  message  from  "‐
215          bob@example.com"  and a (possibly distinct) message to "bob at exam‐
216          ple.com")
217
218       (to (or bob@example.com mallory@example.org)) (or (to  bob@example.com)
219       (to mallory@example.org))
220          Match   in   the   "To"   or  "Cc"  headers,  "bob@example.com",  "‐
221          mallory@example.org", and also "bob@example.com.au"  since  it  con‐
222          tains the adjacent triple "bob", "example", "com".
223
224       (not (to *))
225          Match messages with an empty or invalid 'To' and 'Cc' field.
226
227       (List *)
228          Match  messages with a non-empty List-Id header, assuming configura‐
229          tion index.header.List=List-Id
230
231   MACRO EXAMPLES
232       A macro that takes two parameters and applies different fields to them.
233
234          $ notmuch config set squery.TagSubject '(macro (tagname subj) (and (tag ,tagname) (subject ,subj)))'
235          $ notmuch search --query=sexp '(TagSubject inbox maildir)'
236
237       Nested macros are allowed.
238
239          $ notmuch config set squery.Inner '(macro (x) (subject ,x))'
240          $ notmuch config set squery.Outer  '(macro (x y) (and (tag ,x) (Inner ,y)))'
241          $ notmuch search --query=sexp '(Outer inbox maildir)'
242
243       Parameters can be re-used to reduce boilerplate. Any  field,  including
244       user defined fields is permitted within a macro.
245
246          $ notmuch config set squery.About '(macro (name) (or (subject ,name) (List ,name)))'
247          $ notmuch search --query=sexp '(About notmuch)'
248

NOTES

250       [1]  Technically  macros  impliment  lazy evaluation and lexical scope.
251            There is one top level scope containing all macro definitions, but
252            all parameter definitions are local to a given macro.
253
254       [2]  a.k.a. prefixes
255
256       [3]  a.k.a. probabilistic prefixes
257
258       [4]  a.k.a. boolean prefixes
259
260       [5]  Due  to the implemention of phrase fields in Xapian, regex queries
261            could only match individual words.
262
263       [6]  Due the the way body is implemented in notmuch, this  modifier  is
264            not supported in the body field.
265
266       [7]  Due  to the way recursive path queries are implemented in notmuch,
267            this modifier is not supported in the path field.
268

AUTHOR

270       Carl Worth and many others
271
273       2009-2022, Carl Worth and many others
274
275
276
277
2780.36                             Jun 25, 2022          NOTMUCH-SEXP-QUERIES(7)
Impressum