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