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 (defmacro with-current-notmuch-show-message (&rest body)
159 "Evaluate body with current buffer set to the text of current message"
161 (let ((id (notmuch-show-get-message-id)))
162 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
163 (with-current-buffer buf
164 (call-process notmuch-command nil t nil "show" "--format=raw" id)
166 (kill-buffer buf)))))
168 (defun notmuch-show-turn-on-visual-line-mode ()
169 "Enable Visual Line mode."
170 (visual-line-mode t))
172 (defun notmuch-show-view-all-mime-parts ()
173 "Use external viewers to view all attachments from the current message."
175 (with-current-notmuch-show-message
176 ;; We override the mm-inline-media-tests to indicate which message
177 ;; parts are already sufficiently handled by the original
178 ;; presentation of the message in notmuch-show mode. These parts
179 ;; will be inserted directly into the temporary buffer of
180 ;; with-current-notmuch-show-message and silently discarded.
182 ;; Any MIME part not explicitly mentioned here will be handled by an
183 ;; external viewer as configured in the various mailcap files.
184 (let ((mm-inline-media-tests '(
185 ("text/.*" ignore identity)
186 ("application/pgp-signature" ignore identity)
187 ("multipart/alternative" ignore identity)
188 ("multipart/mixed" ignore identity)
189 ("multipart/related" ignore identity)
191 (mm-display-parts (mm-dissect-buffer)))))
193 (defun notmuch-foreach-mime-part (function mm-handle)
194 (cond ((stringp (car mm-handle))
195 (dolist (part (cdr mm-handle))
196 (notmuch-foreach-mime-part function part)))
197 ((bufferp (car mm-handle))
198 (funcall function mm-handle))
199 (t (dolist (part mm-handle)
200 (notmuch-foreach-mime-part function part)))))
202 (defun notmuch-count-attachments (mm-handle)
204 (notmuch-foreach-mime-part
206 (let ((disposition (mm-handle-disposition p)))
207 (and (listp disposition)
208 (or (equal (car disposition) "attachment")
209 (and (equal (car disposition) "inline")
210 (assq 'filename disposition)))
215 (defun notmuch-save-attachments (mm-handle &optional queryp)
216 (notmuch-foreach-mime-part
218 (let ((disposition (mm-handle-disposition p)))
219 (and (listp disposition)
220 (or (equal (car disposition) "attachment")
221 (and (equal (car disposition) "inline")
222 (assq 'filename disposition)))
225 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
229 (defun notmuch-show-save-attachments ()
230 "Save all attachments from the current message."
232 (with-current-notmuch-show-message
233 (let ((mm-handle (mm-dissect-buffer)))
234 (notmuch-save-attachments
235 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
238 (defun notmuch-show-with-message-as-text (fn)
239 "Apply FN to a text representation of the current message.
241 FN is called with one argument, the message properties. It should
242 operation on the contents of the current buffer."
244 ;; Remake the header to ensure that all information is available.
245 (let* ((to (notmuch-show-get-to))
246 (cc (notmuch-show-get-cc))
247 (from (notmuch-show-get-from))
248 (subject (notmuch-show-get-subject))
249 (date (notmuch-show-get-date))
250 (tags (notmuch-show-get-tags))
251 (depth (notmuch-show-get-depth))
254 "Subject: " subject "\n"
256 (if (not (string= cc ""))
257 (concat "Cc: " cc "\n")
263 (mapconcat #'identity tags ", ") "\n")
265 (all (buffer-substring (notmuch-show-message-top)
266 (notmuch-show-message-bottom)))
268 (props (notmuch-show-get-message-properties))
269 (indenting notmuch-show-indent-content))
273 (indent-rigidly (point-min) (point-max) (- depth)))
274 ;; Remove the original header.
275 (goto-char (point-min))
276 (re-search-forward "^$" (point-max) nil)
277 (delete-region (point-min) (point))
279 (funcall fn props))))
281 (defun notmuch-show-print-message ()
282 "Print the current message."
284 (notmuch-show-with-message-as-text 'notmuch-print-message))
286 (defun notmuch-show-fontify-header ()
288 ((looking-at "[Tt]o:")
290 ((looking-at "[Bb]?[Cc][Cc]:")
292 ((looking-at "[Ss]ubject:")
293 'message-header-subject)
294 ((looking-at "[Ff]rom:")
295 'message-header-from)
297 'message-header-other))))
299 (overlay-put (make-overlay (point) (re-search-forward ":"))
300 'face 'message-header-name)
301 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
304 (defun notmuch-show-colour-headers ()
305 "Apply some colouring to the current headers."
306 (goto-char (point-min))
307 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
308 (notmuch-show-fontify-header)
311 (defun notmuch-show-spaces-n (n)
312 "Return a string comprised of `n' spaces."
315 (defun notmuch-show-update-tags (tags)
316 "Update the displayed tags of the current message."
318 (goto-char (notmuch-show-message-top))
319 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
320 (let ((inhibit-read-only t))
321 (replace-match (concat "("
322 (propertize (mapconcat 'identity tags " ")
323 'face 'notmuch-tag-face)
326 (defun notmuch-show-clean-address (address)
327 "Try to clean a single email ADDRESS for display. Return
328 unchanged ADDRESS if parsing fails."
330 (let (p-name p-address)
331 ;; It would be convenient to use `mail-header-parse-address',
332 ;; but that expects un-decoded mailbox parts, whereas our
333 ;; mailbox parts are already decoded (and hence may contain
334 ;; UTF-8). Given that notmuch should handle most of the awkward
335 ;; cases, some simple string deconstruction should be sufficient
338 ;; "User <user@dom.ain>" style.
339 ((string-match "\\(.*\\) <\\(.*\\)>" address)
340 (setq p-name (match-string 1 address)
341 p-address (match-string 2 address)))
343 ;; "<user@dom.ain>" style.
344 ((string-match "<\\(.*\\)>" address)
345 (setq p-address (match-string 1 address)))
349 (setq p-address address)))
352 ;; Remove elements of the mailbox part that are not relevant for
353 ;; display, even if they are required during transport:
356 (setq p-name (replace-regexp-in-string "\\\\" "" p-name))
358 ;; Outer single and double quotes, which might be nested.
361 do (setq start-of-loop p-name)
363 when (string-match "^\"\\(.*\\)\"$" p-name)
364 do (setq p-name (match-string 1 p-name))
366 when (string-match "^'\\(.*\\)'$" p-name)
367 do (setq p-name (match-string 1 p-name))
369 until (string= start-of-loop p-name)))
371 ;; If the address is 'foo@bar.com <foo@bar.com>' then show just
373 (when (string= p-name p-address)
376 ;; If no name results, return just the address.
379 ;; Otherwise format the name and address together.
380 (concat p-name " <" p-address ">")))
383 (defun notmuch-show-insert-headerline (headers date tags depth)
384 "Insert a notmuch style headerline based on HEADERS for a
385 message at DEPTH in the current thread."
386 (let ((start (point)))
387 (insert (notmuch-show-spaces-n (* notmuch-show-indent-messages-width depth))
388 (notmuch-show-clean-address (plist-get headers :From))
392 (propertize (mapconcat 'identity tags " ")
393 'face 'notmuch-tag-face)
395 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
397 (defun notmuch-show-insert-header (header header-value)
398 "Insert a single header."
399 (insert header ": " header-value "\n"))
401 (defun notmuch-show-insert-headers (headers)
402 "Insert the headers of the current message."
403 (let ((start (point)))
404 (mapc (lambda (header)
405 (let* ((header-symbol (intern (concat ":" header)))
406 (header-value (plist-get headers header-symbol)))
407 (if (and header-value
408 (not (string-equal "" header-value)))
409 (notmuch-show-insert-header header header-value))))
410 notmuch-message-headers)
413 (narrow-to-region start (point-max))
414 (run-hooks 'notmuch-show-markup-headers-hook)))))
416 (define-button-type 'notmuch-show-part-button-type
417 'action 'notmuch-show-part-button-default
418 'keymap 'notmuch-show-part-button-map
422 (defvar notmuch-show-part-button-map
423 (let ((map (make-sparse-keymap)))
424 (set-keymap-parent map button-map)
425 (define-key map "s" 'notmuch-show-part-button-save)
426 (define-key map "v" 'notmuch-show-part-button-view)
427 (define-key map "o" 'notmuch-show-part-button-interactively-view)
429 "Submap for button commands")
430 (fset 'notmuch-show-part-button-map notmuch-show-part-button-map)
432 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name comment)
437 (if name (concat name ": ") "")
439 (if (not (string-equal declared-type content-type))
440 (concat " (as " content-type ")")
444 :type 'notmuch-show-part-button-type
446 :notmuch-filename name
447 :notmuch-content-type content-type))
452 ;; Functions handling particular MIME parts.
454 (defmacro notmuch-with-temp-part-buffer (message-id nth &rest body)
456 (let ((process-crypto (make-symbol "process-crypto")))
457 `(let ((,process-crypto notmuch-show-process-crypto))
459 (setq notmuch-show-process-crypto ,process-crypto)
460 ;; Always acquires the part via `notmuch part', even if it is
461 ;; available in the JSON output.
462 (insert (notmuch-show-get-bodypart-internal ,message-id ,nth))
465 (defun notmuch-show-save-part (message-id nth &optional filename content-type)
466 (notmuch-with-temp-part-buffer message-id nth
467 (let ((file (read-file-name
468 "Filename to save as: "
469 (or mailcap-download-directory "~/")
472 ;; Don't re-compress .gz & al. Arguably we should make
473 ;; `file-name-handler-alist' nil, but that would chop
474 ;; ange-ftp, which is reasonable to use here.
475 (mm-write-region (point-min) (point-max) file nil nil nil 'no-conversion t))))
477 (defun notmuch-show-view-part (message-id nth &optional filename content-type )
478 (notmuch-with-temp-part-buffer message-id nth
479 ;; set mm-inlined-types to nil to force an external viewer
480 (let ((handle (mm-make-handle (current-buffer) (list content-type)))
481 (mm-inlined-types nil))
482 ;; We override mm-save-part as notmuch-show-save-part is better
483 ;; since it offers the filename. We need to lexically bind
484 ;; everything we need for notmuch-show-save-part to prevent
485 ;; potential dynamic shadowing.
486 (lexical-let ((message-id message-id)
489 (content-type content-type))
490 (flet ((mm-save-part (&rest args) (notmuch-show-save-part
491 message-id nth filename content-type)))
492 (mm-display-part handle))))))
494 (defun notmuch-show-interactively-view-part (message-id nth &optional filename content-type)
495 (notmuch-with-temp-part-buffer message-id nth
496 (let ((handle (mm-make-handle (current-buffer) (list content-type))))
497 (mm-interactively-view-part handle))))
499 (defun notmuch-show-mm-display-part-inline (msg part nth content-type)
500 "Use the mm-decode/mm-view functions to display a part in the
501 current buffer, if possible."
502 (let ((display-buffer (current-buffer)))
504 (let* ((charset (plist-get part :content-charset))
505 (handle (mm-make-handle (current-buffer) `(,content-type (charset . ,charset)))))
506 ;; If the user wants the part inlined, insert the content and
507 ;; test whether we are able to inline it (which includes both
508 ;; capability and suitability tests).
509 (when (mm-inlined-p handle)
510 (insert (notmuch-show-get-bodypart-content msg part nth))
511 (when (mm-inlinable-p handle)
512 (set-buffer display-buffer)
513 (mm-display-part handle)
516 (defvar notmuch-show-multipart/alternative-discouraged
520 ;; multipart/related usually contain a text/html part and some associated graphics.
524 (defun notmuch-show-multipart/*-to-list (part)
525 (mapcar (lambda (inner-part) (plist-get inner-part :content-type))
526 (plist-get part :content)))
528 (defun notmuch-show-multipart/alternative-choose (types)
529 ;; Based on `mm-preferred-alternative-precedence'.
531 (dolist (pref (reverse notmuch-show-multipart/alternative-discouraged))
532 (dolist (elem (copy-sequence seq))
533 (when (string-match pref elem)
534 (setq seq (nconc (delete elem seq) (list elem))))))
537 (defun notmuch-show-insert-part-multipart/alternative (msg part content-type nth depth declared-type)
538 (notmuch-show-insert-part-header nth declared-type content-type nil)
539 (let ((chosen-type (car (notmuch-show-multipart/alternative-choose (notmuch-show-multipart/*-to-list part))))
540 (inner-parts (plist-get part :content))
542 ;; This inserts all parts of the chosen type rather than just one,
543 ;; but it's not clear that this is the wrong thing to do - which
544 ;; should be chosen if there are more than one that match?
545 (mapc (lambda (inner-part)
546 (let ((inner-type (plist-get inner-part :content-type)))
547 (if (or notmuch-show-all-multipart/alternative-parts
548 (string= chosen-type inner-type))
549 (notmuch-show-insert-bodypart msg inner-part depth)
550 (notmuch-show-insert-part-header (plist-get inner-part :id) inner-type inner-type nil " (not shown)"))))
553 (when notmuch-show-indent-multipart
554 (indent-rigidly start (point) 1)))
557 (defun notmuch-show-setup-w3m ()
558 "Instruct w3m how to retrieve content from a \"related\" part of a message."
560 (if (boundp 'w3m-cid-retrieve-function-alist)
561 (unless (assq 'notmuch-show-mode w3m-cid-retrieve-function-alist)
562 (push (cons 'notmuch-show-mode 'notmuch-show-w3m-cid-retrieve)
563 w3m-cid-retrieve-function-alist)))
564 (setq mm-inline-text-html-with-images t))
566 (defvar w3m-current-buffer) ;; From `w3m.el'.
567 (defvar notmuch-show-w3m-cid-store nil)
568 (make-variable-buffer-local 'notmuch-show-w3m-cid-store)
570 (defun notmuch-show-w3m-cid-store-internal (content-id
575 (push (list content-id
580 notmuch-show-w3m-cid-store))
582 (defun notmuch-show-w3m-cid-store (msg part)
583 (let ((content-id (plist-get part :content-id)))
585 (notmuch-show-w3m-cid-store-internal (concat "cid:" content-id)
588 (plist-get part :content-type)
591 (defun notmuch-show-w3m-cid-retrieve (url &rest args)
592 (let ((matching-part (with-current-buffer w3m-current-buffer
593 (assoc url notmuch-show-w3m-cid-store))))
595 (let ((message-id (nth 1 matching-part))
596 (part-number (nth 2 matching-part))
597 (content-type (nth 3 matching-part))
598 (content (nth 4 matching-part)))
599 ;; If we don't already have the content, get it and cache
600 ;; it, as some messages reference the same cid: part many
601 ;; times (hundreds!), which results in many calls to
604 (setq content (notmuch-show-get-bodypart-internal (concat "id:" message-id)
606 (with-current-buffer w3m-current-buffer
607 (notmuch-show-w3m-cid-store-internal url
616 (defun notmuch-show-insert-part-multipart/related (msg part content-type nth depth declared-type)
617 (notmuch-show-insert-part-header nth declared-type content-type nil)
618 (let ((inner-parts (plist-get part :content))
621 ;; We assume that the first part is text/html and the remainder
622 ;; things that it references.
624 ;; Stash the non-primary parts.
626 (notmuch-show-w3m-cid-store msg part))
629 ;; Render the primary part.
630 (notmuch-show-insert-bodypart msg (car inner-parts) depth)
632 (when notmuch-show-indent-multipart
633 (indent-rigidly start (point) 1)))
636 (defun notmuch-show-insert-part-multipart/signed (msg part content-type nth depth declared-type)
637 (let ((button (notmuch-show-insert-part-header nth declared-type content-type nil)))
638 (button-put button 'face 'notmuch-crypto-part-header)
639 ;; add signature status button if sigstatus provided
640 (if (plist-member part :sigstatus)
641 (let* ((from (notmuch-show-get-header :From msg))
642 (sigstatus (car (plist-get part :sigstatus))))
643 (notmuch-crypto-insert-sigstatus-button sigstatus from))
644 ;; if we're not adding sigstatus, tell the user how they can get it
645 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts.")))
647 (let ((inner-parts (plist-get part :content))
649 ;; Show all of the parts.
650 (mapc (lambda (inner-part)
651 (notmuch-show-insert-bodypart msg inner-part depth))
654 (when notmuch-show-indent-multipart
655 (indent-rigidly start (point) 1)))
658 (defun notmuch-show-insert-part-multipart/encrypted (msg part content-type nth depth declared-type)
659 (let ((button (notmuch-show-insert-part-header nth declared-type content-type nil)))
660 (button-put button 'face 'notmuch-crypto-part-header)
661 ;; add encryption status button if encstatus specified
662 (if (plist-member part :encstatus)
663 (let ((encstatus (car (plist-get part :encstatus))))
664 (notmuch-crypto-insert-encstatus-button encstatus)
665 ;; add signature status button if sigstatus specified
666 (if (plist-member part :sigstatus)
667 (let* ((from (notmuch-show-get-header :From msg))
668 (sigstatus (car (plist-get part :sigstatus))))
669 (notmuch-crypto-insert-sigstatus-button sigstatus from))))
670 ;; if we're not adding encstatus, tell the user how they can get it
671 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts.")))
673 (let ((inner-parts (plist-get part :content))
675 ;; Show all of the parts.
676 (mapc (lambda (inner-part)
677 (notmuch-show-insert-bodypart msg inner-part depth))
680 (when notmuch-show-indent-multipart
681 (indent-rigidly start (point) 1)))
684 (defun notmuch-show-insert-part-multipart/* (msg part content-type nth depth declared-type)
685 (notmuch-show-insert-part-header nth declared-type content-type nil)
686 (let ((inner-parts (plist-get part :content))
688 ;; Show all of the parts.
689 (mapc (lambda (inner-part)
690 (notmuch-show-insert-bodypart msg inner-part depth))
693 (when notmuch-show-indent-multipart
694 (indent-rigidly start (point) 1)))
697 (defun notmuch-show-insert-part-message/rfc822 (msg part content-type nth depth declared-type)
698 (notmuch-show-insert-part-header nth declared-type content-type nil)
699 (let* ((message (car (plist-get part :content)))
700 (body (car (plist-get message :body)))
703 ;; Override `notmuch-message-headers' to force `From' to be
705 (let ((notmuch-message-headers '("From" "Subject" "To" "Cc" "Date")))
706 (notmuch-show-insert-headers (plist-get message :headers)))
708 ;; Blank line after headers to be compatible with the normal
713 (notmuch-show-insert-bodypart msg body depth)
715 (when notmuch-show-indent-multipart
716 (indent-rigidly start (point) 1)))
719 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
720 (let ((start (point)))
721 ;; If this text/plain part is not the first part in the message,
722 ;; insert a header to make this clear.
724 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
725 (insert (notmuch-show-get-bodypart-content msg part nth))
728 (narrow-to-region start (point-max))
729 (run-hook-with-args 'notmuch-show-insert-text/plain-hook msg depth))))
732 (defun notmuch-show-insert-part-text/x-vcalendar (msg part content-type nth depth declared-type)
733 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename))
734 (insert (with-temp-buffer
735 (insert (notmuch-show-get-bodypart-content msg part nth))
736 (goto-char (point-min))
737 (let ((file (make-temp-file "notmuch-ical"))
739 (icalendar--convert-ical-to-diary
740 (icalendar--read-element nil nil)
742 (set-buffer (get-file-buffer file))
743 (setq result (buffer-substring (point-min) (point-max)))
744 (set-buffer-modified-p nil)
745 (kill-buffer (current-buffer))
750 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
751 ;; If we can deduce a MIME type from the filename of the attachment,
752 ;; do so and pass it on to the handler for that type.
753 (if (plist-get part :filename)
754 (let ((extension (file-name-extension (plist-get part :filename)))
758 (mailcap-parse-mimetypes)
759 (setq mime-type (mailcap-extension-to-mime extension))
761 (not (string-equal mime-type "application/octet-stream")))
762 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
766 ;; Handler for wash generated inline patch fake parts.
767 (defun notmuch-show-insert-part-inline-patch-fake-part (msg part content-type nth depth declared-type)
768 (notmuch-show-insert-part-*/* msg part "text/x-diff" nth depth "inline patch"))
770 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
771 ;; This handler _must_ succeed - it is the handler of last resort.
772 (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
773 (notmuch-show-mm-display-part-inline msg part nth content-type)
776 ;; Functions for determining how to handle MIME parts.
778 (defun notmuch-show-split-content-type (content-type)
779 (split-string content-type "/"))
781 (defun notmuch-show-handlers-for (content-type)
782 "Return a list of content handlers for a part of type CONTENT-TYPE."
787 ;; Reverse order of prefrence.
788 (list (intern (concat "notmuch-show-insert-part-*/*"))
790 "notmuch-show-insert-part-"
791 (car (notmuch-show-split-content-type content-type))
793 (intern (concat "notmuch-show-insert-part-" content-type))))
796 ;; Helper for parts which are generally not included in the default
798 (defun notmuch-show-get-bodypart-internal (message-id part-number)
799 (let ((args '("show" "--format=raw"))
800 (part-arg (format "--part=%s" part-number)))
801 (setq args (append args (list part-arg)))
802 (if notmuch-show-process-crypto
803 (setq args (append args '("--decrypt"))))
804 (setq args (append args (list message-id)))
806 (let ((coding-system-for-read 'no-conversion))
808 (apply 'call-process (append (list notmuch-command nil (list t nil) nil) args))
811 (defun notmuch-show-get-bodypart-content (msg part nth)
812 (or (plist-get part :content)
813 (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
817 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
818 (let ((handlers (notmuch-show-handlers-for content-type)))
819 ;; Run the content handlers until one of them returns a non-nil
822 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
823 (setq handlers (cdr handlers))))
826 (defun notmuch-show-insert-bodypart (msg part depth)
827 "Insert the body part PART at depth DEPTH in the current thread."
828 (let ((content-type (downcase (plist-get part :content-type)))
829 (nth (plist-get part :id)))
830 (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
831 ;; Some of the body part handlers leave point somewhere up in the
832 ;; part, so we make sure that we're down at the end.
833 (goto-char (point-max))
834 ;; Ensure that the part ends with a carriage return.
838 (defun notmuch-show-insert-body (msg body depth)
839 "Insert the body BODY at depth DEPTH in the current thread."
840 (mapc (lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
842 (defun notmuch-show-make-symbol (type)
843 (make-symbol (concat "notmuch-show-" type)))
845 (defun notmuch-show-strip-re (string)
846 (replace-regexp-in-string "\\([Rr]e: *\\)+" "" string))
848 (defvar notmuch-show-previous-subject "")
849 (make-variable-buffer-local 'notmuch-show-previous-subject)
851 (defun notmuch-show-insert-msg (msg depth)
852 "Insert the message MSG at depth DEPTH in the current thread."
853 (let* ((headers (plist-get msg :headers))
854 ;; Indentation causes the buffer offset of the start/end
855 ;; points to move, so we must use markers.
856 message-start message-end
857 content-start content-end
858 headers-start headers-end
860 (headers-invis-spec (notmuch-show-make-symbol "header"))
861 (message-invis-spec (notmuch-show-make-symbol "message"))
862 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
864 ;; Set `buffer-invisibility-spec' to `nil' (a list), otherwise
865 ;; removing items from `buffer-invisibility-spec' (which is what
866 ;; `notmuch-show-headers-visible' and
867 ;; `notmuch-show-message-visible' do) is a no-op and has no
868 ;; effect. This caused threads with only matching messages to have
869 ;; those messages hidden initially because
870 ;; `buffer-invisibility-spec' stayed `t'.
872 ;; This needs to be set here (rather than just above the call to
873 ;; `notmuch-show-headers-visible') because some of the part
874 ;; rendering or body washing functions
875 ;; (e.g. `notmuch-wash-text/plain-citations') manipulate
876 ;; `buffer-invisibility-spec').
877 (when (eq buffer-invisibility-spec t)
878 (setq buffer-invisibility-spec nil))
880 (setq message-start (point-marker))
882 (notmuch-show-insert-headerline headers
883 (or (if notmuch-show-relative-dates
884 (plist-get msg :date_relative)
886 (plist-get headers :Date))
887 (plist-get msg :tags) depth)
889 (setq content-start (point-marker))
891 (plist-put msg :headers-invis-spec headers-invis-spec)
892 (plist-put msg :message-invis-spec message-invis-spec)
894 ;; Set `headers-start' to point after the 'Subject:' header to be
895 ;; compatible with the existing implementation. This just sets it
896 ;; to after the first header.
897 (notmuch-show-insert-headers headers)
899 (goto-char content-start)
900 ;; If the subject of this message is the same as that of the
901 ;; previous message, don't display it when this message is
903 (when (not (string= notmuch-show-previous-subject
906 (setq headers-start (point-marker)))
907 (setq headers-end (point-marker))
909 (setq notmuch-show-previous-subject bare-subject)
911 (setq body-start (point-marker))
912 ;; A blank line between the headers and the body.
914 (notmuch-show-insert-body msg (plist-get msg :body)
915 (if notmuch-show-indent-content depth 0))
916 ;; Ensure that the body ends with a newline.
919 (setq body-end (point-marker))
920 (setq content-end (point-marker))
922 ;; Indent according to the depth in the thread.
923 (if notmuch-show-indent-content
924 (indent-rigidly content-start content-end (* notmuch-show-indent-messages-width depth)))
926 (setq message-end (point-max-marker))
928 ;; Save the extents of this message over the whole text of the
930 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
932 (let ((headers-overlay (make-overlay headers-start headers-end))
933 (invis-specs (list headers-invis-spec message-invis-spec)))
934 (overlay-put headers-overlay 'invisible invis-specs)
935 (overlay-put headers-overlay 'priority 10))
936 (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
938 (plist-put msg :depth depth)
940 ;; Save the properties for this message. Currently this saves the
941 ;; entire message (augmented it with other stuff), which seems
942 ;; like overkill. We might save a reduced subset (for example, not
944 (notmuch-show-set-message-properties msg)
946 ;; Set header visibility.
947 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
949 ;; Message visibility depends on whether it matched the search
951 (notmuch-show-message-visible msg (plist-get msg :match))))
953 (defun notmuch-show-toggle-process-crypto ()
954 "Toggle the processing of cryptographic MIME parts."
956 (setq notmuch-show-process-crypto (not notmuch-show-process-crypto))
957 (message (if notmuch-show-process-crypto
958 "Processing cryptographic MIME parts."
959 "Not processing cryptographic MIME parts."))
960 (notmuch-show-refresh-view t))
962 (defun notmuch-show-toggle-elide-non-matching ()
963 "Toggle the display of non-matching messages."
965 (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages))
966 (message (if notmuch-show-elide-non-matching-messages
967 "Showing matching messages only."
968 "Showing all messages."))
969 (notmuch-show-refresh-view t))
971 (defun notmuch-show-toggle-thread-indentation ()
972 "Toggle the indentation of threads."
974 (setq notmuch-show-indent-content (not notmuch-show-indent-content))
975 (message (if notmuch-show-indent-content
976 "Content is indented."
977 "Content is not indented."))
978 (notmuch-show-refresh-view t))
980 (defun notmuch-show-insert-tree (tree depth)
981 "Insert the message tree TREE at depth DEPTH in the current thread."
982 (let ((msg (car tree))
983 (replies (cadr tree)))
984 (if (or (not notmuch-show-elide-non-matching-messages)
985 (plist-get msg :match))
986 (notmuch-show-insert-msg msg depth))
987 (notmuch-show-insert-thread replies (1+ depth))))
989 (defun notmuch-show-insert-thread (thread depth)
990 "Insert the thread THREAD at depth DEPTH in the current forest."
991 (mapc (lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
993 (defun notmuch-show-insert-forest (forest)
994 "Insert the forest of threads FOREST."
995 (mapc (lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
997 (defun notmuch-show-buttonise-links (start end)
998 "Buttonise URLs and mail addresses between START and END.
1000 This also turns id:\"<message id>\"-parts into buttons for
1001 a corresponding notmuch search."
1002 (goto-address-fontify-region start end)
1005 (while (re-search-forward "id:\\(\"?\\)[^[:space:]\"]+\\1" end t)
1006 ;; remove the overlay created by goto-address-mode
1007 (remove-overlays (match-beginning 0) (match-end 0) 'goto-address t)
1008 (make-text-button (match-beginning 0) (match-end 0)
1009 'action `(lambda (arg)
1010 (notmuch-show ,(match-string-no-properties 0)))
1012 'help-echo "Mouse-1, RET: search for this message"
1013 'face goto-address-mail-face))))
1016 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
1017 "Run \"notmuch show\" with the given thread ID and display results.
1019 The optional PARENT-BUFFER is the notmuch-search buffer from
1020 which this notmuch-show command was executed, (so that the
1021 next thread from that buffer can be show when done with this
1024 The optional QUERY-CONTEXT is a notmuch search term. Only
1025 messages from the thread matching this search term are shown if
1028 The optional BUFFER-NAME provides the name of the buffer in
1029 which the message thread is shown. If it is nil (which occurs
1030 when the command is called interactively) the argument to the
1032 (interactive "sNotmuch show: ")
1033 (let ((buffer-name (generate-new-buffer-name
1035 (concat "*notmuch-" thread-id "*")))))
1036 (switch-to-buffer (get-buffer-create buffer-name))
1037 ;; Set the default value for `notmuch-show-process-crypto' in this
1039 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1040 ;; Set the default value for
1041 ;; `notmuch-show-elide-non-matching-messages' in this buffer. If
1042 ;; there is a prefix argument, invert the default.
1043 (setq notmuch-show-elide-non-matching-messages notmuch-show-only-matching-messages)
1044 (if current-prefix-arg
1045 (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages)))
1047 (setq notmuch-show-thread-id thread-id
1048 notmuch-show-parent-buffer parent-buffer
1049 notmuch-show-query-context query-context)
1050 (notmuch-show-worker)))
1052 (defun notmuch-show-worker ()
1053 (let ((inhibit-read-only t))
1056 ;; Don't track undo information for this buffer
1057 (set 'buffer-undo-list t)
1060 (goto-char (point-min))
1062 (let* ((basic-args (list notmuch-show-thread-id))
1063 (args (if notmuch-show-query-context
1064 (append (list "\'") basic-args
1065 (list "and (" notmuch-show-query-context ")\'"))
1066 (append (list "\'") basic-args (list "\'")))))
1067 (notmuch-show-insert-forest (notmuch-query-get-threads args))
1068 ;; If the query context reduced the results to nothing, run
1070 (when (and (eq (buffer-size) 0)
1071 notmuch-show-query-context)
1072 (notmuch-show-insert-forest
1073 (notmuch-query-get-threads basic-args))))
1075 (jit-lock-register #'notmuch-show-buttonise-links)
1077 (run-hooks 'notmuch-show-hook))
1079 ;; Move straight to the first open message
1080 (unless (notmuch-show-message-visible-p)
1081 (notmuch-show-next-open-message))
1083 ;; Set the header line to the subject of the first open message.
1084 (setq header-line-format (notmuch-show-strip-re (notmuch-show-get-pretty-subject)))
1086 (notmuch-show-mark-read)))
1088 (defun notmuch-show-capture-state ()
1089 "Capture the state of the current buffer.
1092 - the list of open messages,
1093 - the current message."
1094 (list (notmuch-show-get-message-id) (notmuch-show-get-message-ids-for-open-messages)))
1096 (defun notmuch-show-apply-state (state)
1097 "Apply STATE to the current buffer.
1100 - opening the messages previously opened,
1101 - closing all other messages,
1102 - moving to the correct current message."
1103 (let ((current (car state))
1104 (open (cadr state)))
1106 ;; Open those that were open.
1107 (goto-char (point-min))
1108 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1109 (member (notmuch-show-get-message-id) open))
1110 until (not (notmuch-show-goto-message-next)))
1112 ;; Go to the previously open message.
1113 (goto-char (point-min))
1114 (unless (loop if (string= current (notmuch-show-get-message-id))
1116 until (not (notmuch-show-goto-message-next)))
1117 (goto-char (point-min))
1118 (message "Previously current message not found."))
1119 (notmuch-show-message-adjust)))
1121 (defun notmuch-show-refresh-view (&optional retain-state)
1122 "Refresh the current view.
1124 Refreshes the current view, observing changes in display
1125 preferences. If RETAIN-STATE is non-nil then the state of the
1126 buffer is stored and re-applied after the refresh."
1128 (let ((inhibit-read-only t)
1131 (setq state (notmuch-show-capture-state)))
1133 (notmuch-show-worker)
1135 (notmuch-show-apply-state state))))
1137 (defvar notmuch-show-stash-map
1138 (let ((map (make-sparse-keymap)))
1139 (define-key map "c" 'notmuch-show-stash-cc)
1140 (define-key map "d" 'notmuch-show-stash-date)
1141 (define-key map "F" 'notmuch-show-stash-filename)
1142 (define-key map "f" 'notmuch-show-stash-from)
1143 (define-key map "i" 'notmuch-show-stash-message-id)
1144 (define-key map "I" 'notmuch-show-stash-message-id-stripped)
1145 (define-key map "s" 'notmuch-show-stash-subject)
1146 (define-key map "T" 'notmuch-show-stash-tags)
1147 (define-key map "t" 'notmuch-show-stash-to)
1149 "Submap for stash commands")
1150 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
1152 (defvar notmuch-show-mode-map
1153 (let ((map (make-sparse-keymap)))
1154 (define-key map "?" 'notmuch-help)
1155 (define-key map "q" 'notmuch-kill-this-buffer)
1156 (define-key map (kbd "<C-tab>") 'widget-backward)
1157 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
1158 (define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
1159 (define-key map (kbd "TAB") 'notmuch-show-next-button)
1160 (define-key map "s" 'notmuch-search)
1161 (define-key map "m" 'notmuch-mua-new-mail)
1162 (define-key map "f" 'notmuch-show-forward-message)
1163 (define-key map "r" 'notmuch-show-reply-sender)
1164 (define-key map "R" 'notmuch-show-reply)
1165 (define-key map "|" 'notmuch-show-pipe-message)
1166 (define-key map "w" 'notmuch-show-save-attachments)
1167 (define-key map "V" 'notmuch-show-view-raw-message)
1168 (define-key map "v" 'notmuch-show-view-all-mime-parts)
1169 (define-key map "c" 'notmuch-show-stash-map)
1170 (define-key map "=" 'notmuch-show-refresh-view)
1171 (define-key map "h" 'notmuch-show-toggle-headers)
1172 (define-key map "*" 'notmuch-show-tag-all)
1173 (define-key map "-" 'notmuch-show-remove-tag)
1174 (define-key map "+" 'notmuch-show-add-tag)
1175 (define-key map "X" 'notmuch-show-archive-thread-then-exit)
1176 (define-key map "x" 'notmuch-show-archive-message-then-next-or-exit)
1177 (define-key map "A" 'notmuch-show-archive-thread-then-next)
1178 (define-key map "a" 'notmuch-show-archive-message-then-next-or-next-thread)
1179 (define-key map "N" 'notmuch-show-next-message)
1180 (define-key map "P" 'notmuch-show-previous-message)
1181 (define-key map "n" 'notmuch-show-next-open-message)
1182 (define-key map "p" 'notmuch-show-previous-open-message)
1183 (define-key map (kbd "DEL") 'notmuch-show-rewind)
1184 (define-key map " " 'notmuch-show-advance-and-archive)
1185 (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
1186 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
1187 (define-key map "#" 'notmuch-show-print-message)
1188 (define-key map "!" 'notmuch-show-toggle-elide-non-matching)
1189 (define-key map "$" 'notmuch-show-toggle-process-crypto)
1190 (define-key map "<" 'notmuch-show-toggle-thread-indentation)
1191 (define-key map "t" 'toggle-truncate-lines)
1193 "Keymap for \"notmuch show\" buffers.")
1194 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
1196 (defun notmuch-show-mode ()
1197 "Major mode for viewing a thread with notmuch.
1199 This buffer contains the results of the \"notmuch show\" command
1200 for displaying a single thread of email from your email archives.
1202 By default, various components of email messages, (citations,
1203 signatures, already-read messages), are hidden. You can make
1204 these parts visible by clicking with the mouse button or by
1205 pressing RET after positioning the cursor on a hidden part, (for
1206 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
1208 Reading the thread sequentially is well-supported by pressing
1209 \\[notmuch-show-advance-and-archive]. This will scroll the current message (if necessary), advance
1210 to the next message, or advance to the next thread (if already on
1211 the last message of a thread).
1213 Other commands are available to read or manipulate the thread
1214 more selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages
1215 without removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread
1216 without scrolling through with \\[notmuch-show-advance-and-archive]).
1218 You can add or remove arbitrary tags from the current message with
1219 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
1221 All currently available key bindings:
1223 \\{notmuch-show-mode-map}"
1225 (kill-all-local-variables)
1226 (use-local-map notmuch-show-mode-map)
1227 (setq major-mode 'notmuch-show-mode
1228 mode-name "notmuch-show")
1229 (setq buffer-read-only t
1232 (defun notmuch-show-move-to-message-top ()
1233 (goto-char (notmuch-show-message-top)))
1235 (defun notmuch-show-move-to-message-bottom ()
1236 (goto-char (notmuch-show-message-bottom)))
1238 (defun notmuch-show-message-adjust ()
1241 ;; Movement related functions.
1243 ;; There's some strangeness here where a text property applied to a
1244 ;; region a->b is not found when point is at b. We walk backwards
1245 ;; until finding the property.
1246 (defun notmuch-show-message-extent ()
1249 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
1253 (defun notmuch-show-message-top ()
1254 (car (notmuch-show-message-extent)))
1256 (defun notmuch-show-message-bottom ()
1257 (cdr (notmuch-show-message-extent)))
1259 (defun notmuch-show-goto-message-next ()
1260 (let ((start (point)))
1261 (notmuch-show-move-to-message-bottom)
1267 (defun notmuch-show-goto-message-previous ()
1268 (notmuch-show-move-to-message-top)
1272 (notmuch-show-move-to-message-top)
1275 (defun notmuch-show-mapc (function)
1276 "Iterate through all messages in the current thread with
1277 `notmuch-show-goto-message-next' and call FUNCTION for side
1280 (goto-char (point-min))
1281 (loop do (funcall function)
1282 while (notmuch-show-goto-message-next))))
1284 ;; Functions relating to the visibility of messages and their
1287 (defun notmuch-show-element-visible (props visible-p spec-property)
1288 (let ((spec (plist-get props spec-property)))
1290 (remove-from-invisibility-spec spec)
1291 (add-to-invisibility-spec spec))))
1293 (defun notmuch-show-message-visible (props visible-p)
1294 (notmuch-show-element-visible props visible-p :message-invis-spec)
1295 (notmuch-show-set-prop :message-visible visible-p props))
1297 (defun notmuch-show-headers-visible (props visible-p)
1298 (notmuch-show-element-visible props visible-p :headers-invis-spec)
1299 (notmuch-show-set-prop :headers-visible visible-p props))
1301 ;; Functions for setting and getting attributes of the current
1304 (defun notmuch-show-set-message-properties (props)
1306 (notmuch-show-move-to-message-top)
1307 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
1309 (defun notmuch-show-get-message-properties ()
1310 "Return the properties of the current message as a plist.
1312 Some useful entries are:
1313 :headers - Property list containing the headers :Date, :Subject, :From, etc.
1314 :body - Body of the message
1315 :tags - Tags for this message"
1317 (notmuch-show-move-to-message-top)
1318 (get-text-property (point) :notmuch-message-properties)))
1320 (defun notmuch-show-set-prop (prop val &optional props)
1321 (let ((inhibit-read-only t)
1323 (notmuch-show-get-message-properties))))
1324 (plist-put props prop val)
1325 (notmuch-show-set-message-properties props)))
1327 (defun notmuch-show-get-prop (prop &optional props)
1328 (let ((props (or props
1329 (notmuch-show-get-message-properties))))
1330 (plist-get props prop)))
1332 (defun notmuch-show-get-message-id ()
1333 "Return the message id of the current message."
1334 (concat "id:\"" (notmuch-show-get-prop :id) "\""))
1336 (defun notmuch-show-get-messages-ids ()
1337 "Return all message ids of messages in the current thread."
1338 (let ((message-ids))
1340 (lambda () (push (notmuch-show-get-message-id) message-ids)))
1343 (defun notmuch-show-get-messages-ids-search ()
1344 "Return a search string for all message ids of messages in the
1346 (mapconcat 'identity (notmuch-show-get-messages-ids) " or "))
1348 ;; dme: Would it make sense to use a macro for many of these?
1350 (defun notmuch-show-get-filename ()
1351 "Return the filename of the current message."
1352 (notmuch-show-get-prop :filename))
1354 (defun notmuch-show-get-header (header &optional props)
1355 "Return the named header of the current message, if any."
1356 (plist-get (notmuch-show-get-prop :headers props) header))
1358 (defun notmuch-show-get-cc ()
1359 (notmuch-show-get-header :Cc))
1361 (defun notmuch-show-get-date ()
1362 (notmuch-show-get-header :Date))
1364 (defun notmuch-show-get-from ()
1365 (notmuch-show-get-header :From))
1367 (defun notmuch-show-get-subject ()
1368 (notmuch-show-get-header :Subject))
1370 (defun notmuch-show-get-to ()
1371 (notmuch-show-get-header :To))
1373 (defun notmuch-show-get-depth ()
1374 (notmuch-show-get-prop :depth))
1376 (defun notmuch-show-get-pretty-subject ()
1377 (notmuch-prettify-subject (notmuch-show-get-subject)))
1379 (defun notmuch-show-set-tags (tags)
1380 "Set the tags of the current message."
1381 (notmuch-show-set-prop :tags tags)
1382 (notmuch-show-update-tags tags))
1384 (defun notmuch-show-get-tags ()
1385 "Return the tags of the current message."
1386 (notmuch-show-get-prop :tags))
1388 (defun notmuch-show-message-visible-p ()
1389 "Is the current message visible?"
1390 (notmuch-show-get-prop :message-visible))
1392 (defun notmuch-show-headers-visible-p ()
1393 "Are the headers of the current message visible?"
1394 (notmuch-show-get-prop :headers-visible))
1396 (defun notmuch-show-mark-read ()
1397 "Mark the current message as read."
1398 (notmuch-show-tag-message "-unread"))
1400 ;; Functions for getting attributes of several messages in the current
1403 (defun notmuch-show-get-message-ids-for-open-messages ()
1404 "Return a list of all message IDs for open messages in the current thread."
1406 (let (message-ids done)
1407 (goto-char (point-min))
1409 (if (notmuch-show-message-visible-p)
1410 (setq message-ids (append message-ids (list (notmuch-show-get-message-id)))))
1411 (setq done (not (notmuch-show-goto-message-next)))
1416 ;; Commands typically bound to keys.
1418 (defun notmuch-show-advance ()
1419 "Advance through thread.
1421 If the current message in the thread is not yet fully visible,
1422 scroll by a near screenful to read more of the message.
1424 Otherwise, (the end of the current message is already within the
1425 current window), advance to the next open message."
1427 (let* ((end-of-this-message (notmuch-show-message-bottom))
1428 (visible-end-of-this-message (1- end-of-this-message))
1430 (while (invisible-p visible-end-of-this-message)
1431 (setq visible-end-of-this-message
1433 (1- (previous-single-char-property-change
1434 visible-end-of-this-message 'invisible)))))
1436 ;; Ideally we would test `end-of-this-message' against the result
1437 ;; of `window-end', but that doesn't account for the fact that
1438 ;; the end of the message might be hidden.
1439 ((and visible-end-of-this-message
1440 (> visible-end-of-this-message (window-end)))
1441 ;; The bottom of this message is not visible - scroll.
1444 ((not (= end-of-this-message (point-max)))
1445 ;; This is not the last message - move to the next visible one.
1446 (notmuch-show-next-open-message))
1449 ;; This is the last message - change the return value
1453 (defun notmuch-show-advance-and-archive ()
1454 "Advance through thread and archive.
1456 This command is intended to be one of the simplest ways to
1457 process a thread of email. It works exactly like
1458 notmuch-show-advance, in that it scrolls through messages in a
1459 show buffer, except that when it gets to the end of the buffer it
1460 archives the entire current thread, (remove the \"inbox\" tag
1461 from each message), kills the buffer, and displays the next
1462 thread from the search from which this thread was originally
1465 (if (notmuch-show-advance)
1466 (notmuch-show-archive-thread-then-next)))
1468 (defun notmuch-show-rewind ()
1469 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
1471 Specifically, if the beginning of the previous email is fewer
1472 than `window-height' lines from the current point, move to it
1473 just like `notmuch-show-previous-message'.
1475 Otherwise, just scroll down a screenful of the current message.
1477 This command does not modify any message tags, (it does not undo
1478 any effects from previous calls to
1479 `notmuch-show-advance-and-archive'."
1481 (let ((start-of-message (notmuch-show-message-top))
1482 (start-of-window (window-start)))
1484 ;; Either this message is properly aligned with the start of the
1485 ;; window or the start of this message is not visible on the
1487 ((or (= start-of-message start-of-window)
1488 (< start-of-message start-of-window))
1490 ;; If a small number of lines from the previous message are
1491 ;; visible, realign so that the top of the current message is at
1492 ;; the top of the screen.
1493 (when (<= (count-screen-lines (window-start) start-of-message)
1494 next-screen-context-lines)
1495 (goto-char (notmuch-show-message-top))
1496 (notmuch-show-message-adjust))
1497 ;; Move to the top left of the window.
1498 (goto-char (window-start)))
1500 ;; Move to the previous message.
1501 (notmuch-show-previous-message)))))
1503 (defun notmuch-show-reply (&optional prompt-for-sender)
1504 "Reply to the sender and all recipients of the current message."
1506 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender t))
1508 (defun notmuch-show-reply-sender (&optional prompt-for-sender)
1509 "Reply to the sender of the current message."
1511 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender nil))
1513 (defun notmuch-show-forward-message (&optional prompt-for-sender)
1514 "Forward the current message."
1516 (with-current-notmuch-show-message
1517 (notmuch-mua-new-forward-message prompt-for-sender)))
1519 (defun notmuch-show-next-message (&optional pop-at-end)
1520 "Show the next message.
1522 If a prefix argument is given and this is the last message in the
1523 thread, navigate to the next thread in the parent search buffer."
1525 (if (notmuch-show-goto-message-next)
1527 (notmuch-show-mark-read)
1528 (notmuch-show-message-adjust))
1530 (notmuch-show-next-thread)
1531 (goto-char (point-max)))))
1533 (defun notmuch-show-previous-message ()
1534 "Show the previous message."
1536 (notmuch-show-goto-message-previous)
1537 (notmuch-show-mark-read)
1538 (notmuch-show-message-adjust))
1540 (defun notmuch-show-next-open-message (&optional pop-at-end)
1541 "Show the next open message.
1543 If a prefix argument is given and this is the last open message
1544 in the thread, navigate to the next thread in the parent search
1545 buffer. Return t if there was a next open message in the thread
1546 to show, nil otherwise."
1549 (while (and (setq r (notmuch-show-goto-message-next))
1550 (not (notmuch-show-message-visible-p))))
1553 (notmuch-show-mark-read)
1554 (notmuch-show-message-adjust))
1556 (notmuch-show-next-thread)
1557 (goto-char (point-max))))
1560 (defun notmuch-show-previous-open-message ()
1561 "Show the previous open message."
1563 (while (and (notmuch-show-goto-message-previous)
1564 (not (notmuch-show-message-visible-p))))
1565 (notmuch-show-mark-read)
1566 (notmuch-show-message-adjust))
1568 (defun notmuch-show-view-raw-message ()
1569 "View the file holding the current message."
1571 (let* ((id (notmuch-show-get-message-id))
1572 (buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
1573 (call-process notmuch-command nil buf nil "show" "--format=raw" id)
1574 (switch-to-buffer buf)
1575 (goto-char (point-min))
1576 (set-buffer-modified-p nil)
1577 (view-buffer buf 'kill-buffer-if-not-modified)))
1579 (defun notmuch-show-pipe-message (entire-thread command)
1580 "Pipe the contents of the current message (or thread) to the given command.
1582 The given command will be executed with the raw contents of the
1583 current email message as stdin. Anything printed by the command
1584 to stdout or stderr will appear in the *notmuch-pipe* buffer.
1586 When invoked with a prefix argument, the command will receive all
1587 open messages in the current thread (formatted as an mbox) rather
1588 than only the current message."
1589 (interactive "P\nsPipe message to command: ")
1590 (let (shell-command)
1593 (concat notmuch-command " show --format=mbox "
1594 (shell-quote-argument
1595 (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
1598 (concat notmuch-command " show --format=raw "
1599 (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
1600 (let ((buf (get-buffer-create (concat "*notmuch-pipe*"))))
1601 (with-current-buffer buf
1602 (setq buffer-read-only nil)
1604 (let ((exit-code (call-process-shell-command shell-command nil buf)))
1605 (goto-char (point-max))
1606 (set-buffer-modified-p nil)
1607 (setq buffer-read-only t)
1608 (unless (zerop exit-code)
1609 (switch-to-buffer-other-window buf)
1610 (message (format "Command '%s' exited abnormally with code %d"
1611 shell-command exit-code))))))))
1613 (defun notmuch-show-tag-message (&rest tag-changes)
1614 "Change tags for the current message.
1616 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1617 (let* ((current-tags (notmuch-show-get-tags))
1618 (new-tags (notmuch-update-tags current-tags tag-changes)))
1619 (unless (equal current-tags new-tags)
1620 (apply 'notmuch-tag (notmuch-show-get-message-id) tag-changes)
1621 (notmuch-show-set-tags new-tags))))
1623 (defun notmuch-show-tag (&optional initial-input)
1624 "Change tags for the current message, read input from the minibuffer."
1626 (let ((tag-changes (notmuch-read-tag-changes
1627 initial-input (notmuch-show-get-message-id))))
1628 (apply 'notmuch-show-tag-message tag-changes)))
1630 (defun notmuch-show-tag-all (&rest tag-changes)
1631 "Change tags for all messages in the current thread.
1633 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1634 (interactive (notmuch-read-tag-changes nil notmuch-show-thread-id))
1635 (apply 'notmuch-tag (notmuch-show-get-messages-ids-search) tag-changes)
1638 (let* ((current-tags (notmuch-show-get-tags))
1639 (new-tags (notmuch-update-tags current-tags tag-changes)))
1640 (unless (equal current-tags new-tags)
1641 (notmuch-show-set-tags new-tags))))))
1643 (defun notmuch-show-add-tag ()
1644 "Same as `notmuch-show-tag' but sets initial input to '+'."
1646 (notmuch-show-tag "+"))
1648 (defun notmuch-show-remove-tag ()
1649 "Same as `notmuch-show-tag' but sets initial input to '-'."
1651 (notmuch-show-tag "-"))
1653 (defun notmuch-show-toggle-headers ()
1654 "Toggle the visibility of the current message headers."
1656 (let ((props (notmuch-show-get-message-properties)))
1657 (notmuch-show-headers-visible
1659 (not (plist-get props :headers-visible))))
1660 (force-window-update))
1662 (defun notmuch-show-toggle-message ()
1663 "Toggle the visibility of the current message."
1665 (let ((props (notmuch-show-get-message-properties)))
1666 (notmuch-show-message-visible
1668 (not (plist-get props :message-visible))))
1669 (force-window-update))
1671 (defun notmuch-show-open-or-close-all ()
1672 "Set the visibility all of the messages in the current thread.
1673 By default make all of the messages visible. With a prefix
1674 argument, hide all of the messages."
1677 (goto-char (point-min))
1678 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1679 (not current-prefix-arg))
1680 until (not (notmuch-show-goto-message-next))))
1681 (force-window-update))
1683 (defun notmuch-show-next-button ()
1684 "Advance point to the next button in the buffer."
1688 (defun notmuch-show-previous-button ()
1689 "Move point back to the previous button in the buffer."
1691 (backward-button 1))
1693 (defun notmuch-show-next-thread (&optional show-next)
1694 "Move to the next item in the search results, if any."
1696 (let ((parent-buffer notmuch-show-parent-buffer))
1697 (notmuch-kill-this-buffer)
1698 (when (buffer-live-p parent-buffer)
1699 (switch-to-buffer parent-buffer)
1700 (notmuch-search-next-thread)
1702 (notmuch-search-show-thread)))))
1704 (defun notmuch-show-archive-thread (&optional unarchive)
1705 "Archive each message in thread.
1707 If a prefix argument is given, the messages will be
1708 \"unarchived\" (ie. the \"inbox\" tag will be added instead of
1711 Archive each message currently shown by removing the \"inbox\"
1712 tag from each. Then kill this buffer and show the next thread
1713 from the search from which this thread was originally shown.
1715 Note: This command is safe from any race condition of new messages
1716 being delivered to the same thread. It does not archive the
1717 entire thread, but only the messages shown in the current
1720 (let ((op (if unarchive "+" "-")))
1721 (notmuch-show-tag-all (concat op "inbox"))))
1723 (defun notmuch-show-archive-thread-then-next ()
1724 "Archive each message in thread, then show next thread from search."
1726 (notmuch-show-archive-thread)
1727 (notmuch-show-next-thread t))
1729 (defun notmuch-show-archive-thread-then-exit ()
1730 "Archive each message in thread, then exit back to search results."
1732 (notmuch-show-archive-thread)
1733 (notmuch-show-next-thread))
1735 (defun notmuch-show-archive-message (&optional unarchive)
1736 "Archive the current message.
1738 If a prefix argument is given, the message will be
1739 \"unarchived\" (ie. the \"inbox\" tag will be added instead of
1742 (let ((op (if unarchive "+" "-")))
1743 (notmuch-show-tag-message (concat op "inbox"))))
1745 (defun notmuch-show-archive-message-then-next-or-exit ()
1746 "Archive the current message, then show the next open message in the current thread.
1748 If at the last open message in the current thread, then exit back
1751 (notmuch-show-archive-message)
1752 (notmuch-show-next-open-message t))
1754 (defun notmuch-show-archive-message-then-next-or-next-thread ()
1755 "Archive the current message, then show the next open message in the current thread.
1757 If at the last open message in the current thread, then show next
1758 thread from search."
1760 (notmuch-show-archive-message)
1761 (unless (notmuch-show-next-open-message)
1762 (notmuch-show-next-thread t)))
1764 (defun notmuch-show-stash-cc ()
1765 "Copy CC field of current message to kill-ring."
1767 (notmuch-common-do-stash (notmuch-show-get-cc)))
1769 (defun notmuch-show-stash-date ()
1770 "Copy date of current message to kill-ring."
1772 (notmuch-common-do-stash (notmuch-show-get-date)))
1774 (defun notmuch-show-stash-filename ()
1775 "Copy filename of current message to kill-ring."
1777 (notmuch-common-do-stash (notmuch-show-get-filename)))
1779 (defun notmuch-show-stash-from ()
1780 "Copy From address of current message to kill-ring."
1782 (notmuch-common-do-stash (notmuch-show-get-from)))
1784 (defun notmuch-show-stash-message-id ()
1785 "Copy message ID of current message to kill-ring."
1787 (notmuch-common-do-stash (notmuch-show-get-message-id)))
1789 (defun notmuch-show-stash-message-id-stripped ()
1790 "Copy message ID of current message (sans `id:' prefix) to kill-ring."
1792 (notmuch-common-do-stash (substring (notmuch-show-get-message-id) 4 -1)))
1794 (defun notmuch-show-stash-subject ()
1795 "Copy Subject field of current message to kill-ring."
1797 (notmuch-common-do-stash (notmuch-show-get-subject)))
1799 (defun notmuch-show-stash-tags ()
1800 "Copy tags of current message to kill-ring as a comma separated list."
1802 (notmuch-common-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1804 (defun notmuch-show-stash-to ()
1805 "Copy To address of current message to kill-ring."
1807 (notmuch-common-do-stash (notmuch-show-get-to)))
1809 ;; Commands typically bound to buttons.
1811 (defun notmuch-show-part-button-default (&optional button)
1813 (notmuch-show-part-button-internal button notmuch-show-part-button-default-action))
1815 (defun notmuch-show-part-button-save (&optional button)
1817 (notmuch-show-part-button-internal button #'notmuch-show-save-part))
1819 (defun notmuch-show-part-button-view (&optional button)
1821 (notmuch-show-part-button-internal button #'notmuch-show-view-part))
1823 (defun notmuch-show-part-button-interactively-view (&optional button)
1825 (notmuch-show-part-button-internal button #'notmuch-show-interactively-view-part))
1827 (defun notmuch-show-part-button-internal (button handler)
1828 (let ((button (or button (button-at (point)))))
1830 (let ((nth (button-get button :notmuch-part)))
1832 (funcall handler (notmuch-show-get-message-id) nth
1833 (button-get button :notmuch-filename)
1834 (button-get button :notmuch-content-type)))))))
1838 (provide 'notmuch-show)