-
Notifications
You must be signed in to change notification settings - Fork 1
/
mu4e-query.el
259 lines (213 loc) · 9.57 KB
/
mu4e-query.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
;;; mu4e-query.el --- s-expression query builder for mu4e -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Mickey Petersen
;; Author: Mickey Petersen <mickey@masteringemacs.org>
;; Keywords: email utility
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; S-expression query builder for `mu find'.
;;
;; Build a `mu' QUERY using s-expressions instead of plain strings.
;;
;; All common notation supported by `mu find' also work here, along
;; with a couple of helper forms to speed up complex query building.
;;
;; All fields are supported, in both their abbreviated and complete
;; forms. The same is true for flags.
;;
;; Flags and fields have their own forms, like so:
;;
;; (subject "Your Festivus Catalogue is on its way")
;;
;; (from "G. Costanza")
;;
;; Whitespaced strings are automatically quoted.
;;
;; You can optionally enable regular expressions for a field in one
;; of two ways:
;;
;; (from (regex "[FG]. Costanza"))
;;
;; (from (rx (| "George" "Frank")))
;;
;; The latter uses Emacs's `rx' package to build the regular
;; expression. But note that Emacs's regexp engine is not the one
;; that is ultimately used by `mu find', so not all features work
;; the same.
;;
;; If you are searching for distinct values in a field, you can use
;; the `one-of' or `all-of' helper forms to simplify your query:
;;
;; (to (one-of "cosmo@example.com" "van.nostrand@example.com"))
;;
;; This is turned into:
;;
;; (to:cosmo@example.com or to:van.nostrand@example.com)
;;
;; Logical operators such as `not', `or' and `and' also work like they do in Lisp:
;;
;;
;; (and (flag seen) (not (flag trashed)) (from "pennypacker@example.com"))
;;
;; `size' and `date' queries expect simple cons cells like this:
;;
;; (start .. end), (.. end), or (start ..)
;;
;; Where `start' and `end' is either a string, number, nil or absent
;; entirely (to indicate it is unbounded). Constants such as `1w' or
;; `now' also work, in accordance with `mu''s own support for this.
;;
;; There are simple checks in place to prevent invalid flags or
;; fields from getting passed through to `mu', though it is not
;; infallible.
;;
;; You can always revert back to strings for parts of
;; your queries if you so desire. No effort is made to ensure they
;; are correct, though.
;;; Code:
(defconst mu4e--query-fields
'(bcc h body b cc c changed k embed e file j flags flag g from f maildir m list v message-id
msgid i mime mime-type y path l priority prio p references r subject s tags tag x thread w to t
contact recip)
"List of queryable fields mu supports.")
(defconst mu4e--query-range-fields '(size z date d)
"List of fields that accept ranges of values.")
(defconst mu4e--query-flags
'(draft D flagged F passed P replied R seen S trashed T new N signed z encrypted x
attach a unread u list l personal q calendar c)
"List of accepted values for the `flag' field.")
(defconst mu4e--query-priority '(low normal high)
"List of priorities for the `prio' field.")
(defun mu4e--make-field-value (field value)
"Create a FIELD-VALUE pair for a `mu' query as a string.
If VALUE is a form of `(one-of v1 ... vn)' or `(all-of v1
... vn)' then rewrite VALUE using `mu4e-make-query' into a string
of `and' or `or' for each of its cdr elements as a shorthand for
doing so manually.
If VALUE is not a string or a form -- such as an integer or
symbol -- coerce it to a string.
VALUE is double-quoted if it contains at least one whitespace.
The key-value set is separated with `:' as per the specification
for a mu FIELD query."
(pcase value
;; helper form that expands
;;
;; (subject (one-of "a" "b"))
;;
;; to the query form
;; (or (subject "a") ...)
;;
;; as a shorthand.
((and `(one-of . ,rest))
(mu4e-make-query `(or ,@(mapcar (lambda (element) `(,field ,element)) rest))))
;; ... as above, but using `and' instead of `or'.
((and `(all-of . ,rest))
(mu4e-make-query `(and ,@(mapcar (lambda (element) `(,field ,element)) rest))))
;; default behaviour
((or (pred stringp) (pred integerp) (pred symbolp) (pred consp))
(format "%s:%s" field (mu4e-make-query (mu4e--make-quoted-string value))))))
(defun mu4e--make-quoted-string (value)
"Maybe double-quote VALUE if it has whitespaces."
(if (and (stringp value)
(seq-position value ?\ ))
;; whitespaced queries must be quoted.
(format "\"%s\"" value)
value))
(defun mu4e--make-range-query (field value)
"Create a range FIELD-VALUE pair for a `mu' query string.
Ranged FIELDs are limited to `date' and `size' field queries.
The expected notation for VALUE is `(start .. end)' where `start' or `end'
can be nil (or absent) to mean unbounded."
(pcase value
((or `(.. ,end) `(nil .. ,end)) (mu4e--make-field-value field (format "..%s" end)))
((or `(,start ..) `(,start .. nil)) (mu4e--make-field-value field (format "%s.." start)))
(`(,start .. ,end) (mu4e--make-field-value field (format "%s..%s" start end)))
(_ (error "Error: `%s' is not a valid range for `%s'" field value))))
(defun mu4e-make-query (&rest query)
"Build a `mu' QUERY using s-expressions instead of plain strings.
All common notation supported by `mu find' also work here, along
with a couple of helper forms to speed up complex query building.
All fields are supported, in both their abbreviated and complete
forms. The same is true for flags.
Flags and fields have their own forms, like so:
(subject \"Your Festivus Catalogue is on its way\")
(from \"G. Costanza\")
Whitespaced strings are automatically quoted.
You can optionally enable regular expressions for a field in one
of two ways:
(from (regex \"[FG]. Costanza\"))
(from (rx (| \"George\" \"Frank\")))
The latter uses Emacs's `rx' package to build the regular
expression. But note that Emacs's regexp engine is not the one
that is ultimately used by `mu find', so not all features work
the same.
If you are searching for distinct values in a field, you can use
the `one-of' or `all-of' helper forms to simplify your query:
(to (one-of \"cosmo@example.com\" \"van.nostrand@example.com\"))
This is turned into:
(to:cosmo@example.com or to:van.nostrand@example.com)
Logical operators such as `not', `or' and `and' also work like they do in Lisp:
(and (flag seen) (not (flag trashed)) (from \"pennypacker@example.com\"))
`size' and `date' queries expect simple cons cells like this:
(start .. end), (.. end), or (start ..)
Where `start' and `end' is either a string, number, nil or absent
entirely (to indicate it is unbounded). Constants such as `1w' or
`now' also work, in accordance with `mu''s own support for this.
There are simple checks in place to prevent invalid flags or
fields from getting passed through to `mu', though it is not
infallible.
You can always revert back to strings for parts of
your queries if you so desire. No effort is made to ensure they
are correct, though."
(mapconcat
(lambda (q)
(pcase q
;; ranged field values (`date' and `size')
((and `(,field ,value)
(guard (memq field mu4e--query-range-fields)))
(pcase value
;; we must disambiguate `one-of' / `all-of' helpers here.
((or `(one-of . ,_) `(all-of . ,_)) (mu4e--make-field-value field value))
;; ... everything else
(_ (mu4e--make-range-query field value))))
;; `prio'-only field values.
((and (or `(prio ,prio) `(priority ,prio))
(guard (or (consp prio) (memq prio mu4e--query-priority))))
(mu4e--make-field-value 'prio prio))
;; `flag'-only field values.
((and (or `(flag ,flag) `(flags ,flag) `(g ,flag))
(guard (or (consp flag) (memq flag mu4e--query-flags))))
(mu4e--make-field-value 'flag flag))
;; all other known, supported fields
;; excl. `flag' / `g' / `prio' / `date' / `d' / `size' / `z'.
((and (pred consp) `(,field ,value)
(guard (memq field mu4e--query-fields)))
(mu4e--make-field-value field value))
;; maps to the ed-style /regex/ notation
(`(regex ,regex) (format "/%s/" regex))
;; reifies an `rx' regexp form to a string. note that mu does
;; not use Emacs's regexp engine under the hood though.
(`(rx . ,regex) (format "/%s/" (rx-to-string `(and ,@regex) t)))
;; boolean logic
(`(not . ,rest) (if rest (format "(not %s)" (mapconcat 'mu4e-make-query rest "")) ""))
(`(or . ,rest) (if rest (format "(%s)" (mapconcat 'mu4e-make-query rest " or ")) ""))
(`(and . ,rest) (if rest (format "(%s)" (mapconcat 'mu4e-make-query rest " and ")) ""))
;; handle string forms as it is possible to search a range of
;; fields without a field specifier.
((and (pred consp) `(,s)) (mu4e--make-quoted-string (format "%s" s)))
;; coalesce strings, integers and symbols into their canonical
;; string represenation.
((or (pred stringp) (pred integerp) (pred symbolp)) (format "%s" q))
(_ (error "Error: `%s' is not a valid expression, field, or value" q))))
query
" "))
(provide 'mu4e-query)
;;; mu4e-query.el ends here