1 Return-Path: <dme@dme.org>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id D7A20431FC3
\r
6 for <notmuch@notmuchmail.org>; Tue, 25 Nov 2014 01:07:07 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=-0.699 tagged_above=-999 required=5
\r
12 tests=[RCVD_IN_DNSWL_LOW=-0.7, UNPARSEABLE_RELAY=0.001]
\r
14 Received: from olra.theworths.org ([127.0.0.1])
\r
15 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
16 with ESMTP id Pv2Yf5HeDRTb for <notmuch@notmuchmail.org>;
\r
17 Tue, 25 Nov 2014 01:07:00 -0800 (PST)
\r
18 Received: from mail-wg0-f50.google.com (mail-wg0-f50.google.com
\r
19 [74.125.82.50]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client
\r
20 certificate requested) by olra.theworths.org (Postfix) with ESMTPS id
\r
21 1316F431FAF for <notmuch@notmuchmail.org>; Tue, 25 Nov 2014 01:07:00 -0800
\r
23 Received: by mail-wg0-f50.google.com with SMTP id k14so228219wgh.23
\r
24 for <notmuch@notmuchmail.org>; Tue, 25 Nov 2014 01:06:59 -0800 (PST)
\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
\r
26 d=1e100.net; s=20130820;
\r
27 h=x-gm-message-state:user-agent:from:to:subject:date:message-id;
\r
28 bh=qNqAimSRgAqUvWo9JF+eLK4arCODnursqIvPaWAs0Rc=;
\r
29 b=f1aP94TylBERCxZo3X69WSnqxT+he1v69y9lJtjXKSrcNawt0cSltkdV6x9xHZTsYb
\r
30 qkeGq0UAYSqhgQTGUCXIC0Z1z6xQCWHZ/72yGuFwl/Dg+aI49m6YhLaM+7HqVeJqzDaw
\r
31 D5XF+BVpamaZvFDndBmr3w4dvHehAbHfCo3Nc5HpV30rzCP/7jRlSHolqwu3vw3nK8pi
\r
32 Lh/yKjbcaqwOoi+OqO2KzYx0pIYs+F1xc11ZR2BwBwZZi3vZD1mkcYwPn3broEfTHRv0
\r
33 qF/468AbothFn5PO+BGi/OxN1tR3w9npVF205H/uGxXXYqGnlx3bz8qzbbeRVGDhUGsT
\r
36 ALoCoQlPWy1vcm8NEQ9LcepslS0y/vlKKaGrBWsVbKwNHOt9IYSkAZO2bNXlXKC9HS9Pj6pZAXpb
\r
37 X-Received: by 10.180.92.129 with SMTP id cm1mr6687547wib.33.1416906418764;
\r
38 Tue, 25 Nov 2014 01:06:58 -0800 (PST)
\r
39 Received: from disaster-area.hh.sledj.net
\r
40 ([2a01:348:1a2:1:ea39:35ff:fe2c:a227])
\r
41 by mx.google.com with ESMTPSA id s9sm1913937wiz.12.2014.11.25.01.06.57
\r
42 for <notmuch@notmuchmail.org>
\r
43 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
\r
44 Tue, 25 Nov 2014 01:06:58 -0800 (PST)
\r
45 Received: from localhost (30000@localhost [local]);
\r
46 by localhost (OpenSMTPD) with ESMTPA id af847ce3;
\r
47 for <notmuch@notmuchmail.org>; Tue, 25 Nov 2014 09:06:56 +0000 (UTC)
\r
48 User-Agent: OpenSMTPD enqueuer (Demoostik)
\r
49 From: David Edmondson <dme@dme.org>
\r
50 To: notmuch@notmuchmail.org
\r
51 Subject: [RFC][PATCH v2] emacs: Replace `notmuch-search-result-format'
\r
53 Date: Tue, 25 Nov 2014 09:06:56 +0000
\r
54 Message-Id: <1416906416-3952-1-git-send-email-dme@dme.org>
\r
55 X-Mailer: git-send-email 2.1.3
\r
56 X-BeenThere: notmuch@notmuchmail.org
\r
57 X-Mailman-Version: 2.1.13
\r
59 List-Id: "Use and development of the notmuch mail system."
\r
60 <notmuch.notmuchmail.org>
\r
61 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
62 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
63 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
64 List-Post: <mailto:notmuch@notmuchmail.org>
\r
65 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
66 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
67 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
68 X-List-Received-Date: Tue, 25 Nov 2014 09:07:08 -0000
\r
70 Alternative implementation of code that interprets
\r
71 `notmuch-search-result-format' to insert the results of a search.
\r
74 Added some more documentation, as per bremner.
\r
76 emacs/notmuch.el | 300 +++++++++++++++++++++++++++++++++----------------------
\r
77 1 file changed, 178 insertions(+), 122 deletions(-)
\r
79 diff --git a/emacs/notmuch.el b/emacs/notmuch.el
\r
80 index 218486a..eb79a29 100644
\r
81 --- a/emacs/notmuch.el
\r
82 +++ b/emacs/notmuch.el
\r
83 @@ -66,23 +66,103 @@
\r
84 "Placeholder variable when notmuch-version.el[c] is not available."))
\r
86 (defcustom notmuch-search-result-format
\r
87 - `(("date" . "%12s ")
\r
88 - ("count" . "%-7s ")
\r
89 - ("authors" . "%-20s ")
\r
90 - ("subject" . "%s ")
\r
91 - ("tags" . "(%s)"))
\r
92 - "Search result formatting. Supported fields are:
\r
93 - date, count, authors, subject, tags
\r
95 - (setq notmuch-search-result-format \(\(\"authors\" . \"%-40s\"\)
\r
96 - \(\"subject\" . \"%s\"\)\)\)
\r
97 -Line breaks are permitted in format strings (though this is
\r
98 -currently experimental). Note that a line break at the end of an
\r
99 -\"authors\" field will get elided if the authors list is long;
\r
100 -place it instead at the beginning of the following field. To
\r
101 -enter a line break when setting this variable with setq, use \\n.
\r
102 -To enter a line break in customize, press \\[quoted-insert] C-j."
\r
103 - :type '(alist :key-type (string) :value-type (string))
\r
104 + '((:width -12 :date_relative) " "
\r
105 + (:width -7 (:concat "[" :matched "/" :total "]")) " "
\r
106 + ;; This splitting will not be necessary once
\r
107 + ;; id:1414172643-28270-1-git-send-email-dme@dme.org is integrated.
\r
108 + (:width 40 (:eval (let* ((split-authors (split-string
\r
109 + (plist-get notmuch-thread :authors)
\r
110 + "|" t split-string-default-separators))
\r
111 + ;; There will always be matched authors.
\r
112 + (matched-authors (car split-authors))
\r
113 + ;; There may not be non-matched authors.
\r
114 + (non-matched-authors (mapconcat #'identity (cdr split-authors) " ")))
\r
115 + (notmuch-search-format-authors matched-authors non-matched-authors)))) " "
\r
117 + "(" (:eval (notmuch-tag-format-tags (plist-get notmuch-thread :tags)
\r
118 + (plist-get notmuch-thread :orig-tags)))
\r
121 + "Template for displaying search results.
\r
123 +The value is a list of items to insert in the search
\r
124 +results. Individual items should be in one of the following
\r
127 +A string that is inserted directly (e.g. \" \").
\r
129 +A number that is inserted directly (e.g. 5).
\r
131 +A list that is recursively evaluated (e.g. `(:concat FORM1
\r
134 +A function that is evaluated with a single argument, the current
\r
137 +A symbol corresponding to an attribute of the
\r
138 + thread. Currently available attributes include:
\r
140 + :date_relative -- a user-readable rendering of the Date: header
\r
141 + of the first matching message in the thread, as a string.
\r
143 + :timestamp -- the number of seconds since the Epoch, 1970-01-01
\r
144 + 00:00:00 +0000 (UTC), corresponding to the Date: header of the
\r
145 + first matching message in the thread.
\r
147 + :subject -- the subject of the first matching message in the
\r
148 + thread as a string.
\r
150 + :authors -- a comma separated string containing a list of the
\r
151 + authors of messages in the thread. If there are non-matching
\r
152 + authors (i.e. the thread contains messages which did not match
\r
153 + the search terms and those messages have authors who are not
\r
154 + also authors of messages that did match the search terms) then
\r
155 + the matching and non-matching authors are separated by a `|'
\r
156 + symbol in the results in place of a comma.
\r
158 + :matched -- the number of messages that matched search terms in
\r
159 + the thread (a number).
\r
161 + :total -- the total number of messages in the thread (i.e. the
\r
162 + count of both matching and non-matching messages).
\r
164 + :tags -- a list of tags associated with messages in the
\r
165 + thread. Each tag is included as a distinct string.
\r
167 + :orig-tags -- a list of tags that were associated with messages
\r
168 + in the thread when the search originally took place.
\r
170 + :query -- a list with two elements. The first element is a query
\r
171 + string that will return all of the matching messages in the
\r
172 + current thread. The second element is a query string that will
\r
173 + return all of the non-matching messages in the current thread.
\r
175 + :thread -- the thread id of the matching thread, as a string.
\r
177 + For a full list of the attributes available, see the source code
\r
178 + for notmuch itself.
\r
180 +A directive that describes how to evaluate the remainder of the
\r
181 + list. The following directives are available:
\r
183 + `:concat FORM...' -- evaluate each element of FORM and return
\r
184 + the concatenated results.
\r
186 + `:width WIDTH FORM' -- evaluate FORM and return the result
\r
187 + truncated to WIDTH characters. The result will be padded with
\r
188 + spaces to WIDTH characters, with padding at on the left if the
\r
189 + WIDTH is negative.
\r
191 + `:tag TAG FORM' -- if the thread has the tag TAG, insert the
\r
192 + result of evaluating FORM.
\r
194 + `:eval COMPLEX-FORM' -- evaluate COMPLEX-FORM as emacs lisp and
\r
195 + evaluate the result.
\r
197 +During the evaluation of `notmuch-search-result-format', the
\r
198 +symbol `notmuch-thread' is bound to the current thread (a
\r
199 +property list), which can then be used in `:eval' forms."
\r
201 :group 'notmuch-search)
\r
203 ;; The name of this variable `notmuch-init-file' is consistent with the
\r
204 @@ -672,109 +752,84 @@ foreground and blue background."
\r
205 ;; Reverse the list so earlier entries take precedence
\r
206 (reverse notmuch-search-line-faces)))
\r
208 -(defun notmuch-search-author-propertize (authors)
\r
209 - "Split `authors' into matching and non-matching authors and
\r
210 -propertize appropriately. If no boundary between authors and
\r
211 -non-authors is found, assume that all of the authors match."
\r
212 - (if (string-match "\\(.*\\)|\\(.*\\)" authors)
\r
213 - (concat (propertize (concat (match-string 1 authors) ",")
\r
214 - 'face 'notmuch-search-matching-authors)
\r
215 - (propertize (match-string 2 authors)
\r
216 - 'face 'notmuch-search-non-matching-authors))
\r
217 - (propertize authors 'face 'notmuch-search-matching-authors)))
\r
219 -(defun notmuch-search-insert-authors (format-string authors)
\r
220 - ;; Save the match data to avoid interfering with
\r
221 - ;; `notmuch-search-process-filter'.
\r
223 - (let* ((formatted-authors (format format-string authors))
\r
224 - (formatted-sample (format format-string ""))
\r
225 - (visible-string formatted-authors)
\r
226 - (invisible-string "")
\r
229 - ;; Truncate the author string to fit the specification.
\r
230 - (if (> (length formatted-authors)
\r
231 - (length formatted-sample))
\r
232 - (let ((visible-length (- (length formatted-sample)
\r
233 - (length "... "))))
\r
234 - ;; Truncate the visible string according to the width of
\r
235 - ;; the display string.
\r
236 - (setq visible-string (substring formatted-authors 0 visible-length)
\r
237 - invisible-string (substring formatted-authors visible-length))
\r
238 - ;; If possible, truncate the visible string at a natural
\r
239 - ;; break (comma or pipe), as incremental search doesn't
\r
240 - ;; match across the visible/invisible border.
\r
241 - (when (string-match "\\(.*\\)\\([,|] \\)\\([^,|]*\\)" visible-string)
\r
242 - ;; Second clause is destructive on `visible-string', so
\r
243 - ;; order is important.
\r
244 - (setq invisible-string (concat (match-string 3 visible-string)
\r
245 - invisible-string)
\r
246 - visible-string (concat (match-string 1 visible-string)
\r
247 - (match-string 2 visible-string))))
\r
248 - ;; `visible-string' may be shorter than the space allowed
\r
249 - ;; by `format-string'. If so we must insert some padding
\r
250 - ;; after `invisible-string'.
\r
251 - (setq padding (make-string (- (length formatted-sample)
\r
252 - (length visible-string)
\r
256 - ;; Use different faces to show matching and non-matching authors.
\r
257 - (if (string-match "\\(.*\\)|\\(.*\\)" visible-string)
\r
258 - ;; The visible string contains both matching and
\r
259 - ;; non-matching authors.
\r
260 - (setq visible-string (notmuch-search-author-propertize visible-string)
\r
261 - ;; The invisible string must contain only non-matching
\r
262 - ;; authors, as the visible-string contains both.
\r
263 - invisible-string (propertize invisible-string
\r
264 - 'face 'notmuch-search-non-matching-authors))
\r
265 - ;; The visible string contains only matching authors.
\r
266 - (setq visible-string (propertize visible-string
\r
267 - 'face 'notmuch-search-matching-authors)
\r
268 - ;; The invisible string may contain both matching and
\r
269 - ;; non-matching authors.
\r
270 - invisible-string (notmuch-search-author-propertize invisible-string)))
\r
272 - ;; If there is any invisible text, add it as a tooltip to the
\r
274 - (when (not (string= invisible-string ""))
\r
275 - (setq visible-string (propertize visible-string 'help-echo (concat "..." invisible-string))))
\r
277 - ;; Insert the visible and, if present, invisible author strings.
\r
278 - (insert visible-string)
\r
279 - (when (not (string= invisible-string ""))
\r
280 - (let ((start (point))
\r
282 - (insert invisible-string)
\r
283 - (setq overlay (make-overlay start (point)))
\r
284 - (overlay-put overlay 'invisible 'ellipsis)
\r
285 - (overlay-put overlay 'isearch-open-invisible #'delete-overlay)))
\r
286 - (insert padding))))
\r
288 -(defun notmuch-search-insert-field (field format-string result)
\r
289 +(defun notmuch-search-format-authors (matched non-matched)
\r
290 + (if (string-equal "" non-matched)
\r
291 + (propertize matched 'face 'notmuch-search-matching-authors)
\r
292 + (concat (propertize (concat matched ", ")
\r
293 + 'face 'notmuch-search-matching-authors)
\r
294 + (propertize non-matched
\r
295 + 'face 'notmuch-search-non-matching-authors))))
\r
297 +(defun notmuch-search-make-width (width string)
\r
298 + (let ((neg (< 0 width))
\r
299 + (width (abs width))
\r
300 + (orig-len (length string)))
\r
302 + (if (< width orig-len)
\r
303 + ;; A sub-set of the string will be visible.
\r
304 + (let* ((ellipsis "...")
\r
305 + (visible-len (- width (length ellipsis)))
\r
306 + (visible-string (substring string 0 visible-len))
\r
307 + (invisible-string (substring string visible-len)))
\r
309 + (concat (propertize (concat visible-string
\r
310 + ;; The ellipsis should share the
\r
311 + ;; face of the preceding
\r
313 + (propertize ellipsis 'face
\r
314 + (get-text-property visible-len 'face string)))
\r
315 + 'help-echo (concat ellipsis invisible-string))))
\r
317 + ;; All of the string is visible - pad it.
\r
318 + (concat (if neg string)
\r
319 + (make-string (- width orig-len) ? )
\r
320 + (if neg "" string)))))
\r
322 +(defun notmuch-search-elem-repr (elem thread)
\r
324 - ((string-equal field "date")
\r
325 - (insert (propertize (format format-string (plist-get result :date_relative))
\r
326 - 'face 'notmuch-search-date)))
\r
327 - ((string-equal field "count")
\r
328 - (insert (propertize (format format-string
\r
329 - (format "[%s/%s]" (plist-get result :matched)
\r
330 - (plist-get result :total)))
\r
331 - 'face 'notmuch-search-count)))
\r
332 - ((string-equal field "subject")
\r
333 - (insert (propertize (format format-string
\r
334 - (notmuch-sanitize (plist-get result :subject)))
\r
335 - 'face 'notmuch-search-subject)))
\r
337 - ((string-equal field "authors")
\r
338 - (notmuch-search-insert-authors
\r
339 - format-string (notmuch-sanitize (plist-get result :authors))))
\r
341 - ((string-equal field "tags")
\r
342 - (let ((tags (plist-get result :tags))
\r
343 - (orig-tags (plist-get result :orig-tags)))
\r
344 - (insert (format format-string (notmuch-tag-format-tags tags orig-tags)))))))
\r
349 + (format "%d" elem))
\r
354 + ((functionp elem)
\r
355 + (funcall elem thread))
\r
358 + (let ((op (car elem))
\r
359 + (rest (cdr elem)))
\r
362 + (mapconcat (lambda (inner-elem)
\r
363 + (notmuch-search-elem-repr inner-elem thread))
\r
367 + (notmuch-search-make-width (car rest)
\r
368 + (notmuch-search-elem-repr (cadr rest) thread)))
\r
371 + (when (member (car rest) (plist-get thread :tags))
\r
372 + (notmuch-search-elem-repr (cadr rest) thread)))
\r
375 + (notmuch-search-elem-repr (apply #'eval rest) thread))
\r
379 + (lambda (inner-elem) (notmuch-search-elem-repr inner-elem thread))
\r
383 + (let ((val (plist-get thread elem)))
\r
385 + (notmuch-search-elem-repr val thread)
\r
386 + (message "Unknown message attribute in `notmuch-search-elem-repr': %s" elem)
\r
389 (defun notmuch-search-show-result (result pos)
\r
390 "Insert RESULT at POS."
\r
391 @@ -782,9 +837,10 @@ non-authors is found, assume that all of the authors match."
\r
392 (unless (= (plist-get result :matched) 0)
\r
395 - (dolist (spec notmuch-search-result-format)
\r
396 - (notmuch-search-insert-field (car spec) (cdr spec) result))
\r
398 + ;; `notmuch-thread' is a well known symbol for functions to
\r
399 + ;; use during evaluation of `notmuch-search-result-format'.
\r
400 + (insert (let ((notmuch-thread result))
\r
401 + (notmuch-search-elem-repr notmuch-search-result-format notmuch-thread)))
\r
402 (notmuch-search-color-line pos (point) (plist-get result :tags))
\r
403 (put-text-property pos (point) 'notmuch-search-result result))))
\r