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)
45 (defcustom notmuch-message-headers '("Subject" "To" "Cc" "Date")
46 "Headers that should be shown in a message, in this order.
48 For an open message, all of these headers will be made visible
49 according to `notmuch-message-headers-visible' or can be toggled
50 with `notmuch-show-toggle-visibility-headers'. For a closed message,
51 only the first header in the list will be visible."
52 :type '(repeat string)
55 (defcustom notmuch-message-headers-visible t
56 "Should the headers be visible by default?
58 If this value is non-nil, then all of the headers defined in
59 `notmuch-message-headers' will be visible by default in the display
60 of each message. Otherwise, these headers will be hidden and
61 `notmuch-show-toggle-visibility-headers' can be used to make them
62 visible for any given message."
66 (defcustom notmuch-show-relative-dates t
67 "Display relative dates in the message summary line."
71 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
72 "A list of functions called to decorate the headers listed in
73 `notmuch-message-headers'.")
75 (defcustom notmuch-show-hook '(notmuch-show-turn-on-visual-line-mode)
76 "Functions called after populating a `notmuch-show' buffer."
78 :options '(notmuch-show-turn-on-visual-line-mode)
80 :group 'notmuch-hooks)
82 (defcustom notmuch-show-insert-text/plain-hook '(notmuch-wash-wrap-long-lines
83 notmuch-wash-tidy-citations
84 notmuch-wash-elide-blank-lines
85 notmuch-wash-excerpt-citations)
86 "Functions used to improve the display of text/plain parts."
88 :options '(notmuch-wash-convert-inline-patch-to-part
89 notmuch-wash-wrap-long-lines
90 notmuch-wash-tidy-citations
91 notmuch-wash-elide-blank-lines
92 notmuch-wash-excerpt-citations)
94 :group 'notmuch-hooks)
96 ;; Mostly useful for debugging.
97 (defcustom notmuch-show-all-multipart/alternative-parts nil
98 "Should all parts of multipart/alternative parts be shown?"
100 :group 'notmuch-show)
102 (defcustom notmuch-show-indent-messages-width 1
103 "Width of message indentation in threads.
105 Messages are shown indented according to their depth in a thread.
106 This variable determines the width of this indentation measured
107 in number of blanks. Defaults to `1', choose `0' to disable
110 :group 'notmuch-show)
112 (defcustom notmuch-show-indent-multipart nil
113 "Should the sub-parts of a multipart/* part be indented?"
114 ;; dme: Not sure which is a good default.
116 :group 'notmuch-show)
118 (defcustom notmuch-show-part-button-default-action 'notmuch-show-save-part
119 "Default part header button action (on ENTER or mouse click)."
121 :type '(choice (const :tag "Save part"
122 notmuch-show-save-part)
123 (const :tag "View part"
124 notmuch-show-view-part)
125 (const :tag "View interactively"
126 notmuch-show-interactively-view-part)))
128 (defcustom notmuch-show-only-matching-messages nil
129 "Only matching messages are shown by default."
131 :group 'notmuch-show)
133 (defvar notmuch-show-thread-id nil)
134 (make-variable-buffer-local 'notmuch-show-thread-id)
135 (put 'notmuch-show-thread-id 'permanent-local t)
137 (defvar notmuch-show-parent-buffer nil)
138 (make-variable-buffer-local 'notmuch-show-parent-buffer)
139 (put 'notmuch-show-parent-buffer 'permanent-local t)
141 (defvar notmuch-show-query-context nil)
142 (make-variable-buffer-local 'notmuch-show-query-context)
143 (put 'notmuch-show-query-context 'permanent-local t)
145 (defvar notmuch-show-process-crypto nil)
146 (make-variable-buffer-local 'notmuch-show-process-crypto)
147 (put 'notmuch-show-process-crypto 'permanent-local t)
149 (defvar notmuch-show-elide-non-matching-messages nil)
150 (make-variable-buffer-local 'notmuch-show-elide-non-matching-messages)
151 (put 'notmuch-show-elide-non-matching-messages 'permanent-local t)
153 (defvar notmuch-show-indent-content t)
154 (make-variable-buffer-local 'notmuch-show-indent-content)
155 (put 'notmuch-show-indent-content 'permanent-local t)
157 (defcustom notmuch-show-stash-mlarchive-link-alist
158 '(("Gmane" . "http://mid.gmane.org/")
159 ("MARC" . "http://marc.info/?i=")
160 ("Mail Archive, The" . "http://mail-archive.com/search?l=mid&q=")
161 ("LKML" . "http://lkml.kernel.org/r/")
162 ;; FIXME: can these services be searched by `Message-Id' ?
163 ;; ("MarkMail" . "http://markmail.org/")
164 ;; ("Nabble" . "http://nabble.com/")
165 ;; ("opensubscriber" . "http://opensubscriber.com/")
167 "List of Mailing List Archives to use when stashing links.
169 These URIs are concatenated with the current message's
170 Message-Id in `notmuch-show-stash-mlarchive-link'."
171 :type '(alist :key-type (string :tag "Name")
172 :value-type (string :tag "URL"))
173 :group 'notmuch-show)
175 (defcustom notmuch-show-stash-mlarchive-link-default "Gmane"
176 "Default Mailing List Archive to use when stashing links.
178 This is used when `notmuch-show-stash-mlarchive-link' isn't
179 provided with an MLA argument nor `completing-read' input."
183 (list 'const :tag (car mla) :value (car mla)))
184 notmuch-show-stash-mlarchive-link-alist))
185 :group 'notmuch-show)
187 (defcustom notmuch-show-mark-read-tags '("-unread")
188 "List of tag changes to apply to a message when it is marked as read.
190 Tags starting with \"+\" (or not starting with either \"+\" or
191 \"-\") in the list will be added, and tags starting with \"-\"
192 will be removed from the message being marked as read.
194 For example, if you wanted to remove an \"unread\" tag and add a
195 \"read\" tag (which would make little sense), you would set:
196 (\"-unread\" \"+read\")"
197 :type '(repeat string)
198 :group 'notmuch-show)
201 (defmacro with-current-notmuch-show-message (&rest body)
202 "Evaluate body with current buffer set to the text of current message"
204 (let ((id (notmuch-show-get-message-id)))
205 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
206 (with-current-buffer buf
207 (let ((coding-system-for-read 'no-conversion))
208 (call-process notmuch-command nil t nil "show" "--format=raw" id)
210 (kill-buffer buf))))))
212 (defun notmuch-show-turn-on-visual-line-mode ()
213 "Enable Visual Line mode."
214 (visual-line-mode t))
216 (defun notmuch-show-view-all-mime-parts ()
217 "Use external viewers to view all attachments from the current message."
219 (with-current-notmuch-show-message
220 ;; We override the mm-inline-media-tests to indicate which message
221 ;; parts are already sufficiently handled by the original
222 ;; presentation of the message in notmuch-show mode. These parts
223 ;; will be inserted directly into the temporary buffer of
224 ;; with-current-notmuch-show-message and silently discarded.
226 ;; Any MIME part not explicitly mentioned here will be handled by an
227 ;; external viewer as configured in the various mailcap files.
228 (let ((mm-inline-media-tests '(
229 ("text/.*" ignore identity)
230 ("application/pgp-signature" ignore identity)
231 ("multipart/alternative" ignore identity)
232 ("multipart/mixed" ignore identity)
233 ("multipart/related" ignore identity)
235 (mm-display-parts (mm-dissect-buffer)))))
237 (defun notmuch-foreach-mime-part (function mm-handle)
238 (cond ((stringp (car mm-handle))
239 (dolist (part (cdr mm-handle))
240 (notmuch-foreach-mime-part function part)))
241 ((bufferp (car mm-handle))
242 (funcall function mm-handle))
243 (t (dolist (part mm-handle)
244 (notmuch-foreach-mime-part function part)))))
246 (defun notmuch-count-attachments (mm-handle)
248 (notmuch-foreach-mime-part
250 (let ((disposition (mm-handle-disposition p)))
251 (and (listp disposition)
252 (or (equal (car disposition) "attachment")
253 (and (equal (car disposition) "inline")
254 (assq 'filename disposition)))
259 (defun notmuch-save-attachments (mm-handle &optional queryp)
260 (notmuch-foreach-mime-part
262 (let ((disposition (mm-handle-disposition p)))
263 (and (listp disposition)
264 (or (equal (car disposition) "attachment")
265 (and (equal (car disposition) "inline")
266 (assq 'filename disposition)))
269 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
273 (defun notmuch-show-save-attachments ()
274 "Save all attachments from the current message."
276 (with-current-notmuch-show-message
277 (let ((mm-handle (mm-dissect-buffer)))
278 (notmuch-save-attachments
279 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
282 (defun notmuch-show-with-message-as-text (fn)
283 "Apply FN to a text representation of the current message.
285 FN is called with one argument, the message properties. It should
286 operation on the contents of the current buffer."
288 ;; Remake the header to ensure that all information is available.
289 (let* ((to (notmuch-show-get-to))
290 (cc (notmuch-show-get-cc))
291 (from (notmuch-show-get-from))
292 (subject (notmuch-show-get-subject))
293 (date (notmuch-show-get-date))
294 (tags (notmuch-show-get-tags))
295 (depth (notmuch-show-get-depth))
298 "Subject: " subject "\n"
300 (if (not (string= cc ""))
301 (concat "Cc: " cc "\n")
307 (mapconcat #'identity tags ", ") "\n")
309 (all (buffer-substring (notmuch-show-message-top)
310 (notmuch-show-message-bottom)))
312 (props (notmuch-show-get-message-properties))
313 (indenting notmuch-show-indent-content))
317 (indent-rigidly (point-min) (point-max) (- depth)))
318 ;; Remove the original header.
319 (goto-char (point-min))
320 (re-search-forward "^$" (point-max) nil)
321 (delete-region (point-min) (point))
323 (funcall fn props))))
325 (defun notmuch-show-print-message ()
326 "Print the current message."
328 (notmuch-show-with-message-as-text 'notmuch-print-message))
330 (defun notmuch-show-fontify-header ()
332 ((looking-at "[Tt]o:")
334 ((looking-at "[Bb]?[Cc][Cc]:")
336 ((looking-at "[Ss]ubject:")
337 'message-header-subject)
338 ((looking-at "[Ff]rom:")
339 'message-header-from)
341 'message-header-other))))
343 (overlay-put (make-overlay (point) (re-search-forward ":"))
344 'face 'message-header-name)
345 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
348 (defun notmuch-show-colour-headers ()
349 "Apply some colouring to the current headers."
350 (goto-char (point-min))
351 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
352 (notmuch-show-fontify-header)
355 (defun notmuch-show-spaces-n (n)
356 "Return a string comprised of `n' spaces."
359 (defun notmuch-show-update-tags (tags)
360 "Update the displayed tags of the current message."
362 (goto-char (notmuch-show-message-top))
363 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
364 (let ((inhibit-read-only t))
365 (replace-match (concat "("
366 (notmuch-tag-format-tags tags)
369 (defun notmuch-clean-address (address)
370 "Try to clean a single email ADDRESS for display. Return a cons
371 cell of (AUTHOR_EMAIL AUTHOR_NAME). Return (ADDRESS nil) if
374 (let (p-name p-address)
375 ;; It would be convenient to use `mail-header-parse-address',
376 ;; but that expects un-decoded mailbox parts, whereas our
377 ;; mailbox parts are already decoded (and hence may contain
378 ;; UTF-8). Given that notmuch should handle most of the awkward
379 ;; cases, some simple string deconstruction should be sufficient
382 ;; "User <user@dom.ain>" style.
383 ((string-match "\\(.*\\) <\\(.*\\)>" address)
384 (setq p-name (match-string 1 address)
385 p-address (match-string 2 address)))
387 ;; "<user@dom.ain>" style.
388 ((string-match "<\\(.*\\)>" address)
389 (setq p-address (match-string 1 address)))
393 (setq p-address address)))
396 ;; Remove elements of the mailbox part that are not relevant for
397 ;; display, even if they are required during transport:
400 (setq p-name (replace-regexp-in-string "\\\\" "" p-name))
402 ;; Outer single and double quotes, which might be nested.
405 do (setq start-of-loop p-name)
407 when (string-match "^\"\\(.*\\)\"$" p-name)
408 do (setq p-name (match-string 1 p-name))
410 when (string-match "^'\\(.*\\)'$" p-name)
411 do (setq p-name (match-string 1 p-name))
413 until (string= start-of-loop p-name)))
415 ;; If the address is 'foo@bar.com <foo@bar.com>' then show just
417 (when (string= p-name p-address)
420 (cons p-address p-name))
421 (error (cons address nil))))
423 (defun notmuch-show-clean-address (address)
424 "Try to clean a single email ADDRESS for display. Return
425 unchanged ADDRESS if parsing fails."
426 (let* ((clean-address (notmuch-clean-address address))
427 (p-address (car clean-address))
428 (p-name (cdr clean-address)))
429 ;; If no name, return just the address.
432 ;; Otherwise format the name and address together.
433 (concat p-name " <" p-address ">"))))
435 (defun notmuch-show-insert-headerline (headers date tags depth)
436 "Insert a notmuch style headerline based on HEADERS for a
437 message at DEPTH in the current thread."
438 (let ((start (point)))
439 (insert (notmuch-show-spaces-n (* notmuch-show-indent-messages-width depth))
440 (notmuch-show-clean-address (plist-get headers :From))
444 (notmuch-tag-format-tags tags)
446 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
448 (defun notmuch-show-insert-header (header header-value)
449 "Insert a single header."
450 (insert header ": " header-value "\n"))
452 (defun notmuch-show-insert-headers (headers)
453 "Insert the headers of the current message."
454 (let ((start (point)))
455 (mapc (lambda (header)
456 (let* ((header-symbol (intern (concat ":" header)))
457 (header-value (plist-get headers header-symbol)))
458 (if (and header-value
459 (not (string-equal "" header-value)))
460 (notmuch-show-insert-header header header-value))))
461 notmuch-message-headers)
464 (narrow-to-region start (point-max))
465 (run-hooks 'notmuch-show-markup-headers-hook)))))
467 (define-button-type 'notmuch-show-part-button-type
468 'action 'notmuch-show-part-button-default
471 :supertype 'notmuch-button-type)
473 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name comment)
475 (base-label (concat (when name (concat name ": "))
477 (unless (string-equal declared-type content-type)
478 (concat " (as " content-type ")"))
483 (concat "[ " base-label " ]")
484 :base-label base-label
485 :type 'notmuch-show-part-button-type
486 :notmuch-part-hidden nil))
491 (defun notmuch-show-toggle-part-invisibility (&optional button)
493 (let* ((button (or button (button-at (point))))
494 (overlay (button-get button 'overlay))
495 (lazy-part (button-get button :notmuch-lazy-part)))
496 ;; We have a part to toggle if there is an overlay or if there is a lazy part.
497 ;; If neither is present we cannot toggle the part so we just return nil.
498 (when (or overlay lazy-part)
499 (let* ((show (button-get button :notmuch-part-hidden))
500 (new-start (button-start button))
501 (button-label (button-get button :base-label))
503 (properties (text-properties-at (point)))
504 (inhibit-read-only t))
505 ;; Toggle the button itself.
506 (button-put button :notmuch-part-hidden (not show))
507 (goto-char new-start)
508 (insert "[ " button-label (if show " ]" " (hidden) ]"))
509 (set-text-properties new-start (point) properties)
510 (let ((old-end (button-end button)))
511 (move-overlay button new-start (point))
512 (delete-region (point) old-end))
513 (goto-char (min old-point (1- (button-end button))))
514 ;; Return nil if there is a lazy-part, it is empty, and we are
515 ;; trying to show it. In all other cases return t.
518 (button-put button :notmuch-lazy-part nil)
519 (notmuch-show-lazy-part lazy-part button))
520 ;; else there must be an overlay.
521 (overlay-put overlay 'invisible (not show))
524 ;; MIME part renderers
526 (defun notmuch-show-multipart/*-to-list (part)
527 (mapcar (lambda (inner-part) (plist-get inner-part :content-type))
528 (plist-get part :content)))
530 (defun notmuch-show-insert-part-multipart/alternative (msg part content-type nth depth button)
531 (let ((chosen-type (car (notmuch-multipart/alternative-choose (notmuch-show-multipart/*-to-list part))))
532 (inner-parts (plist-get part :content))
534 ;; This inserts all parts of the chosen type rather than just one,
535 ;; but it's not clear that this is the wrong thing to do - which
536 ;; should be chosen if there are more than one that match?
537 (mapc (lambda (inner-part)
538 (let* ((inner-type (plist-get inner-part :content-type))
539 (hide (not (or notmuch-show-all-multipart/alternative-parts
540 (string= chosen-type inner-type)))))
541 (notmuch-show-insert-bodypart msg inner-part depth hide)))
544 (when notmuch-show-indent-multipart
545 (indent-rigidly start (point) 1)))
548 (defun notmuch-show-setup-w3m ()
549 "Instruct w3m how to retrieve content from a \"related\" part of a message."
551 (if (boundp 'w3m-cid-retrieve-function-alist)
552 (unless (assq 'notmuch-show-mode w3m-cid-retrieve-function-alist)
553 (push (cons 'notmuch-show-mode 'notmuch-show-w3m-cid-retrieve)
554 w3m-cid-retrieve-function-alist)))
555 (setq mm-inline-text-html-with-images t))
557 (defvar w3m-current-buffer) ;; From `w3m.el'.
558 (defvar notmuch-show-w3m-cid-store nil)
559 (make-variable-buffer-local 'notmuch-show-w3m-cid-store)
561 (defun notmuch-show-w3m-cid-store-internal (content-id
566 (push (list content-id
571 notmuch-show-w3m-cid-store))
573 (defun notmuch-show-w3m-cid-store (msg part)
574 (let ((content-id (plist-get part :content-id)))
576 (notmuch-show-w3m-cid-store-internal (concat "cid:" content-id)
579 (plist-get part :content-type)
582 (defun notmuch-show-w3m-cid-retrieve (url &rest args)
583 (let ((matching-part (with-current-buffer w3m-current-buffer
584 (assoc url notmuch-show-w3m-cid-store))))
586 (let ((message-id (nth 1 matching-part))
587 (part-number (nth 2 matching-part))
588 (content-type (nth 3 matching-part))
589 (content (nth 4 matching-part)))
590 ;; If we don't already have the content, get it and cache
591 ;; it, as some messages reference the same cid: part many
592 ;; times (hundreds!), which results in many calls to
595 (setq content (notmuch-get-bodypart-internal (notmuch-id-to-query message-id)
596 part-number notmuch-show-process-crypto))
597 (with-current-buffer w3m-current-buffer
598 (notmuch-show-w3m-cid-store-internal url
607 (defun notmuch-show-insert-part-multipart/related (msg part content-type nth depth button)
608 (let ((inner-parts (plist-get part :content))
611 ;; We assume that the first part is text/html and the remainder
612 ;; things that it references.
614 ;; Stash the non-primary parts.
616 (notmuch-show-w3m-cid-store msg part))
619 ;; Render the primary part.
620 (notmuch-show-insert-bodypart msg (car inner-parts) depth)
622 (when notmuch-show-indent-multipart
623 (indent-rigidly start (point) 1)))
626 (defun notmuch-show-insert-part-multipart/signed (msg part content-type nth depth button)
627 (button-put button 'face 'notmuch-crypto-part-header)
628 ;; add signature status button if sigstatus provided
629 (if (plist-member part :sigstatus)
630 (let* ((from (notmuch-show-get-header :From msg))
631 (sigstatus (car (plist-get part :sigstatus))))
632 (notmuch-crypto-insert-sigstatus-button sigstatus from))
633 ;; if we're not adding sigstatus, tell the user how they can get it
634 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts."))
636 (let ((inner-parts (plist-get part :content))
638 ;; Show all of the parts.
639 (mapc (lambda (inner-part)
640 (notmuch-show-insert-bodypart msg inner-part depth))
643 (when notmuch-show-indent-multipart
644 (indent-rigidly start (point) 1)))
647 (defun notmuch-show-insert-part-multipart/encrypted (msg part content-type nth depth button)
648 (button-put button 'face 'notmuch-crypto-part-header)
649 ;; add encryption status button if encstatus specified
650 (if (plist-member part :encstatus)
651 (let ((encstatus (car (plist-get part :encstatus))))
652 (notmuch-crypto-insert-encstatus-button encstatus)
653 ;; add signature status button if sigstatus specified
654 (if (plist-member part :sigstatus)
655 (let* ((from (notmuch-show-get-header :From msg))
656 (sigstatus (car (plist-get part :sigstatus))))
657 (notmuch-crypto-insert-sigstatus-button sigstatus from))))
658 ;; if we're not adding encstatus, tell the user how they can get it
659 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts."))
661 (let ((inner-parts (plist-get part :content))
663 ;; Show all of the parts.
664 (mapc (lambda (inner-part)
665 (notmuch-show-insert-bodypart msg inner-part depth))
668 (when notmuch-show-indent-multipart
669 (indent-rigidly start (point) 1)))
672 (defun notmuch-show-insert-part-multipart/* (msg part content-type nth depth button)
673 (let ((inner-parts (plist-get part :content))
675 ;; Show all of the parts.
676 (mapc (lambda (inner-part)
677 (notmuch-show-insert-bodypart msg inner-part depth))
680 (when notmuch-show-indent-multipart
681 (indent-rigidly start (point) 1)))
684 (defun notmuch-show-insert-part-message/rfc822 (msg part content-type nth depth button)
685 (let* ((message (car (plist-get part :content)))
686 (body (car (plist-get message :body)))
689 ;; Override `notmuch-message-headers' to force `From' to be
691 (let ((notmuch-message-headers '("From" "Subject" "To" "Cc" "Date")))
692 (notmuch-show-insert-headers (plist-get message :headers)))
694 ;; Blank line after headers to be compatible with the normal
699 (notmuch-show-insert-bodypart msg body depth)
701 (when notmuch-show-indent-multipart
702 (indent-rigidly start (point) 1)))
705 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth button)
706 ;; For backward compatibility we want to apply the text/plain hook
707 ;; to the whole of the part including the part button if there is
709 (let ((start (if button
710 (button-start button)
712 (insert (notmuch-get-bodypart-content msg part nth notmuch-show-process-crypto))
715 (narrow-to-region start (point-max))
716 (run-hook-with-args 'notmuch-show-insert-text/plain-hook msg depth))))
719 (defun notmuch-show-insert-part-text/calendar (msg part content-type nth depth button)
720 (insert (with-temp-buffer
721 (insert (notmuch-get-bodypart-content msg part nth notmuch-show-process-crypto))
722 ;; notmuch-get-bodypart-content provides "raw", non-converted
723 ;; data. Replace CRLF with LF before icalendar can use it.
724 (goto-char (point-min))
725 (while (re-search-forward "\r\n" nil t)
726 (replace-match "\n" nil nil))
727 (let ((file (make-temp-file "notmuch-ical"))
731 (unless (icalendar-import-buffer file t)
732 (error "Icalendar import error. See *icalendar-errors* for more information"))
733 (set-buffer (get-file-buffer file))
734 (setq result (buffer-substring (point-min) (point-max)))
735 (set-buffer-modified-p nil)
736 (kill-buffer (current-buffer)))
741 ;; For backwards compatibility.
742 (defun notmuch-show-insert-part-text/x-vcalendar (msg part content-type nth depth button)
743 (notmuch-show-insert-part-text/calendar msg part content-type nth depth button))
745 (defun notmuch-show-get-mime-type-of-application/octet-stream (part)
746 ;; If we can deduce a MIME type from the filename of the attachment,
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")))
761 (defun notmuch-show-insert-part-text/html (msg part content-type nth depth button)
762 ;; text/html handler to work around bugs in renderers and our
763 ;; invisibile parts code. In particular w3m sets up a keymap which
764 ;; "leaks" outside the invisible region and causes strange effects
765 ;; in notmuch. We set mm-inline-text-html-with-w3m-keymap to nil to
766 ;; tell w3m not to set a keymap (so the normal notmuch-show-mode-map
768 (let ((mm-inline-text-html-with-w3m-keymap nil))
769 (notmuch-show-insert-part-*/* msg part content-type nth depth button)))
771 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth button)
772 ;; This handler _must_ succeed - it is the handler of last resort.
773 (notmuch-mm-display-part-inline msg part nth content-type notmuch-show-process-crypto)
776 ;; Functions for determining how to handle MIME parts.
778 (defun notmuch-show-handlers-for (content-type)
779 "Return a list of content handlers for a part of type CONTENT-TYPE."
784 ;; Reverse order of prefrence.
785 (list (intern (concat "notmuch-show-insert-part-*/*"))
787 "notmuch-show-insert-part-"
788 (car (notmuch-split-content-type content-type))
790 (intern (concat "notmuch-show-insert-part-" content-type))))
795 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth button)
796 (let ((handlers (notmuch-show-handlers-for content-type)))
797 ;; Run the content handlers until one of them returns a non-nil
800 (not (condition-case err
801 (funcall (car handlers) msg part content-type nth depth button)
803 (insert "!!! Bodypart insert error: ")
804 (insert (error-message-string err))
805 (insert " !!!\n") nil)))))
806 (setq handlers (cdr handlers))))
809 (defun notmuch-show-create-part-overlays (button beg end)
810 "Add an overlay to the part between BEG and END"
812 ;; If there is no button (i.e., the part is text/plain and the first
813 ;; part) or if the part has no content then we don't make the part
815 (when (and button (/= beg end))
816 (button-put button 'overlay (make-overlay beg end))
817 ;; Return true if we created an overlay.
820 (defun notmuch-show-record-part-information (part beg end)
821 "Store PART as a text property from BEG to END"
823 ;; Record part information. Since we already inserted subparts,
824 ;; don't override existing :notmuch-part properties.
825 (notmuch-map-text-property beg end :notmuch-part
826 (lambda (v) (or v part)))
827 ;; Make :notmuch-part front sticky and rear non-sticky so it stays
828 ;; applied to the beginning of each line when we indent the
829 ;; message. Since we're operating on arbitrary renderer output,
830 ;; watch out for sticky specs of t, which means all properties are
831 ;; front-sticky/rear-nonsticky.
832 (notmuch-map-text-property beg end 'front-sticky
833 (lambda (v) (if (listp v)
834 (pushnew :notmuch-part v)
836 (notmuch-map-text-property beg end 'rear-nonsticky
837 (lambda (v) (if (listp v)
838 (pushnew :notmuch-part v)
841 (defun notmuch-show-lazy-part (part-args button)
842 ;; Insert the lazy part after the button for the part. We would just
843 ;; move to the start of the new line following the button and insert
844 ;; the part but that point might have text properties (eg colours
845 ;; from a message header etc) so instead we start from the last
846 ;; character of the button by adding a newline and finish by
847 ;; removing the extra newline from the end of the part.
849 (goto-char (button-end button))
851 (let* ((inhibit-read-only t)
852 ;; We need to use markers for the start and end of the part
853 ;; because the part insertion functions do not guarantee
854 ;; to leave point at the end of the part.
855 (part-beg (copy-marker (point) nil))
856 (part-end (copy-marker (point) t))
857 ;; We have to save the depth as we can't find the depth
859 (depth (notmuch-show-get-depth)))
861 (narrow-to-region part-beg part-end)
862 (delete-region part-beg part-end)
863 (apply #'notmuch-show-insert-bodypart-internal part-args)
864 (indent-rigidly part-beg part-end depth))
867 (notmuch-show-record-part-information (second part-args)
868 (button-start button)
870 ;; Create the overlay. If the lazy-part turned out to be empty/not
871 ;; showable this returns nil.
872 (notmuch-show-create-part-overlays button part-beg part-end))))
874 (defun notmuch-show-insert-bodypart (msg part depth &optional hide)
875 "Insert the body part PART at depth DEPTH in the current thread.
877 If HIDE is non-nil then initially hide this part."
879 (let* ((content-type (downcase (plist-get part :content-type)))
880 (mime-type (or (and (string= content-type "application/octet-stream")
881 (notmuch-show-get-mime-type-of-application/octet-stream part))
882 (and (string= content-type "inline patch")
885 (nth (plist-get part :id))
887 ;; We omit the part button for the first (or only) part if this is text/plain.
888 (button (unless (and (string= mime-type "text/plain") (<= nth 1))
889 (notmuch-show-insert-part-header nth mime-type content-type (plist-get part :filename))))
890 (content-beg (point)))
893 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth button)
894 (button-put button :notmuch-lazy-part
895 (list msg part mime-type nth depth button)))
897 ;; Some of the body part handlers leave point somewhere up in the
898 ;; part, so we make sure that we're down at the end.
899 (goto-char (point-max))
900 ;; Ensure that the part ends with a carriage return.
903 ;; We do not create the overlay for hidden (lazy) parts until
904 ;; they are inserted.
906 (notmuch-show-create-part-overlays button content-beg (point))
908 (notmuch-show-toggle-part-invisibility button)))
909 (notmuch-show-record-part-information part beg (point))))
911 (defun notmuch-show-insert-body (msg body depth)
912 "Insert the body BODY at depth DEPTH in the current thread."
913 (mapc (lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
915 (defun notmuch-show-make-symbol (type)
916 (make-symbol (concat "notmuch-show-" type)))
918 (defun notmuch-show-strip-re (string)
919 (replace-regexp-in-string "^\\([Rr]e: *\\)+" "" string))
921 (defvar notmuch-show-previous-subject "")
922 (make-variable-buffer-local 'notmuch-show-previous-subject)
924 (defun notmuch-show-insert-msg (msg depth)
925 "Insert the message MSG at depth DEPTH in the current thread."
926 (let* ((headers (plist-get msg :headers))
927 ;; Indentation causes the buffer offset of the start/end
928 ;; points to move, so we must use markers.
929 message-start message-end
930 content-start content-end
931 headers-start headers-end
932 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
934 (setq message-start (point-marker))
936 (notmuch-show-insert-headerline headers
937 (or (if notmuch-show-relative-dates
938 (plist-get msg :date_relative)
940 (plist-get headers :Date))
941 (plist-get msg :tags) depth)
943 (setq content-start (point-marker))
945 ;; Set `headers-start' to point after the 'Subject:' header to be
946 ;; compatible with the existing implementation. This just sets it
947 ;; to after the first header.
948 (notmuch-show-insert-headers headers)
950 (goto-char content-start)
951 ;; If the subject of this message is the same as that of the
952 ;; previous message, don't display it when this message is
954 (when (not (string= notmuch-show-previous-subject
957 (setq headers-start (point-marker)))
958 (setq headers-end (point-marker))
960 (setq notmuch-show-previous-subject bare-subject)
962 ;; A blank line between the headers and the body.
964 (notmuch-show-insert-body msg (plist-get msg :body)
965 (if notmuch-show-indent-content depth 0))
966 ;; Ensure that the body ends with a newline.
969 (setq content-end (point-marker))
971 ;; Indent according to the depth in the thread.
972 (if notmuch-show-indent-content
973 (indent-rigidly content-start content-end (* notmuch-show-indent-messages-width depth)))
975 (setq message-end (point-max-marker))
977 ;; Save the extents of this message over the whole text of the
979 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
981 ;; Create overlays used to control visibility
982 (plist-put msg :headers-overlay (make-overlay headers-start headers-end))
983 (plist-put msg :message-overlay (make-overlay headers-start content-end))
985 (plist-put msg :depth depth)
987 ;; Save the properties for this message. Currently this saves the
988 ;; entire message (augmented it with other stuff), which seems
989 ;; like overkill. We might save a reduced subset (for example, not
991 (notmuch-show-set-message-properties msg)
993 ;; Set header visibility.
994 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
996 ;; Message visibility depends on whether it matched the search
998 (notmuch-show-message-visible msg (and (plist-get msg :match)
999 (not (plist-get msg :excluded))))))
1001 (defun notmuch-show-toggle-process-crypto ()
1002 "Toggle the processing of cryptographic MIME parts."
1004 (setq notmuch-show-process-crypto (not notmuch-show-process-crypto))
1005 (message (if notmuch-show-process-crypto
1006 "Processing cryptographic MIME parts."
1007 "Not processing cryptographic MIME parts."))
1008 (notmuch-show-refresh-view))
1010 (defun notmuch-show-toggle-elide-non-matching ()
1011 "Toggle the display of non-matching messages."
1013 (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages))
1014 (message (if notmuch-show-elide-non-matching-messages
1015 "Showing matching messages only."
1016 "Showing all messages."))
1017 (notmuch-show-refresh-view))
1019 (defun notmuch-show-toggle-thread-indentation ()
1020 "Toggle the indentation of threads."
1022 (setq notmuch-show-indent-content (not notmuch-show-indent-content))
1023 (message (if notmuch-show-indent-content
1024 "Content is indented."
1025 "Content is not indented."))
1026 (notmuch-show-refresh-view))
1028 (defun notmuch-show-insert-tree (tree depth)
1029 "Insert the message tree TREE at depth DEPTH in the current thread."
1030 (let ((msg (car tree))
1031 (replies (cadr tree)))
1032 ;; We test whether there is a message or just some replies.
1034 (notmuch-show-insert-msg msg depth))
1035 (notmuch-show-insert-thread replies (1+ depth))))
1037 (defun notmuch-show-insert-thread (thread depth)
1038 "Insert the thread THREAD at depth DEPTH in the current forest."
1039 (mapc (lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
1041 (defun notmuch-show-insert-forest (forest)
1042 "Insert the forest of threads FOREST."
1043 (mapc (lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
1045 (defvar notmuch-id-regexp
1047 ;; Match the id: prefix only if it begins a word (to disallow, for
1048 ;; example, matching cid:).
1050 ;; If the term starts with a ", then parse Xapian's quoted boolean
1051 ;; term syntax, which allows for anything as long as embedded
1052 ;; double quotes escaped by doubling them. We also disallow
1053 ;; newlines (which Xapian allows) to prevent runaway terms.
1054 "\"\\([^\"\n]\\|\"\"\\)*\""
1055 ;; Otherwise, parse Xapian's unquoted syntax, which goes up to the
1056 ;; next space or ). We disallow [.,;] as the last character
1057 ;; because these are probably part of the surrounding text, and not
1058 ;; part of the id. This doesn't match single character ids; meh.
1059 "\\|[^\"[:space:])][^[:space:])]*[^])[:space:].,:;?!]"
1061 "The regexp used to match id: links in messages.")
1063 (defvar notmuch-mid-regexp
1064 ;; goto-address-url-regexp matched cid: links, which have the same
1065 ;; grammar as the message ID part of a mid: link. Construct the
1066 ;; regexp using the same technique as goto-address-url-regexp.
1067 (concat "\\<mid:\\(" thing-at-point-url-path-regexp "\\)")
1068 "The regexp used to match mid: links in messages.
1072 (defun notmuch-show-buttonise-links (start end)
1073 "Buttonise URLs and mail addresses between START and END.
1075 This also turns id:\"<message id>\"-parts and mid: links into
1076 buttons for a corresponding notmuch search."
1077 (goto-address-fontify-region start end)
1081 (while (re-search-forward notmuch-id-regexp end t)
1082 (push (list (match-beginning 0) (match-end 0)
1083 (match-string-no-properties 0)) links))
1085 (while (re-search-forward notmuch-mid-regexp end t)
1086 (let* ((mid-cid (match-string-no-properties 1))
1087 (mid (save-match-data
1088 (string-match "^[^/]*" mid-cid)
1089 (url-unhex-string (match-string 0 mid-cid)))))
1090 (push (list (match-beginning 0) (match-end 0)
1091 (notmuch-id-to-query mid)) links)))
1092 (dolist (link links)
1093 ;; Remove the overlay created by goto-address-mode
1094 (remove-overlays (first link) (second link) 'goto-address t)
1095 (make-text-button (first link) (second link)
1096 :type 'notmuch-button-type
1097 'action `(lambda (arg)
1098 (notmuch-show ,(third link)))
1100 'help-echo "Mouse-1, RET: search for this message"
1101 'face goto-address-mail-face)))))
1104 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
1105 "Run \"notmuch show\" with the given thread ID and display results.
1107 The optional PARENT-BUFFER is the notmuch-search buffer from
1108 which this notmuch-show command was executed, (so that the
1109 next thread from that buffer can be show when done with this
1112 The optional QUERY-CONTEXT is a notmuch search term. Only
1113 messages from the thread matching this search term are shown if
1116 The optional BUFFER-NAME provides the name of the buffer in
1117 which the message thread is shown. If it is nil (which occurs
1118 when the command is called interactively) the argument to the
1120 (interactive "sNotmuch show: ")
1121 (let ((buffer-name (generate-new-buffer-name
1123 (concat "*notmuch-" thread-id "*")))))
1124 (switch-to-buffer (get-buffer-create buffer-name))
1125 ;; Set the default value for `notmuch-show-process-crypto' in this
1127 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1128 ;; Set the default value for
1129 ;; `notmuch-show-elide-non-matching-messages' in this buffer. If
1130 ;; there is a prefix argument, invert the default.
1131 (setq notmuch-show-elide-non-matching-messages notmuch-show-only-matching-messages)
1132 (if current-prefix-arg
1133 (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages)))
1135 (setq notmuch-show-thread-id thread-id
1136 notmuch-show-parent-buffer parent-buffer
1137 notmuch-show-query-context query-context)
1138 (notmuch-show-build-buffer)
1139 (notmuch-show-goto-first-wanted-message)
1142 (defun notmuch-show-build-buffer ()
1143 (let ((inhibit-read-only t))
1146 ;; Don't track undo information for this buffer
1147 (set 'buffer-undo-list t)
1150 (goto-char (point-min))
1152 (let* ((basic-args (list notmuch-show-thread-id))
1153 (args (if notmuch-show-query-context
1154 (append (list "\'") basic-args
1155 (list "and (" notmuch-show-query-context ")\'"))
1156 (append (list "\'") basic-args (list "\'"))))
1157 (cli-args (cons "--exclude=false"
1158 (when notmuch-show-elide-non-matching-messages
1159 (list "--entire-thread=false")))))
1161 (notmuch-show-insert-forest (notmuch-query-get-threads (append cli-args args)))
1162 ;; If the query context reduced the results to nothing, run
1164 (when (and (eq (buffer-size) 0)
1165 notmuch-show-query-context)
1166 (notmuch-show-insert-forest
1167 (notmuch-query-get-threads (append cli-args basic-args)))))
1169 (jit-lock-register #'notmuch-show-buttonise-links)
1171 ;; Set the header line to the subject of the first message.
1172 (setq header-line-format (notmuch-show-strip-re (notmuch-show-get-subject)))
1174 (run-hooks 'notmuch-show-hook))))
1176 (defun notmuch-show-capture-state ()
1177 "Capture the state of the current buffer.
1180 - the list of open messages,
1181 - the current message."
1182 (list (notmuch-show-get-message-id) (notmuch-show-get-message-ids-for-open-messages)))
1184 (defun notmuch-show-apply-state (state)
1185 "Apply STATE to the current buffer.
1188 - opening the messages previously opened,
1189 - closing all other messages,
1190 - moving to the correct current message."
1191 (let ((current (car state))
1192 (open (cadr state)))
1194 ;; Open those that were open.
1195 (goto-char (point-min))
1196 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1197 (member (notmuch-show-get-message-id) open))
1198 until (not (notmuch-show-goto-message-next)))
1200 ;; Go to the previously open message.
1201 (goto-char (point-min))
1202 (unless (loop if (string= current (notmuch-show-get-message-id))
1204 until (not (notmuch-show-goto-message-next)))
1205 (goto-char (point-min))
1206 (message "Previously current message not found."))
1207 (notmuch-show-message-adjust)))
1209 (defun notmuch-show-refresh-view (&optional reset-state)
1210 "Refresh the current view.
1212 Refreshes the current view, observing changes in display
1213 preferences. If invoked with a prefix argument (or RESET-STATE is
1214 non-nil) then the state of the buffer (open/closed messages) is
1215 reset based on the original query."
1217 (let ((inhibit-read-only t)
1218 (state (unless reset-state
1219 (notmuch-show-capture-state))))
1220 ;; erase-buffer does not seem to remove overlays, which can lead
1221 ;; to weird effects such as remaining images, so remove them
1225 (notmuch-show-build-buffer)
1227 (notmuch-show-apply-state state)
1228 ;; We're resetting state, so navigate to the first open message
1229 ;; and mark it read, just like opening a new show buffer.
1230 (notmuch-show-goto-first-wanted-message))))
1232 (defvar notmuch-show-stash-map
1233 (let ((map (make-sparse-keymap)))
1234 (define-key map "c" 'notmuch-show-stash-cc)
1235 (define-key map "d" 'notmuch-show-stash-date)
1236 (define-key map "F" 'notmuch-show-stash-filename)
1237 (define-key map "f" 'notmuch-show-stash-from)
1238 (define-key map "i" 'notmuch-show-stash-message-id)
1239 (define-key map "I" 'notmuch-show-stash-message-id-stripped)
1240 (define-key map "s" 'notmuch-show-stash-subject)
1241 (define-key map "T" 'notmuch-show-stash-tags)
1242 (define-key map "t" 'notmuch-show-stash-to)
1243 (define-key map "l" 'notmuch-show-stash-mlarchive-link)
1244 (define-key map "L" 'notmuch-show-stash-mlarchive-link-and-go)
1246 "Submap for stash commands")
1247 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
1249 (defvar notmuch-show-part-map
1250 (let ((map (make-sparse-keymap)))
1251 (define-key map "s" 'notmuch-show-save-part)
1252 (define-key map "v" 'notmuch-show-view-part)
1253 (define-key map "o" 'notmuch-show-interactively-view-part)
1254 (define-key map "|" 'notmuch-show-pipe-part)
1256 "Submap for part commands")
1257 (fset 'notmuch-show-part-map notmuch-show-part-map)
1259 (defvar notmuch-show-mode-map
1260 (let ((map (make-sparse-keymap)))
1261 (define-key map "?" 'notmuch-help)
1262 (define-key map "q" 'notmuch-kill-this-buffer)
1263 (define-key map (kbd "<C-tab>") 'widget-backward)
1264 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
1265 (define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
1266 (define-key map (kbd "TAB") 'notmuch-show-next-button)
1267 (define-key map "s" 'notmuch-search)
1268 (define-key map "m" 'notmuch-mua-new-mail)
1269 (define-key map "f" 'notmuch-show-forward-message)
1270 (define-key map "r" 'notmuch-show-reply-sender)
1271 (define-key map "R" 'notmuch-show-reply)
1272 (define-key map "|" 'notmuch-show-pipe-message)
1273 (define-key map "w" 'notmuch-show-save-attachments)
1274 (define-key map "V" 'notmuch-show-view-raw-message)
1275 (define-key map "v" 'notmuch-show-view-all-mime-parts)
1276 (define-key map "c" 'notmuch-show-stash-map)
1277 (define-key map "=" 'notmuch-show-refresh-view)
1278 (define-key map "h" 'notmuch-show-toggle-visibility-headers)
1279 (define-key map "*" 'notmuch-show-tag-all)
1280 (define-key map "-" 'notmuch-show-remove-tag)
1281 (define-key map "+" 'notmuch-show-add-tag)
1282 (define-key map "X" 'notmuch-show-archive-thread-then-exit)
1283 (define-key map "x" 'notmuch-show-archive-message-then-next-or-exit)
1284 (define-key map "A" 'notmuch-show-archive-thread-then-next)
1285 (define-key map "a" 'notmuch-show-archive-message-then-next-or-next-thread)
1286 (define-key map "N" 'notmuch-show-next-message)
1287 (define-key map "P" 'notmuch-show-previous-message)
1288 (define-key map "n" 'notmuch-show-next-open-message)
1289 (define-key map "p" 'notmuch-show-previous-open-message)
1290 (define-key map (kbd "M-n") 'notmuch-show-next-thread-show)
1291 (define-key map (kbd "M-p") 'notmuch-show-previous-thread-show)
1292 (define-key map (kbd "DEL") 'notmuch-show-rewind)
1293 (define-key map " " 'notmuch-show-advance-and-archive)
1294 (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
1295 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
1296 (define-key map "#" 'notmuch-show-print-message)
1297 (define-key map "!" 'notmuch-show-toggle-elide-non-matching)
1298 (define-key map "$" 'notmuch-show-toggle-process-crypto)
1299 (define-key map "<" 'notmuch-show-toggle-thread-indentation)
1300 (define-key map "t" 'toggle-truncate-lines)
1301 (define-key map "." 'notmuch-show-part-map)
1303 "Keymap for \"notmuch show\" buffers.")
1304 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
1306 (defun notmuch-show-mode ()
1307 "Major mode for viewing a thread with notmuch.
1309 This buffer contains the results of the \"notmuch show\" command
1310 for displaying a single thread of email from your email archives.
1312 By default, various components of email messages, (citations,
1313 signatures, already-read messages), are hidden. You can make
1314 these parts visible by clicking with the mouse button or by
1315 pressing RET after positioning the cursor on a hidden part, (for
1316 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
1318 Reading the thread sequentially is well-supported by pressing
1319 \\[notmuch-show-advance-and-archive]. This will scroll the current message (if necessary), advance
1320 to the next message, or advance to the next thread (if already on
1321 the last message of a thread).
1323 Other commands are available to read or manipulate the thread
1324 more selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages
1325 without removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread
1326 without scrolling through with \\[notmuch-show-advance-and-archive]).
1328 You can add or remove arbitrary tags from the current message with
1329 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
1331 All currently available key bindings:
1333 \\{notmuch-show-mode-map}"
1335 (kill-all-local-variables)
1336 (use-local-map notmuch-show-mode-map)
1337 (setq major-mode 'notmuch-show-mode
1338 mode-name "notmuch-show")
1339 (setq buffer-read-only t
1342 (defun notmuch-show-move-to-message-top ()
1343 (goto-char (notmuch-show-message-top)))
1345 (defun notmuch-show-move-to-message-bottom ()
1346 (goto-char (notmuch-show-message-bottom)))
1348 (defun notmuch-show-message-adjust ()
1351 ;; Movement related functions.
1353 ;; There's some strangeness here where a text property applied to a
1354 ;; region a->b is not found when point is at b. We walk backwards
1355 ;; until finding the property.
1356 (defun notmuch-show-message-extent ()
1359 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
1363 (defun notmuch-show-message-top ()
1364 (car (notmuch-show-message-extent)))
1366 (defun notmuch-show-message-bottom ()
1367 (cdr (notmuch-show-message-extent)))
1369 (defun notmuch-show-goto-message-next ()
1370 (let ((start (point)))
1371 (notmuch-show-move-to-message-bottom)
1377 (defun notmuch-show-goto-message-previous ()
1378 (notmuch-show-move-to-message-top)
1382 (notmuch-show-move-to-message-top)
1385 (defun notmuch-show-mapc (function)
1386 "Iterate through all messages in the current thread with
1387 `notmuch-show-goto-message-next' and call FUNCTION for side
1390 (goto-char (point-min))
1391 (loop do (funcall function)
1392 while (notmuch-show-goto-message-next))))
1394 ;; Functions relating to the visibility of messages and their
1397 (defun notmuch-show-message-visible (props visible-p)
1398 (overlay-put (plist-get props :message-overlay) 'invisible (not visible-p))
1399 (notmuch-show-set-prop :message-visible visible-p props))
1401 (defun notmuch-show-headers-visible (props visible-p)
1402 (overlay-put (plist-get props :headers-overlay) 'invisible (not visible-p))
1403 (notmuch-show-set-prop :headers-visible visible-p props))
1405 ;; Functions for setting and getting attributes of the current
1408 (defun notmuch-show-set-message-properties (props)
1410 (notmuch-show-move-to-message-top)
1411 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
1413 (defun notmuch-show-get-message-properties ()
1414 "Return the properties of the current message as a plist.
1416 Some useful entries are:
1417 :headers - Property list containing the headers :Date, :Subject, :From, etc.
1418 :body - Body of the message
1419 :tags - Tags for this message"
1421 (notmuch-show-move-to-message-top)
1422 (get-text-property (point) :notmuch-message-properties)))
1424 (defun notmuch-show-get-part-properties ()
1425 "Return the properties of the innermost part containing point.
1427 This is the part property list retrieved from the CLI. Signals
1428 an error if there is no part containing point."
1429 (or (get-text-property (point) :notmuch-part)
1430 (error "No message part here")))
1432 (defun notmuch-show-set-prop (prop val &optional props)
1433 (let ((inhibit-read-only t)
1435 (notmuch-show-get-message-properties))))
1436 (plist-put props prop val)
1437 (notmuch-show-set-message-properties props)))
1439 (defun notmuch-show-get-prop (prop &optional props)
1440 (let ((props (or props
1441 (notmuch-show-get-message-properties))))
1442 (plist-get props prop)))
1444 (defun notmuch-show-get-message-id (&optional bare)
1445 "Return an id: query for the Message-Id of the current message.
1447 If optional argument BARE is non-nil, return
1448 the Message-Id without id: prefix and escaping."
1450 (notmuch-show-get-prop :id)
1451 (notmuch-id-to-query (notmuch-show-get-prop :id))))
1453 (defun notmuch-show-get-messages-ids ()
1454 "Return all id: queries of messages in the current thread."
1455 (let ((message-ids))
1457 (lambda () (push (notmuch-show-get-message-id) message-ids)))
1460 (defun notmuch-show-get-messages-ids-search ()
1461 "Return a search string for all message ids of messages in the
1463 (mapconcat 'identity (notmuch-show-get-messages-ids) " or "))
1465 ;; dme: Would it make sense to use a macro for many of these?
1467 (defun notmuch-show-get-filename ()
1468 "Return the filename of the current message."
1469 (notmuch-show-get-prop :filename))
1471 (defun notmuch-show-get-header (header &optional props)
1472 "Return the named header of the current message, if any."
1473 (plist-get (notmuch-show-get-prop :headers props) header))
1475 (defun notmuch-show-get-cc ()
1476 (notmuch-show-get-header :Cc))
1478 (defun notmuch-show-get-date ()
1479 (notmuch-show-get-header :Date))
1481 (defun notmuch-show-get-from ()
1482 (notmuch-show-get-header :From))
1484 (defun notmuch-show-get-subject ()
1485 (notmuch-show-get-header :Subject))
1487 (defun notmuch-show-get-to ()
1488 (notmuch-show-get-header :To))
1490 (defun notmuch-show-get-depth ()
1491 (notmuch-show-get-prop :depth))
1493 (defun notmuch-show-set-tags (tags)
1494 "Set the tags of the current message."
1495 (notmuch-show-set-prop :tags tags)
1496 (notmuch-show-update-tags tags))
1498 (defun notmuch-show-get-tags ()
1499 "Return the tags of the current message."
1500 (notmuch-show-get-prop :tags))
1502 (defun notmuch-show-message-visible-p ()
1503 "Is the current message visible?"
1504 (notmuch-show-get-prop :message-visible))
1506 (defun notmuch-show-headers-visible-p ()
1507 "Are the headers of the current message visible?"
1508 (notmuch-show-get-prop :headers-visible))
1510 (defun notmuch-show-mark-read (&optional unread)
1511 "Mark the current message as read.
1513 Mark the current message as read by applying the tag changes in
1514 `notmuch-show-mark-read-tags' to it (remove the \"unread\" tag by
1515 default). If a prefix argument is given, the message will be
1516 marked as unread, i.e. the tag changes in
1517 `notmuch-show-mark-read-tags' will be reversed."
1519 (when notmuch-show-mark-read-tags
1520 (apply 'notmuch-show-tag-message
1521 (notmuch-tag-change-list notmuch-show-mark-read-tags unread))))
1523 ;; Functions for getting attributes of several messages in the current
1526 (defun notmuch-show-get-message-ids-for-open-messages ()
1527 "Return a list of all id: queries for open messages in the current thread."
1529 (let (message-ids done)
1530 (goto-char (point-min))
1532 (if (notmuch-show-message-visible-p)
1533 (setq message-ids (append message-ids (list (notmuch-show-get-message-id)))))
1534 (setq done (not (notmuch-show-goto-message-next)))
1539 ;; Commands typically bound to keys.
1541 (defun notmuch-show-advance ()
1542 "Advance through thread.
1544 If the current message in the thread is not yet fully visible,
1545 scroll by a near screenful to read more of the message.
1547 Otherwise, (the end of the current message is already within the
1548 current window), advance to the next open message."
1550 (let* ((end-of-this-message (notmuch-show-message-bottom))
1551 (visible-end-of-this-message (1- end-of-this-message))
1553 (while (invisible-p visible-end-of-this-message)
1554 (setq visible-end-of-this-message
1556 (1- (previous-single-char-property-change
1557 visible-end-of-this-message 'invisible)))))
1559 ;; Ideally we would test `end-of-this-message' against the result
1560 ;; of `window-end', but that doesn't account for the fact that
1561 ;; the end of the message might be hidden.
1562 ((and visible-end-of-this-message
1563 (> visible-end-of-this-message (window-end)))
1564 ;; The bottom of this message is not visible - scroll.
1567 ((not (= end-of-this-message (point-max)))
1568 ;; This is not the last message - move to the next visible one.
1569 (notmuch-show-next-open-message))
1571 ((not (= (point) (point-max)))
1572 ;; This is the last message, but the cursor is not at the end of
1573 ;; the buffer. Move it there.
1574 (goto-char (point-max)))
1577 ;; This is the last message - change the return value
1581 (defun notmuch-show-advance-and-archive ()
1582 "Advance through thread and archive.
1584 This command is intended to be one of the simplest ways to
1585 process a thread of email. It works exactly like
1586 notmuch-show-advance, in that it scrolls through messages in a
1587 show buffer, except that when it gets to the end of the buffer it
1588 archives the entire current thread, (apply changes in
1589 `notmuch-archive-tags'), kills the buffer, and displays the next
1590 thread from the search from which this thread was originally
1593 (if (notmuch-show-advance)
1594 (notmuch-show-archive-thread-then-next)))
1596 (defun notmuch-show-rewind ()
1597 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
1599 Specifically, if the beginning of the previous email is fewer
1600 than `window-height' lines from the current point, move to it
1601 just like `notmuch-show-previous-message'.
1603 Otherwise, just scroll down a screenful of the current message.
1605 This command does not modify any message tags, (it does not undo
1606 any effects from previous calls to
1607 `notmuch-show-advance-and-archive'."
1609 (let ((start-of-message (notmuch-show-message-top))
1610 (start-of-window (window-start)))
1612 ;; Either this message is properly aligned with the start of the
1613 ;; window or the start of this message is not visible on the
1615 ((or (= start-of-message start-of-window)
1616 (< start-of-message start-of-window))
1618 ;; If a small number of lines from the previous message are
1619 ;; visible, realign so that the top of the current message is at
1620 ;; the top of the screen.
1621 (when (<= (count-screen-lines (window-start) start-of-message)
1622 next-screen-context-lines)
1623 (goto-char (notmuch-show-message-top))
1624 (notmuch-show-message-adjust))
1625 ;; Move to the top left of the window.
1626 (goto-char (window-start)))
1628 ;; Move to the previous message.
1629 (notmuch-show-previous-message)))))
1631 (defun notmuch-show-reply (&optional prompt-for-sender)
1632 "Reply to the sender and all recipients of the current message."
1634 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender t))
1636 (defun notmuch-show-reply-sender (&optional prompt-for-sender)
1637 "Reply to the sender of the current message."
1639 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender nil))
1641 (defun notmuch-show-forward-message (&optional prompt-for-sender)
1642 "Forward the current message."
1644 (with-current-notmuch-show-message
1645 (notmuch-mua-new-forward-message prompt-for-sender)))
1647 (defun notmuch-show-next-message (&optional pop-at-end)
1648 "Show the next message.
1650 If a prefix argument is given and this is the last message in the
1651 thread, navigate to the next thread in the parent search buffer."
1653 (if (notmuch-show-goto-message-next)
1655 (notmuch-show-mark-read)
1656 (notmuch-show-message-adjust))
1658 (notmuch-show-next-thread)
1659 (goto-char (point-max)))))
1661 (defun notmuch-show-previous-message ()
1662 "Show the previous message or the start of the current message."
1664 (if (= (point) (notmuch-show-message-top))
1665 (notmuch-show-goto-message-previous)
1666 (notmuch-show-move-to-message-top))
1667 (notmuch-show-mark-read)
1668 (notmuch-show-message-adjust))
1670 (defun notmuch-show-next-open-message (&optional pop-at-end)
1671 "Show the next open message.
1673 If a prefix argument is given and this is the last open message
1674 in the thread, navigate to the next thread in the parent search
1675 buffer. Return t if there was a next open message in the thread
1676 to show, nil otherwise."
1679 (while (and (setq r (notmuch-show-goto-message-next))
1680 (not (notmuch-show-message-visible-p))))
1683 (notmuch-show-mark-read)
1684 (notmuch-show-message-adjust))
1686 (notmuch-show-next-thread)
1687 (goto-char (point-max))))
1690 (defun notmuch-show-next-matching-message ()
1691 "Show the next matching message."
1694 (while (and (setq r (notmuch-show-goto-message-next))
1695 (not (notmuch-show-get-prop :match))))
1698 (notmuch-show-mark-read)
1699 (notmuch-show-message-adjust))
1700 (goto-char (point-max)))))
1702 (defun notmuch-show-open-if-matched ()
1703 "Open a message if it is matched (whether or not excluded)."
1704 (let ((props (notmuch-show-get-message-properties)))
1705 (notmuch-show-message-visible props (plist-get props :match))))
1707 (defun notmuch-show-goto-first-wanted-message ()
1708 "Move to the first open message and mark it read"
1709 (goto-char (point-min))
1710 (if (notmuch-show-message-visible-p)
1711 (notmuch-show-mark-read)
1712 (notmuch-show-next-open-message))
1714 ;; There are no matched non-excluded messages so open all matched
1715 ;; (necessarily excluded) messages and go to the first.
1716 (notmuch-show-mapc 'notmuch-show-open-if-matched)
1717 (force-window-update)
1718 (goto-char (point-min))
1719 (if (notmuch-show-message-visible-p)
1720 (notmuch-show-mark-read)
1721 (notmuch-show-next-open-message))))
1723 (defun notmuch-show-previous-open-message ()
1724 "Show the previous open message."
1726 (while (and (if (= (point) (notmuch-show-message-top))
1727 (notmuch-show-goto-message-previous)
1728 (notmuch-show-move-to-message-top))
1729 (not (notmuch-show-message-visible-p))))
1730 (notmuch-show-mark-read)
1731 (notmuch-show-message-adjust))
1733 (defun notmuch-show-view-raw-message ()
1734 "View the file holding the current message."
1736 (let* ((id (notmuch-show-get-message-id))
1737 (buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
1738 (call-process notmuch-command nil buf nil "show" "--format=raw" id)
1739 (switch-to-buffer buf)
1740 (goto-char (point-min))
1741 (set-buffer-modified-p nil)
1742 (view-buffer buf 'kill-buffer-if-not-modified)))
1744 (defun notmuch-show-pipe-message (entire-thread command)
1745 "Pipe the contents of the current message (or thread) to the given command.
1747 The given command will be executed with the raw contents of the
1748 current email message as stdin. Anything printed by the command
1749 to stdout or stderr will appear in the *notmuch-pipe* buffer.
1751 When invoked with a prefix argument, the command will receive all
1752 open messages in the current thread (formatted as an mbox) rather
1753 than only the current message."
1754 (interactive "P\nsPipe message to command: ")
1755 (let (shell-command)
1758 (concat notmuch-command " show --format=mbox --exclude=false "
1759 (shell-quote-argument
1760 (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
1763 (concat notmuch-command " show --format=raw "
1764 (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
1765 (let ((buf (get-buffer-create (concat "*notmuch-pipe*"))))
1766 (with-current-buffer buf
1767 (setq buffer-read-only nil)
1769 (let ((exit-code (call-process-shell-command shell-command nil buf)))
1770 (goto-char (point-max))
1771 (set-buffer-modified-p nil)
1772 (setq buffer-read-only t)
1773 (unless (zerop exit-code)
1774 (switch-to-buffer-other-window buf)
1775 (message (format "Command '%s' exited abnormally with code %d"
1776 shell-command exit-code))))))))
1778 (defun notmuch-show-tag-message (&rest tag-changes)
1779 "Change tags for the current message.
1781 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1782 (let* ((current-tags (notmuch-show-get-tags))
1783 (new-tags (notmuch-update-tags current-tags tag-changes)))
1784 (unless (equal current-tags new-tags)
1785 (notmuch-tag (notmuch-show-get-message-id) tag-changes)
1786 (notmuch-show-set-tags new-tags))))
1788 (defun notmuch-show-tag (&optional tag-changes)
1789 "Change tags for the current message.
1791 See `notmuch-tag' for information on the format of TAG-CHANGES."
1793 (let* ((tag-changes (notmuch-tag (notmuch-show-get-message-id) tag-changes))
1794 (current-tags (notmuch-show-get-tags))
1795 (new-tags (notmuch-update-tags current-tags tag-changes)))
1796 (unless (equal current-tags new-tags)
1797 (notmuch-show-set-tags new-tags))))
1799 (defun notmuch-show-tag-all (&optional tag-changes)
1800 "Change tags for all messages in the current show buffer.
1802 See `notmuch-tag' for information on the format of TAG-CHANGES."
1804 (setq tag-changes (notmuch-tag (notmuch-show-get-messages-ids-search) tag-changes))
1807 (let* ((current-tags (notmuch-show-get-tags))
1808 (new-tags (notmuch-update-tags current-tags tag-changes)))
1809 (unless (equal current-tags new-tags)
1810 (notmuch-show-set-tags new-tags))))))
1812 (defun notmuch-show-add-tag ()
1813 "Same as `notmuch-show-tag' but sets initial input to '+'."
1815 (notmuch-show-tag "+"))
1817 (defun notmuch-show-remove-tag ()
1818 "Same as `notmuch-show-tag' but sets initial input to '-'."
1820 (notmuch-show-tag "-"))
1822 (defun notmuch-show-toggle-visibility-headers ()
1823 "Toggle the visibility of the current message headers."
1825 (let ((props (notmuch-show-get-message-properties)))
1826 (notmuch-show-headers-visible
1828 (not (plist-get props :headers-visible))))
1829 (force-window-update))
1831 (defun notmuch-show-toggle-message ()
1832 "Toggle the visibility of the current message."
1834 (let ((props (notmuch-show-get-message-properties)))
1835 (notmuch-show-message-visible
1837 (not (plist-get props :message-visible))))
1838 (force-window-update))
1840 (defun notmuch-show-open-or-close-all ()
1841 "Set the visibility all of the messages in the current thread.
1842 By default make all of the messages visible. With a prefix
1843 argument, hide all of the messages."
1846 (goto-char (point-min))
1847 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1848 (not current-prefix-arg))
1849 until (not (notmuch-show-goto-message-next))))
1850 (force-window-update))
1852 (defun notmuch-show-next-button ()
1853 "Advance point to the next button in the buffer."
1857 (defun notmuch-show-previous-button ()
1858 "Move point back to the previous button in the buffer."
1860 (backward-button 1))
1862 (defun notmuch-show-next-thread (&optional show previous)
1863 "Move to the next item in the search results, if any.
1865 If SHOW is non-nil, open the next item in a show
1866 buffer. Otherwise just highlight the next item in the search
1867 buffer. If PREVIOUS is non-nil, move to the previous item in the
1868 search results instead."
1870 (let ((parent-buffer notmuch-show-parent-buffer))
1871 (notmuch-kill-this-buffer)
1872 (when (buffer-live-p parent-buffer)
1873 (switch-to-buffer parent-buffer)
1875 (notmuch-search-previous-thread)
1876 (notmuch-search-next-thread))
1878 (notmuch-search-show-thread)))))
1880 (defun notmuch-show-next-thread-show ()
1881 "Show the next thread in the search results, if any."
1883 (notmuch-show-next-thread t))
1885 (defun notmuch-show-previous-thread-show ()
1886 "Show the previous thread in the search results, if any."
1888 (notmuch-show-next-thread t t))
1890 (defun notmuch-show-archive-thread (&optional unarchive)
1891 "Archive each message in thread.
1893 Archive each message currently shown by applying the tag changes
1894 in `notmuch-archive-tags' to each. If a prefix argument is given,
1895 the messages will be \"unarchived\", i.e. the tag changes in
1896 `notmuch-archive-tags' will be reversed.
1898 Note: This command is safe from any race condition of new messages
1899 being delivered to the same thread. It does not archive the
1900 entire thread, but only the messages shown in the current
1903 (when notmuch-archive-tags
1904 (notmuch-show-tag-all
1905 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
1907 (defun notmuch-show-archive-thread-then-next ()
1908 "Archive all messages in the current buffer, then show next thread from search."
1910 (notmuch-show-archive-thread)
1911 (notmuch-show-next-thread t))
1913 (defun notmuch-show-archive-thread-then-exit ()
1914 "Archive all messages in the current buffer, then exit back to search results."
1916 (notmuch-show-archive-thread)
1917 (notmuch-show-next-thread))
1919 (defun notmuch-show-archive-message (&optional unarchive)
1920 "Archive the current message.
1922 Archive the current message by applying the tag changes in
1923 `notmuch-archive-tags' to it. If a prefix argument is given, the
1924 message will be \"unarchived\", i.e. the tag changes in
1925 `notmuch-archive-tags' will be reversed."
1927 (when notmuch-archive-tags
1928 (apply 'notmuch-show-tag-message
1929 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
1931 (defun notmuch-show-archive-message-then-next-or-exit ()
1932 "Archive the current message, then show the next open message in the current thread.
1934 If at the last open message in the current thread, then exit back
1937 (notmuch-show-archive-message)
1938 (notmuch-show-next-open-message t))
1940 (defun notmuch-show-archive-message-then-next-or-next-thread ()
1941 "Archive the current message, then show the next open message in the current thread.
1943 If at the last open message in the current thread, then show next
1944 thread from search."
1946 (notmuch-show-archive-message)
1947 (unless (notmuch-show-next-open-message)
1948 (notmuch-show-next-thread t)))
1950 (defun notmuch-show-stash-cc ()
1951 "Copy CC field of current message to kill-ring."
1953 (notmuch-common-do-stash (notmuch-show-get-cc)))
1955 (defun notmuch-show-stash-date ()
1956 "Copy date of current message to kill-ring."
1958 (notmuch-common-do-stash (notmuch-show-get-date)))
1960 (defun notmuch-show-stash-filename ()
1961 "Copy filename of current message to kill-ring."
1963 (notmuch-common-do-stash (notmuch-show-get-filename)))
1965 (defun notmuch-show-stash-from ()
1966 "Copy From address of current message to kill-ring."
1968 (notmuch-common-do-stash (notmuch-show-get-from)))
1970 (defun notmuch-show-stash-message-id (&optional stash-thread-id)
1971 "Copy id: query matching the current message to kill-ring.
1973 If invoked with a prefix argument (or STASH-THREAD-ID is
1974 non-nil), copy thread: query matching the current thread to
1978 (notmuch-common-do-stash notmuch-show-thread-id)
1979 (notmuch-common-do-stash (notmuch-show-get-message-id))))
1981 (defun notmuch-show-stash-message-id-stripped ()
1982 "Copy message ID of current message (sans `id:' prefix) to kill-ring."
1984 (notmuch-common-do-stash (notmuch-show-get-message-id t)))
1986 (defun notmuch-show-stash-subject ()
1987 "Copy Subject field of current message to kill-ring."
1989 (notmuch-common-do-stash (notmuch-show-get-subject)))
1991 (defun notmuch-show-stash-tags ()
1992 "Copy tags of current message to kill-ring as a comma separated list."
1994 (notmuch-common-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1996 (defun notmuch-show-stash-to ()
1997 "Copy To address of current message to kill-ring."
1999 (notmuch-common-do-stash (notmuch-show-get-to)))
2001 (defun notmuch-show-stash-mlarchive-link (&optional mla)
2002 "Copy an ML Archive URI for the current message to the kill-ring.
2004 This presumes that the message is available at the selected Mailing List Archive.
2006 If optional argument MLA is non-nil, use the provided key instead of prompting
2007 the user (see `notmuch-show-stash-mlarchive-link-alist')."
2009 (notmuch-common-do-stash
2012 (let ((completion-ignore-case t))
2014 "Mailing List Archive: "
2015 notmuch-show-stash-mlarchive-link-alist
2016 nil t nil nil notmuch-show-stash-mlarchive-link-default)))
2017 notmuch-show-stash-mlarchive-link-alist))
2018 (notmuch-show-get-message-id t))))
2020 (defun notmuch-show-stash-mlarchive-link-and-go (&optional mla)
2021 "Copy an ML Archive URI for the current message to the kill-ring and visit it.
2023 This presumes that the message is available at the selected Mailing List Archive.
2025 If optional argument MLA is non-nil, use the provided key instead of prompting
2026 the user (see `notmuch-show-stash-mlarchive-link-alist')."
2028 (notmuch-show-stash-mlarchive-link mla)
2029 (browse-url (current-kill 0 t)))
2031 ;; Interactive part functions and their helpers
2033 (defun notmuch-show-generate-part-buffer (message-id nth)
2034 "Return a temporary buffer containing the specified part's content."
2035 (let ((buf (generate-new-buffer " *notmuch-part*"))
2036 (process-crypto notmuch-show-process-crypto))
2037 (with-current-buffer buf
2038 (setq notmuch-show-process-crypto process-crypto)
2039 ;; Always acquires the part via `notmuch part', even if it is
2040 ;; available in the JSON output.
2041 (insert (notmuch-get-bodypart-internal message-id nth notmuch-show-process-crypto)))
2044 (defun notmuch-show-current-part-handle ()
2045 "Return an mm-handle for the part containing point.
2047 This creates a temporary buffer for the part's content; the
2048 caller is responsible for killing this buffer as appropriate."
2049 (let* ((part (notmuch-show-get-part-properties))
2050 (message-id (notmuch-show-get-message-id))
2051 (nth (plist-get part :id))
2052 (buf (notmuch-show-generate-part-buffer message-id nth))
2053 (content-type (plist-get part :content-type))
2054 (filename (plist-get part :filename))
2055 (disposition (if filename `(attachment (filename . ,filename)))))
2056 (mm-make-handle buf (list content-type) nil nil disposition)))
2058 (defun notmuch-show-apply-to-current-part-handle (fn)
2059 "Apply FN to an mm-handle for the part containing point.
2061 This ensures that the temporary buffer created for the mm-handle
2062 is destroyed when FN returns."
2063 (let ((handle (notmuch-show-current-part-handle)))
2066 (kill-buffer (mm-handle-buffer handle)))))
2068 (defun notmuch-show-part-button-default (&optional button)
2070 (let ((button (or button (button-at (point)))))
2071 ;; Try to toggle the part, if that fails then call the default
2072 ;; action. The toggle fails if the part has no emacs renderable
2074 (unless (notmuch-show-toggle-part-invisibility button)
2075 (call-interactively notmuch-show-part-button-default-action))))
2077 (defun notmuch-show-save-part ()
2078 "Save the MIME part containing point to a file."
2080 (notmuch-show-apply-to-current-part-handle #'mm-save-part))
2082 (defun notmuch-show-view-part ()
2083 "View the MIME part containing point in an external viewer."
2085 ;; Set mm-inlined-types to nil to force an external viewer
2086 (let ((mm-inlined-types nil))
2087 (notmuch-show-apply-to-current-part-handle #'mm-display-part)))
2089 (defun notmuch-show-interactively-view-part ()
2090 "View the MIME part containing point, prompting for a viewer."
2092 (notmuch-show-apply-to-current-part-handle #'mm-interactively-view-part))
2094 (defun notmuch-show-pipe-part ()
2095 "Pipe the MIME part containing point to an external command."
2097 (notmuch-show-apply-to-current-part-handle #'mm-pipe-part))
2100 (provide 'notmuch-show)