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