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))
48 (defcustom notmuch-message-headers '("Subject" "To" "Cc" "Date")
49 "Headers that should be shown in a message, in this order.
51 For an open message, all of these headers will be made visible
52 according to `notmuch-message-headers-visible' or can be toggled
53 with `notmuch-show-toggle-visibility-headers'. For a closed message,
54 only the first header in the list will be visible."
55 :type '(repeat string)
58 (defcustom notmuch-message-headers-visible t
59 "Should the headers be visible by default?
61 If this value is non-nil, then all of the headers defined in
62 `notmuch-message-headers' will be visible by default in the display
63 of each message. Otherwise, these headers will be hidden and
64 `notmuch-show-toggle-visibility-headers' can be used to make them
65 visible for any given message."
69 (defcustom notmuch-show-relative-dates t
70 "Display relative dates in the message summary line."
74 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
75 "A list of functions called to decorate the headers listed in
76 `notmuch-message-headers'.")
78 (defcustom notmuch-show-hook '(notmuch-show-turn-on-visual-line-mode)
79 "Functions called after populating a `notmuch-show' buffer."
81 :options '(notmuch-show-turn-on-visual-line-mode)
83 :group 'notmuch-hooks)
85 (defcustom notmuch-show-insert-text/plain-hook '(notmuch-wash-wrap-long-lines
86 notmuch-wash-tidy-citations
87 notmuch-wash-elide-blank-lines
88 notmuch-wash-excerpt-citations)
89 "Functions used to improve the display of text/plain parts."
91 :options '(notmuch-wash-convert-inline-patch-to-part
92 notmuch-wash-wrap-long-lines
93 notmuch-wash-tidy-citations
94 notmuch-wash-elide-blank-lines
95 notmuch-wash-excerpt-citations)
97 :group 'notmuch-hooks)
99 ;; Mostly useful for debugging.
100 (defcustom notmuch-show-all-multipart/alternative-parts nil
101 "Should all parts of multipart/alternative parts be shown?"
103 :group 'notmuch-show)
105 (defcustom notmuch-show-indent-messages-width 1
106 "Width of message indentation in threads.
108 Messages are shown indented according to their depth in a thread.
109 This variable determines the width of this indentation measured
110 in number of blanks. Defaults to `1', choose `0' to disable
113 :group 'notmuch-show)
115 (defcustom notmuch-show-indent-multipart nil
116 "Should the sub-parts of a multipart/* part be indented?"
117 ;; dme: Not sure which is a good default.
119 :group 'notmuch-show)
121 (defcustom notmuch-show-part-button-default-action 'notmuch-show-save-part
122 "Default part header button action (on ENTER or mouse click)."
124 :type '(choice (const :tag "Save part"
125 notmuch-show-save-part)
126 (const :tag "View part"
127 notmuch-show-view-part)
128 (const :tag "View interactively"
129 notmuch-show-interactively-view-part)))
131 (defcustom notmuch-show-only-matching-messages nil
132 "Only matching messages are shown by default."
134 :group 'notmuch-show)
136 (defvar notmuch-show-thread-id nil)
137 (make-variable-buffer-local 'notmuch-show-thread-id)
138 (put 'notmuch-show-thread-id 'permanent-local t)
140 (defvar notmuch-show-parent-buffer nil)
141 (make-variable-buffer-local 'notmuch-show-parent-buffer)
142 (put 'notmuch-show-parent-buffer 'permanent-local t)
144 (defvar notmuch-show-query-context nil)
145 (make-variable-buffer-local 'notmuch-show-query-context)
146 (put 'notmuch-show-query-context 'permanent-local t)
148 (defvar notmuch-show-process-crypto nil)
149 (make-variable-buffer-local 'notmuch-show-process-crypto)
150 (put 'notmuch-show-process-crypto 'permanent-local t)
152 (defvar notmuch-show-elide-non-matching-messages nil)
153 (make-variable-buffer-local 'notmuch-show-elide-non-matching-messages)
154 (put 'notmuch-show-elide-non-matching-messages 'permanent-local t)
156 (defvar notmuch-show-indent-content t)
157 (make-variable-buffer-local 'notmuch-show-indent-content)
158 (put 'notmuch-show-indent-content 'permanent-local t)
160 (defcustom notmuch-show-stash-mlarchive-link-alist
161 '(("Gmane" . "http://mid.gmane.org/")
162 ("MARC" . "http://marc.info/?i=")
163 ("Mail Archive, The" . "http://mail-archive.com/search?l=mid&q=")
164 ("LKML" . "http://lkml.kernel.org/r/")
165 ;; FIXME: can these services be searched by `Message-Id' ?
166 ;; ("MarkMail" . "http://markmail.org/")
167 ;; ("Nabble" . "http://nabble.com/")
168 ;; ("opensubscriber" . "http://opensubscriber.com/")
170 "List of Mailing List Archives to use when stashing links.
172 These URIs are concatenated with the current message's
173 Message-Id in `notmuch-show-stash-mlarchive-link'."
174 :type '(alist :key-type (string :tag "Name")
175 :value-type (string :tag "URL"))
176 :group 'notmuch-show)
178 (defcustom notmuch-show-stash-mlarchive-link-default "Gmane"
179 "Default Mailing List Archive to use when stashing links.
181 This is used when `notmuch-show-stash-mlarchive-link' isn't
182 provided with an MLA argument nor `completing-read' input."
186 (list 'const :tag (car mla) :value (car mla)))
187 notmuch-show-stash-mlarchive-link-alist))
188 :group 'notmuch-show)
190 (defcustom notmuch-show-mark-read-tags '("-unread")
191 "List of tag changes to apply to a message when it is marked as read.
193 Tags starting with \"+\" (or not starting with either \"+\" or
194 \"-\") in the list will be added, and tags starting with \"-\"
195 will be removed from the message being marked as read.
197 For example, if you wanted to remove an \"unread\" tag and add a
198 \"read\" tag (which would make little sense), you would set:
199 (\"-unread\" \"+read\")"
200 :type '(repeat string)
201 :group 'notmuch-show)
204 (defmacro with-current-notmuch-show-message (&rest body)
205 "Evaluate body with current buffer set to the text of current message"
207 (let ((id (notmuch-show-get-message-id)))
208 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
209 (with-current-buffer buf
210 (let ((coding-system-for-read 'no-conversion))
211 (call-process notmuch-command nil t nil "show" "--format=raw" id)
213 (kill-buffer buf))))))
215 (defun notmuch-show-turn-on-visual-line-mode ()
216 "Enable Visual Line mode."
217 (visual-line-mode t))
219 ;; DEPRECATED in Notmuch 0.16 since we now have convenient part
220 ;; commands. We'll keep the command around for a version or two in
221 ;; case people want to bind it themselves.
222 (defun notmuch-show-view-all-mime-parts ()
223 "Use external viewers to view all attachments from the current message."
225 (with-current-notmuch-show-message
226 ;; We override the mm-inline-media-tests to indicate which message
227 ;; parts are already sufficiently handled by the original
228 ;; presentation of the message in notmuch-show mode. These parts
229 ;; will be inserted directly into the temporary buffer of
230 ;; with-current-notmuch-show-message and silently discarded.
232 ;; Any MIME part not explicitly mentioned here will be handled by an
233 ;; external viewer as configured in the various mailcap files.
234 (let ((mm-inline-media-tests '(
235 ("text/.*" ignore identity)
236 ("application/pgp-signature" ignore identity)
237 ("multipart/alternative" ignore identity)
238 ("multipart/mixed" ignore identity)
239 ("multipart/related" ignore identity)
241 (mm-display-parts (mm-dissect-buffer)))))
243 (defun notmuch-show-save-attachments ()
244 "Save all attachments from the current message."
246 (with-current-notmuch-show-message
247 (let ((mm-handle (mm-dissect-buffer)))
248 (notmuch-save-attachments
249 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
252 (defun notmuch-show-with-message-as-text (fn)
253 "Apply FN to a text representation of the current message.
255 FN is called with one argument, the message properties. It should
256 operation on the contents of the current buffer."
258 ;; Remake the header to ensure that all information is available.
259 (let* ((to (notmuch-show-get-to))
260 (cc (notmuch-show-get-cc))
261 (from (notmuch-show-get-from))
262 (subject (notmuch-show-get-subject))
263 (date (notmuch-show-get-date))
264 (tags (notmuch-show-get-tags))
265 (depth (notmuch-show-get-depth))
268 "Subject: " subject "\n"
270 (if (not (string= cc ""))
271 (concat "Cc: " cc "\n")
277 (mapconcat #'identity tags ", ") "\n")
279 (all (buffer-substring (notmuch-show-message-top)
280 (notmuch-show-message-bottom)))
282 (props (notmuch-show-get-message-properties))
283 (indenting notmuch-show-indent-content))
287 (indent-rigidly (point-min) (point-max) (- depth)))
288 ;; Remove the original header.
289 (goto-char (point-min))
290 (re-search-forward "^$" (point-max) nil)
291 (delete-region (point-min) (point))
293 (funcall fn props))))
295 (defun notmuch-show-print-message ()
296 "Print the current message."
298 (notmuch-show-with-message-as-text 'notmuch-print-message))
300 (defun notmuch-show-fontify-header ()
302 ((looking-at "[Tt]o:")
304 ((looking-at "[Bb]?[Cc][Cc]:")
306 ((looking-at "[Ss]ubject:")
307 'message-header-subject)
308 ((looking-at "[Ff]rom:")
309 'message-header-from)
311 'message-header-other))))
313 (overlay-put (make-overlay (point) (re-search-forward ":"))
314 'face 'message-header-name)
315 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
318 (defun notmuch-show-colour-headers ()
319 "Apply some colouring to the current headers."
320 (goto-char (point-min))
321 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
322 (notmuch-show-fontify-header)
325 (defun notmuch-show-spaces-n (n)
326 "Return a string comprised of `n' spaces."
329 (defun notmuch-show-update-tags (tags)
330 "Update the displayed tags of the current message."
332 (goto-char (notmuch-show-message-top))
333 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
334 (let ((inhibit-read-only t))
335 (replace-match (concat "("
336 (notmuch-tag-format-tags tags)
339 (defun notmuch-clean-address (address)
340 "Try to clean a single email ADDRESS for display. Return a cons
341 cell of (AUTHOR_EMAIL AUTHOR_NAME). Return (ADDRESS nil) if
344 (let (p-name p-address)
345 ;; It would be convenient to use `mail-header-parse-address',
346 ;; but that expects un-decoded mailbox parts, whereas our
347 ;; mailbox parts are already decoded (and hence may contain
348 ;; UTF-8). Given that notmuch should handle most of the awkward
349 ;; cases, some simple string deconstruction should be sufficient
352 ;; "User <user@dom.ain>" style.
353 ((string-match "\\(.*\\) <\\(.*\\)>" address)
354 (setq p-name (match-string 1 address)
355 p-address (match-string 2 address)))
357 ;; "<user@dom.ain>" style.
358 ((string-match "<\\(.*\\)>" address)
359 (setq p-address (match-string 1 address)))
363 (setq p-address address)))
366 ;; Remove elements of the mailbox part that are not relevant for
367 ;; display, even if they are required during transport:
370 (setq p-name (replace-regexp-in-string "\\\\" "" p-name))
372 ;; Outer single and double quotes, which might be nested.
375 do (setq start-of-loop p-name)
377 when (string-match "^\"\\(.*\\)\"$" p-name)
378 do (setq p-name (match-string 1 p-name))
380 when (string-match "^'\\(.*\\)'$" p-name)
381 do (setq p-name (match-string 1 p-name))
383 until (string= start-of-loop p-name)))
385 ;; If the address is 'foo@bar.com <foo@bar.com>' then show just
387 (when (string= p-name p-address)
390 (cons p-address p-name))
391 (error (cons address nil))))
393 (defun notmuch-show-clean-address (address)
394 "Try to clean a single email ADDRESS for display. Return
395 unchanged ADDRESS if parsing fails."
396 (let* ((clean-address (notmuch-clean-address address))
397 (p-address (car clean-address))
398 (p-name (cdr clean-address)))
399 ;; If no name, return just the address.
402 ;; Otherwise format the name and address together.
403 (concat p-name " <" p-address ">"))))
405 (defun notmuch-show-insert-headerline (headers date tags depth)
406 "Insert a notmuch style headerline based on HEADERS for a
407 message at DEPTH in the current thread."
408 (let ((start (point)))
409 (insert (notmuch-show-spaces-n (* notmuch-show-indent-messages-width depth))
410 (notmuch-show-clean-address (plist-get headers :From))
414 (notmuch-tag-format-tags tags)
416 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
418 (defun notmuch-show-insert-header (header header-value)
419 "Insert a single header."
420 (insert header ": " header-value "\n"))
422 (defun notmuch-show-insert-headers (headers)
423 "Insert the headers of the current message."
424 (let ((start (point)))
425 (mapc (lambda (header)
426 (let* ((header-symbol (intern (concat ":" header)))
427 (header-value (plist-get headers header-symbol)))
428 (if (and header-value
429 (not (string-equal "" header-value)))
430 (notmuch-show-insert-header header header-value))))
431 notmuch-message-headers)
434 (narrow-to-region start (point-max))
435 (run-hooks 'notmuch-show-markup-headers-hook)))))
437 (define-button-type 'notmuch-show-part-button-type
438 'action 'notmuch-show-part-button-default
441 :supertype 'notmuch-button-type)
443 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name comment)
445 (base-label (concat (when name (concat name ": "))
447 (unless (string-equal declared-type content-type)
448 (concat " (as " content-type ")"))
453 (concat "[ " base-label " ]")
454 :base-label base-label
455 :type 'notmuch-show-part-button-type
456 :notmuch-part-hidden nil))
461 (defun notmuch-show-toggle-part-invisibility (&optional button)
463 (let* ((button (or button (button-at (point))))
464 (overlay (button-get button 'overlay))
465 (lazy-part (button-get button :notmuch-lazy-part)))
466 ;; We have a part to toggle if there is an overlay or if there is a lazy part.
467 ;; If neither is present we cannot toggle the part so we just return nil.
468 (when (or overlay lazy-part)
469 (let* ((show (button-get button :notmuch-part-hidden))
470 (new-start (button-start button))
471 (button-label (button-get button :base-label))
473 (properties (text-properties-at (point)))
474 (inhibit-read-only t))
475 ;; Toggle the button itself.
476 (button-put button :notmuch-part-hidden (not show))
477 (goto-char new-start)
478 (insert "[ " button-label (if show " ]" " (hidden) ]"))
479 (set-text-properties new-start (point) properties)
480 (let ((old-end (button-end button)))
481 (move-overlay button new-start (point))
482 (delete-region (point) old-end))
483 (goto-char (min old-point (1- (button-end button))))
484 ;; Return nil if there is a lazy-part, it is empty, and we are
485 ;; trying to show it. In all other cases return t.
488 (button-put button :notmuch-lazy-part nil)
489 (notmuch-show-lazy-part lazy-part button))
490 ;; else there must be an overlay.
491 (overlay-put overlay 'invisible (not show))
494 ;; MIME part renderers
496 (defun notmuch-show-multipart/*-to-list (part)
497 (mapcar (lambda (inner-part) (plist-get inner-part :content-type))
498 (plist-get part :content)))
500 (defun notmuch-show-insert-part-multipart/alternative (msg part content-type nth depth button)
501 (let ((chosen-type (car (notmuch-multipart/alternative-choose (notmuch-show-multipart/*-to-list part))))
502 (inner-parts (plist-get part :content))
504 ;; This inserts all parts of the chosen type rather than just one,
505 ;; but it's not clear that this is the wrong thing to do - which
506 ;; should be chosen if there are more than one that match?
507 (mapc (lambda (inner-part)
508 (let* ((inner-type (plist-get inner-part :content-type))
509 (hide (not (or notmuch-show-all-multipart/alternative-parts
510 (string= chosen-type inner-type)))))
511 (notmuch-show-insert-bodypart msg inner-part depth hide)))
514 (when notmuch-show-indent-multipart
515 (indent-rigidly start (point) 1)))
518 (defun notmuch-show-setup-w3m ()
519 "Instruct w3m how to retrieve content from a \"related\" part of a message."
521 (if (boundp 'w3m-cid-retrieve-function-alist)
522 (unless (assq 'notmuch-show-mode w3m-cid-retrieve-function-alist)
523 (push (cons 'notmuch-show-mode 'notmuch-show-w3m-cid-retrieve)
524 w3m-cid-retrieve-function-alist)))
525 (setq mm-inline-text-html-with-images t))
527 (defvar w3m-current-buffer) ;; From `w3m.el'.
528 (defvar notmuch-show-w3m-cid-store nil)
529 (make-variable-buffer-local 'notmuch-show-w3m-cid-store)
531 (defun notmuch-show-w3m-cid-store-internal (content-id
536 (push (list content-id
541 notmuch-show-w3m-cid-store))
543 (defun notmuch-show-w3m-cid-store (msg part)
544 (let ((content-id (plist-get part :content-id)))
546 (notmuch-show-w3m-cid-store-internal (concat "cid:" content-id)
549 (plist-get part :content-type)
552 (defun notmuch-show-w3m-cid-retrieve (url &rest args)
553 (let ((matching-part (with-current-buffer w3m-current-buffer
554 (assoc url notmuch-show-w3m-cid-store))))
556 (let ((message-id (nth 1 matching-part))
557 (part-number (nth 2 matching-part))
558 (content-type (nth 3 matching-part))
559 (content (nth 4 matching-part)))
560 ;; If we don't already have the content, get it and cache
561 ;; it, as some messages reference the same cid: part many
562 ;; times (hundreds!), which results in many calls to
565 (setq content (notmuch-get-bodypart-internal (notmuch-id-to-query message-id)
566 part-number notmuch-show-process-crypto))
567 (with-current-buffer w3m-current-buffer
568 (notmuch-show-w3m-cid-store-internal url
577 (defun notmuch-show-insert-part-multipart/related (msg part content-type nth depth button)
578 (let ((inner-parts (plist-get part :content))
581 ;; We assume that the first part is text/html and the remainder
582 ;; things that it references.
584 ;; Stash the non-primary parts.
586 (notmuch-show-w3m-cid-store msg part))
589 ;; Render the primary part.
590 (notmuch-show-insert-bodypart msg (car inner-parts) depth)
592 (when notmuch-show-indent-multipart
593 (indent-rigidly start (point) 1)))
596 (defun notmuch-show-insert-part-multipart/signed (msg part content-type nth depth button)
597 (button-put button 'face 'notmuch-crypto-part-header)
598 ;; add signature status button if sigstatus provided
599 (if (plist-member part :sigstatus)
600 (let* ((from (notmuch-show-get-header :From msg))
601 (sigstatus (car (plist-get part :sigstatus))))
602 (notmuch-crypto-insert-sigstatus-button sigstatus from))
603 ;; if we're not adding sigstatus, tell the user how they can get it
604 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts."))
606 (let ((inner-parts (plist-get part :content))
608 ;; Show all of the parts.
609 (mapc (lambda (inner-part)
610 (notmuch-show-insert-bodypart msg inner-part depth))
613 (when notmuch-show-indent-multipart
614 (indent-rigidly start (point) 1)))
617 (defun notmuch-show-insert-part-multipart/encrypted (msg part content-type nth depth button)
618 (button-put button 'face 'notmuch-crypto-part-header)
619 ;; add encryption status button if encstatus specified
620 (if (plist-member part :encstatus)
621 (let ((encstatus (car (plist-get part :encstatus))))
622 (notmuch-crypto-insert-encstatus-button encstatus)
623 ;; add signature status button if sigstatus specified
624 (if (plist-member part :sigstatus)
625 (let* ((from (notmuch-show-get-header :From msg))
626 (sigstatus (car (plist-get part :sigstatus))))
627 (notmuch-crypto-insert-sigstatus-button sigstatus from))))
628 ;; if we're not adding encstatus, tell the user how they can get it
629 (button-put button 'help-echo "Set notmuch-crypto-process-mime to process cryptographic MIME parts."))
631 (let ((inner-parts (plist-get part :content))
633 ;; Show all of the parts.
634 (mapc (lambda (inner-part)
635 (notmuch-show-insert-bodypart msg inner-part depth))
638 (when notmuch-show-indent-multipart
639 (indent-rigidly start (point) 1)))
642 (defun notmuch-show-insert-part-multipart/* (msg part content-type nth depth button)
643 (let ((inner-parts (plist-get part :content))
645 ;; Show all of the parts.
646 (mapc (lambda (inner-part)
647 (notmuch-show-insert-bodypart msg inner-part depth))
650 (when notmuch-show-indent-multipart
651 (indent-rigidly start (point) 1)))
654 (defun notmuch-show-insert-part-message/rfc822 (msg part content-type nth depth button)
655 (let* ((message (car (plist-get part :content)))
656 (body (car (plist-get message :body)))
659 ;; Override `notmuch-message-headers' to force `From' to be
661 (let ((notmuch-message-headers '("From" "Subject" "To" "Cc" "Date")))
662 (notmuch-show-insert-headers (plist-get message :headers)))
664 ;; Blank line after headers to be compatible with the normal
669 (notmuch-show-insert-bodypart msg body depth)
671 (when notmuch-show-indent-multipart
672 (indent-rigidly start (point) 1)))
675 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth button)
676 ;; For backward compatibility we want to apply the text/plain hook
677 ;; to the whole of the part including the part button if there is
679 (let ((start (if button
680 (button-start button)
682 (insert (notmuch-get-bodypart-content msg part nth notmuch-show-process-crypto))
685 (narrow-to-region start (point-max))
686 (run-hook-with-args 'notmuch-show-insert-text/plain-hook msg depth))))
689 (defun notmuch-show-insert-part-text/calendar (msg part content-type nth depth button)
690 (insert (with-temp-buffer
691 (insert (notmuch-get-bodypart-content msg part nth notmuch-show-process-crypto))
692 ;; notmuch-get-bodypart-content provides "raw", non-converted
693 ;; data. Replace CRLF with LF before icalendar can use it.
694 (goto-char (point-min))
695 (while (re-search-forward "\r\n" nil t)
696 (replace-match "\n" nil nil))
697 (let ((file (make-temp-file "notmuch-ical"))
701 (unless (icalendar-import-buffer file t)
702 (error "Icalendar import error. See *icalendar-errors* for more information"))
703 (set-buffer (get-file-buffer file))
704 (setq result (buffer-substring (point-min) (point-max)))
705 (set-buffer-modified-p nil)
706 (kill-buffer (current-buffer)))
711 ;; For backwards compatibility.
712 (defun notmuch-show-insert-part-text/x-vcalendar (msg part content-type nth depth button)
713 (notmuch-show-insert-part-text/calendar msg part content-type nth depth button))
715 (defun notmuch-show-get-mime-type-of-application/octet-stream (part)
716 ;; If we can deduce a MIME type from the filename of the attachment,
718 (if (plist-get part :filename)
719 (let ((extension (file-name-extension (plist-get part :filename)))
723 (mailcap-parse-mimetypes)
724 (setq mime-type (mailcap-extension-to-mime extension))
726 (not (string-equal mime-type "application/octet-stream")))
731 (defun notmuch-show-insert-part-text/html (msg part content-type nth depth button)
732 ;; text/html handler to work around bugs in renderers and our
733 ;; invisibile parts code. In particular w3m sets up a keymap which
734 ;; "leaks" outside the invisible region and causes strange effects
735 ;; in notmuch. We set mm-inline-text-html-with-w3m-keymap to nil to
736 ;; tell w3m not to set a keymap (so the normal notmuch-show-mode-map
738 (let ((mm-inline-text-html-with-w3m-keymap nil))
739 (notmuch-show-insert-part-*/* msg part content-type nth depth button)))
741 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth button)
742 ;; This handler _must_ succeed - it is the handler of last resort.
743 (notmuch-mm-display-part-inline msg part nth content-type notmuch-show-process-crypto)
746 ;; Functions for determining how to handle MIME parts.
748 (defun notmuch-show-handlers-for (content-type)
749 "Return a list of content handlers for a part of type CONTENT-TYPE."
754 ;; Reverse order of prefrence.
755 (list (intern (concat "notmuch-show-insert-part-*/*"))
757 "notmuch-show-insert-part-"
758 (car (notmuch-split-content-type content-type))
760 (intern (concat "notmuch-show-insert-part-" content-type))))
765 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth button)
766 (let ((handlers (notmuch-show-handlers-for content-type)))
767 ;; Run the content handlers until one of them returns a non-nil
770 (not (condition-case err
771 (funcall (car handlers) msg part content-type nth depth button)
773 (insert "!!! Bodypart insert error: ")
774 (insert (error-message-string err))
775 (insert " !!!\n") nil)))))
776 (setq handlers (cdr handlers))))
779 (defun notmuch-show-create-part-overlays (button beg end)
780 "Add an overlay to the part between BEG and END"
782 ;; If there is no button (i.e., the part is text/plain and the first
783 ;; part) or if the part has no content then we don't make the part
785 (when (and button (/= beg end))
786 (button-put button 'overlay (make-overlay beg end))
787 ;; Return true if we created an overlay.
790 (defun notmuch-show-record-part-information (part beg end)
791 "Store PART as a text property from BEG to END"
793 ;; Record part information. Since we already inserted subparts,
794 ;; don't override existing :notmuch-part properties.
795 (notmuch-map-text-property beg end :notmuch-part
796 (lambda (v) (or v part)))
797 ;; Make :notmuch-part front sticky and rear non-sticky so it stays
798 ;; applied to the beginning of each line when we indent the
799 ;; message. Since we're operating on arbitrary renderer output,
800 ;; watch out for sticky specs of t, which means all properties are
801 ;; front-sticky/rear-nonsticky.
802 (notmuch-map-text-property beg end 'front-sticky
803 (lambda (v) (if (listp v)
804 (pushnew :notmuch-part v)
806 (notmuch-map-text-property beg end 'rear-nonsticky
807 (lambda (v) (if (listp v)
808 (pushnew :notmuch-part v)
811 (defun notmuch-show-lazy-part (part-args button)
812 ;; Insert the lazy part after the button for the part. We would just
813 ;; move to the start of the new line following the button and insert
814 ;; the part but that point might have text properties (eg colours
815 ;; from a message header etc) so instead we start from the last
816 ;; character of the button by adding a newline and finish by
817 ;; removing the extra newline from the end of the part.
819 (goto-char (button-end button))
821 (let* ((inhibit-read-only t)
822 ;; We need to use markers for the start and end of the part
823 ;; because the part insertion functions do not guarantee
824 ;; to leave point at the end of the part.
825 (part-beg (copy-marker (point) nil))
826 (part-end (copy-marker (point) t))
827 ;; We have to save the depth as we can't find the depth
829 (depth (notmuch-show-get-depth)))
831 (narrow-to-region part-beg part-end)
832 (delete-region part-beg part-end)
833 (apply #'notmuch-show-insert-bodypart-internal part-args)
834 (indent-rigidly part-beg part-end depth))
837 (notmuch-show-record-part-information (second part-args)
838 (button-start button)
840 ;; Create the overlay. If the lazy-part turned out to be empty/not
841 ;; showable this returns nil.
842 (notmuch-show-create-part-overlays button part-beg part-end))))
844 (defun notmuch-show-insert-bodypart (msg part depth &optional hide)
845 "Insert the body part PART at depth DEPTH in the current thread.
847 If HIDE is non-nil then initially hide this part."
849 (let* ((content-type (downcase (plist-get part :content-type)))
850 (mime-type (or (and (string= content-type "application/octet-stream")
851 (notmuch-show-get-mime-type-of-application/octet-stream part))
852 (and (string= content-type "inline patch")
855 (nth (plist-get part :id))
857 ;; We omit the part button for the first (or only) part if this is text/plain.
858 (button (unless (and (string= mime-type "text/plain") (<= nth 1))
859 (notmuch-show-insert-part-header nth mime-type content-type (plist-get part :filename))))
860 (content-beg (point)))
862 ;; Store the computed mime-type for later use (e.g. by attachment handlers).
863 (plist-put part :computed-type mime-type)
866 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth button)
867 (button-put button :notmuch-lazy-part
868 (list msg part mime-type nth depth button)))
870 ;; Some of the body part handlers leave point somewhere up in the
871 ;; part, so we make sure that we're down at the end.
872 (goto-char (point-max))
873 ;; Ensure that the part ends with a carriage return.
876 ;; We do not create the overlay for hidden (lazy) parts until
877 ;; they are inserted.
879 (notmuch-show-create-part-overlays button content-beg (point))
881 (notmuch-show-toggle-part-invisibility button)))
882 (notmuch-show-record-part-information part beg (point))))
884 (defun notmuch-show-insert-body (msg body depth)
885 "Insert the body BODY at depth DEPTH in the current thread."
886 (mapc (lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
888 (defun notmuch-show-make-symbol (type)
889 (make-symbol (concat "notmuch-show-" type)))
891 (defun notmuch-show-strip-re (string)
892 (replace-regexp-in-string "^\\([Rr]e: *\\)+" "" string))
894 (defvar notmuch-show-previous-subject "")
895 (make-variable-buffer-local 'notmuch-show-previous-subject)
897 (defun notmuch-show-insert-msg (msg depth)
898 "Insert the message MSG at depth DEPTH in the current thread."
899 (let* ((headers (plist-get msg :headers))
900 ;; Indentation causes the buffer offset of the start/end
901 ;; points to move, so we must use markers.
902 message-start message-end
903 content-start content-end
904 headers-start headers-end
905 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
907 (setq message-start (point-marker))
909 (notmuch-show-insert-headerline headers
910 (or (if notmuch-show-relative-dates
911 (plist-get msg :date_relative)
913 (plist-get headers :Date))
914 (plist-get msg :tags) depth)
916 (setq content-start (point-marker))
918 ;; Set `headers-start' to point after the 'Subject:' header to be
919 ;; compatible with the existing implementation. This just sets it
920 ;; to after the first header.
921 (notmuch-show-insert-headers headers)
923 (goto-char content-start)
924 ;; If the subject of this message is the same as that of the
925 ;; previous message, don't display it when this message is
927 (when (not (string= notmuch-show-previous-subject
930 (setq headers-start (point-marker)))
931 (setq headers-end (point-marker))
933 (setq notmuch-show-previous-subject bare-subject)
935 ;; A blank line between the headers and the body.
937 (notmuch-show-insert-body msg (plist-get msg :body)
938 (if notmuch-show-indent-content depth 0))
939 ;; Ensure that the body ends with a newline.
942 (setq content-end (point-marker))
944 ;; Indent according to the depth in the thread.
945 (if notmuch-show-indent-content
946 (indent-rigidly content-start content-end (* notmuch-show-indent-messages-width depth)))
948 (setq message-end (point-max-marker))
950 ;; Save the extents of this message over the whole text of the
952 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
954 ;; Create overlays used to control visibility
955 (plist-put msg :headers-overlay (make-overlay headers-start headers-end))
956 (plist-put msg :message-overlay (make-overlay headers-start content-end))
958 (plist-put msg :depth depth)
960 ;; Save the properties for this message. Currently this saves the
961 ;; entire message (augmented it with other stuff), which seems
962 ;; like overkill. We might save a reduced subset (for example, not
964 (notmuch-show-set-message-properties msg)
966 ;; Set header visibility.
967 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
969 ;; Message visibility depends on whether it matched the search
971 (notmuch-show-message-visible msg (and (plist-get msg :match)
972 (not (plist-get msg :excluded))))))
974 (defun notmuch-show-toggle-process-crypto ()
975 "Toggle the processing of cryptographic MIME parts."
977 (setq notmuch-show-process-crypto (not notmuch-show-process-crypto))
978 (message (if notmuch-show-process-crypto
979 "Processing cryptographic MIME parts."
980 "Not processing cryptographic MIME parts."))
981 (notmuch-show-refresh-view))
983 (defun notmuch-show-toggle-elide-non-matching ()
984 "Toggle the display of non-matching messages."
986 (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages))
987 (message (if notmuch-show-elide-non-matching-messages
988 "Showing matching messages only."
989 "Showing all messages."))
990 (notmuch-show-refresh-view))
992 (defun notmuch-show-toggle-thread-indentation ()
993 "Toggle the indentation of threads."
995 (setq notmuch-show-indent-content (not notmuch-show-indent-content))
996 (message (if notmuch-show-indent-content
997 "Content is indented."
998 "Content is not indented."))
999 (notmuch-show-refresh-view))
1001 (defun notmuch-show-insert-tree (tree depth)
1002 "Insert the message tree TREE at depth DEPTH in the current thread."
1003 (let ((msg (car tree))
1004 (replies (cadr tree)))
1005 ;; We test whether there is a message or just some replies.
1007 (notmuch-show-insert-msg msg depth))
1008 (notmuch-show-insert-thread replies (1+ depth))))
1010 (defun notmuch-show-insert-thread (thread depth)
1011 "Insert the thread THREAD at depth DEPTH in the current forest."
1012 (mapc (lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
1014 (defun notmuch-show-insert-forest (forest)
1015 "Insert the forest of threads FOREST."
1016 (mapc (lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
1018 (defvar notmuch-id-regexp
1020 ;; Match the id: prefix only if it begins a word (to disallow, for
1021 ;; example, matching cid:).
1023 ;; If the term starts with a ", then parse Xapian's quoted boolean
1024 ;; term syntax, which allows for anything as long as embedded
1025 ;; double quotes escaped by doubling them. We also disallow
1026 ;; newlines (which Xapian allows) to prevent runaway terms.
1027 "\"\\([^\"\n]\\|\"\"\\)*\""
1028 ;; Otherwise, parse Xapian's unquoted syntax, which goes up to the
1029 ;; next space or ). We disallow [.,;] as the last character
1030 ;; because these are probably part of the surrounding text, and not
1031 ;; part of the id. This doesn't match single character ids; meh.
1032 "\\|[^\"[:space:])][^[:space:])]*[^])[:space:].,:;?!]"
1034 "The regexp used to match id: links in messages.")
1036 (defvar notmuch-mid-regexp
1037 ;; goto-address-url-regexp matched cid: links, which have the same
1038 ;; grammar as the message ID part of a mid: link. Construct the
1039 ;; regexp using the same technique as goto-address-url-regexp.
1040 (concat "\\<mid:\\(" thing-at-point-url-path-regexp "\\)")
1041 "The regexp used to match mid: links in messages.
1045 (defun notmuch-show-buttonise-links (start end)
1046 "Buttonise URLs and mail addresses between START and END.
1048 This also turns id:\"<message id>\"-parts and mid: links into
1049 buttons for a corresponding notmuch search."
1050 (goto-address-fontify-region start end)
1054 (while (re-search-forward notmuch-id-regexp end t)
1055 (push (list (match-beginning 0) (match-end 0)
1056 (match-string-no-properties 0)) links))
1058 (while (re-search-forward notmuch-mid-regexp end t)
1059 (let* ((mid-cid (match-string-no-properties 1))
1060 (mid (save-match-data
1061 (string-match "^[^/]*" mid-cid)
1062 (url-unhex-string (match-string 0 mid-cid)))))
1063 (push (list (match-beginning 0) (match-end 0)
1064 (notmuch-id-to-query mid)) links)))
1065 (dolist (link links)
1066 ;; Remove the overlay created by goto-address-mode
1067 (remove-overlays (first link) (second link) 'goto-address t)
1068 (make-text-button (first link) (second link)
1069 :type 'notmuch-button-type
1070 'action `(lambda (arg)
1071 (notmuch-show ,(third link)))
1073 'help-echo "Mouse-1, RET: search for this message"
1074 'face goto-address-mail-face)))))
1077 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
1078 "Run \"notmuch show\" with the given thread ID and display results.
1080 The optional PARENT-BUFFER is the notmuch-search buffer from
1081 which this notmuch-show command was executed, (so that the
1082 next thread from that buffer can be show when done with this
1085 The optional QUERY-CONTEXT is a notmuch search term. Only
1086 messages from the thread matching this search term are shown if
1089 The optional BUFFER-NAME provides the name of the buffer in
1090 which the message thread is shown. If it is nil (which occurs
1091 when the command is called interactively) the argument to the
1093 (interactive "sNotmuch show: ")
1094 (let ((buffer-name (generate-new-buffer-name
1096 (concat "*notmuch-" thread-id "*")))))
1097 (switch-to-buffer (get-buffer-create buffer-name))
1098 ;; Set the default value for `notmuch-show-process-crypto' in this
1100 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1101 ;; Set the default value for
1102 ;; `notmuch-show-elide-non-matching-messages' in this buffer. If
1103 ;; there is a prefix argument, invert the default.
1104 (setq notmuch-show-elide-non-matching-messages notmuch-show-only-matching-messages)
1105 (if current-prefix-arg
1106 (setq notmuch-show-elide-non-matching-messages (not notmuch-show-elide-non-matching-messages)))
1108 (setq notmuch-show-thread-id thread-id
1109 notmuch-show-parent-buffer parent-buffer
1110 notmuch-show-query-context query-context)
1111 (notmuch-show-build-buffer)
1112 (notmuch-show-goto-first-wanted-message)
1115 (defun notmuch-show-build-buffer ()
1116 (let ((inhibit-read-only t))
1119 ;; Don't track undo information for this buffer
1120 (set 'buffer-undo-list t)
1123 (goto-char (point-min))
1125 (let* ((basic-args (list notmuch-show-thread-id))
1126 (args (if notmuch-show-query-context
1127 (append (list "\'") basic-args
1128 (list "and (" notmuch-show-query-context ")\'"))
1129 (append (list "\'") basic-args (list "\'"))))
1130 (cli-args (cons "--exclude=false"
1131 (when notmuch-show-elide-non-matching-messages
1132 (list "--entire-thread=false")))))
1134 (notmuch-show-insert-forest (notmuch-query-get-threads (append cli-args args)))
1135 ;; If the query context reduced the results to nothing, run
1137 (when (and (eq (buffer-size) 0)
1138 notmuch-show-query-context)
1139 (notmuch-show-insert-forest
1140 (notmuch-query-get-threads (append cli-args basic-args)))))
1142 (jit-lock-register #'notmuch-show-buttonise-links)
1144 ;; Set the header line to the subject of the first message.
1145 (setq header-line-format (notmuch-show-strip-re (notmuch-show-get-subject)))
1147 (run-hooks 'notmuch-show-hook))))
1149 (defun notmuch-show-capture-state ()
1150 "Capture the state of the current buffer.
1153 - the list of open messages,
1154 - the current message."
1155 (list (notmuch-show-get-message-id) (notmuch-show-get-message-ids-for-open-messages)))
1157 (defun notmuch-show-apply-state (state)
1158 "Apply STATE to the current buffer.
1161 - opening the messages previously opened,
1162 - closing all other messages,
1163 - moving to the correct current message."
1164 (let ((current (car state))
1165 (open (cadr state)))
1167 ;; Open those that were open.
1168 (goto-char (point-min))
1169 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1170 (member (notmuch-show-get-message-id) open))
1171 until (not (notmuch-show-goto-message-next)))
1173 ;; Go to the previously open message.
1174 (goto-char (point-min))
1175 (unless (loop if (string= current (notmuch-show-get-message-id))
1177 until (not (notmuch-show-goto-message-next)))
1178 (goto-char (point-min))
1179 (message "Previously current message not found."))
1180 (notmuch-show-message-adjust)))
1182 (defun notmuch-show-refresh-view (&optional reset-state)
1183 "Refresh the current view.
1185 Refreshes the current view, observing changes in display
1186 preferences. If invoked with a prefix argument (or RESET-STATE is
1187 non-nil) then the state of the buffer (open/closed messages) is
1188 reset based on the original query."
1190 (let ((inhibit-read-only t)
1191 (state (unless reset-state
1192 (notmuch-show-capture-state))))
1193 ;; erase-buffer does not seem to remove overlays, which can lead
1194 ;; to weird effects such as remaining images, so remove them
1198 (notmuch-show-build-buffer)
1200 (notmuch-show-apply-state state)
1201 ;; We're resetting state, so navigate to the first open message
1202 ;; and mark it read, just like opening a new show buffer.
1203 (notmuch-show-goto-first-wanted-message))))
1205 (defvar notmuch-show-stash-map
1206 (let ((map (make-sparse-keymap)))
1207 (define-key map "c" 'notmuch-show-stash-cc)
1208 (define-key map "d" 'notmuch-show-stash-date)
1209 (define-key map "F" 'notmuch-show-stash-filename)
1210 (define-key map "f" 'notmuch-show-stash-from)
1211 (define-key map "i" 'notmuch-show-stash-message-id)
1212 (define-key map "I" 'notmuch-show-stash-message-id-stripped)
1213 (define-key map "s" 'notmuch-show-stash-subject)
1214 (define-key map "T" 'notmuch-show-stash-tags)
1215 (define-key map "t" 'notmuch-show-stash-to)
1216 (define-key map "l" 'notmuch-show-stash-mlarchive-link)
1217 (define-key map "L" 'notmuch-show-stash-mlarchive-link-and-go)
1219 "Submap for stash commands")
1220 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
1222 (defvar notmuch-show-part-map
1223 (let ((map (make-sparse-keymap)))
1224 (define-key map "s" 'notmuch-show-save-part)
1225 (define-key map "v" 'notmuch-show-view-part)
1226 (define-key map "o" 'notmuch-show-interactively-view-part)
1227 (define-key map "|" 'notmuch-show-pipe-part)
1229 "Submap for part commands")
1230 (fset 'notmuch-show-part-map notmuch-show-part-map)
1232 (defvar notmuch-show-mode-map
1233 (let ((map (make-sparse-keymap)))
1234 (define-key map "?" 'notmuch-help)
1235 (define-key map "q" 'notmuch-kill-this-buffer)
1236 (define-key map (kbd "<C-tab>") 'widget-backward)
1237 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
1238 (define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
1239 (define-key map (kbd "TAB") 'notmuch-show-next-button)
1240 (define-key map "s" 'notmuch-search)
1241 (define-key map "m" 'notmuch-mua-new-mail)
1242 (define-key map "f" 'notmuch-show-forward-message)
1243 (define-key map "r" 'notmuch-show-reply-sender)
1244 (define-key map "R" 'notmuch-show-reply)
1245 (define-key map "|" 'notmuch-show-pipe-message)
1246 (define-key map "w" 'notmuch-show-save-attachments)
1247 (define-key map "V" 'notmuch-show-view-raw-message)
1248 (define-key map "c" 'notmuch-show-stash-map)
1249 (define-key map "=" 'notmuch-show-refresh-view)
1250 (define-key map "h" 'notmuch-show-toggle-visibility-headers)
1251 (define-key map "*" 'notmuch-show-tag-all)
1252 (define-key map "-" 'notmuch-show-remove-tag)
1253 (define-key map "+" 'notmuch-show-add-tag)
1254 (define-key map "X" 'notmuch-show-archive-thread-then-exit)
1255 (define-key map "x" 'notmuch-show-archive-message-then-next-or-exit)
1256 (define-key map "A" 'notmuch-show-archive-thread-then-next)
1257 (define-key map "a" 'notmuch-show-archive-message-then-next-or-next-thread)
1258 (define-key map "N" 'notmuch-show-next-message)
1259 (define-key map "P" 'notmuch-show-previous-message)
1260 (define-key map "n" 'notmuch-show-next-open-message)
1261 (define-key map "p" 'notmuch-show-previous-open-message)
1262 (define-key map (kbd "M-n") 'notmuch-show-next-thread-show)
1263 (define-key map (kbd "M-p") 'notmuch-show-previous-thread-show)
1264 (define-key map (kbd "DEL") 'notmuch-show-rewind)
1265 (define-key map " " 'notmuch-show-advance-and-archive)
1266 (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
1267 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
1268 (define-key map "#" 'notmuch-show-print-message)
1269 (define-key map "!" 'notmuch-show-toggle-elide-non-matching)
1270 (define-key map "$" 'notmuch-show-toggle-process-crypto)
1271 (define-key map "<" 'notmuch-show-toggle-thread-indentation)
1272 (define-key map "t" 'toggle-truncate-lines)
1273 (define-key map "." 'notmuch-show-part-map)
1275 "Keymap for \"notmuch show\" buffers.")
1276 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
1278 (defun notmuch-show-mode ()
1279 "Major mode for viewing a thread with notmuch.
1281 This buffer contains the results of the \"notmuch show\" command
1282 for displaying a single thread of email from your email archives.
1284 By default, various components of email messages, (citations,
1285 signatures, already-read messages), are hidden. You can make
1286 these parts visible by clicking with the mouse button or by
1287 pressing RET after positioning the cursor on a hidden part, (for
1288 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
1290 Reading the thread sequentially is well-supported by pressing
1291 \\[notmuch-show-advance-and-archive]. This will scroll the current message (if necessary), advance
1292 to the next message, or advance to the next thread (if already on
1293 the last message of a thread).
1295 Other commands are available to read or manipulate the thread
1296 more selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages
1297 without removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread
1298 without scrolling through with \\[notmuch-show-advance-and-archive]).
1300 You can add or remove arbitrary tags from the current message with
1301 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
1303 All currently available key bindings:
1305 \\{notmuch-show-mode-map}"
1307 (kill-all-local-variables)
1308 (use-local-map notmuch-show-mode-map)
1309 (setq major-mode 'notmuch-show-mode
1310 mode-name "notmuch-show")
1311 (setq buffer-read-only t
1314 (defun notmuch-show-move-to-message-top ()
1315 (goto-char (notmuch-show-message-top)))
1317 (defun notmuch-show-move-to-message-bottom ()
1318 (goto-char (notmuch-show-message-bottom)))
1320 (defun notmuch-show-message-adjust ()
1323 ;; Movement related functions.
1325 ;; There's some strangeness here where a text property applied to a
1326 ;; region a->b is not found when point is at b. We walk backwards
1327 ;; until finding the property.
1328 (defun notmuch-show-message-extent ()
1331 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
1335 (defun notmuch-show-message-top ()
1336 (car (notmuch-show-message-extent)))
1338 (defun notmuch-show-message-bottom ()
1339 (cdr (notmuch-show-message-extent)))
1341 (defun notmuch-show-goto-message-next ()
1342 (let ((start (point)))
1343 (notmuch-show-move-to-message-bottom)
1349 (defun notmuch-show-goto-message-previous ()
1350 (notmuch-show-move-to-message-top)
1354 (notmuch-show-move-to-message-top)
1357 (defun notmuch-show-mapc (function)
1358 "Iterate through all messages in the current thread with
1359 `notmuch-show-goto-message-next' and call FUNCTION for side
1362 (goto-char (point-min))
1363 (loop do (funcall function)
1364 while (notmuch-show-goto-message-next))))
1366 ;; Functions relating to the visibility of messages and their
1369 (defun notmuch-show-message-visible (props visible-p)
1370 (overlay-put (plist-get props :message-overlay) 'invisible (not visible-p))
1371 (notmuch-show-set-prop :message-visible visible-p props))
1373 (defun notmuch-show-headers-visible (props visible-p)
1374 (overlay-put (plist-get props :headers-overlay) 'invisible (not visible-p))
1375 (notmuch-show-set-prop :headers-visible visible-p props))
1377 ;; Functions for setting and getting attributes of the current
1380 (defun notmuch-show-set-message-properties (props)
1382 (notmuch-show-move-to-message-top)
1383 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
1385 (defun notmuch-show-get-message-properties ()
1386 "Return the properties of the current message as a plist.
1388 Some useful entries are:
1389 :headers - Property list containing the headers :Date, :Subject, :From, etc.
1390 :body - Body of the message
1391 :tags - Tags for this message"
1393 (notmuch-show-move-to-message-top)
1394 (get-text-property (point) :notmuch-message-properties)))
1396 (defun notmuch-show-get-part-properties ()
1397 "Return the properties of the innermost part containing point.
1399 This is the part property list retrieved from the CLI. Signals
1400 an error if there is no part containing point."
1401 (or (get-text-property (point) :notmuch-part)
1402 (error "No message part here")))
1404 (defun notmuch-show-set-prop (prop val &optional props)
1405 (let ((inhibit-read-only t)
1407 (notmuch-show-get-message-properties))))
1408 (plist-put props prop val)
1409 (notmuch-show-set-message-properties props)))
1411 (defun notmuch-show-get-prop (prop &optional props)
1412 (let ((props (or props
1413 (notmuch-show-get-message-properties))))
1414 (plist-get props prop)))
1416 (defun notmuch-show-get-message-id (&optional bare)
1417 "Return an id: query for the Message-Id of the current message.
1419 If optional argument BARE is non-nil, return
1420 the Message-Id without id: prefix and escaping."
1422 (notmuch-show-get-prop :id)
1423 (notmuch-id-to-query (notmuch-show-get-prop :id))))
1425 (defun notmuch-show-get-messages-ids ()
1426 "Return all id: queries of messages in the current thread."
1427 (let ((message-ids))
1429 (lambda () (push (notmuch-show-get-message-id) message-ids)))
1432 (defun notmuch-show-get-messages-ids-search ()
1433 "Return a search string for all message ids of messages in the
1435 (mapconcat 'identity (notmuch-show-get-messages-ids) " or "))
1437 ;; dme: Would it make sense to use a macro for many of these?
1439 (defun notmuch-show-get-filename ()
1440 "Return the filename of the current message."
1441 (notmuch-show-get-prop :filename))
1443 (defun notmuch-show-get-header (header &optional props)
1444 "Return the named header of the current message, if any."
1445 (plist-get (notmuch-show-get-prop :headers props) header))
1447 (defun notmuch-show-get-cc ()
1448 (notmuch-show-get-header :Cc))
1450 (defun notmuch-show-get-date ()
1451 (notmuch-show-get-header :Date))
1453 (defun notmuch-show-get-from ()
1454 (notmuch-show-get-header :From))
1456 (defun notmuch-show-get-subject ()
1457 (notmuch-show-get-header :Subject))
1459 (defun notmuch-show-get-to ()
1460 (notmuch-show-get-header :To))
1462 (defun notmuch-show-get-depth ()
1463 (notmuch-show-get-prop :depth))
1465 (defun notmuch-show-set-tags (tags)
1466 "Set the tags of the current message."
1467 (notmuch-show-set-prop :tags tags)
1468 (notmuch-show-update-tags tags))
1470 (defun notmuch-show-get-tags ()
1471 "Return the tags of the current message."
1472 (notmuch-show-get-prop :tags))
1474 (defun notmuch-show-message-visible-p ()
1475 "Is the current message visible?"
1476 (notmuch-show-get-prop :message-visible))
1478 (defun notmuch-show-headers-visible-p ()
1479 "Are the headers of the current message visible?"
1480 (notmuch-show-get-prop :headers-visible))
1482 (defun notmuch-show-mark-read (&optional unread)
1483 "Mark the current message as read.
1485 Mark the current message as read by applying the tag changes in
1486 `notmuch-show-mark-read-tags' to it (remove the \"unread\" tag by
1487 default). If a prefix argument is given, the message will be
1488 marked as unread, i.e. the tag changes in
1489 `notmuch-show-mark-read-tags' will be reversed."
1491 (when notmuch-show-mark-read-tags
1492 (apply 'notmuch-show-tag-message
1493 (notmuch-tag-change-list notmuch-show-mark-read-tags unread))))
1495 ;; Functions for getting attributes of several messages in the current
1498 (defun notmuch-show-get-message-ids-for-open-messages ()
1499 "Return a list of all id: queries for open messages in the current thread."
1501 (let (message-ids done)
1502 (goto-char (point-min))
1504 (if (notmuch-show-message-visible-p)
1505 (setq message-ids (append message-ids (list (notmuch-show-get-message-id)))))
1506 (setq done (not (notmuch-show-goto-message-next)))
1511 ;; Commands typically bound to keys.
1513 (defun notmuch-show-advance ()
1514 "Advance through thread.
1516 If the current message in the thread is not yet fully visible,
1517 scroll by a near screenful to read more of the message.
1519 Otherwise, (the end of the current message is already within the
1520 current window), advance to the next open message."
1522 (let* ((end-of-this-message (notmuch-show-message-bottom))
1523 (visible-end-of-this-message (1- end-of-this-message))
1525 (while (invisible-p visible-end-of-this-message)
1526 (setq visible-end-of-this-message
1528 (1- (previous-single-char-property-change
1529 visible-end-of-this-message 'invisible)))))
1531 ;; Ideally we would test `end-of-this-message' against the result
1532 ;; of `window-end', but that doesn't account for the fact that
1533 ;; the end of the message might be hidden.
1534 ((and visible-end-of-this-message
1535 (> visible-end-of-this-message (window-end)))
1536 ;; The bottom of this message is not visible - scroll.
1539 ((not (= end-of-this-message (point-max)))
1540 ;; This is not the last message - move to the next visible one.
1541 (notmuch-show-next-open-message))
1543 ((not (= (point) (point-max)))
1544 ;; This is the last message, but the cursor is not at the end of
1545 ;; the buffer. Move it there.
1546 (goto-char (point-max)))
1549 ;; This is the last message - change the return value
1553 (defun notmuch-show-advance-and-archive ()
1554 "Advance through thread and archive.
1556 This command is intended to be one of the simplest ways to
1557 process a thread of email. It works exactly like
1558 notmuch-show-advance, in that it scrolls through messages in a
1559 show buffer, except that when it gets to the end of the buffer it
1560 archives the entire current thread, (apply changes in
1561 `notmuch-archive-tags'), kills the buffer, and displays the next
1562 thread from the search from which this thread was originally
1565 (if (notmuch-show-advance)
1566 (notmuch-show-archive-thread-then-next)))
1568 (defun notmuch-show-rewind ()
1569 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
1571 Specifically, if the beginning of the previous email is fewer
1572 than `window-height' lines from the current point, move to it
1573 just like `notmuch-show-previous-message'.
1575 Otherwise, just scroll down a screenful of the current message.
1577 This command does not modify any message tags, (it does not undo
1578 any effects from previous calls to
1579 `notmuch-show-advance-and-archive'."
1581 (let ((start-of-message (notmuch-show-message-top))
1582 (start-of-window (window-start)))
1584 ;; Either this message is properly aligned with the start of the
1585 ;; window or the start of this message is not visible on the
1587 ((or (= start-of-message start-of-window)
1588 (< start-of-message start-of-window))
1590 ;; If a small number of lines from the previous message are
1591 ;; visible, realign so that the top of the current message is at
1592 ;; the top of the screen.
1593 (when (<= (count-screen-lines (window-start) start-of-message)
1594 next-screen-context-lines)
1595 (goto-char (notmuch-show-message-top))
1596 (notmuch-show-message-adjust))
1597 ;; Move to the top left of the window.
1598 (goto-char (window-start)))
1600 ;; Move to the previous message.
1601 (notmuch-show-previous-message)))))
1603 (defun notmuch-show-reply (&optional prompt-for-sender)
1604 "Reply to the sender and all recipients of the current message."
1606 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender t))
1608 (defun notmuch-show-reply-sender (&optional prompt-for-sender)
1609 "Reply to the sender of the current message."
1611 (notmuch-mua-new-reply (notmuch-show-get-message-id) prompt-for-sender nil))
1613 (defun notmuch-show-forward-message (&optional prompt-for-sender)
1614 "Forward the current message."
1616 (with-current-notmuch-show-message
1617 (notmuch-mua-new-forward-message prompt-for-sender)))
1619 (defun notmuch-show-next-message (&optional pop-at-end)
1620 "Show the next message.
1622 If a prefix argument is given and this is the last message in the
1623 thread, navigate to the next thread in the parent search buffer."
1625 (if (notmuch-show-goto-message-next)
1627 (notmuch-show-mark-read)
1628 (notmuch-show-message-adjust))
1630 (notmuch-show-next-thread)
1631 (goto-char (point-max)))))
1633 (defun notmuch-show-previous-message ()
1634 "Show the previous message or the start of the current message."
1636 (if (= (point) (notmuch-show-message-top))
1637 (notmuch-show-goto-message-previous)
1638 (notmuch-show-move-to-message-top))
1639 (notmuch-show-mark-read)
1640 (notmuch-show-message-adjust))
1642 (defun notmuch-show-next-open-message (&optional pop-at-end)
1643 "Show the next open message.
1645 If a prefix argument is given and this is the last open message
1646 in the thread, navigate to the next thread in the parent search
1647 buffer. Return t if there was a next open message in the thread
1648 to show, nil otherwise."
1651 (while (and (setq r (notmuch-show-goto-message-next))
1652 (not (notmuch-show-message-visible-p))))
1655 (notmuch-show-mark-read)
1656 (notmuch-show-message-adjust))
1658 (notmuch-show-next-thread)
1659 (goto-char (point-max))))
1662 (defun notmuch-show-next-matching-message ()
1663 "Show the next matching message."
1666 (while (and (setq r (notmuch-show-goto-message-next))
1667 (not (notmuch-show-get-prop :match))))
1670 (notmuch-show-mark-read)
1671 (notmuch-show-message-adjust))
1672 (goto-char (point-max)))))
1674 (defun notmuch-show-open-if-matched ()
1675 "Open a message if it is matched (whether or not excluded)."
1676 (let ((props (notmuch-show-get-message-properties)))
1677 (notmuch-show-message-visible props (plist-get props :match))))
1679 (defun notmuch-show-goto-first-wanted-message ()
1680 "Move to the first open message and mark it read"
1681 (goto-char (point-min))
1682 (if (notmuch-show-message-visible-p)
1683 (notmuch-show-mark-read)
1684 (notmuch-show-next-open-message))
1686 ;; There are no matched non-excluded messages so open all matched
1687 ;; (necessarily excluded) messages and go to the first.
1688 (notmuch-show-mapc 'notmuch-show-open-if-matched)
1689 (force-window-update)
1690 (goto-char (point-min))
1691 (if (notmuch-show-message-visible-p)
1692 (notmuch-show-mark-read)
1693 (notmuch-show-next-open-message))))
1695 (defun notmuch-show-previous-open-message ()
1696 "Show the previous open message."
1698 (while (and (if (= (point) (notmuch-show-message-top))
1699 (notmuch-show-goto-message-previous)
1700 (notmuch-show-move-to-message-top))
1701 (not (notmuch-show-message-visible-p))))
1702 (notmuch-show-mark-read)
1703 (notmuch-show-message-adjust))
1705 (defun notmuch-show-view-raw-message ()
1706 "View the file holding the current message."
1708 (let* ((id (notmuch-show-get-message-id))
1709 (buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
1710 (call-process notmuch-command nil buf nil "show" "--format=raw" id)
1711 (switch-to-buffer buf)
1712 (goto-char (point-min))
1713 (set-buffer-modified-p nil)
1714 (view-buffer buf 'kill-buffer-if-not-modified)))
1716 (defun notmuch-show-pipe-message (entire-thread command)
1717 "Pipe the contents of the current message (or thread) to the given command.
1719 The given command will be executed with the raw contents of the
1720 current email message as stdin. Anything printed by the command
1721 to stdout or stderr will appear in the *notmuch-pipe* buffer.
1723 When invoked with a prefix argument, the command will receive all
1724 open messages in the current thread (formatted as an mbox) rather
1725 than only the current message."
1726 (interactive (let ((query-string (if current-prefix-arg
1727 "Pipe all open messages to command: "
1728 "Pipe message to command: ")))
1729 (list current-prefix-arg (read-string query-string))))
1730 (let (shell-command)
1733 (concat notmuch-command " show --format=mbox --exclude=false "
1734 (shell-quote-argument
1735 (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
1738 (concat notmuch-command " show --format=raw "
1739 (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
1740 (let ((buf (get-buffer-create (concat "*notmuch-pipe*"))))
1741 (with-current-buffer buf
1742 (setq buffer-read-only nil)
1744 (let ((exit-code (call-process-shell-command shell-command nil buf)))
1745 (goto-char (point-max))
1746 (set-buffer-modified-p nil)
1747 (setq buffer-read-only t)
1748 (unless (zerop exit-code)
1749 (switch-to-buffer-other-window buf)
1750 (message (format "Command '%s' exited abnormally with code %d"
1751 shell-command exit-code))))))))
1753 (defun notmuch-show-tag-message (&rest tag-changes)
1754 "Change tags for the current message.
1756 TAG-CHANGES is a list of tag operations for `notmuch-tag'."
1757 (let* ((current-tags (notmuch-show-get-tags))
1758 (new-tags (notmuch-update-tags current-tags tag-changes)))
1759 (unless (equal current-tags new-tags)
1760 (notmuch-tag (notmuch-show-get-message-id) tag-changes)
1761 (notmuch-show-set-tags new-tags))))
1763 (defun notmuch-show-tag (&optional tag-changes)
1764 "Change tags for the current message.
1766 See `notmuch-tag' for information on the format of TAG-CHANGES."
1768 (let* ((tag-changes (notmuch-tag (notmuch-show-get-message-id) tag-changes))
1769 (current-tags (notmuch-show-get-tags))
1770 (new-tags (notmuch-update-tags current-tags tag-changes)))
1771 (unless (equal current-tags new-tags)
1772 (notmuch-show-set-tags new-tags))))
1774 (defun notmuch-show-tag-all (&optional tag-changes)
1775 "Change tags for all messages in the current show buffer.
1777 See `notmuch-tag' for information on the format of TAG-CHANGES."
1779 (setq tag-changes (notmuch-tag (notmuch-show-get-messages-ids-search) tag-changes))
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-show-set-tags new-tags))))))
1787 (defun notmuch-show-add-tag ()
1788 "Same as `notmuch-show-tag' but sets initial input to '+'."
1790 (notmuch-show-tag "+"))
1792 (defun notmuch-show-remove-tag ()
1793 "Same as `notmuch-show-tag' but sets initial input to '-'."
1795 (notmuch-show-tag "-"))
1797 (defun notmuch-show-toggle-visibility-headers ()
1798 "Toggle the visibility of the current message headers."
1800 (let ((props (notmuch-show-get-message-properties)))
1801 (notmuch-show-headers-visible
1803 (not (plist-get props :headers-visible))))
1804 (force-window-update))
1806 (defun notmuch-show-toggle-message ()
1807 "Toggle the visibility of the current message."
1809 (let ((props (notmuch-show-get-message-properties)))
1810 (notmuch-show-message-visible
1812 (not (plist-get props :message-visible))))
1813 (force-window-update))
1815 (defun notmuch-show-open-or-close-all ()
1816 "Set the visibility all of the messages in the current thread.
1817 By default make all of the messages visible. With a prefix
1818 argument, hide all of the messages."
1821 (goto-char (point-min))
1822 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1823 (not current-prefix-arg))
1824 until (not (notmuch-show-goto-message-next))))
1825 (force-window-update))
1827 (defun notmuch-show-next-button ()
1828 "Advance point to the next button in the buffer."
1832 (defun notmuch-show-previous-button ()
1833 "Move point back to the previous button in the buffer."
1835 (backward-button 1))
1837 (defun notmuch-show-next-thread (&optional show previous)
1838 "Move to the next item in the search results, if any.
1840 If SHOW is non-nil, open the next item in a show
1841 buffer. Otherwise just highlight the next item in the search
1842 buffer. If PREVIOUS is non-nil, move to the previous item in the
1843 search results instead."
1845 (let ((parent-buffer notmuch-show-parent-buffer))
1846 (notmuch-kill-this-buffer)
1847 (when (buffer-live-p parent-buffer)
1848 (switch-to-buffer parent-buffer)
1850 (notmuch-search-previous-thread)
1851 (notmuch-search-next-thread))
1853 (notmuch-search-show-thread)))))
1855 (defun notmuch-show-next-thread-show ()
1856 "Show the next thread in the search results, if any."
1858 (notmuch-show-next-thread t))
1860 (defun notmuch-show-previous-thread-show ()
1861 "Show the previous thread in the search results, if any."
1863 (notmuch-show-next-thread t t))
1865 (defun notmuch-show-archive-thread (&optional unarchive)
1866 "Archive each message in thread.
1868 Archive each message currently shown by applying the tag changes
1869 in `notmuch-archive-tags' to each. If a prefix argument is given,
1870 the messages will be \"unarchived\", i.e. the tag changes in
1871 `notmuch-archive-tags' will be reversed.
1873 Note: This command is safe from any race condition of new messages
1874 being delivered to the same thread. It does not archive the
1875 entire thread, but only the messages shown in the current
1878 (when notmuch-archive-tags
1879 (notmuch-show-tag-all
1880 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
1882 (defun notmuch-show-archive-thread-then-next ()
1883 "Archive all messages in the current buffer, then show next thread from search."
1885 (notmuch-show-archive-thread)
1886 (notmuch-show-next-thread t))
1888 (defun notmuch-show-archive-thread-then-exit ()
1889 "Archive all messages in the current buffer, then exit back to search results."
1891 (notmuch-show-archive-thread)
1892 (notmuch-show-next-thread))
1894 (defun notmuch-show-archive-message (&optional unarchive)
1895 "Archive the current message.
1897 Archive the current message by applying the tag changes in
1898 `notmuch-archive-tags' to it. If a prefix argument is given, the
1899 message will be \"unarchived\", i.e. the tag changes in
1900 `notmuch-archive-tags' will be reversed."
1902 (when notmuch-archive-tags
1903 (apply 'notmuch-show-tag-message
1904 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
1906 (defun notmuch-show-archive-message-then-next-or-exit ()
1907 "Archive the current message, then show the next open message in the current thread.
1909 If at the last open message in the current thread, then exit back
1912 (notmuch-show-archive-message)
1913 (notmuch-show-next-open-message t))
1915 (defun notmuch-show-archive-message-then-next-or-next-thread ()
1916 "Archive the current message, then show the next open message in the current thread.
1918 If at the last open message in the current thread, then show next
1919 thread from search."
1921 (notmuch-show-archive-message)
1922 (unless (notmuch-show-next-open-message)
1923 (notmuch-show-next-thread t)))
1925 (defun notmuch-show-stash-cc ()
1926 "Copy CC field of current message to kill-ring."
1928 (notmuch-common-do-stash (notmuch-show-get-cc)))
1930 (defun notmuch-show-stash-date ()
1931 "Copy date of current message to kill-ring."
1933 (notmuch-common-do-stash (notmuch-show-get-date)))
1935 (defun notmuch-show-stash-filename ()
1936 "Copy filename of current message to kill-ring."
1938 (notmuch-common-do-stash (notmuch-show-get-filename)))
1940 (defun notmuch-show-stash-from ()
1941 "Copy From address of current message to kill-ring."
1943 (notmuch-common-do-stash (notmuch-show-get-from)))
1945 (defun notmuch-show-stash-message-id (&optional stash-thread-id)
1946 "Copy id: query matching the current message to kill-ring.
1948 If invoked with a prefix argument (or STASH-THREAD-ID is
1949 non-nil), copy thread: query matching the current thread to
1953 (notmuch-common-do-stash notmuch-show-thread-id)
1954 (notmuch-common-do-stash (notmuch-show-get-message-id))))
1956 (defun notmuch-show-stash-message-id-stripped ()
1957 "Copy message ID of current message (sans `id:' prefix) to kill-ring."
1959 (notmuch-common-do-stash (notmuch-show-get-message-id t)))
1961 (defun notmuch-show-stash-subject ()
1962 "Copy Subject field of current message to kill-ring."
1964 (notmuch-common-do-stash (notmuch-show-get-subject)))
1966 (defun notmuch-show-stash-tags ()
1967 "Copy tags of current message to kill-ring as a comma separated list."
1969 (notmuch-common-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1971 (defun notmuch-show-stash-to ()
1972 "Copy To address of current message to kill-ring."
1974 (notmuch-common-do-stash (notmuch-show-get-to)))
1976 (defun notmuch-show-stash-mlarchive-link (&optional mla)
1977 "Copy an ML Archive URI for the current message to the kill-ring.
1979 This presumes that the message is available at the selected Mailing List Archive.
1981 If optional argument MLA is non-nil, use the provided key instead of prompting
1982 the user (see `notmuch-show-stash-mlarchive-link-alist')."
1984 (notmuch-common-do-stash
1987 (let ((completion-ignore-case t))
1989 "Mailing List Archive: "
1990 notmuch-show-stash-mlarchive-link-alist
1991 nil t nil nil notmuch-show-stash-mlarchive-link-default)))
1992 notmuch-show-stash-mlarchive-link-alist))
1993 (notmuch-show-get-message-id t))))
1995 (defun notmuch-show-stash-mlarchive-link-and-go (&optional mla)
1996 "Copy an ML Archive URI for the current message to the kill-ring and visit it.
1998 This presumes that the message is available at the selected Mailing List Archive.
2000 If optional argument MLA is non-nil, use the provided key instead of prompting
2001 the user (see `notmuch-show-stash-mlarchive-link-alist')."
2003 (notmuch-show-stash-mlarchive-link mla)
2004 (browse-url (current-kill 0 t)))
2006 ;; Interactive part functions and their helpers
2008 (defun notmuch-show-generate-part-buffer (message-id nth)
2009 "Return a temporary buffer containing the specified part's content."
2010 (let ((buf (generate-new-buffer " *notmuch-part*"))
2011 (process-crypto notmuch-show-process-crypto))
2012 (with-current-buffer buf
2013 (setq notmuch-show-process-crypto process-crypto)
2014 ;; Always acquires the part via `notmuch part', even if it is
2015 ;; available in the SEXP output.
2016 (insert (notmuch-get-bodypart-internal message-id nth notmuch-show-process-crypto)))
2019 (defun notmuch-show-current-part-handle ()
2020 "Return an mm-handle for the part containing point.
2022 This creates a temporary buffer for the part's content; the
2023 caller is responsible for killing this buffer as appropriate."
2024 (let* ((part (notmuch-show-get-part-properties))
2025 (message-id (notmuch-show-get-message-id))
2026 (nth (plist-get part :id))
2027 (buf (notmuch-show-generate-part-buffer message-id nth))
2028 (computed-type (plist-get part :computed-type))
2029 (filename (plist-get part :filename))
2030 (disposition (if filename `(attachment (filename . ,filename)))))
2031 (mm-make-handle buf (list computed-type) nil nil disposition)))
2033 (defun notmuch-show-apply-to-current-part-handle (fn)
2034 "Apply FN to an mm-handle for the part containing point.
2036 This ensures that the temporary buffer created for the mm-handle
2037 is destroyed when FN returns."
2038 (let ((handle (notmuch-show-current-part-handle)))
2041 (kill-buffer (mm-handle-buffer handle)))))
2043 (defun notmuch-show-part-button-default (&optional button)
2045 (let ((button (or button (button-at (point)))))
2046 ;; Try to toggle the part, if that fails then call the default
2047 ;; action. The toggle fails if the part has no emacs renderable
2049 (unless (notmuch-show-toggle-part-invisibility button)
2050 (call-interactively notmuch-show-part-button-default-action))))
2052 (defun notmuch-show-save-part ()
2053 "Save the MIME part containing point to a file."
2055 (notmuch-show-apply-to-current-part-handle #'mm-save-part))
2057 (defun notmuch-show-view-part ()
2058 "View the MIME part containing point in an external viewer."
2060 ;; Set mm-inlined-types to nil to force an external viewer
2061 (let ((mm-inlined-types nil))
2062 (notmuch-show-apply-to-current-part-handle #'mm-display-part)))
2064 (defun notmuch-show-interactively-view-part ()
2065 "View the MIME part containing point, prompting for a viewer."
2067 (notmuch-show-apply-to-current-part-handle #'mm-interactively-view-part))
2069 (defun notmuch-show-pipe-part ()
2070 "Pipe the MIME part containing point to an external command."
2072 (notmuch-show-apply-to-current-part-handle #'mm-pipe-part))
2075 (provide 'notmuch-show)