]> git.cworth.org Git - notmuch/blob - doc/man7/notmuch-sexp-queries.rst
doc/sexp-queries: update synopsis and description
[notmuch] / doc / man7 / notmuch-sexp-queries.rst
1 .. _notmuch-sexp-queries(7):
2
3 ====================
4 notmuch-sexp-queries
5 ====================
6
7 SYNOPSIS
8 ========
9
10 **notmuch** *subcommand* ``--query=sexp`` [option ...]  ``--`` '(and (to santa) (date december))'
11
12 DESCRIPTION
13 ===========
14
15 Notmuch supports an alternative query syntax based on `S-expressions
16 <https://en.wikipedia.org/wiki/S-expression>`_ . It can be selected
17 with the command line ``--query=sexp`` or with the appropriate option
18 to the library function :c:func:`notmuch_query_create_with_syntax`.
19 Support for this syntax is currently optional, you can test if your
20 build of notmuch supports it with
21
22 ::
23
24    $ notmuch config get built_with.sexpr_query
25
26
27 S-EXPRESSIONS
28 -------------
29
30 An *s-expression* is either an atom, or list of whitespace delimited
31 s-expressions inside parentheses. Atoms are either
32
33 *basic value*
34     A basic value is an unquoted string containing no whitespace, double quotes, or
35     parentheses.
36
37 *quoted string*
38     Double quotes (") delimit strings possibly containing whitespace
39     or parentheses. These can contain double quote characters by
40     escaping with backslash. E.g. ``"this is a quote \""``.
41
42 S-EXPRESSION QUERIES
43 --------------------
44
45 An s-expression query is either an atom, the empty list, or a
46 *compound query* consisting of a prefix atom (first element) defining
47 a *field*, *logical operation*, or *modifier*, and 0 or more
48 subqueries.
49
50 ``*``
51    "*" matches any non-empty string in the current field.
52
53 ``()``
54     The empty list matches all messages
55
56 *term*
57
58     Match all messages containing *term*, possibly after stemming or
59     phrase splitting. For discussion of stemming in notmuch see
60     :any:`notmuch-search-terms(7)`. Stemming only applies to unquoted
61     terms (basic values) in s-expression queries.  For information on
62     phrase splitting see :any:`fields`.
63
64 ``(`` *field* |q1| |q2| ... |qn| ``)``
65     Restrict the queries |q1| to |qn| to *field*, and combine with *and*
66     (for most fields) or *or*. See :any:`fields` for more information.
67
68 ``(`` *operator* |q1| |q2| ... |qn| ``)``
69     Combine queries |q1| to |qn|. Currently supported operators are
70     ``and``, ``or``, and ``not``. ``(not`` |q1| ... |qn| ``)`` is equivalent
71     to ``(and (not`` |q1| ``) ... (not`` |qn| ``))``.
72
73 ``(`` *modifier* |q1| |q2| ... |qn| ``)``
74     Combine queries |q1| to |qn|, and reinterpret the result (e.g. as a regular expression).
75     See :any:`modifiers` for more information.
76
77 ``(macro (`` |p1| ... |pn| ``) body)``
78     Define saved query with parameter substitution. The syntax is
79     recognized only in saved s-expression queries (see ``squery.*`` in
80     :any:`notmuch-config(1)`). Parameter names in ``body`` must be
81     prefixed with ``,`` to be expanded (see :any:`macro_examples`).
82     Macros may refer to other macros, but only to their own
83     parameters [#macro-details]_.
84
85 .. _fields:
86
87 FIELDS
88 ``````
89
90 *Fields* [#aka-pref]_
91 correspond to attributes of mail messages. Some are inherent (and
92 immutable) like ``subject``, while others ``tag`` and ``property`` are
93 settable by the user.  Each concrete field in
94 :any:`the table below <field-table>`
95 is discussed further under "Search prefixes" in
96 :any:`notmuch-search-terms(7)`. The row *user* refers to user defined
97 fields, described in :any:`notmuch-config(1)`.
98
99 Most fields are either *phrase fields* [#aka-prob]_ (which match
100 sequences of words), or *term fields* [#aka-bool]_ (which match exact
101 strings). *Phrase splitting* breaks the term (basic value or quoted
102 string) into words, ignore punctuation. Phrase splitting is applied to
103 terms in phrase (probabilistic) fields. Both phrase splitting and
104 stemming apply only in phrase fields.
105
106 Each term or phrase field has an associated combining operator
107 (``and`` or ``or``) used to combine the queries from each element of
108 the tail of the list. This is generally ``or`` for those fields where
109 a message has one such attribute, and ``and`` otherwise.
110
111 Term or phrase fields can contain arbitrarily complex queries made up
112 from terms, operators, and modifiers, but not other fields.
113
114 .. _field-table:
115
116 .. table:: Fields with supported modifiers
117
118   +------------+-----------+-----------+-----------+-----------+----------+
119   |   field    |  combine  |   type    |  expand   | wildcard  |  regex   |
120   +============+===========+===========+===========+===========+==========+
121   |   *none*   |    and    |           |    no     |    yes    |    no    |
122   +------------+-----------+-----------+-----------+-----------+----------+
123   |   *user*   |    and    |  phrase   |    no     |    yes    |    no    |
124   +------------+-----------+-----------+-----------+-----------+----------+
125   | attachment |    and    |  phrase   |    yes    |    yes    |    no    |
126   +------------+-----------+-----------+-----------+-----------+----------+
127   |    body    |    and    |  phrase   |    no     |    no     |    no    |
128   +------------+-----------+-----------+-----------+-----------+----------+
129   |    date    |           |   range   |    no     |    no     |    no    |
130   +------------+-----------+-----------+-----------+-----------+----------+
131   |   folder   |    or     |  phrase   |    yes    |    yes    |   yes    |
132   +------------+-----------+-----------+-----------+-----------+----------+
133   |    from    |    and    |  phrase   |    yes    |    yes    |   yes    |
134   +------------+-----------+-----------+-----------+-----------+----------+
135   |     id     |    or     |   term    |    no     |    yes    |   yes    |
136   +------------+-----------+-----------+-----------+-----------+----------+
137   |     is     |    and    |   term    |    yes    |    yes    |   yes    |
138   +------------+-----------+-----------+-----------+-----------+----------+
139   |  lastmod   |           |   range   |    no     |    no     |    no    |
140   +------------+-----------+-----------+-----------+-----------+----------+
141   |    mid     |    or     |   term    |    no     |    yes    |   yes    |
142   +------------+-----------+-----------+-----------+-----------+----------+
143   |  mimetype  |    or     |  phrase   |    yes    |    yes    |    no    |
144   +------------+-----------+-----------+-----------+-----------+----------+
145   |    path    |    or     |   term    |    no     |    yes    |   yes    |
146   +------------+-----------+-----------+-----------+-----------+----------+
147   |  property  |    and    |   term    |    yes    |    yes    |   yes    |
148   +------------+-----------+-----------+-----------+-----------+----------+
149   |  subject   |    and    |  phrase   |    yes    |    yes    |   yes    |
150   +------------+-----------+-----------+-----------+-----------+----------+
151   |    tag     |    and    |   term    |    yes    |    yes    |   yes    |
152   +------------+-----------+-----------+-----------+-----------+----------+
153   |   thread   |    or     |   term    |    yes    |    yes    |   yes    |
154   +------------+-----------+-----------+-----------+-----------+----------+
155   |     to     |    and    |  phrase   |    yes    |    yes    |    no    |
156   +------------+-----------+-----------+-----------+-----------+----------+
157
158 .. _modifiers:
159
160 MODIFIERS
161 `````````
162
163 *Modifiers* refer to any prefixes (first elements of compound queries)
164 that are neither operators nor fields.
165
166 ``(infix`` *atom* ``)``
167     Interpret *atom* as an infix notmuch query (see
168     :any:`notmuch-search-terms(7)`). Not supported inside fields.
169
170 ``(matching`` |q1| |q2| ... |qn| ``)`` ``(of`` |q1| |q2| ... |qn|  ``)``
171     Match all messages have the same values of the current field as
172     those matching all of |q1| ... |qn|. Supported in most term [#not-path]_ or
173     phrase fields. Most commonly used in the ``thread`` field.
174
175 ``(query`` *atom* ``)``
176     Expand to the saved query named by *atom*. See
177     :any:`notmuch-config(1)` for more. Note that the saved query must
178     be in infix syntax (:any:`notmuch-search-terms(7)`). Not supported
179     inside fields.
180
181 ``(regex`` *atom* ``)`` ``(rx`` *atom* ``)``
182     Interpret *atom* as a POSIX.2 regular expression (see
183     :manpage:`regex(7)`). This applies in term fields and a subset [#not-phrase]_ of
184     phrase fields (see :any:`field-table`).
185
186 ``(starts-with`` *subword* ``)``
187     Matches any term starting with *subword*.  This applies in either
188     phrase or term :any:`fields <fields>`, or outside of fields [#not-body]_. Note that
189     a ``starts-with`` query cannot be part of a phrase. The
190     atom ``*`` is a synonym for ``(starts-with "")``.
191
192 EXAMPLES
193 ========
194
195 ``Wizard``
196     Match all messages containing the word "wizard", ignoring case.
197
198 ``added``
199     Match all messages containing "added", but also those containing "add", "additional",
200     "Additional", "adds", etc... via stemming.
201
202 ``(and Bob Marley)``
203     Match messages containing words "Bob" and "Marley", or their stems
204     The words need not be adjacent.
205
206 ``(not Bob Marley)``
207     Match messages containing neither "Bob" nor "Marley", nor their stems,
208
209 ``"quick fox"`` ``quick-fox`` ``quick@fox``
210     Match the *phrase* "quick" followed by "fox" in phrase fields (or
211     outside a field). Match the literal string in a term field.
212
213 ``(folder (of (id 1234@invalid)))``
214     Match any message in the same folder as the one with Message-Id "1234@invalid"
215
216 ``(id 1234@invalid blah@test)``
217     Matches Message-Id "1234@invalid" *or* Message-Id "blah@test"
218
219 ``(and (infix "date:2009-11-18..2009-11-18") (tag unread))``
220     Match messages in the given date range with tag unread.
221
222 ``(starts-with prelim)``
223     Match any words starting with "prelim".
224
225 ``(subject quick "brown fox")``
226     Match messages whose subject contains "quick" (anywhere, stemmed) and
227     the phrase "brown fox".
228
229 ``(subject (starts-with prelim))``
230     Matches any word starting with "prelim", inside a message subject.
231
232 ``(subject (starts-wih quick) "brown fox")``
233     Match messages whose subject contains "quick brown fox", but also
234     "brown fox quicksand".
235
236 ``(thread (of (id 1234@invalid)))``
237     Match any message in the same thread as the one with Message-Id "1234@invalid"
238
239 ``(thread (matching (from bob@example.com) (to bob@example.com)))``
240     Match any (messages in) a thread containing a message from
241     "bob@example.com" and a (possibly distinct) message to "bob at
242     example.com")
243
244 ``(to (or bob@example.com mallory@example.org))`` ``(or (to bob@example.com) (to mallory@example.org))``
245     Match in the "To" or "Cc" headers, "bob@example.com",
246     "mallory@example.org", and also "bob@example.com.au" since it
247     contains the adjacent triple "bob", "example", "com".
248
249 ``(not (to *))``
250     Match messages with an empty or invalid 'To' and 'Cc' field.
251
252 ``(List *)``
253     Match messages with a non-empty List-Id header, assuming
254     configuration ``index.header.List=List-Id``
255
256 .. _macro_examples:
257
258 MACRO EXAMPLES
259 --------------
260
261 A macro that takes two parameters and applies different fields to them.
262
263 ::
264
265    $ notmuch config set squery.TagSubject '(macro (tagname subj) (and (tag ,tagname) (subject ,subj)))'
266    $ notmuch search --query=sexp '(TagSubject inbox maildir)'
267
268 Nested macros are allowed.
269
270 ::
271
272     $ notmuch config set squery.Inner '(macro (x) (subject ,x))'
273     $ notmuch config set squery.Outer  '(macro (x y) (and (tag ,x) (Inner ,y)))'
274     $ notmuch search --query=sexp '(Outer inbox maildir)'
275
276 Parameters can be re-used to reduce boilerplate. Any field, including
277 user defined fields is permitted within a macro.
278
279 ::
280
281     $ notmuch config set squery.About '(macro (name) (or (subject ,name) (List ,name)))'
282     $ notmuch search --query=sexp '(About notmuch)'
283
284
285 NOTES
286 =====
287
288 .. [#macro-details] Technically macros impliment lazy evaluation and
289                     lexical scope. There is one top level scope
290                     containing all macro definitions, but all
291                     parameter definitions are local to a given macro.
292
293 .. [#aka-pref] a.k.a. prefixes
294
295 .. [#aka-prob] a.k.a. probabilistic prefixes
296
297 .. [#aka-bool] a.k.a. boolean prefixes
298
299 .. [#not-phrase] Due to the implemention of phrase fields in Xapian,
300                  regex queries could only match individual words.
301
302 .. [#not-body] Due the the way ``body`` is implemented in notmuch,
303                this modifier is not supported in the ``body`` field.
304
305 .. [#not-path] Due to the way recursive ``path`` queries are implemented
306                in notmuch, this modifier is not supported in the
307                ``path`` field.
308
309 .. |q1| replace:: :math:`q_1`
310 .. |q2| replace:: :math:`q_2`
311 .. |qn| replace:: :math:`q_n`
312
313 .. |p1| replace:: :math:`p_1`
314 .. |p2| replace:: :math:`p_2`
315 .. |pn| replace:: :math:`p_n`