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