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-query)
34 (require 'notmuch-wash)
35 (require 'notmuch-mua)
36 (require 'notmuch-crypto)
37 (require 'notmuch-print)
39 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
40 (declare-function notmuch-fontify-headers "notmuch" nil)
41 (declare-function notmuch-read-tag-changes "notmuch" (&optional initial-input &rest search-terms))
42 (declare-function notmuch-search-next-thread "notmuch" nil)
43 (declare-function notmuch-search-show-thread "notmuch" nil)
44 (declare-function notmuch-update-tags "notmuch" (current-tags tag-changes))
46 (defcustom notmuch-message-headers '("Subject" "To" "Cc" "Date")
47 "Headers that should be shown in a message, in this order.
49 For an open message, all of these headers will be made visible
50 according to `notmuch-message-headers-visible' or can be toggled
51 with `notmuch-show-toggle-headers'. For a closed message, only
52 the first header in the list will be visible."
53 :type '(repeat string)
56 (defcustom notmuch-message-headers-visible t
57 "Should the headers be visible by default?
59 If this value is non-nil, then all of the headers defined in
60 `notmuch-message-headers' will be visible by default in the display
61 of each message. Otherwise, these headers will be hidden and
62 `notmuch-show-toggle-headers' can be used to make the visible for
67 (defcustom notmuch-show-relative-dates t
68 "Display relative dates in the message summary line."
72 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
73 "A list of functions called to decorate the headers listed in
74 `notmuch-message-headers'.")
76 (defcustom notmuch-show-hook '(notmuch-show-turn-on-visual-line-mode)
77 "Functions called after populating a `notmuch-show' buffer."
79 :options '(notmuch-show-turn-on-visual-line-mode)
81 :group 'notmuch-hooks)
83 (defcustom notmuch-show-insert-text/plain-hook '(notmuch-wash-wrap-long-lines
84 notmuch-wash-tidy-citations
85 notmuch-wash-elide-blank-lines
86 notmuch-wash-excerpt-citations)
87 "Functions used to improve the display of text/plain parts."
89 :options '(notmuch-wash-convert-inline-patch-to-part
90 notmuch-wash-wrap-long-lines
91 notmuch-wash-tidy-citations
92 notmuch-wash-elide-blank-lines
93 notmuch-wash-excerpt-citations)
95 :group 'notmuch-hooks)
97 ;; Mostly useful for debugging.
98 (defcustom notmuch-show-all-multipart/alternative-parts t
99 "Should all parts of multipart/alternative parts be shown?"
101 :group 'notmuch-show)
103 (defcustom notmuch-show-indent-messages-width 1
104 "Width of message indentation in threads.
106 Messages are shown indented according to their depth in a thread.
107 This variable determines the width of this indentation measured
108 in number of blanks. Defaults to `1', choose `0' to disable
111 :group 'notmuch-show)
113 (defcustom notmuch-show-indent-multipart nil
114 "Should the sub-parts of a multipart/* part be indented?"
115 ;; dme: Not sure which is a good default.
117 :group 'notmuch-show)
119 (defcustom notmuch-show-part-button-default-action 'notmuch-show-save-part
120 "Default part header button action (on ENTER or mouse click)."
122 :type '(choice (const :tag "Save part"
123 notmuch-show-save-part)
124 (const :tag "View part"
125 notmuch-show-view-part)
126 (const :tag "View interactively"
127 notmuch-show-interactively-view-part)))
129 (defcustom notmuch-show-only-matching-messages nil
130 "Only matching messages are shown by default."
132 :group 'notmuch-show)
134 (defvar notmuch-show-thread-id nil)
135 (make-variable-buffer-local 'notmuch-show-thread-id)
136 (put 'notmuch-show-thread-id 'permanent-local t)
138 (defvar notmuch-show-parent-buffer nil)
139 (make-variable-buffer-local 'notmuch-show-parent-buffer)
140 (put 'notmuch-show-parent-buffer 'permanent-local t)
142 (defvar notmuch-show-query-context nil)
143 (make-variable-buffer-local 'notmuch-show-query-context)
144 (put 'notmuch-show-query-context 'permanent-local t)
146 (defvar notmuch-show-process-crypto nil)
147 (make-variable-buffer-local 'notmuch-show-process-crypto)
148 (put 'notmuch-show-process-crypto 'permanent-local t)
150 (defvar notmuch-show-elide-non-matching-messages nil)
151 (make-variable-buffer-local 'notmuch-show-elide-non-matching-messages)
152 (put 'notmuch-show-elide-non-matching-messages 'permanent-local t)
154 (defvar notmuch-show-indent-content t)
155 (make-variable-buffer-local 'notmuch-show-indent-content)
156 (put 'notmuch-show-indent-content 'permanent-local t)
158 (defcustom notmuch-show-stash-mlarchive-link-alist
159 '(("Gmane" . "http://mid.gmane.org/")
160 ("MARC" . "http://marc.info/?i=")
161 ("Mail Archive, The" . "http://mail-archive.com/search?l=mid&q=")
162 ;; FIXME: can these services be searched by `Message-Id' ?
163 ;; ("MarkMail" . "http://markmail.org/")
164 ;; ("Nabble" . "http://nabble.com/")
165 ;; ("opensubscriber" . "http://opensubscriber.com/")
167 "List of Mailing List Archives to use when stashing links.
169 These URIs are concatenated with the current message's
170 Message-Id in `notmuch-show-stash-mlarchive-link'."
171 :type '(alist :key-type (string :tag "Name")
172 :value-type (string :tag "URL"))
173 :group 'notmuch-show)
175 (defcustom notmuch-show-stash-mlarchive-link-default "Gmane"
176 "Default Mailing List Archive to use when stashing links.
178 This is used when `notmuch-show-stash-mlarchive-link' isn't
179 provided with an MLA argument nor `completing-read' input."
183 (list 'const :tag (car mla) :value (car mla)))
184 notmuch-show-stash-mlarchive-link-alist))
185 :group 'notmuch-show)
187 (defmacro with-current-notmuch-show-message (&rest body)
188 "Evaluate body with current buffer set to the text of current message"
190 (let ((id (notmuch-show-get-message-id)))
191 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
192 (with-current-buffer buf
193 (call-process notmuch-command nil t nil "show" "--format=raw" id)
195 (kill-buffer buf)))))
197 (defun notmuch-show-turn-on-visual-line-mode ()
198 "Enable Visual Line mode."
199 (visual-line-mode t))
201 (defun notmuch-show-view-all-mime-parts ()
202 "Use external viewers to view all attachments from the current message."
204 (with-current-notmuch-show-message
205 ;; We override the mm-inline-media-tests to indicate which message
206 ;; parts are already sufficiently handled by the original
207 ;; presentation of the message in notmuch-show mode. These parts
208 ;; will be inserted directly into the temporary buffer of
209 ;; with-current-notmuch-show-message and silently discarded.
211 ;; Any MIME part not explicitly mentioned here will be handled by an
212 ;; external viewer as configured in the various mailcap files.
213 (let ((mm-inline-media-tests '(
214 ("text/.*" ignore identity)
215 ("application/pgp-signature" ignore identity)
216 ("multipart/alternative" ignore identity)
217 ("multipart/mixed" ignore identity)
218 ("multipart/related" ignore identity)
220 (mm-display-parts (mm-dissect-buffer)))))
222 (defun notmuch-foreach-mime-part (function mm-handle)
223 (cond ((stringp (car mm-handle))
224 (dolist (part (cdr mm-handle))
225 (notmuch-foreach-mime-part function part)))
226 ((bufferp (car mm-handle))
227 (funcall function mm-handle))
228 (t (dolist (part mm-handle)
229 (notmuch-foreach-mime-part function part)))))
231 (defun notmuch-count-attachments (mm-handle)
233 (notmuch-foreach-mime-part
235 (let ((disposition (mm-handle-disposition p)))
236 (and (listp disposition)
237 (or (equal (car disposition) "attachment")
238 (and (equal (car disposition) "inline")
239 (assq 'filename disposition)))
244 (defun notmuch-save-attachments (mm-handle &optional queryp)
245 (notmuch-foreach-mime-part
247 (let ((disposition (mm-handle-disposition p)))
248 (and (listp disposition)
249 (or (equal (car disposition) "attachment")
250 (and (equal (car disposition) "inline")
251 (assq 'filename disposition)))
254 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
258 (defun notmuch-show-save-attachments ()
259 "Save all attachments from the current message."
261 (with-current-notmuch-show-message
262 (let ((mm-handle (mm-dissect-buffer)))
263 (notmuch-save-attachments
264 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
267 (defun notmuch-show-with-message-as-text (fn)
268 "Apply FN to a text representation of the current message.
270 FN is called with one argument, the message properties. It should
271 operation on the contents of the current buffer."
273 ;; Remake the header to ensure that all information is available.
274 (let* ((to (notmuch-show-get-to))
275 (cc (notmuch-show-get-cc))
276 (from (notmuch-show-get-from))
277 (subject (notmuch-show-get-subject))
278 (date (notmuch-show-get-date))
279 (tags (notmuch-show-get-tags))
280 (depth (notmuch-show-get-depth))
283 "Subject: " subject "\n"
285 (if (not (string= cc ""))
286 (concat "Cc: " cc "\n")
292 (mapconcat #'identity tags ", ") "\n")
294 (all (buffer-substring (notmuch-show-message-top)
295 (notmuch-show-message-bottom)))
297 (props (notmuch-show-get-message-properties))
298 (indenting notmuch-show-indent-content))
302 (indent-rigidly (point-min) (point-max) (- depth)))
303 ;; Remove the original header.
304 (goto-char (point-min))
305 (re-search-forward "^$" (point-max) nil)
306 (delete-region (point-min) (point))
308 (funcall fn props))))
310 (defun notmuch-show-print-message ()
311 "Print the current message."
313 (notmuch-show-with-message-as-text 'notmuch-print-message))
315 (defun notmuch-show-fontify-header ()
317 ((looking-at "[Tt]o:")
319 ((looking-at "[Bb]?[Cc][Cc]:")
321 ((looking-at "[Ss]ubject:")
322 'message-header-subject)
323 ((looking-at "[Ff]rom:")
324 'message-header-from)
326 'message-header-other))))
328 (overlay-put (make-overlay (point) (re-search-forward ":"))
329 'face 'message-header-name)
330 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
333 (defun notmuch-show-colour-headers ()
334 "Apply some colouring to the current headers."
335 (goto-char (point-min))
336 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
337 (notmuch-show-fontify-header)
340 (defun notmuch-show-spaces-n (n)
341 "Return a string comprised of `n' spaces."
344 (defun notmuch-show-update-tags (tags)
345 "Update the displayed tags of the current message."
347 (goto-char (notmuch-show-message-top))
348 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
349 (let ((inhibit-read-only t))
350 (replace-match (concat "("
351 (propertize (mapconcat 'identity tags " ")
352 'face 'notmuch-tag-face)
355 (defun notmuch-show-clean-address (address)
356 "Try to clean a single email ADDRESS for display. Return
357 unchanged ADDRESS if parsing fails."
359 (let (p-name p-address)
360 ;; It would be convenient to use `mail-header-parse-address',
361 ;; but that expects un-decoded mailbox parts, whereas our
362 ;; mailbox parts are already decoded (and hence may contain
363 ;; UTF-8). Given that notmuch should handle most of the awkward
364 ;; cases, some simple string deconstruction should be sufficient
367 ;; "User <user@dom.ain>" style.
368 ((string-match "\\(.*\\) <\\(.*\\)>" address)
369 (setq p-name (match-string 1 address)
370 p-address (match-string 2 address)))
372 ;; "<user@dom.ain>" style.
373 ((string-match "<\\(.*\\)>" address)
374 (setq p-address (match-string 1 address)))
378 (setq p-address address)))
381 ;; Remove elements of the mailbox part that are not relevant for
382 ;; display, even if they are required during transport:
385 (setq p-name (replace-regexp-in-string "\\\\" "" p-name))
387 ;; Outer single and double quotes, which might be nested.
390 do (setq start-of-loop p-name)
392 when (string-match "^\"\\(.*\\)\"$" p-name)
393 do (setq p-name (match-string 1 p-name))
395 when (string-match "^'\\(.*\\)'$" p-name)
396 do (setq p-name (match-string 1 p-name))
398 until (string= start-of-loop p-name)))
400 ;; If the address is 'foo@bar.com <foo@bar.com>' then show just
402 (when (string= p-name p-address)
405 ;; If no name results, return just the address.
408 ;; Otherwise format the name and address together.
409 (concat p-name " <" p-address ">")))
412 (defun notmuch-show-insert-headerline (headers date tags depth)
413 "Insert a notmuch style headerline based on HEADERS for a
414 message at DEPTH in the current thread."
415 (let ((start (point)))
416 (insert (notmuch-show-spaces-n (* notmuch-show-indent-messages-width depth))
417 (notmuch-show-clean-address (plist-get headers :From))
421 (propertize (mapconcat 'identity tags " ")
422 'face 'notmuch-tag-face)
424 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
426 (defun notmuch-show-insert-header (header header-value)
427 "Insert a single header."
428 (insert header ": " header-value "\n"))
430 (defun notmuch-show-insert-headers (headers)
431 "Insert the headers of the current message."
432 (let ((start (point)))
433 (mapc (lambda (header)
434 (let* ((header-symbol (intern (concat ":" header)))
435 (header-value (plist-get headers header-symbol)))
436 (if (and header-value
437 (not (string-equal "" header-value)))
438 (notmuch-show-insert-header header header-value))))
439 notmuch-message-headers)
442 (narrow-to-region start (point-max))
443 (run-hooks 'notmuch-show-markup-headers-hook)))))
445 (define-button-type 'notmuch-show-part-button-type
446 'action 'notmuch-show-part-button-default
447 'keymap 'notmuch-show-part-button-map
451 (defvar notmuch-show-part-button-map
452 (let ((map (make-sparse-keymap)))
453 (set-keymap-parent map button-map)
454 (define-key map "s" 'notmuch-show-part-button-save)
455 (define-key map "v" 'notmuch-show-part-button-view)
456 (define-key map "o" 'notmuch-show-part-button-interactively-view)
458 "Submap for button commands")
459 (fset 'notmuch-show-part-button-map notmuch-show-part-button-map)
461 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name comment)
466 (if name (concat name ": ") "")
468 (if (not (string-equal declared-type content-type))
469 (concat " (as " content-type ")")
473 :type 'notmuch-show-part-button-type
475 :notmuch-filename name
476 :notmuch-content-type content-type))
481 ;; Functions handling particular MIME parts.
483 (defmacro notmuch-with-temp-part-buffer (message-id nth &rest body)
485 (let ((process-crypto (make-symbol "process-crypto")))
486 `(let ((,process-crypto notmuch-show-process-crypto))
488 (setq notmuch-show-process-crypto ,process-crypto)
489 ;; Always acquires the part via `notmuch part', even if it is
490 ;; available in the JSON output.
491 (insert (notmuch-show-get-bodypart-internal ,message-id ,nth))
494 (defun notmuch-show-save-part (message-id nth &optional filename content-type)
495 (notmuch-with-temp-part-buffer message-id nth
496 (let ((file (read-file-name
497 "Filename to save as: "
498 (or mailcap-download-directory "~/")
501 ;; Don't re-compress .gz & al. Arguably we should make
502 ;; `file-name-handler-alist' nil, but that would chop
503 ;; ange-ftp, which is reasonable to use here.
504 (mm-write-region (point-min) (point-max) file nil nil nil 'no-conversion t))))
506 (defun notmuch-show-view-part (message-id nth &optional filename content-type )
507 (notmuch-with-temp-part-buffer message-id nth
508 ;; set mm-inlined-types to nil to force an external viewer
509 (let ((handle (mm-make-handle (current-buffer) (list content-type)))
510 (mm-inlined-types nil))
511 ;; We override mm-save-part as notmuch-show-save-part is better
512 ;; since it offers the filename. We need to lexically bind
513 ;; everything we need for notmuch-show-save-part to prevent
514 ;; potential dynamic shadowing.
515 (lexical-let ((message-id message-id)
518 (content-type content-type))
519 (flet ((mm-save-part (&rest args) (notmuch-show-save-part
520 message-id nth filename content-type)))
521 (mm-display-part handle))))))
523 (defun notmuch-show-interactively-view-part (message-id nth &optional filename content-type)
524 (notmuch-with-temp-part-buffer message-id nth
525 (let ((handle (mm-make-handle (current-buffer) (list content-type))))
526 (mm-interactively-view-part handle))))
528 (defun notmuch-show-mm-display-part-inline (msg part nth content-type)
529 "Use the mm-decode/mm-view functions to display a part in the
530 current buffer, if possible."
531 (let ((display-buffer (current-buffer)))
533 (let* ((charset (plist-get part :content-charset))
534 (handle (mm-make-handle (current-buffer) `(,content-type (charset . ,charset)))))
535 ;; If the user wants the part inlined, insert the content and
536 ;; test whether we are able to inline it (which includes both
537 ;; capability and suitability tests).
538 (when (mm-inlined-p handle)
539 (insert (notmuch-show-get-bodypart-content msg part nth))
540 (when (mm-inlinable-p handle)
541 (set-buffer display-buffer)
542 (mm-display-part handle)
545 (defvar notmuch-show-multipart/alternative-discouraged
549 ;; multipart/related usually contain a text/html part and some associated graphics.
553 (defun notmuch-show-multipart/*-to-list (part)
554 (mapcar (lambda (inner-part) (plist-get inner-part :content-type))
555 (plist-get part :content)))
557 (defun notmuch-show-multipart/alternative-choose (types)
558 ;; Based on `mm-preferred-alternative-precedence'.
560 (dolist (pref (reverse notmuch-show-multipart/alternative-discouraged))
561 (dolist (elem (copy-sequence seq))
562 (when (string-match pref elem)
563 (setq seq (nconc (delete elem seq) (list elem))))))
566 (defun notmuch-show-insert-part-multipart/alternative (msg part content-type nth depth declared-type)
567 (notmuch-show-insert-part-header nth declared-type content-type nil)
568 (let ((chosen-type (car (notmuch-show-multipart/alternative-choose (notmuch-show-multipart/*-to-list part))))
569 (inner-parts (plist-get part :content))
571 ;; This inserts all parts of the chosen type rather than just one,
572 ;; but it's not clear that this is the wrong thing to do - which
573 ;; should be chosen if there are more than one that match?
574 (mapc (lambda (inner-part)
575 (let ((inner-type (plist-get inner-part :content-type)))
576 (if (or notmuch-show-all-multipart/alternative-parts
577 (string= chosen-type inner-type))
578 (notmuch-show-insert-bodypart msg inner-part depth)
579 (notmuch-show-insert-part-header (plist-get inner-part :id) inner-type inner-type nil " (not shown)"))))
582 (when notmuch-show-indent-multipart
583 (indent-rigidly start (point) 1)))
586 (defun notmuch-show-setup-w3m ()
587 "Instruct w3m how to retrieve content from a \"related\" part of a message."
589 (if (boundp 'w3m-cid-retrieve-function-alist)
590 (unless (assq 'notmuch-show-mode w3m-cid-retrieve-function-alist)
591 (push (cons 'notmuch-show-mode 'notmuch-show-w3m-cid-retrieve)
592 w3m-cid-retrieve-function-alist)))
593 (setq mm-inline-text-html-with-images t))
595 (defvar w3m-current-buffer) ;; From `w3m.el'.
596 (defvar notmuch-show-w3m-cid-store nil)
597 (make-variable-buffer-local 'notmuch-show-w3m-cid-store)
599 (defun notmuch-show-w3m-cid-store-internal (content-id
604 (push (list content-id
609 notmuch-show-w3m-cid-store))
611 (defun notmuch-show-w3m-cid-store (msg part)
612 (let ((content-id (plist-get part :content-id)))
614 (notmuch-show-w3m-cid-store-internal (concat "cid:" content-id)
617 (plist-get part :content-type)
620 (defun notmuch-show-w3m-cid-retrieve (url &rest args)
621 (let ((matching-part (with-current-buffer w3m-current-buffer
622 (assoc url notmuch-show-w3m-cid-store))))
624 (let ((message-id (nth 1 matching-part))
625 (part-number (nth 2 matching-part))
626 (content-type (nth 3 matching-part))
627 (content (nth 4 matching-part)))
628 ;; If we don't already have the content, get it and cache
629 ;; it, as some messages reference the same cid: part many
630 ;; times (hundreds!), which results in many calls to
633 (setq content (notmuch-show-get-bodypart-internal (concat "id:" message-id)
635 (with-current-buffer w3m-current-buffer
636 (notmuch-show-w3m-cid-store-internal url
645 (defun notmuch-show-insert-part-multipart/related (msg part content-type nth depth declared-type)
646 (notmuch-show-insert-part-header nth declared-type content-type nil)
647 (let ((inner-parts (plist-get part :content))
650 ;; We assume that the first part is text/html and the remainder
651 ;; things that it references.
653 ;; Stash the non-primary parts.
655 (notmuch-show-w3m-cid-store msg part))
658 ;; Render the primary part.
659 (notmuch-show-insert-bodypart msg (car inner-parts) depth)
661 (when notmuch-show-indent-multipart
662 (indent-rigidly start (point) 1)))
665 (defun notmuch-show-insert-part-multipart/signed (msg part content-type nth depth declared-type)
666 (let ((button (notmuch-show-insert-part-header nth declared-type content-type nil)))
667 (button-put button 'face 'notmuch-crypto-part-header)
668 ;; add signature status button if sigstatus provided
669 (if (plist-member part :sigstatus)
670 (let* ((from (notmuch-show-get-header :From msg))
671 (sigstatus (car (plist-get part :sigstatus))))
672 (notmuch-crypto-insert-sigstatus-button sigstatus from))
673 ;; if we're not adding sigstatus, tell the user how they can get it
674 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts.")))
676 (let ((inner-parts (plist-get part :content))
678 ;; Show all of the parts.
679 (mapc (lambda (inner-part)
680 (notmuch-show-insert-bodypart msg inner-part depth))
683 (when notmuch-show-indent-multipart
684 (indent-rigidly start (point) 1)))
687 (defun notmuch-show-insert-part-multipart/encrypted (msg part content-type nth depth declared-type)
688 (let ((button (notmuch-show-insert-part-header nth declared-type content-type nil)))
689 (button-put button 'face 'notmuch-crypto-part-header)
690 ;; add encryption status button if encstatus specified
691 (if (plist-member part :encstatus)
692 (let ((encstatus (car (plist-get part :encstatus))))
693 (notmuch-crypto-insert-encstatus-button encstatus)
694 ;; add signature status button if sigstatus specified
695 (if (plist-member part :sigstatus)
696 (let* ((from (notmuch-show-get-header :From msg))
697 (sigstatus (car (plist-get part :sigstatus))))
698 (notmuch-crypto-insert-sigstatus-button sigstatus from))))
699 ;; if we're not adding encstatus, tell the user how they can get it
700 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts.")))
702 (let ((inner-parts (plist-get part :content))
704 ;; Show all of the parts.
705 (mapc (lambda (inner-part)
706 (notmuch-show-insert-bodypart msg inner-part depth))
709 (when notmuch-show-indent-multipart
710 (indent-rigidly start (point) 1)))
713 (defun notmuch-show-insert-part-multipart/* (msg part content-type nth depth declared-type)
714 (notmuch-show-insert-part-header nth declared-type content-type nil)
715 (let ((inner-parts (plist-get part :content))
717 ;; Show all of the parts.
718 (mapc (lambda (inner-part)
719 (notmuch-show-insert-bodypart msg inner-part depth))
722 (when notmuch-show-indent-multipart
723 (indent-rigidly start (point) 1)))
726 (defun notmuch-show-insert-part-message/rfc822 (msg part content-type nth depth declared-type)
727 (notmuch-show-insert-part-header nth declared-type content-type nil)
728 (let* ((message (car (plist-get part :content)))
729 (body (car (plist-get message :body)))
732 ;; Override `notmuch-message-headers' to force `From' to be
734 (let ((notmuch-message-headers '("From" "Subject" "To" "Cc" "Date")))
735 (notmuch-show-insert-headers (plist-get message :headers)))
737 ;; Blank line after headers to be compatible with the normal
742 (notmuch-show-insert-bodypart msg body depth)
744 (when notmuch-show-indent-multipart
745 (indent-rigidly start (point) 1)))
748 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
749 (let ((start (point)))
750 ;; If this text/plain part is not the first part in the message,
751 ;; insert a header to make this clear.
753 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
754 (insert (notmuch-show-get-bodypart-content msg part nth))
757 (narrow-to-region start (point-max))
758 (run-hook-with-args 'notmuch-show-insert-text/plain-hook msg depth))))
761 (defun notmuch-show-insert-part-text/calendar (msg part content-type nth depth declared-type)
762 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename))
763 (insert (with-temp-buffer
764 (insert (notmuch-show-get-bodypart-content msg part nth))
765 (goto-char (point-min))
766 (let ((file (make-temp-file "notmuch-ical"))
768 (icalendar--convert-ical-to-diary
769 (icalendar--read-element nil nil)
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))
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))
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)))
791 (mailcap-parse-mimetypes)
792 (setq mime-type (mailcap-extension-to-mime extension))
794 (not (string-equal mime-type "application/octet-stream")))
795 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
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"))
803 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
804 ;; This handler _must_ succeed - it is the handler of last resort.
805 (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
806 (notmuch-show-mm-display-part-inline msg part nth content-type)
809 ;; Functions for determining how to handle MIME parts.
811 (defun notmuch-show-split-content-type (content-type)
812 (split-string content-type "/"))
814 (defun notmuch-show-handlers-for (content-type)
815 "Return a list of content handlers for a part of type CONTENT-TYPE."
820 ;; Reverse order of prefrence.
821 (list (intern (concat "notmuch-show-insert-part-*/*"))
823 "notmuch-show-insert-part-"
824 (car (notmuch-show-split-content-type content-type))
826 (intern (concat "notmuch-show-insert-part-" content-type))))
829 ;; Helper for parts which are generally not included in the default
831 (defun notmuch-show-get-bodypart-internal (message-id part-number)
832 (let ((args '("show" "--format=raw"))
833 (part-arg (format "--part=%s" part-number)))
834 (setq args (append args (list part-arg)))
835 (if notmuch-show-process-crypto
836 (setq args (append args '("--decrypt"))))
837 (setq args (append args (list message-id)))
839 (let ((coding-system-for-read 'no-conversion))
841 (apply 'call-process (append (list notmuch-command nil (list t nil) nil) args))
844 (defun notmuch-show-get-bodypart-content (msg part nth)
845 (or (plist-get part :content)
846 (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
850 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
851 (let ((handlers (notmuch-show-handlers-for content-type)))
852 ;; Run the content handlers until one of them returns a non-nil
855 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
856 (setq handlers (cdr handlers))))
859 (defun notmuch-show-insert-bodypart (msg part depth)
860 "Insert the body part PART at depth DEPTH in the current thread."
861 (let ((content-type (downcase (plist-get part :content-type)))
862 (nth (plist-get part :id)))
863 (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
864 ;; Some of the body part handlers leave point somewhere up in the
865 ;; part, so we make sure that we're down at the end.
866 (goto-char (point-max))
867 ;; Ensure that the part ends with a carriage return.
871 (defun notmuch-show-insert-body (msg body depth)
872 "Insert the body BODY at depth DEPTH in the current thread."
873 (mapc (lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
875 (defun notmuch-show-make-symbol (type)
876 (make-symbol (concat "notmuch-show-" type)))
878 (defun notmuch-show-strip-re (string)
879 (replace-regexp-in-string "\\([Rr]e: *\\)+" "" string))
881 (defvar notmuch-show-previous-subject "")
882 (make-variable-buffer-local 'notmuch-show-previous-subject)
884 (defun notmuch-show-insert-msg (msg depth)
885 "Insert the message MSG at depth DEPTH in the current thread."
886 (let* ((headers (plist-get msg :headers))
887 ;; Indentation causes the buffer offset of the start/end
888 ;; points to move, so we must use markers.
889 message-start message-end
890 content-start content-end
891 headers-start headers-end
893 (headers-invis-spec (notmuch-show-make-symbol "header"))
894 (message-invis-spec (notmuch-show-make-symbol "message"))
895 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
897 ;; Set `buffer-invisibility-spec' to `nil' (a list), otherwise
898 ;; removing items from `buffer-invisibility-spec' (which is what
899 ;; `notmuch-show-headers-visible' and
900 ;; `notmuch-show-message-visible' do) is a no-op and has no
901 ;; effect. This caused threads with only matching messages to have
902 ;; those messages hidden initially because
903 ;; `buffer-invisibility-spec' stayed `t'.
905 ;; This needs to be set here (rather than just above the call to
906 ;; `notmuch-show-headers-visible') because some of the part
907 ;; rendering or body washing functions
908 ;; (e.g. `notmuch-wash-text/plain-citations') manipulate
909 ;; `buffer-invisibility-spec').
910 (when (eq buffer-invisibility-spec t)
911 (setq buffer-invisibility-spec nil))
913 (setq message-start (point-marker))
915 (notmuch-show-insert-headerline headers
916 (or (if notmuch-show-relative-dates
917 (plist-get msg :date_relative)
919 (plist-get headers :Date))
920 (plist-get msg :tags) depth)
922 (setq content-start (point-marker))
924 (plist-put msg :headers-invis-spec headers-invis-spec)
925 (plist-put msg :message-invis-spec message-invis-spec)
927 ;; Set `headers-start' to point after the 'Subject:' header to be
928 ;; compatible with the existing implementation. This just sets it
929 ;; to after the first header.
930 (notmuch-show-insert-headers headers)
932 (goto-char content-start)
933 ;; If the subject of this message is the same as that of the
934 ;; previous message, don't display it when this message is
936 (when (not (string= notmuch-show-previous-subject
939 (setq headers-start (point-marker)))
940 (setq headers-end (point-marker))
942 (setq notmuch-show-previous-subject bare-subject)
944 (setq body-start (point-marker))
945 ;; A blank line between the headers and the body.
947 (notmuch-show-insert-body msg (plist-get msg :body)
948 (if notmuch-show-indent-content depth 0))
949 ;; Ensure that the body ends with a newline.
952 (setq body-end (point-marker))
953 (setq content-end (point-marker))
955 ;; Indent according to the depth in the thread.
956 (if notmuch-show-indent-content
957 (indent-rigidly content-start content-end (* notmuch-show-indent-messages-width depth)))
959 (setq message-end (point-max-marker))
961 ;; Save the extents of this message over the whole text of the
963 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
965 (let ((headers-overlay (make-overlay headers-start headers-end))
966 (invis-specs (list headers-invis-spec message-invis-spec)))
967 (overlay-put headers-overlay 'invisible invis-specs)
968 (overlay-put headers-overlay 'priority 10))
969 (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
971 (plist-put msg :depth depth)
973 ;; Save the properties for this message. Currently this saves the
974 ;; entire message (augmented it with other stuff), which seems
975 ;; like overkill. We might save a reduced subset (for example, not
977 (notmuch-show-set-message-properties msg)
979 ;; Set header visibility.
980 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
982 ;; Message visibility depends on whether it matched the search
984 (notmuch-show-message-visible msg (plist-get msg :match))))
986 (defun notmuch-show-toggle-process-crypto ()
987 "Toggle the processing of cryptographic MIME parts."
989 (setq notmuch-show-process-crypto (not notmuch-show-process-crypto))
990 (message (if notmuch-show-process-crypto
991 "Processing cryptographic MIME parts."
992 "Not processing cryptographic MIME parts."))
993 (notmuch-show-refresh-view))
995 (defun notmuch-show-toggle-elide-non-matching ()
996 "Toggle the display of non-matching messages."
998 (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages))
999 (message (if notmuch-show-elide-non-matching-messages
1000 "Showing matching messages only."
1001 "Showing all messages."))
1002 (notmuch-show-refresh-view))
1004 (defun notmuch-show-toggle-thread-indentation ()
1005 "Toggle the indentation of threads."
1007 (setq notmuch-show-indent-content (not notmuch-show-indent-content))
1008 (message (if notmuch-show-indent-content
1009 "Content is indented."
1010 "Content is not indented."))
1011 (notmuch-show-refresh-view))
1013 (defun notmuch-show-insert-tree (tree depth)
1014 "Insert the message tree TREE at depth DEPTH in the current thread."
1015 (let ((msg (car tree))
1016 (replies (cadr tree)))
1017 (if (or (not notmuch-show-elide-non-matching-messages)
1018 (plist-get msg :match))
1019 (notmuch-show-insert-msg msg depth))
1020 (notmuch-show-insert-thread replies (1+ depth))))
1022 (defun notmuch-show-insert-thread (thread depth)
1023 "Insert the thread THREAD at depth DEPTH in the current forest."
1024 (mapc (lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
1026 (defun notmuch-show-insert-forest (forest)
1027 "Insert the forest of threads FOREST."
1028 (mapc (lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
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 into buttons for
1034 a corresponding notmuch search."
1035 (goto-address-fontify-region start end)
1038 (while (re-search-forward "id:\\(\"?\\)[^[:space:]\"]+\\1" end t)
1039 ;; remove the overlay created by goto-address-mode
1040 (remove-overlays (match-beginning 0) (match-end 0) 'goto-address t)
1041 (make-text-button (match-beginning 0) (match-end 0)
1042 'action `(lambda (arg)
1043 (notmuch-show ,(match-string-no-properties 0)))
1045 'help-echo "Mouse-1, RET: search for this message"
1046 'face goto-address-mail-face))))
1049 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
1050 "Run \"notmuch show\" with the given thread ID and display results.
1052 The optional PARENT-BUFFER is the notmuch-search buffer from
1053 which this notmuch-show command was executed, (so that the
1054 next thread from that buffer can be show when done with this
1057 The optional QUERY-CONTEXT is a notmuch search term. Only
1058 messages from the thread matching this search term are shown if
1061 The optional BUFFER-NAME provides the name of the buffer in
1062 which the message thread is shown. If it is nil (which occurs
1063 when the command is called interactively) the argument to the
1065 (interactive "sNotmuch show: ")
1066 (let ((buffer-name (generate-new-buffer-name
1068 (concat "*notmuch-" thread-id "*")))))
1069 (switch-to-buffer (get-buffer-create buffer-name))
1070 ;; Set the default value for `notmuch-show-process-crypto' in this
1072 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1073 ;; Set the default value for
1074 ;; `notmuch-show-elide-non-matching-messages' in this buffer. If
1075 ;; there is a prefix argument, invert the default.
1076 (setq notmuch-show-elide-non-matching-messages notmuch-show-only-matching-messages)
1077 (if current-prefix-arg
1078 (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages)))
1080 (setq notmuch-show-thread-id thread-id
1081 notmuch-show-parent-buffer parent-buffer
1082 notmuch-show-query-context query-context)
1083 (notmuch-show-build-buffer)
1085 ;; Move to the first open message and mark it read
1086 (if (notmuch-show-message-visible-p)
1087 (notmuch-show-mark-read)
1088 (notmuch-show-next-open-message))))
1090 (defun notmuch-show-build-buffer ()
1091 (let ((inhibit-read-only t))
1094 ;; Don't track undo information for this buffer
1095 (set 'buffer-undo-list t)
1098 (goto-char (point-min))
1100 (let* ((basic-args (list notmuch-show-thread-id))
1101 (args (if notmuch-show-query-context
1102 (append (list "\'") basic-args
1103 (list "and (" notmuch-show-query-context ")\'"))
1104 (append (list "\'") basic-args (list "\'")))))
1105 (notmuch-show-insert-forest (notmuch-query-get-threads args))
1106 ;; If the query context reduced the results to nothing, run
1108 (when (and (eq (buffer-size) 0)
1109 notmuch-show-query-context)
1110 (notmuch-show-insert-forest
1111 (notmuch-query-get-threads basic-args))))
1113 (jit-lock-register #'notmuch-show-buttonise-links)
1115 (run-hooks 'notmuch-show-hook))
1117 ;; Set the header line to the subject of the first message.
1118 (setq header-line-format (notmuch-show-strip-re (notmuch-show-get-pretty-subject)))))
1120 (defun notmuch-show-capture-state ()
1121 "Capture the state of the current buffer.
1124 - the list of open messages,
1125 - the current message."
1126 (list (notmuch-show-get-message-id) (notmuch-show-get-message-ids-for-open-messages)))
1128 (defun notmuch-show-apply-state (state)
1129 "Apply STATE to the current buffer.
1132 - opening the messages previously opened,
1133 - closing all other messages,
1134 - moving to the correct current message."
1135 (let ((current (car state))
1136 (open (cadr state)))
1138 ;; Open those that were open.
1139 (goto-char (point-min))
1140 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1141 (member (notmuch-show-get-message-id) open))
1142 until (not (notmuch-show-goto-message-next)))
1144 ;; Go to the previously open message.
1145 (goto-char (point-min))
1146 (unless (loop if (string= current (notmuch-show-get-message-id))
1148 until (not (notmuch-show-goto-message-next)))
1149 (goto-char (point-min))
1150 (message "Previously current message not found."))
1151 (notmuch-show-message-adjust)))
1153 (defun notmuch-show-refresh-view (&optional reset-state)
1154 "Refresh the current view.
1156 Refreshes the current view, observing changes in display
1157 preferences. If invoked with a prefix argument (or RESET-STATE is
1158 non-nil) then the state of the buffer (open/closed messages) is
1159 reset based on the original query."
1161 (let ((inhibit-read-only t)
1162 (state (unless reset-state
1163 (notmuch-show-capture-state))))
1165 (notmuch-show-build-buffer)
1167 (notmuch-show-apply-state state)
1168 ;; We're resetting state, so navigate to the first open message
1169 ;; and mark it read, just like opening a new show buffer.
1170 (if (notmuch-show-message-visible-p)
1171 (notmuch-show-mark-read)
1172 (notmuch-show-next-open-message)))))
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)
1188 "Submap for stash commands")
1189 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
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-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)
1232 "Keymap for \"notmuch show\" buffers.")
1233 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
1235 (defun notmuch-show-mode ()
1236 "Major mode for viewing a thread with notmuch.
1238 This buffer contains the results of the \"notmuch show\" command
1239 for displaying a single thread of email from your email archives.
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).
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).
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]).
1257 You can add or remove arbitrary tags from the current message with
1258 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
1260 All currently available key bindings:
1262 \\{notmuch-show-mode-map}"
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
1271 (defun notmuch-show-move-to-message-top ()
1272 (goto-char (notmuch-show-message-top)))
1274 (defun notmuch-show-move-to-message-bottom ()
1275 (goto-char (notmuch-show-message-bottom)))
1277 (defun notmuch-show-message-adjust ()
1280 ;; Movement related functions.
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 ()
1288 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
1292 (defun notmuch-show-message-top ()
1293 (car (notmuch-show-message-extent)))
1295 (defun notmuch-show-message-bottom ()
1296 (cdr (notmuch-show-message-extent)))
1298 (defun notmuch-show-goto-message-next ()
1299 (let ((start (point)))
1300 (notmuch-show-move-to-message-bottom)
1306 (defun notmuch-show-goto-message-previous ()
1307 (notmuch-show-move-to-message-top)
1311 (notmuch-show-move-to-message-top)
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
1319 (goto-char (point-min))
1320 (loop do (funcall function)
1321 while (notmuch-show-goto-message-next))))
1323 ;; Functions relating to the visibility of messages and their
1326 (defun notmuch-show-element-visible (props visible-p spec-property)
1327 (let ((spec (plist-get props spec-property)))
1329 (remove-from-invisibility-spec spec)
1330 (add-to-invisibility-spec spec))))
1332 (defun notmuch-show-message-visible (props visible-p)
1333 (notmuch-show-element-visible props visible-p :message-invis-spec)
1334 (notmuch-show-set-prop :message-visible visible-p props))
1336 (defun notmuch-show-headers-visible (props visible-p)
1337 (notmuch-show-element-visible props visible-p :headers-invis-spec)
1338 (notmuch-show-set-prop :headers-visible visible-p props))
1340 ;; Functions for setting and getting attributes of the current
1343 (defun notmuch-show-set-message-properties (props)
1345 (notmuch-show-move-to-message-top)
1346 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
1348 (defun notmuch-show-get-message-properties ()
1349 "Return the properties of the current message as a plist.
1351 Some useful entries are:
1352 :headers - Property list containing the headers :Date, :Subject, :From, etc.
1353 :body - Body of the message
1354 :tags - Tags for this message"
1356 (notmuch-show-move-to-message-top)
1357 (get-text-property (point) :notmuch-message-properties)))
1359 (defun notmuch-show-set-prop (prop val &optional props)
1360 (let ((inhibit-read-only t)
1362 (notmuch-show-get-message-properties))))
1363 (plist-put props prop val)
1364 (notmuch-show-set-message-properties props)))
1366 (defun notmuch-show-get-prop (prop &optional props)
1367 (let ((props (or props
1368 (notmuch-show-get-message-properties))))
1369 (plist-get props prop)))
1371 (defun notmuch-show-get-message-id (&optional bare)
1372 "Return the Message-Id of the current message.
1374 If optional argument BARE is non-nil, return
1375 the Message-Id without prefix and quotes."
1377 (notmuch-show-get-prop :id)
1378 (concat "id:\"" (notmuch-show-get-prop :id) "\"")))
1380 (defun notmuch-show-get-messages-ids ()
1381 "Return all message ids of messages in the current thread."
1382 (let ((message-ids))
1384 (lambda () (push (notmuch-show-get-message-id) message-ids)))
1387 (defun notmuch-show-get-messages-ids-search ()
1388 "Return a search string for all message ids of messages in the
1390 (mapconcat 'identity (notmuch-show-get-messages-ids) " or "))
1392 ;; dme: Would it make sense to use a macro for many of these?
1394 (defun notmuch-show-get-filename ()
1395 "Return the filename of the current message."
1396 (notmuch-show-get-prop :filename))
1398 (defun notmuch-show-get-header (header &optional props)
1399 "Return the named header of the current message, if any."
1400 (plist-get (notmuch-show-get-prop :headers props) header))
1402 (defun notmuch-show-get-cc ()
1403 (notmuch-show-get-header :Cc))
1405 (defun notmuch-show-get-date ()
1406 (notmuch-show-get-header :Date))
1408 (defun notmuch-show-get-from ()
1409 (notmuch-show-get-header :From))
1411 (defun notmuch-show-get-subject ()
1412 (notmuch-show-get-header :Subject))
1414 (defun notmuch-show-get-to ()
1415 (notmuch-show-get-header :To))
1417 (defun notmuch-show-get-depth ()
1418 (notmuch-show-get-prop :depth))
1420 (defun notmuch-show-get-pretty-subject ()
1421 (notmuch-prettify-subject (notmuch-show-get-subject)))
1423 (defun notmuch-show-set-tags (tags)
1424 "Set the tags of the current message."
1425 (notmuch-show-set-prop :tags tags)
1426 (notmuch-show-update-tags tags))
1428 (defun notmuch-show-get-tags ()
1429 "Return the tags of the current message."
1430 (notmuch-show-get-prop :tags))
1432 (defun notmuch-show-message-visible-p ()
1433 "Is the current message visible?"
1434 (notmuch-show-get-prop :message-visible))
1436 (defun notmuch-show-headers-visible-p ()
1437 "Are the headers of the current message visible?"
1438 (notmuch-show-get-prop :headers-visible))
1440 (defun notmuch-show-mark-read ()
1441 "Mark the current message as read."
1442 (notmuch-show-tag-message "-unread"))
1444 ;; Functions for getting attributes of several messages in the current
1447 (defun notmuch-show-get-message-ids-for-open-messages ()
1448 "Return a list of all message IDs for open messages in the current thread."
1450 (let (message-ids done)
1451 (goto-char (point-min))
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)))
1460 ;; Commands typically bound to keys.
1462 (defun notmuch-show-advance ()
1463 "Advance through thread.
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.
1468 Otherwise, (the end of the current message is already within the
1469 current window), advance to the next open message."
1471 (let* ((end-of-this-message (notmuch-show-message-bottom))
1472 (visible-end-of-this-message (1- end-of-this-message))
1474 (while (invisible-p visible-end-of-this-message)
1475 (setq visible-end-of-this-message
1477 (1- (previous-single-char-property-change
1478 visible-end-of-this-message 'invisible)))))
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.
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))
1493 ;; This is the last message - change the return value
1497 (defun notmuch-show-advance-and-archive ()
1498 "Advance through thread and archive.
1500 This command is intended to be one of the simplest ways to
1501 process a thread of email. It works exactly like
1502 notmuch-show-advance, in that it scrolls through messages in a
1503 show buffer, except that when it gets to the end of the buffer it
1504 archives the entire current thread, (remove the \"inbox\" tag
1505 from each message), kills the buffer, and displays the next
1506 thread from the search from which this thread was originally
1509 (if (notmuch-show-advance)
1510 (notmuch-show-archive-thread-then-next)))
1512 (defun notmuch-show-rewind ()
1513 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
1515 Specifically, if the beginning of the previous email is fewer
1516 than `window-height' lines from the current point, move to it
1517 just like `notmuch-show-previous-message'.
1519 Otherwise, just scroll down a screenful of the current message.
1521 This command does not modify any message tags, (it does not undo
1522 any effects from previous calls to
1523 `notmuch-show-advance-and-archive'."
1525 (let ((start-of-message (notmuch-show-message-top))
1526 (start-of-window (window-start)))
1528 ;; Either this message is properly aligned with the start of the
1529 ;; window or the start of this message is not visible on the
1531 ((or (= start-of-message start-of-window)
1532 (< start-of-message start-of-window))
1534 ;; If a small number of lines from the previous message are
1535 ;; visible, realign so that the top of the current message is at
1536 ;; the top of the screen.
1537 (when (<= (count-screen-lines (window-start) start-of-message)
1538 next-screen-context-lines)
1539 (goto-char (notmuch-show-message-top))
1540 (notmuch-show-message-adjust))
1541 ;; Move to the top left of the window.
1542 (goto-char (window-start)))
1544 ;; Move to the previous message.
1545 (notmuch-show-previous-message)))))
1547 (defun notmuch-show-reply (&optional prompt-for-sender)
1548 "Reply to the sender and all recipients of the current message."
1550 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender t))
1552 (defun notmuch-show-reply-sender (&optional prompt-for-sender)
1553 "Reply to the sender of the current message."
1555 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender nil))
1557 (defun notmuch-show-forward-message (&optional prompt-for-sender)
1558 "Forward the current message."
1560 (with-current-notmuch-show-message
1561 (notmuch-mua-new-forward-message prompt-for-sender)))
1563 (defun notmuch-show-next-message (&optional pop-at-end)
1564 "Show the next message.
1566 If a prefix argument is given and this is the last message in the
1567 thread, navigate to the next thread in the parent search buffer."
1569 (if (notmuch-show-goto-message-next)
1571 (notmuch-show-mark-read)
1572 (notmuch-show-message-adjust))
1574 (notmuch-show-next-thread)
1575 (goto-char (point-max)))))
1577 (defun notmuch-show-previous-message ()
1578 "Show the previous message."
1580 (notmuch-show-goto-message-previous)
1581 (notmuch-show-mark-read)
1582 (notmuch-show-message-adjust))
1584 (defun notmuch-show-next-open-message (&optional pop-at-end)
1585 "Show the next open message.
1587 If a prefix argument is given and this is the last open message
1588 in the thread, navigate to the next thread in the parent search
1589 buffer. Return t if there was a next open message in the thread
1590 to show, nil otherwise."
1593 (while (and (setq r (notmuch-show-goto-message-next))
1594 (not (notmuch-show-message-visible-p))))
1597 (notmuch-show-mark-read)
1598 (notmuch-show-message-adjust))
1600 (notmuch-show-next-thread)
1601 (goto-char (point-max))))
1604 (defun notmuch-show-previous-open-message ()
1605 "Show the previous open message."
1607 (while (and (notmuch-show-goto-message-previous)
1608 (not (notmuch-show-message-visible-p))))
1609 (notmuch-show-mark-read)
1610 (notmuch-show-message-adjust))
1612 (defun notmuch-show-view-raw-message ()
1613 "View the file holding the current message."
1615 (let* ((id (notmuch-show-get-message-id))
1616 (buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
1617 (call-process notmuch-command nil buf nil "show" "--format=raw" id)
1618 (switch-to-buffer buf)
1619 (goto-char (point-min))
1620 (set-buffer-modified-p nil)
1621 (view-buffer buf 'kill-buffer-if-not-modified)))
1623 (defun notmuch-show-pipe-message (entire-thread command)
1624 "Pipe the contents of the current message (or thread) to the given command.
1626 The given command will be executed with the raw contents of the
1627 current email message as stdin. Anything printed by the command
1628 to stdout or stderr will appear in the *notmuch-pipe* buffer.
1630 When invoked with a prefix argument, the command will receive all
1631 open messages in the current thread (formatted as an mbox) rather
1632 than only the current message."
1633 (interactive "P\nsPipe message to command: ")
1634 (let (shell-command)
1637 (concat notmuch-command " show --format=mbox "
1638 (shell-quote-argument
1639 (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
1642 (concat notmuch-command " show --format=raw "
1643 (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
1644 (let ((buf (get-buffer-create (concat "*notmuch-pipe*"))))
1645 (with-current-buffer buf
1646 (setq buffer-read-only nil)
1648 (let ((exit-code (call-process-shell-command shell-command nil buf)))
1649 (goto-char (point-max))
1650 (set-buffer-modified-p nil)
1651 (setq buffer-read-only t)
1652 (unless (zerop exit-code)
1653 (switch-to-buffer-other-window buf)
1654 (message (format "Command '%s' exited abnormally with code %d"
1655 shell-command exit-code))))))))
1657 (defun notmuch-show-tag-message (&rest tag-changes)
1658 "Change tags for the current message.
1660 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1661 (let* ((current-tags (notmuch-show-get-tags))
1662 (new-tags (notmuch-update-tags current-tags tag-changes)))
1663 (unless (equal current-tags new-tags)
1664 (apply 'notmuch-tag (notmuch-show-get-message-id) tag-changes)
1665 (notmuch-show-set-tags new-tags))))
1667 (defun notmuch-show-tag (&optional initial-input)
1668 "Change tags for the current message, read input from the minibuffer."
1670 (let ((tag-changes (notmuch-read-tag-changes
1671 initial-input (notmuch-show-get-message-id))))
1672 (apply 'notmuch-show-tag-message tag-changes)))
1674 (defun notmuch-show-tag-all (&rest tag-changes)
1675 "Change tags for all messages in the current buffer.
1677 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1678 (interactive (notmuch-read-tag-changes nil notmuch-show-thread-id))
1679 (apply 'notmuch-tag (notmuch-show-get-messages-ids-search) tag-changes)
1682 (let* ((current-tags (notmuch-show-get-tags))
1683 (new-tags (notmuch-update-tags current-tags tag-changes)))
1684 (unless (equal current-tags new-tags)
1685 (notmuch-show-set-tags new-tags))))))
1687 (defun notmuch-show-add-tag ()
1688 "Same as `notmuch-show-tag' but sets initial input to '+'."
1690 (notmuch-show-tag "+"))
1692 (defun notmuch-show-remove-tag ()
1693 "Same as `notmuch-show-tag' but sets initial input to '-'."
1695 (notmuch-show-tag "-"))
1697 (defun notmuch-show-toggle-headers ()
1698 "Toggle the visibility of the current message headers."
1700 (let ((props (notmuch-show-get-message-properties)))
1701 (notmuch-show-headers-visible
1703 (not (plist-get props :headers-visible))))
1704 (force-window-update))
1706 (defun notmuch-show-toggle-message ()
1707 "Toggle the visibility of the current message."
1709 (let ((props (notmuch-show-get-message-properties)))
1710 (notmuch-show-message-visible
1712 (not (plist-get props :message-visible))))
1713 (force-window-update))
1715 (defun notmuch-show-open-or-close-all ()
1716 "Set the visibility all of the messages in the current thread.
1717 By default make all of the messages visible. With a prefix
1718 argument, hide all of the messages."
1721 (goto-char (point-min))
1722 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1723 (not current-prefix-arg))
1724 until (not (notmuch-show-goto-message-next))))
1725 (force-window-update))
1727 (defun notmuch-show-next-button ()
1728 "Advance point to the next button in the buffer."
1732 (defun notmuch-show-previous-button ()
1733 "Move point back to the previous button in the buffer."
1735 (backward-button 1))
1737 (defun notmuch-show-next-thread (&optional show-next)
1738 "Move to the next item in the search results, if any."
1740 (let ((parent-buffer notmuch-show-parent-buffer))
1741 (notmuch-kill-this-buffer)
1742 (when (buffer-live-p parent-buffer)
1743 (switch-to-buffer parent-buffer)
1744 (notmuch-search-next-thread)
1746 (notmuch-search-show-thread)))))
1748 (defun notmuch-show-archive-thread (&optional unarchive)
1749 "Archive each message in thread.
1751 If a prefix argument is given, the messages will be
1752 \"unarchived\" (ie. the \"inbox\" tag will be added instead of
1755 Archive each message currently shown by removing the \"inbox\"
1756 tag from each. Then kill this buffer and show the next thread
1757 from the search from which this thread was originally shown.
1759 Note: This command is safe from any race condition of new messages
1760 being delivered to the same thread. It does not archive the
1761 entire thread, but only the messages shown in the current
1764 (let ((op (if unarchive "+" "-")))
1765 (notmuch-show-tag-all (concat op "inbox"))))
1767 (defun notmuch-show-archive-thread-then-next ()
1768 "Archive all messages in the current buffer, then show next thread from search."
1770 (notmuch-show-archive-thread)
1771 (notmuch-show-next-thread t))
1773 (defun notmuch-show-archive-thread-then-exit ()
1774 "Archive all messages in the current buffer, then exit back to search results."
1776 (notmuch-show-archive-thread)
1777 (notmuch-show-next-thread))
1779 (defun notmuch-show-archive-message (&optional unarchive)
1780 "Archive the current message.
1782 If a prefix argument is given, the message will be
1783 \"unarchived\" (ie. the \"inbox\" tag will be added instead of
1786 (let ((op (if unarchive "+" "-")))
1787 (notmuch-show-tag-message (concat op "inbox"))))
1789 (defun notmuch-show-archive-message-then-next-or-exit ()
1790 "Archive the current message, then show the next open message in the current thread.
1792 If at the last open message in the current thread, then exit back
1795 (notmuch-show-archive-message)
1796 (notmuch-show-next-open-message t))
1798 (defun notmuch-show-archive-message-then-next-or-next-thread ()
1799 "Archive the current message, then show the next open message in the current thread.
1801 If at the last open message in the current thread, then show next
1802 thread from search."
1804 (notmuch-show-archive-message)
1805 (unless (notmuch-show-next-open-message)
1806 (notmuch-show-next-thread t)))
1808 (defun notmuch-show-stash-cc ()
1809 "Copy CC field of current message to kill-ring."
1811 (notmuch-common-do-stash (notmuch-show-get-cc)))
1813 (defun notmuch-show-stash-date ()
1814 "Copy date of current message to kill-ring."
1816 (notmuch-common-do-stash (notmuch-show-get-date)))
1818 (defun notmuch-show-stash-filename ()
1819 "Copy filename of current message to kill-ring."
1821 (notmuch-common-do-stash (notmuch-show-get-filename)))
1823 (defun notmuch-show-stash-from ()
1824 "Copy From address of current message to kill-ring."
1826 (notmuch-common-do-stash (notmuch-show-get-from)))
1828 (defun notmuch-show-stash-message-id ()
1829 "Copy message ID of current message to kill-ring."
1831 (notmuch-common-do-stash (notmuch-show-get-message-id)))
1833 (defun notmuch-show-stash-message-id-stripped ()
1834 "Copy message ID of current message (sans `id:' prefix) to kill-ring."
1836 (notmuch-common-do-stash (notmuch-show-get-message-id t)))
1838 (defun notmuch-show-stash-subject ()
1839 "Copy Subject field of current message to kill-ring."
1841 (notmuch-common-do-stash (notmuch-show-get-subject)))
1843 (defun notmuch-show-stash-tags ()
1844 "Copy tags of current message to kill-ring as a comma separated list."
1846 (notmuch-common-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1848 (defun notmuch-show-stash-to ()
1849 "Copy To address of current message to kill-ring."
1851 (notmuch-common-do-stash (notmuch-show-get-to)))
1853 (defun notmuch-show-stash-mlarchive-link (&optional mla)
1854 "Copy an ML Archive URI for the current message to the kill-ring.
1856 This presumes that the message is available at the selected Mailing List Archive.
1858 If optional argument MLA is non-nil, use the provided key instead of prompting
1859 the user (see `notmuch-show-stash-mlarchive-link-alist')."
1861 (notmuch-common-do-stash
1864 (let ((completion-ignore-case t))
1866 "Mailing List Archive: "
1867 notmuch-show-stash-mlarchive-link-alist
1868 nil t nil nil notmuch-show-stash-mlarchive-link-default)))
1869 notmuch-show-stash-mlarchive-link-alist))
1870 (notmuch-show-get-message-id t))))
1872 (defun notmuch-show-stash-mlarchive-link-and-go (&optional mla)
1873 "Copy an ML Archive URI for the current message to the kill-ring and visit it.
1875 This presumes that the message is available at the selected Mailing List Archive.
1877 If optional argument MLA is non-nil, use the provided key instead of prompting
1878 the user (see `notmuch-show-stash-mlarchive-link-alist')."
1880 (notmuch-show-stash-mlarchive-link mla)
1881 (browse-url (current-kill 0 t)))
1883 ;; Commands typically bound to buttons.
1885 (defun notmuch-show-part-button-default (&optional button)
1887 (notmuch-show-part-button-internal button notmuch-show-part-button-default-action))
1889 (defun notmuch-show-part-button-save (&optional button)
1891 (notmuch-show-part-button-internal button #'notmuch-show-save-part))
1893 (defun notmuch-show-part-button-view (&optional button)
1895 (notmuch-show-part-button-internal button #'notmuch-show-view-part))
1897 (defun notmuch-show-part-button-interactively-view (&optional button)
1899 (notmuch-show-part-button-internal button #'notmuch-show-interactively-view-part))
1901 (defun notmuch-show-part-button-internal (button handler)
1902 (let ((button (or button (button-at (point)))))
1904 (let ((nth (button-get button :notmuch-part)))
1906 (funcall handler (notmuch-show-get-message-id) nth
1907 (button-get button :notmuch-filename)
1908 (button-get button :notmuch-content-type)))))))
1912 (provide 'notmuch-show)