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