1 ;; notmuch-show.el --- displaying notmuch forests.
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
6 ;; This file is part of Notmuch.
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.
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.
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/>.
21 ;; Authors: Carl Worth <cworth@cworth.org>
22 ;; David Edmondson <dme@dme.org>
24 (eval-when-compile (require 'cl))
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)
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)
45 (defcustom notmuch-message-headers '("Subject" "To" "Cc" "Date")
46 "Headers that should be shown in a message, in this order.
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)
55 (defcustom notmuch-message-headers-visible t
56 "Should the headers be visible by default?
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."
66 (defcustom notmuch-show-relative-dates t
67 "Display relative dates in the message summary line."
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'.")
75 (defcustom notmuch-show-hook '(notmuch-show-turn-on-visual-line-mode)
76 "Functions called after populating a `notmuch-show' buffer."
78 :options '(notmuch-show-turn-on-visual-line-mode)
80 :group 'notmuch-hooks)
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."
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)
94 :group 'notmuch-hooks)
96 ;; Mostly useful for debugging.
97 (defcustom notmuch-show-all-multipart/alternative-parts t
98 "Should all parts of multipart/alternative parts be shown?"
100 :group 'notmuch-show)
102 (defcustom notmuch-show-indent-messages-width 1
103 "Width of message indentation in threads.
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
110 :group 'notmuch-show)
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.
116 :group 'notmuch-show)
118 (defcustom notmuch-show-part-button-default-action 'notmuch-show-save-part
119 "Default part header button action (on ENTER or mouse click)."
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)))
128 (defcustom notmuch-show-only-matching-messages nil
129 "Only matching messages are shown by default."
131 :group 'notmuch-show)
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)
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)
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)
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)
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)
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)
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/")
166 "List of Mailing List Archives to use when stashing links.
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)
174 (defcustom notmuch-show-stash-mlarchive-link-default "Gmane"
175 "Default Mailing List Archive to use when stashing links.
177 This is used when `notmuch-show-stash-mlarchive-link' isn't
178 provided with an MLA argument nor `completing-read' input."
182 (list 'const :tag (car mla) :value (car mla)))
183 notmuch-show-stash-mlarchive-link-alist))
184 :group 'notmuch-show)
186 (defcustom notmuch-show-mark-read-tags '("-unread")
187 "List of tag changes to apply to a message when it is marked as read.
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.
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)
200 (defmacro with-current-notmuch-show-message (&rest body)
201 "Evaluate body with current buffer set to the text of current message"
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)
209 (kill-buffer buf))))))
211 (defun notmuch-show-turn-on-visual-line-mode ()
212 "Enable Visual Line mode."
213 (visual-line-mode t))
215 (defun notmuch-show-view-all-mime-parts ()
216 "Use external viewers to view all attachments from the current message."
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.
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)
234 (mm-display-parts (mm-dissect-buffer)))))
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)))))
245 (defun notmuch-count-attachments (mm-handle)
247 (notmuch-foreach-mime-part
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)))
258 (defun notmuch-save-attachments (mm-handle &optional queryp)
259 (notmuch-foreach-mime-part
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)))
268 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
272 (defun notmuch-show-save-attachments ()
273 "Save all attachments from the current message."
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))))
281 (defun notmuch-show-with-message-as-text (fn)
282 "Apply FN to a text representation of the current message.
284 FN is called with one argument, the message properties. It should
285 operation on the contents of the current buffer."
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))
297 "Subject: " subject "\n"
299 (if (not (string= cc ""))
300 (concat "Cc: " cc "\n")
306 (mapconcat #'identity tags ", ") "\n")
308 (all (buffer-substring (notmuch-show-message-top)
309 (notmuch-show-message-bottom)))
311 (props (notmuch-show-get-message-properties))
312 (indenting notmuch-show-indent-content))
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))
322 (funcall fn props))))
324 (defun notmuch-show-print-message ()
325 "Print the current message."
327 (notmuch-show-with-message-as-text 'notmuch-print-message))
329 (defun notmuch-show-fontify-header ()
331 ((looking-at "[Tt]o:")
333 ((looking-at "[Bb]?[Cc][Cc]:")
335 ((looking-at "[Ss]ubject:")
336 'message-header-subject)
337 ((looking-at "[Ff]rom:")
338 'message-header-from)
340 'message-header-other))))
342 (overlay-put (make-overlay (point) (re-search-forward ":"))
343 'face 'message-header-name)
344 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
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)
354 (defun notmuch-show-spaces-n (n)
355 "Return a string comprised of `n' spaces."
358 (defun notmuch-show-update-tags (tags)
359 "Update the displayed tags of the current message."
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)
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
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
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)))
387 ;; "<user@dom.ain>" style.
388 ((string-match "<\\(.*\\)>" address)
389 (setq p-address (match-string 1 address)))
393 (setq p-address address)))
396 ;; Remove elements of the mailbox part that are not relevant for
397 ;; display, even if they are required during transport:
400 (setq p-name (replace-regexp-in-string "\\\\" "" p-name))
402 ;; Outer single and double quotes, which might be nested.
405 do (setq start-of-loop p-name)
407 when (string-match "^\"\\(.*\\)\"$" p-name)
408 do (setq p-name (match-string 1 p-name))
410 when (string-match "^'\\(.*\\)'$" p-name)
411 do (setq p-name (match-string 1 p-name))
413 until (string= start-of-loop p-name)))
415 ;; If the address is 'foo@bar.com <foo@bar.com>' then show just
417 (when (string= p-name p-address)
420 (cons p-address p-name))
421 (error (cons address nil))))
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.
432 ;; Otherwise format the name and address together.
433 (concat p-name " <" p-address ">"))))
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))
444 (propertize (mapconcat 'identity tags " ")
445 'face 'notmuch-tag-face)
447 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
449 (defun notmuch-show-insert-header (header header-value)
450 "Insert a single header."
451 (insert header ": " header-value "\n"))
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)
465 (narrow-to-region start (point-max))
466 (run-hooks 'notmuch-show-markup-headers-hook)))))
468 (define-button-type 'notmuch-show-part-button-type
469 'action 'notmuch-show-part-button-default
470 'keymap 'notmuch-show-part-button-map
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)
482 "Submap for button commands")
483 (fset 'notmuch-show-part-button-map notmuch-show-part-button-map)
485 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name comment)
487 (base-label (concat (when name (concat name ": "))
489 (unless (string-equal declared-type content-type)
490 (concat " (as " content-type ")"))
495 (concat "[ " base-label " ]")
496 :base-label base-label
497 :type 'notmuch-show-part-button-type
499 :notmuch-filename name
500 :notmuch-content-type content-type))
505 ;; Functions handling particular MIME parts.
507 (defmacro notmuch-with-temp-part-buffer (message-id nth &rest body)
509 (let ((process-crypto (make-symbol "process-crypto")))
510 `(let ((,process-crypto notmuch-show-process-crypto))
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))
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 "~/")
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))))
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)
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))))))
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))))
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))))
557 (defun notmuch-show-multipart/*-to-list (part)
558 (mapcar (lambda (inner-part) (plist-get inner-part :content-type))
559 (plist-get part :content)))
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))
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 (hide (not (or notmuch-show-all-multipart/alternative-parts
572 (string= chosen-type inner-type)))))
573 (notmuch-show-insert-bodypart msg inner-part depth hide)))
576 (when notmuch-show-indent-multipart
577 (indent-rigidly start (point) 1)))
580 (defun notmuch-show-setup-w3m ()
581 "Instruct w3m how to retrieve content from a \"related\" part of a message."
583 (if (boundp 'w3m-cid-retrieve-function-alist)
584 (unless (assq 'notmuch-show-mode w3m-cid-retrieve-function-alist)
585 (push (cons 'notmuch-show-mode 'notmuch-show-w3m-cid-retrieve)
586 w3m-cid-retrieve-function-alist)))
587 (setq mm-inline-text-html-with-images t))
589 (defvar w3m-current-buffer) ;; From `w3m.el'.
590 (defvar notmuch-show-w3m-cid-store nil)
591 (make-variable-buffer-local 'notmuch-show-w3m-cid-store)
593 (defun notmuch-show-w3m-cid-store-internal (content-id
598 (push (list content-id
603 notmuch-show-w3m-cid-store))
605 (defun notmuch-show-w3m-cid-store (msg part)
606 (let ((content-id (plist-get part :content-id)))
608 (notmuch-show-w3m-cid-store-internal (concat "cid:" content-id)
611 (plist-get part :content-type)
614 (defun notmuch-show-w3m-cid-retrieve (url &rest args)
615 (let ((matching-part (with-current-buffer w3m-current-buffer
616 (assoc url notmuch-show-w3m-cid-store))))
618 (let ((message-id (nth 1 matching-part))
619 (part-number (nth 2 matching-part))
620 (content-type (nth 3 matching-part))
621 (content (nth 4 matching-part)))
622 ;; If we don't already have the content, get it and cache
623 ;; it, as some messages reference the same cid: part many
624 ;; times (hundreds!), which results in many calls to
627 (setq content (notmuch-get-bodypart-internal (notmuch-id-to-query message-id)
628 part-number notmuch-show-process-crypto))
629 (with-current-buffer w3m-current-buffer
630 (notmuch-show-w3m-cid-store-internal url
639 (defun notmuch-show-insert-part-multipart/related (msg part content-type nth depth declared-type)
640 (notmuch-show-insert-part-header nth declared-type content-type nil)
641 (let ((inner-parts (plist-get part :content))
644 ;; We assume that the first part is text/html and the remainder
645 ;; things that it references.
647 ;; Stash the non-primary parts.
649 (notmuch-show-w3m-cid-store msg part))
652 ;; Render the primary part.
653 (notmuch-show-insert-bodypart msg (car inner-parts) depth)
655 (when notmuch-show-indent-multipart
656 (indent-rigidly start (point) 1)))
659 (defun notmuch-show-insert-part-multipart/signed (msg part content-type nth depth declared-type)
660 (let ((button (notmuch-show-insert-part-header nth declared-type content-type nil)))
661 (button-put button 'face 'notmuch-crypto-part-header)
662 ;; add signature status button if sigstatus provided
663 (if (plist-member part :sigstatus)
664 (let* ((from (notmuch-show-get-header :From msg))
665 (sigstatus (car (plist-get part :sigstatus))))
666 (notmuch-crypto-insert-sigstatus-button sigstatus from))
667 ;; if we're not adding sigstatus, tell the user how they can get it
668 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts.")))
670 (let ((inner-parts (plist-get part :content))
672 ;; Show all of the parts.
673 (mapc (lambda (inner-part)
674 (notmuch-show-insert-bodypart msg inner-part depth))
677 (when notmuch-show-indent-multipart
678 (indent-rigidly start (point) 1)))
681 (defun notmuch-show-insert-part-multipart/encrypted (msg part content-type nth depth declared-type)
682 (let ((button (notmuch-show-insert-part-header nth declared-type content-type nil)))
683 (button-put button 'face 'notmuch-crypto-part-header)
684 ;; add encryption status button if encstatus specified
685 (if (plist-member part :encstatus)
686 (let ((encstatus (car (plist-get part :encstatus))))
687 (notmuch-crypto-insert-encstatus-button encstatus)
688 ;; add signature status button if sigstatus specified
689 (if (plist-member part :sigstatus)
690 (let* ((from (notmuch-show-get-header :From msg))
691 (sigstatus (car (plist-get part :sigstatus))))
692 (notmuch-crypto-insert-sigstatus-button sigstatus from))))
693 ;; if we're not adding encstatus, tell the user how they can get it
694 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts.")))
696 (let ((inner-parts (plist-get part :content))
698 ;; Show all of the parts.
699 (mapc (lambda (inner-part)
700 (notmuch-show-insert-bodypart msg inner-part depth))
703 (when notmuch-show-indent-multipart
704 (indent-rigidly start (point) 1)))
707 (defun notmuch-show-insert-part-multipart/* (msg part content-type nth depth declared-type)
708 (notmuch-show-insert-part-header nth declared-type content-type nil)
709 (let ((inner-parts (plist-get part :content))
711 ;; Show all of the parts.
712 (mapc (lambda (inner-part)
713 (notmuch-show-insert-bodypart msg inner-part depth))
716 (when notmuch-show-indent-multipart
717 (indent-rigidly start (point) 1)))
720 (defun notmuch-show-insert-part-message/rfc822 (msg part content-type nth depth declared-type)
721 (notmuch-show-insert-part-header nth declared-type content-type nil)
722 (let* ((message (car (plist-get part :content)))
723 (body (car (plist-get message :body)))
726 ;; Override `notmuch-message-headers' to force `From' to be
728 (let ((notmuch-message-headers '("From" "Subject" "To" "Cc" "Date")))
729 (notmuch-show-insert-headers (plist-get message :headers)))
731 ;; Blank line after headers to be compatible with the normal
736 (notmuch-show-insert-bodypart msg body depth)
738 (when notmuch-show-indent-multipart
739 (indent-rigidly start (point) 1)))
742 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
743 (let ((start (point)))
744 ;; If this text/plain part is not the first part in the message,
745 ;; insert a header to make this clear.
747 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
748 (insert (notmuch-get-bodypart-content msg part nth notmuch-show-process-crypto))
751 (narrow-to-region start (point-max))
752 (run-hook-with-args 'notmuch-show-insert-text/plain-hook msg depth))))
755 (defun notmuch-show-insert-part-text/calendar (msg part content-type nth depth declared-type)
756 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename))
757 (insert (with-temp-buffer
758 (insert (notmuch-get-bodypart-content msg part nth notmuch-show-process-crypto))
759 ;; notmuch-get-bodypart-content provides "raw", non-converted
760 ;; data. Replace CRLF with LF before icalendar can use it.
761 (goto-char (point-min))
762 (while (re-search-forward "\r\n" nil t)
763 (replace-match "\n" nil nil))
764 (let ((file (make-temp-file "notmuch-ical"))
768 (unless (icalendar-import-buffer file t)
769 (error "Icalendar import error. See *icalendar-errors* for more information"))
770 (set-buffer (get-file-buffer file))
771 (setq result (buffer-substring (point-min) (point-max)))
772 (set-buffer-modified-p nil)
773 (kill-buffer (current-buffer)))
778 ;; For backwards compatibility.
779 (defun notmuch-show-insert-part-text/x-vcalendar (msg part content-type nth depth declared-type)
780 (notmuch-show-insert-part-text/calendar msg part content-type nth depth declared-type))
782 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
783 ;; If we can deduce a MIME type from the filename of the attachment,
784 ;; do so and pass it on to the handler for that type.
785 (if (plist-get part :filename)
786 (let ((extension (file-name-extension (plist-get part :filename)))
790 (mailcap-parse-mimetypes)
791 (setq mime-type (mailcap-extension-to-mime extension))
793 (not (string-equal mime-type "application/octet-stream")))
794 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
798 ;; Handler for wash generated inline patch fake parts.
799 (defun notmuch-show-insert-part-inline-patch-fake-part (msg part content-type nth depth declared-type)
800 (notmuch-show-insert-part-*/* msg part "text/x-diff" nth depth "inline patch"))
802 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
803 ;; This handler _must_ succeed - it is the handler of last resort.
804 (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
805 (notmuch-mm-display-part-inline msg part nth content-type notmuch-show-process-crypto)
808 ;; Functions for determining how to handle MIME parts.
810 (defun notmuch-show-handlers-for (content-type)
811 "Return a list of content handlers for a part of type CONTENT-TYPE."
816 ;; Reverse order of prefrence.
817 (list (intern (concat "notmuch-show-insert-part-*/*"))
819 "notmuch-show-insert-part-"
820 (car (notmuch-split-content-type content-type))
822 (intern (concat "notmuch-show-insert-part-" content-type))))
827 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
828 (let ((handlers (notmuch-show-handlers-for content-type)))
829 ;; Run the content handlers until one of them returns a non-nil
832 (not (condition-case err
833 (funcall (car handlers) msg part content-type nth depth declared-type)
835 (insert "!!! Bodypart insert error: ")
836 (insert (error-message-string err))
837 (insert " !!!\n") nil)))))
838 (setq handlers (cdr handlers))))
841 (defun notmuch-show-create-part-overlays (msg beg end hide)
842 "Add an overlay to the part between BEG and END"
843 (let* ((button (button-at beg))
844 (part-beg (and button (1+ (button-end button)))))
846 ;; If the part contains no text we do not make it toggleable. We
847 ;; also need to check that the button is a genuine part button not
848 ;; a notmuch-wash button.
849 (when (and button (/= part-beg end) (button-get button :base-label))
850 (button-put button 'overlay (make-overlay part-beg end)))))
852 (defun notmuch-show-insert-bodypart (msg part depth &optional hide)
853 "Insert the body part PART at depth DEPTH in the current thread.
855 If HIDE is non-nil then initially hide this part."
856 (let ((content-type (downcase (plist-get part :content-type)))
857 (nth (plist-get part :id))
860 (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type)
861 ;; Some of the body part handlers leave point somewhere up in the
862 ;; part, so we make sure that we're down at the end.
863 (goto-char (point-max))
864 ;; Ensure that the part ends with a carriage return.
867 (notmuch-show-create-part-overlays msg beg (point) hide)))
869 (defun notmuch-show-insert-body (msg body depth)
870 "Insert the body BODY at depth DEPTH in the current thread."
871 (mapc (lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
873 (defun notmuch-show-make-symbol (type)
874 (make-symbol (concat "notmuch-show-" type)))
876 (defun notmuch-show-strip-re (string)
877 (replace-regexp-in-string "^\\([Rr]e: *\\)+" "" string))
879 (defvar notmuch-show-previous-subject "")
880 (make-variable-buffer-local 'notmuch-show-previous-subject)
882 (defun notmuch-show-insert-msg (msg depth)
883 "Insert the message MSG at depth DEPTH in the current thread."
884 (let* ((headers (plist-get msg :headers))
885 ;; Indentation causes the buffer offset of the start/end
886 ;; points to move, so we must use markers.
887 message-start message-end
888 content-start content-end
889 headers-start headers-end
890 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
892 (setq message-start (point-marker))
894 (notmuch-show-insert-headerline headers
895 (or (if notmuch-show-relative-dates
896 (plist-get msg :date_relative)
898 (plist-get headers :Date))
899 (plist-get msg :tags) depth)
901 (setq content-start (point-marker))
903 ;; Set `headers-start' to point after the 'Subject:' header to be
904 ;; compatible with the existing implementation. This just sets it
905 ;; to after the first header.
906 (notmuch-show-insert-headers headers)
908 (goto-char content-start)
909 ;; If the subject of this message is the same as that of the
910 ;; previous message, don't display it when this message is
912 (when (not (string= notmuch-show-previous-subject
915 (setq headers-start (point-marker)))
916 (setq headers-end (point-marker))
918 (setq notmuch-show-previous-subject bare-subject)
920 ;; A blank line between the headers and the body.
922 (notmuch-show-insert-body msg (plist-get msg :body)
923 (if notmuch-show-indent-content depth 0))
924 ;; Ensure that the body ends with a newline.
927 (setq content-end (point-marker))
929 ;; Indent according to the depth in the thread.
930 (if notmuch-show-indent-content
931 (indent-rigidly content-start content-end (* notmuch-show-indent-messages-width depth)))
933 (setq message-end (point-max-marker))
935 ;; Save the extents of this message over the whole text of the
937 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
939 ;; Create overlays used to control visibility
940 (plist-put msg :headers-overlay (make-overlay headers-start headers-end))
941 (plist-put msg :message-overlay (make-overlay headers-start content-end))
943 (plist-put msg :depth depth)
945 ;; Save the properties for this message. Currently this saves the
946 ;; entire message (augmented it with other stuff), which seems
947 ;; like overkill. We might save a reduced subset (for example, not
949 (notmuch-show-set-message-properties msg)
951 ;; Set header visibility.
952 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
954 ;; Message visibility depends on whether it matched the search
956 (notmuch-show-message-visible msg (and (plist-get msg :match)
957 (not (plist-get msg :excluded))))))
959 (defun notmuch-show-toggle-process-crypto ()
960 "Toggle the processing of cryptographic MIME parts."
962 (setq notmuch-show-process-crypto (not notmuch-show-process-crypto))
963 (message (if notmuch-show-process-crypto
964 "Processing cryptographic MIME parts."
965 "Not processing cryptographic MIME parts."))
966 (notmuch-show-refresh-view))
968 (defun notmuch-show-toggle-elide-non-matching ()
969 "Toggle the display of non-matching messages."
971 (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages))
972 (message (if notmuch-show-elide-non-matching-messages
973 "Showing matching messages only."
974 "Showing all messages."))
975 (notmuch-show-refresh-view))
977 (defun notmuch-show-toggle-thread-indentation ()
978 "Toggle the indentation of threads."
980 (setq notmuch-show-indent-content (not notmuch-show-indent-content))
981 (message (if notmuch-show-indent-content
982 "Content is indented."
983 "Content is not indented."))
984 (notmuch-show-refresh-view))
986 (defun notmuch-show-insert-tree (tree depth)
987 "Insert the message tree TREE at depth DEPTH in the current thread."
988 (let ((msg (car tree))
989 (replies (cadr tree)))
990 ;; We test whether there is a message or just some replies.
992 (notmuch-show-insert-msg msg depth))
993 (notmuch-show-insert-thread replies (1+ depth))))
995 (defun notmuch-show-insert-thread (thread depth)
996 "Insert the thread THREAD at depth DEPTH in the current forest."
997 (mapc (lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
999 (defun notmuch-show-insert-forest (forest)
1000 "Insert the forest of threads FOREST."
1001 (mapc (lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
1003 (defvar notmuch-id-regexp
1005 ;; Match the id: prefix only if it begins a word (to disallow, for
1006 ;; example, matching cid:).
1008 ;; If the term starts with a ", then parse Xapian's quoted boolean
1009 ;; term syntax, which allows for anything as long as embedded
1010 ;; double quotes escaped by doubling them. We also disallow
1011 ;; newlines (which Xapian allows) to prevent runaway terms.
1012 "\"\\([^\"\n]\\|\"\"\\)*\""
1013 ;; Otherwise, parse Xapian's unquoted syntax, which goes up to the
1014 ;; next space or ). We disallow [.,;] as the last character
1015 ;; because these are probably part of the surrounding text, and not
1016 ;; part of the id. This doesn't match single character ids; meh.
1017 "\\|[^\"[:space:])][^[:space:])]*[^])[:space:].,:;?!]"
1019 "The regexp used to match id: links in messages.")
1021 (defvar notmuch-mid-regexp
1022 ;; goto-address-url-regexp matched cid: links, which have the same
1023 ;; grammar as the message ID part of a mid: link. Construct the
1024 ;; regexp using the same technique as goto-address-url-regexp.
1025 (concat "\\<mid:\\(" thing-at-point-url-path-regexp "\\)")
1026 "The regexp used to match mid: links in messages.
1030 (defun notmuch-show-buttonise-links (start end)
1031 "Buttonise URLs and mail addresses between START and END.
1033 This also turns id:\"<message id>\"-parts and mid: links into
1034 buttons for a corresponding notmuch search."
1035 (goto-address-fontify-region start end)
1039 (while (re-search-forward notmuch-id-regexp end t)
1040 (push (list (match-beginning 0) (match-end 0)
1041 (match-string-no-properties 0)) links))
1043 (while (re-search-forward notmuch-mid-regexp end t)
1044 (let* ((mid-cid (match-string-no-properties 1))
1045 (mid (save-match-data
1046 (string-match "^[^/]*" mid-cid)
1047 (url-unhex-string (match-string 0 mid-cid)))))
1048 (push (list (match-beginning 0) (match-end 0)
1049 (notmuch-id-to-query mid)) links)))
1050 (dolist (link links)
1051 ;; Remove the overlay created by goto-address-mode
1052 (remove-overlays (first link) (second link) 'goto-address t)
1053 (make-text-button (first link) (second link)
1054 'action `(lambda (arg)
1055 (notmuch-show ,(third link)))
1057 'help-echo "Mouse-1, RET: search for this message"
1058 'face goto-address-mail-face)))))
1061 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
1062 "Run \"notmuch show\" with the given thread ID and display results.
1064 The optional PARENT-BUFFER is the notmuch-search buffer from
1065 which this notmuch-show command was executed, (so that the
1066 next thread from that buffer can be show when done with this
1069 The optional QUERY-CONTEXT is a notmuch search term. Only
1070 messages from the thread matching this search term are shown if
1073 The optional BUFFER-NAME provides the name of the buffer in
1074 which the message thread is shown. If it is nil (which occurs
1075 when the command is called interactively) the argument to the
1077 (interactive "sNotmuch show: ")
1078 (let ((buffer-name (generate-new-buffer-name
1080 (concat "*notmuch-" thread-id "*")))))
1081 (switch-to-buffer (get-buffer-create buffer-name))
1082 ;; Set the default value for `notmuch-show-process-crypto' in this
1084 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1085 ;; Set the default value for
1086 ;; `notmuch-show-elide-non-matching-messages' in this buffer. If
1087 ;; there is a prefix argument, invert the default.
1088 (setq notmuch-show-elide-non-matching-messages notmuch-show-only-matching-messages)
1089 (if current-prefix-arg
1090 (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages)))
1092 (setq notmuch-show-thread-id thread-id
1093 notmuch-show-parent-buffer parent-buffer
1094 notmuch-show-query-context query-context)
1095 (notmuch-show-build-buffer)
1096 (notmuch-show-goto-first-wanted-message)
1099 (defun notmuch-show-build-buffer ()
1100 (let ((inhibit-read-only t))
1103 ;; Don't track undo information for this buffer
1104 (set 'buffer-undo-list t)
1107 (goto-char (point-min))
1109 (let* ((basic-args (list notmuch-show-thread-id))
1110 (args (if notmuch-show-query-context
1111 (append (list "\'") basic-args
1112 (list "and (" notmuch-show-query-context ")\'"))
1113 (append (list "\'") basic-args (list "\'"))))
1114 (cli-args (cons "--exclude=false"
1115 (when notmuch-show-elide-non-matching-messages
1116 (list "--entire-thread=false")))))
1118 (notmuch-show-insert-forest (notmuch-query-get-threads (append cli-args args)))
1119 ;; If the query context reduced the results to nothing, run
1121 (when (and (eq (buffer-size) 0)
1122 notmuch-show-query-context)
1123 (notmuch-show-insert-forest
1124 (notmuch-query-get-threads (append cli-args basic-args)))))
1126 (jit-lock-register #'notmuch-show-buttonise-links)
1128 ;; Set the header line to the subject of the first message.
1129 (setq header-line-format (notmuch-show-strip-re (notmuch-show-get-subject)))
1131 (run-hooks 'notmuch-show-hook))))
1133 (defun notmuch-show-capture-state ()
1134 "Capture the state of the current buffer.
1137 - the list of open messages,
1138 - the current message."
1139 (list (notmuch-show-get-message-id) (notmuch-show-get-message-ids-for-open-messages)))
1141 (defun notmuch-show-apply-state (state)
1142 "Apply STATE to the current buffer.
1145 - opening the messages previously opened,
1146 - closing all other messages,
1147 - moving to the correct current message."
1148 (let ((current (car state))
1149 (open (cadr state)))
1151 ;; Open those that were open.
1152 (goto-char (point-min))
1153 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1154 (member (notmuch-show-get-message-id) open))
1155 until (not (notmuch-show-goto-message-next)))
1157 ;; Go to the previously open message.
1158 (goto-char (point-min))
1159 (unless (loop if (string= current (notmuch-show-get-message-id))
1161 until (not (notmuch-show-goto-message-next)))
1162 (goto-char (point-min))
1163 (message "Previously current message not found."))
1164 (notmuch-show-message-adjust)))
1166 (defun notmuch-show-refresh-view (&optional reset-state)
1167 "Refresh the current view.
1169 Refreshes the current view, observing changes in display
1170 preferences. If invoked with a prefix argument (or RESET-STATE is
1171 non-nil) then the state of the buffer (open/closed messages) is
1172 reset based on the original query."
1174 (let ((inhibit-read-only t)
1175 (state (unless reset-state
1176 (notmuch-show-capture-state))))
1177 ;; erase-buffer does not seem to remove overlays, which can lead
1178 ;; to weird effects such as remaining images, so remove them
1182 (notmuch-show-build-buffer)
1184 (notmuch-show-apply-state state)
1185 ;; We're resetting state, so navigate to the first open message
1186 ;; and mark it read, just like opening a new show buffer.
1187 (notmuch-show-goto-first-wanted-message))))
1189 (defvar notmuch-show-stash-map
1190 (let ((map (make-sparse-keymap)))
1191 (define-key map "c" 'notmuch-show-stash-cc)
1192 (define-key map "d" 'notmuch-show-stash-date)
1193 (define-key map "F" 'notmuch-show-stash-filename)
1194 (define-key map "f" 'notmuch-show-stash-from)
1195 (define-key map "i" 'notmuch-show-stash-message-id)
1196 (define-key map "I" 'notmuch-show-stash-message-id-stripped)
1197 (define-key map "s" 'notmuch-show-stash-subject)
1198 (define-key map "T" 'notmuch-show-stash-tags)
1199 (define-key map "t" 'notmuch-show-stash-to)
1200 (define-key map "l" 'notmuch-show-stash-mlarchive-link)
1201 (define-key map "L" 'notmuch-show-stash-mlarchive-link-and-go)
1203 "Submap for stash commands")
1204 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
1206 (defvar notmuch-show-mode-map
1207 (let ((map (make-sparse-keymap)))
1208 (define-key map "?" 'notmuch-help)
1209 (define-key map "q" 'notmuch-kill-this-buffer)
1210 (define-key map (kbd "<C-tab>") 'widget-backward)
1211 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
1212 (define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
1213 (define-key map (kbd "TAB") 'notmuch-show-next-button)
1214 (define-key map "s" 'notmuch-search)
1215 (define-key map "m" 'notmuch-mua-new-mail)
1216 (define-key map "f" 'notmuch-show-forward-message)
1217 (define-key map "r" 'notmuch-show-reply-sender)
1218 (define-key map "R" 'notmuch-show-reply)
1219 (define-key map "|" 'notmuch-show-pipe-message)
1220 (define-key map "w" 'notmuch-show-save-attachments)
1221 (define-key map "V" 'notmuch-show-view-raw-message)
1222 (define-key map "v" 'notmuch-show-view-all-mime-parts)
1223 (define-key map "c" 'notmuch-show-stash-map)
1224 (define-key map "=" 'notmuch-show-refresh-view)
1225 (define-key map "h" 'notmuch-show-toggle-visibility-headers)
1226 (define-key map "*" 'notmuch-show-tag-all)
1227 (define-key map "-" 'notmuch-show-remove-tag)
1228 (define-key map "+" 'notmuch-show-add-tag)
1229 (define-key map "X" 'notmuch-show-archive-thread-then-exit)
1230 (define-key map "x" 'notmuch-show-archive-message-then-next-or-exit)
1231 (define-key map "A" 'notmuch-show-archive-thread-then-next)
1232 (define-key map "a" 'notmuch-show-archive-message-then-next-or-next-thread)
1233 (define-key map "N" 'notmuch-show-next-message)
1234 (define-key map "P" 'notmuch-show-previous-message)
1235 (define-key map "n" 'notmuch-show-next-open-message)
1236 (define-key map "p" 'notmuch-show-previous-open-message)
1237 (define-key map (kbd "DEL") 'notmuch-show-rewind)
1238 (define-key map " " 'notmuch-show-advance-and-archive)
1239 (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
1240 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
1241 (define-key map "#" 'notmuch-show-print-message)
1242 (define-key map "!" 'notmuch-show-toggle-elide-non-matching)
1243 (define-key map "$" 'notmuch-show-toggle-process-crypto)
1244 (define-key map "<" 'notmuch-show-toggle-thread-indentation)
1245 (define-key map "t" 'toggle-truncate-lines)
1247 "Keymap for \"notmuch show\" buffers.")
1248 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
1250 (defun notmuch-show-mode ()
1251 "Major mode for viewing a thread with notmuch.
1253 This buffer contains the results of the \"notmuch show\" command
1254 for displaying a single thread of email from your email archives.
1256 By default, various components of email messages, (citations,
1257 signatures, already-read messages), are hidden. You can make
1258 these parts visible by clicking with the mouse button or by
1259 pressing RET after positioning the cursor on a hidden part, (for
1260 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
1262 Reading the thread sequentially is well-supported by pressing
1263 \\[notmuch-show-advance-and-archive]. This will scroll the current message (if necessary), advance
1264 to the next message, or advance to the next thread (if already on
1265 the last message of a thread).
1267 Other commands are available to read or manipulate the thread
1268 more selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages
1269 without removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread
1270 without scrolling through with \\[notmuch-show-advance-and-archive]).
1272 You can add or remove arbitrary tags from the current message with
1273 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
1275 All currently available key bindings:
1277 \\{notmuch-show-mode-map}"
1279 (kill-all-local-variables)
1280 (use-local-map notmuch-show-mode-map)
1281 (setq major-mode 'notmuch-show-mode
1282 mode-name "notmuch-show")
1283 (setq buffer-read-only t
1286 (defun notmuch-show-move-to-message-top ()
1287 (goto-char (notmuch-show-message-top)))
1289 (defun notmuch-show-move-to-message-bottom ()
1290 (goto-char (notmuch-show-message-bottom)))
1292 (defun notmuch-show-message-adjust ()
1295 ;; Movement related functions.
1297 ;; There's some strangeness here where a text property applied to a
1298 ;; region a->b is not found when point is at b. We walk backwards
1299 ;; until finding the property.
1300 (defun notmuch-show-message-extent ()
1303 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
1307 (defun notmuch-show-message-top ()
1308 (car (notmuch-show-message-extent)))
1310 (defun notmuch-show-message-bottom ()
1311 (cdr (notmuch-show-message-extent)))
1313 (defun notmuch-show-goto-message-next ()
1314 (let ((start (point)))
1315 (notmuch-show-move-to-message-bottom)
1321 (defun notmuch-show-goto-message-previous ()
1322 (notmuch-show-move-to-message-top)
1326 (notmuch-show-move-to-message-top)
1329 (defun notmuch-show-mapc (function)
1330 "Iterate through all messages in the current thread with
1331 `notmuch-show-goto-message-next' and call FUNCTION for side
1334 (goto-char (point-min))
1335 (loop do (funcall function)
1336 while (notmuch-show-goto-message-next))))
1338 ;; Functions relating to the visibility of messages and their
1341 (defun notmuch-show-message-visible (props visible-p)
1342 (overlay-put (plist-get props :message-overlay) 'invisible (not visible-p))
1343 (notmuch-show-set-prop :message-visible visible-p props))
1345 (defun notmuch-show-headers-visible (props visible-p)
1346 (overlay-put (plist-get props :headers-overlay) 'invisible (not visible-p))
1347 (notmuch-show-set-prop :headers-visible visible-p props))
1349 ;; Functions for setting and getting attributes of the current
1352 (defun notmuch-show-set-message-properties (props)
1354 (notmuch-show-move-to-message-top)
1355 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
1357 (defun notmuch-show-get-message-properties ()
1358 "Return the properties of the current message as a plist.
1360 Some useful entries are:
1361 :headers - Property list containing the headers :Date, :Subject, :From, etc.
1362 :body - Body of the message
1363 :tags - Tags for this message"
1365 (notmuch-show-move-to-message-top)
1366 (get-text-property (point) :notmuch-message-properties)))
1368 (defun notmuch-show-set-prop (prop val &optional props)
1369 (let ((inhibit-read-only t)
1371 (notmuch-show-get-message-properties))))
1372 (plist-put props prop val)
1373 (notmuch-show-set-message-properties props)))
1375 (defun notmuch-show-get-prop (prop &optional props)
1376 (let ((props (or props
1377 (notmuch-show-get-message-properties))))
1378 (plist-get props prop)))
1380 (defun notmuch-show-get-message-id (&optional bare)
1381 "Return an id: query for the Message-Id of the current message.
1383 If optional argument BARE is non-nil, return
1384 the Message-Id without id: prefix and escaping."
1386 (notmuch-show-get-prop :id)
1387 (notmuch-id-to-query (notmuch-show-get-prop :id))))
1389 (defun notmuch-show-get-messages-ids ()
1390 "Return all id: queries of messages in the current thread."
1391 (let ((message-ids))
1393 (lambda () (push (notmuch-show-get-message-id) message-ids)))
1396 (defun notmuch-show-get-messages-ids-search ()
1397 "Return a search string for all message ids of messages in the
1399 (mapconcat 'identity (notmuch-show-get-messages-ids) " or "))
1401 ;; dme: Would it make sense to use a macro for many of these?
1403 (defun notmuch-show-get-filename ()
1404 "Return the filename of the current message."
1405 (notmuch-show-get-prop :filename))
1407 (defun notmuch-show-get-header (header &optional props)
1408 "Return the named header of the current message, if any."
1409 (plist-get (notmuch-show-get-prop :headers props) header))
1411 (defun notmuch-show-get-cc ()
1412 (notmuch-show-get-header :Cc))
1414 (defun notmuch-show-get-date ()
1415 (notmuch-show-get-header :Date))
1417 (defun notmuch-show-get-from ()
1418 (notmuch-show-get-header :From))
1420 (defun notmuch-show-get-subject ()
1421 (notmuch-show-get-header :Subject))
1423 (defun notmuch-show-get-to ()
1424 (notmuch-show-get-header :To))
1426 (defun notmuch-show-get-depth ()
1427 (notmuch-show-get-prop :depth))
1429 (defun notmuch-show-set-tags (tags)
1430 "Set the tags of the current message."
1431 (notmuch-show-set-prop :tags tags)
1432 (notmuch-show-update-tags tags))
1434 (defun notmuch-show-get-tags ()
1435 "Return the tags of the current message."
1436 (notmuch-show-get-prop :tags))
1438 (defun notmuch-show-message-visible-p ()
1439 "Is the current message visible?"
1440 (notmuch-show-get-prop :message-visible))
1442 (defun notmuch-show-headers-visible-p ()
1443 "Are the headers of the current message visible?"
1444 (notmuch-show-get-prop :headers-visible))
1446 (defun notmuch-show-mark-read (&optional unread)
1447 "Mark the current message as read.
1449 Mark the current message as read by applying the tag changes in
1450 `notmuch-show-mark-read-tags' to it (remove the \"unread\" tag by
1451 default). If a prefix argument is given, the message will be
1452 marked as unread, i.e. the tag changes in
1453 `notmuch-show-mark-read-tags' will be reversed."
1455 (when notmuch-show-mark-read-tags
1456 (apply 'notmuch-show-tag-message
1457 (notmuch-tag-change-list notmuch-show-mark-read-tags unread))))
1459 ;; Functions for getting attributes of several messages in the current
1462 (defun notmuch-show-get-message-ids-for-open-messages ()
1463 "Return a list of all id: queries for open messages in the current thread."
1465 (let (message-ids done)
1466 (goto-char (point-min))
1468 (if (notmuch-show-message-visible-p)
1469 (setq message-ids (append message-ids (list (notmuch-show-get-message-id)))))
1470 (setq done (not (notmuch-show-goto-message-next)))
1475 ;; Commands typically bound to keys.
1477 (defun notmuch-show-advance ()
1478 "Advance through thread.
1480 If the current message in the thread is not yet fully visible,
1481 scroll by a near screenful to read more of the message.
1483 Otherwise, (the end of the current message is already within the
1484 current window), advance to the next open message."
1486 (let* ((end-of-this-message (notmuch-show-message-bottom))
1487 (visible-end-of-this-message (1- end-of-this-message))
1489 (while (invisible-p visible-end-of-this-message)
1490 (setq visible-end-of-this-message
1492 (1- (previous-single-char-property-change
1493 visible-end-of-this-message 'invisible)))))
1495 ;; Ideally we would test `end-of-this-message' against the result
1496 ;; of `window-end', but that doesn't account for the fact that
1497 ;; the end of the message might be hidden.
1498 ((and visible-end-of-this-message
1499 (> visible-end-of-this-message (window-end)))
1500 ;; The bottom of this message is not visible - scroll.
1503 ((not (= end-of-this-message (point-max)))
1504 ;; This is not the last message - move to the next visible one.
1505 (notmuch-show-next-open-message))
1507 ((not (= (point) (point-max)))
1508 ;; This is the last message, but the cursor is not at the end of
1509 ;; the buffer. Move it there.
1510 (goto-char (point-max)))
1513 ;; This is the last message - change the return value
1517 (defun notmuch-show-advance-and-archive ()
1518 "Advance through thread and archive.
1520 This command is intended to be one of the simplest ways to
1521 process a thread of email. It works exactly like
1522 notmuch-show-advance, in that it scrolls through messages in a
1523 show buffer, except that when it gets to the end of the buffer it
1524 archives the entire current thread, (remove the \"inbox\" tag
1525 from each message), kills the buffer, and displays the next
1526 thread from the search from which this thread was originally
1529 (if (notmuch-show-advance)
1530 (notmuch-show-archive-thread-then-next)))
1532 (defun notmuch-show-rewind ()
1533 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
1535 Specifically, if the beginning of the previous email is fewer
1536 than `window-height' lines from the current point, move to it
1537 just like `notmuch-show-previous-message'.
1539 Otherwise, just scroll down a screenful of the current message.
1541 This command does not modify any message tags, (it does not undo
1542 any effects from previous calls to
1543 `notmuch-show-advance-and-archive'."
1545 (let ((start-of-message (notmuch-show-message-top))
1546 (start-of-window (window-start)))
1548 ;; Either this message is properly aligned with the start of the
1549 ;; window or the start of this message is not visible on the
1551 ((or (= start-of-message start-of-window)
1552 (< start-of-message start-of-window))
1554 ;; If a small number of lines from the previous message are
1555 ;; visible, realign so that the top of the current message is at
1556 ;; the top of the screen.
1557 (when (<= (count-screen-lines (window-start) start-of-message)
1558 next-screen-context-lines)
1559 (goto-char (notmuch-show-message-top))
1560 (notmuch-show-message-adjust))
1561 ;; Move to the top left of the window.
1562 (goto-char (window-start)))
1564 ;; Move to the previous message.
1565 (notmuch-show-previous-message)))))
1567 (defun notmuch-show-reply (&optional prompt-for-sender)
1568 "Reply to the sender and all recipients of the current message."
1570 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender t))
1572 (defun notmuch-show-reply-sender (&optional prompt-for-sender)
1573 "Reply to the sender of the current message."
1575 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender nil))
1577 (defun notmuch-show-forward-message (&optional prompt-for-sender)
1578 "Forward the current message."
1580 (with-current-notmuch-show-message
1581 (notmuch-mua-new-forward-message prompt-for-sender)))
1583 (defun notmuch-show-next-message (&optional pop-at-end)
1584 "Show the next message.
1586 If a prefix argument is given and this is the last message in the
1587 thread, navigate to the next thread in the parent search buffer."
1589 (if (notmuch-show-goto-message-next)
1591 (notmuch-show-mark-read)
1592 (notmuch-show-message-adjust))
1594 (notmuch-show-next-thread)
1595 (goto-char (point-max)))))
1597 (defun notmuch-show-previous-message ()
1598 "Show the previous message or the start of the current message."
1600 (if (= (point) (notmuch-show-message-top))
1601 (notmuch-show-goto-message-previous)
1602 (notmuch-show-move-to-message-top))
1603 (notmuch-show-mark-read)
1604 (notmuch-show-message-adjust))
1606 (defun notmuch-show-next-open-message (&optional pop-at-end)
1607 "Show the next open message.
1609 If a prefix argument is given and this is the last open message
1610 in the thread, navigate to the next thread in the parent search
1611 buffer. Return t if there was a next open message in the thread
1612 to show, nil otherwise."
1615 (while (and (setq r (notmuch-show-goto-message-next))
1616 (not (notmuch-show-message-visible-p))))
1619 (notmuch-show-mark-read)
1620 (notmuch-show-message-adjust))
1622 (notmuch-show-next-thread)
1623 (goto-char (point-max))))
1626 (defun notmuch-show-next-matching-message ()
1627 "Show the next matching message."
1630 (while (and (setq r (notmuch-show-goto-message-next))
1631 (not (notmuch-show-get-prop :match))))
1634 (notmuch-show-mark-read)
1635 (notmuch-show-message-adjust))
1636 (goto-char (point-max)))))
1638 (defun notmuch-show-open-if-matched ()
1639 "Open a message if it is matched (whether or not excluded)."
1640 (let ((props (notmuch-show-get-message-properties)))
1641 (notmuch-show-message-visible props (plist-get props :match))))
1643 (defun notmuch-show-goto-first-wanted-message ()
1644 "Move to the first open message and mark it read"
1645 (goto-char (point-min))
1646 (if (notmuch-show-message-visible-p)
1647 (notmuch-show-mark-read)
1648 (notmuch-show-next-open-message))
1650 ;; There are no matched non-excluded messages so open all matched
1651 ;; (necessarily excluded) messages and go to the first.
1652 (notmuch-show-mapc 'notmuch-show-open-if-matched)
1653 (force-window-update)
1654 (goto-char (point-min))
1655 (if (notmuch-show-message-visible-p)
1656 (notmuch-show-mark-read)
1657 (notmuch-show-next-open-message))))
1659 (defun notmuch-show-previous-open-message ()
1660 "Show the previous open message."
1662 (while (and (if (= (point) (notmuch-show-message-top))
1663 (notmuch-show-goto-message-previous)
1664 (notmuch-show-move-to-message-top))
1665 (not (notmuch-show-message-visible-p))))
1666 (notmuch-show-mark-read)
1667 (notmuch-show-message-adjust))
1669 (defun notmuch-show-view-raw-message ()
1670 "View the file holding the current message."
1672 (let* ((id (notmuch-show-get-message-id))
1673 (buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
1674 (call-process notmuch-command nil buf nil "show" "--format=raw" id)
1675 (switch-to-buffer buf)
1676 (goto-char (point-min))
1677 (set-buffer-modified-p nil)
1678 (view-buffer buf 'kill-buffer-if-not-modified)))
1680 (defun notmuch-show-pipe-message (entire-thread command)
1681 "Pipe the contents of the current message (or thread) to the given command.
1683 The given command will be executed with the raw contents of the
1684 current email message as stdin. Anything printed by the command
1685 to stdout or stderr will appear in the *notmuch-pipe* buffer.
1687 When invoked with a prefix argument, the command will receive all
1688 open messages in the current thread (formatted as an mbox) rather
1689 than only the current message."
1690 (interactive "P\nsPipe message to command: ")
1691 (let (shell-command)
1694 (concat notmuch-command " show --format=mbox --exclude=false "
1695 (shell-quote-argument
1696 (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
1699 (concat notmuch-command " show --format=raw "
1700 (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
1701 (let ((buf (get-buffer-create (concat "*notmuch-pipe*"))))
1702 (with-current-buffer buf
1703 (setq buffer-read-only nil)
1705 (let ((exit-code (call-process-shell-command shell-command nil buf)))
1706 (goto-char (point-max))
1707 (set-buffer-modified-p nil)
1708 (setq buffer-read-only t)
1709 (unless (zerop exit-code)
1710 (switch-to-buffer-other-window buf)
1711 (message (format "Command '%s' exited abnormally with code %d"
1712 shell-command exit-code))))))))
1714 (defun notmuch-show-tag-message (&rest tag-changes)
1715 "Change tags for the current message.
1717 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1718 (let* ((current-tags (notmuch-show-get-tags))
1719 (new-tags (notmuch-update-tags current-tags tag-changes)))
1720 (unless (equal current-tags new-tags)
1721 (funcall 'notmuch-tag (notmuch-show-get-message-id) tag-changes)
1722 (notmuch-show-set-tags new-tags))))
1724 (defun notmuch-show-tag (&optional tag-changes)
1725 "Change tags for the current message.
1727 See `notmuch-tag' for information on the format of TAG-CHANGES."
1729 (setq tag-changes (funcall 'notmuch-tag (notmuch-show-get-message-id) tag-changes))
1730 (let* ((current-tags (notmuch-show-get-tags))
1731 (new-tags (notmuch-update-tags current-tags tag-changes)))
1732 (unless (equal current-tags new-tags)
1733 (notmuch-show-set-tags new-tags))))
1735 (defun notmuch-show-tag-all (&optional tag-changes)
1736 "Change tags for all messages in the current show buffer.
1738 See `notmuch-tag' for information on the format of TAG-CHANGES."
1740 (setq tag-changes (funcall 'notmuch-tag (notmuch-show-get-messages-ids-search) tag-changes))
1743 (let* ((current-tags (notmuch-show-get-tags))
1744 (new-tags (notmuch-update-tags current-tags tag-changes)))
1745 (unless (equal current-tags new-tags)
1746 (notmuch-show-set-tags new-tags))))))
1748 (defun notmuch-show-add-tag ()
1749 "Same as `notmuch-show-tag' but sets initial input to '+'."
1751 (notmuch-show-tag "+"))
1753 (defun notmuch-show-remove-tag ()
1754 "Same as `notmuch-show-tag' but sets initial input to '-'."
1756 (notmuch-show-tag "-"))
1758 (defun notmuch-show-toggle-visibility-headers ()
1759 "Toggle the visibility of the current message headers."
1761 (let ((props (notmuch-show-get-message-properties)))
1762 (notmuch-show-headers-visible
1764 (not (plist-get props :headers-visible))))
1765 (force-window-update))
1767 (defun notmuch-show-toggle-message ()
1768 "Toggle the visibility of the current message."
1770 (let ((props (notmuch-show-get-message-properties)))
1771 (notmuch-show-message-visible
1773 (not (plist-get props :message-visible))))
1774 (force-window-update))
1776 (defun notmuch-show-open-or-close-all ()
1777 "Set the visibility all of the messages in the current thread.
1778 By default make all of the messages visible. With a prefix
1779 argument, hide all of the messages."
1782 (goto-char (point-min))
1783 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1784 (not current-prefix-arg))
1785 until (not (notmuch-show-goto-message-next))))
1786 (force-window-update))
1788 (defun notmuch-show-next-button ()
1789 "Advance point to the next button in the buffer."
1793 (defun notmuch-show-previous-button ()
1794 "Move point back to the previous button in the buffer."
1796 (backward-button 1))
1798 (defun notmuch-show-next-thread (&optional show-next)
1799 "Move to the next item in the search results, if any."
1801 (let ((parent-buffer notmuch-show-parent-buffer))
1802 (notmuch-kill-this-buffer)
1803 (when (buffer-live-p parent-buffer)
1804 (switch-to-buffer parent-buffer)
1805 (notmuch-search-next-thread)
1807 (notmuch-search-show-thread)))))
1809 (defun notmuch-show-archive-thread (&optional unarchive)
1810 "Archive each message in thread.
1812 Archive each message currently shown by applying the tag changes
1813 in `notmuch-archive-tags' to each (remove the \"inbox\" tag by
1814 default). If a prefix argument is given, the messages will be
1815 \"unarchived\", i.e. the tag changes in `notmuch-archive-tags'
1818 Note: This command is safe from any race condition of new messages
1819 being delivered to the same thread. It does not archive the
1820 entire thread, but only the messages shown in the current
1823 (when notmuch-archive-tags
1824 (notmuch-show-tag-all
1825 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
1827 (defun notmuch-show-archive-thread-then-next ()
1828 "Archive all messages in the current buffer, then show next thread from search."
1830 (notmuch-show-archive-thread)
1831 (notmuch-show-next-thread t))
1833 (defun notmuch-show-archive-thread-then-exit ()
1834 "Archive all messages in the current buffer, then exit back to search results."
1836 (notmuch-show-archive-thread)
1837 (notmuch-show-next-thread))
1839 (defun notmuch-show-archive-message (&optional unarchive)
1840 "Archive the current message.
1842 Archive the current message by applying the tag changes in
1843 `notmuch-archive-tags' to it (remove the \"inbox\" tag by
1844 default). If a prefix argument is given, the message will be
1845 \"unarchived\", i.e. the tag changes in `notmuch-archive-tags'
1848 (when notmuch-archive-tags
1849 (apply 'notmuch-show-tag-message
1850 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
1852 (defun notmuch-show-archive-message-then-next-or-exit ()
1853 "Archive the current message, then show the next open message in the current thread.
1855 If at the last open message in the current thread, then exit back
1858 (notmuch-show-archive-message)
1859 (notmuch-show-next-open-message t))
1861 (defun notmuch-show-archive-message-then-next-or-next-thread ()
1862 "Archive the current message, then show the next open message in the current thread.
1864 If at the last open message in the current thread, then show next
1865 thread from search."
1867 (notmuch-show-archive-message)
1868 (unless (notmuch-show-next-open-message)
1869 (notmuch-show-next-thread t)))
1871 (defun notmuch-show-stash-cc ()
1872 "Copy CC field of current message to kill-ring."
1874 (notmuch-common-do-stash (notmuch-show-get-cc)))
1876 (defun notmuch-show-stash-date ()
1877 "Copy date of current message to kill-ring."
1879 (notmuch-common-do-stash (notmuch-show-get-date)))
1881 (defun notmuch-show-stash-filename ()
1882 "Copy filename of current message to kill-ring."
1884 (notmuch-common-do-stash (notmuch-show-get-filename)))
1886 (defun notmuch-show-stash-from ()
1887 "Copy From address of current message to kill-ring."
1889 (notmuch-common-do-stash (notmuch-show-get-from)))
1891 (defun notmuch-show-stash-message-id (&optional stash-thread-id)
1892 "Copy id: query matching the current message to kill-ring.
1894 If invoked with a prefix argument (or STASH-THREAD-ID is
1895 non-nil), copy thread: query matching the current thread to
1899 (notmuch-common-do-stash notmuch-show-thread-id)
1900 (notmuch-common-do-stash (notmuch-show-get-message-id))))
1902 (defun notmuch-show-stash-message-id-stripped ()
1903 "Copy message ID of current message (sans `id:' prefix) to kill-ring."
1905 (notmuch-common-do-stash (notmuch-show-get-message-id t)))
1907 (defun notmuch-show-stash-subject ()
1908 "Copy Subject field of current message to kill-ring."
1910 (notmuch-common-do-stash (notmuch-show-get-subject)))
1912 (defun notmuch-show-stash-tags ()
1913 "Copy tags of current message to kill-ring as a comma separated list."
1915 (notmuch-common-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1917 (defun notmuch-show-stash-to ()
1918 "Copy To address of current message to kill-ring."
1920 (notmuch-common-do-stash (notmuch-show-get-to)))
1922 (defun notmuch-show-stash-mlarchive-link (&optional mla)
1923 "Copy an ML Archive URI for the current message to the kill-ring.
1925 This presumes that the message is available at the selected Mailing List Archive.
1927 If optional argument MLA is non-nil, use the provided key instead of prompting
1928 the user (see `notmuch-show-stash-mlarchive-link-alist')."
1930 (notmuch-common-do-stash
1933 (let ((completion-ignore-case t))
1935 "Mailing List Archive: "
1936 notmuch-show-stash-mlarchive-link-alist
1937 nil t nil nil notmuch-show-stash-mlarchive-link-default)))
1938 notmuch-show-stash-mlarchive-link-alist))
1939 (notmuch-show-get-message-id t))))
1941 (defun notmuch-show-stash-mlarchive-link-and-go (&optional mla)
1942 "Copy an ML Archive URI for the current message to the kill-ring and visit it.
1944 This presumes that the message is available at the selected Mailing List Archive.
1946 If optional argument MLA is non-nil, use the provided key instead of prompting
1947 the user (see `notmuch-show-stash-mlarchive-link-alist')."
1949 (notmuch-show-stash-mlarchive-link mla)
1950 (browse-url (current-kill 0 t)))
1952 ;; Commands typically bound to buttons.
1954 (defun notmuch-show-part-button-default (&optional button)
1956 (notmuch-show-part-button-internal button notmuch-show-part-button-default-action))
1958 (defun notmuch-show-part-button-save (&optional button)
1960 (notmuch-show-part-button-internal button #'notmuch-show-save-part))
1962 (defun notmuch-show-part-button-view (&optional button)
1964 (notmuch-show-part-button-internal button #'notmuch-show-view-part))
1966 (defun notmuch-show-part-button-interactively-view (&optional button)
1968 (notmuch-show-part-button-internal button #'notmuch-show-interactively-view-part))
1970 (defun notmuch-show-part-button-pipe (&optional button)
1972 (notmuch-show-part-button-internal button #'notmuch-show-pipe-part))
1974 (defun notmuch-show-part-button-internal (button handler)
1975 (let ((button (or button (button-at (point)))))
1977 (let ((nth (button-get button :notmuch-part)))
1979 (funcall handler (notmuch-show-get-message-id) nth
1980 (button-get button :notmuch-filename)
1981 (button-get button :notmuch-content-type)))))))
1985 (provide 'notmuch-show)