]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-show.el
emacs: Allow renderer of multipart/related parts access to non-primary parts.
[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
31 (require 'notmuch-lib)
32 (require 'notmuch-query)
33 (require 'notmuch-wash)
34 (require 'notmuch-mua)
35
36 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
37 (declare-function notmuch-fontify-headers "notmuch" nil)
38 (declare-function notmuch-select-tag-with-completion "notmuch" (prompt &rest search-terms))
39 (declare-function notmuch-search-show-thread "notmuch" nil)
40
41 (defcustom notmuch-message-headers '("Subject" "To" "Cc" "Date")
42   "Headers that should be shown in a message, in this order.
43
44 For an open message, all of these headers will be made visible
45 according to `notmuch-message-headers-visible' or can be toggled
46 with `notmuch-show-toggle-headers'. For a closed message, only
47 the first header in the list will be visible."
48   :group 'notmuch
49   :type '(repeat string))
50
51 (defcustom notmuch-message-headers-visible t
52   "Should the headers be visible by default?
53
54 If this value is non-nil, then all of the headers defined in
55 `notmuch-message-headers' will be visible by default in the display
56 of each message. Otherwise, these headers will be hidden and
57 `notmuch-show-toggle-headers' can be used to make the visible for
58 any given message."
59   :group 'notmuch
60   :type 'boolean)
61
62 (defcustom notmuch-show-relative-dates t
63   "Display relative dates in the message summary line."
64   :group 'notmuch
65   :type 'boolean)
66
67 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
68   "A list of functions called to decorate the headers listed in
69 `notmuch-message-headers'.")
70
71 (defcustom notmuch-show-hook nil
72   "Functions called after populating a `notmuch-show' buffer."
73   :group 'notmuch
74   :type 'hook)
75
76 (defcustom notmuch-show-insert-text/plain-hook '(notmuch-wash-excerpt-citations)
77   "Functions used to improve the display of text/plain parts."
78   :group 'notmuch
79   :type 'hook
80   :options '(notmuch-wash-convert-inline-patch-to-part
81              notmuch-wash-wrap-long-lines
82              notmuch-wash-tidy-citations
83              notmuch-wash-elide-blank-lines
84              notmuch-wash-excerpt-citations))
85
86 ;; Mostly useful for debugging.
87 (defcustom notmuch-show-all-multipart/alternative-parts nil
88   "Should all parts of multipart/alternative parts be shown?"
89   :group 'notmuch
90   :type 'boolean)
91
92 (defcustom notmuch-show-indent-multipart nil
93   "Should the sub-parts of a multipart/* part be indented?"
94   ;; dme: Not sure which is a good default.
95   :group 'notmuch
96   :type 'boolean)
97
98 (defmacro with-current-notmuch-show-message (&rest body)
99   "Evaluate body with current buffer set to the text of current message"
100   `(save-excursion
101      (let ((id (notmuch-show-get-message-id)))
102        (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
103          (with-current-buffer buf
104             (call-process notmuch-command nil t nil "show" "--format=raw" id)
105            ,@body)
106          (kill-buffer buf)))))
107
108 (defun notmuch-show-view-all-mime-parts ()
109   "Use external viewers to view all attachments from the current message."
110   (interactive)
111   (with-current-notmuch-show-message
112    ; We ovverride the mm-inline-media-tests to indicate which message
113    ; parts are already sufficiently handled by the original
114    ; presentation of the message in notmuch-show mode. These parts
115    ; will be inserted directly into the temporary buffer of
116    ; with-current-notmuch-show-message and silently discarded.
117    ;
118    ; Any MIME part not explicitly mentioned here will be handled by an
119    ; external viewer as configured in the various mailcap files.
120    (let ((mm-inline-media-tests '(
121                                   ("text/.*" ignore identity)
122                                   ("application/pgp-signature" ignore identity)
123                                   ("multipart/alternative" ignore identity)
124                                   ("multipart/mixed" ignore identity)
125                                   ("multipart/related" ignore identity)
126                                  )))
127      (mm-display-parts (mm-dissect-buffer)))))
128
129 (defun notmuch-foreach-mime-part (function mm-handle)
130   (cond ((stringp (car mm-handle))
131          (dolist (part (cdr mm-handle))
132            (notmuch-foreach-mime-part function part)))
133         ((bufferp (car mm-handle))
134          (funcall function mm-handle))
135         (t (dolist (part mm-handle)
136              (notmuch-foreach-mime-part function part)))))
137
138 (defun notmuch-count-attachments (mm-handle)
139   (let ((count 0))
140     (notmuch-foreach-mime-part
141      (lambda (p)
142        (let ((disposition (mm-handle-disposition p)))
143          (and (listp disposition)
144               (or (equal (car disposition) "attachment")
145                   (and (equal (car disposition) "inline")
146                        (assq 'filename disposition)))
147               (incf count))))
148      mm-handle)
149     count))
150
151 (defun notmuch-save-attachments (mm-handle &optional queryp)
152   (notmuch-foreach-mime-part
153    (lambda (p)
154      (let ((disposition (mm-handle-disposition p)))
155        (and (listp disposition)
156             (or (equal (car disposition) "attachment")
157                 (and (equal (car disposition) "inline")
158                      (assq 'filename disposition)))
159             (or (not queryp)
160                 (y-or-n-p
161                  (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
162             (mm-save-part p))))
163    mm-handle))
164
165 (defun notmuch-show-save-attachments ()
166   "Save all attachments from the current message."
167   (interactive)
168   (with-current-notmuch-show-message
169    (let ((mm-handle (mm-dissect-buffer)))
170      (notmuch-save-attachments
171       mm-handle (> (notmuch-count-attachments mm-handle) 1))))
172   (message "Done"))
173
174 (defun notmuch-show-fontify-header ()
175   (let ((face (cond
176                ((looking-at "[Tt]o:")
177                 'message-header-to)
178                ((looking-at "[Bb]?[Cc][Cc]:")
179                 'message-header-cc)
180                ((looking-at "[Ss]ubject:")
181                 'message-header-subject)
182                ((looking-at "[Ff]rom:")
183                 'message-header-from)
184                (t
185                 'message-header-other))))
186
187     (overlay-put (make-overlay (point) (re-search-forward ":"))
188                  'face 'message-header-name)
189     (overlay-put (make-overlay (point) (re-search-forward ".*$"))
190                  'face face)))
191
192 (defun notmuch-show-colour-headers ()
193   "Apply some colouring to the current headers."
194   (goto-char (point-min))
195   (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
196     (notmuch-show-fontify-header)
197     (forward-line)))
198
199 (defun notmuch-show-spaces-n (n)
200   "Return a string comprised of `n' spaces."
201   (make-string n ? ))
202
203 (defun notmuch-show-update-tags (tags)
204   "Update the displayed tags of the current message."
205   (save-excursion
206     (goto-char (notmuch-show-message-top))
207     (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
208         (let ((inhibit-read-only t))
209           (replace-match (concat "("
210                                  (propertize (mapconcat 'identity tags " ")
211                                              'face 'notmuch-tag-face)
212                                  ")"))))))
213
214 (defun notmuch-show-insert-headerline (headers date tags depth)
215   "Insert a notmuch style headerline based on HEADERS for a
216 message at DEPTH in the current thread."
217   (let ((start (point)))
218     (insert (notmuch-show-spaces-n depth)
219             (plist-get headers :From)
220             " ("
221             date
222             ") ("
223             (propertize (mapconcat 'identity tags " ")
224                         'face 'notmuch-tag-face)
225             ")\n")
226     (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
227
228 (defun notmuch-show-insert-header (header header-value)
229   "Insert a single header."
230   (insert header ": " header-value "\n"))
231
232 (defun notmuch-show-insert-headers (headers)
233   "Insert the headers of the current message."
234   (let ((start (point)))
235     (mapc '(lambda (header)
236              (let* ((header-symbol (intern (concat ":" header)))
237                     (header-value (plist-get headers header-symbol)))
238                (if (and header-value
239                         (not (string-equal "" header-value)))
240                    (notmuch-show-insert-header header header-value))))
241           notmuch-message-headers)
242     (save-excursion
243       (save-restriction
244         (narrow-to-region start (point-max))
245         (run-hooks 'notmuch-show-markup-headers-hook)))))
246
247 (define-button-type 'notmuch-show-part-button-type
248   'action 'notmuch-show-part-button-action
249   'follow-link t
250   'face 'message-mml)
251
252 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name comment)
253   (insert-button
254    (concat "[ "
255            (if name (concat name ": ") "")
256            declared-type
257            (if (not (string-equal declared-type content-type))
258                (concat " (as " content-type ")")
259              "")
260            (or comment "")
261            " ]\n")
262    :type 'notmuch-show-part-button-type
263    :notmuch-part nth
264    :notmuch-filename name))
265
266 ;; Functions handling particular MIME parts.
267
268 (defun notmuch-show-save-part (message-id nth &optional filename)
269   (with-temp-buffer
270     ;; Always acquires the part via `notmuch part', even if it is
271     ;; available in the JSON output.
272     (insert (notmuch-show-get-bodypart-internal message-id nth))
273     (let ((file (read-file-name
274                  "Filename to save as: "
275                  (or mailcap-download-directory "~/")
276                  nil nil
277                  filename))
278           (require-final-newline nil)
279           (coding-system-for-write 'no-conversion))
280       (write-region (point-min) (point-max) file))))
281
282 (defun notmuch-show-mm-display-part-inline (msg part content-type content)
283   "Use the mm-decode/mm-view functions to display a part in the
284 current buffer, if possible."
285   (let ((display-buffer (current-buffer)))
286     (with-temp-buffer
287       (insert content)
288       (let ((handle (mm-make-handle (current-buffer) (list content-type))))
289         (set-buffer display-buffer)
290         (if (and (mm-inlinable-p handle)
291                  (mm-inlined-p handle))
292             (progn
293               (mm-display-part handle)
294               t)
295           nil)))))
296
297 (defvar notmuch-show-multipart/alternative-discouraged
298   '(
299     ;; Avoid HTML parts.
300     "text/html"
301     ;; multipart/related usually contain a text/html part and some associated graphics.
302     "multipart/related"
303     ))
304
305 (defun notmuch-show-multipart/*-to-list (part)
306   (mapcar '(lambda (inner-part) (plist-get inner-part :content-type))
307           (plist-get part :content)))
308
309 (defun notmuch-show-multipart/alternative-choose (types)
310   ;; Based on `mm-preferred-alternative-precedence'.
311   (let ((seq types))
312     (dolist (pref (reverse notmuch-show-multipart/alternative-discouraged))
313       (dolist (elem (copy-sequence seq))
314         (when (string-match pref elem)
315           (setq seq (nconc (delete elem seq) (list elem))))))
316     seq))
317
318 (defun notmuch-show-insert-part-multipart/alternative (msg part content-type nth depth declared-type)
319   (notmuch-show-insert-part-header nth declared-type content-type nil)
320   (let ((chosen-type (car (notmuch-show-multipart/alternative-choose (notmuch-show-multipart/*-to-list part))))
321         (inner-parts (plist-get part :content))
322         (start (point)))
323     ;; This inserts all parts of the chosen type rather than just one,
324     ;; but it's not clear that this is the wrong thing to do - which
325     ;; should be chosen if there are more than one that match?
326     (mapc (lambda (inner-part)
327             (let ((inner-type (plist-get inner-part :content-type)))
328               (if (or notmuch-show-all-multipart/alternative-parts
329                       (string= chosen-type inner-type))
330                   (notmuch-show-insert-bodypart msg inner-part depth)
331                 (notmuch-show-insert-part-header (plist-get inner-part :id) inner-type inner-type nil " (not shown)"))))
332           inner-parts)
333
334     (when notmuch-show-indent-multipart
335       (indent-rigidly start (point) 1)))
336   t)
337
338 (defun notmuch-show-setup-w3m ()
339   "Instruct w3m how to retrieve content from a \"related\" part of a message."
340   (interactive)
341   (if (boundp 'w3m-cid-retrieve-function-alist)
342     (unless (assq 'notmuch-show-mode w3m-cid-retrieve-function-alist)
343       (push (cons 'notmuch-show-mode 'notmuch-show-w3m-cid-retrieve)
344             w3m-cid-retrieve-function-alist)))
345   (setq mm-inline-text-html-with-images t))
346
347 (defvar w3m-current-buffer) ;; From `w3m.el'.
348 (defvar notmuch-show-w3m-cid-store nil)
349 (make-variable-buffer-local 'notmuch-show-w3m-cid-store)
350
351 (defun notmuch-show-w3m-cid-store-internal (content-id
352                                             message-id
353                                             part-number
354                                             content-type
355                                             content)
356   (push (list content-id
357               message-id
358               part-number
359               content-type
360               content)
361         notmuch-show-w3m-cid-store))
362
363 (defun notmuch-show-w3m-cid-store (msg part)
364   (let ((content-id (plist-get part :content-id)))
365     (when content-id
366       (notmuch-show-w3m-cid-store-internal (concat "cid:" content-id)
367                                            (plist-get msg :id)
368                                            (plist-get part :id)
369                                            (plist-get part :content-type)
370                                            nil))))
371
372 (defun notmuch-show-w3m-cid-retrieve (url &rest args)
373   (let ((matching-part (with-current-buffer w3m-current-buffer
374                          (assoc url notmuch-show-w3m-cid-store))))
375     (if matching-part
376         (let ((message-id (nth 1 matching-part))
377               (part-number (nth 2 matching-part))
378               (content-type (nth 3 matching-part))
379               (content (nth 4 matching-part)))
380           ;; If we don't already have the content, get it and cache
381           ;; it, as some messages reference the same cid: part many
382           ;; times (hundreds!), which results in many calls to
383           ;; `notmuch part'.
384           (unless content
385             (setq content (notmuch-show-get-bodypart-internal (concat "id:" message-id)
386                                                               part-number))
387             (with-current-buffer w3m-current-buffer
388               (notmuch-show-w3m-cid-store-internal url
389                                                    message-id
390                                                    part-number
391                                                    content-type
392                                                    content)))
393           (insert content)
394           content-type)
395       nil)))
396
397 (defun notmuch-show-insert-part-multipart/related (msg part content-type nth depth declared-type)
398   (notmuch-show-insert-part-header nth declared-type content-type nil)
399   (let ((inner-parts (plist-get part :content))
400         (start (point)))
401
402     ;; We assume that the first part is text/html and the remainder
403     ;; things that it references.
404
405     ;; Stash the non-primary parts.
406     (mapc (lambda (part)
407             (notmuch-show-w3m-cid-store msg part))
408           (cdr inner-parts))
409
410     ;; Render the primary part.
411     (notmuch-show-insert-bodypart msg (car inner-parts) depth)
412
413     (when notmuch-show-indent-multipart
414       (indent-rigidly start (point) 1)))
415   t)
416
417 (defun notmuch-show-insert-part-multipart/* (msg part content-type nth depth declared-type)
418   (notmuch-show-insert-part-header nth declared-type content-type nil)
419   (let ((inner-parts (plist-get part :content))
420         (start (point)))
421     ;; Show all of the parts.
422     (mapc (lambda (inner-part)
423             (notmuch-show-insert-bodypart msg inner-part depth))
424           inner-parts)
425
426     (when notmuch-show-indent-multipart
427       (indent-rigidly start (point) 1)))
428   t)
429
430 (defun notmuch-show-insert-part-message/rfc822 (msg part content-type nth depth declared-type)
431   (let* ((message-part (plist-get part :content))
432          (inner-parts (plist-get message-part :content)))
433     (notmuch-show-insert-part-header nth declared-type content-type nil)
434     ;; Override `notmuch-message-headers' to force `From' to be
435     ;; displayed.
436     (let ((notmuch-message-headers '("From" "Subject" "To" "Cc" "Date")))
437       (notmuch-show-insert-headers (plist-get part :headers)))
438     ;; Blank line after headers to be compatible with the normal
439     ;; message display.
440     (insert "\n")
441
442     ;; Show all of the parts.
443     (mapc (lambda (inner-part)
444             (notmuch-show-insert-bodypart msg inner-part depth))
445           inner-parts))
446   t)
447
448 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
449   (let ((start (point)))
450     ;; If this text/plain part is not the first part in the message,
451     ;; insert a header to make this clear.
452     (if (> nth 1)
453         (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
454     (insert (notmuch-show-get-bodypart-content msg part nth))
455     (save-excursion
456       (save-restriction
457         (narrow-to-region start (point-max))
458         (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
459   t)
460
461 (defun notmuch-show-insert-part-text/x-vcalendar (msg part content-type nth depth declared-type)
462   (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename))
463   (insert (with-temp-buffer
464             (insert (notmuch-show-get-bodypart-content msg part nth))
465             (goto-char (point-min))
466             (let ((file (make-temp-file "notmuch-ical"))
467                   result)
468               (icalendar--convert-ical-to-diary
469                (icalendar--read-element nil nil)
470                file t)
471               (set-buffer (get-file-buffer file))
472               (setq result (buffer-substring (point-min) (point-max)))
473               (set-buffer-modified-p nil)
474               (kill-buffer (current-buffer))
475               (delete-file file)
476               result)))
477   t)
478
479 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
480   ;; If we can deduce a MIME type from the filename of the attachment,
481   ;; do so and pass it on to the handler for that type.
482   (if (plist-get part :filename)
483       (let ((extension (file-name-extension (plist-get part :filename)))
484             mime-type)
485         (if extension
486             (progn
487               (mailcap-parse-mimetypes)
488               (setq mime-type (mailcap-extension-to-mime extension))
489               (if (and mime-type
490                        (not (string-equal mime-type "application/octet-stream")))
491                   (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
492                 nil))
493           nil))))
494
495 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
496   ;; This handler _must_ succeed - it is the handler of last resort.
497   (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
498   (let ((content (notmuch-show-get-bodypart-content msg part nth)))
499     (if content
500         (notmuch-show-mm-display-part-inline msg part content-type content)))
501   t)
502
503 ;; Functions for determining how to handle MIME parts.
504
505 (defun notmuch-show-split-content-type (content-type)
506   (split-string content-type "/"))
507
508 (defun notmuch-show-handlers-for (content-type)
509   "Return a list of content handlers for a part of type CONTENT-TYPE."
510   (let (result)
511     (mapc (lambda (func)
512             (if (functionp func)
513                 (push func result)))
514           ;; Reverse order of prefrence.
515           (list (intern (concat "notmuch-show-insert-part-*/*"))
516                 (intern (concat
517                          "notmuch-show-insert-part-"
518                          (car (notmuch-show-split-content-type content-type))
519                          "/*"))
520                 (intern (concat "notmuch-show-insert-part-" content-type))))
521     result))
522
523 ;; Helper for parts which are generally not included in the default
524 ;; JSON output.
525
526 (defun notmuch-show-get-bodypart-internal (message-id part-number)
527   (with-temp-buffer
528     (let ((coding-system-for-read 'no-conversion))
529       (call-process notmuch-command nil t nil
530                     "part" (format "--part=%s" part-number) message-id)
531       (buffer-string))))
532
533 (defun notmuch-show-get-bodypart-content (msg part nth)
534   (or (plist-get part :content)
535       (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
536
537 ;; \f
538
539 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
540   (let ((handlers (notmuch-show-handlers-for content-type)))
541     ;; Run the content handlers until one of them returns a non-nil
542     ;; value.
543     (while (and handlers
544                 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
545       (setq handlers (cdr handlers))))
546   t)
547
548 (defun notmuch-show-insert-bodypart (msg part depth)
549   "Insert the body part PART at depth DEPTH in the current thread."
550   (let ((content-type (downcase (plist-get part :content-type)))
551         (nth (plist-get part :id)))
552     (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
553   ;; Some of the body part handlers leave point somewhere up in the
554   ;; part, so we make sure that we're down at the end.
555   (goto-char (point-max))
556   ;; Ensure that the part ends with a carriage return.
557   (if (not (bolp))
558       (insert "\n")))
559
560 (defun notmuch-show-insert-body (msg body depth)
561   "Insert the body BODY at depth DEPTH in the current thread."
562   (mapc '(lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
563
564 (defun notmuch-show-make-symbol (type)
565   (make-symbol (concat "notmuch-show-" type)))
566
567 (defun notmuch-show-strip-re (string)
568   (replace-regexp-in-string "\\([Rr]e: *\\)+" "" string))
569
570 (defvar notmuch-show-previous-subject "")
571 (make-variable-buffer-local 'notmuch-show-previous-subject)
572
573 (defun notmuch-show-insert-msg (msg depth)
574   "Insert the message MSG at depth DEPTH in the current thread."
575   (let* ((headers (plist-get msg :headers))
576          ;; Indentation causes the buffer offset of the start/end
577          ;; points to move, so we must use markers.
578          message-start message-end
579          content-start content-end
580          headers-start headers-end
581          body-start body-end
582          (headers-invis-spec (notmuch-show-make-symbol "header"))
583          (message-invis-spec (notmuch-show-make-symbol "message"))
584          (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
585
586     ;; Set `buffer-invisibility-spec' to `nil' (a list), otherwise
587     ;; removing items from `buffer-invisibility-spec' (which is what
588     ;; `notmuch-show-headers-visible' and
589     ;; `notmuch-show-message-visible' do) is a no-op and has no
590     ;; effect. This caused threads with only matching messages to have
591     ;; those messages hidden initially because
592     ;; `buffer-invisibility-spec' stayed `t'.
593     ;;
594     ;; This needs to be set here (rather than just above the call to
595     ;; `notmuch-show-headers-visible') because some of the part
596     ;; rendering or body washing functions
597     ;; (e.g. `notmuch-wash-text/plain-citations') manipulate
598     ;; `buffer-invisibility-spec').
599     (when (eq buffer-invisibility-spec t)
600       (setq buffer-invisibility-spec nil))
601
602     (setq message-start (point-marker))
603
604     (notmuch-show-insert-headerline headers
605                                     (or (if notmuch-show-relative-dates
606                                             (plist-get msg :date_relative)
607                                           nil)
608                                         (plist-get headers :Date))
609                                     (plist-get msg :tags) depth)
610
611     (setq content-start (point-marker))
612
613     ;; Set `headers-start' to point after the 'Subject:' header to be
614     ;; compatible with the existing implementation. This just sets it
615     ;; to after the first header.
616     (notmuch-show-insert-headers headers)
617     ;; Headers should include a blank line (backwards compatibility).
618     (insert "\n")
619     (save-excursion
620       (goto-char content-start)
621       ;; If the subject of this message is the same as that of the
622       ;; previous message, don't display it when this message is
623       ;; collapsed.
624       (when (not (string= notmuch-show-previous-subject
625                           bare-subject))
626         (forward-line 1))
627       (setq headers-start (point-marker)))
628     (setq headers-end (point-marker))
629
630     (setq notmuch-show-previous-subject bare-subject)
631
632     (setq body-start (point-marker))
633     (notmuch-show-insert-body msg (plist-get msg :body) depth)
634     ;; Ensure that the body ends with a newline.
635     (if (not (bolp))
636         (insert "\n"))
637     (setq body-end (point-marker))
638     (setq content-end (point-marker))
639
640     ;; Indent according to the depth in the thread.
641     (indent-rigidly content-start content-end depth)
642
643     (setq message-end (point-max-marker))
644
645     ;; Save the extents of this message over the whole text of the
646     ;; message.
647     (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
648
649     (plist-put msg :headers-invis-spec headers-invis-spec)
650     (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
651
652     (plist-put msg :message-invis-spec message-invis-spec)
653     (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
654
655     ;; Save the properties for this message. Currently this saves the
656     ;; entire message (augmented it with other stuff), which seems
657     ;; like overkill. We might save a reduced subset (for example, not
658     ;; the content).
659     (notmuch-show-set-message-properties msg)
660
661     ;; Set header visibility.
662     (notmuch-show-headers-visible msg notmuch-message-headers-visible)
663
664     ;; Message visibility depends on whether it matched the search
665     ;; criteria.
666     (notmuch-show-message-visible msg (plist-get msg :match))))
667
668 (defun notmuch-show-insert-tree (tree depth)
669   "Insert the message tree TREE at depth DEPTH in the current thread."
670   (let ((msg (car tree))
671         (replies (cadr tree)))
672     (notmuch-show-insert-msg msg depth)
673     (notmuch-show-insert-thread replies (1+ depth))))
674
675 (defun notmuch-show-insert-thread (thread depth)
676   "Insert the thread THREAD at depth DEPTH in the current forest."
677   (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
678
679 (defun notmuch-show-insert-forest (forest)
680   "Insert the forest of threads FOREST."
681   (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
682
683 (defvar notmuch-show-parent-buffer nil)
684
685 ;;;###autoload
686 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
687   "Run \"notmuch show\" with the given thread ID and display results.
688
689 The optional PARENT-BUFFER is the notmuch-search buffer from
690 which this notmuch-show command was executed, (so that the
691 next thread from that buffer can be show when done with this
692 one).
693
694 The optional QUERY-CONTEXT is a notmuch search term. Only
695 messages from the thread matching this search term are shown if
696 non-nil.
697
698 The optional BUFFER-NAME provides the neame of the buffer in
699 which the message thread is shown. If it is nil (which occurs
700 when the command is called interactively) the argument to the
701 function is used. "
702   (interactive "sNotmuch show: ")
703   (let ((buffer (get-buffer-create (generate-new-buffer-name
704                                     (or buffer-name
705                                         (concat "*notmuch-" thread-id "*")))))
706         (inhibit-read-only t))
707     (switch-to-buffer buffer)
708     (notmuch-show-mode)
709     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
710     (erase-buffer)
711     (goto-char (point-min))
712     (save-excursion
713       (let* ((basic-args (list thread-id))
714              (args (if query-context
715                        (append (list "\'") basic-args (list "and (" query-context ")\'"))
716                      (append (list "\'") basic-args (list "\'")))))
717         (notmuch-show-insert-forest (notmuch-query-get-threads args))
718         ;; If the query context reduced the results to nothing, run
719         ;; the basic query.
720         (when (and (eq (buffer-size) 0)
721                    query-context)
722           (notmuch-show-insert-forest
723            (notmuch-query-get-threads basic-args))))
724
725       ;; Enable buttonisation of URLs and email addresses in the
726       ;; buffer.
727       (goto-address-mode t)
728       ;; Act on visual lines rather than logical lines.
729       (visual-line-mode t)
730
731       (run-hooks 'notmuch-show-hook))
732
733     ;; Move straight to the first open message
734     (if (not (notmuch-show-message-visible-p))
735         (notmuch-show-next-open-message))
736
737     ;; Set the header line to the subject of the first open message.
738     (setq header-line-format (notmuch-show-strip-re (notmuch-show-get-subject)))
739
740     (notmuch-show-mark-read)))
741
742 (defvar notmuch-show-stash-map
743   (let ((map (make-sparse-keymap)))
744     (define-key map "c" 'notmuch-show-stash-cc)
745     (define-key map "d" 'notmuch-show-stash-date)
746     (define-key map "F" 'notmuch-show-stash-filename)
747     (define-key map "f" 'notmuch-show-stash-from)
748     (define-key map "i" 'notmuch-show-stash-message-id)
749     (define-key map "s" 'notmuch-show-stash-subject)
750     (define-key map "T" 'notmuch-show-stash-tags)
751     (define-key map "t" 'notmuch-show-stash-to)
752     map)
753   "Submap for stash commands")
754 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
755
756 (defvar notmuch-show-mode-map
757       (let ((map (make-sparse-keymap)))
758         (define-key map "?" 'notmuch-help)
759         (define-key map "q" 'notmuch-kill-this-buffer)
760         (define-key map (kbd "<C-tab>") 'widget-backward)
761         (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
762         (define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
763         (define-key map (kbd "TAB") 'notmuch-show-next-button)
764         (define-key map "s" 'notmuch-search)
765         (define-key map "m" 'notmuch-mua-mail)
766         (define-key map "f" 'notmuch-show-forward-message)
767         (define-key map "r" 'notmuch-show-reply)
768         (define-key map "|" 'notmuch-show-pipe-message)
769         (define-key map "w" 'notmuch-show-save-attachments)
770         (define-key map "V" 'notmuch-show-view-raw-message)
771         (define-key map "v" 'notmuch-show-view-all-mime-parts)
772         (define-key map "c" 'notmuch-show-stash-map)
773         (define-key map "h" 'notmuch-show-toggle-headers)
774         (define-key map "-" 'notmuch-show-remove-tag)
775         (define-key map "+" 'notmuch-show-add-tag)
776         (define-key map "x" 'notmuch-show-archive-thread-then-exit)
777         (define-key map "a" 'notmuch-show-archive-thread)
778         (define-key map "N" 'notmuch-show-next-message)
779         (define-key map "P" 'notmuch-show-previous-message)
780         (define-key map "n" 'notmuch-show-next-open-message)
781         (define-key map "p" 'notmuch-show-previous-open-message)
782         (define-key map (kbd "DEL") 'notmuch-show-rewind)
783         (define-key map " " 'notmuch-show-advance-and-archive)
784         (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
785         (define-key map (kbd "RET") 'notmuch-show-toggle-message)
786         map)
787       "Keymap for \"notmuch show\" buffers.")
788 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
789
790 (defun notmuch-show-mode ()
791   "Major mode for viewing a thread with notmuch.
792
793 This buffer contains the results of the \"notmuch show\" command
794 for displaying a single thread of email from your email archives.
795
796 By default, various components of email messages, (citations,
797 signatures, already-read messages), are hidden. You can make
798 these parts visible by clicking with the mouse button or by
799 pressing RET after positioning the cursor on a hidden part, (for
800 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
801
802 Reading the thread sequentially is well-supported by pressing
803 \\[notmuch-show-advance-and-archive]. This will scroll the current message (if necessary), advance
804 to the next message, or advance to the next thread (if already on
805 the last message of a thread).
806
807 Other commands are available to read or manipulate the thread
808 more selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages
809 without removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread
810 without scrolling through with \\[notmuch-show-advance-and-archive]).
811
812 You can add or remove arbitary tags from the current message with
813 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
814
815 All currently available key bindings:
816
817 \\{notmuch-show-mode-map}"
818   (interactive)
819   (kill-all-local-variables)
820   (use-local-map notmuch-show-mode-map)
821   (setq major-mode 'notmuch-show-mode
822         mode-name "notmuch-show")
823   (setq buffer-read-only t))
824
825 (defun notmuch-show-move-to-message-top ()
826   (goto-char (notmuch-show-message-top)))
827
828 (defun notmuch-show-move-to-message-bottom ()
829   (goto-char (notmuch-show-message-bottom)))
830
831 (defun notmuch-show-message-adjust ()
832   (recenter 0))
833
834 ;; Movement related functions.
835
836 ;; There's some strangeness here where a text property applied to a
837 ;; region a->b is not found when point is at b. We walk backwards
838 ;; until finding the property.
839 (defun notmuch-show-message-extent ()
840   (let (r)
841     (save-excursion
842       (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
843         (backward-char)))
844     r))
845
846 (defun notmuch-show-message-top ()
847   (car (notmuch-show-message-extent)))
848
849 (defun notmuch-show-message-bottom ()
850   (cdr (notmuch-show-message-extent)))
851
852 (defun notmuch-show-goto-message-next ()
853   (let ((start (point)))
854     (notmuch-show-move-to-message-bottom)
855     (if (not (eobp))
856         t
857       (goto-char start)
858       nil)))
859
860 (defun notmuch-show-goto-message-previous ()
861   (notmuch-show-move-to-message-top)
862   (if (bobp)
863       nil
864     (backward-char)
865     (notmuch-show-move-to-message-top)
866     t))
867
868 (defun notmuch-show-move-past-invisible-forward ()
869   (while (point-invisible-p)
870     (forward-char)))
871
872 (defun notmuch-show-move-past-invisible-backward ()
873   (while (point-invisible-p)
874     (backward-char)))
875
876 ;; Functions relating to the visibility of messages and their
877 ;; components.
878
879 (defun notmuch-show-element-visible (props visible-p spec-property)
880   (let ((spec (plist-get props spec-property)))
881     (if visible-p
882         (remove-from-invisibility-spec spec)
883       (add-to-invisibility-spec spec))))
884
885 (defun notmuch-show-message-visible (props visible-p)
886   (if visible-p
887       ;; When making the message visible, the headers may or not be
888       ;; visible. So we check that property separately.
889       (let ((headers-visible (plist-get props :headers-visible)))
890         (notmuch-show-element-visible props headers-visible :headers-invis-spec)
891         (notmuch-show-element-visible props t :message-invis-spec))
892     (notmuch-show-element-visible props nil :headers-invis-spec)
893     (notmuch-show-element-visible props nil :message-invis-spec))
894
895   (notmuch-show-set-prop :message-visible visible-p props))
896
897 (defun notmuch-show-headers-visible (props visible-p)
898   (if (plist-get props :message-visible)
899       (notmuch-show-element-visible props visible-p :headers-invis-spec))
900   (notmuch-show-set-prop :headers-visible visible-p props))
901
902 ;; Functions for setting and getting attributes of the current
903 ;; message.
904
905 (defun notmuch-show-set-message-properties (props)
906   (save-excursion
907     (notmuch-show-move-to-message-top)
908     (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
909
910 (defun notmuch-show-get-message-properties ()
911   (save-excursion
912     (notmuch-show-move-to-message-top)
913     (get-text-property (point) :notmuch-message-properties)))
914
915 (defun notmuch-show-set-prop (prop val &optional props)
916   (let ((inhibit-read-only t)
917         (props (or props
918                    (notmuch-show-get-message-properties))))
919     (plist-put props prop val)
920     (notmuch-show-set-message-properties props)))
921
922 (defun notmuch-show-get-prop (prop &optional props)
923   (let ((props (or props
924                    (notmuch-show-get-message-properties))))
925     (plist-get props prop)))
926
927 (defun notmuch-show-get-message-id ()
928   "Return the message id of the current message."
929   (concat "id:\"" (notmuch-show-get-prop :id) "\""))
930
931 ;; dme: Would it make sense to use a macro for many of these?
932
933 (defun notmuch-show-get-filename ()
934   "Return the filename of the current message."
935   (notmuch-show-get-prop :filename))
936
937 (defun notmuch-show-get-header (header)
938   "Return the named header of the current message, if any."
939   (plist-get (notmuch-show-get-prop :headers) header))
940
941 (defun notmuch-show-get-cc ()
942   (notmuch-show-get-header :Cc))
943
944 (defun notmuch-show-get-date ()
945   (notmuch-show-get-header :Date))
946
947 (defun notmuch-show-get-from ()
948   (notmuch-show-get-header :From))
949
950 (defun notmuch-show-get-subject ()
951   (notmuch-show-get-header :Subject))
952
953 (defun notmuch-show-get-to ()
954   (notmuch-show-get-header :To))
955
956 (defun notmuch-show-set-tags (tags)
957   "Set the tags of the current message."
958   (notmuch-show-set-prop :tags tags)
959   (notmuch-show-update-tags tags))
960
961 (defun notmuch-show-get-tags ()
962   "Return the tags of the current message."
963   (notmuch-show-get-prop :tags))
964
965 (defun notmuch-show-message-visible-p ()
966   "Is the current message visible?"
967   (notmuch-show-get-prop :message-visible))
968
969 (defun notmuch-show-headers-visible-p ()
970   "Are the headers of the current message visible?"
971   (notmuch-show-get-prop :headers-visible))
972
973 (defun notmuch-show-mark-read ()
974   "Mark the current message as read."
975   (notmuch-show-remove-tag "unread"))
976
977 ;; Functions for getting attributes of several messages in the current
978 ;; thread.
979
980 (defun notmuch-show-get-message-ids-for-open-messages ()
981   "Return a list of all message IDs for open messages in the current thread."
982   (save-excursion
983     (let (message-ids done)
984       (goto-char (point-min))
985       (while (not done)
986         (if (notmuch-show-message-visible-p)
987             (setq message-ids (append message-ids (list (notmuch-show-get-message-id)))))
988         (setq done (not (notmuch-show-goto-message-next)))
989         )
990       message-ids
991       )))
992
993 ;; Commands typically bound to keys.
994
995 (defun notmuch-show-advance-and-archive ()
996   "Advance through thread and archive.
997
998 This command is intended to be one of the simplest ways to
999 process a thread of email. It does the following:
1000
1001 If the current message in the thread is not yet fully visible,
1002 scroll by a near screenful to read more of the message.
1003
1004 Otherwise, (the end of the current message is already within the
1005 current window), advance to the next open message.
1006
1007 Finally, if there is no further message to advance to, and this
1008 last message is already read, then archive the entire current
1009 thread, (remove the \"inbox\" tag from each message). Also kill
1010 this buffer, and display the next thread from the search from
1011 which this thread was originally shown."
1012   (interactive)
1013   (let ((end-of-this-message (notmuch-show-message-bottom)))
1014     (cond
1015      ;; Ideally we would test `end-of-this-message' against the result
1016      ;; of `window-end', but that doesn't account for the fact that
1017      ;; the end of the message might be hidden, so we have to actually
1018      ;; go to the end, walk back over invisible text and then see if
1019      ;; point is visible.
1020      ((save-excursion
1021         (goto-char (- end-of-this-message 1))
1022         (notmuch-show-move-past-invisible-backward)
1023         (> (point) (window-end)))
1024       ;; The bottom of this message is not visible - scroll.
1025       (scroll-up nil))
1026
1027      ((not (= end-of-this-message (point-max)))
1028       ;; This is not the last message - move to the next visible one.
1029       (notmuch-show-next-open-message))
1030
1031      (t
1032       ;; This is the last message - archive the thread.
1033       (notmuch-show-archive-thread)))))
1034
1035 (defun notmuch-show-rewind ()
1036   "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
1037
1038 Specifically, if the beginning of the previous email is fewer
1039 than `window-height' lines from the current point, move to it
1040 just like `notmuch-show-previous-message'.
1041
1042 Otherwise, just scroll down a screenful of the current message.
1043
1044 This command does not modify any message tags, (it does not undo
1045 any effects from previous calls to
1046 `notmuch-show-advance-and-archive'."
1047   (interactive)
1048   (let ((start-of-message (notmuch-show-message-top))
1049         (start-of-window (window-start)))
1050     (cond
1051       ;; Either this message is properly aligned with the start of the
1052       ;; window or the start of this message is not visible on the
1053       ;; screen - scroll.
1054      ((or (= start-of-message start-of-window)
1055           (< start-of-message start-of-window))
1056       (scroll-down)
1057       ;; If a small number of lines from the previous message are
1058       ;; visible, realign so that the top of the current message is at
1059       ;; the top of the screen.
1060       (if (<= (count-screen-lines (window-start) start-of-message)
1061               next-screen-context-lines)
1062           (progn
1063             (goto-char (notmuch-show-message-top))
1064             (notmuch-show-message-adjust)))
1065       ;; Move to the top left of the window.
1066       (goto-char (window-start)))
1067      (t
1068       ;; Move to the previous message.
1069       (notmuch-show-previous-message)))))
1070
1071 (defun notmuch-show-reply ()
1072   "Reply to the current message."
1073   (interactive)
1074   (notmuch-mua-reply (notmuch-show-get-message-id)))
1075
1076 (defun notmuch-show-forward-message ()
1077   "Forward the current message."
1078   (interactive)
1079   (with-current-notmuch-show-message
1080    (notmuch-mua-forward-message)))
1081
1082 (defun notmuch-show-next-message ()
1083   "Show the next message."
1084   (interactive)
1085   (if (notmuch-show-goto-message-next)
1086       (progn
1087         (notmuch-show-mark-read)
1088         (notmuch-show-message-adjust))
1089     (goto-char (point-max))))
1090
1091 (defun notmuch-show-previous-message ()
1092   "Show the previous message."
1093   (interactive)
1094   (notmuch-show-goto-message-previous)
1095   (notmuch-show-mark-read)
1096   (notmuch-show-message-adjust))
1097
1098 (defun notmuch-show-next-open-message ()
1099   "Show the next message."
1100   (interactive)
1101   (let (r)
1102     (while (and (setq r (notmuch-show-goto-message-next))
1103                 (not (notmuch-show-message-visible-p))))
1104     (if r
1105         (progn
1106           (notmuch-show-mark-read)
1107           (notmuch-show-message-adjust))
1108       (goto-char (point-max)))))
1109
1110 (defun notmuch-show-previous-open-message ()
1111   "Show the previous message."
1112   (interactive)
1113   (while (and (notmuch-show-goto-message-previous)
1114               (not (notmuch-show-message-visible-p))))
1115   (notmuch-show-mark-read)
1116   (notmuch-show-message-adjust))
1117
1118 (defun notmuch-show-view-raw-message ()
1119   "View the file holding the current message."
1120   (interactive)
1121   (let* ((id (notmuch-show-get-message-id))
1122          (buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
1123     (call-process notmuch-command nil buf nil "show" "--format=raw" id)
1124     (switch-to-buffer buf)
1125     (goto-char (point-min))
1126     (set-buffer-modified-p nil)
1127     (view-buffer buf 'kill-buffer-if-not-modified)))
1128
1129 (defun notmuch-show-pipe-message (entire-thread command)
1130   "Pipe the contents of the current message (or thread) to the given command.
1131
1132 The given command will be executed with the raw contents of the
1133 current email message as stdin. Anything printed by the command
1134 to stdout or stderr will appear in the *notmuch-pipe* buffer.
1135
1136 When invoked with a prefix argument, the command will receive all
1137 open messages in the current thread (formatted as an mbox) rather
1138 than only the current message."
1139   (interactive "P\nsPipe message to command: ")
1140   (let (shell-command)
1141     (if entire-thread
1142         (setq shell-command 
1143               (concat notmuch-command " show --format=mbox "
1144                       (shell-quote-argument
1145                        (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
1146                       " | " command))
1147       (setq shell-command
1148             (concat notmuch-command " show --format=raw "
1149                     (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
1150     (let ((buf (get-buffer-create (concat "*notmuch-pipe*"))))
1151       (with-current-buffer buf
1152         (setq buffer-read-only nil)
1153         (erase-buffer)
1154         (let ((exit-code (call-process-shell-command shell-command nil buf)))
1155           (goto-char (point-max))
1156           (set-buffer-modified-p nil)
1157           (setq buffer-read-only t)
1158           (unless (zerop exit-code)
1159             (switch-to-buffer-other-window buf)
1160             (message (format "Command '%s' exited abnormally with code %d"
1161                              shell-command exit-code))))))))
1162
1163 (defun notmuch-show-add-tags-worker (current-tags add-tags)
1164   "Add to `current-tags' with any tags from `add-tags' not
1165 currently present and return the result."
1166   (let ((result-tags (copy-sequence current-tags)))
1167     (mapc (lambda (add-tag)
1168             (unless (member add-tag current-tags)
1169               (setq result-tags (push add-tag result-tags))))
1170             add-tags)
1171     (sort result-tags 'string<)))
1172
1173 (defun notmuch-show-del-tags-worker (current-tags del-tags)
1174   "Remove any tags in `del-tags' from `current-tags' and return
1175 the result."
1176   (let ((result-tags (copy-sequence current-tags)))
1177     (mapc (lambda (del-tag)
1178             (setq result-tags (delete del-tag result-tags)))
1179           del-tags)
1180     result-tags))
1181
1182 (defun notmuch-show-add-tag (&rest toadd)
1183   "Add a tag to the current message."
1184   (interactive
1185    (list (notmuch-select-tag-with-completion "Tag to add: ")))
1186
1187   (let* ((current-tags (notmuch-show-get-tags))
1188          (new-tags (notmuch-show-add-tags-worker current-tags toadd)))
1189
1190     (unless (equal current-tags new-tags)
1191       (apply 'notmuch-call-notmuch-process
1192              (append (cons "tag"
1193                            (mapcar (lambda (s) (concat "+" s)) toadd))
1194                      (cons (notmuch-show-get-message-id) nil)))
1195       (notmuch-show-set-tags new-tags))))
1196
1197 (defun notmuch-show-remove-tag (&rest toremove)
1198   "Remove a tag from the current message."
1199   (interactive
1200    (list (notmuch-select-tag-with-completion
1201           "Tag to remove: " (notmuch-show-get-message-id))))
1202
1203   (let* ((current-tags (notmuch-show-get-tags))
1204          (new-tags (notmuch-show-del-tags-worker current-tags toremove)))
1205
1206     (unless (equal current-tags new-tags)
1207       (apply 'notmuch-call-notmuch-process
1208              (append (cons "tag"
1209                            (mapcar (lambda (s) (concat "-" s)) toremove))
1210                      (cons (notmuch-show-get-message-id) nil)))
1211       (notmuch-show-set-tags new-tags))))
1212
1213 (defun notmuch-show-toggle-headers ()
1214   "Toggle the visibility of the current message headers."
1215   (interactive)
1216   (let ((props (notmuch-show-get-message-properties)))
1217     (notmuch-show-headers-visible
1218      props
1219      (not (plist-get props :headers-visible))))
1220   (force-window-update))
1221
1222 (defun notmuch-show-toggle-message ()
1223   "Toggle the visibility of the current message."
1224   (interactive)
1225   (let ((props (notmuch-show-get-message-properties)))
1226     (notmuch-show-message-visible
1227      props
1228      (not (plist-get props :message-visible))))
1229   (force-window-update))
1230
1231 (defun notmuch-show-open-or-close-all ()
1232   "Set the visibility all of the messages in the current thread.
1233 By default make all of the messages visible. With a prefix
1234 argument, hide all of the messages."
1235   (interactive)
1236   (save-excursion
1237     (goto-char (point-min))
1238     (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1239                                            (not current-prefix-arg))
1240           until (not (notmuch-show-goto-message-next))))
1241   (force-window-update))
1242
1243 (defun notmuch-show-next-button ()
1244   "Advance point to the next button in the buffer."
1245   (interactive)
1246   (forward-button 1))
1247
1248 (defun notmuch-show-previous-button ()
1249   "Move point back to the previous button in the buffer."
1250   (interactive)
1251   (backward-button 1))
1252
1253 (defun notmuch-show-archive-thread-internal (show-next)
1254   ;; Remove the tag from the current set of messages.
1255   (goto-char (point-min))
1256   (loop do (notmuch-show-remove-tag "inbox")
1257         until (not (notmuch-show-goto-message-next)))
1258   ;; Move to the next item in the search results, if any.
1259   (let ((parent-buffer notmuch-show-parent-buffer))
1260     (notmuch-kill-this-buffer)
1261     (if parent-buffer
1262         (progn
1263           (switch-to-buffer parent-buffer)
1264           (forward-line)
1265           (if show-next
1266               (notmuch-search-show-thread))))))
1267
1268 (defun notmuch-show-archive-thread ()
1269   "Archive each message in thread, then show next thread from search.
1270
1271 Archive each message currently shown by removing the \"inbox\"
1272 tag from each. Then kill this buffer and show the next thread
1273 from the search from which this thread was originally shown.
1274
1275 Note: This command is safe from any race condition of new messages
1276 being delivered to the same thread. It does not archive the
1277 entire thread, but only the messages shown in the current
1278 buffer."
1279   (interactive)
1280   (notmuch-show-archive-thread-internal t))
1281
1282 (defun notmuch-show-archive-thread-then-exit ()
1283   "Archive each message in thread, then exit back to search results."
1284   (interactive)
1285   (notmuch-show-archive-thread-internal nil))
1286
1287 (defun notmuch-show-stash-cc ()
1288   "Copy CC field of current message to kill-ring."
1289   (interactive)
1290   (notmuch-common-do-stash (notmuch-show-get-cc)))
1291
1292 (defun notmuch-show-stash-date ()
1293   "Copy date of current message to kill-ring."
1294   (interactive)
1295   (notmuch-common-do-stash (notmuch-show-get-date)))
1296
1297 (defun notmuch-show-stash-filename ()
1298   "Copy filename of current message to kill-ring."
1299   (interactive)
1300   (notmuch-common-do-stash (notmuch-show-get-filename)))
1301
1302 (defun notmuch-show-stash-from ()
1303   "Copy From address of current message to kill-ring."
1304   (interactive)
1305   (notmuch-common-do-stash (notmuch-show-get-from)))
1306
1307 (defun notmuch-show-stash-message-id ()
1308   "Copy message ID of current message to kill-ring."
1309   (interactive)
1310   (notmuch-common-do-stash (notmuch-show-get-message-id)))
1311
1312 (defun notmuch-show-stash-subject ()
1313   "Copy Subject field of current message to kill-ring."
1314   (interactive)
1315   (notmuch-common-do-stash (notmuch-show-get-subject)))
1316
1317 (defun notmuch-show-stash-tags ()
1318   "Copy tags of current message to kill-ring as a comma separated list."
1319   (interactive)
1320   (notmuch-common-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1321
1322 (defun notmuch-show-stash-to ()
1323   "Copy To address of current message to kill-ring."
1324   (interactive)
1325   (notmuch-common-do-stash (notmuch-show-get-to)))
1326
1327 ;; Commands typically bound to buttons.
1328
1329 (defun notmuch-show-part-button-action (button)
1330   (let ((nth (button-get button :notmuch-part)))
1331     (if nth
1332         (notmuch-show-save-part (notmuch-show-get-message-id) nth
1333                                 (button-get button :notmuch-filename))
1334       (message "Not a valid part (is it a fake part?)."))))
1335
1336 ;;
1337
1338 (provide 'notmuch-show)