]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-show.el
emacs: Add a binding (t) to toggle the truncation of long lines.
[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         (define-key map "t" 'toggle-truncate-lines)
1141         map)
1142       "Keymap for \"notmuch show\" buffers.")
1143 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
1144
1145 (defun notmuch-show-mode ()
1146   "Major mode for viewing a thread with notmuch.
1147
1148 This buffer contains the results of the \"notmuch show\" command
1149 for displaying a single thread of email from your email archives.
1150
1151 By default, various components of email messages, (citations,
1152 signatures, already-read messages), are hidden. You can make
1153 these parts visible by clicking with the mouse button or by
1154 pressing RET after positioning the cursor on a hidden part, (for
1155 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
1156
1157 Reading the thread sequentially is well-supported by pressing
1158 \\[notmuch-show-advance-and-archive]. This will scroll the current message (if necessary), advance
1159 to the next message, or advance to the next thread (if already on
1160 the last message of a thread).
1161
1162 Other commands are available to read or manipulate the thread
1163 more selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages
1164 without removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread
1165 without scrolling through with \\[notmuch-show-advance-and-archive]).
1166
1167 You can add or remove arbitrary tags from the current message with
1168 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
1169
1170 All currently available key bindings:
1171
1172 \\{notmuch-show-mode-map}"
1173   (interactive)
1174   (kill-all-local-variables)
1175   (use-local-map notmuch-show-mode-map)
1176   (setq major-mode 'notmuch-show-mode
1177         mode-name "notmuch-show")
1178   (setq buffer-read-only t
1179         truncate-lines t))
1180
1181 (defun notmuch-show-move-to-message-top ()
1182   (goto-char (notmuch-show-message-top)))
1183
1184 (defun notmuch-show-move-to-message-bottom ()
1185   (goto-char (notmuch-show-message-bottom)))
1186
1187 (defun notmuch-show-message-adjust ()
1188   (recenter 0))
1189
1190 ;; Movement related functions.
1191
1192 ;; There's some strangeness here where a text property applied to a
1193 ;; region a->b is not found when point is at b. We walk backwards
1194 ;; until finding the property.
1195 (defun notmuch-show-message-extent ()
1196   (let (r)
1197     (save-excursion
1198       (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
1199         (backward-char)))
1200     r))
1201
1202 (defun notmuch-show-message-top ()
1203   (car (notmuch-show-message-extent)))
1204
1205 (defun notmuch-show-message-bottom ()
1206   (cdr (notmuch-show-message-extent)))
1207
1208 (defun notmuch-show-goto-message-next ()
1209   (let ((start (point)))
1210     (notmuch-show-move-to-message-bottom)
1211     (if (not (eobp))
1212         t
1213       (goto-char start)
1214       nil)))
1215
1216 (defun notmuch-show-goto-message-previous ()
1217   (notmuch-show-move-to-message-top)
1218   (if (bobp)
1219       nil
1220     (backward-char)
1221     (notmuch-show-move-to-message-top)
1222     t))
1223
1224 (defun notmuch-show-mapc (function)
1225   "Iterate through all messages in the current thread with
1226 `notmuch-show-goto-message-next' and call FUNCTION for side
1227 effects."
1228   (save-excursion
1229     (goto-char (point-min))
1230     (loop do (funcall function)
1231           while (notmuch-show-goto-message-next))))
1232
1233 ;; Functions relating to the visibility of messages and their
1234 ;; components.
1235
1236 (defun notmuch-show-element-visible (props visible-p spec-property)
1237   (let ((spec (plist-get props spec-property)))
1238     (if visible-p
1239         (remove-from-invisibility-spec spec)
1240       (add-to-invisibility-spec spec))))
1241
1242 (defun notmuch-show-message-visible (props visible-p)
1243   (notmuch-show-element-visible props visible-p :message-invis-spec)
1244   (notmuch-show-set-prop :message-visible visible-p props))
1245
1246 (defun notmuch-show-headers-visible (props visible-p)
1247   (notmuch-show-element-visible props visible-p :headers-invis-spec)
1248   (notmuch-show-set-prop :headers-visible visible-p props))
1249
1250 ;; Functions for setting and getting attributes of the current
1251 ;; message.
1252
1253 (defun notmuch-show-set-message-properties (props)
1254   (save-excursion
1255     (notmuch-show-move-to-message-top)
1256     (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
1257
1258 (defun notmuch-show-get-message-properties ()
1259   "Return the properties of the current message as a plist.
1260
1261 Some useful entries are:
1262 :headers - Property list containing the headers :Date, :Subject, :From, etc.
1263 :body - Body of the message
1264 :tags - Tags for this message"
1265   (save-excursion
1266     (notmuch-show-move-to-message-top)
1267     (get-text-property (point) :notmuch-message-properties)))
1268
1269 (defun notmuch-show-set-prop (prop val &optional props)
1270   (let ((inhibit-read-only t)
1271         (props (or props
1272                    (notmuch-show-get-message-properties))))
1273     (plist-put props prop val)
1274     (notmuch-show-set-message-properties props)))
1275
1276 (defun notmuch-show-get-prop (prop &optional props)
1277   (let ((props (or props
1278                    (notmuch-show-get-message-properties))))
1279     (plist-get props prop)))
1280
1281 (defun notmuch-show-get-message-id ()
1282   "Return the message id of the current message."
1283   (concat "id:\"" (notmuch-show-get-prop :id) "\""))
1284
1285 (defun notmuch-show-get-messages-ids ()
1286   "Return all message ids of messages in the current thread."
1287   (let ((message-ids))
1288     (notmuch-show-mapc
1289      (lambda () (push (notmuch-show-get-message-id) message-ids)))
1290     message-ids))
1291
1292 (defun notmuch-show-get-messages-ids-search ()
1293   "Return a search string for all message ids of messages in the
1294 current thread."
1295   (mapconcat 'identity (notmuch-show-get-messages-ids) " or "))
1296
1297 ;; dme: Would it make sense to use a macro for many of these?
1298
1299 (defun notmuch-show-get-filename ()
1300   "Return the filename of the current message."
1301   (notmuch-show-get-prop :filename))
1302
1303 (defun notmuch-show-get-header (header &optional props)
1304   "Return the named header of the current message, if any."
1305   (plist-get (notmuch-show-get-prop :headers props) header))
1306
1307 (defun notmuch-show-get-cc ()
1308   (notmuch-show-get-header :Cc))
1309
1310 (defun notmuch-show-get-date ()
1311   (notmuch-show-get-header :Date))
1312
1313 (defun notmuch-show-get-from ()
1314   (notmuch-show-get-header :From))
1315
1316 (defun notmuch-show-get-subject ()
1317   (notmuch-show-get-header :Subject))
1318
1319 (defun notmuch-show-get-to ()
1320   (notmuch-show-get-header :To))
1321
1322 (defun notmuch-show-get-depth ()
1323   (notmuch-show-get-prop :depth))
1324
1325 (defun notmuch-show-get-pretty-subject ()
1326   (notmuch-prettify-subject (notmuch-show-get-subject)))
1327
1328 (defun notmuch-show-set-tags (tags)
1329   "Set the tags of the current message."
1330   (notmuch-show-set-prop :tags tags)
1331   (notmuch-show-update-tags tags))
1332
1333 (defun notmuch-show-get-tags ()
1334   "Return the tags of the current message."
1335   (notmuch-show-get-prop :tags))
1336
1337 (defun notmuch-show-message-visible-p ()
1338   "Is the current message visible?"
1339   (notmuch-show-get-prop :message-visible))
1340
1341 (defun notmuch-show-headers-visible-p ()
1342   "Are the headers of the current message visible?"
1343   (notmuch-show-get-prop :headers-visible))
1344
1345 (defun notmuch-show-mark-read ()
1346   "Mark the current message as read."
1347   (notmuch-show-tag-message "-unread"))
1348
1349 ;; Functions for getting attributes of several messages in the current
1350 ;; thread.
1351
1352 (defun notmuch-show-get-message-ids-for-open-messages ()
1353   "Return a list of all message IDs for open messages in the current thread."
1354   (save-excursion
1355     (let (message-ids done)
1356       (goto-char (point-min))
1357       (while (not done)
1358         (if (notmuch-show-message-visible-p)
1359             (setq message-ids (append message-ids (list (notmuch-show-get-message-id)))))
1360         (setq done (not (notmuch-show-goto-message-next)))
1361         )
1362       message-ids
1363       )))
1364
1365 ;; Commands typically bound to keys.
1366
1367 (defun notmuch-show-advance ()
1368   "Advance through thread.
1369
1370 If the current message in the thread is not yet fully visible,
1371 scroll by a near screenful to read more of the message.
1372
1373 Otherwise, (the end of the current message is already within the
1374 current window), advance to the next open message."
1375   (interactive)
1376   (let* ((end-of-this-message (notmuch-show-message-bottom))
1377          (visible-end-of-this-message (1- end-of-this-message))
1378          (ret nil))
1379     (while (invisible-p visible-end-of-this-message)
1380       (setq visible-end-of-this-message
1381             (max (point-min)
1382                  (1- (previous-single-char-property-change
1383                       visible-end-of-this-message 'invisible)))))
1384     (cond
1385      ;; Ideally we would test `end-of-this-message' against the result
1386      ;; of `window-end', but that doesn't account for the fact that
1387      ;; the end of the message might be hidden.
1388      ((and visible-end-of-this-message
1389            (> visible-end-of-this-message (window-end)))
1390       ;; The bottom of this message is not visible - scroll.
1391       (scroll-up nil))
1392
1393      ((not (= end-of-this-message (point-max)))
1394       ;; This is not the last message - move to the next visible one.
1395       (notmuch-show-next-open-message))
1396
1397      (t
1398       ;; This is the last message - change the return value
1399       (setq ret t)))
1400     ret))
1401
1402 (defun notmuch-show-advance-and-archive ()
1403   "Advance through thread and archive.
1404
1405 This command is intended to be one of the simplest ways to
1406 process a thread of email. It works exactly like
1407 notmuch-show-advance, in that it scrolls through messages in a
1408 show buffer, except that when it gets to the end of the buffer it
1409 archives the entire current thread, (remove the \"inbox\" tag
1410 from each message), kills the buffer, and displays the next
1411 thread from the search from which this thread was originally
1412 shown."
1413   (interactive)
1414   (if (notmuch-show-advance)
1415       (notmuch-show-archive-thread-then-next)))
1416
1417 (defun notmuch-show-rewind ()
1418   "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
1419
1420 Specifically, if the beginning of the previous email is fewer
1421 than `window-height' lines from the current point, move to it
1422 just like `notmuch-show-previous-message'.
1423
1424 Otherwise, just scroll down a screenful of the current message.
1425
1426 This command does not modify any message tags, (it does not undo
1427 any effects from previous calls to
1428 `notmuch-show-advance-and-archive'."
1429   (interactive)
1430   (let ((start-of-message (notmuch-show-message-top))
1431         (start-of-window (window-start)))
1432     (cond
1433       ;; Either this message is properly aligned with the start of the
1434       ;; window or the start of this message is not visible on the
1435       ;; screen - scroll.
1436      ((or (= start-of-message start-of-window)
1437           (< start-of-message start-of-window))
1438       (scroll-down)
1439       ;; If a small number of lines from the previous message are
1440       ;; visible, realign so that the top of the current message is at
1441       ;; the top of the screen.
1442       (when (<= (count-screen-lines (window-start) start-of-message)
1443                 next-screen-context-lines)
1444         (goto-char (notmuch-show-message-top))
1445         (notmuch-show-message-adjust))
1446       ;; Move to the top left of the window.
1447       (goto-char (window-start)))
1448      (t
1449       ;; Move to the previous message.
1450       (notmuch-show-previous-message)))))
1451
1452 (defun notmuch-show-reply (&optional prompt-for-sender)
1453   "Reply to the sender and all recipients of the current message."
1454   (interactive "P")
1455   (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender t))
1456
1457 (defun notmuch-show-reply-sender (&optional prompt-for-sender)
1458   "Reply to the sender of the current message."
1459   (interactive "P")
1460   (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender nil))
1461
1462 (defun notmuch-show-forward-message (&optional prompt-for-sender)
1463   "Forward the current message."
1464   (interactive "P")
1465   (with-current-notmuch-show-message
1466    (notmuch-mua-new-forward-message prompt-for-sender)))
1467
1468 (defun notmuch-show-next-message (&optional pop-at-end)
1469   "Show the next message.
1470
1471 If a prefix argument is given and this is the last message in the
1472 thread, navigate to the next thread in the parent search buffer."
1473   (interactive "P")
1474   (if (notmuch-show-goto-message-next)
1475       (progn
1476         (notmuch-show-mark-read)
1477         (notmuch-show-message-adjust))
1478     (if pop-at-end
1479         (notmuch-show-next-thread)
1480       (goto-char (point-max)))))
1481
1482 (defun notmuch-show-previous-message ()
1483   "Show the previous message."
1484   (interactive)
1485   (notmuch-show-goto-message-previous)
1486   (notmuch-show-mark-read)
1487   (notmuch-show-message-adjust))
1488
1489 (defun notmuch-show-next-open-message (&optional pop-at-end)
1490   "Show the next open message.
1491
1492 If a prefix argument is given and this is the last open message
1493 in the thread, navigate to the next thread in the parent search
1494 buffer. Return t if there was a next open message in the thread
1495 to show, nil otherwise."
1496   (interactive "P")
1497   (let (r)
1498     (while (and (setq r (notmuch-show-goto-message-next))
1499                 (not (notmuch-show-message-visible-p))))
1500     (if r
1501         (progn
1502           (notmuch-show-mark-read)
1503           (notmuch-show-message-adjust))
1504       (if pop-at-end
1505           (notmuch-show-next-thread)
1506         (goto-char (point-max))))
1507     r))
1508
1509 (defun notmuch-show-previous-open-message ()
1510   "Show the previous open message."
1511   (interactive)
1512   (while (and (notmuch-show-goto-message-previous)
1513               (not (notmuch-show-message-visible-p))))
1514   (notmuch-show-mark-read)
1515   (notmuch-show-message-adjust))
1516
1517 (defun notmuch-show-view-raw-message ()
1518   "View the file holding the current message."
1519   (interactive)
1520   (let* ((id (notmuch-show-get-message-id))
1521          (buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
1522     (call-process notmuch-command nil buf nil "show" "--format=raw" id)
1523     (switch-to-buffer buf)
1524     (goto-char (point-min))
1525     (set-buffer-modified-p nil)
1526     (view-buffer buf 'kill-buffer-if-not-modified)))
1527
1528 (defun notmuch-show-pipe-message (entire-thread command)
1529   "Pipe the contents of the current message (or thread) to the given command.
1530
1531 The given command will be executed with the raw contents of the
1532 current email message as stdin. Anything printed by the command
1533 to stdout or stderr will appear in the *notmuch-pipe* buffer.
1534
1535 When invoked with a prefix argument, the command will receive all
1536 open messages in the current thread (formatted as an mbox) rather
1537 than only the current message."
1538   (interactive "P\nsPipe message to command: ")
1539   (let (shell-command)
1540     (if entire-thread
1541         (setq shell-command
1542               (concat notmuch-command " show --format=mbox "
1543                       (shell-quote-argument
1544                        (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
1545                       " | " command))
1546       (setq shell-command
1547             (concat notmuch-command " show --format=raw "
1548                     (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
1549     (let ((buf (get-buffer-create (concat "*notmuch-pipe*"))))
1550       (with-current-buffer buf
1551         (setq buffer-read-only nil)
1552         (erase-buffer)
1553         (let ((exit-code (call-process-shell-command shell-command nil buf)))
1554           (goto-char (point-max))
1555           (set-buffer-modified-p nil)
1556           (setq buffer-read-only t)
1557           (unless (zerop exit-code)
1558             (switch-to-buffer-other-window buf)
1559             (message (format "Command '%s' exited abnormally with code %d"
1560                              shell-command exit-code))))))))
1561
1562 (defun notmuch-show-tag-message (&rest tag-changes)
1563   "Change tags for the current message.
1564
1565 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1566   (let* ((current-tags (notmuch-show-get-tags))
1567          (new-tags (notmuch-update-tags current-tags tag-changes)))
1568     (unless (equal current-tags new-tags)
1569       (apply 'notmuch-tag (notmuch-show-get-message-id) tag-changes)
1570       (notmuch-show-set-tags new-tags))))
1571
1572 (defun notmuch-show-tag (&optional initial-input)
1573   "Change tags for the current message, read input from the minibuffer."
1574   (interactive)
1575   (let ((tag-changes (notmuch-read-tag-changes
1576                       initial-input (notmuch-show-get-message-id))))
1577     (apply 'notmuch-show-tag-message tag-changes)))
1578
1579 (defun notmuch-show-tag-all (&rest tag-changes)
1580   "Change tags for all messages in the current thread.
1581
1582 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1583   (interactive (notmuch-read-tag-changes nil notmuch-show-thread-id))
1584   (apply 'notmuch-tag (notmuch-show-get-messages-ids-search) tag-changes)
1585   (notmuch-show-mapc
1586    (lambda ()
1587      (let* ((current-tags (notmuch-show-get-tags))
1588             (new-tags (notmuch-update-tags current-tags tag-changes)))
1589        (unless (equal current-tags new-tags)
1590          (notmuch-show-set-tags new-tags))))))
1591
1592 (defun notmuch-show-add-tag ()
1593   "Same as `notmuch-show-tag' but sets initial input to '+'."
1594   (interactive)
1595   (notmuch-show-tag "+"))
1596
1597 (defun notmuch-show-remove-tag ()
1598   "Same as `notmuch-show-tag' but sets initial input to '-'."
1599   (interactive)
1600   (notmuch-show-tag "-"))
1601
1602 (defun notmuch-show-toggle-headers ()
1603   "Toggle the visibility of the current message headers."
1604   (interactive)
1605   (let ((props (notmuch-show-get-message-properties)))
1606     (notmuch-show-headers-visible
1607      props
1608      (not (plist-get props :headers-visible))))
1609   (force-window-update))
1610
1611 (defun notmuch-show-toggle-message ()
1612   "Toggle the visibility of the current message."
1613   (interactive)
1614   (let ((props (notmuch-show-get-message-properties)))
1615     (notmuch-show-message-visible
1616      props
1617      (not (plist-get props :message-visible))))
1618   (force-window-update))
1619
1620 (defun notmuch-show-open-or-close-all ()
1621   "Set the visibility all of the messages in the current thread.
1622 By default make all of the messages visible. With a prefix
1623 argument, hide all of the messages."
1624   (interactive)
1625   (save-excursion
1626     (goto-char (point-min))
1627     (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1628                                            (not current-prefix-arg))
1629           until (not (notmuch-show-goto-message-next))))
1630   (force-window-update))
1631
1632 (defun notmuch-show-next-button ()
1633   "Advance point to the next button in the buffer."
1634   (interactive)
1635   (forward-button 1))
1636
1637 (defun notmuch-show-previous-button ()
1638   "Move point back to the previous button in the buffer."
1639   (interactive)
1640   (backward-button 1))
1641
1642 (defun notmuch-show-tag-thread-internal (tag &optional remove)
1643   "Add tag to the current set of messages.
1644
1645 If the remove switch is given, tags will be removed instead of
1646 added."
1647   (goto-char (point-min))
1648   (let ((op (if remove "-" "+")))
1649     (loop do (notmuch-show-tag-message (concat op tag))
1650           until (not (notmuch-show-goto-message-next)))))
1651
1652 (defun notmuch-show-add-tag-thread (tag)
1653   "Add tag to all messages in the current thread."
1654   (interactive)
1655   (notmuch-show-tag-thread-internal tag))
1656
1657 (defun notmuch-show-remove-tag-thread (tag)
1658   "Remove tag from all messages in the current thread."
1659   (interactive)
1660   (notmuch-show-tag-thread-internal tag t))
1661
1662 (defun notmuch-show-next-thread (&optional show-next)
1663   "Move to the next item in the search results, if any."
1664   (interactive "P")
1665   (let ((parent-buffer notmuch-show-parent-buffer))
1666     (notmuch-kill-this-buffer)
1667     (when parent-buffer
1668       (switch-to-buffer parent-buffer)
1669       (notmuch-search-next-thread)
1670       (if show-next
1671           (notmuch-search-show-thread)))))
1672
1673 (defun notmuch-show-archive-thread (&optional unarchive)
1674   "Archive each message in thread.
1675
1676 If a prefix argument is given, the messages will be
1677 \"unarchived\" (ie. the \"inbox\" tag will be added instead of
1678 removed).
1679
1680 Archive each message currently shown by removing the \"inbox\"
1681 tag from each. Then kill this buffer and show the next thread
1682 from the search from which this thread was originally shown.
1683
1684 Note: This command is safe from any race condition of new messages
1685 being delivered to the same thread. It does not archive the
1686 entire thread, but only the messages shown in the current
1687 buffer."
1688   (interactive "P")
1689   (if unarchive
1690       (notmuch-show-add-tag-thread "inbox")
1691     (notmuch-show-remove-tag-thread "inbox")))
1692
1693 (defun notmuch-show-archive-thread-then-next ()
1694   "Archive each message in thread, then show next thread from search."
1695   (interactive)
1696   (notmuch-show-archive-thread)
1697   (notmuch-show-next-thread t))
1698
1699 (defun notmuch-show-archive-thread-then-exit ()
1700   "Archive each message in thread, then exit back to search results."
1701   (interactive)
1702   (notmuch-show-archive-thread)
1703   (notmuch-show-next-thread))
1704
1705 (defun notmuch-show-archive-message (&optional unarchive)
1706   "Archive the current message.
1707
1708 If a prefix argument is given, the message will be
1709 \"unarchived\" (ie. the \"inbox\" tag will be added instead of
1710 removed)."
1711   (interactive "P")
1712   (let ((op (if unarchive "+" "-")))
1713     (notmuch-show-tag-message (concat op "inbox"))))
1714
1715 (defun notmuch-show-archive-message-then-next-or-exit ()
1716   "Archive the current message, then show the next open message in the current thread.
1717
1718 If at the last open message in the current thread, then exit back
1719 to search results."
1720   (interactive)
1721   (notmuch-show-archive-message)
1722   (notmuch-show-next-open-message t))
1723
1724 (defun notmuch-show-archive-message-then-next-or-next-thread ()
1725   "Archive the current message, then show the next open message in the current thread.
1726
1727 If at the last open message in the current thread, then show next
1728 thread from search."
1729   (interactive)
1730   (notmuch-show-archive-message)
1731   (unless (notmuch-show-next-open-message)
1732     (notmuch-show-next-thread t)))
1733
1734 (defun notmuch-show-stash-cc ()
1735   "Copy CC field of current message to kill-ring."
1736   (interactive)
1737   (notmuch-common-do-stash (notmuch-show-get-cc)))
1738
1739 (defun notmuch-show-stash-date ()
1740   "Copy date of current message to kill-ring."
1741   (interactive)
1742   (notmuch-common-do-stash (notmuch-show-get-date)))
1743
1744 (defun notmuch-show-stash-filename ()
1745   "Copy filename of current message to kill-ring."
1746   (interactive)
1747   (notmuch-common-do-stash (notmuch-show-get-filename)))
1748
1749 (defun notmuch-show-stash-from ()
1750   "Copy From address of current message to kill-ring."
1751   (interactive)
1752   (notmuch-common-do-stash (notmuch-show-get-from)))
1753
1754 (defun notmuch-show-stash-message-id ()
1755   "Copy message ID of current message to kill-ring."
1756   (interactive)
1757   (notmuch-common-do-stash (notmuch-show-get-message-id)))
1758
1759 (defun notmuch-show-stash-message-id-stripped ()
1760   "Copy message ID of current message (sans `id:' prefix) to kill-ring."
1761   (interactive)
1762   (notmuch-common-do-stash (substring (notmuch-show-get-message-id) 4 -1)))
1763
1764 (defun notmuch-show-stash-subject ()
1765   "Copy Subject field of current message to kill-ring."
1766   (interactive)
1767   (notmuch-common-do-stash (notmuch-show-get-subject)))
1768
1769 (defun notmuch-show-stash-tags ()
1770   "Copy tags of current message to kill-ring as a comma separated list."
1771   (interactive)
1772   (notmuch-common-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1773
1774 (defun notmuch-show-stash-to ()
1775   "Copy To address of current message to kill-ring."
1776   (interactive)
1777   (notmuch-common-do-stash (notmuch-show-get-to)))
1778
1779 ;; Commands typically bound to buttons.
1780
1781 (defun notmuch-show-part-button-default (&optional button)
1782   (interactive)
1783   (notmuch-show-part-button-internal button notmuch-show-part-button-default-action))
1784
1785 (defun notmuch-show-part-button-save (&optional button)
1786   (interactive)
1787   (notmuch-show-part-button-internal button #'notmuch-show-save-part))
1788
1789 (defun notmuch-show-part-button-view (&optional button)
1790   (interactive)
1791   (notmuch-show-part-button-internal button #'notmuch-show-view-part))
1792
1793 (defun notmuch-show-part-button-interactively-view (&optional button)
1794   (interactive)
1795   (notmuch-show-part-button-internal button #'notmuch-show-interactively-view-part))
1796
1797 (defun notmuch-show-part-button-internal (button handler)
1798   (let ((button (or button (button-at (point)))))
1799     (if button
1800         (let ((nth (button-get button :notmuch-part)))
1801           (if nth
1802               (funcall handler (notmuch-show-get-message-id) nth
1803                        (button-get button :notmuch-filename)
1804                        (button-get button :notmuch-content-type)))))))
1805
1806 ;;
1807
1808 (provide 'notmuch-show)