]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-show.el
emacs: Optionally show all parts in multipart/alternative.
[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-insert-part-multipart/* (msg part content-type nth depth declared-type)
339   (notmuch-show-insert-part-header nth declared-type content-type nil)
340   (let ((inner-parts (plist-get part :content))
341         (start (point)))
342     ;; Show all of the parts.
343     (mapc (lambda (inner-part)
344             (notmuch-show-insert-bodypart msg inner-part depth))
345           inner-parts)
346
347     (when notmuch-show-indent-multipart
348       (indent-rigidly start (point) 1)))
349   t)
350
351 (defun notmuch-show-insert-part-message/rfc822 (msg part content-type nth depth declared-type)
352   (let* ((message-part (plist-get part :content))
353          (inner-parts (plist-get message-part :content)))
354     (notmuch-show-insert-part-header nth declared-type content-type nil)
355     ;; Override `notmuch-message-headers' to force `From' to be
356     ;; displayed.
357     (let ((notmuch-message-headers '("From" "Subject" "To" "Cc" "Date")))
358       (notmuch-show-insert-headers (plist-get part :headers)))
359     ;; Blank line after headers to be compatible with the normal
360     ;; message display.
361     (insert "\n")
362
363     ;; Show all of the parts.
364     (mapc (lambda (inner-part)
365             (notmuch-show-insert-bodypart msg inner-part depth))
366           inner-parts))
367   t)
368
369 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
370   (let ((start (point)))
371     ;; If this text/plain part is not the first part in the message,
372     ;; insert a header to make this clear.
373     (if (> nth 1)
374         (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
375     (insert (notmuch-show-get-bodypart-content msg part nth))
376     (save-excursion
377       (save-restriction
378         (narrow-to-region start (point-max))
379         (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
380   t)
381
382 (defun notmuch-show-insert-part-text/x-vcalendar (msg part content-type nth depth declared-type)
383   (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename))
384   (insert (with-temp-buffer
385             (insert (notmuch-show-get-bodypart-content msg part nth))
386             (goto-char (point-min))
387             (let ((file (make-temp-file "notmuch-ical"))
388                   result)
389               (icalendar--convert-ical-to-diary
390                (icalendar--read-element nil nil)
391                file t)
392               (set-buffer (get-file-buffer file))
393               (setq result (buffer-substring (point-min) (point-max)))
394               (set-buffer-modified-p nil)
395               (kill-buffer (current-buffer))
396               (delete-file file)
397               result)))
398   t)
399
400 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
401   ;; If we can deduce a MIME type from the filename of the attachment,
402   ;; do so and pass it on to the handler for that type.
403   (if (plist-get part :filename)
404       (let ((extension (file-name-extension (plist-get part :filename)))
405             mime-type)
406         (if extension
407             (progn
408               (mailcap-parse-mimetypes)
409               (setq mime-type (mailcap-extension-to-mime extension))
410               (if (and mime-type
411                        (not (string-equal mime-type "application/octet-stream")))
412                   (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
413                 nil))
414           nil))))
415
416 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
417   ;; This handler _must_ succeed - it is the handler of last resort.
418   (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
419   (let ((content (notmuch-show-get-bodypart-content msg part nth)))
420     (if content
421         (notmuch-show-mm-display-part-inline msg part content-type content)))
422   t)
423
424 ;; Functions for determining how to handle MIME parts.
425
426 (defun notmuch-show-split-content-type (content-type)
427   (split-string content-type "/"))
428
429 (defun notmuch-show-handlers-for (content-type)
430   "Return a list of content handlers for a part of type CONTENT-TYPE."
431   (let (result)
432     (mapc (lambda (func)
433             (if (functionp func)
434                 (push func result)))
435           ;; Reverse order of prefrence.
436           (list (intern (concat "notmuch-show-insert-part-*/*"))
437                 (intern (concat
438                          "notmuch-show-insert-part-"
439                          (car (notmuch-show-split-content-type content-type))
440                          "/*"))
441                 (intern (concat "notmuch-show-insert-part-" content-type))))
442     result))
443
444 ;; Helper for parts which are generally not included in the default
445 ;; JSON output.
446
447 (defun notmuch-show-get-bodypart-internal (message-id part-number)
448   (with-temp-buffer
449     (let ((coding-system-for-read 'no-conversion))
450       (call-process notmuch-command nil t nil
451                     "part" (format "--part=%s" part-number) message-id)
452       (buffer-string))))
453
454 (defun notmuch-show-get-bodypart-content (msg part nth)
455   (or (plist-get part :content)
456       (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
457
458 ;; \f
459
460 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
461   (let ((handlers (notmuch-show-handlers-for content-type)))
462     ;; Run the content handlers until one of them returns a non-nil
463     ;; value.
464     (while (and handlers
465                 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
466       (setq handlers (cdr handlers))))
467   t)
468
469 (defun notmuch-show-insert-bodypart (msg part depth)
470   "Insert the body part PART at depth DEPTH in the current thread."
471   (let ((content-type (downcase (plist-get part :content-type)))
472         (nth (plist-get part :id)))
473     (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
474   ;; Some of the body part handlers leave point somewhere up in the
475   ;; part, so we make sure that we're down at the end.
476   (goto-char (point-max))
477   ;; Ensure that the part ends with a carriage return.
478   (if (not (bolp))
479       (insert "\n")))
480
481 (defun notmuch-show-insert-body (msg body depth)
482   "Insert the body BODY at depth DEPTH in the current thread."
483   (mapc '(lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
484
485 (defun notmuch-show-make-symbol (type)
486   (make-symbol (concat "notmuch-show-" type)))
487
488 (defun notmuch-show-strip-re (string)
489   (replace-regexp-in-string "\\([Rr]e: *\\)+" "" string))
490
491 (defvar notmuch-show-previous-subject "")
492 (make-variable-buffer-local 'notmuch-show-previous-subject)
493
494 (defun notmuch-show-insert-msg (msg depth)
495   "Insert the message MSG at depth DEPTH in the current thread."
496   (let* ((headers (plist-get msg :headers))
497          ;; Indentation causes the buffer offset of the start/end
498          ;; points to move, so we must use markers.
499          message-start message-end
500          content-start content-end
501          headers-start headers-end
502          body-start body-end
503          (headers-invis-spec (notmuch-show-make-symbol "header"))
504          (message-invis-spec (notmuch-show-make-symbol "message"))
505          (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
506
507     ;; Set `buffer-invisibility-spec' to `nil' (a list), otherwise
508     ;; removing items from `buffer-invisibility-spec' (which is what
509     ;; `notmuch-show-headers-visible' and
510     ;; `notmuch-show-message-visible' do) is a no-op and has no
511     ;; effect. This caused threads with only matching messages to have
512     ;; those messages hidden initially because
513     ;; `buffer-invisibility-spec' stayed `t'.
514     ;;
515     ;; This needs to be set here (rather than just above the call to
516     ;; `notmuch-show-headers-visible') because some of the part
517     ;; rendering or body washing functions
518     ;; (e.g. `notmuch-wash-text/plain-citations') manipulate
519     ;; `buffer-invisibility-spec').
520     (when (eq buffer-invisibility-spec t)
521       (setq buffer-invisibility-spec nil))
522
523     (setq message-start (point-marker))
524
525     (notmuch-show-insert-headerline headers
526                                     (or (if notmuch-show-relative-dates
527                                             (plist-get msg :date_relative)
528                                           nil)
529                                         (plist-get headers :Date))
530                                     (plist-get msg :tags) depth)
531
532     (setq content-start (point-marker))
533
534     ;; Set `headers-start' to point after the 'Subject:' header to be
535     ;; compatible with the existing implementation. This just sets it
536     ;; to after the first header.
537     (notmuch-show-insert-headers headers)
538     ;; Headers should include a blank line (backwards compatibility).
539     (insert "\n")
540     (save-excursion
541       (goto-char content-start)
542       ;; If the subject of this message is the same as that of the
543       ;; previous message, don't display it when this message is
544       ;; collapsed.
545       (when (not (string= notmuch-show-previous-subject
546                           bare-subject))
547         (forward-line 1))
548       (setq headers-start (point-marker)))
549     (setq headers-end (point-marker))
550
551     (setq notmuch-show-previous-subject bare-subject)
552
553     (setq body-start (point-marker))
554     (notmuch-show-insert-body msg (plist-get msg :body) depth)
555     ;; Ensure that the body ends with a newline.
556     (if (not (bolp))
557         (insert "\n"))
558     (setq body-end (point-marker))
559     (setq content-end (point-marker))
560
561     ;; Indent according to the depth in the thread.
562     (indent-rigidly content-start content-end depth)
563
564     (setq message-end (point-max-marker))
565
566     ;; Save the extents of this message over the whole text of the
567     ;; message.
568     (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
569
570     (plist-put msg :headers-invis-spec headers-invis-spec)
571     (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
572
573     (plist-put msg :message-invis-spec message-invis-spec)
574     (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
575
576     ;; Save the properties for this message. Currently this saves the
577     ;; entire message (augmented it with other stuff), which seems
578     ;; like overkill. We might save a reduced subset (for example, not
579     ;; the content).
580     (notmuch-show-set-message-properties msg)
581
582     ;; Set header visibility.
583     (notmuch-show-headers-visible msg notmuch-message-headers-visible)
584
585     ;; Message visibility depends on whether it matched the search
586     ;; criteria.
587     (notmuch-show-message-visible msg (plist-get msg :match))))
588
589 (defun notmuch-show-insert-tree (tree depth)
590   "Insert the message tree TREE at depth DEPTH in the current thread."
591   (let ((msg (car tree))
592         (replies (cadr tree)))
593     (notmuch-show-insert-msg msg depth)
594     (notmuch-show-insert-thread replies (1+ depth))))
595
596 (defun notmuch-show-insert-thread (thread depth)
597   "Insert the thread THREAD at depth DEPTH in the current forest."
598   (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
599
600 (defun notmuch-show-insert-forest (forest)
601   "Insert the forest of threads FOREST."
602   (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
603
604 (defvar notmuch-show-parent-buffer nil)
605
606 ;;;###autoload
607 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
608   "Run \"notmuch show\" with the given thread ID and display results.
609
610 The optional PARENT-BUFFER is the notmuch-search buffer from
611 which this notmuch-show command was executed, (so that the
612 next thread from that buffer can be show when done with this
613 one).
614
615 The optional QUERY-CONTEXT is a notmuch search term. Only
616 messages from the thread matching this search term are shown if
617 non-nil.
618
619 The optional BUFFER-NAME provides the neame of the buffer in
620 which the message thread is shown. If it is nil (which occurs
621 when the command is called interactively) the argument to the
622 function is used. "
623   (interactive "sNotmuch show: ")
624   (let ((buffer (get-buffer-create (generate-new-buffer-name
625                                     (or buffer-name
626                                         (concat "*notmuch-" thread-id "*")))))
627         (inhibit-read-only t))
628     (switch-to-buffer buffer)
629     (notmuch-show-mode)
630     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
631     (erase-buffer)
632     (goto-char (point-min))
633     (save-excursion
634       (let* ((basic-args (list thread-id))
635              (args (if query-context
636                        (append (list "\'") basic-args (list "and (" query-context ")\'"))
637                      (append (list "\'") basic-args (list "\'")))))
638         (notmuch-show-insert-forest (notmuch-query-get-threads args))
639         ;; If the query context reduced the results to nothing, run
640         ;; the basic query.
641         (when (and (eq (buffer-size) 0)
642                    query-context)
643           (notmuch-show-insert-forest
644            (notmuch-query-get-threads basic-args))))
645
646       ;; Enable buttonisation of URLs and email addresses in the
647       ;; buffer.
648       (goto-address-mode t)
649       ;; Act on visual lines rather than logical lines.
650       (visual-line-mode t)
651
652       (run-hooks 'notmuch-show-hook))
653
654     ;; Move straight to the first open message
655     (if (not (notmuch-show-message-visible-p))
656         (notmuch-show-next-open-message))
657
658     ;; Set the header line to the subject of the first open message.
659     (setq header-line-format (notmuch-show-strip-re (notmuch-show-get-subject)))
660
661     (notmuch-show-mark-read)))
662
663 (defvar notmuch-show-stash-map
664   (let ((map (make-sparse-keymap)))
665     (define-key map "c" 'notmuch-show-stash-cc)
666     (define-key map "d" 'notmuch-show-stash-date)
667     (define-key map "F" 'notmuch-show-stash-filename)
668     (define-key map "f" 'notmuch-show-stash-from)
669     (define-key map "i" 'notmuch-show-stash-message-id)
670     (define-key map "s" 'notmuch-show-stash-subject)
671     (define-key map "T" 'notmuch-show-stash-tags)
672     (define-key map "t" 'notmuch-show-stash-to)
673     map)
674   "Submap for stash commands")
675 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
676
677 (defvar notmuch-show-mode-map
678       (let ((map (make-sparse-keymap)))
679         (define-key map "?" 'notmuch-help)
680         (define-key map "q" 'notmuch-kill-this-buffer)
681         (define-key map (kbd "<C-tab>") 'widget-backward)
682         (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
683         (define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
684         (define-key map (kbd "TAB") 'notmuch-show-next-button)
685         (define-key map "s" 'notmuch-search)
686         (define-key map "m" 'notmuch-mua-mail)
687         (define-key map "f" 'notmuch-show-forward-message)
688         (define-key map "r" 'notmuch-show-reply)
689         (define-key map "|" 'notmuch-show-pipe-message)
690         (define-key map "w" 'notmuch-show-save-attachments)
691         (define-key map "V" 'notmuch-show-view-raw-message)
692         (define-key map "v" 'notmuch-show-view-all-mime-parts)
693         (define-key map "c" 'notmuch-show-stash-map)
694         (define-key map "h" 'notmuch-show-toggle-headers)
695         (define-key map "-" 'notmuch-show-remove-tag)
696         (define-key map "+" 'notmuch-show-add-tag)
697         (define-key map "x" 'notmuch-show-archive-thread-then-exit)
698         (define-key map "a" 'notmuch-show-archive-thread)
699         (define-key map "N" 'notmuch-show-next-message)
700         (define-key map "P" 'notmuch-show-previous-message)
701         (define-key map "n" 'notmuch-show-next-open-message)
702         (define-key map "p" 'notmuch-show-previous-open-message)
703         (define-key map (kbd "DEL") 'notmuch-show-rewind)
704         (define-key map " " 'notmuch-show-advance-and-archive)
705         (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
706         (define-key map (kbd "RET") 'notmuch-show-toggle-message)
707         map)
708       "Keymap for \"notmuch show\" buffers.")
709 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
710
711 (defun notmuch-show-mode ()
712   "Major mode for viewing a thread with notmuch.
713
714 This buffer contains the results of the \"notmuch show\" command
715 for displaying a single thread of email from your email archives.
716
717 By default, various components of email messages, (citations,
718 signatures, already-read messages), are hidden. You can make
719 these parts visible by clicking with the mouse button or by
720 pressing RET after positioning the cursor on a hidden part, (for
721 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
722
723 Reading the thread sequentially is well-supported by pressing
724 \\[notmuch-show-advance-and-archive]. This will scroll the current message (if necessary), advance
725 to the next message, or advance to the next thread (if already on
726 the last message of a thread).
727
728 Other commands are available to read or manipulate the thread
729 more selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages
730 without removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread
731 without scrolling through with \\[notmuch-show-advance-and-archive]).
732
733 You can add or remove arbitary tags from the current message with
734 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
735
736 All currently available key bindings:
737
738 \\{notmuch-show-mode-map}"
739   (interactive)
740   (kill-all-local-variables)
741   (use-local-map notmuch-show-mode-map)
742   (setq major-mode 'notmuch-show-mode
743         mode-name "notmuch-show")
744   (setq buffer-read-only t))
745
746 (defun notmuch-show-move-to-message-top ()
747   (goto-char (notmuch-show-message-top)))
748
749 (defun notmuch-show-move-to-message-bottom ()
750   (goto-char (notmuch-show-message-bottom)))
751
752 (defun notmuch-show-message-adjust ()
753   (recenter 0))
754
755 ;; Movement related functions.
756
757 ;; There's some strangeness here where a text property applied to a
758 ;; region a->b is not found when point is at b. We walk backwards
759 ;; until finding the property.
760 (defun notmuch-show-message-extent ()
761   (let (r)
762     (save-excursion
763       (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
764         (backward-char)))
765     r))
766
767 (defun notmuch-show-message-top ()
768   (car (notmuch-show-message-extent)))
769
770 (defun notmuch-show-message-bottom ()
771   (cdr (notmuch-show-message-extent)))
772
773 (defun notmuch-show-goto-message-next ()
774   (let ((start (point)))
775     (notmuch-show-move-to-message-bottom)
776     (if (not (eobp))
777         t
778       (goto-char start)
779       nil)))
780
781 (defun notmuch-show-goto-message-previous ()
782   (notmuch-show-move-to-message-top)
783   (if (bobp)
784       nil
785     (backward-char)
786     (notmuch-show-move-to-message-top)
787     t))
788
789 (defun notmuch-show-move-past-invisible-forward ()
790   (while (point-invisible-p)
791     (forward-char)))
792
793 (defun notmuch-show-move-past-invisible-backward ()
794   (while (point-invisible-p)
795     (backward-char)))
796
797 ;; Functions relating to the visibility of messages and their
798 ;; components.
799
800 (defun notmuch-show-element-visible (props visible-p spec-property)
801   (let ((spec (plist-get props spec-property)))
802     (if visible-p
803         (remove-from-invisibility-spec spec)
804       (add-to-invisibility-spec spec))))
805
806 (defun notmuch-show-message-visible (props visible-p)
807   (if visible-p
808       ;; When making the message visible, the headers may or not be
809       ;; visible. So we check that property separately.
810       (let ((headers-visible (plist-get props :headers-visible)))
811         (notmuch-show-element-visible props headers-visible :headers-invis-spec)
812         (notmuch-show-element-visible props t :message-invis-spec))
813     (notmuch-show-element-visible props nil :headers-invis-spec)
814     (notmuch-show-element-visible props nil :message-invis-spec))
815
816   (notmuch-show-set-prop :message-visible visible-p props))
817
818 (defun notmuch-show-headers-visible (props visible-p)
819   (if (plist-get props :message-visible)
820       (notmuch-show-element-visible props visible-p :headers-invis-spec))
821   (notmuch-show-set-prop :headers-visible visible-p props))
822
823 ;; Functions for setting and getting attributes of the current
824 ;; message.
825
826 (defun notmuch-show-set-message-properties (props)
827   (save-excursion
828     (notmuch-show-move-to-message-top)
829     (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
830
831 (defun notmuch-show-get-message-properties ()
832   (save-excursion
833     (notmuch-show-move-to-message-top)
834     (get-text-property (point) :notmuch-message-properties)))
835
836 (defun notmuch-show-set-prop (prop val &optional props)
837   (let ((inhibit-read-only t)
838         (props (or props
839                    (notmuch-show-get-message-properties))))
840     (plist-put props prop val)
841     (notmuch-show-set-message-properties props)))
842
843 (defun notmuch-show-get-prop (prop &optional props)
844   (let ((props (or props
845                    (notmuch-show-get-message-properties))))
846     (plist-get props prop)))
847
848 (defun notmuch-show-get-message-id ()
849   "Return the message id of the current message."
850   (concat "id:\"" (notmuch-show-get-prop :id) "\""))
851
852 ;; dme: Would it make sense to use a macro for many of these?
853
854 (defun notmuch-show-get-filename ()
855   "Return the filename of the current message."
856   (notmuch-show-get-prop :filename))
857
858 (defun notmuch-show-get-header (header)
859   "Return the named header of the current message, if any."
860   (plist-get (notmuch-show-get-prop :headers) header))
861
862 (defun notmuch-show-get-cc ()
863   (notmuch-show-get-header :Cc))
864
865 (defun notmuch-show-get-date ()
866   (notmuch-show-get-header :Date))
867
868 (defun notmuch-show-get-from ()
869   (notmuch-show-get-header :From))
870
871 (defun notmuch-show-get-subject ()
872   (notmuch-show-get-header :Subject))
873
874 (defun notmuch-show-get-to ()
875   (notmuch-show-get-header :To))
876
877 (defun notmuch-show-set-tags (tags)
878   "Set the tags of the current message."
879   (notmuch-show-set-prop :tags tags)
880   (notmuch-show-update-tags tags))
881
882 (defun notmuch-show-get-tags ()
883   "Return the tags of the current message."
884   (notmuch-show-get-prop :tags))
885
886 (defun notmuch-show-message-visible-p ()
887   "Is the current message visible?"
888   (notmuch-show-get-prop :message-visible))
889
890 (defun notmuch-show-headers-visible-p ()
891   "Are the headers of the current message visible?"
892   (notmuch-show-get-prop :headers-visible))
893
894 (defun notmuch-show-mark-read ()
895   "Mark the current message as read."
896   (notmuch-show-remove-tag "unread"))
897
898 ;; Functions for getting attributes of several messages in the current
899 ;; thread.
900
901 (defun notmuch-show-get-message-ids-for-open-messages ()
902   "Return a list of all message IDs for open messages in the current thread."
903   (save-excursion
904     (let (message-ids done)
905       (goto-char (point-min))
906       (while (not done)
907         (if (notmuch-show-message-visible-p)
908             (setq message-ids (append message-ids (list (notmuch-show-get-message-id)))))
909         (setq done (not (notmuch-show-goto-message-next)))
910         )
911       message-ids
912       )))
913
914 ;; Commands typically bound to keys.
915
916 (defun notmuch-show-advance-and-archive ()
917   "Advance through thread and archive.
918
919 This command is intended to be one of the simplest ways to
920 process a thread of email. It does the following:
921
922 If the current message in the thread is not yet fully visible,
923 scroll by a near screenful to read more of the message.
924
925 Otherwise, (the end of the current message is already within the
926 current window), advance to the next open message.
927
928 Finally, if there is no further message to advance to, and this
929 last message is already read, then archive the entire current
930 thread, (remove the \"inbox\" tag from each message). Also kill
931 this buffer, and display the next thread from the search from
932 which this thread was originally shown."
933   (interactive)
934   (let ((end-of-this-message (notmuch-show-message-bottom)))
935     (cond
936      ;; Ideally we would test `end-of-this-message' against the result
937      ;; of `window-end', but that doesn't account for the fact that
938      ;; the end of the message might be hidden, so we have to actually
939      ;; go to the end, walk back over invisible text and then see if
940      ;; point is visible.
941      ((save-excursion
942         (goto-char (- end-of-this-message 1))
943         (notmuch-show-move-past-invisible-backward)
944         (> (point) (window-end)))
945       ;; The bottom of this message is not visible - scroll.
946       (scroll-up nil))
947
948      ((not (= end-of-this-message (point-max)))
949       ;; This is not the last message - move to the next visible one.
950       (notmuch-show-next-open-message))
951
952      (t
953       ;; This is the last message - archive the thread.
954       (notmuch-show-archive-thread)))))
955
956 (defun notmuch-show-rewind ()
957   "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
958
959 Specifically, if the beginning of the previous email is fewer
960 than `window-height' lines from the current point, move to it
961 just like `notmuch-show-previous-message'.
962
963 Otherwise, just scroll down a screenful of the current message.
964
965 This command does not modify any message tags, (it does not undo
966 any effects from previous calls to
967 `notmuch-show-advance-and-archive'."
968   (interactive)
969   (let ((start-of-message (notmuch-show-message-top))
970         (start-of-window (window-start)))
971     (cond
972       ;; Either this message is properly aligned with the start of the
973       ;; window or the start of this message is not visible on the
974       ;; screen - scroll.
975      ((or (= start-of-message start-of-window)
976           (< start-of-message start-of-window))
977       (scroll-down)
978       ;; If a small number of lines from the previous message are
979       ;; visible, realign so that the top of the current message is at
980       ;; the top of the screen.
981       (if (<= (count-screen-lines (window-start) start-of-message)
982               next-screen-context-lines)
983           (progn
984             (goto-char (notmuch-show-message-top))
985             (notmuch-show-message-adjust)))
986       ;; Move to the top left of the window.
987       (goto-char (window-start)))
988      (t
989       ;; Move to the previous message.
990       (notmuch-show-previous-message)))))
991
992 (defun notmuch-show-reply ()
993   "Reply to the current message."
994   (interactive)
995   (notmuch-mua-reply (notmuch-show-get-message-id)))
996
997 (defun notmuch-show-forward-message ()
998   "Forward the current message."
999   (interactive)
1000   (with-current-notmuch-show-message
1001    (notmuch-mua-forward-message)))
1002
1003 (defun notmuch-show-next-message ()
1004   "Show the next message."
1005   (interactive)
1006   (if (notmuch-show-goto-message-next)
1007       (progn
1008         (notmuch-show-mark-read)
1009         (notmuch-show-message-adjust))
1010     (goto-char (point-max))))
1011
1012 (defun notmuch-show-previous-message ()
1013   "Show the previous message."
1014   (interactive)
1015   (notmuch-show-goto-message-previous)
1016   (notmuch-show-mark-read)
1017   (notmuch-show-message-adjust))
1018
1019 (defun notmuch-show-next-open-message ()
1020   "Show the next message."
1021   (interactive)
1022   (let (r)
1023     (while (and (setq r (notmuch-show-goto-message-next))
1024                 (not (notmuch-show-message-visible-p))))
1025     (if r
1026         (progn
1027           (notmuch-show-mark-read)
1028           (notmuch-show-message-adjust))
1029       (goto-char (point-max)))))
1030
1031 (defun notmuch-show-previous-open-message ()
1032   "Show the previous message."
1033   (interactive)
1034   (while (and (notmuch-show-goto-message-previous)
1035               (not (notmuch-show-message-visible-p))))
1036   (notmuch-show-mark-read)
1037   (notmuch-show-message-adjust))
1038
1039 (defun notmuch-show-view-raw-message ()
1040   "View the file holding the current message."
1041   (interactive)
1042   (let* ((id (notmuch-show-get-message-id))
1043          (buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
1044     (call-process notmuch-command nil buf nil "show" "--format=raw" id)
1045     (switch-to-buffer buf)
1046     (goto-char (point-min))
1047     (set-buffer-modified-p nil)
1048     (view-buffer buf 'kill-buffer-if-not-modified)))
1049
1050 (defun notmuch-show-pipe-message (entire-thread command)
1051   "Pipe the contents of the current message (or thread) to the given command.
1052
1053 The given command will be executed with the raw contents of the
1054 current email message as stdin. Anything printed by the command
1055 to stdout or stderr will appear in the *notmuch-pipe* buffer.
1056
1057 When invoked with a prefix argument, the command will receive all
1058 open messages in the current thread (formatted as an mbox) rather
1059 than only the current message."
1060   (interactive "P\nsPipe message to command: ")
1061   (let (shell-command)
1062     (if entire-thread
1063         (setq shell-command 
1064               (concat notmuch-command " show --format=mbox "
1065                       (shell-quote-argument
1066                        (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
1067                       " | " command))
1068       (setq shell-command
1069             (concat notmuch-command " show --format=raw "
1070                     (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
1071     (let ((buf (get-buffer-create (concat "*notmuch-pipe*"))))
1072       (with-current-buffer buf
1073         (setq buffer-read-only nil)
1074         (erase-buffer)
1075         (let ((exit-code (call-process-shell-command shell-command nil buf)))
1076           (goto-char (point-max))
1077           (set-buffer-modified-p nil)
1078           (setq buffer-read-only t)
1079           (unless (zerop exit-code)
1080             (switch-to-buffer-other-window buf)
1081             (message (format "Command '%s' exited abnormally with code %d"
1082                              shell-command exit-code))))))))
1083
1084 (defun notmuch-show-add-tags-worker (current-tags add-tags)
1085   "Add to `current-tags' with any tags from `add-tags' not
1086 currently present and return the result."
1087   (let ((result-tags (copy-sequence current-tags)))
1088     (mapc (lambda (add-tag)
1089             (unless (member add-tag current-tags)
1090               (setq result-tags (push add-tag result-tags))))
1091             add-tags)
1092     (sort result-tags 'string<)))
1093
1094 (defun notmuch-show-del-tags-worker (current-tags del-tags)
1095   "Remove any tags in `del-tags' from `current-tags' and return
1096 the result."
1097   (let ((result-tags (copy-sequence current-tags)))
1098     (mapc (lambda (del-tag)
1099             (setq result-tags (delete del-tag result-tags)))
1100           del-tags)
1101     result-tags))
1102
1103 (defun notmuch-show-add-tag (&rest toadd)
1104   "Add a tag to the current message."
1105   (interactive
1106    (list (notmuch-select-tag-with-completion "Tag to add: ")))
1107
1108   (let* ((current-tags (notmuch-show-get-tags))
1109          (new-tags (notmuch-show-add-tags-worker current-tags toadd)))
1110
1111     (unless (equal current-tags new-tags)
1112       (apply 'notmuch-call-notmuch-process
1113              (append (cons "tag"
1114                            (mapcar (lambda (s) (concat "+" s)) toadd))
1115                      (cons (notmuch-show-get-message-id) nil)))
1116       (notmuch-show-set-tags new-tags))))
1117
1118 (defun notmuch-show-remove-tag (&rest toremove)
1119   "Remove a tag from the current message."
1120   (interactive
1121    (list (notmuch-select-tag-with-completion
1122           "Tag to remove: " (notmuch-show-get-message-id))))
1123
1124   (let* ((current-tags (notmuch-show-get-tags))
1125          (new-tags (notmuch-show-del-tags-worker current-tags toremove)))
1126
1127     (unless (equal current-tags new-tags)
1128       (apply 'notmuch-call-notmuch-process
1129              (append (cons "tag"
1130                            (mapcar (lambda (s) (concat "-" s)) toremove))
1131                      (cons (notmuch-show-get-message-id) nil)))
1132       (notmuch-show-set-tags new-tags))))
1133
1134 (defun notmuch-show-toggle-headers ()
1135   "Toggle the visibility of the current message headers."
1136   (interactive)
1137   (let ((props (notmuch-show-get-message-properties)))
1138     (notmuch-show-headers-visible
1139      props
1140      (not (plist-get props :headers-visible))))
1141   (force-window-update))
1142
1143 (defun notmuch-show-toggle-message ()
1144   "Toggle the visibility of the current message."
1145   (interactive)
1146   (let ((props (notmuch-show-get-message-properties)))
1147     (notmuch-show-message-visible
1148      props
1149      (not (plist-get props :message-visible))))
1150   (force-window-update))
1151
1152 (defun notmuch-show-open-or-close-all ()
1153   "Set the visibility all of the messages in the current thread.
1154 By default make all of the messages visible. With a prefix
1155 argument, hide all of the messages."
1156   (interactive)
1157   (save-excursion
1158     (goto-char (point-min))
1159     (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1160                                            (not current-prefix-arg))
1161           until (not (notmuch-show-goto-message-next))))
1162   (force-window-update))
1163
1164 (defun notmuch-show-next-button ()
1165   "Advance point to the next button in the buffer."
1166   (interactive)
1167   (forward-button 1))
1168
1169 (defun notmuch-show-previous-button ()
1170   "Move point back to the previous button in the buffer."
1171   (interactive)
1172   (backward-button 1))
1173
1174 (defun notmuch-show-archive-thread-internal (show-next)
1175   ;; Remove the tag from the current set of messages.
1176   (goto-char (point-min))
1177   (loop do (notmuch-show-remove-tag "inbox")
1178         until (not (notmuch-show-goto-message-next)))
1179   ;; Move to the next item in the search results, if any.
1180   (let ((parent-buffer notmuch-show-parent-buffer))
1181     (notmuch-kill-this-buffer)
1182     (if parent-buffer
1183         (progn
1184           (switch-to-buffer parent-buffer)
1185           (forward-line)
1186           (if show-next
1187               (notmuch-search-show-thread))))))
1188
1189 (defun notmuch-show-archive-thread ()
1190   "Archive each message in thread, then show next thread from search.
1191
1192 Archive each message currently shown by removing the \"inbox\"
1193 tag from each. Then kill this buffer and show the next thread
1194 from the search from which this thread was originally shown.
1195
1196 Note: This command is safe from any race condition of new messages
1197 being delivered to the same thread. It does not archive the
1198 entire thread, but only the messages shown in the current
1199 buffer."
1200   (interactive)
1201   (notmuch-show-archive-thread-internal t))
1202
1203 (defun notmuch-show-archive-thread-then-exit ()
1204   "Archive each message in thread, then exit back to search results."
1205   (interactive)
1206   (notmuch-show-archive-thread-internal nil))
1207
1208 (defun notmuch-show-stash-cc ()
1209   "Copy CC field of current message to kill-ring."
1210   (interactive)
1211   (notmuch-common-do-stash (notmuch-show-get-cc)))
1212
1213 (defun notmuch-show-stash-date ()
1214   "Copy date of current message to kill-ring."
1215   (interactive)
1216   (notmuch-common-do-stash (notmuch-show-get-date)))
1217
1218 (defun notmuch-show-stash-filename ()
1219   "Copy filename of current message to kill-ring."
1220   (interactive)
1221   (notmuch-common-do-stash (notmuch-show-get-filename)))
1222
1223 (defun notmuch-show-stash-from ()
1224   "Copy From address of current message to kill-ring."
1225   (interactive)
1226   (notmuch-common-do-stash (notmuch-show-get-from)))
1227
1228 (defun notmuch-show-stash-message-id ()
1229   "Copy message ID of current message to kill-ring."
1230   (interactive)
1231   (notmuch-common-do-stash (notmuch-show-get-message-id)))
1232
1233 (defun notmuch-show-stash-subject ()
1234   "Copy Subject field of current message to kill-ring."
1235   (interactive)
1236   (notmuch-common-do-stash (notmuch-show-get-subject)))
1237
1238 (defun notmuch-show-stash-tags ()
1239   "Copy tags of current message to kill-ring as a comma separated list."
1240   (interactive)
1241   (notmuch-common-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1242
1243 (defun notmuch-show-stash-to ()
1244   "Copy To address of current message to kill-ring."
1245   (interactive)
1246   (notmuch-common-do-stash (notmuch-show-get-to)))
1247
1248 ;; Commands typically bound to buttons.
1249
1250 (defun notmuch-show-part-button-action (button)
1251   (let ((nth (button-get button :notmuch-part)))
1252     (if nth
1253         (notmuch-show-save-part (notmuch-show-get-message-id) nth
1254                                 (button-get button :notmuch-filename))
1255       (message "Not a valid part (is it a fake part?)."))))
1256
1257 ;;
1258
1259 (provide 'notmuch-show)