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