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