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 (defvar notmuch-show-thread-id nil)
130 (make-variable-buffer-local 'notmuch-show-thread-id)
131 (put 'notmuch-show-thread-id 'permanent-local t)
133 (defvar notmuch-show-parent-buffer nil)
134 (make-variable-buffer-local 'notmuch-show-parent-buffer)
135 (put 'notmuch-show-parent-buffer 'permanent-local t)
137 (defvar notmuch-show-query-context nil)
138 (make-variable-buffer-local 'notmuch-show-query-context)
139 (put 'notmuch-show-query-context 'permanent-local t)
141 (defvar notmuch-show-process-crypto nil)
142 (make-variable-buffer-local 'notmuch-show-process-crypto)
143 (put 'notmuch-show-process-crypto 'permanent-local t)
145 (defvar notmuch-show-elide-non-matching-messages nil)
146 (make-variable-buffer-local 'notmuch-show-elide-non-matching-messages)
147 (put 'notmuch-show-elide-non-matching-messages 'permanent-local t)
149 (defvar notmuch-show-indent-content t)
150 (make-variable-buffer-local 'notmuch-show-indent-content)
151 (put 'notmuch-show-indent-content 'permanent-local t)
153 (defmacro with-current-notmuch-show-message (&rest body)
154 "Evaluate body with current buffer set to the text of current message"
156 (let ((id (notmuch-show-get-message-id)))
157 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
158 (with-current-buffer buf
159 (call-process notmuch-command nil t nil "show" "--format=raw" id)
161 (kill-buffer buf)))))
163 (defun notmuch-show-turn-on-visual-line-mode ()
164 "Enable Visual Line mode."
165 (visual-line-mode t))
167 (defun notmuch-show-view-all-mime-parts ()
168 "Use external viewers to view all attachments from the current message."
170 (with-current-notmuch-show-message
171 ;; We override the mm-inline-media-tests to indicate which message
172 ;; parts are already sufficiently handled by the original
173 ;; presentation of the message in notmuch-show mode. These parts
174 ;; will be inserted directly into the temporary buffer of
175 ;; with-current-notmuch-show-message and silently discarded.
177 ;; Any MIME part not explicitly mentioned here will be handled by an
178 ;; external viewer as configured in the various mailcap files.
179 (let ((mm-inline-media-tests '(
180 ("text/.*" ignore identity)
181 ("application/pgp-signature" ignore identity)
182 ("multipart/alternative" ignore identity)
183 ("multipart/mixed" ignore identity)
184 ("multipart/related" ignore identity)
186 (mm-display-parts (mm-dissect-buffer)))))
188 (defun notmuch-foreach-mime-part (function mm-handle)
189 (cond ((stringp (car mm-handle))
190 (dolist (part (cdr mm-handle))
191 (notmuch-foreach-mime-part function part)))
192 ((bufferp (car mm-handle))
193 (funcall function mm-handle))
194 (t (dolist (part mm-handle)
195 (notmuch-foreach-mime-part function part)))))
197 (defun notmuch-count-attachments (mm-handle)
199 (notmuch-foreach-mime-part
201 (let ((disposition (mm-handle-disposition p)))
202 (and (listp disposition)
203 (or (equal (car disposition) "attachment")
204 (and (equal (car disposition) "inline")
205 (assq 'filename disposition)))
210 (defun notmuch-save-attachments (mm-handle &optional queryp)
211 (notmuch-foreach-mime-part
213 (let ((disposition (mm-handle-disposition p)))
214 (and (listp disposition)
215 (or (equal (car disposition) "attachment")
216 (and (equal (car disposition) "inline")
217 (assq 'filename disposition)))
220 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
224 (defun notmuch-show-save-attachments ()
225 "Save all attachments from the current message."
227 (with-current-notmuch-show-message
228 (let ((mm-handle (mm-dissect-buffer)))
229 (notmuch-save-attachments
230 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
233 (defun notmuch-show-with-message-as-text (fn)
234 "Apply FN to a text representation of the current message.
236 FN is called with one argument, the message properties. It should
237 operation on the contents of the current buffer."
239 ;; Remake the header to ensure that all information is available.
240 (let* ((to (notmuch-show-get-to))
241 (cc (notmuch-show-get-cc))
242 (from (notmuch-show-get-from))
243 (subject (notmuch-show-get-subject))
244 (date (notmuch-show-get-date))
245 (tags (notmuch-show-get-tags))
246 (depth (notmuch-show-get-depth))
249 "Subject: " subject "\n"
251 (if (not (string= cc ""))
252 (concat "Cc: " cc "\n")
258 (mapconcat #'identity tags ", ") "\n")
260 (all (buffer-substring (notmuch-show-message-top)
261 (notmuch-show-message-bottom)))
263 (props (notmuch-show-get-message-properties))
264 (indenting notmuch-show-indent-content))
268 (indent-rigidly (point-min) (point-max) (- depth)))
269 ;; Remove the original header.
270 (goto-char (point-min))
271 (re-search-forward "^$" (point-max) nil)
272 (delete-region (point-min) (point))
274 (funcall fn props))))
276 (defun notmuch-show-print-message ()
277 "Print the current message."
279 (notmuch-show-with-message-as-text 'notmuch-print-message))
281 (defun notmuch-show-fontify-header ()
283 ((looking-at "[Tt]o:")
285 ((looking-at "[Bb]?[Cc][Cc]:")
287 ((looking-at "[Ss]ubject:")
288 'message-header-subject)
289 ((looking-at "[Ff]rom:")
290 'message-header-from)
292 'message-header-other))))
294 (overlay-put (make-overlay (point) (re-search-forward ":"))
295 'face 'message-header-name)
296 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
299 (defun notmuch-show-colour-headers ()
300 "Apply some colouring to the current headers."
301 (goto-char (point-min))
302 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
303 (notmuch-show-fontify-header)
306 (defun notmuch-show-spaces-n (n)
307 "Return a string comprised of `n' spaces."
310 (defun notmuch-show-update-tags (tags)
311 "Update the displayed tags of the current message."
313 (goto-char (notmuch-show-message-top))
314 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
315 (let ((inhibit-read-only t))
316 (replace-match (concat "("
317 (propertize (mapconcat 'identity tags " ")
318 'face 'notmuch-tag-face)
321 (defun notmuch-show-clean-address (address)
322 "Try to clean a single email ADDRESS for display. Return
323 unchanged ADDRESS if parsing fails."
325 (let (p-name p-address)
326 ;; It would be convenient to use `mail-header-parse-address',
327 ;; but that expects un-decoded mailbox parts, whereas our
328 ;; mailbox parts are already decoded (and hence may contain
329 ;; UTF-8). Given that notmuch should handle most of the awkward
330 ;; cases, some simple string deconstruction should be sufficient
333 ;; "User <user@dom.ain>" style.
334 ((string-match "\\(.*\\) <\\(.*\\)>" address)
335 (setq p-name (match-string 1 address)
336 p-address (match-string 2 address)))
338 ;; "<user@dom.ain>" style.
339 ((string-match "<\\(.*\\)>" address)
340 (setq p-address (match-string 1 address)))
344 (setq p-address address)))
347 ;; Remove elements of the mailbox part that are not relevant for
348 ;; display, even if they are required during transport:
351 (setq p-name (replace-regexp-in-string "\\\\" "" p-name))
353 ;; Outer single and double quotes, which might be nested.
356 do (setq start-of-loop p-name)
358 when (string-match "^\"\\(.*\\)\"$" p-name)
359 do (setq p-name (match-string 1 p-name))
361 when (string-match "^'\\(.*\\)'$" p-name)
362 do (setq p-name (match-string 1 p-name))
364 until (string= start-of-loop p-name)))
366 ;; If the address is 'foo@bar.com <foo@bar.com>' then show just
368 (when (string= p-name p-address)
371 ;; If no name results, return just the address.
374 ;; Otherwise format the name and address together.
375 (concat p-name " <" p-address ">")))
378 (defun notmuch-show-insert-headerline (headers date tags depth)
379 "Insert a notmuch style headerline based on HEADERS for a
380 message at DEPTH in the current thread."
381 (let ((start (point)))
382 (insert (notmuch-show-spaces-n (* notmuch-show-indent-messages-width depth))
383 (notmuch-show-clean-address (plist-get headers :From))
387 (propertize (mapconcat 'identity tags " ")
388 'face 'notmuch-tag-face)
390 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
392 (defun notmuch-show-insert-header (header header-value)
393 "Insert a single header."
394 (insert header ": " header-value "\n"))
396 (defun notmuch-show-insert-headers (headers)
397 "Insert the headers of the current message."
398 (let ((start (point)))
399 (mapc (lambda (header)
400 (let* ((header-symbol (intern (concat ":" header)))
401 (header-value (plist-get headers header-symbol)))
402 (if (and header-value
403 (not (string-equal "" header-value)))
404 (notmuch-show-insert-header header header-value))))
405 notmuch-message-headers)
408 (narrow-to-region start (point-max))
409 (run-hooks 'notmuch-show-markup-headers-hook)))))
411 (define-button-type 'notmuch-show-part-button-type
412 'action 'notmuch-show-part-button-default
413 'keymap 'notmuch-show-part-button-map
417 (defvar notmuch-show-part-button-map
418 (let ((map (make-sparse-keymap)))
419 (set-keymap-parent map button-map)
420 (define-key map "s" 'notmuch-show-part-button-save)
421 (define-key map "v" 'notmuch-show-part-button-view)
422 (define-key map "o" 'notmuch-show-part-button-interactively-view)
424 "Submap for button commands")
425 (fset 'notmuch-show-part-button-map notmuch-show-part-button-map)
427 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name comment)
432 (if name (concat name ": ") "")
434 (if (not (string-equal declared-type content-type))
435 (concat " (as " content-type ")")
439 :type 'notmuch-show-part-button-type
441 :notmuch-filename name
442 :notmuch-content-type content-type))
447 ;; Functions handling particular MIME parts.
449 (defmacro notmuch-with-temp-part-buffer (message-id nth &rest body)
451 (let ((process-crypto (make-symbol "process-crypto")))
452 `(let ((,process-crypto notmuch-show-process-crypto))
454 (setq notmuch-show-process-crypto ,process-crypto)
455 ;; Always acquires the part via `notmuch part', even if it is
456 ;; available in the JSON output.
457 (insert (notmuch-show-get-bodypart-internal ,message-id ,nth))
460 (defun notmuch-show-save-part (message-id nth &optional filename content-type)
461 (notmuch-with-temp-part-buffer message-id nth
462 (let ((file (read-file-name
463 "Filename to save as: "
464 (or mailcap-download-directory "~/")
467 ;; Don't re-compress .gz & al. Arguably we should make
468 ;; `file-name-handler-alist' nil, but that would chop
469 ;; ange-ftp, which is reasonable to use here.
470 (mm-write-region (point-min) (point-max) file nil nil nil 'no-conversion t))))
472 (defun notmuch-show-view-part (message-id nth &optional filename content-type )
473 (notmuch-with-temp-part-buffer message-id nth
474 ;; set mm-inlined-types to nil to force an external viewer
475 (let ((handle (mm-make-handle (current-buffer) (list content-type)))
476 (mm-inlined-types nil))
477 ;; We override mm-save-part as notmuch-show-save-part is better
478 ;; since it offers the filename. We need to lexically bind
479 ;; everything we need for notmuch-show-save-part to prevent
480 ;; potential dynamic shadowing.
481 (lexical-let ((message-id message-id)
484 (content-type content-type))
485 (flet ((mm-save-part (&rest args) (notmuch-show-save-part
486 message-id nth filename content-type)))
487 (mm-display-part handle))))))
489 (defun notmuch-show-interactively-view-part (message-id nth &optional filename content-type)
490 (notmuch-with-temp-part-buffer message-id nth
491 (let ((handle (mm-make-handle (current-buffer) (list content-type))))
492 (mm-interactively-view-part handle))))
494 (defun notmuch-show-mm-display-part-inline (msg part nth content-type)
495 "Use the mm-decode/mm-view functions to display a part in the
496 current buffer, if possible."
497 (let ((display-buffer (current-buffer)))
499 (let* ((charset (plist-get part :content-charset))
500 (handle (mm-make-handle (current-buffer) `(,content-type (charset . ,charset)))))
501 ;; If the user wants the part inlined, insert the content and
502 ;; test whether we are able to inline it (which includes both
503 ;; capability and suitability tests).
504 (when (mm-inlined-p handle)
505 (insert (notmuch-show-get-bodypart-content msg part nth))
506 (when (mm-inlinable-p handle)
507 (set-buffer display-buffer)
508 (mm-display-part handle)
511 (defvar notmuch-show-multipart/alternative-discouraged
515 ;; multipart/related usually contain a text/html part and some associated graphics.
519 (defun notmuch-show-multipart/*-to-list (part)
520 (mapcar (lambda (inner-part) (plist-get inner-part :content-type))
521 (plist-get part :content)))
523 (defun notmuch-show-multipart/alternative-choose (types)
524 ;; Based on `mm-preferred-alternative-precedence'.
526 (dolist (pref (reverse notmuch-show-multipart/alternative-discouraged))
527 (dolist (elem (copy-sequence seq))
528 (when (string-match pref elem)
529 (setq seq (nconc (delete elem seq) (list elem))))))
532 (defun notmuch-show-insert-part-multipart/alternative (msg part content-type nth depth declared-type)
533 (notmuch-show-insert-part-header nth declared-type content-type nil)
534 (let ((chosen-type (car (notmuch-show-multipart/alternative-choose (notmuch-show-multipart/*-to-list part))))
535 (inner-parts (plist-get part :content))
537 ;; This inserts all parts of the chosen type rather than just one,
538 ;; but it's not clear that this is the wrong thing to do - which
539 ;; should be chosen if there are more than one that match?
540 (mapc (lambda (inner-part)
541 (let ((inner-type (plist-get inner-part :content-type)))
542 (if (or notmuch-show-all-multipart/alternative-parts
543 (string= chosen-type inner-type))
544 (notmuch-show-insert-bodypart msg inner-part depth)
545 (notmuch-show-insert-part-header (plist-get inner-part :id) inner-type inner-type nil " (not shown)"))))
548 (when notmuch-show-indent-multipart
549 (indent-rigidly start (point) 1)))
552 (defun notmuch-show-setup-w3m ()
553 "Instruct w3m how to retrieve content from a \"related\" part of a message."
555 (if (boundp 'w3m-cid-retrieve-function-alist)
556 (unless (assq 'notmuch-show-mode w3m-cid-retrieve-function-alist)
557 (push (cons 'notmuch-show-mode 'notmuch-show-w3m-cid-retrieve)
558 w3m-cid-retrieve-function-alist)))
559 (setq mm-inline-text-html-with-images t))
561 (defvar w3m-current-buffer) ;; From `w3m.el'.
562 (defvar notmuch-show-w3m-cid-store nil)
563 (make-variable-buffer-local 'notmuch-show-w3m-cid-store)
565 (defun notmuch-show-w3m-cid-store-internal (content-id
570 (push (list content-id
575 notmuch-show-w3m-cid-store))
577 (defun notmuch-show-w3m-cid-store (msg part)
578 (let ((content-id (plist-get part :content-id)))
580 (notmuch-show-w3m-cid-store-internal (concat "cid:" content-id)
583 (plist-get part :content-type)
586 (defun notmuch-show-w3m-cid-retrieve (url &rest args)
587 (let ((matching-part (with-current-buffer w3m-current-buffer
588 (assoc url notmuch-show-w3m-cid-store))))
590 (let ((message-id (nth 1 matching-part))
591 (part-number (nth 2 matching-part))
592 (content-type (nth 3 matching-part))
593 (content (nth 4 matching-part)))
594 ;; If we don't already have the content, get it and cache
595 ;; it, as some messages reference the same cid: part many
596 ;; times (hundreds!), which results in many calls to
599 (setq content (notmuch-show-get-bodypart-internal (concat "id:" message-id)
601 (with-current-buffer w3m-current-buffer
602 (notmuch-show-w3m-cid-store-internal url
611 (defun notmuch-show-insert-part-multipart/related (msg part content-type nth depth declared-type)
612 (notmuch-show-insert-part-header nth declared-type content-type nil)
613 (let ((inner-parts (plist-get part :content))
616 ;; We assume that the first part is text/html and the remainder
617 ;; things that it references.
619 ;; Stash the non-primary parts.
621 (notmuch-show-w3m-cid-store msg part))
624 ;; Render the primary part.
625 (notmuch-show-insert-bodypart msg (car inner-parts) depth)
627 (when notmuch-show-indent-multipart
628 (indent-rigidly start (point) 1)))
631 (defun notmuch-show-insert-part-multipart/signed (msg part content-type nth depth declared-type)
632 (let ((button (notmuch-show-insert-part-header nth declared-type content-type nil)))
633 (button-put button 'face 'notmuch-crypto-part-header)
634 ;; add signature status button if sigstatus provided
635 (if (plist-member part :sigstatus)
636 (let* ((from (notmuch-show-get-header :From msg))
637 (sigstatus (car (plist-get part :sigstatus))))
638 (notmuch-crypto-insert-sigstatus-button sigstatus from))
639 ;; if we're not adding sigstatus, tell the user how they can get it
640 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts.")))
642 (let ((inner-parts (plist-get part :content))
644 ;; Show all of the parts.
645 (mapc (lambda (inner-part)
646 (notmuch-show-insert-bodypart msg inner-part depth))
649 (when notmuch-show-indent-multipart
650 (indent-rigidly start (point) 1)))
653 (defun notmuch-show-insert-part-multipart/encrypted (msg part content-type nth depth declared-type)
654 (let ((button (notmuch-show-insert-part-header nth declared-type content-type nil)))
655 (button-put button 'face 'notmuch-crypto-part-header)
656 ;; add encryption status button if encstatus specified
657 (if (plist-member part :encstatus)
658 (let ((encstatus (car (plist-get part :encstatus))))
659 (notmuch-crypto-insert-encstatus-button encstatus)
660 ;; add signature status button if sigstatus specified
661 (if (plist-member part :sigstatus)
662 (let* ((from (notmuch-show-get-header :From msg))
663 (sigstatus (car (plist-get part :sigstatus))))
664 (notmuch-crypto-insert-sigstatus-button sigstatus from))))
665 ;; if we're not adding encstatus, tell the user how they can get it
666 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts.")))
668 (let ((inner-parts (plist-get part :content))
670 ;; Show all of the parts.
671 (mapc (lambda (inner-part)
672 (notmuch-show-insert-bodypart msg inner-part depth))
675 (when notmuch-show-indent-multipart
676 (indent-rigidly start (point) 1)))
679 (defun notmuch-show-insert-part-multipart/* (msg part content-type nth depth declared-type)
680 (notmuch-show-insert-part-header nth declared-type content-type nil)
681 (let ((inner-parts (plist-get part :content))
683 ;; Show all of the parts.
684 (mapc (lambda (inner-part)
685 (notmuch-show-insert-bodypart msg inner-part depth))
688 (when notmuch-show-indent-multipart
689 (indent-rigidly start (point) 1)))
692 (defun notmuch-show-insert-part-message/rfc822 (msg part content-type nth depth declared-type)
693 (notmuch-show-insert-part-header nth declared-type content-type nil)
694 (let* ((message (car (plist-get part :content)))
695 (body (car (plist-get message :body)))
698 ;; Override `notmuch-message-headers' to force `From' to be
700 (let ((notmuch-message-headers '("From" "Subject" "To" "Cc" "Date")))
701 (notmuch-show-insert-headers (plist-get message :headers)))
703 ;; Blank line after headers to be compatible with the normal
708 (notmuch-show-insert-bodypart msg body depth)
710 (when notmuch-show-indent-multipart
711 (indent-rigidly start (point) 1)))
714 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
715 (let ((start (point)))
716 ;; If this text/plain part is not the first part in the message,
717 ;; insert a header to make this clear.
719 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
720 (insert (notmuch-show-get-bodypart-content msg part nth))
723 (narrow-to-region start (point-max))
724 (run-hook-with-args 'notmuch-show-insert-text/plain-hook msg depth))))
727 (defun notmuch-show-insert-part-text/x-vcalendar (msg part content-type nth depth declared-type)
728 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename))
729 (insert (with-temp-buffer
730 (insert (notmuch-show-get-bodypart-content msg part nth))
731 (goto-char (point-min))
732 (let ((file (make-temp-file "notmuch-ical"))
734 (icalendar--convert-ical-to-diary
735 (icalendar--read-element nil nil)
737 (set-buffer (get-file-buffer file))
738 (setq result (buffer-substring (point-min) (point-max)))
739 (set-buffer-modified-p nil)
740 (kill-buffer (current-buffer))
745 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
746 ;; If we can deduce a MIME type from the filename of the attachment,
747 ;; do so and pass it on to the handler for that type.
748 (if (plist-get part :filename)
749 (let ((extension (file-name-extension (plist-get part :filename)))
753 (mailcap-parse-mimetypes)
754 (setq mime-type (mailcap-extension-to-mime extension))
756 (not (string-equal mime-type "application/octet-stream")))
757 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
761 ;; Handler for wash generated inline patch fake parts.
762 (defun notmuch-show-insert-part-inline-patch-fake-part (msg part content-type nth depth declared-type)
763 (notmuch-show-insert-part-*/* msg part "text/x-diff" nth depth "inline patch"))
765 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
766 ;; This handler _must_ succeed - it is the handler of last resort.
767 (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
768 (notmuch-show-mm-display-part-inline msg part nth content-type)
771 ;; Functions for determining how to handle MIME parts.
773 (defun notmuch-show-split-content-type (content-type)
774 (split-string content-type "/"))
776 (defun notmuch-show-handlers-for (content-type)
777 "Return a list of content handlers for a part of type CONTENT-TYPE."
782 ;; Reverse order of prefrence.
783 (list (intern (concat "notmuch-show-insert-part-*/*"))
785 "notmuch-show-insert-part-"
786 (car (notmuch-show-split-content-type content-type))
788 (intern (concat "notmuch-show-insert-part-" content-type))))
791 ;; Helper for parts which are generally not included in the default
793 (defun notmuch-show-get-bodypart-internal (message-id part-number)
794 (let ((args '("show" "--format=raw"))
795 (part-arg (format "--part=%s" part-number)))
796 (setq args (append args (list part-arg)))
797 (if notmuch-show-process-crypto
798 (setq args (append args '("--decrypt"))))
799 (setq args (append args (list message-id)))
801 (let ((coding-system-for-read 'no-conversion))
803 (apply 'call-process (append (list notmuch-command nil (list t nil) nil) args))
806 (defun notmuch-show-get-bodypart-content (msg part nth)
807 (or (plist-get part :content)
808 (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
812 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
813 (let ((handlers (notmuch-show-handlers-for content-type)))
814 ;; Run the content handlers until one of them returns a non-nil
817 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
818 (setq handlers (cdr handlers))))
821 (defun notmuch-show-insert-bodypart (msg part depth)
822 "Insert the body part PART at depth DEPTH in the current thread."
823 (let ((content-type (downcase (plist-get part :content-type)))
824 (nth (plist-get part :id)))
825 (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
826 ;; Some of the body part handlers leave point somewhere up in the
827 ;; part, so we make sure that we're down at the end.
828 (goto-char (point-max))
829 ;; Ensure that the part ends with a carriage return.
833 (defun notmuch-show-insert-body (msg body depth)
834 "Insert the body BODY at depth DEPTH in the current thread."
835 (mapc (lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
837 (defun notmuch-show-make-symbol (type)
838 (make-symbol (concat "notmuch-show-" type)))
840 (defun notmuch-show-strip-re (string)
841 (replace-regexp-in-string "\\([Rr]e: *\\)+" "" string))
843 (defvar notmuch-show-previous-subject "")
844 (make-variable-buffer-local 'notmuch-show-previous-subject)
846 (defun notmuch-show-insert-msg (msg depth)
847 "Insert the message MSG at depth DEPTH in the current thread."
848 (let* ((headers (plist-get msg :headers))
849 ;; Indentation causes the buffer offset of the start/end
850 ;; points to move, so we must use markers.
851 message-start message-end
852 content-start content-end
853 headers-start headers-end
855 (headers-invis-spec (notmuch-show-make-symbol "header"))
856 (message-invis-spec (notmuch-show-make-symbol "message"))
857 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
859 ;; Set `buffer-invisibility-spec' to `nil' (a list), otherwise
860 ;; removing items from `buffer-invisibility-spec' (which is what
861 ;; `notmuch-show-headers-visible' and
862 ;; `notmuch-show-message-visible' do) is a no-op and has no
863 ;; effect. This caused threads with only matching messages to have
864 ;; those messages hidden initially because
865 ;; `buffer-invisibility-spec' stayed `t'.
867 ;; This needs to be set here (rather than just above the call to
868 ;; `notmuch-show-headers-visible') because some of the part
869 ;; rendering or body washing functions
870 ;; (e.g. `notmuch-wash-text/plain-citations') manipulate
871 ;; `buffer-invisibility-spec').
872 (when (eq buffer-invisibility-spec t)
873 (setq buffer-invisibility-spec nil))
875 (setq message-start (point-marker))
877 (notmuch-show-insert-headerline headers
878 (or (if notmuch-show-relative-dates
879 (plist-get msg :date_relative)
881 (plist-get headers :Date))
882 (plist-get msg :tags) depth)
884 (setq content-start (point-marker))
886 (plist-put msg :headers-invis-spec headers-invis-spec)
887 (plist-put msg :message-invis-spec message-invis-spec)
889 ;; Set `headers-start' to point after the 'Subject:' header to be
890 ;; compatible with the existing implementation. This just sets it
891 ;; to after the first header.
892 (notmuch-show-insert-headers headers)
894 (goto-char content-start)
895 ;; If the subject of this message is the same as that of the
896 ;; previous message, don't display it when this message is
898 (when (not (string= notmuch-show-previous-subject
901 (setq headers-start (point-marker)))
902 (setq headers-end (point-marker))
904 (setq notmuch-show-previous-subject bare-subject)
906 (setq body-start (point-marker))
907 ;; A blank line between the headers and the body.
909 (notmuch-show-insert-body msg (plist-get msg :body)
910 (if notmuch-show-indent-content depth 0))
911 ;; Ensure that the body ends with a newline.
914 (setq body-end (point-marker))
915 (setq content-end (point-marker))
917 ;; Indent according to the depth in the thread.
918 (if notmuch-show-indent-content
919 (indent-rigidly content-start content-end (* notmuch-show-indent-messages-width depth)))
921 (setq message-end (point-max-marker))
923 ;; Save the extents of this message over the whole text of the
925 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
927 (let ((headers-overlay (make-overlay headers-start headers-end))
928 (invis-specs (list headers-invis-spec message-invis-spec)))
929 (overlay-put headers-overlay 'invisible invis-specs)
930 (overlay-put headers-overlay 'priority 10))
931 (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
933 (plist-put msg :depth depth)
935 ;; Save the properties for this message. Currently this saves the
936 ;; entire message (augmented it with other stuff), which seems
937 ;; like overkill. We might save a reduced subset (for example, not
939 (notmuch-show-set-message-properties msg)
941 ;; Set header visibility.
942 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
944 ;; Message visibility depends on whether it matched the search
946 (notmuch-show-message-visible msg (plist-get msg :match))))
948 (defun notmuch-show-toggle-process-crypto ()
949 "Toggle the processing of cryptographic MIME parts."
951 (setq notmuch-show-process-crypto (not notmuch-show-process-crypto))
952 (message (if notmuch-show-process-crypto
953 "Processing cryptographic MIME parts."
954 "Not processing cryptographic MIME parts."))
955 (notmuch-show-refresh-view t))
957 (defun notmuch-show-toggle-elide-non-matching ()
958 "Toggle the display of non-matching messages."
960 (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages))
961 (message (if notmuch-show-elide-non-matching-messages
962 "Showing matching messages only."
963 "Showing all messages."))
964 (notmuch-show-refresh-view t))
966 (defun notmuch-show-toggle-thread-indentation ()
967 "Toggle the indentation of threads."
969 (setq notmuch-show-indent-content (not notmuch-show-indent-content))
970 (message (if notmuch-show-indent-content
971 "Content is indented."
972 "Content is not indented."))
973 (notmuch-show-refresh-view t))
975 (defun notmuch-show-insert-tree (tree depth)
976 "Insert the message tree TREE at depth DEPTH in the current thread."
977 (let ((msg (car tree))
978 (replies (cadr tree)))
979 (if (or (not notmuch-show-elide-non-matching-messages)
980 (plist-get msg :match))
981 (notmuch-show-insert-msg msg depth))
982 (notmuch-show-insert-thread replies (1+ depth))))
984 (defun notmuch-show-insert-thread (thread depth)
985 "Insert the thread THREAD at depth DEPTH in the current forest."
986 (mapc (lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
988 (defun notmuch-show-insert-forest (forest)
989 "Insert the forest of threads FOREST."
990 (mapc (lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
992 (defun notmuch-show-buttonise-links (start end)
993 "Buttonise URLs and mail addresses between START and END.
995 This also turns id:\"<message id>\"-parts into buttons for
996 a corresponding notmuch search."
997 (goto-address-fontify-region start end)
1000 (while (re-search-forward "id:\\(\"?\\)[^[:space:]\"]+\\1" end t)
1001 ;; remove the overlay created by goto-address-mode
1002 (remove-overlays (match-beginning 0) (match-end 0) 'goto-address t)
1003 (make-text-button (match-beginning 0) (match-end 0)
1004 'action `(lambda (arg)
1005 (notmuch-show ,(match-string-no-properties 0)))
1007 'help-echo "Mouse-1, RET: search for this message"
1008 'face goto-address-mail-face))))
1011 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
1012 "Run \"notmuch show\" with the given thread ID and display results.
1014 The optional PARENT-BUFFER is the notmuch-search buffer from
1015 which this notmuch-show command was executed, (so that the
1016 next thread from that buffer can be show when done with this
1019 The optional QUERY-CONTEXT is a notmuch search term. Only
1020 messages from the thread matching this search term are shown if
1023 The optional BUFFER-NAME provides the name of the buffer in
1024 which the message thread is shown. If it is nil (which occurs
1025 when the command is called interactively) the argument to the
1027 (interactive "sNotmuch show: ")
1028 (let ((buffer-name (generate-new-buffer-name
1030 (concat "*notmuch-" thread-id "*")))))
1031 (switch-to-buffer (get-buffer-create buffer-name))
1032 ;; Set the default value for `notmuch-show-process-crypto' in this
1034 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1036 (setq notmuch-show-thread-id thread-id
1037 notmuch-show-parent-buffer parent-buffer
1038 notmuch-show-query-context query-context)
1039 (notmuch-show-worker)))
1041 (defun notmuch-show-worker ()
1042 (let ((inhibit-read-only t))
1045 ;; Don't track undo information for this buffer
1046 (set 'buffer-undo-list t)
1049 (goto-char (point-min))
1051 (let* ((basic-args (list notmuch-show-thread-id))
1052 (args (if notmuch-show-query-context
1053 (append (list "\'") basic-args
1054 (list "and (" notmuch-show-query-context ")\'"))
1055 (append (list "\'") basic-args (list "\'")))))
1056 (notmuch-show-insert-forest (notmuch-query-get-threads args))
1057 ;; If the query context reduced the results to nothing, run
1059 (when (and (eq (buffer-size) 0)
1060 notmuch-show-query-context)
1061 (notmuch-show-insert-forest
1062 (notmuch-query-get-threads basic-args))))
1064 (jit-lock-register #'notmuch-show-buttonise-links)
1066 (run-hooks 'notmuch-show-hook))
1068 ;; Move straight to the first open message
1069 (unless (notmuch-show-message-visible-p)
1070 (notmuch-show-next-open-message))
1072 ;; Set the header line to the subject of the first open message.
1073 (setq header-line-format (notmuch-show-strip-re (notmuch-show-get-pretty-subject)))
1075 (notmuch-show-mark-read)))
1077 (defun notmuch-show-capture-state ()
1078 "Capture the state of the current buffer.
1081 - the list of open messages,
1082 - the current message."
1083 (list (notmuch-show-get-message-id) (notmuch-show-get-message-ids-for-open-messages)))
1085 (defun notmuch-show-apply-state (state)
1086 "Apply STATE to the current buffer.
1089 - opening the messages previously opened,
1090 - closing all other messages,
1091 - moving to the correct current message."
1092 (let ((current (car state))
1093 (open (cadr state)))
1095 ;; Open those that were open.
1096 (goto-char (point-min))
1097 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1098 (member (notmuch-show-get-message-id) open))
1099 until (not (notmuch-show-goto-message-next)))
1101 ;; Go to the previously open message.
1102 (goto-char (point-min))
1103 (unless (loop if (string= current (notmuch-show-get-message-id))
1105 until (not (notmuch-show-goto-message-next)))
1106 (goto-char (point-min))
1107 (message "Previously current message not found."))
1108 (notmuch-show-message-adjust)))
1110 (defun notmuch-show-refresh-view (&optional retain-state)
1111 "Refresh the current view.
1113 Refreshes the current view, observing changes in display
1114 preferences. If RETAIN-STATE is non-nil then the state of the
1115 buffer is stored and re-applied after the refresh."
1117 (let ((inhibit-read-only t)
1120 (setq state (notmuch-show-capture-state)))
1122 (notmuch-show-worker)
1124 (notmuch-show-apply-state state))))
1126 (defvar notmuch-show-stash-map
1127 (let ((map (make-sparse-keymap)))
1128 (define-key map "c" 'notmuch-show-stash-cc)
1129 (define-key map "d" 'notmuch-show-stash-date)
1130 (define-key map "F" 'notmuch-show-stash-filename)
1131 (define-key map "f" 'notmuch-show-stash-from)
1132 (define-key map "i" 'notmuch-show-stash-message-id)
1133 (define-key map "I" 'notmuch-show-stash-message-id-stripped)
1134 (define-key map "s" 'notmuch-show-stash-subject)
1135 (define-key map "T" 'notmuch-show-stash-tags)
1136 (define-key map "t" 'notmuch-show-stash-to)
1138 "Submap for stash commands")
1139 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
1141 (defvar notmuch-show-mode-map
1142 (let ((map (make-sparse-keymap)))
1143 (define-key map "?" 'notmuch-help)
1144 (define-key map "q" 'notmuch-kill-this-buffer)
1145 (define-key map (kbd "<C-tab>") 'widget-backward)
1146 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
1147 (define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
1148 (define-key map (kbd "TAB") 'notmuch-show-next-button)
1149 (define-key map "s" 'notmuch-search)
1150 (define-key map "m" 'notmuch-mua-new-mail)
1151 (define-key map "f" 'notmuch-show-forward-message)
1152 (define-key map "r" 'notmuch-show-reply-sender)
1153 (define-key map "R" 'notmuch-show-reply)
1154 (define-key map "|" 'notmuch-show-pipe-message)
1155 (define-key map "w" 'notmuch-show-save-attachments)
1156 (define-key map "V" 'notmuch-show-view-raw-message)
1157 (define-key map "v" 'notmuch-show-view-all-mime-parts)
1158 (define-key map "c" 'notmuch-show-stash-map)
1159 (define-key map "=" 'notmuch-show-refresh-view)
1160 (define-key map "h" 'notmuch-show-toggle-headers)
1161 (define-key map "*" 'notmuch-show-tag-all)
1162 (define-key map "-" 'notmuch-show-remove-tag)
1163 (define-key map "+" 'notmuch-show-add-tag)
1164 (define-key map "X" 'notmuch-show-archive-thread-then-exit)
1165 (define-key map "x" 'notmuch-show-archive-message-then-next-or-exit)
1166 (define-key map "A" 'notmuch-show-archive-thread-then-next)
1167 (define-key map "a" 'notmuch-show-archive-message-then-next-or-next-thread)
1168 (define-key map "N" 'notmuch-show-next-message)
1169 (define-key map "P" 'notmuch-show-previous-message)
1170 (define-key map "n" 'notmuch-show-next-open-message)
1171 (define-key map "p" 'notmuch-show-previous-open-message)
1172 (define-key map (kbd "DEL") 'notmuch-show-rewind)
1173 (define-key map " " 'notmuch-show-advance-and-archive)
1174 (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
1175 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
1176 (define-key map "#" 'notmuch-show-print-message)
1177 (define-key map "!" 'notmuch-show-toggle-elide-non-matching)
1178 (define-key map "$" 'notmuch-show-toggle-process-crypto)
1179 (define-key map "<" 'notmuch-show-toggle-thread-indentation)
1180 (define-key map "t" 'toggle-truncate-lines)
1182 "Keymap for \"notmuch show\" buffers.")
1183 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
1185 (defun notmuch-show-mode ()
1186 "Major mode for viewing a thread with notmuch.
1188 This buffer contains the results of the \"notmuch show\" command
1189 for displaying a single thread of email from your email archives.
1191 By default, various components of email messages, (citations,
1192 signatures, already-read messages), are hidden. You can make
1193 these parts visible by clicking with the mouse button or by
1194 pressing RET after positioning the cursor on a hidden part, (for
1195 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
1197 Reading the thread sequentially is well-supported by pressing
1198 \\[notmuch-show-advance-and-archive]. This will scroll the current message (if necessary), advance
1199 to the next message, or advance to the next thread (if already on
1200 the last message of a thread).
1202 Other commands are available to read or manipulate the thread
1203 more selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages
1204 without removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread
1205 without scrolling through with \\[notmuch-show-advance-and-archive]).
1207 You can add or remove arbitrary tags from the current message with
1208 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
1210 All currently available key bindings:
1212 \\{notmuch-show-mode-map}"
1214 (kill-all-local-variables)
1215 (use-local-map notmuch-show-mode-map)
1216 (setq major-mode 'notmuch-show-mode
1217 mode-name "notmuch-show")
1218 (setq buffer-read-only t
1221 (defun notmuch-show-move-to-message-top ()
1222 (goto-char (notmuch-show-message-top)))
1224 (defun notmuch-show-move-to-message-bottom ()
1225 (goto-char (notmuch-show-message-bottom)))
1227 (defun notmuch-show-message-adjust ()
1230 ;; Movement related functions.
1232 ;; There's some strangeness here where a text property applied to a
1233 ;; region a->b is not found when point is at b. We walk backwards
1234 ;; until finding the property.
1235 (defun notmuch-show-message-extent ()
1238 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
1242 (defun notmuch-show-message-top ()
1243 (car (notmuch-show-message-extent)))
1245 (defun notmuch-show-message-bottom ()
1246 (cdr (notmuch-show-message-extent)))
1248 (defun notmuch-show-goto-message-next ()
1249 (let ((start (point)))
1250 (notmuch-show-move-to-message-bottom)
1256 (defun notmuch-show-goto-message-previous ()
1257 (notmuch-show-move-to-message-top)
1261 (notmuch-show-move-to-message-top)
1264 (defun notmuch-show-mapc (function)
1265 "Iterate through all messages in the current thread with
1266 `notmuch-show-goto-message-next' and call FUNCTION for side
1269 (goto-char (point-min))
1270 (loop do (funcall function)
1271 while (notmuch-show-goto-message-next))))
1273 ;; Functions relating to the visibility of messages and their
1276 (defun notmuch-show-element-visible (props visible-p spec-property)
1277 (let ((spec (plist-get props spec-property)))
1279 (remove-from-invisibility-spec spec)
1280 (add-to-invisibility-spec spec))))
1282 (defun notmuch-show-message-visible (props visible-p)
1283 (notmuch-show-element-visible props visible-p :message-invis-spec)
1284 (notmuch-show-set-prop :message-visible visible-p props))
1286 (defun notmuch-show-headers-visible (props visible-p)
1287 (notmuch-show-element-visible props visible-p :headers-invis-spec)
1288 (notmuch-show-set-prop :headers-visible visible-p props))
1290 ;; Functions for setting and getting attributes of the current
1293 (defun notmuch-show-set-message-properties (props)
1295 (notmuch-show-move-to-message-top)
1296 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
1298 (defun notmuch-show-get-message-properties ()
1299 "Return the properties of the current message as a plist.
1301 Some useful entries are:
1302 :headers - Property list containing the headers :Date, :Subject, :From, etc.
1303 :body - Body of the message
1304 :tags - Tags for this message"
1306 (notmuch-show-move-to-message-top)
1307 (get-text-property (point) :notmuch-message-properties)))
1309 (defun notmuch-show-set-prop (prop val &optional props)
1310 (let ((inhibit-read-only t)
1312 (notmuch-show-get-message-properties))))
1313 (plist-put props prop val)
1314 (notmuch-show-set-message-properties props)))
1316 (defun notmuch-show-get-prop (prop &optional props)
1317 (let ((props (or props
1318 (notmuch-show-get-message-properties))))
1319 (plist-get props prop)))
1321 (defun notmuch-show-get-message-id ()
1322 "Return the message id of the current message."
1323 (concat "id:\"" (notmuch-show-get-prop :id) "\""))
1325 (defun notmuch-show-get-messages-ids ()
1326 "Return all message ids of messages in the current thread."
1327 (let ((message-ids))
1329 (lambda () (push (notmuch-show-get-message-id) message-ids)))
1332 (defun notmuch-show-get-messages-ids-search ()
1333 "Return a search string for all message ids of messages in the
1335 (mapconcat 'identity (notmuch-show-get-messages-ids) " or "))
1337 ;; dme: Would it make sense to use a macro for many of these?
1339 (defun notmuch-show-get-filename ()
1340 "Return the filename of the current message."
1341 (notmuch-show-get-prop :filename))
1343 (defun notmuch-show-get-header (header &optional props)
1344 "Return the named header of the current message, if any."
1345 (plist-get (notmuch-show-get-prop :headers props) header))
1347 (defun notmuch-show-get-cc ()
1348 (notmuch-show-get-header :Cc))
1350 (defun notmuch-show-get-date ()
1351 (notmuch-show-get-header :Date))
1353 (defun notmuch-show-get-from ()
1354 (notmuch-show-get-header :From))
1356 (defun notmuch-show-get-subject ()
1357 (notmuch-show-get-header :Subject))
1359 (defun notmuch-show-get-to ()
1360 (notmuch-show-get-header :To))
1362 (defun notmuch-show-get-depth ()
1363 (notmuch-show-get-prop :depth))
1365 (defun notmuch-show-get-pretty-subject ()
1366 (notmuch-prettify-subject (notmuch-show-get-subject)))
1368 (defun notmuch-show-set-tags (tags)
1369 "Set the tags of the current message."
1370 (notmuch-show-set-prop :tags tags)
1371 (notmuch-show-update-tags tags))
1373 (defun notmuch-show-get-tags ()
1374 "Return the tags of the current message."
1375 (notmuch-show-get-prop :tags))
1377 (defun notmuch-show-message-visible-p ()
1378 "Is the current message visible?"
1379 (notmuch-show-get-prop :message-visible))
1381 (defun notmuch-show-headers-visible-p ()
1382 "Are the headers of the current message visible?"
1383 (notmuch-show-get-prop :headers-visible))
1385 (defun notmuch-show-mark-read ()
1386 "Mark the current message as read."
1387 (notmuch-show-tag-message "-unread"))
1389 ;; Functions for getting attributes of several messages in the current
1392 (defun notmuch-show-get-message-ids-for-open-messages ()
1393 "Return a list of all message IDs for open messages in the current thread."
1395 (let (message-ids done)
1396 (goto-char (point-min))
1398 (if (notmuch-show-message-visible-p)
1399 (setq message-ids (append message-ids (list (notmuch-show-get-message-id)))))
1400 (setq done (not (notmuch-show-goto-message-next)))
1405 ;; Commands typically bound to keys.
1407 (defun notmuch-show-advance ()
1408 "Advance through thread.
1410 If the current message in the thread is not yet fully visible,
1411 scroll by a near screenful to read more of the message.
1413 Otherwise, (the end of the current message is already within the
1414 current window), advance to the next open message."
1416 (let* ((end-of-this-message (notmuch-show-message-bottom))
1417 (visible-end-of-this-message (1- end-of-this-message))
1419 (while (invisible-p visible-end-of-this-message)
1420 (setq visible-end-of-this-message
1422 (1- (previous-single-char-property-change
1423 visible-end-of-this-message 'invisible)))))
1425 ;; Ideally we would test `end-of-this-message' against the result
1426 ;; of `window-end', but that doesn't account for the fact that
1427 ;; the end of the message might be hidden.
1428 ((and visible-end-of-this-message
1429 (> visible-end-of-this-message (window-end)))
1430 ;; The bottom of this message is not visible - scroll.
1433 ((not (= end-of-this-message (point-max)))
1434 ;; This is not the last message - move to the next visible one.
1435 (notmuch-show-next-open-message))
1438 ;; This is the last message - change the return value
1442 (defun notmuch-show-advance-and-archive ()
1443 "Advance through thread and archive.
1445 This command is intended to be one of the simplest ways to
1446 process a thread of email. It works exactly like
1447 notmuch-show-advance, in that it scrolls through messages in a
1448 show buffer, except that when it gets to the end of the buffer it
1449 archives the entire current thread, (remove the \"inbox\" tag
1450 from each message), kills the buffer, and displays the next
1451 thread from the search from which this thread was originally
1454 (if (notmuch-show-advance)
1455 (notmuch-show-archive-thread-then-next)))
1457 (defun notmuch-show-rewind ()
1458 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
1460 Specifically, if the beginning of the previous email is fewer
1461 than `window-height' lines from the current point, move to it
1462 just like `notmuch-show-previous-message'.
1464 Otherwise, just scroll down a screenful of the current message.
1466 This command does not modify any message tags, (it does not undo
1467 any effects from previous calls to
1468 `notmuch-show-advance-and-archive'."
1470 (let ((start-of-message (notmuch-show-message-top))
1471 (start-of-window (window-start)))
1473 ;; Either this message is properly aligned with the start of the
1474 ;; window or the start of this message is not visible on the
1476 ((or (= start-of-message start-of-window)
1477 (< start-of-message start-of-window))
1479 ;; If a small number of lines from the previous message are
1480 ;; visible, realign so that the top of the current message is at
1481 ;; the top of the screen.
1482 (when (<= (count-screen-lines (window-start) start-of-message)
1483 next-screen-context-lines)
1484 (goto-char (notmuch-show-message-top))
1485 (notmuch-show-message-adjust))
1486 ;; Move to the top left of the window.
1487 (goto-char (window-start)))
1489 ;; Move to the previous message.
1490 (notmuch-show-previous-message)))))
1492 (defun notmuch-show-reply (&optional prompt-for-sender)
1493 "Reply to the sender and all recipients of the current message."
1495 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender t))
1497 (defun notmuch-show-reply-sender (&optional prompt-for-sender)
1498 "Reply to the sender of the current message."
1500 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender nil))
1502 (defun notmuch-show-forward-message (&optional prompt-for-sender)
1503 "Forward the current message."
1505 (with-current-notmuch-show-message
1506 (notmuch-mua-new-forward-message prompt-for-sender)))
1508 (defun notmuch-show-next-message (&optional pop-at-end)
1509 "Show the next message.
1511 If a prefix argument is given and this is the last message in the
1512 thread, navigate to the next thread in the parent search buffer."
1514 (if (notmuch-show-goto-message-next)
1516 (notmuch-show-mark-read)
1517 (notmuch-show-message-adjust))
1519 (notmuch-show-next-thread)
1520 (goto-char (point-max)))))
1522 (defun notmuch-show-previous-message ()
1523 "Show the previous message."
1525 (notmuch-show-goto-message-previous)
1526 (notmuch-show-mark-read)
1527 (notmuch-show-message-adjust))
1529 (defun notmuch-show-next-open-message (&optional pop-at-end)
1530 "Show the next open message.
1532 If a prefix argument is given and this is the last open message
1533 in the thread, navigate to the next thread in the parent search
1534 buffer. Return t if there was a next open message in the thread
1535 to show, nil otherwise."
1538 (while (and (setq r (notmuch-show-goto-message-next))
1539 (not (notmuch-show-message-visible-p))))
1542 (notmuch-show-mark-read)
1543 (notmuch-show-message-adjust))
1545 (notmuch-show-next-thread)
1546 (goto-char (point-max))))
1549 (defun notmuch-show-previous-open-message ()
1550 "Show the previous open message."
1552 (while (and (notmuch-show-goto-message-previous)
1553 (not (notmuch-show-message-visible-p))))
1554 (notmuch-show-mark-read)
1555 (notmuch-show-message-adjust))
1557 (defun notmuch-show-view-raw-message ()
1558 "View the file holding the current message."
1560 (let* ((id (notmuch-show-get-message-id))
1561 (buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
1562 (call-process notmuch-command nil buf nil "show" "--format=raw" id)
1563 (switch-to-buffer buf)
1564 (goto-char (point-min))
1565 (set-buffer-modified-p nil)
1566 (view-buffer buf 'kill-buffer-if-not-modified)))
1568 (defun notmuch-show-pipe-message (entire-thread command)
1569 "Pipe the contents of the current message (or thread) to the given command.
1571 The given command will be executed with the raw contents of the
1572 current email message as stdin. Anything printed by the command
1573 to stdout or stderr will appear in the *notmuch-pipe* buffer.
1575 When invoked with a prefix argument, the command will receive all
1576 open messages in the current thread (formatted as an mbox) rather
1577 than only the current message."
1578 (interactive "P\nsPipe message to command: ")
1579 (let (shell-command)
1582 (concat notmuch-command " show --format=mbox "
1583 (shell-quote-argument
1584 (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
1587 (concat notmuch-command " show --format=raw "
1588 (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
1589 (let ((buf (get-buffer-create (concat "*notmuch-pipe*"))))
1590 (with-current-buffer buf
1591 (setq buffer-read-only nil)
1593 (let ((exit-code (call-process-shell-command shell-command nil buf)))
1594 (goto-char (point-max))
1595 (set-buffer-modified-p nil)
1596 (setq buffer-read-only t)
1597 (unless (zerop exit-code)
1598 (switch-to-buffer-other-window buf)
1599 (message (format "Command '%s' exited abnormally with code %d"
1600 shell-command exit-code))))))))
1602 (defun notmuch-show-tag-message (&rest tag-changes)
1603 "Change tags for the current message.
1605 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1606 (let* ((current-tags (notmuch-show-get-tags))
1607 (new-tags (notmuch-update-tags current-tags tag-changes)))
1608 (unless (equal current-tags new-tags)
1609 (apply 'notmuch-tag (notmuch-show-get-message-id) tag-changes)
1610 (notmuch-show-set-tags new-tags))))
1612 (defun notmuch-show-tag (&optional initial-input)
1613 "Change tags for the current message, read input from the minibuffer."
1615 (let ((tag-changes (notmuch-read-tag-changes
1616 initial-input (notmuch-show-get-message-id))))
1617 (apply 'notmuch-show-tag-message tag-changes)))
1619 (defun notmuch-show-tag-all (&rest tag-changes)
1620 "Change tags for all messages in the current thread.
1622 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1623 (interactive (notmuch-read-tag-changes nil notmuch-show-thread-id))
1624 (apply 'notmuch-tag (notmuch-show-get-messages-ids-search) tag-changes)
1627 (let* ((current-tags (notmuch-show-get-tags))
1628 (new-tags (notmuch-update-tags current-tags tag-changes)))
1629 (unless (equal current-tags new-tags)
1630 (notmuch-show-set-tags new-tags))))))
1632 (defun notmuch-show-add-tag ()
1633 "Same as `notmuch-show-tag' but sets initial input to '+'."
1635 (notmuch-show-tag "+"))
1637 (defun notmuch-show-remove-tag ()
1638 "Same as `notmuch-show-tag' but sets initial input to '-'."
1640 (notmuch-show-tag "-"))
1642 (defun notmuch-show-toggle-headers ()
1643 "Toggle the visibility of the current message headers."
1645 (let ((props (notmuch-show-get-message-properties)))
1646 (notmuch-show-headers-visible
1648 (not (plist-get props :headers-visible))))
1649 (force-window-update))
1651 (defun notmuch-show-toggle-message ()
1652 "Toggle the visibility of the current message."
1654 (let ((props (notmuch-show-get-message-properties)))
1655 (notmuch-show-message-visible
1657 (not (plist-get props :message-visible))))
1658 (force-window-update))
1660 (defun notmuch-show-open-or-close-all ()
1661 "Set the visibility all of the messages in the current thread.
1662 By default make all of the messages visible. With a prefix
1663 argument, hide all of the messages."
1666 (goto-char (point-min))
1667 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1668 (not current-prefix-arg))
1669 until (not (notmuch-show-goto-message-next))))
1670 (force-window-update))
1672 (defun notmuch-show-next-button ()
1673 "Advance point to the next button in the buffer."
1677 (defun notmuch-show-previous-button ()
1678 "Move point back to the previous button in the buffer."
1680 (backward-button 1))
1682 (defun notmuch-show-tag-thread-internal (tag &optional remove)
1683 "Add tag to the current set of messages.
1685 If the remove switch is given, tags will be removed instead of
1687 (goto-char (point-min))
1688 (let ((op (if remove "-" "+")))
1689 (loop do (notmuch-show-tag-message (concat op tag))
1690 until (not (notmuch-show-goto-message-next)))))
1692 (defun notmuch-show-add-tag-thread (tag)
1693 "Add tag to all messages in the current thread."
1695 (notmuch-show-tag-thread-internal tag))
1697 (defun notmuch-show-remove-tag-thread (tag)
1698 "Remove tag from all messages in the current thread."
1700 (notmuch-show-tag-thread-internal tag t))
1702 (defun notmuch-show-next-thread (&optional show-next)
1703 "Move to the next item in the search results, if any."
1705 (let ((parent-buffer notmuch-show-parent-buffer))
1706 (notmuch-kill-this-buffer)
1707 (when (buffer-live-p parent-buffer)
1708 (switch-to-buffer parent-buffer)
1709 (notmuch-search-next-thread)
1711 (notmuch-search-show-thread)))))
1713 (defun notmuch-show-archive-thread (&optional unarchive)
1714 "Archive each message in thread.
1716 If a prefix argument is given, the messages will be
1717 \"unarchived\" (ie. the \"inbox\" tag will be added instead of
1720 Archive each message currently shown by removing the \"inbox\"
1721 tag from each. Then kill this buffer and show the next thread
1722 from the search from which this thread was originally shown.
1724 Note: This command is safe from any race condition of new messages
1725 being delivered to the same thread. It does not archive the
1726 entire thread, but only the messages shown in the current
1730 (notmuch-show-add-tag-thread "inbox")
1731 (notmuch-show-remove-tag-thread "inbox")))
1733 (defun notmuch-show-archive-thread-then-next ()
1734 "Archive each message in thread, then show next thread from search."
1736 (notmuch-show-archive-thread)
1737 (notmuch-show-next-thread t))
1739 (defun notmuch-show-archive-thread-then-exit ()
1740 "Archive each message in thread, then exit back to search results."
1742 (notmuch-show-archive-thread)
1743 (notmuch-show-next-thread))
1745 (defun notmuch-show-archive-message (&optional unarchive)
1746 "Archive the current message.
1748 If a prefix argument is given, the message will be
1749 \"unarchived\" (ie. the \"inbox\" tag will be added instead of
1752 (let ((op (if unarchive "+" "-")))
1753 (notmuch-show-tag-message (concat op "inbox"))))
1755 (defun notmuch-show-archive-message-then-next-or-exit ()
1756 "Archive the current message, then show the next open message in the current thread.
1758 If at the last open message in the current thread, then exit back
1761 (notmuch-show-archive-message)
1762 (notmuch-show-next-open-message t))
1764 (defun notmuch-show-archive-message-then-next-or-next-thread ()
1765 "Archive the current message, then show the next open message in the current thread.
1767 If at the last open message in the current thread, then show next
1768 thread from search."
1770 (notmuch-show-archive-message)
1771 (unless (notmuch-show-next-open-message)
1772 (notmuch-show-next-thread t)))
1774 (defun notmuch-show-stash-cc ()
1775 "Copy CC field of current message to kill-ring."
1777 (notmuch-common-do-stash (notmuch-show-get-cc)))
1779 (defun notmuch-show-stash-date ()
1780 "Copy date of current message to kill-ring."
1782 (notmuch-common-do-stash (notmuch-show-get-date)))
1784 (defun notmuch-show-stash-filename ()
1785 "Copy filename of current message to kill-ring."
1787 (notmuch-common-do-stash (notmuch-show-get-filename)))
1789 (defun notmuch-show-stash-from ()
1790 "Copy From address of current message to kill-ring."
1792 (notmuch-common-do-stash (notmuch-show-get-from)))
1794 (defun notmuch-show-stash-message-id ()
1795 "Copy message ID of current message to kill-ring."
1797 (notmuch-common-do-stash (notmuch-show-get-message-id)))
1799 (defun notmuch-show-stash-message-id-stripped ()
1800 "Copy message ID of current message (sans `id:' prefix) to kill-ring."
1802 (notmuch-common-do-stash (substring (notmuch-show-get-message-id) 4 -1)))
1804 (defun notmuch-show-stash-subject ()
1805 "Copy Subject field of current message to kill-ring."
1807 (notmuch-common-do-stash (notmuch-show-get-subject)))
1809 (defun notmuch-show-stash-tags ()
1810 "Copy tags of current message to kill-ring as a comma separated list."
1812 (notmuch-common-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1814 (defun notmuch-show-stash-to ()
1815 "Copy To address of current message to kill-ring."
1817 (notmuch-common-do-stash (notmuch-show-get-to)))
1819 ;; Commands typically bound to buttons.
1821 (defun notmuch-show-part-button-default (&optional button)
1823 (notmuch-show-part-button-internal button notmuch-show-part-button-default-action))
1825 (defun notmuch-show-part-button-save (&optional button)
1827 (notmuch-show-part-button-internal button #'notmuch-show-save-part))
1829 (defun notmuch-show-part-button-view (&optional button)
1831 (notmuch-show-part-button-internal button #'notmuch-show-view-part))
1833 (defun notmuch-show-part-button-interactively-view (&optional button)
1835 (notmuch-show-part-button-internal button #'notmuch-show-interactively-view-part))
1837 (defun notmuch-show-part-button-internal (button handler)
1838 (let ((button (or button (button-at (point)))))
1840 (let ((nth (button-get button :notmuch-part)))
1842 (funcall handler (notmuch-show-get-message-id) nth
1843 (button-get button :notmuch-filename)
1844 (button-get button :notmuch-content-type)))))))
1848 (provide 'notmuch-show)