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