1 ;; notmuch-show.el --- displaying notmuch forests.
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
6 ;; This file is part of Notmuch.
8 ;; Notmuch is free software: you can redistribute it and/or modify it
9 ;; under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; Notmuch is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;; General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with Notmuch. If not, see <http://www.gnu.org/licenses/>.
21 ;; Authors: Carl Worth <cworth@cworth.org>
22 ;; David Edmondson <dme@dme.org>
24 (eval-when-compile (require 'cl))
32 (require 'notmuch-lib)
33 (require 'notmuch-tag)
34 (require 'notmuch-query)
35 (require 'notmuch-wash)
36 (require 'notmuch-mua)
37 (require 'notmuch-crypto)
38 (require 'notmuch-print)
40 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
41 (declare-function notmuch-search-next-thread "notmuch" nil)
42 (declare-function notmuch-search-previous-thread "notmuch" nil)
43 (declare-function notmuch-search-show-thread "notmuch" nil)
44 (declare-function notmuch-foreach-mime-part "notmuch" (function mm-handle))
45 (declare-function notmuch-count-attachments "notmuch" (mm-handle))
46 (declare-function notmuch-save-attachments "notmuch" (mm-handle &optional queryp))
47 (declare-function notmuch-tree "notmuch-tree"
48 (&optional query query-context target buffer-name open-target))
49 (declare-function notmuch-tree-get-message-properties "notmuch-tree" nil)
51 (defcustom notmuch-message-headers '("Subject" "To" "Cc" "Date")
52 "Headers that should be shown in a message, in this order.
54 For an open message, all of these headers will be made visible
55 according to `notmuch-message-headers-visible' or can be toggled
56 with `notmuch-show-toggle-visibility-headers'. For a closed message,
57 only the first header in the list will be visible."
58 :type '(repeat string)
61 (defcustom notmuch-message-headers-visible t
62 "Should the headers be visible by default?
64 If this value is non-nil, then all of the headers defined in
65 `notmuch-message-headers' will be visible by default in the display
66 of each message. Otherwise, these headers will be hidden and
67 `notmuch-show-toggle-visibility-headers' can be used to make them
68 visible for any given message."
72 (defcustom notmuch-show-relative-dates t
73 "Display relative dates in the message summary line."
77 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
78 "A list of functions called to decorate the headers listed in
79 `notmuch-message-headers'.")
81 (defcustom notmuch-show-hook '(notmuch-show-turn-on-visual-line-mode)
82 "Functions called after populating a `notmuch-show' buffer."
84 :options '(notmuch-show-turn-on-visual-line-mode)
86 :group 'notmuch-hooks)
88 (defcustom notmuch-show-insert-text/plain-hook '(notmuch-wash-wrap-long-lines
89 notmuch-wash-tidy-citations
90 notmuch-wash-elide-blank-lines
91 notmuch-wash-excerpt-citations)
92 "Functions used to improve the display of text/plain parts."
94 :options '(notmuch-wash-convert-inline-patch-to-part
95 notmuch-wash-wrap-long-lines
96 notmuch-wash-tidy-citations
97 notmuch-wash-elide-blank-lines
98 notmuch-wash-excerpt-citations)
100 :group 'notmuch-hooks)
102 ;; Mostly useful for debugging.
103 (defcustom notmuch-show-all-multipart/alternative-parts nil
104 "Should all parts of multipart/alternative parts be shown?"
106 :group 'notmuch-show)
108 (defcustom notmuch-show-indent-messages-width 1
109 "Width of message indentation in threads.
111 Messages are shown indented according to their depth in a thread.
112 This variable determines the width of this indentation measured
113 in number of blanks. Defaults to `1', choose `0' to disable
116 :group 'notmuch-show)
118 (defcustom notmuch-show-indent-multipart nil
119 "Should the sub-parts of a multipart/* part be indented?"
120 ;; dme: Not sure which is a good default.
122 :group 'notmuch-show)
124 (defcustom notmuch-show-part-button-default-action 'notmuch-show-save-part
125 "Default part header button action (on ENTER or mouse click)."
127 :type '(choice (const :tag "Save part"
128 notmuch-show-save-part)
129 (const :tag "View part"
130 notmuch-show-view-part)
131 (const :tag "View interactively"
132 notmuch-show-interactively-view-part)))
134 (defcustom notmuch-show-only-matching-messages nil
135 "Only matching messages are shown by default."
137 :group 'notmuch-show)
139 (defvar notmuch-show-thread-id nil)
140 (make-variable-buffer-local 'notmuch-show-thread-id)
141 (put 'notmuch-show-thread-id 'permanent-local t)
143 (defvar notmuch-show-parent-buffer nil)
144 (make-variable-buffer-local 'notmuch-show-parent-buffer)
145 (put 'notmuch-show-parent-buffer 'permanent-local t)
147 (defvar notmuch-show-query-context nil)
148 (make-variable-buffer-local 'notmuch-show-query-context)
149 (put 'notmuch-show-query-context 'permanent-local t)
151 (defvar notmuch-show-process-crypto nil)
152 (make-variable-buffer-local 'notmuch-show-process-crypto)
153 (put 'notmuch-show-process-crypto 'permanent-local t)
155 (defvar notmuch-show-elide-non-matching-messages nil)
156 (make-variable-buffer-local 'notmuch-show-elide-non-matching-messages)
157 (put 'notmuch-show-elide-non-matching-messages 'permanent-local t)
159 (defvar notmuch-show-indent-content t)
160 (make-variable-buffer-local 'notmuch-show-indent-content)
161 (put 'notmuch-show-indent-content 'permanent-local t)
163 (defvar notmuch-show-attachment-debug nil
164 "If t log stdout and stderr from attachment handlers
166 When set to nil (the default) stdout and stderr from attachment
167 handlers is discarded. When set to t the stdout and stderr from
168 each attachment handler is logged in buffers with names beginning
169 \" *notmuch-part*\". This option requires emacs version at least
172 (defcustom notmuch-show-stash-mlarchive-link-alist
173 '(("Gmane" . "http://mid.gmane.org/")
174 ("MARC" . "http://marc.info/?i=")
175 ("Mail Archive, The" . "http://mid.mail-archive.com/")
176 ("LKML" . "http://lkml.kernel.org/r/")
177 ;; FIXME: can these services be searched by `Message-Id' ?
178 ;; ("MarkMail" . "http://markmail.org/")
179 ;; ("Nabble" . "http://nabble.com/")
180 ;; ("opensubscriber" . "http://opensubscriber.com/")
182 "List of Mailing List Archives to use when stashing links.
184 This list is used for generating a Mailing List Archive reference
185 URI with the current message's Message-Id in
186 `notmuch-show-stash-mlarchive-link'.
188 If the cdr of the alist element is not a function, the cdr is
189 expected to contain a URI that is concatenated with the current
190 message's Message-Id to create a ML archive reference URI.
192 If the cdr is a function, the function is called with the
193 Message-Id as the argument, and the function is expected to
194 return the ML archive reference URI."
195 :type '(alist :key-type (string :tag "Name")
198 (function :tag "Function returning the URL")))
199 :group 'notmuch-show)
201 (defcustom notmuch-show-stash-mlarchive-link-default "Gmane"
202 "Default Mailing List Archive to use when stashing links.
204 This is used when `notmuch-show-stash-mlarchive-link' isn't
205 provided with an MLA argument nor `completing-read' input."
209 (list 'const :tag (car mla) :value (car mla)))
210 notmuch-show-stash-mlarchive-link-alist))
211 :group 'notmuch-show)
213 (defcustom notmuch-show-mark-read-tags '("-unread")
214 "List of tag changes to apply to a message when it is marked as read.
216 Tags starting with \"+\" (or not starting with either \"+\" or
217 \"-\") in the list will be added, and tags starting with \"-\"
218 will be removed from the message being marked as read.
220 For example, if you wanted to remove an \"unread\" tag and add a
221 \"read\" tag (which would make little sense), you would set:
222 (\"-unread\" \"+read\")"
223 :type '(repeat string)
224 :group 'notmuch-show)
226 (defcustom notmuch-show-mark-read-function #'notmuch-show-seen-current-message
227 "Function to control which messages are marked read.
229 The function should take two arguments START and END which will
230 be the start and end of the visible portion of the buffer and
231 should mark the appropriate messages read by applying
232 `notmuch-show-mark-read'. This function will be called after
233 every user interaction with notmuch."
235 :group 'notmuch-show)
237 (defmacro with-current-notmuch-show-message (&rest body)
238 "Evaluate body with current buffer set to the text of current message"
240 (let ((id (notmuch-show-get-message-id)))
241 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
242 (with-current-buffer buf
243 (let ((coding-system-for-read 'no-conversion))
244 (call-process notmuch-command nil t nil "show" "--format=raw" id))
246 (kill-buffer buf)))))
248 (defun notmuch-show-turn-on-visual-line-mode ()
249 "Enable Visual Line mode."
250 (visual-line-mode t))
252 ;; DEPRECATED in Notmuch 0.16 since we now have convenient part
253 ;; commands. We'll keep the command around for a version or two in
254 ;; case people want to bind it themselves.
255 (defun notmuch-show-view-all-mime-parts ()
256 "Use external viewers to view all attachments from the current message."
258 (with-current-notmuch-show-message
259 ;; We override the mm-inline-media-tests to indicate which message
260 ;; parts are already sufficiently handled by the original
261 ;; presentation of the message in notmuch-show mode. These parts
262 ;; will be inserted directly into the temporary buffer of
263 ;; with-current-notmuch-show-message and silently discarded.
265 ;; Any MIME part not explicitly mentioned here will be handled by an
266 ;; external viewer as configured in the various mailcap files.
267 (let ((mm-inline-media-tests '(
268 ("text/.*" ignore identity)
269 ("application/pgp-signature" ignore identity)
270 ("multipart/alternative" ignore identity)
271 ("multipart/mixed" ignore identity)
272 ("multipart/related" ignore identity)
274 (mm-display-parts (mm-dissect-buffer)))))
276 (defun notmuch-show-save-attachments ()
277 "Save all attachments from the current message."
279 (with-current-notmuch-show-message
280 (let ((mm-handle (mm-dissect-buffer)))
281 (notmuch-save-attachments
282 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
285 (defun notmuch-show-with-message-as-text (fn)
286 "Apply FN to a text representation of the current message.
288 FN is called with one argument, the message properties. It should
289 operation on the contents of the current buffer."
291 ;; Remake the header to ensure that all information is available.
292 (let* ((to (notmuch-show-get-to))
293 (cc (notmuch-show-get-cc))
294 (from (notmuch-show-get-from))
295 (subject (notmuch-show-get-subject))
296 (date (notmuch-show-get-date))
297 (tags (notmuch-show-get-tags))
298 (depth (notmuch-show-get-depth))
301 "Subject: " subject "\n"
303 (if (not (string= cc ""))
304 (concat "Cc: " cc "\n")
310 (mapconcat #'identity tags ", ") "\n")
312 (all (buffer-substring (notmuch-show-message-top)
313 (notmuch-show-message-bottom)))
315 (props (notmuch-show-get-message-properties))
316 (indenting notmuch-show-indent-content))
320 (indent-rigidly (point-min) (point-max) (- depth)))
321 ;; Remove the original header.
322 (goto-char (point-min))
323 (re-search-forward "^$" (point-max) nil)
324 (delete-region (point-min) (point))
326 (funcall fn props))))
328 (defun notmuch-show-print-message ()
329 "Print the current message."
331 (notmuch-show-with-message-as-text 'notmuch-print-message))
333 (defun notmuch-show-fontify-header ()
335 ((looking-at "[Tt]o:")
337 ((looking-at "[Bb]?[Cc][Cc]:")
339 ((looking-at "[Ss]ubject:")
340 'message-header-subject)
341 ((looking-at "[Ff]rom:")
342 'message-header-from)
344 'message-header-other))))
346 (overlay-put (make-overlay (point) (re-search-forward ":"))
347 'face 'message-header-name)
348 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
351 (defun notmuch-show-colour-headers ()
352 "Apply some colouring to the current headers."
353 (goto-char (point-min))
354 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
355 (notmuch-show-fontify-header)
358 (defun notmuch-show-spaces-n (n)
359 "Return a string comprised of `n' spaces."
362 (defun notmuch-show-update-tags (tags)
363 "Update the displayed tags of the current message."
365 (goto-char (notmuch-show-message-top))
366 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
367 (let ((inhibit-read-only t))
368 (replace-match (concat "("
369 (notmuch-tag-format-tags tags (notmuch-show-get-prop :orig-tags))
372 (defun notmuch-clean-address (address)
373 "Try to clean a single email ADDRESS for display. Return a cons
374 cell of (AUTHOR_EMAIL AUTHOR_NAME). Return (ADDRESS nil) if
377 (let (p-name p-address)
378 ;; It would be convenient to use `mail-header-parse-address',
379 ;; but that expects un-decoded mailbox parts, whereas our
380 ;; mailbox parts are already decoded (and hence may contain
381 ;; UTF-8). Given that notmuch should handle most of the awkward
382 ;; cases, some simple string deconstruction should be sufficient
385 ;; "User <user@dom.ain>" style.
386 ((string-match "\\(.*\\) <\\(.*\\)>" address)
387 (setq p-name (match-string 1 address)
388 p-address (match-string 2 address)))
390 ;; "<user@dom.ain>" style.
391 ((string-match "<\\(.*\\)>" address)
392 (setq p-address (match-string 1 address)))
396 (setq p-address address)))
399 ;; Remove elements of the mailbox part that are not relevant for
400 ;; display, even if they are required during transport:
403 (setq p-name (replace-regexp-in-string "\\\\" "" p-name))
405 ;; Outer single and double quotes, which might be nested.
408 do (setq start-of-loop p-name)
410 when (string-match "^\"\\(.*\\)\"$" p-name)
411 do (setq p-name (match-string 1 p-name))
413 when (string-match "^'\\(.*\\)'$" p-name)
414 do (setq p-name (match-string 1 p-name))
416 until (string= start-of-loop p-name)))
418 ;; If the address is 'foo@bar.com <foo@bar.com>' then show just
420 (when (string= p-name p-address)
423 (cons p-address p-name))
424 (error (cons address nil))))
426 (defun notmuch-show-clean-address (address)
427 "Try to clean a single email ADDRESS for display. Return
428 unchanged ADDRESS if parsing fails."
429 (let* ((clean-address (notmuch-clean-address address))
430 (p-address (car clean-address))
431 (p-name (cdr clean-address)))
432 ;; If no name, return just the address.
435 ;; Otherwise format the name and address together.
436 (concat p-name " <" p-address ">"))))
438 (defun notmuch-show-insert-headerline (headers date tags depth)
439 "Insert a notmuch style headerline based on HEADERS for a
440 message at DEPTH in the current thread."
441 (let ((start (point)))
442 (insert (notmuch-show-spaces-n (* notmuch-show-indent-messages-width depth))
444 (notmuch-show-clean-address (plist-get headers :From)))
448 (notmuch-tag-format-tags tags tags)
450 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
452 (defun notmuch-show-insert-header (header header-value)
453 "Insert a single header."
454 (insert header ": " (notmuch-sanitize header-value) "\n"))
456 (defun notmuch-show-insert-headers (headers)
457 "Insert the headers of the current message."
458 (let ((start (point)))
459 (mapc (lambda (header)
460 (let* ((header-symbol (intern (concat ":" header)))
461 (header-value (plist-get headers header-symbol)))
462 (if (and header-value
463 (not (string-equal "" header-value)))
464 (notmuch-show-insert-header header header-value))))
465 notmuch-message-headers)
468 (narrow-to-region start (point-max))
469 (run-hooks 'notmuch-show-markup-headers-hook)))))
471 (define-button-type 'notmuch-show-part-button-type
472 'action 'notmuch-show-part-button-default
475 :supertype 'notmuch-button-type)
477 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name comment)
479 (base-label (concat (when name (concat name ": "))
481 (unless (string-equal declared-type content-type)
482 (concat " (as " content-type ")"))
487 (concat "[ " base-label " ]")
488 :base-label base-label
489 :type 'notmuch-show-part-button-type
490 :notmuch-part-hidden nil))
495 (defun notmuch-show-toggle-part-invisibility (&optional button)
497 (let* ((button (or button (button-at (point))))
498 (overlay (button-get button 'overlay))
499 (lazy-part (button-get button :notmuch-lazy-part)))
500 ;; We have a part to toggle if there is an overlay or if there is a lazy part.
501 ;; If neither is present we cannot toggle the part so we just return nil.
502 (when (or overlay lazy-part)
503 (let* ((show (button-get button :notmuch-part-hidden))
504 (new-start (button-start button))
505 (button-label (button-get button :base-label))
507 (properties (text-properties-at (button-start button)))
508 (inhibit-read-only t))
509 ;; Toggle the button itself.
510 (button-put button :notmuch-part-hidden (not show))
511 (goto-char new-start)
512 (insert "[ " button-label (if show " ]" " (hidden) ]"))
513 (set-text-properties new-start (point) properties)
514 (let ((old-end (button-end button)))
515 (move-overlay button new-start (point))
516 (delete-region (point) old-end))
517 (goto-char (min old-point (1- (button-end button))))
518 ;; Return nil if there is a lazy-part, it is empty, and we are
519 ;; trying to show it. In all other cases return t.
522 (button-put button :notmuch-lazy-part nil)
523 (notmuch-show-lazy-part lazy-part button))
524 ;; else there must be an overlay.
525 (overlay-put overlay 'invisible (not show))
528 ;; Part content ID handling
530 (defvar notmuch-show--cids nil
531 "Alist from raw content ID to (MSG PART).")
532 (make-variable-buffer-local 'notmuch-show--cids)
534 (defun notmuch-show--register-cids (msg part)
535 "Register content-IDs in PART and all of PART's sub-parts."
536 (let ((content-id (plist-get part :content-id)))
538 ;; Note that content-IDs are globally unique, except when they
539 ;; aren't: RFC 2046 section 5.1.4 permits children of a
540 ;; multipart/alternative to have the same content-ID, in which
541 ;; case the MUA is supposed to pick the best one it can render.
542 ;; We simply add the content-ID to the beginning of our alist;
543 ;; so if this happens, we'll take the last (and "best")
544 ;; alternative (even if we can't render it).
545 (push (list content-id msg part) notmuch-show--cids)))
546 ;; Recurse on sub-parts
547 (let ((ctype (notmuch-split-content-type
548 (downcase (plist-get part :content-type)))))
549 (cond ((equal (first ctype) "multipart")
550 (mapc (apply-partially #'notmuch-show--register-cids msg)
551 (plist-get part :content)))
552 ((equal ctype '("message" "rfc822"))
553 (notmuch-show--register-cids
555 (first (plist-get (first (plist-get part :content)) :body)))))))
557 (defun notmuch-show--get-cid-content (cid)
558 "Return a list (CID-content content-type) or nil.
560 This will only find parts from messages that have been inserted
561 into the current buffer. CID must be a raw content ID, without
562 enclosing angle brackets, a cid: prefix, or URL encoding. This
563 will return nil if the CID is unknown or cannot be retrieved."
564 (let ((descriptor (cdr (assoc cid notmuch-show--cids))))
566 (let* ((msg (first descriptor))
567 (part (second descriptor))
568 ;; Request caching for this content, as some messages
569 ;; reference the same cid: part many times (hundreds!).
570 (content (notmuch-get-bodypart-binary
571 msg part notmuch-show-process-crypto 'cache))
572 (content-type (plist-get part :content-type)))
573 (list content content-type)))))
575 (defun notmuch-show-setup-w3m ()
576 "Instruct w3m how to retrieve content from a \"related\" part of a message."
578 (if (boundp 'w3m-cid-retrieve-function-alist)
579 (unless (assq 'notmuch-show-mode w3m-cid-retrieve-function-alist)
580 (push (cons 'notmuch-show-mode #'notmuch-show--cid-w3m-retrieve)
581 w3m-cid-retrieve-function-alist)))
582 (setq mm-inline-text-html-with-images t))
584 (defvar w3m-current-buffer) ;; From `w3m.el'.
585 (defun notmuch-show--cid-w3m-retrieve (url &rest args)
586 ;; url includes the cid: prefix and is URL encoded (see RFC 2392).
587 (let* ((cid (url-unhex-string (substring url 4)))
589 (with-current-buffer w3m-current-buffer
590 (notmuch-show--get-cid-content cid))))
591 (when content-and-type
592 (insert (first content-and-type))
593 (second content-and-type))))
595 ;; MIME part renderers
597 (defun notmuch-show-multipart/*-to-list (part)
598 (mapcar (lambda (inner-part) (plist-get inner-part :content-type))
599 (plist-get part :content)))
601 (defun notmuch-show-insert-part-multipart/alternative (msg part content-type nth depth button)
602 (let ((chosen-type (car (notmuch-multipart/alternative-choose (notmuch-show-multipart/*-to-list part))))
603 (inner-parts (plist-get part :content))
605 ;; This inserts all parts of the chosen type rather than just one,
606 ;; but it's not clear that this is the wrong thing to do - which
607 ;; should be chosen if there are more than one that match?
608 (mapc (lambda (inner-part)
609 (let* ((inner-type (plist-get inner-part :content-type))
610 (hide (not (or notmuch-show-all-multipart/alternative-parts
611 (string= chosen-type inner-type)))))
612 (notmuch-show-insert-bodypart msg inner-part depth hide)))
615 (when notmuch-show-indent-multipart
616 (indent-rigidly start (point) 1)))
619 (defun notmuch-show-insert-part-multipart/related (msg part content-type nth depth button)
620 (let ((inner-parts (plist-get part :content))
623 ;; Render the primary part. FIXME: Support RFC 2387 Start header.
624 (notmuch-show-insert-bodypart msg (car inner-parts) depth)
625 ;; Add hidden buttons for the rest
626 (mapc (lambda (inner-part)
627 (notmuch-show-insert-bodypart msg inner-part depth t))
630 (when notmuch-show-indent-multipart
631 (indent-rigidly start (point) 1)))
634 (defun notmuch-show-insert-part-multipart/signed (msg part content-type nth depth button)
635 (button-put button 'face 'notmuch-crypto-part-header)
636 ;; add signature status button if sigstatus provided
637 (if (plist-member part :sigstatus)
638 (let* ((from (notmuch-show-get-header :From msg))
639 (sigstatus (car (plist-get part :sigstatus))))
640 (notmuch-crypto-insert-sigstatus-button sigstatus from))
641 ;; if we're not adding sigstatus, tell the user how they can get it
642 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts."))
644 (let ((inner-parts (plist-get part :content))
646 ;; Show all of the parts.
647 (mapc (lambda (inner-part)
648 (notmuch-show-insert-bodypart msg inner-part depth))
651 (when notmuch-show-indent-multipart
652 (indent-rigidly start (point) 1)))
655 (defun notmuch-show-insert-part-multipart/encrypted (msg part content-type nth depth button)
656 (button-put button 'face 'notmuch-crypto-part-header)
657 ;; add encryption status button if encstatus specified
658 (if (plist-member part :encstatus)
659 (let ((encstatus (car (plist-get part :encstatus))))
660 (notmuch-crypto-insert-encstatus-button encstatus)
661 ;; add signature status button if sigstatus specified
662 (if (plist-member part :sigstatus)
663 (let* ((from (notmuch-show-get-header :From msg))
664 (sigstatus (car (plist-get part :sigstatus))))
665 (notmuch-crypto-insert-sigstatus-button sigstatus from))))
666 ;; if we're not adding encstatus, tell the user how they can get it
667 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts."))
669 (let ((inner-parts (plist-get part :content))
671 ;; Show all of the parts.
672 (mapc (lambda (inner-part)
673 (notmuch-show-insert-bodypart msg inner-part depth))
676 (when notmuch-show-indent-multipart
677 (indent-rigidly start (point) 1)))
680 (defun notmuch-show-insert-part-multipart/* (msg part content-type nth depth button)
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 button)
693 (let* ((message (car (plist-get part :content)))
694 (body (car (plist-get message :body)))
697 ;; Override `notmuch-message-headers' to force `From' to be
699 (let ((notmuch-message-headers '("From" "Subject" "To" "Cc" "Date")))
700 (notmuch-show-insert-headers (plist-get message :headers)))
702 ;; Blank line after headers to be compatible with the normal
707 (notmuch-show-insert-bodypart msg body depth)
709 (when notmuch-show-indent-multipart
710 (indent-rigidly start (point) 1)))
713 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth button)
714 ;; For backward compatibility we want to apply the text/plain hook
715 ;; to the whole of the part including the part button if there is
717 (let ((start (if button
718 (button-start button)
720 (insert (notmuch-get-bodypart-text msg part notmuch-show-process-crypto))
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/calendar (msg part content-type nth depth button)
728 (insert (with-temp-buffer
729 (insert (notmuch-get-bodypart-text msg part notmuch-show-process-crypto))
730 ;; notmuch-get-bodypart-text does no newline conversion.
731 ;; Replace CRLF with LF before icalendar can use it.
732 (goto-char (point-min))
733 (while (re-search-forward "\r\n" nil t)
734 (replace-match "\n" nil nil))
735 (let ((file (make-temp-file "notmuch-ical"))
739 (unless (icalendar-import-buffer file t)
740 (error "Icalendar import error. See *icalendar-errors* for more information"))
741 (set-buffer (get-file-buffer file))
742 (setq result (buffer-substring (point-min) (point-max)))
743 (set-buffer-modified-p nil)
744 (kill-buffer (current-buffer)))
749 ;; For backwards compatibility.
750 (defun notmuch-show-insert-part-text/x-vcalendar (msg part content-type nth depth button)
751 (notmuch-show-insert-part-text/calendar msg part content-type nth depth button))
753 (defun notmuch-show-get-mime-type-of-application/octet-stream (part)
754 ;; If we can deduce a MIME type from the filename of the attachment,
756 (if (plist-get part :filename)
757 (let ((extension (file-name-extension (plist-get part :filename)))
761 (mailcap-parse-mimetypes)
762 (setq mime-type (mailcap-extension-to-mime extension))
764 (not (string-equal mime-type "application/octet-stream")))
769 (defun notmuch-show-insert-part-text/html (msg part content-type nth depth button)
770 (if (eq mm-text-html-renderer 'shr)
771 ;; It's easier to drive shr ourselves than to work around the
772 ;; goofy things `mm-shr' does (like irreversibly taking over
773 ;; content ID handling).
774 (notmuch-show--insert-part-text/html-shr msg part)
775 ;; Otherwise, let message-mode do the heavy lifting
777 ;; w3m sets up a keymap which "leaks" outside the invisible region
778 ;; and causes strange effects in notmuch. We set
779 ;; mm-inline-text-html-with-w3m-keymap to nil to tell w3m not to
780 ;; set a keymap (so the normal notmuch-show-mode-map remains).
781 (let ((mm-inline-text-html-with-w3m-keymap nil))
782 (notmuch-show-insert-part-*/* msg part content-type nth depth button))))
784 ;; These functions are used by notmuch-show--insert-part-text/html-shr
785 (declare-function libxml-parse-html-region "xml.c")
786 (declare-function shr-insert-document "shr")
788 (defun notmuch-show--insert-part-text/html-shr (msg part)
789 ;; Make sure shr is loaded before we start let-binding its globals
791 (let ((dom (let ((process-crypto notmuch-show-process-crypto))
793 (insert (notmuch-get-bodypart-text msg part process-crypto))
794 (libxml-parse-html-region (point-min) (point-max)))))
795 (shr-content-function
797 ;; shr strips the "cid:" part of URL, but doesn't
798 ;; URL-decode it (see RFC 2392).
799 (let ((cid (url-unhex-string url)))
800 (first (notmuch-show--get-cid-content cid)))))
801 ;; Block all external images to prevent privacy leaks and
802 ;; potential attacks. FIXME: If we block an image, offer a
803 ;; button to load external images.
804 (shr-blocked-images "."))
805 (shr-insert-document dom)
808 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth button)
809 ;; This handler _must_ succeed - it is the handler of last resort.
810 (notmuch-mm-display-part-inline msg part content-type notmuch-show-process-crypto)
813 ;; Functions for determining how to handle MIME parts.
815 (defun notmuch-show-handlers-for (content-type)
816 "Return a list of content handlers for a part of type CONTENT-TYPE."
821 ;; Reverse order of prefrence.
822 (list (intern (concat "notmuch-show-insert-part-*/*"))
824 "notmuch-show-insert-part-"
825 (car (notmuch-split-content-type content-type))
827 (intern (concat "notmuch-show-insert-part-" content-type))))
832 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth button)
833 (let ((handlers (notmuch-show-handlers-for content-type)))
834 ;; Run the content handlers until one of them returns a non-nil
837 (not (condition-case err
838 (funcall (car handlers) msg part content-type nth depth button)
839 ;; Specifying `debug' here lets the debugger
840 ;; run if `debug-on-error' is non-nil.
843 (insert "!!! Bodypart insert error: ")
844 (insert (error-message-string err))
845 (insert " !!!\n") nil)))))
846 (setq handlers (cdr handlers))))
849 (defun notmuch-show-create-part-overlays (button beg end)
850 "Add an overlay to the part between BEG and END"
852 ;; If there is no button (i.e., the part is text/plain and the first
853 ;; part) or if the part has no content then we don't make the part
855 (when (and button (/= beg end))
856 (button-put button 'overlay (make-overlay beg end))
857 ;; Return true if we created an overlay.
860 (defun notmuch-show-record-part-information (part beg end)
861 "Store PART as a text property from BEG to END"
863 ;; Record part information. Since we already inserted subparts,
864 ;; don't override existing :notmuch-part properties.
865 (notmuch-map-text-property beg end :notmuch-part
866 (lambda (v) (or v part)))
867 ;; Make :notmuch-part front sticky and rear non-sticky so it stays
868 ;; applied to the beginning of each line when we indent the
869 ;; message. Since we're operating on arbitrary renderer output,
870 ;; watch out for sticky specs of t, which means all properties are
871 ;; front-sticky/rear-nonsticky.
872 (notmuch-map-text-property beg end 'front-sticky
873 (lambda (v) (if (listp v)
874 (pushnew :notmuch-part v)
876 (notmuch-map-text-property beg end 'rear-nonsticky
877 (lambda (v) (if (listp v)
878 (pushnew :notmuch-part v)
881 (defun notmuch-show-lazy-part (part-args button)
882 ;; Insert the lazy part after the button for the part. We would just
883 ;; move to the start of the new line following the button and insert
884 ;; the part but that point might have text properties (eg colours
885 ;; from a message header etc) so instead we start from the last
886 ;; character of the button by adding a newline and finish by
887 ;; removing the extra newline from the end of the part.
889 (goto-char (button-end button))
891 (let* ((inhibit-read-only t)
892 ;; We need to use markers for the start and end of the part
893 ;; because the part insertion functions do not guarantee
894 ;; to leave point at the end of the part.
895 (part-beg (copy-marker (point) nil))
896 (part-end (copy-marker (point) t))
897 ;; We have to save the depth as we can't find the depth
899 (depth (notmuch-show-get-depth)))
901 (narrow-to-region part-beg part-end)
902 (delete-region part-beg part-end)
903 (apply #'notmuch-show-insert-bodypart-internal part-args)
904 (indent-rigidly part-beg part-end depth))
907 (notmuch-show-record-part-information (second part-args)
908 (button-start button)
910 ;; Create the overlay. If the lazy-part turned out to be empty/not
911 ;; showable this returns nil.
912 (notmuch-show-create-part-overlays button part-beg part-end))))
914 (defun notmuch-show-insert-bodypart (msg part depth &optional hide)
915 "Insert the body part PART at depth DEPTH in the current thread.
917 HIDE determines whether to show or hide the part and the button
918 as follows: If HIDE is nil, show the part and the button. If HIDE
919 is t, hide the part initially and show the button. If HIDE is
920 'no-buttons, show the part but do not add any buttons (this is
921 useful for quoting in replies)."
923 (let* ((content-type (downcase (plist-get part :content-type)))
924 (mime-type (or (and (string= content-type "application/octet-stream")
925 (notmuch-show-get-mime-type-of-application/octet-stream part))
926 (and (string= content-type "inline patch")
929 (nth (plist-get part :id))
931 ;; Hide the part initially if HIDE is t.
932 (show-part (not (equal hide t)))
933 ;; We omit the part button for the first (or only) part if
934 ;; this is text/plain, or HIDE is 'no-buttons.
935 (button (unless (or (equal hide 'no-buttons)
936 (and (string= mime-type "text/plain") (<= nth 1)))
937 (notmuch-show-insert-part-header nth mime-type content-type (plist-get part :filename))))
938 (content-beg (point)))
940 ;; Store the computed mime-type for later use (e.g. by attachment handlers).
941 (plist-put part :computed-type mime-type)
944 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth button)
945 (button-put button :notmuch-lazy-part
946 (list msg part mime-type nth depth button)))
948 ;; Some of the body part handlers leave point somewhere up in the
949 ;; part, so we make sure that we're down at the end.
950 (goto-char (point-max))
951 ;; Ensure that the part ends with a carriage return.
954 ;; We do not create the overlay for hidden (lazy) parts until
955 ;; they are inserted.
957 (notmuch-show-create-part-overlays button content-beg (point))
959 (notmuch-show-toggle-part-invisibility button)))
960 (notmuch-show-record-part-information part beg (point))))
962 (defun notmuch-show-insert-body (msg body depth)
963 "Insert the body BODY at depth DEPTH in the current thread."
965 ;; Register all content IDs for this message. According to RFC
966 ;; 2392, content IDs are *global*, but it's okay if an MUA treats
967 ;; them as only global within a message.
968 (notmuch-show--register-cids msg (first body))
970 (mapc (lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
972 (defun notmuch-show-make-symbol (type)
973 (make-symbol (concat "notmuch-show-" type)))
975 (defun notmuch-show-strip-re (string)
976 (replace-regexp-in-string "^\\([Rr]e: *\\)+" "" string))
978 (defvar notmuch-show-previous-subject "")
979 (make-variable-buffer-local 'notmuch-show-previous-subject)
981 (defun notmuch-show-insert-msg (msg depth)
982 "Insert the message MSG at depth DEPTH in the current thread."
983 (let* ((headers (plist-get msg :headers))
984 ;; Indentation causes the buffer offset of the start/end
985 ;; points to move, so we must use markers.
986 message-start message-end
987 content-start content-end
988 headers-start headers-end
989 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
991 (setq message-start (point-marker))
993 (notmuch-show-insert-headerline headers
994 (or (if notmuch-show-relative-dates
995 (plist-get msg :date_relative)
997 (plist-get headers :Date))
998 (plist-get msg :tags) depth)
1000 (setq content-start (point-marker))
1002 ;; Set `headers-start' to point after the 'Subject:' header to be
1003 ;; compatible with the existing implementation. This just sets it
1004 ;; to after the first header.
1005 (notmuch-show-insert-headers headers)
1007 (goto-char content-start)
1008 ;; If the subject of this message is the same as that of the
1009 ;; previous message, don't display it when this message is
1011 (when (not (string= notmuch-show-previous-subject
1014 (setq headers-start (point-marker)))
1015 (setq headers-end (point-marker))
1017 (setq notmuch-show-previous-subject bare-subject)
1019 ;; A blank line between the headers and the body.
1021 (notmuch-show-insert-body msg (plist-get msg :body)
1022 (if notmuch-show-indent-content depth 0))
1023 ;; Ensure that the body ends with a newline.
1026 (setq content-end (point-marker))
1028 ;; Indent according to the depth in the thread.
1029 (if notmuch-show-indent-content
1030 (indent-rigidly content-start content-end (* notmuch-show-indent-messages-width depth)))
1032 (setq message-end (point-max-marker))
1034 ;; Save the extents of this message over the whole text of the
1036 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
1038 ;; Create overlays used to control visibility
1039 (plist-put msg :headers-overlay (make-overlay headers-start headers-end))
1040 (plist-put msg :message-overlay (make-overlay headers-start content-end))
1042 (plist-put msg :depth depth)
1044 ;; Save the properties for this message. Currently this saves the
1045 ;; entire message (augmented it with other stuff), which seems
1046 ;; like overkill. We might save a reduced subset (for example, not
1048 (notmuch-show-set-message-properties msg)
1050 ;; Set header visibility.
1051 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
1053 ;; Message visibility depends on whether it matched the search
1055 (notmuch-show-message-visible msg (and (plist-get msg :match)
1056 (not (plist-get msg :excluded))))))
1058 (defun notmuch-show-toggle-process-crypto ()
1059 "Toggle the processing of cryptographic MIME parts."
1061 (setq notmuch-show-process-crypto (not notmuch-show-process-crypto))
1062 (message (if notmuch-show-process-crypto
1063 "Processing cryptographic MIME parts."
1064 "Not processing cryptographic MIME parts."))
1065 (notmuch-show-refresh-view))
1067 (defun notmuch-show-toggle-elide-non-matching ()
1068 "Toggle the display of non-matching messages."
1070 (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages))
1071 (message (if notmuch-show-elide-non-matching-messages
1072 "Showing matching messages only."
1073 "Showing all messages."))
1074 (notmuch-show-refresh-view))
1076 (defun notmuch-show-toggle-thread-indentation ()
1077 "Toggle the indentation of threads."
1079 (setq notmuch-show-indent-content (not notmuch-show-indent-content))
1080 (message (if notmuch-show-indent-content
1081 "Content is indented."
1082 "Content is not indented."))
1083 (notmuch-show-refresh-view))
1085 (defun notmuch-show-insert-tree (tree depth)
1086 "Insert the message tree TREE at depth DEPTH in the current thread."
1087 (let ((msg (car tree))
1088 (replies (cadr tree)))
1089 ;; We test whether there is a message or just some replies.
1091 (notmuch-show-insert-msg msg depth))
1092 (notmuch-show-insert-thread replies (1+ depth))))
1094 (defun notmuch-show-insert-thread (thread depth)
1095 "Insert the thread THREAD at depth DEPTH in the current forest."
1096 (mapc (lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
1098 (defun notmuch-show-insert-forest (forest)
1099 "Insert the forest of threads FOREST."
1100 (mapc (lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
1102 (defvar notmuch-id-regexp
1104 ;; Match the id: prefix only if it begins a word (to disallow, for
1105 ;; example, matching cid:).
1107 ;; If the term starts with a ", then parse Xapian's quoted boolean
1108 ;; term syntax, which allows for anything as long as embedded
1109 ;; double quotes escaped by doubling them. We also disallow
1110 ;; newlines (which Xapian allows) to prevent runaway terms.
1111 "\"\\([^\"\n]\\|\"\"\\)*\""
1112 ;; Otherwise, parse Xapian's unquoted syntax, which goes up to the
1113 ;; next space or ). We disallow [.,;] as the last character
1114 ;; because these are probably part of the surrounding text, and not
1115 ;; part of the id. This doesn't match single character ids; meh.
1116 "\\|[^\"[:space:])][^[:space:])]*[^])[:space:].,:;?!]"
1118 "The regexp used to match id: links in messages.")
1120 (defvar notmuch-mid-regexp
1121 ;; goto-address-url-regexp matched cid: links, which have the same
1122 ;; grammar as the message ID part of a mid: link. Construct the
1123 ;; regexp using the same technique as goto-address-url-regexp.
1124 (concat "\\<mid:\\(" thing-at-point-url-path-regexp "\\)")
1125 "The regexp used to match mid: links in messages.
1129 (defun notmuch-show-buttonise-links (start end)
1130 "Buttonise URLs and mail addresses between START and END.
1132 This also turns id:\"<message id>\"-parts and mid: links into
1133 buttons for a corresponding notmuch search."
1134 (goto-address-fontify-region start end)
1138 (while (re-search-forward notmuch-id-regexp end t)
1139 (push (list (match-beginning 0) (match-end 0)
1140 (match-string-no-properties 0)) links))
1142 (while (re-search-forward notmuch-mid-regexp end t)
1143 (let* ((mid-cid (match-string-no-properties 1))
1144 (mid (save-match-data
1145 (string-match "^[^/]*" mid-cid)
1146 (url-unhex-string (match-string 0 mid-cid)))))
1147 (push (list (match-beginning 0) (match-end 0)
1148 (notmuch-id-to-query mid)) links)))
1149 (dolist (link links)
1150 ;; Remove the overlay created by goto-address-mode
1151 (remove-overlays (first link) (second link) 'goto-address t)
1152 (make-text-button (first link) (second link)
1153 :type 'notmuch-button-type
1154 'action `(lambda (arg)
1155 (notmuch-show ,(third link) current-prefix-arg))
1157 'help-echo "Mouse-1, RET: search for this message"
1158 'face goto-address-mail-face)))))
1161 (defun notmuch-show (thread-id &optional elide-toggle parent-buffer query-context buffer-name)
1162 "Run \"notmuch show\" with the given thread ID and display results.
1164 ELIDE-TOGGLE, if non-nil, inverts the default elide behavior.
1166 The optional PARENT-BUFFER is the notmuch-search buffer from
1167 which this notmuch-show command was executed, (so that the
1168 next thread from that buffer can be show when done with this
1171 The optional QUERY-CONTEXT is a notmuch search term. Only
1172 messages from the thread matching this search term are shown if
1175 The optional BUFFER-NAME provides the name of the buffer in
1176 which the message thread is shown. If it is nil (which occurs
1177 when the command is called interactively) the argument to the
1179 (interactive "sNotmuch show: \nP")
1180 (let ((buffer-name (generate-new-buffer-name
1182 (concat "*notmuch-" thread-id "*")))))
1183 (switch-to-buffer (get-buffer-create buffer-name))
1184 ;; Set the default value for `notmuch-show-process-crypto' in this
1186 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1187 ;; Set the default value for
1188 ;; `notmuch-show-elide-non-matching-messages' in this buffer. If
1189 ;; elide-toggle is set, invert the default.
1190 (setq notmuch-show-elide-non-matching-messages notmuch-show-only-matching-messages)
1192 (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages)))
1194 (setq notmuch-show-thread-id thread-id
1195 notmuch-show-parent-buffer parent-buffer
1196 notmuch-show-query-context query-context)
1197 (notmuch-show-build-buffer)
1198 (notmuch-show-goto-first-wanted-message)
1201 (defun notmuch-show-build-buffer ()
1202 (let ((inhibit-read-only t))
1205 (add-hook 'post-command-hook #'notmuch-show-command-hook nil t)
1207 ;; Don't track undo information for this buffer
1208 (set 'buffer-undo-list t)
1210 (notmuch-tag-clear-cache)
1212 (goto-char (point-min))
1214 (let* ((basic-args (list notmuch-show-thread-id))
1215 (args (if notmuch-show-query-context
1216 (append (list "\'") basic-args
1217 (list "and (" notmuch-show-query-context ")\'"))
1218 (append (list "\'") basic-args (list "\'"))))
1219 (cli-args (cons "--exclude=false"
1220 (when notmuch-show-elide-non-matching-messages
1221 (list "--entire-thread=false")))))
1223 (notmuch-show-insert-forest (notmuch-query-get-threads (append cli-args args)))
1224 ;; If the query context reduced the results to nothing, run
1226 (when (and (eq (buffer-size) 0)
1227 notmuch-show-query-context)
1228 (notmuch-show-insert-forest
1229 (notmuch-query-get-threads (append cli-args basic-args)))))
1231 (jit-lock-register #'notmuch-show-buttonise-links)
1233 (notmuch-show-mapc (lambda () (notmuch-show-set-prop :orig-tags (notmuch-show-get-tags))))
1235 ;; Set the header line to the subject of the first message.
1236 (setq header-line-format
1237 (replace-regexp-in-string "%" "%%"
1239 (notmuch-show-strip-re
1240 (notmuch-show-get-subject)))))
1242 (run-hooks 'notmuch-show-hook))))
1244 (defun notmuch-show-capture-state ()
1245 "Capture the state of the current buffer.
1248 - the list of open messages,
1249 - the current message."
1250 (list (notmuch-show-get-message-id) (notmuch-show-get-message-ids-for-open-messages)))
1252 (defun notmuch-show-get-query ()
1253 "Return the current query in this show buffer"
1254 (if notmuch-show-query-context
1255 (concat notmuch-show-thread-id
1257 notmuch-show-query-context
1259 notmuch-show-thread-id))
1261 (defun notmuch-show-apply-state (state)
1262 "Apply STATE to the current buffer.
1265 - opening the messages previously opened,
1266 - closing all other messages,
1267 - moving to the correct current message."
1268 (let ((current (car state))
1269 (open (cadr state)))
1271 ;; Open those that were open.
1272 (goto-char (point-min))
1273 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1274 (member (notmuch-show-get-message-id) open))
1275 until (not (notmuch-show-goto-message-next)))
1277 ;; Go to the previously open message.
1278 (goto-char (point-min))
1279 (unless (loop if (string= current (notmuch-show-get-message-id))
1281 until (not (notmuch-show-goto-message-next)))
1282 (goto-char (point-min))
1283 (message "Previously current message not found."))
1284 (notmuch-show-message-adjust)))
1286 (defun notmuch-show-refresh-view (&optional reset-state)
1287 "Refresh the current view.
1289 Refreshes the current view, observing changes in display
1290 preferences. If invoked with a prefix argument (or RESET-STATE is
1291 non-nil) then the state of the buffer (open/closed messages) is
1292 reset based on the original query."
1294 (let ((inhibit-read-only t)
1295 (state (unless reset-state
1296 (notmuch-show-capture-state))))
1297 ;; erase-buffer does not seem to remove overlays, which can lead
1298 ;; to weird effects such as remaining images, so remove them
1302 (notmuch-show-build-buffer)
1304 (notmuch-show-apply-state state)
1305 ;; We're resetting state, so navigate to the first open message
1306 ;; and mark it read, just like opening a new show buffer.
1307 (notmuch-show-goto-first-wanted-message))))
1309 (defvar notmuch-show-stash-map
1310 (let ((map (make-sparse-keymap)))
1311 (define-key map "c" 'notmuch-show-stash-cc)
1312 (define-key map "d" 'notmuch-show-stash-date)
1313 (define-key map "F" 'notmuch-show-stash-filename)
1314 (define-key map "f" 'notmuch-show-stash-from)
1315 (define-key map "i" 'notmuch-show-stash-message-id)
1316 (define-key map "I" 'notmuch-show-stash-message-id-stripped)
1317 (define-key map "s" 'notmuch-show-stash-subject)
1318 (define-key map "T" 'notmuch-show-stash-tags)
1319 (define-key map "t" 'notmuch-show-stash-to)
1320 (define-key map "l" 'notmuch-show-stash-mlarchive-link)
1321 (define-key map "L" 'notmuch-show-stash-mlarchive-link-and-go)
1322 (define-key map "G" 'notmuch-show-stash-git-send-email)
1323 (define-key map "?" 'notmuch-subkeymap-help)
1325 "Submap for stash commands")
1326 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
1328 (defvar notmuch-show-part-map
1329 (let ((map (make-sparse-keymap)))
1330 (define-key map "s" 'notmuch-show-save-part)
1331 (define-key map "v" 'notmuch-show-view-part)
1332 (define-key map "o" 'notmuch-show-interactively-view-part)
1333 (define-key map "|" 'notmuch-show-pipe-part)
1334 (define-key map "?" 'notmuch-subkeymap-help)
1336 "Submap for part commands")
1337 (fset 'notmuch-show-part-map notmuch-show-part-map)
1339 (defvar notmuch-show-mode-map
1340 (let ((map (make-sparse-keymap)))
1341 (set-keymap-parent map notmuch-common-keymap)
1342 (define-key map "Z" 'notmuch-tree-from-show-current-query)
1343 (define-key map (kbd "<C-tab>") 'widget-backward)
1344 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
1345 (define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
1346 (define-key map (kbd "TAB") 'notmuch-show-next-button)
1347 (define-key map "f" 'notmuch-show-forward-message)
1348 (define-key map "r" 'notmuch-show-reply-sender)
1349 (define-key map "R" 'notmuch-show-reply)
1350 (define-key map "|" 'notmuch-show-pipe-message)
1351 (define-key map "w" 'notmuch-show-save-attachments)
1352 (define-key map "V" 'notmuch-show-view-raw-message)
1353 (define-key map "c" 'notmuch-show-stash-map)
1354 (define-key map "h" 'notmuch-show-toggle-visibility-headers)
1355 (define-key map "*" 'notmuch-show-tag-all)
1356 (define-key map "-" 'notmuch-show-remove-tag)
1357 (define-key map "+" 'notmuch-show-add-tag)
1358 (define-key map "X" 'notmuch-show-archive-thread-then-exit)
1359 (define-key map "x" 'notmuch-show-archive-message-then-next-or-exit)
1360 (define-key map "A" 'notmuch-show-archive-thread-then-next)
1361 (define-key map "a" 'notmuch-show-archive-message-then-next-or-next-thread)
1362 (define-key map "N" 'notmuch-show-next-message)
1363 (define-key map "P" 'notmuch-show-previous-message)
1364 (define-key map "n" 'notmuch-show-next-open-message)
1365 (define-key map "p" 'notmuch-show-previous-open-message)
1366 (define-key map (kbd "M-n") 'notmuch-show-next-thread-show)
1367 (define-key map (kbd "M-p") 'notmuch-show-previous-thread-show)
1368 (define-key map (kbd "DEL") 'notmuch-show-rewind)
1369 (define-key map " " 'notmuch-show-advance-and-archive)
1370 (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
1371 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
1372 (define-key map "#" 'notmuch-show-print-message)
1373 (define-key map "!" 'notmuch-show-toggle-elide-non-matching)
1374 (define-key map "$" 'notmuch-show-toggle-process-crypto)
1375 (define-key map "<" 'notmuch-show-toggle-thread-indentation)
1376 (define-key map "t" 'toggle-truncate-lines)
1377 (define-key map "." 'notmuch-show-part-map)
1379 "Keymap for \"notmuch show\" buffers.")
1380 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
1382 (defun notmuch-show-mode ()
1383 "Major mode for viewing a thread with notmuch.
1385 This buffer contains the results of the \"notmuch show\" command
1386 for displaying a single thread of email from your email archives.
1388 By default, various components of email messages, (citations,
1389 signatures, already-read messages), are hidden. You can make
1390 these parts visible by clicking with the mouse button or by
1391 pressing RET after positioning the cursor on a hidden part, (for
1392 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
1394 Reading the thread sequentially is well-supported by pressing
1395 \\[notmuch-show-advance-and-archive]. This will scroll the current message (if necessary), advance
1396 to the next message, or advance to the next thread (if already on
1397 the last message of a thread).
1399 Other commands are available to read or manipulate the thread
1400 more selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages
1401 without removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread
1402 without scrolling through with \\[notmuch-show-advance-and-archive]).
1404 You can add or remove arbitrary tags from the current message with
1405 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
1407 All currently available key bindings:
1409 \\{notmuch-show-mode-map}"
1411 (kill-all-local-variables)
1412 (setq notmuch-buffer-refresh-function #'notmuch-show-refresh-view)
1413 (use-local-map notmuch-show-mode-map)
1414 (setq major-mode 'notmuch-show-mode
1415 mode-name "notmuch-show")
1416 (setq buffer-read-only t
1419 (defun notmuch-tree-from-show-current-query ()
1420 "Call notmuch tree with the current query"
1422 (notmuch-tree notmuch-show-thread-id
1423 notmuch-show-query-context
1424 (notmuch-show-get-message-id)))
1426 (defun notmuch-show-move-to-message-top ()
1427 (goto-char (notmuch-show-message-top)))
1429 (defun notmuch-show-move-to-message-bottom ()
1430 (goto-char (notmuch-show-message-bottom)))
1432 (defun notmuch-show-message-adjust ()
1435 ;; Movement related functions.
1437 ;; There's some strangeness here where a text property applied to a
1438 ;; region a->b is not found when point is at b. We walk backwards
1439 ;; until finding the property.
1440 (defun notmuch-show-message-extent ()
1443 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
1447 (defun notmuch-show-message-top ()
1448 (car (notmuch-show-message-extent)))
1450 (defun notmuch-show-message-bottom ()
1451 (cdr (notmuch-show-message-extent)))
1453 (defun notmuch-show-goto-message-next ()
1454 (let ((start (point)))
1455 (notmuch-show-move-to-message-bottom)
1461 (defun notmuch-show-goto-message-previous ()
1462 (notmuch-show-move-to-message-top)
1466 (notmuch-show-move-to-message-top)
1469 (defun notmuch-show-mapc (function)
1470 "Iterate through all messages in the current thread with
1471 `notmuch-show-goto-message-next' and call FUNCTION for side
1474 (goto-char (point-min))
1475 (loop do (funcall function)
1476 while (notmuch-show-goto-message-next))))
1478 ;; Functions relating to the visibility of messages and their
1481 (defun notmuch-show-message-visible (props visible-p)
1482 (overlay-put (plist-get props :message-overlay) 'invisible (not visible-p))
1483 (notmuch-show-set-prop :message-visible visible-p props))
1485 (defun notmuch-show-headers-visible (props visible-p)
1486 (overlay-put (plist-get props :headers-overlay) 'invisible (not visible-p))
1487 (notmuch-show-set-prop :headers-visible visible-p props))
1489 ;; Functions for setting and getting attributes of the current
1492 (defun notmuch-show-set-message-properties (props)
1494 (notmuch-show-move-to-message-top)
1495 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
1497 (defun notmuch-show-get-message-properties ()
1498 "Return the properties of the current message as a plist.
1500 Some useful entries are:
1501 :headers - Property list containing the headers :Date, :Subject, :From, etc.
1502 :body - Body of the message
1503 :tags - Tags for this message"
1505 (notmuch-show-move-to-message-top)
1506 (get-text-property (point) :notmuch-message-properties)))
1508 (defun notmuch-show-get-part-properties ()
1509 "Return the properties of the innermost part containing point.
1511 This is the part property list retrieved from the CLI. Signals
1512 an error if there is no part containing point."
1513 (or (get-text-property (point) :notmuch-part)
1514 (error "No message part here")))
1516 (defun notmuch-show-set-prop (prop val &optional props)
1517 (let ((inhibit-read-only t)
1519 (notmuch-show-get-message-properties))))
1520 (plist-put props prop val)
1521 (notmuch-show-set-message-properties props)))
1523 (defun notmuch-show-get-prop (prop &optional props)
1524 "Get property PROP from current message in show or tree mode.
1526 It gets property PROP from PROPS or, if PROPS is nil, the current
1527 message in either tree or show. This means that several utility
1528 functions in notmuch-show can be used directly by notmuch-tree as
1529 they just need the correct message properties."
1530 (let ((props (or props
1531 (cond ((eq major-mode 'notmuch-show-mode)
1532 (notmuch-show-get-message-properties))
1533 ((eq major-mode 'notmuch-tree-mode)
1534 (notmuch-tree-get-message-properties))
1536 (plist-get props prop)))
1538 (defun notmuch-show-get-message-id (&optional bare)
1539 "Return an id: query for the Message-Id of the current message.
1541 If optional argument BARE is non-nil, return
1542 the Message-Id without id: prefix and escaping."
1544 (notmuch-show-get-prop :id)
1545 (notmuch-id-to-query (notmuch-show-get-prop :id))))
1547 (defun notmuch-show-get-messages-ids ()
1548 "Return all id: queries of messages in the current thread."
1549 (let ((message-ids))
1551 (lambda () (push (notmuch-show-get-message-id) message-ids)))
1554 (defun notmuch-show-get-messages-ids-search ()
1555 "Return a search string for all message ids of messages in the
1557 (mapconcat 'identity (notmuch-show-get-messages-ids) " or "))
1559 ;; dme: Would it make sense to use a macro for many of these?
1561 (defun notmuch-show-get-filename ()
1562 "Return the filename of the current message."
1563 (notmuch-show-get-prop :filename))
1565 (defun notmuch-show-get-header (header &optional props)
1566 "Return the named header of the current message, if any."
1567 (plist-get (notmuch-show-get-prop :headers props) header))
1569 (defun notmuch-show-get-cc ()
1570 (notmuch-show-get-header :Cc))
1572 (defun notmuch-show-get-date ()
1573 (notmuch-show-get-header :Date))
1575 (defun notmuch-show-get-from ()
1576 (notmuch-show-get-header :From))
1578 (defun notmuch-show-get-subject ()
1579 (notmuch-show-get-header :Subject))
1581 (defun notmuch-show-get-to ()
1582 (notmuch-show-get-header :To))
1584 (defun notmuch-show-get-depth ()
1585 (notmuch-show-get-prop :depth))
1587 (defun notmuch-show-set-tags (tags)
1588 "Set the tags of the current message."
1589 (notmuch-show-set-prop :tags tags)
1590 (notmuch-show-update-tags tags))
1592 (defun notmuch-show-get-tags ()
1593 "Return the tags of the current message."
1594 (notmuch-show-get-prop :tags))
1596 (defun notmuch-show-message-visible-p ()
1597 "Is the current message visible?"
1598 (notmuch-show-get-prop :message-visible))
1600 (defun notmuch-show-headers-visible-p ()
1601 "Are the headers of the current message visible?"
1602 (notmuch-show-get-prop :headers-visible))
1604 (put 'notmuch-show-mark-read 'notmuch-prefix-doc
1605 "Mark the current message as unread.")
1606 (defun notmuch-show-mark-read (&optional unread)
1607 "Mark the current message as read.
1609 Mark the current message as read by applying the tag changes in
1610 `notmuch-show-mark-read-tags' to it (remove the \"unread\" tag by
1611 default). If a prefix argument is given, the message will be
1612 marked as unread, i.e. the tag changes in
1613 `notmuch-show-mark-read-tags' will be reversed."
1615 (when notmuch-show-mark-read-tags
1616 (apply 'notmuch-show-tag-message
1617 (notmuch-tag-change-list notmuch-show-mark-read-tags unread))))
1619 (defun notmuch-show-seen-current-message (start end)
1620 "Mark the current message read if it is open.
1622 We only mark it read once: if it is changed back then that is a
1623 user decision and we should not override it."
1624 (when (and (notmuch-show-message-visible-p)
1625 (not (notmuch-show-get-prop :seen)))
1626 (notmuch-show-mark-read)
1627 (notmuch-show-set-prop :seen t)))
1629 (defun notmuch-show-command-hook ()
1630 (when (eq major-mode 'notmuch-show-mode)
1631 ;; We need to redisplay to get window-start and window-end correct.
1634 (funcall notmuch-show-mark-read-function (window-start) (window-end)))))
1636 ;; Functions for getting attributes of several messages in the current
1639 (defun notmuch-show-get-message-ids-for-open-messages ()
1640 "Return a list of all id: queries for open messages in the current thread."
1642 (let (message-ids done)
1643 (goto-char (point-min))
1645 (if (notmuch-show-message-visible-p)
1646 (setq message-ids (append message-ids (list (notmuch-show-get-message-id)))))
1647 (setq done (not (notmuch-show-goto-message-next)))
1652 ;; Commands typically bound to keys.
1654 (defun notmuch-show-advance ()
1655 "Advance through thread.
1657 If the current message in the thread is not yet fully visible,
1658 scroll by a near screenful to read more of the message.
1660 Otherwise, (the end of the current message is already within the
1661 current window), advance to the next open message."
1663 (let* ((end-of-this-message (notmuch-show-message-bottom))
1664 (visible-end-of-this-message (1- end-of-this-message))
1666 (while (invisible-p visible-end-of-this-message)
1667 (setq visible-end-of-this-message
1669 (1- (previous-single-char-property-change
1670 visible-end-of-this-message 'invisible)))))
1672 ;; Ideally we would test `end-of-this-message' against the result
1673 ;; of `window-end', but that doesn't account for the fact that
1674 ;; the end of the message might be hidden.
1675 ((and visible-end-of-this-message
1676 (> visible-end-of-this-message (window-end)))
1677 ;; The bottom of this message is not visible - scroll.
1680 ((not (= end-of-this-message (point-max)))
1681 ;; This is not the last message - move to the next visible one.
1682 (notmuch-show-next-open-message))
1684 ((not (= (point) (point-max)))
1685 ;; This is the last message, but the cursor is not at the end of
1686 ;; the buffer. Move it there.
1687 (goto-char (point-max)))
1690 ;; This is the last message - change the return value
1694 (defun notmuch-show-advance-and-archive ()
1695 "Advance through thread and archive.
1697 This command is intended to be one of the simplest ways to
1698 process a thread of email. It works exactly like
1699 notmuch-show-advance, in that it scrolls through messages in a
1700 show buffer, except that when it gets to the end of the buffer it
1701 archives the entire current thread, (apply changes in
1702 `notmuch-archive-tags'), kills the buffer, and displays the next
1703 thread from the search from which this thread was originally
1706 (if (notmuch-show-advance)
1707 (notmuch-show-archive-thread-then-next)))
1709 (defun notmuch-show-rewind ()
1710 "Backup through the thread (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
1712 Specifically, if the beginning of the previous email is fewer
1713 than `window-height' lines from the current point, move to it
1714 just like `notmuch-show-previous-message'.
1716 Otherwise, just scroll down a screenful of the current message.
1718 This command does not modify any message tags, (it does not undo
1719 any effects from previous calls to
1720 `notmuch-show-advance-and-archive'."
1722 (let ((start-of-message (notmuch-show-message-top))
1723 (start-of-window (window-start)))
1725 ;; Either this message is properly aligned with the start of the
1726 ;; window or the start of this message is not visible on the
1728 ((or (= start-of-message start-of-window)
1729 (< start-of-message start-of-window))
1731 ;; If a small number of lines from the previous message are
1732 ;; visible, realign so that the top of the current message is at
1733 ;; the top of the screen.
1734 (when (<= (count-screen-lines (window-start) start-of-message)
1735 next-screen-context-lines)
1736 (goto-char (notmuch-show-message-top))
1737 (notmuch-show-message-adjust))
1738 ;; Move to the top left of the window.
1739 (goto-char (window-start)))
1741 ;; Move to the previous message.
1742 (notmuch-show-previous-message)))))
1744 (put 'notmuch-show-reply 'notmuch-prefix-doc "... and prompt for sender")
1745 (defun notmuch-show-reply (&optional prompt-for-sender)
1746 "Reply to the sender and all recipients of the current message."
1748 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender t))
1750 (put 'notmuch-show-reply-sender 'notmuch-prefix-doc "... and prompt for sender")
1751 (defun notmuch-show-reply-sender (&optional prompt-for-sender)
1752 "Reply to the sender of the current message."
1754 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender nil))
1756 (put 'notmuch-show-forward-message 'notmuch-prefix-doc
1757 "... and prompt for sender")
1758 (defun notmuch-show-forward-message (&optional prompt-for-sender)
1759 "Forward the current message."
1761 (with-current-notmuch-show-message
1762 (notmuch-mua-new-forward-message prompt-for-sender)))
1764 (defun notmuch-show-next-message (&optional pop-at-end)
1765 "Show the next message.
1767 If a prefix argument is given and this is the last message in the
1768 thread, navigate to the next thread in the parent search buffer."
1770 (if (notmuch-show-goto-message-next)
1771 (notmuch-show-message-adjust)
1773 (notmuch-show-next-thread)
1774 (goto-char (point-max)))))
1776 (defun notmuch-show-previous-message ()
1777 "Show the previous message or the start of the current message."
1779 (if (= (point) (notmuch-show-message-top))
1780 (notmuch-show-goto-message-previous)
1781 (notmuch-show-move-to-message-top))
1782 (notmuch-show-message-adjust))
1784 (defun notmuch-show-next-open-message (&optional pop-at-end)
1785 "Show the next open message.
1787 If a prefix argument is given and this is the last open message
1788 in the thread, navigate to the next thread in the parent search
1789 buffer. Return t if there was a next open message in the thread
1790 to show, nil otherwise."
1793 (while (and (setq r (notmuch-show-goto-message-next))
1794 (not (notmuch-show-message-visible-p))))
1796 (notmuch-show-message-adjust)
1798 (notmuch-show-next-thread)
1799 (goto-char (point-max))))
1802 (defun notmuch-show-next-matching-message ()
1803 "Show the next matching message."
1806 (while (and (setq r (notmuch-show-goto-message-next))
1807 (not (notmuch-show-get-prop :match))))
1809 (notmuch-show-message-adjust)
1810 (goto-char (point-max)))))
1812 (defun notmuch-show-open-if-matched ()
1813 "Open a message if it is matched (whether or not excluded)."
1814 (let ((props (notmuch-show-get-message-properties)))
1815 (notmuch-show-message-visible props (plist-get props :match))))
1817 (defun notmuch-show-goto-first-wanted-message ()
1818 "Move to the first open message and mark it read"
1819 (goto-char (point-min))
1820 (unless (notmuch-show-message-visible-p)
1821 (notmuch-show-next-open-message))
1823 ;; There are no matched non-excluded messages so open all matched
1824 ;; (necessarily excluded) messages and go to the first.
1825 (notmuch-show-mapc 'notmuch-show-open-if-matched)
1826 (force-window-update)
1827 (goto-char (point-min))
1828 (unless (notmuch-show-message-visible-p)
1829 (notmuch-show-next-open-message))))
1831 (defun notmuch-show-previous-open-message ()
1832 "Show the previous open message."
1834 (while (and (if (= (point) (notmuch-show-message-top))
1835 (notmuch-show-goto-message-previous)
1836 (notmuch-show-move-to-message-top))
1837 (not (notmuch-show-message-visible-p))))
1838 (notmuch-show-message-adjust))
1840 (defun notmuch-show-view-raw-message ()
1841 "View the original source of the current message."
1843 (let* ((id (notmuch-show-get-message-id))
1844 (buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
1845 (let ((coding-system-for-read 'no-conversion))
1846 (call-process notmuch-command nil buf nil "show" "--format=raw" id))
1847 (switch-to-buffer buf)
1848 (goto-char (point-min))
1849 (set-buffer-modified-p nil)
1850 (view-buffer buf 'kill-buffer-if-not-modified)))
1852 (put 'notmuch-show-pipe-message 'notmuch-doc
1853 "Pipe the contents of the current message to a command.")
1854 (put 'notmuch-show-pipe-message 'notmuch-prefix-doc
1855 "Pipe the thread as an mbox to a command.")
1856 (defun notmuch-show-pipe-message (entire-thread command)
1857 "Pipe the contents of the current message (or thread) to COMMAND.
1859 COMMAND will be executed with the raw contents of the current
1860 email message as stdin. Anything printed by the command to stdout
1861 or stderr will appear in the *notmuch-pipe* buffer.
1863 If ENTIRE-THREAD is non-nil (or when invoked with a prefix
1864 argument), COMMAND will receive all open messages in the current
1865 thread (formatted as an mbox) rather than only the current
1867 (interactive (let ((query-string (if current-prefix-arg
1868 "Pipe all open messages to command: "
1869 "Pipe message to command: ")))
1870 (list current-prefix-arg (read-string query-string))))
1871 (let (shell-command)
1874 (concat notmuch-command " show --format=mbox --exclude=false "
1875 (shell-quote-argument
1876 (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
1879 (concat notmuch-command " show --format=raw "
1880 (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
1881 (let ((cwd default-directory)
1882 (buf (get-buffer-create (concat "*notmuch-pipe*"))))
1883 (with-current-buffer buf
1884 (setq buffer-read-only nil)
1886 ;; Use the originating buffer's working directory instead of
1887 ;; that of the pipe buffer.
1889 (let ((exit-code (call-process-shell-command shell-command nil buf)))
1890 (goto-char (point-max))
1891 (set-buffer-modified-p nil)
1892 (setq buffer-read-only t)
1893 (unless (zerop exit-code)
1894 (switch-to-buffer-other-window buf)
1895 (message (format "Command '%s' exited abnormally with code %d"
1896 shell-command exit-code))))))))
1898 (defun notmuch-show-tag-message (&rest tag-changes)
1899 "Change tags for the current message.
1901 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1902 (let* ((current-tags (notmuch-show-get-tags))
1903 (new-tags (notmuch-update-tags current-tags tag-changes)))
1904 (unless (equal current-tags new-tags)
1905 (notmuch-tag (notmuch-show-get-message-id) tag-changes)
1906 (notmuch-show-set-tags new-tags))))
1908 (defun notmuch-show-tag (tag-changes)
1909 "Change tags for the current message.
1911 See `notmuch-tag' for information on the format of TAG-CHANGES."
1912 (interactive (list (notmuch-read-tag-changes (notmuch-show-get-tags)
1914 (notmuch-tag (notmuch-show-get-message-id) tag-changes)
1915 (let* ((current-tags (notmuch-show-get-tags))
1916 (new-tags (notmuch-update-tags current-tags tag-changes)))
1917 (unless (equal current-tags new-tags)
1918 (notmuch-show-set-tags new-tags))))
1920 (defun notmuch-show-tag-all (tag-changes)
1921 "Change tags for all messages in the current show buffer.
1923 See `notmuch-tag' for information on the format of TAG-CHANGES."
1927 (lambda () (setq tags (append (notmuch-show-get-tags) tags))))
1928 (notmuch-read-tag-changes tags "Tag thread"))))
1929 (notmuch-tag (notmuch-show-get-messages-ids-search) tag-changes)
1932 (let* ((current-tags (notmuch-show-get-tags))
1933 (new-tags (notmuch-update-tags current-tags tag-changes)))
1934 (unless (equal current-tags new-tags)
1935 (notmuch-show-set-tags new-tags))))))
1937 (defun notmuch-show-add-tag (tag-changes)
1938 "Change tags for the current message (defaulting to add).
1940 Same as `notmuch-show-tag' but sets initial input to '+'."
1942 (list (notmuch-read-tag-changes (notmuch-show-get-tags) "Tag message" "+")))
1943 (notmuch-show-tag tag-changes))
1945 (defun notmuch-show-remove-tag (tag-changes)
1946 "Change tags for the current message (defaulting to remove).
1948 Same as `notmuch-show-tag' but sets initial input to '-'."
1950 (list (notmuch-read-tag-changes (notmuch-show-get-tags) "Tag message" "-")))
1951 (notmuch-show-tag tag-changes))
1953 (defun notmuch-show-toggle-visibility-headers ()
1954 "Toggle the visibility of the current message headers."
1956 (let ((props (notmuch-show-get-message-properties)))
1957 (notmuch-show-headers-visible
1959 (not (plist-get props :headers-visible))))
1960 (force-window-update))
1962 (defun notmuch-show-toggle-message ()
1963 "Toggle the visibility of the current message."
1965 (let ((props (notmuch-show-get-message-properties)))
1966 (notmuch-show-message-visible
1968 (not (plist-get props :message-visible))))
1969 (force-window-update))
1971 (put 'notmuch-show-open-or-close-all 'notmuch-doc "Show all messages.")
1972 (put 'notmuch-show-open-or-close-all 'notmuch-prefix-doc "Hide all messages.")
1973 (defun notmuch-show-open-or-close-all ()
1974 "Set the visibility all of the messages in the current thread.
1976 By default make all of the messages visible. With a prefix
1977 argument, hide all of the messages."
1980 (goto-char (point-min))
1981 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1982 (not current-prefix-arg))
1983 until (not (notmuch-show-goto-message-next))))
1984 (force-window-update))
1986 (defun notmuch-show-next-button ()
1987 "Advance point to the next button in the buffer."
1991 (defun notmuch-show-previous-button ()
1992 "Move point back to the previous button in the buffer."
1994 (backward-button 1))
1996 (defun notmuch-show-next-thread (&optional show previous)
1997 "Move to the next item in the search results, if any.
1999 If SHOW is non-nil, open the next item in a show
2000 buffer. Otherwise just highlight the next item in the search
2001 buffer. If PREVIOUS is non-nil, move to the previous item in the
2002 search results instead."
2004 (let ((parent-buffer notmuch-show-parent-buffer))
2005 (notmuch-bury-or-kill-this-buffer)
2006 (when (buffer-live-p parent-buffer)
2007 (switch-to-buffer parent-buffer)
2009 (notmuch-search-previous-thread)
2010 (notmuch-search-next-thread))
2012 (notmuch-search-show-thread)))))
2014 (defun notmuch-show-next-thread-show ()
2015 "Show the next thread in the search results, if any."
2017 (notmuch-show-next-thread t))
2019 (defun notmuch-show-previous-thread-show ()
2020 "Show the previous thread in the search results, if any."
2022 (notmuch-show-next-thread t t))
2024 (put 'notmuch-show-archive-thread 'notmuch-prefix-doc
2025 "Un-archive each message in thread.")
2026 (defun notmuch-show-archive-thread (&optional unarchive)
2027 "Archive each message in thread.
2029 Archive each message currently shown by applying the tag changes
2030 in `notmuch-archive-tags' to each. If a prefix argument is given,
2031 the messages will be \"unarchived\", i.e. the tag changes in
2032 `notmuch-archive-tags' will be reversed.
2034 Note: This command is safe from any race condition of new messages
2035 being delivered to the same thread. It does not archive the
2036 entire thread, but only the messages shown in the current
2039 (when notmuch-archive-tags
2040 (notmuch-show-tag-all
2041 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
2043 (defun notmuch-show-archive-thread-then-next ()
2044 "Archive all messages in the current buffer, then show next thread from search."
2046 (notmuch-show-archive-thread)
2047 (notmuch-show-next-thread t))
2049 (defun notmuch-show-archive-thread-then-exit ()
2050 "Archive all messages in the current buffer, then exit back to search results."
2052 (notmuch-show-archive-thread)
2053 (notmuch-show-next-thread))
2055 (put 'notmuch-show-archive-message 'notmuch-prefix-doc
2056 "Un-archive the current message.")
2057 (defun notmuch-show-archive-message (&optional unarchive)
2058 "Archive the current message.
2060 Archive the current message by applying the tag changes in
2061 `notmuch-archive-tags' to it. If a prefix argument is given, the
2062 message will be \"unarchived\", i.e. the tag changes in
2063 `notmuch-archive-tags' will be reversed."
2065 (when notmuch-archive-tags
2066 (apply 'notmuch-show-tag-message
2067 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
2069 (defun notmuch-show-archive-message-then-next-or-exit ()
2070 "Archive the current message, then show the next open message in the current thread.
2072 If at the last open message in the current thread, then exit back
2075 (notmuch-show-archive-message)
2076 (notmuch-show-next-open-message t))
2078 (defun notmuch-show-archive-message-then-next-or-next-thread ()
2079 "Archive the current message, then show the next open message in the current thread.
2081 If at the last open message in the current thread, then show next
2082 thread from search."
2084 (notmuch-show-archive-message)
2085 (unless (notmuch-show-next-open-message)
2086 (notmuch-show-next-thread t)))
2088 (defun notmuch-show-stash-cc ()
2089 "Copy CC field of current message to kill-ring."
2091 (notmuch-common-do-stash (notmuch-show-get-cc)))
2093 (defun notmuch-show-stash-date ()
2094 "Copy date of current message to kill-ring."
2096 (notmuch-common-do-stash (notmuch-show-get-date)))
2098 (defun notmuch-show-stash-filename ()
2099 "Copy filename of current message to kill-ring."
2101 (notmuch-common-do-stash (notmuch-show-get-filename)))
2103 (defun notmuch-show-stash-from ()
2104 "Copy From address of current message to kill-ring."
2106 (notmuch-common-do-stash (notmuch-show-get-from)))
2108 (put 'notmuch-show-stash-message-id 'notmuch-prefix-doc
2109 "Copy thread: query matching current thread to kill-ring.")
2110 (defun notmuch-show-stash-message-id (&optional stash-thread-id)
2111 "Copy id: query matching the current message to kill-ring.
2113 If invoked with a prefix argument (or STASH-THREAD-ID is
2114 non-nil), copy thread: query matching the current thread to
2118 (notmuch-common-do-stash notmuch-show-thread-id)
2119 (notmuch-common-do-stash (notmuch-show-get-message-id))))
2121 (defun notmuch-show-stash-message-id-stripped ()
2122 "Copy message ID of current message (sans `id:' prefix) to kill-ring."
2124 (notmuch-common-do-stash (notmuch-show-get-message-id t)))
2126 (defun notmuch-show-stash-subject ()
2127 "Copy Subject field of current message to kill-ring."
2129 (notmuch-common-do-stash (notmuch-show-get-subject)))
2131 (defun notmuch-show-stash-tags ()
2132 "Copy tags of current message to kill-ring as a comma separated list."
2134 (notmuch-common-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
2136 (defun notmuch-show-stash-to ()
2137 "Copy To address of current message to kill-ring."
2139 (notmuch-common-do-stash (notmuch-show-get-to)))
2141 (defun notmuch-show-stash-mlarchive-link (&optional mla)
2142 "Copy an ML Archive URI for the current message to the kill-ring.
2144 This presumes that the message is available at the selected Mailing List Archive.
2146 If optional argument MLA is non-nil, use the provided key instead of prompting
2147 the user (see `notmuch-show-stash-mlarchive-link-alist')."
2149 (let ((url (cdr (assoc
2151 (let ((completion-ignore-case t))
2153 "Mailing List Archive: "
2154 notmuch-show-stash-mlarchive-link-alist
2156 notmuch-show-stash-mlarchive-link-default)))
2157 notmuch-show-stash-mlarchive-link-alist))))
2158 (notmuch-common-do-stash
2160 (funcall url (notmuch-show-get-message-id t))
2161 (concat url (notmuch-show-get-message-id t))))))
2163 (defun notmuch-show-stash-mlarchive-link-and-go (&optional mla)
2164 "Copy an ML Archive URI for the current message to the kill-ring and visit it.
2166 This presumes that the message is available at the selected Mailing List Archive.
2168 If optional argument MLA is non-nil, use the provided key instead of prompting
2169 the user (see `notmuch-show-stash-mlarchive-link-alist')."
2171 (notmuch-show-stash-mlarchive-link mla)
2172 (browse-url (current-kill 0 t)))
2174 (defun notmuch-show-stash-git-helper (addresses prefix)
2175 "Escape, trim, quote, and add PREFIX to each address in list of ADDRESSES, and return the result as a single string."
2176 (mapconcat (lambda (x)
2178 ;; escape double-quotes
2179 (replace-regexp-in-string
2181 ;; trim leading and trailing spaces
2182 (replace-regexp-in-string
2183 "\\(^ *\\| *$\\)" ""
2187 (put 'notmuch-show-stash-git-send-email 'notmuch-prefix-doc
2188 "Copy From/To/Cc of current message to kill-ring in a form suitable for pasting to git send-email command line.")
2190 (defun notmuch-show-stash-git-send-email (&optional no-in-reply-to)
2191 "Copy From/To/Cc/Message-Id of current message to kill-ring in a form suitable for pasting to git send-email command line.
2193 If invoked with a prefix argument (or NO-IN-REPLY-TO is non-nil),
2194 omit --in-reply-to=<Message-Id>."
2196 (notmuch-common-do-stash
2197 (mapconcat 'identity
2200 (notmuch-show-stash-git-helper
2201 (message-tokenize-header (notmuch-show-get-from)) "--to=")
2202 (notmuch-show-stash-git-helper
2203 (message-tokenize-header (notmuch-show-get-to)) "--to=")
2204 (notmuch-show-stash-git-helper
2205 (message-tokenize-header (notmuch-show-get-cc)) "--cc=")
2206 (unless no-in-reply-to
2207 (notmuch-show-stash-git-helper
2208 (list (notmuch-show-get-message-id t)) "--in-reply-to="))))
2211 ;; Interactive part functions and their helpers
2213 (defun notmuch-show-generate-part-buffer (msg part)
2214 "Return a temporary buffer containing the specified part's content."
2215 (let ((buf (generate-new-buffer " *notmuch-part*"))
2216 (process-crypto notmuch-show-process-crypto))
2217 (with-current-buffer buf
2218 ;; This is always used in the content of mm handles, which
2219 ;; expect undecoded, binary part content.
2220 (insert (notmuch-get-bodypart-binary msg part process-crypto)))
2223 (defun notmuch-show-current-part-handle ()
2224 "Return an mm-handle for the part containing point.
2226 This creates a temporary buffer for the part's content; the
2227 caller is responsible for killing this buffer as appropriate."
2228 (let* ((msg (notmuch-show-get-message-properties))
2229 (part (notmuch-show-get-part-properties))
2230 (buf (notmuch-show-generate-part-buffer msg part))
2231 (computed-type (plist-get part :computed-type))
2232 (filename (plist-get part :filename))
2233 (disposition (if filename `(attachment (filename . ,filename)))))
2234 (mm-make-handle buf (list computed-type) nil nil disposition)))
2236 (defun notmuch-show-apply-to-current-part-handle (fn)
2237 "Apply FN to an mm-handle for the part containing point.
2239 This ensures that the temporary buffer created for the mm-handle
2240 is destroyed when FN returns."
2241 (let ((handle (notmuch-show-current-part-handle)))
2242 ;; emacs 24.3+ puts stdout/stderr into the calling buffer so we
2243 ;; call it from a temp-buffer, unless
2244 ;; notmuch-show-attachment-debug is non-nil in which case we put
2245 ;; it in " *notmuch-part*".
2247 (if notmuch-show-attachment-debug
2248 (with-current-buffer (generate-new-buffer " *notmuch-part*")
2249 (funcall fn handle))
2251 (funcall fn handle)))
2252 (kill-buffer (mm-handle-buffer handle)))))
2254 (defun notmuch-show-part-button-default (&optional button)
2256 (let ((button (or button (button-at (point)))))
2257 ;; Try to toggle the part, if that fails then call the default
2258 ;; action. The toggle fails if the part has no emacs renderable
2260 (unless (notmuch-show-toggle-part-invisibility button)
2261 (call-interactively notmuch-show-part-button-default-action))))
2263 (defun notmuch-show-save-part ()
2264 "Save the MIME part containing point to a file."
2266 (notmuch-show-apply-to-current-part-handle #'mm-save-part))
2268 (defun notmuch-show-view-part ()
2269 "View the MIME part containing point in an external viewer."
2271 ;; Set mm-inlined-types to nil to force an external viewer
2272 (let ((mm-inlined-types nil))
2273 (notmuch-show-apply-to-current-part-handle #'mm-display-part)))
2275 (defun notmuch-show-interactively-view-part ()
2276 "View the MIME part containing point, prompting for a viewer."
2278 (notmuch-show-apply-to-current-part-handle #'mm-interactively-view-part))
2280 (defun notmuch-show-pipe-part ()
2281 "Pipe the MIME part containing point to an external command."
2283 (notmuch-show-apply-to-current-part-handle #'mm-pipe-part))
2286 (provide 'notmuch-show)