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