]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-show.el
c8371e7539f1e19054fecc395509b97b5fa3b8f0
[notmuch] / emacs / notmuch-show.el
1 ;; notmuch-show.el --- displaying notmuch forests.
2 ;;
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
5 ;;
6 ;; This file is part of Notmuch.
7 ;;
8 ;; Notmuch is free software: you can redistribute it and/or modify it
9 ;; under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12 ;;
13 ;; Notmuch is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ;; General Public License for more details.
17 ;;
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.
20 ;;
21 ;; Authors: Carl Worth <cworth@cworth.org>
22 ;;          David Edmondson <dme@dme.org>
23
24 (eval-when-compile (require 'cl))
25 (require 'mm-view)
26 (require 'message)
27 (require 'mm-decode)
28 (require 'mailcap)
29 (require 'icalendar)
30 (require 'goto-addr)
31
32 (require 'notmuch-lib)
33 (require 'notmuch-query)
34 (require 'notmuch-wash)
35 (require 'notmuch-mua)
36 (require 'notmuch-crypto)
37 (require 'notmuch-print)
38
39 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
40 (declare-function notmuch-fontify-headers "notmuch" nil)
41 (declare-function notmuch-read-tag-changes "notmuch" (&optional initial-input &rest search-terms))
42 (declare-function notmuch-search-next-thread "notmuch" nil)
43 (declare-function notmuch-search-show-thread "notmuch" nil)
44 (declare-function notmuch-update-tags "notmuch" (current-tags tag-changes))
45
46 (defcustom notmuch-message-headers '("Subject" "To" "Cc" "Date")
47   "Headers that should be shown in a message, in this order.
48
49 For an open message, all of these headers will be made visible
50 according to `notmuch-message-headers-visible' or can be toggled
51 with `notmuch-show-toggle-headers'. For a closed message, only
52 the first header in the list will be visible."
53   :type '(repeat string)
54   :group 'notmuch-show)
55
56 (defcustom notmuch-message-headers-visible t
57   "Should the headers be visible by default?
58
59 If this value is non-nil, then all of the headers defined in
60 `notmuch-message-headers' will be visible by default in the display
61 of each message. Otherwise, these headers will be hidden and
62 `notmuch-show-toggle-headers' can be used to make the visible for
63 any given message."
64   :type 'boolean
65   :group 'notmuch-show)
66
67 (defcustom notmuch-show-relative-dates t
68   "Display relative dates in the message summary line."
69   :type 'boolean
70   :group 'notmuch-show)
71
72 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
73   "A list of functions called to decorate the headers listed in
74 `notmuch-message-headers'.")
75
76 (defcustom notmuch-show-hook '(notmuch-show-turn-on-visual-line-mode)
77   "Functions called after populating a `notmuch-show' buffer."
78   :type 'hook
79   :options '(notmuch-show-turn-on-visual-line-mode)
80   :group 'notmuch-show
81   :group 'notmuch-hooks)
82
83 (defcustom notmuch-show-insert-text/plain-hook '(notmuch-wash-wrap-long-lines
84                                                  notmuch-wash-tidy-citations
85                                                  notmuch-wash-elide-blank-lines
86                                                  notmuch-wash-excerpt-citations)
87   "Functions used to improve the display of text/plain parts."
88   :type 'hook
89   :options '(notmuch-wash-convert-inline-patch-to-part
90              notmuch-wash-wrap-long-lines
91              notmuch-wash-tidy-citations
92              notmuch-wash-elide-blank-lines
93              notmuch-wash-excerpt-citations)
94   :group 'notmuch-show
95   :group 'notmuch-hooks)
96
97 ;; Mostly useful for debugging.
98 (defcustom notmuch-show-all-multipart/alternative-parts t
99   "Should all parts of multipart/alternative parts be shown?"
100   :type 'boolean
101   :group 'notmuch-show)
102
103 (defcustom notmuch-show-indent-messages-width 1
104   "Width of message indentation in threads.
105
106 Messages are shown indented according to their depth in a thread.
107 This variable determines the width of this indentation measured
108 in number of blanks.  Defaults to `1', choose `0' to disable
109 indentation."
110   :type 'integer
111   :group 'notmuch-show)
112
113 (defcustom notmuch-show-indent-multipart nil
114   "Should the sub-parts of a multipart/* part be indented?"
115   ;; dme: Not sure which is a good default.
116   :type 'boolean
117   :group 'notmuch-show)
118
119 (defcustom notmuch-show-part-button-default-action 'notmuch-show-save-part
120   "Default part header button action (on ENTER or mouse click)."
121   :group 'notmuch-show
122   :type '(choice (const :tag "Save part"
123                         notmuch-show-save-part)
124                  (const :tag "View part"
125                         notmuch-show-view-part)
126                  (const :tag "View interactively"
127                         notmuch-show-interactively-view-part)))
128
129 (defvar notmuch-show-thread-id nil)
130 (make-variable-buffer-local 'notmuch-show-thread-id)
131 (put 'notmuch-show-thread-id 'permanent-local t)
132
133 (defvar notmuch-show-parent-buffer nil)
134 (make-variable-buffer-local 'notmuch-show-parent-buffer)
135 (put 'notmuch-show-parent-buffer 'permanent-local t)
136
137 (defvar notmuch-show-query-context nil)
138 (make-variable-buffer-local 'notmuch-show-query-context)
139 (put 'notmuch-show-query-context 'permanent-local t)
140
141 (defvar notmuch-show-process-crypto nil)
142 (make-variable-buffer-local 'notmuch-show-process-crypto)
143 (put 'notmuch-show-process-crypto 'permanent-local t)
144
145 (defvar notmuch-show-elide-non-matching-messages nil)
146 (make-variable-buffer-local 'notmuch-show-elide-non-matching-messages)
147 (put 'notmuch-show-elide-non-matching-messages 'permanent-local t)
148
149 (defmacro with-current-notmuch-show-message (&rest body)
150   "Evaluate body with current buffer set to the text of current message"
151   `(save-excursion
152      (let ((id (notmuch-show-get-message-id)))
153        (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
154          (with-current-buffer buf
155             (call-process notmuch-command nil t nil "show" "--format=raw" id)
156            ,@body)
157          (kill-buffer buf)))))
158
159 (defun notmuch-show-turn-on-visual-line-mode ()
160   "Enable Visual Line mode."
161   (visual-line-mode t))
162
163 (defun notmuch-show-view-all-mime-parts ()
164   "Use external viewers to view all attachments from the current message."
165   (interactive)
166   (with-current-notmuch-show-message
167    ;; We override the mm-inline-media-tests to indicate which message
168    ;; parts are already sufficiently handled by the original
169    ;; presentation of the message in notmuch-show mode. These parts
170    ;; will be inserted directly into the temporary buffer of
171    ;; with-current-notmuch-show-message and silently discarded.
172    ;;
173    ;; Any MIME part not explicitly mentioned here will be handled by an
174    ;; external viewer as configured in the various mailcap files.
175    (let ((mm-inline-media-tests '(
176                                   ("text/.*" ignore identity)
177                                   ("application/pgp-signature" ignore identity)
178                                   ("multipart/alternative" ignore identity)
179                                   ("multipart/mixed" ignore identity)
180                                   ("multipart/related" ignore identity)
181                                  )))
182      (mm-display-parts (mm-dissect-buffer)))))
183
184 (defun notmuch-foreach-mime-part (function mm-handle)
185   (cond ((stringp (car mm-handle))
186          (dolist (part (cdr mm-handle))
187            (notmuch-foreach-mime-part function part)))
188         ((bufferp (car mm-handle))
189          (funcall function mm-handle))
190         (t (dolist (part mm-handle)
191              (notmuch-foreach-mime-part function part)))))
192
193 (defun notmuch-count-attachments (mm-handle)
194   (let ((count 0))
195     (notmuch-foreach-mime-part
196      (lambda (p)
197        (let ((disposition (mm-handle-disposition p)))
198          (and (listp disposition)
199               (or (equal (car disposition) "attachment")
200                   (and (equal (car disposition) "inline")
201                        (assq 'filename disposition)))
202               (incf count))))
203      mm-handle)
204     count))
205
206 (defun notmuch-save-attachments (mm-handle &optional queryp)
207   (notmuch-foreach-mime-part
208    (lambda (p)
209      (let ((disposition (mm-handle-disposition p)))
210        (and (listp disposition)
211             (or (equal (car disposition) "attachment")
212                 (and (equal (car disposition) "inline")
213                      (assq 'filename disposition)))
214             (or (not queryp)
215                 (y-or-n-p
216                  (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
217             (mm-save-part p))))
218    mm-handle))
219
220 (defun notmuch-show-save-attachments ()
221   "Save all attachments from the current message."
222   (interactive)
223   (with-current-notmuch-show-message
224    (let ((mm-handle (mm-dissect-buffer)))
225      (notmuch-save-attachments
226       mm-handle (> (notmuch-count-attachments mm-handle) 1))))
227   (message "Done"))
228
229 (defun notmuch-show-with-message-as-text (fn)
230   "Apply FN to a text representation of the current message.
231
232 FN is called with one argument, the message properties. It should
233 operation on the contents of the current buffer."
234
235   ;; Remake the header to ensure that all information is available.
236   (let* ((to (notmuch-show-get-to))
237          (cc (notmuch-show-get-cc))
238          (from (notmuch-show-get-from))
239          (subject (notmuch-show-get-subject))
240          (date (notmuch-show-get-date))
241          (tags (notmuch-show-get-tags))
242          (depth (notmuch-show-get-depth))
243
244          (header (concat
245                   "Subject: " subject "\n"
246                   "To: " to "\n"
247                   (if (not (string= cc ""))
248                       (concat "Cc: " cc "\n")
249                     "")
250                   "From: " from "\n"
251                   "Date: " date "\n"
252                   (if tags
253                       (concat "Tags: "
254                               (mapconcat #'identity tags ", ") "\n")
255                     "")))
256          (all (buffer-substring (notmuch-show-message-top)
257                                 (notmuch-show-message-bottom)))
258
259          (props (notmuch-show-get-message-properties)))
260     (with-temp-buffer
261       (insert all)
262       (indent-rigidly (point-min) (point-max) (- depth))
263       ;; Remove the original header.
264       (goto-char (point-min))
265       (re-search-forward "^$" (point-max) nil)
266       (delete-region (point-min) (point))
267       (insert header)
268       (funcall fn props))))
269
270 (defun notmuch-show-print-message ()
271   "Print the current message."
272   (interactive)
273   (notmuch-show-with-message-as-text 'notmuch-print-message))
274
275 (defun notmuch-show-fontify-header ()
276   (let ((face (cond
277                ((looking-at "[Tt]o:")
278                 'message-header-to)
279                ((looking-at "[Bb]?[Cc][Cc]:")
280                 'message-header-cc)
281                ((looking-at "[Ss]ubject:")
282                 'message-header-subject)
283                ((looking-at "[Ff]rom:")
284                 'message-header-from)
285                (t
286                 'message-header-other))))
287
288     (overlay-put (make-overlay (point) (re-search-forward ":"))
289                  'face 'message-header-name)
290     (overlay-put (make-overlay (point) (re-search-forward ".*$"))
291                  'face face)))
292
293 (defun notmuch-show-colour-headers ()
294   "Apply some colouring to the current headers."
295   (goto-char (point-min))
296   (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
297     (notmuch-show-fontify-header)
298     (forward-line)))
299
300 (defun notmuch-show-spaces-n (n)
301   "Return a string comprised of `n' spaces."
302   (make-string n ? ))
303
304 (defun notmuch-show-update-tags (tags)
305   "Update the displayed tags of the current message."
306   (save-excursion
307     (goto-char (notmuch-show-message-top))
308     (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
309         (let ((inhibit-read-only t))
310           (replace-match (concat "("
311                                  (propertize (mapconcat 'identity tags " ")
312                                              'face 'notmuch-tag-face)
313                                  ")"))))))
314
315 (defun notmuch-show-clean-address (address)
316   "Try to clean a single email ADDRESS for display.  Return
317 unchanged ADDRESS if parsing fails."
318   (condition-case nil
319     (let (p-name p-address)
320       ;; It would be convenient to use `mail-header-parse-address',
321       ;; but that expects un-decoded mailbox parts, whereas our
322       ;; mailbox parts are already decoded (and hence may contain
323       ;; UTF-8). Given that notmuch should handle most of the awkward
324       ;; cases, some simple string deconstruction should be sufficient
325       ;; here.
326       (cond
327        ;; "User <user@dom.ain>" style.
328        ((string-match "\\(.*\\) <\\(.*\\)>" address)
329         (setq p-name (match-string 1 address)
330               p-address (match-string 2 address)))
331
332        ;; "<user@dom.ain>" style.
333        ((string-match "<\\(.*\\)>" address)
334         (setq p-address (match-string 1 address)))
335
336        ;; Everything else.
337        (t
338         (setq p-address address)))
339
340       (when p-name
341         ;; Remove elements of the mailbox part that are not relevant for
342         ;; display, even if they are required during transport:
343         ;;
344         ;; Backslashes.
345         (setq p-name (replace-regexp-in-string "\\\\" "" p-name))
346
347         ;; Outer single and double quotes, which might be nested.
348         (loop
349          with start-of-loop
350          do (setq start-of-loop p-name)
351
352          when (string-match "^\"\\(.*\\)\"$" p-name)
353          do (setq p-name (match-string 1 p-name))
354
355          when (string-match "^'\\(.*\\)'$" p-name)
356          do (setq p-name (match-string 1 p-name))
357
358          until (string= start-of-loop p-name)))
359
360       ;; If the address is 'foo@bar.com <foo@bar.com>' then show just
361       ;; 'foo@bar.com'.
362       (when (string= p-name p-address)
363         (setq p-name nil))
364
365       ;; If no name results, return just the address.
366       (if (not p-name)
367           p-address
368         ;; Otherwise format the name and address together.
369         (concat p-name " <" p-address ">")))
370     (error address)))
371
372 (defun notmuch-show-insert-headerline (headers date tags depth)
373   "Insert a notmuch style headerline based on HEADERS for a
374 message at DEPTH in the current thread."
375   (let ((start (point)))
376     (insert (notmuch-show-spaces-n (* notmuch-show-indent-messages-width depth))
377             (notmuch-show-clean-address (plist-get headers :From))
378             " ("
379             date
380             ") ("
381             (propertize (mapconcat 'identity tags " ")
382                         'face 'notmuch-tag-face)
383             ")\n")
384     (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
385
386 (defun notmuch-show-insert-header (header header-value)
387   "Insert a single header."
388   (insert header ": " header-value "\n"))
389
390 (defun notmuch-show-insert-headers (headers)
391   "Insert the headers of the current message."
392   (let ((start (point)))
393     (mapc (lambda (header)
394             (let* ((header-symbol (intern (concat ":" header)))
395                    (header-value (plist-get headers header-symbol)))
396               (if (and header-value
397                        (not (string-equal "" header-value)))
398                   (notmuch-show-insert-header header header-value))))
399           notmuch-message-headers)
400     (save-excursion
401       (save-restriction
402         (narrow-to-region start (point-max))
403         (run-hooks 'notmuch-show-markup-headers-hook)))))
404
405 (define-button-type 'notmuch-show-part-button-type
406   'action 'notmuch-show-part-button-default
407   'keymap 'notmuch-show-part-button-map
408   'follow-link t
409   'face 'message-mml)
410
411 (defvar notmuch-show-part-button-map
412   (let ((map (make-sparse-keymap)))
413     (set-keymap-parent map button-map)
414     (define-key map "s" 'notmuch-show-part-button-save)
415     (define-key map "v" 'notmuch-show-part-button-view)
416     (define-key map "o" 'notmuch-show-part-button-interactively-view)
417     map)
418   "Submap for button commands")
419 (fset 'notmuch-show-part-button-map notmuch-show-part-button-map)
420
421 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name comment)
422   (let ((button))
423     (setq button
424           (insert-button
425            (concat "[ "
426                    (if name (concat name ": ") "")
427                    declared-type
428                    (if (not (string-equal declared-type content-type))
429                        (concat " (as " content-type ")")
430                      "")
431                    (or comment "")
432                    " ]")
433            :type 'notmuch-show-part-button-type
434            :notmuch-part nth
435            :notmuch-filename name
436            :notmuch-content-type content-type))
437     (insert "\n")
438     ;; return button
439     button))
440
441 ;; Functions handling particular MIME parts.
442
443 (defmacro notmuch-with-temp-part-buffer (message-id nth &rest body)
444   (declare (indent 2))
445   (let ((process-crypto (make-symbol "process-crypto")))
446     `(let ((,process-crypto notmuch-show-process-crypto))
447        (with-temp-buffer
448          (setq notmuch-show-process-crypto ,process-crypto)
449          ;; Always acquires the part via `notmuch part', even if it is
450          ;; available in the JSON output.
451          (insert (notmuch-show-get-bodypart-internal ,message-id ,nth))
452          ,@body))))
453
454 (defun notmuch-show-save-part (message-id nth &optional filename content-type)
455   (notmuch-with-temp-part-buffer message-id nth
456     (let ((file (read-file-name
457                  "Filename to save as: "
458                  (or mailcap-download-directory "~/")
459                  nil nil
460                  filename)))
461       ;; Don't re-compress .gz & al.  Arguably we should make
462       ;; `file-name-handler-alist' nil, but that would chop
463       ;; ange-ftp, which is reasonable to use here.
464       (mm-write-region (point-min) (point-max) file nil nil nil 'no-conversion t))))
465
466 (defun notmuch-show-view-part (message-id nth &optional filename content-type )
467   (notmuch-with-temp-part-buffer message-id nth
468     ;; set mm-inlined-types to nil to force an external viewer
469     (let ((handle (mm-make-handle (current-buffer) (list content-type)))
470           (mm-inlined-types nil))
471       ;; We override mm-save-part as notmuch-show-save-part is better
472       ;; since it offers the filename. We need to lexically bind
473       ;; everything we need for notmuch-show-save-part to prevent
474       ;; potential dynamic shadowing.
475       (lexical-let ((message-id message-id)
476                     (nth nth)
477                     (filename filename)
478                     (content-type content-type))
479         (flet ((mm-save-part (&rest args) (notmuch-show-save-part
480                                            message-id nth filename content-type)))
481           (mm-display-part handle))))))
482
483 (defun notmuch-show-interactively-view-part (message-id nth &optional filename content-type)
484   (notmuch-with-temp-part-buffer message-id nth
485     (let ((handle (mm-make-handle (current-buffer) (list content-type))))
486       (mm-interactively-view-part handle))))
487
488 (defun notmuch-show-mm-display-part-inline (msg part nth content-type)
489   "Use the mm-decode/mm-view functions to display a part in the
490 current buffer, if possible."
491   (let ((display-buffer (current-buffer)))
492     (with-temp-buffer
493       (let* ((charset (plist-get part :content-charset))
494              (handle (mm-make-handle (current-buffer) `(,content-type (charset . ,charset)))))
495         ;; If the user wants the part inlined, insert the content and
496         ;; test whether we are able to inline it (which includes both
497         ;; capability and suitability tests).
498         (when (mm-inlined-p handle)
499           (insert (notmuch-show-get-bodypart-content msg part nth))
500           (when (mm-inlinable-p handle)
501             (set-buffer display-buffer)
502             (mm-display-part handle)
503             t))))))
504
505 (defvar notmuch-show-multipart/alternative-discouraged
506   '(
507     ;; Avoid HTML parts.
508     "text/html"
509     ;; multipart/related usually contain a text/html part and some associated graphics.
510     "multipart/related"
511     ))
512
513 (defun notmuch-show-multipart/*-to-list (part)
514   (mapcar (lambda (inner-part) (plist-get inner-part :content-type))
515           (plist-get part :content)))
516
517 (defun notmuch-show-multipart/alternative-choose (types)
518   ;; Based on `mm-preferred-alternative-precedence'.
519   (let ((seq types))
520     (dolist (pref (reverse notmuch-show-multipart/alternative-discouraged))
521       (dolist (elem (copy-sequence seq))
522         (when (string-match pref elem)
523           (setq seq (nconc (delete elem seq) (list elem))))))
524     seq))
525
526 (defun notmuch-show-insert-part-multipart/alternative (msg part content-type nth depth declared-type)
527   (notmuch-show-insert-part-header nth declared-type content-type nil)
528   (let ((chosen-type (car (notmuch-show-multipart/alternative-choose (notmuch-show-multipart/*-to-list part))))
529         (inner-parts (plist-get part :content))
530         (start (point)))
531     ;; This inserts all parts of the chosen type rather than just one,
532     ;; but it's not clear that this is the wrong thing to do - which
533     ;; should be chosen if there are more than one that match?
534     (mapc (lambda (inner-part)
535             (let ((inner-type (plist-get inner-part :content-type)))
536               (if (or notmuch-show-all-multipart/alternative-parts
537                       (string= chosen-type inner-type))
538                   (notmuch-show-insert-bodypart msg inner-part depth)
539                 (notmuch-show-insert-part-header (plist-get inner-part :id) inner-type inner-type nil " (not shown)"))))
540           inner-parts)
541
542     (when notmuch-show-indent-multipart
543       (indent-rigidly start (point) 1)))
544   t)
545
546 (defun notmuch-show-setup-w3m ()
547   "Instruct w3m how to retrieve content from a \"related\" part of a message."
548   (interactive)
549   (if (boundp 'w3m-cid-retrieve-function-alist)
550     (unless (assq 'notmuch-show-mode w3m-cid-retrieve-function-alist)
551       (push (cons 'notmuch-show-mode 'notmuch-show-w3m-cid-retrieve)
552             w3m-cid-retrieve-function-alist)))
553   (setq mm-inline-text-html-with-images t))
554
555 (defvar w3m-current-buffer) ;; From `w3m.el'.
556 (defvar notmuch-show-w3m-cid-store nil)
557 (make-variable-buffer-local 'notmuch-show-w3m-cid-store)
558
559 (defun notmuch-show-w3m-cid-store-internal (content-id
560                                             message-id
561                                             part-number
562                                             content-type
563                                             content)
564   (push (list content-id
565               message-id
566               part-number
567               content-type
568               content)
569         notmuch-show-w3m-cid-store))
570
571 (defun notmuch-show-w3m-cid-store (msg part)
572   (let ((content-id (plist-get part :content-id)))
573     (when content-id
574       (notmuch-show-w3m-cid-store-internal (concat "cid:" content-id)
575                                            (plist-get msg :id)
576                                            (plist-get part :id)
577                                            (plist-get part :content-type)
578                                            nil))))
579
580 (defun notmuch-show-w3m-cid-retrieve (url &rest args)
581   (let ((matching-part (with-current-buffer w3m-current-buffer
582                          (assoc url notmuch-show-w3m-cid-store))))
583     (if matching-part
584         (let ((message-id (nth 1 matching-part))
585               (part-number (nth 2 matching-part))
586               (content-type (nth 3 matching-part))
587               (content (nth 4 matching-part)))
588           ;; If we don't already have the content, get it and cache
589           ;; it, as some messages reference the same cid: part many
590           ;; times (hundreds!), which results in many calls to
591           ;; `notmuch part'.
592           (unless content
593             (setq content (notmuch-show-get-bodypart-internal (concat "id:" message-id)
594                                                               part-number))
595             (with-current-buffer w3m-current-buffer
596               (notmuch-show-w3m-cid-store-internal url
597                                                    message-id
598                                                    part-number
599                                                    content-type
600                                                    content)))
601           (insert content)
602           content-type)
603       nil)))
604
605 (defun notmuch-show-insert-part-multipart/related (msg part content-type nth depth declared-type)
606   (notmuch-show-insert-part-header nth declared-type content-type nil)
607   (let ((inner-parts (plist-get part :content))
608         (start (point)))
609
610     ;; We assume that the first part is text/html and the remainder
611     ;; things that it references.
612
613     ;; Stash the non-primary parts.
614     (mapc (lambda (part)
615             (notmuch-show-w3m-cid-store msg part))
616           (cdr inner-parts))
617
618     ;; Render the primary part.
619     (notmuch-show-insert-bodypart msg (car inner-parts) depth)
620
621     (when notmuch-show-indent-multipart
622       (indent-rigidly start (point) 1)))
623   t)
624
625 (defun notmuch-show-insert-part-multipart/signed (msg part content-type nth depth declared-type)
626   (let ((button (notmuch-show-insert-part-header nth declared-type content-type nil)))
627     (button-put button 'face 'notmuch-crypto-part-header)
628     ;; add signature status button if sigstatus provided
629     (if (plist-member part :sigstatus)
630         (let* ((from (notmuch-show-get-header :From msg))
631                (sigstatus (car (plist-get part :sigstatus))))
632           (notmuch-crypto-insert-sigstatus-button sigstatus from))
633       ;; if we're not adding sigstatus, tell the user how they can get it
634       (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts.")))
635
636   (let ((inner-parts (plist-get part :content))
637         (start (point)))
638     ;; Show all of the parts.
639     (mapc (lambda (inner-part)
640             (notmuch-show-insert-bodypart msg inner-part depth))
641           inner-parts)
642
643     (when notmuch-show-indent-multipart
644       (indent-rigidly start (point) 1)))
645   t)
646
647 (defun notmuch-show-insert-part-multipart/encrypted (msg part content-type nth depth declared-type)
648   (let ((button (notmuch-show-insert-part-header nth declared-type content-type nil)))
649     (button-put button 'face 'notmuch-crypto-part-header)
650     ;; add encryption status button if encstatus specified
651     (if (plist-member part :encstatus)
652         (let ((encstatus (car (plist-get part :encstatus))))
653           (notmuch-crypto-insert-encstatus-button encstatus)
654           ;; add signature status button if sigstatus specified
655           (if (plist-member part :sigstatus)
656               (let* ((from (notmuch-show-get-header :From msg))
657                      (sigstatus (car (plist-get part :sigstatus))))
658                 (notmuch-crypto-insert-sigstatus-button sigstatus from))))
659       ;; if we're not adding encstatus, tell the user how they can get it
660       (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts.")))
661
662   (let ((inner-parts (plist-get part :content))
663         (start (point)))
664     ;; Show all of the parts.
665     (mapc (lambda (inner-part)
666             (notmuch-show-insert-bodypart msg inner-part depth))
667           inner-parts)
668
669     (when notmuch-show-indent-multipart
670       (indent-rigidly start (point) 1)))
671   t)
672
673 (defun notmuch-show-insert-part-multipart/* (msg part content-type nth depth declared-type)
674   (notmuch-show-insert-part-header nth declared-type content-type nil)
675   (let ((inner-parts (plist-get part :content))
676         (start (point)))
677     ;; Show all of the parts.
678     (mapc (lambda (inner-part)
679             (notmuch-show-insert-bodypart msg inner-part depth))
680           inner-parts)
681
682     (when notmuch-show-indent-multipart
683       (indent-rigidly start (point) 1)))
684   t)
685
686 (defun notmuch-show-insert-part-message/rfc822 (msg part content-type nth depth declared-type)
687   (notmuch-show-insert-part-header nth declared-type content-type nil)
688   (let* ((message (car (plist-get part :content)))
689          (body (car (plist-get message :body)))
690          (start (point)))
691
692     ;; Override `notmuch-message-headers' to force `From' to be
693     ;; displayed.
694     (let ((notmuch-message-headers '("From" "Subject" "To" "Cc" "Date")))
695       (notmuch-show-insert-headers (plist-get message :headers)))
696
697     ;; Blank line after headers to be compatible with the normal
698     ;; message display.
699     (insert "\n")
700
701     ;; Show the body
702     (notmuch-show-insert-bodypart msg body depth)
703
704     (when notmuch-show-indent-multipart
705       (indent-rigidly start (point) 1)))
706   t)
707
708 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
709   (let ((start (point)))
710     ;; If this text/plain part is not the first part in the message,
711     ;; insert a header to make this clear.
712     (if (> nth 1)
713         (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
714     (insert (notmuch-show-get-bodypart-content msg part nth))
715     (save-excursion
716       (save-restriction
717         (narrow-to-region start (point-max))
718         (run-hook-with-args 'notmuch-show-insert-text/plain-hook msg depth))))
719   t)
720
721 (defun notmuch-show-insert-part-text/x-vcalendar (msg part content-type nth depth declared-type)
722   (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename))
723   (insert (with-temp-buffer
724             (insert (notmuch-show-get-bodypart-content msg part nth))
725             (goto-char (point-min))
726             (let ((file (make-temp-file "notmuch-ical"))
727                   result)
728               (icalendar--convert-ical-to-diary
729                (icalendar--read-element nil nil)
730                file t)
731               (set-buffer (get-file-buffer file))
732               (setq result (buffer-substring (point-min) (point-max)))
733               (set-buffer-modified-p nil)
734               (kill-buffer (current-buffer))
735               (delete-file file)
736               result)))
737   t)
738
739 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
740   ;; If we can deduce a MIME type from the filename of the attachment,
741   ;; do so and pass it on to the handler for that type.
742   (if (plist-get part :filename)
743       (let ((extension (file-name-extension (plist-get part :filename)))
744             mime-type)
745         (if extension
746             (progn
747               (mailcap-parse-mimetypes)
748               (setq mime-type (mailcap-extension-to-mime extension))
749               (if (and mime-type
750                        (not (string-equal mime-type "application/octet-stream")))
751                   (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
752                 nil))
753           nil))))
754
755 ;; Handler for wash generated inline patch fake parts.
756 (defun notmuch-show-insert-part-inline-patch-fake-part (msg part content-type nth depth declared-type)
757   (notmuch-show-insert-part-*/* msg part "text/x-diff" nth depth "inline patch"))
758
759 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
760   ;; This handler _must_ succeed - it is the handler of last resort.
761   (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
762   (notmuch-show-mm-display-part-inline msg part nth content-type)
763   t)
764
765 ;; Functions for determining how to handle MIME parts.
766
767 (defun notmuch-show-split-content-type (content-type)
768   (split-string content-type "/"))
769
770 (defun notmuch-show-handlers-for (content-type)
771   "Return a list of content handlers for a part of type CONTENT-TYPE."
772   (let (result)
773     (mapc (lambda (func)
774             (if (functionp func)
775                 (push func result)))
776           ;; Reverse order of prefrence.
777           (list (intern (concat "notmuch-show-insert-part-*/*"))
778                 (intern (concat
779                          "notmuch-show-insert-part-"
780                          (car (notmuch-show-split-content-type content-type))
781                          "/*"))
782                 (intern (concat "notmuch-show-insert-part-" content-type))))
783     result))
784
785 ;; Helper for parts which are generally not included in the default
786 ;; JSON output.
787 (defun notmuch-show-get-bodypart-internal (message-id part-number)
788   (let ((args '("show" "--format=raw"))
789         (part-arg (format "--part=%s" part-number)))
790     (setq args (append args (list part-arg)))
791     (if notmuch-show-process-crypto
792         (setq args (append args '("--decrypt"))))
793     (setq args (append args (list message-id)))
794     (with-temp-buffer
795       (let ((coding-system-for-read 'no-conversion))
796         (progn
797           (apply 'call-process (append (list notmuch-command nil (list t nil) nil) args))
798           (buffer-string))))))
799
800 (defun notmuch-show-get-bodypart-content (msg part nth)
801   (or (plist-get part :content)
802       (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
803
804 ;; \f
805
806 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
807   (let ((handlers (notmuch-show-handlers-for content-type)))
808     ;; Run the content handlers until one of them returns a non-nil
809     ;; value.
810     (while (and handlers
811                 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
812       (setq handlers (cdr handlers))))
813   t)
814
815 (defun notmuch-show-insert-bodypart (msg part depth)
816   "Insert the body part PART at depth DEPTH in the current thread."
817   (let ((content-type (downcase (plist-get part :content-type)))
818         (nth (plist-get part :id)))
819     (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
820   ;; Some of the body part handlers leave point somewhere up in the
821   ;; part, so we make sure that we're down at the end.
822   (goto-char (point-max))
823   ;; Ensure that the part ends with a carriage return.
824   (unless (bolp)
825     (insert "\n")))
826
827 (defun notmuch-show-insert-body (msg body depth)
828   "Insert the body BODY at depth DEPTH in the current thread."
829   (mapc (lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
830
831 (defun notmuch-show-make-symbol (type)
832   (make-symbol (concat "notmuch-show-" type)))
833
834 (defun notmuch-show-strip-re (string)
835   (replace-regexp-in-string "\\([Rr]e: *\\)+" "" string))
836
837 (defvar notmuch-show-previous-subject "")
838 (make-variable-buffer-local 'notmuch-show-previous-subject)
839
840 (defun notmuch-show-insert-msg (msg depth)
841   "Insert the message MSG at depth DEPTH in the current thread."
842   (let* ((headers (plist-get msg :headers))
843          ;; Indentation causes the buffer offset of the start/end
844          ;; points to move, so we must use markers.
845          message-start message-end
846          content-start content-end
847          headers-start headers-end
848          body-start body-end
849          (headers-invis-spec (notmuch-show-make-symbol "header"))
850          (message-invis-spec (notmuch-show-make-symbol "message"))
851          (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
852
853     ;; Set `buffer-invisibility-spec' to `nil' (a list), otherwise
854     ;; removing items from `buffer-invisibility-spec' (which is what
855     ;; `notmuch-show-headers-visible' and
856     ;; `notmuch-show-message-visible' do) is a no-op and has no
857     ;; effect. This caused threads with only matching messages to have
858     ;; those messages hidden initially because
859     ;; `buffer-invisibility-spec' stayed `t'.
860     ;;
861     ;; This needs to be set here (rather than just above the call to
862     ;; `notmuch-show-headers-visible') because some of the part
863     ;; rendering or body washing functions
864     ;; (e.g. `notmuch-wash-text/plain-citations') manipulate
865     ;; `buffer-invisibility-spec').
866     (when (eq buffer-invisibility-spec t)
867       (setq buffer-invisibility-spec nil))
868
869     (setq message-start (point-marker))
870
871     (notmuch-show-insert-headerline headers
872                                     (or (if notmuch-show-relative-dates
873                                             (plist-get msg :date_relative)
874                                           nil)
875                                         (plist-get headers :Date))
876                                     (plist-get msg :tags) depth)
877
878     (setq content-start (point-marker))
879
880     (plist-put msg :headers-invis-spec headers-invis-spec)
881     (plist-put msg :message-invis-spec message-invis-spec)
882
883     ;; Set `headers-start' to point after the 'Subject:' header to be
884     ;; compatible with the existing implementation. This just sets it
885     ;; to after the first header.
886     (notmuch-show-insert-headers headers)
887     (save-excursion
888       (goto-char content-start)
889       ;; If the subject of this message is the same as that of the
890       ;; previous message, don't display it when this message is
891       ;; collapsed.
892       (when (not (string= notmuch-show-previous-subject
893                           bare-subject))
894         (forward-line 1))
895       (setq headers-start (point-marker)))
896     (setq headers-end (point-marker))
897
898     (setq notmuch-show-previous-subject bare-subject)
899
900     (setq body-start (point-marker))
901     ;; A blank line between the headers and the body.
902     (insert "\n")
903     (notmuch-show-insert-body msg (plist-get msg :body) depth)
904     ;; Ensure that the body ends with a newline.
905     (unless (bolp)
906       (insert "\n"))
907     (setq body-end (point-marker))
908     (setq content-end (point-marker))
909
910     ;; Indent according to the depth in the thread.
911     (indent-rigidly content-start content-end (* notmuch-show-indent-messages-width depth))
912
913     (setq message-end (point-max-marker))
914
915     ;; Save the extents of this message over the whole text of the
916     ;; message.
917     (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
918
919     (let ((headers-overlay (make-overlay headers-start headers-end))
920           (invis-specs (list headers-invis-spec message-invis-spec)))
921       (overlay-put headers-overlay 'invisible invis-specs)
922       (overlay-put headers-overlay 'priority 10))
923     (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
924
925     (plist-put msg :depth depth)
926
927     ;; Save the properties for this message. Currently this saves the
928     ;; entire message (augmented it with other stuff), which seems
929     ;; like overkill. We might save a reduced subset (for example, not
930     ;; the content).
931     (notmuch-show-set-message-properties msg)
932
933     ;; Set header visibility.
934     (notmuch-show-headers-visible msg notmuch-message-headers-visible)
935
936     ;; Message visibility depends on whether it matched the search
937     ;; criteria.
938     (notmuch-show-message-visible msg (plist-get msg :match))))
939
940 (defun notmuch-show-toggle-process-crypto ()
941   "Toggle the processing of cryptographic MIME parts."
942   (interactive)
943   (setq notmuch-show-process-crypto (not notmuch-show-process-crypto))
944   (message (if notmuch-show-process-crypto
945                "Processing cryptographic MIME parts."
946              "Not processing cryptographic MIME parts."))
947   (notmuch-show-refresh-view))
948
949 (defun notmuch-show-toggle-elide-non-matching ()
950   "Toggle the display of non-matching messages."
951   (interactive)
952   (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages))
953   (message (if notmuch-show-elide-non-matching-messages
954                "Showing matching messages only."
955              "Showing all messages."))
956   (notmuch-show-refresh-view))
957
958 (defun notmuch-show-insert-tree (tree depth)
959   "Insert the message tree TREE at depth DEPTH in the current thread."
960   (let ((msg (car tree))
961         (replies (cadr tree)))
962     (if (or (not notmuch-show-elide-non-matching-messages)
963             (plist-get msg :match))
964         (notmuch-show-insert-msg msg depth))
965     (notmuch-show-insert-thread replies (1+ depth))))
966
967 (defun notmuch-show-insert-thread (thread depth)
968   "Insert the thread THREAD at depth DEPTH in the current forest."
969   (mapc (lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
970
971 (defun notmuch-show-insert-forest (forest)
972   "Insert the forest of threads FOREST."
973   (mapc (lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
974
975 (defun notmuch-show-buttonise-links (start end)
976   "Buttonise URLs and mail addresses between START and END.
977
978 This also turns id:\"<message id>\"-parts into buttons for
979 a corresponding notmuch search."
980   (goto-address-fontify-region start end)
981   (save-excursion
982     (goto-char start)
983     (while (re-search-forward "id:\\(\"?\\)[^[:space:]\"]+\\1" end t)
984       ;; remove the overlay created by goto-address-mode
985       (remove-overlays (match-beginning 0) (match-end 0) 'goto-address t)
986       (make-text-button (match-beginning 0) (match-end 0)
987                         'action `(lambda (arg)
988                                    (notmuch-show ,(match-string-no-properties 0)))
989                         'follow-link t
990                         'help-echo "Mouse-1, RET: search for this message"
991                         'face goto-address-mail-face))))
992
993 ;;;###autoload
994 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
995   "Run \"notmuch show\" with the given thread ID and display results.
996
997 The optional PARENT-BUFFER is the notmuch-search buffer from
998 which this notmuch-show command was executed, (so that the
999 next thread from that buffer can be show when done with this
1000 one).
1001
1002 The optional QUERY-CONTEXT is a notmuch search term. Only
1003 messages from the thread matching this search term are shown if
1004 non-nil.
1005
1006 The optional BUFFER-NAME provides the name of the buffer in
1007 which the message thread is shown. If it is nil (which occurs
1008 when the command is called interactively) the argument to the
1009 function is used."
1010   (interactive "sNotmuch show: ")
1011   (let ((buffer-name (generate-new-buffer-name
1012                       (or buffer-name
1013                           (concat "*notmuch-" thread-id "*")))))
1014     (switch-to-buffer (get-buffer-create buffer-name))
1015     ;; Set the default value for `notmuch-show-process-crypto' in this
1016     ;; buffer.
1017     (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1018
1019     (setq notmuch-show-thread-id thread-id
1020           notmuch-show-parent-buffer parent-buffer
1021           notmuch-show-query-context query-context)
1022     (notmuch-show-worker)))
1023
1024 (defun notmuch-show-worker ()
1025   (let ((inhibit-read-only t))
1026
1027     (notmuch-show-mode)
1028     ;; Don't track undo information for this buffer
1029     (set 'buffer-undo-list t)
1030
1031     (erase-buffer)
1032     (goto-char (point-min))
1033     (save-excursion
1034       (let* ((basic-args (list notmuch-show-thread-id))
1035              (args (if notmuch-show-query-context
1036                        (append (list "\'") basic-args
1037                                (list "and (" notmuch-show-query-context ")\'"))
1038                      (append (list "\'") basic-args (list "\'")))))
1039         (notmuch-show-insert-forest (notmuch-query-get-threads args))
1040         ;; If the query context reduced the results to nothing, run
1041         ;; the basic query.
1042         (when (and (eq (buffer-size) 0)
1043                    notmuch-show-query-context)
1044           (notmuch-show-insert-forest
1045            (notmuch-query-get-threads basic-args))))
1046
1047       (jit-lock-register #'notmuch-show-buttonise-links)
1048
1049       (run-hooks 'notmuch-show-hook))
1050
1051     ;; Move straight to the first open message
1052     (unless (notmuch-show-message-visible-p)
1053       (notmuch-show-next-open-message))
1054
1055     ;; Set the header line to the subject of the first open message.
1056     (setq header-line-format (notmuch-show-strip-re (notmuch-show-get-pretty-subject)))
1057
1058     (notmuch-show-mark-read)))
1059
1060 (defun notmuch-show-refresh-view ()
1061   "Refresh the current view.
1062
1063 Refreshes the current view, observing changes in cryptographic preferences."
1064   (interactive)
1065   (let ((inhibit-read-only t))
1066     (erase-buffer))
1067   (notmuch-show-worker))
1068
1069 (defvar notmuch-show-stash-map
1070   (let ((map (make-sparse-keymap)))
1071     (define-key map "c" 'notmuch-show-stash-cc)
1072     (define-key map "d" 'notmuch-show-stash-date)
1073     (define-key map "F" 'notmuch-show-stash-filename)
1074     (define-key map "f" 'notmuch-show-stash-from)
1075     (define-key map "i" 'notmuch-show-stash-message-id)
1076     (define-key map "I" 'notmuch-show-stash-message-id-stripped)
1077     (define-key map "s" 'notmuch-show-stash-subject)
1078     (define-key map "T" 'notmuch-show-stash-tags)
1079     (define-key map "t" 'notmuch-show-stash-to)
1080     map)
1081   "Submap for stash commands")
1082 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
1083
1084 (defvar notmuch-show-mode-map
1085       (let ((map (make-sparse-keymap)))
1086         (define-key map "?" 'notmuch-help)
1087         (define-key map "q" 'notmuch-kill-this-buffer)
1088         (define-key map (kbd "<C-tab>") 'widget-backward)
1089         (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
1090         (define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
1091         (define-key map (kbd "TAB") 'notmuch-show-next-button)
1092         (define-key map "s" 'notmuch-search)
1093         (define-key map "m" 'notmuch-mua-new-mail)
1094         (define-key map "f" 'notmuch-show-forward-message)
1095         (define-key map "r" 'notmuch-show-reply-sender)
1096         (define-key map "R" 'notmuch-show-reply)
1097         (define-key map "|" 'notmuch-show-pipe-message)
1098         (define-key map "w" 'notmuch-show-save-attachments)
1099         (define-key map "V" 'notmuch-show-view-raw-message)
1100         (define-key map "v" 'notmuch-show-view-all-mime-parts)
1101         (define-key map "c" 'notmuch-show-stash-map)
1102         (define-key map "=" 'notmuch-show-refresh-view)
1103         (define-key map "h" 'notmuch-show-toggle-headers)
1104         (define-key map "*" 'notmuch-show-tag-all)
1105         (define-key map "-" 'notmuch-show-remove-tag)
1106         (define-key map "+" 'notmuch-show-add-tag)
1107         (define-key map "X" 'notmuch-show-archive-thread-then-exit)
1108         (define-key map "x" 'notmuch-show-archive-message-then-next-or-exit)
1109         (define-key map "A" 'notmuch-show-archive-thread-then-next)
1110         (define-key map "a" 'notmuch-show-archive-message-then-next-or-next-thread)
1111         (define-key map "N" 'notmuch-show-next-message)
1112         (define-key map "P" 'notmuch-show-previous-message)
1113         (define-key map "n" 'notmuch-show-next-open-message)
1114         (define-key map "p" 'notmuch-show-previous-open-message)
1115         (define-key map (kbd "DEL") 'notmuch-show-rewind)
1116         (define-key map " " 'notmuch-show-advance-and-archive)
1117         (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
1118         (define-key map (kbd "RET") 'notmuch-show-toggle-message)
1119         (define-key map "#" 'notmuch-show-print-message)
1120         (define-key map "!" 'notmuch-show-toggle-elide-non-matching)
1121         (define-key map "$" 'notmuch-show-toggle-process-crypto)
1122         map)
1123       "Keymap for \"notmuch show\" buffers.")
1124 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
1125
1126 (defun notmuch-show-mode ()
1127   "Major mode for viewing a thread with notmuch.
1128
1129 This buffer contains the results of the \"notmuch show\" command
1130 for displaying a single thread of email from your email archives.
1131
1132 By default, various components of email messages, (citations,
1133 signatures, already-read messages), are hidden. You can make
1134 these parts visible by clicking with the mouse button or by
1135 pressing RET after positioning the cursor on a hidden part, (for
1136 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
1137
1138 Reading the thread sequentially is well-supported by pressing
1139 \\[notmuch-show-advance-and-archive]. This will scroll the current message (if necessary), advance
1140 to the next message, or advance to the next thread (if already on
1141 the last message of a thread).
1142
1143 Other commands are available to read or manipulate the thread
1144 more selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages
1145 without removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread
1146 without scrolling through with \\[notmuch-show-advance-and-archive]).
1147
1148 You can add or remove arbitrary tags from the current message with
1149 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
1150
1151 All currently available key bindings:
1152
1153 \\{notmuch-show-mode-map}"
1154   (interactive)
1155   (kill-all-local-variables)
1156   (use-local-map notmuch-show-mode-map)
1157   (setq major-mode 'notmuch-show-mode
1158         mode-name "notmuch-show")
1159   (setq buffer-read-only t
1160         truncate-lines t))
1161
1162 (defun notmuch-show-move-to-message-top ()
1163   (goto-char (notmuch-show-message-top)))
1164
1165 (defun notmuch-show-move-to-message-bottom ()
1166   (goto-char (notmuch-show-message-bottom)))
1167
1168 (defun notmuch-show-message-adjust ()
1169   (recenter 0))
1170
1171 ;; Movement related functions.
1172
1173 ;; There's some strangeness here where a text property applied to a
1174 ;; region a->b is not found when point is at b. We walk backwards
1175 ;; until finding the property.
1176 (defun notmuch-show-message-extent ()
1177   (let (r)
1178     (save-excursion
1179       (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
1180         (backward-char)))
1181     r))
1182
1183 (defun notmuch-show-message-top ()
1184   (car (notmuch-show-message-extent)))
1185
1186 (defun notmuch-show-message-bottom ()
1187   (cdr (notmuch-show-message-extent)))
1188
1189 (defun notmuch-show-goto-message-next ()
1190   (let ((start (point)))
1191     (notmuch-show-move-to-message-bottom)
1192     (if (not (eobp))
1193         t
1194       (goto-char start)
1195       nil)))
1196
1197 (defun notmuch-show-goto-message-previous ()
1198   (notmuch-show-move-to-message-top)
1199   (if (bobp)
1200       nil
1201     (backward-char)
1202     (notmuch-show-move-to-message-top)
1203     t))
1204
1205 (defun notmuch-show-mapc (function)
1206   "Iterate through all messages in the current thread with
1207 `notmuch-show-goto-message-next' and call FUNCTION for side
1208 effects."
1209   (save-excursion
1210     (goto-char (point-min))
1211     (loop do (funcall function)
1212           while (notmuch-show-goto-message-next))))
1213
1214 ;; Functions relating to the visibility of messages and their
1215 ;; components.
1216
1217 (defun notmuch-show-element-visible (props visible-p spec-property)
1218   (let ((spec (plist-get props spec-property)))
1219     (if visible-p
1220         (remove-from-invisibility-spec spec)
1221       (add-to-invisibility-spec spec))))
1222
1223 (defun notmuch-show-message-visible (props visible-p)
1224   (notmuch-show-element-visible props visible-p :message-invis-spec)
1225   (notmuch-show-set-prop :message-visible visible-p props))
1226
1227 (defun notmuch-show-headers-visible (props visible-p)
1228   (notmuch-show-element-visible props visible-p :headers-invis-spec)
1229   (notmuch-show-set-prop :headers-visible visible-p props))
1230
1231 ;; Functions for setting and getting attributes of the current
1232 ;; message.
1233
1234 (defun notmuch-show-set-message-properties (props)
1235   (save-excursion
1236     (notmuch-show-move-to-message-top)
1237     (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
1238
1239 (defun notmuch-show-get-message-properties ()
1240   "Return the properties of the current message as a plist.
1241
1242 Some useful entries are:
1243 :headers - Property list containing the headers :Date, :Subject, :From, etc.
1244 :body - Body of the message
1245 :tags - Tags for this message"
1246   (save-excursion
1247     (notmuch-show-move-to-message-top)
1248     (get-text-property (point) :notmuch-message-properties)))
1249
1250 (defun notmuch-show-set-prop (prop val &optional props)
1251   (let ((inhibit-read-only t)
1252         (props (or props
1253                    (notmuch-show-get-message-properties))))
1254     (plist-put props prop val)
1255     (notmuch-show-set-message-properties props)))
1256
1257 (defun notmuch-show-get-prop (prop &optional props)
1258   (let ((props (or props
1259                    (notmuch-show-get-message-properties))))
1260     (plist-get props prop)))
1261
1262 (defun notmuch-show-get-message-id ()
1263   "Return the message id of the current message."
1264   (concat "id:\"" (notmuch-show-get-prop :id) "\""))
1265
1266 (defun notmuch-show-get-messages-ids ()
1267   "Return all message ids of messages in the current thread."
1268   (let ((message-ids))
1269     (notmuch-show-mapc
1270      (lambda () (push (notmuch-show-get-message-id) message-ids)))
1271     message-ids))
1272
1273 (defun notmuch-show-get-messages-ids-search ()
1274   "Return a search string for all message ids of messages in the
1275 current thread."
1276   (mapconcat 'identity (notmuch-show-get-messages-ids) " or "))
1277
1278 ;; dme: Would it make sense to use a macro for many of these?
1279
1280 (defun notmuch-show-get-filename ()
1281   "Return the filename of the current message."
1282   (notmuch-show-get-prop :filename))
1283
1284 (defun notmuch-show-get-header (header &optional props)
1285   "Return the named header of the current message, if any."
1286   (plist-get (notmuch-show-get-prop :headers props) header))
1287
1288 (defun notmuch-show-get-cc ()
1289   (notmuch-show-get-header :Cc))
1290
1291 (defun notmuch-show-get-date ()
1292   (notmuch-show-get-header :Date))
1293
1294 (defun notmuch-show-get-from ()
1295   (notmuch-show-get-header :From))
1296
1297 (defun notmuch-show-get-subject ()
1298   (notmuch-show-get-header :Subject))
1299
1300 (defun notmuch-show-get-to ()
1301   (notmuch-show-get-header :To))
1302
1303 (defun notmuch-show-get-depth ()
1304   (notmuch-show-get-prop :depth))
1305
1306 (defun notmuch-show-get-pretty-subject ()
1307   (notmuch-prettify-subject (notmuch-show-get-subject)))
1308
1309 (defun notmuch-show-set-tags (tags)
1310   "Set the tags of the current message."
1311   (notmuch-show-set-prop :tags tags)
1312   (notmuch-show-update-tags tags))
1313
1314 (defun notmuch-show-get-tags ()
1315   "Return the tags of the current message."
1316   (notmuch-show-get-prop :tags))
1317
1318 (defun notmuch-show-message-visible-p ()
1319   "Is the current message visible?"
1320   (notmuch-show-get-prop :message-visible))
1321
1322 (defun notmuch-show-headers-visible-p ()
1323   "Are the headers of the current message visible?"
1324   (notmuch-show-get-prop :headers-visible))
1325
1326 (defun notmuch-show-mark-read ()
1327   "Mark the current message as read."
1328   (notmuch-show-tag-message "-unread"))
1329
1330 ;; Functions for getting attributes of several messages in the current
1331 ;; thread.
1332
1333 (defun notmuch-show-get-message-ids-for-open-messages ()
1334   "Return a list of all message IDs for open messages in the current thread."
1335   (save-excursion
1336     (let (message-ids done)
1337       (goto-char (point-min))
1338       (while (not done)
1339         (if (notmuch-show-message-visible-p)
1340             (setq message-ids (append message-ids (list (notmuch-show-get-message-id)))))
1341         (setq done (not (notmuch-show-goto-message-next)))
1342         )
1343       message-ids
1344       )))
1345
1346 ;; Commands typically bound to keys.
1347
1348 (defun notmuch-show-advance ()
1349   "Advance through thread.
1350
1351 If the current message in the thread is not yet fully visible,
1352 scroll by a near screenful to read more of the message.
1353
1354 Otherwise, (the end of the current message is already within the
1355 current window), advance to the next open message."
1356   (interactive)
1357   (let* ((end-of-this-message (notmuch-show-message-bottom))
1358          (visible-end-of-this-message (1- end-of-this-message))
1359          (ret nil))
1360     (while (invisible-p visible-end-of-this-message)
1361       (setq visible-end-of-this-message
1362             (max (point-min)
1363                  (1- (previous-single-char-property-change
1364                       visible-end-of-this-message 'invisible)))))
1365     (cond
1366      ;; Ideally we would test `end-of-this-message' against the result
1367      ;; of `window-end', but that doesn't account for the fact that
1368      ;; the end of the message might be hidden.
1369      ((and visible-end-of-this-message
1370            (> visible-end-of-this-message (window-end)))
1371       ;; The bottom of this message is not visible - scroll.
1372       (scroll-up nil))
1373
1374      ((not (= end-of-this-message (point-max)))
1375       ;; This is not the last message - move to the next visible one.
1376       (notmuch-show-next-open-message))
1377
1378      (t
1379       ;; This is the last message - change the return value
1380       (setq ret t)))
1381     ret))
1382
1383 (defun notmuch-show-advance-and-archive ()
1384   "Advance through thread and archive.
1385
1386 This command is intended to be one of the simplest ways to
1387 process a thread of email. It works exactly like
1388 notmuch-show-advance, in that it scrolls through messages in a
1389 show buffer, except that when it gets to the end of the buffer it
1390 archives the entire current thread, (remove the \"inbox\" tag
1391 from each message), kills the buffer, and displays the next
1392 thread from the search from which this thread was originally
1393 shown."
1394   (interactive)
1395   (if (notmuch-show-advance)
1396       (notmuch-show-archive-thread-then-next)))
1397
1398 (defun notmuch-show-rewind ()
1399   "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
1400
1401 Specifically, if the beginning of the previous email is fewer
1402 than `window-height' lines from the current point, move to it
1403 just like `notmuch-show-previous-message'.
1404
1405 Otherwise, just scroll down a screenful of the current message.
1406
1407 This command does not modify any message tags, (it does not undo
1408 any effects from previous calls to
1409 `notmuch-show-advance-and-archive'."
1410   (interactive)
1411   (let ((start-of-message (notmuch-show-message-top))
1412         (start-of-window (window-start)))
1413     (cond
1414       ;; Either this message is properly aligned with the start of the
1415       ;; window or the start of this message is not visible on the
1416       ;; screen - scroll.
1417      ((or (= start-of-message start-of-window)
1418           (< start-of-message start-of-window))
1419       (scroll-down)
1420       ;; If a small number of lines from the previous message are
1421       ;; visible, realign so that the top of the current message is at
1422       ;; the top of the screen.
1423       (when (<= (count-screen-lines (window-start) start-of-message)
1424                 next-screen-context-lines)
1425         (goto-char (notmuch-show-message-top))
1426         (notmuch-show-message-adjust))
1427       ;; Move to the top left of the window.
1428       (goto-char (window-start)))
1429      (t
1430       ;; Move to the previous message.
1431       (notmuch-show-previous-message)))))
1432
1433 (defun notmuch-show-reply (&optional prompt-for-sender)
1434   "Reply to the sender and all recipients of the current message."
1435   (interactive "P")
1436   (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender t))
1437
1438 (defun notmuch-show-reply-sender (&optional prompt-for-sender)
1439   "Reply to the sender of the current message."
1440   (interactive "P")
1441   (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender nil))
1442
1443 (defun notmuch-show-forward-message (&optional prompt-for-sender)
1444   "Forward the current message."
1445   (interactive "P")
1446   (with-current-notmuch-show-message
1447    (notmuch-mua-new-forward-message prompt-for-sender)))
1448
1449 (defun notmuch-show-next-message (&optional pop-at-end)
1450   "Show the next message.
1451
1452 If a prefix argument is given and this is the last message in the
1453 thread, navigate to the next thread in the parent search buffer."
1454   (interactive "P")
1455   (if (notmuch-show-goto-message-next)
1456       (progn
1457         (notmuch-show-mark-read)
1458         (notmuch-show-message-adjust))
1459     (if pop-at-end
1460         (notmuch-show-next-thread)
1461       (goto-char (point-max)))))
1462
1463 (defun notmuch-show-previous-message ()
1464   "Show the previous message."
1465   (interactive)
1466   (notmuch-show-goto-message-previous)
1467   (notmuch-show-mark-read)
1468   (notmuch-show-message-adjust))
1469
1470 (defun notmuch-show-next-open-message (&optional pop-at-end)
1471   "Show the next open message.
1472
1473 If a prefix argument is given and this is the last open message
1474 in the thread, navigate to the next thread in the parent search
1475 buffer. Return t if there was a next open message in the thread
1476 to show, nil otherwise."
1477   (interactive "P")
1478   (let (r)
1479     (while (and (setq r (notmuch-show-goto-message-next))
1480                 (not (notmuch-show-message-visible-p))))
1481     (if r
1482         (progn
1483           (notmuch-show-mark-read)
1484           (notmuch-show-message-adjust))
1485       (if pop-at-end
1486           (notmuch-show-next-thread)
1487         (goto-char (point-max))))
1488     r))
1489
1490 (defun notmuch-show-previous-open-message ()
1491   "Show the previous open message."
1492   (interactive)
1493   (while (and (notmuch-show-goto-message-previous)
1494               (not (notmuch-show-message-visible-p))))
1495   (notmuch-show-mark-read)
1496   (notmuch-show-message-adjust))
1497
1498 (defun notmuch-show-view-raw-message ()
1499   "View the file holding the current message."
1500   (interactive)
1501   (let* ((id (notmuch-show-get-message-id))
1502          (buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
1503     (call-process notmuch-command nil buf nil "show" "--format=raw" id)
1504     (switch-to-buffer buf)
1505     (goto-char (point-min))
1506     (set-buffer-modified-p nil)
1507     (view-buffer buf 'kill-buffer-if-not-modified)))
1508
1509 (defun notmuch-show-pipe-message (entire-thread command)
1510   "Pipe the contents of the current message (or thread) to the given command.
1511
1512 The given command will be executed with the raw contents of the
1513 current email message as stdin. Anything printed by the command
1514 to stdout or stderr will appear in the *notmuch-pipe* buffer.
1515
1516 When invoked with a prefix argument, the command will receive all
1517 open messages in the current thread (formatted as an mbox) rather
1518 than only the current message."
1519   (interactive "P\nsPipe message to command: ")
1520   (let (shell-command)
1521     (if entire-thread
1522         (setq shell-command
1523               (concat notmuch-command " show --format=mbox "
1524                       (shell-quote-argument
1525                        (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
1526                       " | " command))
1527       (setq shell-command
1528             (concat notmuch-command " show --format=raw "
1529                     (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
1530     (let ((buf (get-buffer-create (concat "*notmuch-pipe*"))))
1531       (with-current-buffer buf
1532         (setq buffer-read-only nil)
1533         (erase-buffer)
1534         (let ((exit-code (call-process-shell-command shell-command nil buf)))
1535           (goto-char (point-max))
1536           (set-buffer-modified-p nil)
1537           (setq buffer-read-only t)
1538           (unless (zerop exit-code)
1539             (switch-to-buffer-other-window buf)
1540             (message (format "Command '%s' exited abnormally with code %d"
1541                              shell-command exit-code))))))))
1542
1543 (defun notmuch-show-tag-message (&rest tag-changes)
1544   "Change tags for the current message.
1545
1546 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1547   (let* ((current-tags (notmuch-show-get-tags))
1548          (new-tags (notmuch-update-tags current-tags tag-changes)))
1549     (unless (equal current-tags new-tags)
1550       (apply 'notmuch-tag (notmuch-show-get-message-id) tag-changes)
1551       (notmuch-show-set-tags new-tags))))
1552
1553 (defun notmuch-show-tag (&optional initial-input)
1554   "Change tags for the current message, read input from the minibuffer."
1555   (interactive)
1556   (let ((tag-changes (notmuch-read-tag-changes
1557                       initial-input (notmuch-show-get-message-id))))
1558     (apply 'notmuch-show-tag-message tag-changes)))
1559
1560 (defun notmuch-show-tag-all (&rest tag-changes)
1561   "Change tags for all messages in the current thread.
1562
1563 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1564   (interactive (notmuch-read-tag-changes nil notmuch-show-thread-id))
1565   (apply 'notmuch-tag (notmuch-show-get-messages-ids-search) tag-changes)
1566   (notmuch-show-mapc
1567    (lambda ()
1568      (let* ((current-tags (notmuch-show-get-tags))
1569             (new-tags (notmuch-update-tags current-tags tag-changes)))
1570        (unless (equal current-tags new-tags)
1571          (notmuch-show-set-tags new-tags))))))
1572
1573 (defun notmuch-show-add-tag ()
1574   "Same as `notmuch-show-tag' but sets initial input to '+'."
1575   (interactive)
1576   (notmuch-show-tag "+"))
1577
1578 (defun notmuch-show-remove-tag ()
1579   "Same as `notmuch-show-tag' but sets initial input to '-'."
1580   (interactive)
1581   (notmuch-show-tag "-"))
1582
1583 (defun notmuch-show-toggle-headers ()
1584   "Toggle the visibility of the current message headers."
1585   (interactive)
1586   (let ((props (notmuch-show-get-message-properties)))
1587     (notmuch-show-headers-visible
1588      props
1589      (not (plist-get props :headers-visible))))
1590   (force-window-update))
1591
1592 (defun notmuch-show-toggle-message ()
1593   "Toggle the visibility of the current message."
1594   (interactive)
1595   (let ((props (notmuch-show-get-message-properties)))
1596     (notmuch-show-message-visible
1597      props
1598      (not (plist-get props :message-visible))))
1599   (force-window-update))
1600
1601 (defun notmuch-show-open-or-close-all ()
1602   "Set the visibility all of the messages in the current thread.
1603 By default make all of the messages visible. With a prefix
1604 argument, hide all of the messages."
1605   (interactive)
1606   (save-excursion
1607     (goto-char (point-min))
1608     (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1609                                            (not current-prefix-arg))
1610           until (not (notmuch-show-goto-message-next))))
1611   (force-window-update))
1612
1613 (defun notmuch-show-next-button ()
1614   "Advance point to the next button in the buffer."
1615   (interactive)
1616   (forward-button 1))
1617
1618 (defun notmuch-show-previous-button ()
1619   "Move point back to the previous button in the buffer."
1620   (interactive)
1621   (backward-button 1))
1622
1623 (defun notmuch-show-tag-thread-internal (tag &optional remove)
1624   "Add tag to the current set of messages.
1625
1626 If the remove switch is given, tags will be removed instead of
1627 added."
1628   (goto-char (point-min))
1629   (let ((op (if remove "-" "+")))
1630     (loop do (notmuch-show-tag-message (concat op tag))
1631           until (not (notmuch-show-goto-message-next)))))
1632
1633 (defun notmuch-show-add-tag-thread (tag)
1634   "Add tag to all messages in the current thread."
1635   (interactive)
1636   (notmuch-show-tag-thread-internal tag))
1637
1638 (defun notmuch-show-remove-tag-thread (tag)
1639   "Remove tag from all messages in the current thread."
1640   (interactive)
1641   (notmuch-show-tag-thread-internal tag t))
1642
1643 (defun notmuch-show-next-thread (&optional show-next)
1644   "Move to the next item in the search results, if any."
1645   (interactive "P")
1646   (let ((parent-buffer notmuch-show-parent-buffer))
1647     (notmuch-kill-this-buffer)
1648     (when parent-buffer
1649       (switch-to-buffer parent-buffer)
1650       (notmuch-search-next-thread)
1651       (if show-next
1652           (notmuch-search-show-thread)))))
1653
1654 (defun notmuch-show-archive-thread (&optional unarchive)
1655   "Archive each message in thread.
1656
1657 If a prefix argument is given, the messages will be
1658 \"unarchived\" (ie. the \"inbox\" tag will be added instead of
1659 removed).
1660
1661 Archive each message currently shown by removing the \"inbox\"
1662 tag from each. Then kill this buffer and show the next thread
1663 from the search from which this thread was originally shown.
1664
1665 Note: This command is safe from any race condition of new messages
1666 being delivered to the same thread. It does not archive the
1667 entire thread, but only the messages shown in the current
1668 buffer."
1669   (interactive "P")
1670   (if unarchive
1671       (notmuch-show-add-tag-thread "inbox")
1672     (notmuch-show-remove-tag-thread "inbox")))
1673
1674 (defun notmuch-show-archive-thread-then-next ()
1675   "Archive each message in thread, then show next thread from search."
1676   (interactive)
1677   (notmuch-show-archive-thread)
1678   (notmuch-show-next-thread t))
1679
1680 (defun notmuch-show-archive-thread-then-exit ()
1681   "Archive each message in thread, then exit back to search results."
1682   (interactive)
1683   (notmuch-show-archive-thread)
1684   (notmuch-show-next-thread))
1685
1686 (defun notmuch-show-archive-message (&optional unarchive)
1687   "Archive the current message.
1688
1689 If a prefix argument is given, the message will be
1690 \"unarchived\" (ie. the \"inbox\" tag will be added instead of
1691 removed)."
1692   (interactive "P")
1693   (let ((op (if unarchive "+" "-")))
1694     (notmuch-show-tag-message (concat op "inbox"))))
1695
1696 (defun notmuch-show-archive-message-then-next-or-exit ()
1697   "Archive the current message, then show the next open message in the current thread.
1698
1699 If at the last open message in the current thread, then exit back
1700 to search results."
1701   (interactive)
1702   (notmuch-show-archive-message)
1703   (notmuch-show-next-open-message t))
1704
1705 (defun notmuch-show-archive-message-then-next-or-next-thread ()
1706   "Archive the current message, then show the next open message in the current thread.
1707
1708 If at the last open message in the current thread, then show next
1709 thread from search."
1710   (interactive)
1711   (notmuch-show-archive-message)
1712   (unless (notmuch-show-next-open-message)
1713     (notmuch-show-next-thread t)))
1714
1715 (defun notmuch-show-stash-cc ()
1716   "Copy CC field of current message to kill-ring."
1717   (interactive)
1718   (notmuch-common-do-stash (notmuch-show-get-cc)))
1719
1720 (defun notmuch-show-stash-date ()
1721   "Copy date of current message to kill-ring."
1722   (interactive)
1723   (notmuch-common-do-stash (notmuch-show-get-date)))
1724
1725 (defun notmuch-show-stash-filename ()
1726   "Copy filename of current message to kill-ring."
1727   (interactive)
1728   (notmuch-common-do-stash (notmuch-show-get-filename)))
1729
1730 (defun notmuch-show-stash-from ()
1731   "Copy From address of current message to kill-ring."
1732   (interactive)
1733   (notmuch-common-do-stash (notmuch-show-get-from)))
1734
1735 (defun notmuch-show-stash-message-id ()
1736   "Copy message ID of current message to kill-ring."
1737   (interactive)
1738   (notmuch-common-do-stash (notmuch-show-get-message-id)))
1739
1740 (defun notmuch-show-stash-message-id-stripped ()
1741   "Copy message ID of current message (sans `id:' prefix) to kill-ring."
1742   (interactive)
1743   (notmuch-common-do-stash (substring (notmuch-show-get-message-id) 4 -1)))
1744
1745 (defun notmuch-show-stash-subject ()
1746   "Copy Subject field of current message to kill-ring."
1747   (interactive)
1748   (notmuch-common-do-stash (notmuch-show-get-subject)))
1749
1750 (defun notmuch-show-stash-tags ()
1751   "Copy tags of current message to kill-ring as a comma separated list."
1752   (interactive)
1753   (notmuch-common-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1754
1755 (defun notmuch-show-stash-to ()
1756   "Copy To address of current message to kill-ring."
1757   (interactive)
1758   (notmuch-common-do-stash (notmuch-show-get-to)))
1759
1760 ;; Commands typically bound to buttons.
1761
1762 (defun notmuch-show-part-button-default (&optional button)
1763   (interactive)
1764   (notmuch-show-part-button-internal button notmuch-show-part-button-default-action))
1765
1766 (defun notmuch-show-part-button-save (&optional button)
1767   (interactive)
1768   (notmuch-show-part-button-internal button #'notmuch-show-save-part))
1769
1770 (defun notmuch-show-part-button-view (&optional button)
1771   (interactive)
1772   (notmuch-show-part-button-internal button #'notmuch-show-view-part))
1773
1774 (defun notmuch-show-part-button-interactively-view (&optional button)
1775   (interactive)
1776   (notmuch-show-part-button-internal button #'notmuch-show-interactively-view-part))
1777
1778 (defun notmuch-show-part-button-internal (button handler)
1779   (let ((button (or button (button-at (point)))))
1780     (if button
1781         (let ((nth (button-get button :notmuch-part)))
1782           (if nth
1783               (funcall handler (notmuch-show-get-message-id) nth
1784                        (button-get button :notmuch-filename)
1785                        (button-get button :notmuch-content-type)))))))
1786
1787 ;;
1788
1789 (provide 'notmuch-show)