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