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))
31 (require 'notmuch-lib)
32 (require 'notmuch-query)
33 (require 'notmuch-wash)
34 (require 'notmuch-mua)
36 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
37 (declare-function notmuch-fontify-headers "notmuch" nil)
38 (declare-function notmuch-select-tag-with-completion "notmuch" (prompt &rest search-terms))
39 (declare-function notmuch-search-show-thread "notmuch" nil)
41 (defcustom notmuch-message-headers '("Subject" "To" "Cc" "Date")
42 "Headers that should be shown in a message, in this order.
44 For an open message, all of these headers will be made visible
45 according to `notmuch-message-headers-visible' or can be toggled
46 with `notmuch-show-toggle-headers'. For a closed message, only
47 the first header in the list will be visible."
49 :type '(repeat string))
51 (defcustom notmuch-message-headers-visible t
52 "Should the headers be visible by default?
54 If this value is non-nil, then all of the headers defined in
55 `notmuch-message-headers' will be visible by default in the display
56 of each message. Otherwise, these headers will be hidden and
57 `notmuch-show-toggle-headers' can be used to make the visible for
62 (defcustom notmuch-show-relative-dates t
63 "Display relative dates in the message summary line."
67 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
68 "A list of functions called to decorate the headers listed in
69 `notmuch-message-headers'.")
71 (defcustom notmuch-show-hook nil
72 "Functions called after populating a `notmuch-show' buffer."
76 (defcustom notmuch-show-insert-text/plain-hook '(notmuch-wash-excerpt-citations)
77 "Functions used to improve the display of text/plain parts."
80 :options '(notmuch-wash-convert-inline-patch-to-part
81 notmuch-wash-wrap-long-lines
82 notmuch-wash-tidy-citations
83 notmuch-wash-elide-blank-lines
84 notmuch-wash-excerpt-citations))
86 (defcustom notmuch-show-indent-multipart nil
87 "Should the sub-parts of a multipart/* part be indented?"
88 ;; dme: Not sure which is a good default.
92 (defmacro with-current-notmuch-show-message (&rest body)
93 "Evaluate body with current buffer set to the text of current message"
95 (let ((id (notmuch-show-get-message-id)))
96 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
97 (with-current-buffer buf
98 (call-process notmuch-command nil t nil "show" "--format=raw" id)
100 (kill-buffer buf)))))
102 (defun notmuch-show-view-all-mime-parts ()
103 "Use external viewers to view all attachments from the current message."
105 (with-current-notmuch-show-message
106 ; We ovverride the mm-inline-media-tests to indicate which message
107 ; parts are already sufficiently handled by the original
108 ; presentation of the message in notmuch-show mode. These parts
109 ; will be inserted directly into the temporary buffer of
110 ; with-current-notmuch-show-message and silently discarded.
112 ; Any MIME part not explicitly mentioned here will be handled by an
113 ; external viewer as configured in the various mailcap files.
114 (let ((mm-inline-media-tests '(
115 ("text/.*" ignore identity)
116 ("application/pgp-signature" ignore identity)
117 ("multipart/alternative" ignore identity)
118 ("multipart/mixed" ignore identity)
119 ("multipart/related" ignore identity)
121 (mm-display-parts (mm-dissect-buffer)))))
123 (defun notmuch-foreach-mime-part (function mm-handle)
124 (cond ((stringp (car mm-handle))
125 (dolist (part (cdr mm-handle))
126 (notmuch-foreach-mime-part function part)))
127 ((bufferp (car mm-handle))
128 (funcall function mm-handle))
129 (t (dolist (part mm-handle)
130 (notmuch-foreach-mime-part function part)))))
132 (defun notmuch-count-attachments (mm-handle)
134 (notmuch-foreach-mime-part
136 (let ((disposition (mm-handle-disposition p)))
137 (and (listp disposition)
138 (or (equal (car disposition) "attachment")
139 (and (equal (car disposition) "inline")
140 (assq 'filename disposition)))
145 (defun notmuch-save-attachments (mm-handle &optional queryp)
146 (notmuch-foreach-mime-part
148 (let ((disposition (mm-handle-disposition p)))
149 (and (listp disposition)
150 (or (equal (car disposition) "attachment")
151 (and (equal (car disposition) "inline")
152 (assq 'filename disposition)))
155 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
159 (defun notmuch-show-save-attachments ()
160 "Save all attachments from the current message."
162 (with-current-notmuch-show-message
163 (let ((mm-handle (mm-dissect-buffer)))
164 (notmuch-save-attachments
165 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
168 (defun notmuch-show-fontify-header ()
170 ((looking-at "[Tt]o:")
172 ((looking-at "[Bb]?[Cc][Cc]:")
174 ((looking-at "[Ss]ubject:")
175 'message-header-subject)
176 ((looking-at "[Ff]rom:")
177 'message-header-from)
179 'message-header-other))))
181 (overlay-put (make-overlay (point) (re-search-forward ":"))
182 'face 'message-header-name)
183 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
186 (defun notmuch-show-colour-headers ()
187 "Apply some colouring to the current headers."
188 (goto-char (point-min))
189 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
190 (notmuch-show-fontify-header)
193 (defun notmuch-show-spaces-n (n)
194 "Return a string comprised of `n' spaces."
197 (defun notmuch-show-update-tags (tags)
198 "Update the displayed tags of the current message."
200 (goto-char (notmuch-show-message-top))
201 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
202 (let ((inhibit-read-only t))
203 (replace-match (concat "("
204 (propertize (mapconcat 'identity tags " ")
205 'face 'notmuch-tag-face)
208 (defun notmuch-show-insert-headerline (headers date tags depth)
209 "Insert a notmuch style headerline based on HEADERS for a
210 message at DEPTH in the current thread."
211 (let ((start (point)))
212 (insert (notmuch-show-spaces-n depth)
213 (plist-get headers :From)
217 (propertize (mapconcat 'identity tags " ")
218 'face 'notmuch-tag-face)
220 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
222 (defun notmuch-show-insert-header (header header-value)
223 "Insert a single header."
224 (insert header ": " header-value "\n"))
226 (defun notmuch-show-insert-headers (headers)
227 "Insert the headers of the current message."
228 (let ((start (point)))
229 (mapc '(lambda (header)
230 (let* ((header-symbol (intern (concat ":" header)))
231 (header-value (plist-get headers header-symbol)))
232 (if (and header-value
233 (not (string-equal "" header-value)))
234 (notmuch-show-insert-header header header-value))))
235 notmuch-message-headers)
238 (narrow-to-region start (point-max))
239 (run-hooks 'notmuch-show-markup-headers-hook)))))
241 (define-button-type 'notmuch-show-part-button-type
242 'action 'notmuch-show-part-button-action
246 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name comment)
249 (if name (concat name ": ") "")
251 (if (not (string-equal declared-type content-type))
252 (concat " (as " content-type ")")
256 :type 'notmuch-show-part-button-type
258 :notmuch-filename name))
260 ;; Functions handling particular MIME parts.
262 (defun notmuch-show-save-part (message-id nth &optional filename)
264 ;; Always acquires the part via `notmuch part', even if it is
265 ;; available in the JSON output.
266 (insert (notmuch-show-get-bodypart-internal message-id nth))
267 (let ((file (read-file-name
268 "Filename to save as: "
269 (or mailcap-download-directory "~/")
272 (require-final-newline nil)
273 (coding-system-for-write 'no-conversion))
274 (write-region (point-min) (point-max) file))))
276 (defun notmuch-show-mm-display-part-inline (msg part content-type content)
277 "Use the mm-decode/mm-view functions to display a part in the
278 current buffer, if possible."
279 (let ((display-buffer (current-buffer)))
282 (let ((handle (mm-make-handle (current-buffer) (list content-type))))
283 (set-buffer display-buffer)
284 (if (and (mm-inlinable-p handle)
285 (mm-inlined-p handle))
287 (mm-display-part handle)
291 (defvar notmuch-show-multipart/alternative-discouraged
295 ;; multipart/related usually contain a text/html part and some associated graphics.
299 (defun notmuch-show-multipart/*-to-list (part)
300 (mapcar '(lambda (inner-part) (plist-get inner-part :content-type))
301 (plist-get part :content)))
303 (defun notmuch-show-multipart/alternative-choose (types)
304 ;; Based on `mm-preferred-alternative-precedence'.
306 (dolist (pref (reverse notmuch-show-multipart/alternative-discouraged))
307 (dolist (elem (copy-sequence seq))
308 (when (string-match pref elem)
309 (setq seq (nconc (delete elem seq) (list elem))))))
312 (defun notmuch-show-insert-part-multipart/alternative (msg part content-type nth depth declared-type)
313 (notmuch-show-insert-part-header nth declared-type content-type nil)
314 (let ((chosen-type (car (notmuch-show-multipart/alternative-choose (notmuch-show-multipart/*-to-list part))))
315 (inner-parts (plist-get part :content))
317 ;; This inserts all parts of the chosen type rather than just one,
318 ;; but it's not clear that this is the wrong thing to do - which
319 ;; should be chosen if there are more than one that match?
320 (mapc (lambda (inner-part)
321 (let ((inner-type (plist-get inner-part :content-type)))
322 (if (string= chosen-type inner-type)
323 (notmuch-show-insert-bodypart msg inner-part depth)
324 (notmuch-show-insert-part-header (plist-get inner-part :id) inner-type inner-type nil " (not shown)"))))
327 (when notmuch-show-indent-multipart
328 (indent-rigidly start (point) 1)))
331 (defun notmuch-show-insert-part-multipart/* (msg part content-type nth depth declared-type)
332 (notmuch-show-insert-part-header nth declared-type content-type nil)
333 (let ((inner-parts (plist-get part :content))
335 ;; Show all of the parts.
336 (mapc (lambda (inner-part)
337 (notmuch-show-insert-bodypart msg inner-part depth))
340 (when notmuch-show-indent-multipart
341 (indent-rigidly start (point) 1)))
344 (defun notmuch-show-insert-part-message/rfc822 (msg part content-type nth depth declared-type)
345 (let* ((message-part (plist-get part :content))
346 (inner-parts (plist-get message-part :content)))
347 (notmuch-show-insert-part-header nth declared-type content-type nil)
348 ;; Override `notmuch-message-headers' to force `From' to be
350 (let ((notmuch-message-headers '("From" "Subject" "To" "Cc" "Date")))
351 (notmuch-show-insert-headers (plist-get part :headers)))
352 ;; Blank line after headers to be compatible with the normal
356 ;; Show all of the parts.
357 (mapc (lambda (inner-part)
358 (notmuch-show-insert-bodypart msg inner-part depth))
362 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
363 (let ((start (point)))
364 ;; If this text/plain part is not the first part in the message,
365 ;; insert a header to make this clear.
367 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
368 (insert (notmuch-show-get-bodypart-content msg part nth))
371 (narrow-to-region start (point-max))
372 (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
375 (defun notmuch-show-insert-part-text/x-vcalendar (msg part content-type nth depth declared-type)
376 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename))
377 (insert (with-temp-buffer
378 (insert (notmuch-show-get-bodypart-content msg part nth))
379 (goto-char (point-min))
380 (let ((file (make-temp-file "notmuch-ical"))
382 (icalendar--convert-ical-to-diary
383 (icalendar--read-element nil nil)
385 (set-buffer (get-file-buffer file))
386 (setq result (buffer-substring (point-min) (point-max)))
387 (set-buffer-modified-p nil)
388 (kill-buffer (current-buffer))
393 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
394 ;; If we can deduce a MIME type from the filename of the attachment,
395 ;; do so and pass it on to the handler for that type.
396 (if (plist-get part :filename)
397 (let ((extension (file-name-extension (plist-get part :filename)))
401 (mailcap-parse-mimetypes)
402 (setq mime-type (mailcap-extension-to-mime extension))
404 (not (string-equal mime-type "application/octet-stream")))
405 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
409 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
410 ;; This handler _must_ succeed - it is the handler of last resort.
411 (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
412 (let ((content (notmuch-show-get-bodypart-content msg part nth)))
414 (notmuch-show-mm-display-part-inline msg part content-type content)))
417 ;; Functions for determining how to handle MIME parts.
419 (defun notmuch-show-split-content-type (content-type)
420 (split-string content-type "/"))
422 (defun notmuch-show-handlers-for (content-type)
423 "Return a list of content handlers for a part of type CONTENT-TYPE."
428 ;; Reverse order of prefrence.
429 (list (intern (concat "notmuch-show-insert-part-*/*"))
431 "notmuch-show-insert-part-"
432 (car (notmuch-show-split-content-type content-type))
434 (intern (concat "notmuch-show-insert-part-" content-type))))
437 ;; Helper for parts which are generally not included in the default
440 (defun notmuch-show-get-bodypart-internal (message-id part-number)
442 (let ((coding-system-for-read 'no-conversion))
443 (call-process notmuch-command nil t nil
444 "part" (format "--part=%s" part-number) message-id)
447 (defun notmuch-show-get-bodypart-content (msg part nth)
448 (or (plist-get part :content)
449 (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
453 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
454 (let ((handlers (notmuch-show-handlers-for content-type)))
455 ;; Run the content handlers until one of them returns a non-nil
458 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
459 (setq handlers (cdr handlers))))
462 (defun notmuch-show-insert-bodypart (msg part depth)
463 "Insert the body part PART at depth DEPTH in the current thread."
464 (let ((content-type (downcase (plist-get part :content-type)))
465 (nth (plist-get part :id)))
466 (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
467 ;; Some of the body part handlers leave point somewhere up in the
468 ;; part, so we make sure that we're down at the end.
469 (goto-char (point-max))
470 ;; Ensure that the part ends with a carriage return.
474 (defun notmuch-show-insert-body (msg body depth)
475 "Insert the body BODY at depth DEPTH in the current thread."
476 (mapc '(lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
478 (defun notmuch-show-make-symbol (type)
479 (make-symbol (concat "notmuch-show-" type)))
481 (defun notmuch-show-strip-re (string)
482 (replace-regexp-in-string "\\([Rr]e: *\\)+" "" string))
484 (defvar notmuch-show-previous-subject "")
485 (make-variable-buffer-local 'notmuch-show-previous-subject)
487 (defun notmuch-show-insert-msg (msg depth)
488 "Insert the message MSG at depth DEPTH in the current thread."
489 (let* ((headers (plist-get msg :headers))
490 ;; Indentation causes the buffer offset of the start/end
491 ;; points to move, so we must use markers.
492 message-start message-end
493 content-start content-end
494 headers-start headers-end
496 (headers-invis-spec (notmuch-show-make-symbol "header"))
497 (message-invis-spec (notmuch-show-make-symbol "message"))
498 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
500 ;; Set `buffer-invisibility-spec' to `nil' (a list), otherwise
501 ;; removing items from `buffer-invisibility-spec' (which is what
502 ;; `notmuch-show-headers-visible' and
503 ;; `notmuch-show-message-visible' do) is a no-op and has no
504 ;; effect. This caused threads with only matching messages to have
505 ;; those messages hidden initially because
506 ;; `buffer-invisibility-spec' stayed `t'.
508 ;; This needs to be set here (rather than just above the call to
509 ;; `notmuch-show-headers-visible') because some of the part
510 ;; rendering or body washing functions
511 ;; (e.g. `notmuch-wash-text/plain-citations') manipulate
512 ;; `buffer-invisibility-spec').
513 (when (eq buffer-invisibility-spec t)
514 (setq buffer-invisibility-spec nil))
516 (setq message-start (point-marker))
518 (notmuch-show-insert-headerline headers
519 (or (if notmuch-show-relative-dates
520 (plist-get msg :date_relative)
522 (plist-get headers :Date))
523 (plist-get msg :tags) depth)
525 (setq content-start (point-marker))
527 ;; Set `headers-start' to point after the 'Subject:' header to be
528 ;; compatible with the existing implementation. This just sets it
529 ;; to after the first header.
530 (notmuch-show-insert-headers headers)
531 ;; Headers should include a blank line (backwards compatibility).
534 (goto-char content-start)
535 ;; If the subject of this message is the same as that of the
536 ;; previous message, don't display it when this message is
538 (when (not (string= notmuch-show-previous-subject
541 (setq headers-start (point-marker)))
542 (setq headers-end (point-marker))
544 (setq notmuch-show-previous-subject bare-subject)
546 (setq body-start (point-marker))
547 (notmuch-show-insert-body msg (plist-get msg :body) depth)
548 ;; Ensure that the body ends with a newline.
551 (setq body-end (point-marker))
552 (setq content-end (point-marker))
554 ;; Indent according to the depth in the thread.
555 (indent-rigidly content-start content-end depth)
557 (setq message-end (point-max-marker))
559 ;; Save the extents of this message over the whole text of the
561 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
563 (plist-put msg :headers-invis-spec headers-invis-spec)
564 (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
566 (plist-put msg :message-invis-spec message-invis-spec)
567 (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
569 ;; Save the properties for this message. Currently this saves the
570 ;; entire message (augmented it with other stuff), which seems
571 ;; like overkill. We might save a reduced subset (for example, not
573 (notmuch-show-set-message-properties msg)
575 ;; Set header visibility.
576 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
578 ;; Message visibility depends on whether it matched the search
580 (notmuch-show-message-visible msg (plist-get msg :match))))
582 (defun notmuch-show-insert-tree (tree depth)
583 "Insert the message tree TREE at depth DEPTH in the current thread."
584 (let ((msg (car tree))
585 (replies (cadr tree)))
586 (notmuch-show-insert-msg msg depth)
587 (notmuch-show-insert-thread replies (1+ depth))))
589 (defun notmuch-show-insert-thread (thread depth)
590 "Insert the thread THREAD at depth DEPTH in the current forest."
591 (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
593 (defun notmuch-show-insert-forest (forest)
594 "Insert the forest of threads FOREST."
595 (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
597 (defvar notmuch-show-parent-buffer nil)
600 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
601 "Run \"notmuch show\" with the given thread ID and display results.
603 The optional PARENT-BUFFER is the notmuch-search buffer from
604 which this notmuch-show command was executed, (so that the
605 next thread from that buffer can be show when done with this
608 The optional QUERY-CONTEXT is a notmuch search term. Only
609 messages from the thread matching this search term are shown if
612 The optional BUFFER-NAME provides the neame of the buffer in
613 which the message thread is shown. If it is nil (which occurs
614 when the command is called interactively) the argument to the
616 (interactive "sNotmuch show: ")
617 (let ((buffer (get-buffer-create (generate-new-buffer-name
619 (concat "*notmuch-" thread-id "*")))))
620 (inhibit-read-only t))
621 (switch-to-buffer buffer)
623 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
625 (goto-char (point-min))
627 (let* ((basic-args (list thread-id))
628 (args (if query-context
629 (append (list "\'") basic-args (list "and (" query-context ")\'"))
630 (append (list "\'") basic-args (list "\'")))))
631 (notmuch-show-insert-forest (notmuch-query-get-threads args))
632 ;; If the query context reduced the results to nothing, run
634 (when (and (eq (buffer-size) 0)
636 (notmuch-show-insert-forest
637 (notmuch-query-get-threads basic-args))))
639 ;; Enable buttonisation of URLs and email addresses in the
641 (goto-address-mode t)
642 ;; Act on visual lines rather than logical lines.
645 (run-hooks 'notmuch-show-hook))
647 ;; Move straight to the first open message
648 (if (not (notmuch-show-message-visible-p))
649 (notmuch-show-next-open-message))
651 ;; Set the header line to the subject of the first open message.
652 (setq header-line-format (notmuch-show-strip-re (notmuch-show-get-subject)))
654 (notmuch-show-mark-read)))
656 (defvar notmuch-show-stash-map
657 (let ((map (make-sparse-keymap)))
658 (define-key map "c" 'notmuch-show-stash-cc)
659 (define-key map "d" 'notmuch-show-stash-date)
660 (define-key map "F" 'notmuch-show-stash-filename)
661 (define-key map "f" 'notmuch-show-stash-from)
662 (define-key map "i" 'notmuch-show-stash-message-id)
663 (define-key map "s" 'notmuch-show-stash-subject)
664 (define-key map "T" 'notmuch-show-stash-tags)
665 (define-key map "t" 'notmuch-show-stash-to)
667 "Submap for stash commands")
668 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
670 (defvar notmuch-show-mode-map
671 (let ((map (make-sparse-keymap)))
672 (define-key map "?" 'notmuch-help)
673 (define-key map "q" 'notmuch-kill-this-buffer)
674 (define-key map (kbd "<C-tab>") 'widget-backward)
675 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
676 (define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
677 (define-key map (kbd "TAB") 'notmuch-show-next-button)
678 (define-key map "s" 'notmuch-search)
679 (define-key map "m" 'notmuch-mua-mail)
680 (define-key map "f" 'notmuch-show-forward-message)
681 (define-key map "r" 'notmuch-show-reply)
682 (define-key map "|" 'notmuch-show-pipe-message)
683 (define-key map "w" 'notmuch-show-save-attachments)
684 (define-key map "V" 'notmuch-show-view-raw-message)
685 (define-key map "v" 'notmuch-show-view-all-mime-parts)
686 (define-key map "c" 'notmuch-show-stash-map)
687 (define-key map "h" 'notmuch-show-toggle-headers)
688 (define-key map "-" 'notmuch-show-remove-tag)
689 (define-key map "+" 'notmuch-show-add-tag)
690 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
691 (define-key map "a" 'notmuch-show-archive-thread)
692 (define-key map "N" 'notmuch-show-next-message)
693 (define-key map "P" 'notmuch-show-previous-message)
694 (define-key map "n" 'notmuch-show-next-open-message)
695 (define-key map "p" 'notmuch-show-previous-open-message)
696 (define-key map (kbd "DEL") 'notmuch-show-rewind)
697 (define-key map " " 'notmuch-show-advance-and-archive)
698 (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
699 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
701 "Keymap for \"notmuch show\" buffers.")
702 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
704 (defun notmuch-show-mode ()
705 "Major mode for viewing a thread with notmuch.
707 This buffer contains the results of the \"notmuch show\" command
708 for displaying a single thread of email from your email archives.
710 By default, various components of email messages, (citations,
711 signatures, already-read messages), are hidden. You can make
712 these parts visible by clicking with the mouse button or by
713 pressing RET after positioning the cursor on a hidden part, (for
714 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
716 Reading the thread sequentially is well-supported by pressing
717 \\[notmuch-show-advance-and-archive]. This will scroll the current message (if necessary), advance
718 to the next message, or advance to the next thread (if already on
719 the last message of a thread).
721 Other commands are available to read or manipulate the thread
722 more selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages
723 without removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread
724 without scrolling through with \\[notmuch-show-advance-and-archive]).
726 You can add or remove arbitary tags from the current message with
727 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
729 All currently available key bindings:
731 \\{notmuch-show-mode-map}"
733 (kill-all-local-variables)
734 (use-local-map notmuch-show-mode-map)
735 (setq major-mode 'notmuch-show-mode
736 mode-name "notmuch-show")
737 (setq buffer-read-only t))
739 (defun notmuch-show-move-to-message-top ()
740 (goto-char (notmuch-show-message-top)))
742 (defun notmuch-show-move-to-message-bottom ()
743 (goto-char (notmuch-show-message-bottom)))
745 (defun notmuch-show-message-adjust ()
748 ;; Movement related functions.
750 ;; There's some strangeness here where a text property applied to a
751 ;; region a->b is not found when point is at b. We walk backwards
752 ;; until finding the property.
753 (defun notmuch-show-message-extent ()
756 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
760 (defun notmuch-show-message-top ()
761 (car (notmuch-show-message-extent)))
763 (defun notmuch-show-message-bottom ()
764 (cdr (notmuch-show-message-extent)))
766 (defun notmuch-show-goto-message-next ()
767 (let ((start (point)))
768 (notmuch-show-move-to-message-bottom)
774 (defun notmuch-show-goto-message-previous ()
775 (notmuch-show-move-to-message-top)
779 (notmuch-show-move-to-message-top)
782 (defun notmuch-show-move-past-invisible-forward ()
783 (while (point-invisible-p)
786 (defun notmuch-show-move-past-invisible-backward ()
787 (while (point-invisible-p)
790 ;; Functions relating to the visibility of messages and their
793 (defun notmuch-show-element-visible (props visible-p spec-property)
794 (let ((spec (plist-get props spec-property)))
796 (remove-from-invisibility-spec spec)
797 (add-to-invisibility-spec spec))))
799 (defun notmuch-show-message-visible (props visible-p)
801 ;; When making the message visible, the headers may or not be
802 ;; visible. So we check that property separately.
803 (let ((headers-visible (plist-get props :headers-visible)))
804 (notmuch-show-element-visible props headers-visible :headers-invis-spec)
805 (notmuch-show-element-visible props t :message-invis-spec))
806 (notmuch-show-element-visible props nil :headers-invis-spec)
807 (notmuch-show-element-visible props nil :message-invis-spec))
809 (notmuch-show-set-prop :message-visible visible-p props))
811 (defun notmuch-show-headers-visible (props visible-p)
812 (if (plist-get props :message-visible)
813 (notmuch-show-element-visible props visible-p :headers-invis-spec))
814 (notmuch-show-set-prop :headers-visible visible-p props))
816 ;; Functions for setting and getting attributes of the current
819 (defun notmuch-show-set-message-properties (props)
821 (notmuch-show-move-to-message-top)
822 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
824 (defun notmuch-show-get-message-properties ()
826 (notmuch-show-move-to-message-top)
827 (get-text-property (point) :notmuch-message-properties)))
829 (defun notmuch-show-set-prop (prop val &optional props)
830 (let ((inhibit-read-only t)
832 (notmuch-show-get-message-properties))))
833 (plist-put props prop val)
834 (notmuch-show-set-message-properties props)))
836 (defun notmuch-show-get-prop (prop &optional props)
837 (let ((props (or props
838 (notmuch-show-get-message-properties))))
839 (plist-get props prop)))
841 (defun notmuch-show-get-message-id ()
842 "Return the message id of the current message."
843 (concat "id:\"" (notmuch-show-get-prop :id) "\""))
845 ;; dme: Would it make sense to use a macro for many of these?
847 (defun notmuch-show-get-filename ()
848 "Return the filename of the current message."
849 (notmuch-show-get-prop :filename))
851 (defun notmuch-show-get-header (header)
852 "Return the named header of the current message, if any."
853 (plist-get (notmuch-show-get-prop :headers) header))
855 (defun notmuch-show-get-cc ()
856 (notmuch-show-get-header :Cc))
858 (defun notmuch-show-get-date ()
859 (notmuch-show-get-header :Date))
861 (defun notmuch-show-get-from ()
862 (notmuch-show-get-header :From))
864 (defun notmuch-show-get-subject ()
865 (notmuch-show-get-header :Subject))
867 (defun notmuch-show-get-to ()
868 (notmuch-show-get-header :To))
870 (defun notmuch-show-set-tags (tags)
871 "Set the tags of the current message."
872 (notmuch-show-set-prop :tags tags)
873 (notmuch-show-update-tags tags))
875 (defun notmuch-show-get-tags ()
876 "Return the tags of the current message."
877 (notmuch-show-get-prop :tags))
879 (defun notmuch-show-message-visible-p ()
880 "Is the current message visible?"
881 (notmuch-show-get-prop :message-visible))
883 (defun notmuch-show-headers-visible-p ()
884 "Are the headers of the current message visible?"
885 (notmuch-show-get-prop :headers-visible))
887 (defun notmuch-show-mark-read ()
888 "Mark the current message as read."
889 (notmuch-show-remove-tag "unread"))
891 ;; Functions for getting attributes of several messages in the current
894 (defun notmuch-show-get-message-ids-for-open-messages ()
895 "Return a list of all message IDs for open messages in the current thread."
897 (let (message-ids done)
898 (goto-char (point-min))
900 (if (notmuch-show-message-visible-p)
901 (setq message-ids (append message-ids (list (notmuch-show-get-message-id)))))
902 (setq done (not (notmuch-show-goto-message-next)))
907 ;; Commands typically bound to keys.
909 (defun notmuch-show-advance-and-archive ()
910 "Advance through thread and archive.
912 This command is intended to be one of the simplest ways to
913 process a thread of email. It does the following:
915 If the current message in the thread is not yet fully visible,
916 scroll by a near screenful to read more of the message.
918 Otherwise, (the end of the current message is already within the
919 current window), advance to the next open message.
921 Finally, if there is no further message to advance to, and this
922 last message is already read, then archive the entire current
923 thread, (remove the \"inbox\" tag from each message). Also kill
924 this buffer, and display the next thread from the search from
925 which this thread was originally shown."
927 (let ((end-of-this-message (notmuch-show-message-bottom)))
929 ;; Ideally we would test `end-of-this-message' against the result
930 ;; of `window-end', but that doesn't account for the fact that
931 ;; the end of the message might be hidden, so we have to actually
932 ;; go to the end, walk back over invisible text and then see if
935 (goto-char (- end-of-this-message 1))
936 (notmuch-show-move-past-invisible-backward)
937 (> (point) (window-end)))
938 ;; The bottom of this message is not visible - scroll.
941 ((not (= end-of-this-message (point-max)))
942 ;; This is not the last message - move to the next visible one.
943 (notmuch-show-next-open-message))
946 ;; This is the last message - archive the thread.
947 (notmuch-show-archive-thread)))))
949 (defun notmuch-show-rewind ()
950 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
952 Specifically, if the beginning of the previous email is fewer
953 than `window-height' lines from the current point, move to it
954 just like `notmuch-show-previous-message'.
956 Otherwise, just scroll down a screenful of the current message.
958 This command does not modify any message tags, (it does not undo
959 any effects from previous calls to
960 `notmuch-show-advance-and-archive'."
962 (let ((start-of-message (notmuch-show-message-top))
963 (start-of-window (window-start)))
965 ;; Either this message is properly aligned with the start of the
966 ;; window or the start of this message is not visible on the
968 ((or (= start-of-message start-of-window)
969 (< start-of-message start-of-window))
971 ;; If a small number of lines from the previous message are
972 ;; visible, realign so that the top of the current message is at
973 ;; the top of the screen.
974 (if (<= (count-screen-lines (window-start) start-of-message)
975 next-screen-context-lines)
977 (goto-char (notmuch-show-message-top))
978 (notmuch-show-message-adjust)))
979 ;; Move to the top left of the window.
980 (goto-char (window-start)))
982 ;; Move to the previous message.
983 (notmuch-show-previous-message)))))
985 (defun notmuch-show-reply ()
986 "Reply to the current message."
988 (notmuch-mua-reply (notmuch-show-get-message-id)))
990 (defun notmuch-show-forward-message ()
991 "Forward the current message."
993 (with-current-notmuch-show-message
994 (notmuch-mua-forward-message)))
996 (defun notmuch-show-next-message ()
997 "Show the next message."
999 (if (notmuch-show-goto-message-next)
1001 (notmuch-show-mark-read)
1002 (notmuch-show-message-adjust))
1003 (goto-char (point-max))))
1005 (defun notmuch-show-previous-message ()
1006 "Show the previous message."
1008 (notmuch-show-goto-message-previous)
1009 (notmuch-show-mark-read)
1010 (notmuch-show-message-adjust))
1012 (defun notmuch-show-next-open-message ()
1013 "Show the next message."
1016 (while (and (setq r (notmuch-show-goto-message-next))
1017 (not (notmuch-show-message-visible-p))))
1020 (notmuch-show-mark-read)
1021 (notmuch-show-message-adjust))
1022 (goto-char (point-max)))))
1024 (defun notmuch-show-previous-open-message ()
1025 "Show the previous message."
1027 (while (and (notmuch-show-goto-message-previous)
1028 (not (notmuch-show-message-visible-p))))
1029 (notmuch-show-mark-read)
1030 (notmuch-show-message-adjust))
1032 (defun notmuch-show-view-raw-message ()
1033 "View the file holding the current message."
1035 (let* ((id (notmuch-show-get-message-id))
1036 (buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
1037 (call-process notmuch-command nil buf nil "show" "--format=raw" id)
1038 (switch-to-buffer buf)
1039 (goto-char (point-min))
1040 (set-buffer-modified-p nil)
1041 (view-buffer buf 'kill-buffer-if-not-modified)))
1043 (defun notmuch-show-pipe-message (entire-thread command)
1044 "Pipe the contents of the current message (or thread) to the given command.
1046 The given command will be executed with the raw contents of the
1047 current email message as stdin. Anything printed by the command
1048 to stdout or stderr will appear in the *notmuch-pipe* buffer.
1050 When invoked with a prefix argument, the command will receive all
1051 open messages in the current thread (formatted as an mbox) rather
1052 than only the current message."
1053 (interactive "P\nsPipe message to command: ")
1054 (let (shell-command)
1057 (concat notmuch-command " show --format=mbox "
1058 (shell-quote-argument
1059 (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
1062 (concat notmuch-command " show --format=raw "
1063 (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
1064 (let ((buf (get-buffer-create (concat "*notmuch-pipe*"))))
1065 (with-current-buffer buf
1066 (setq buffer-read-only nil)
1068 (let ((exit-code (call-process-shell-command shell-command nil buf)))
1069 (goto-char (point-max))
1070 (set-buffer-modified-p nil)
1071 (setq buffer-read-only t)
1072 (unless (zerop exit-code)
1073 (switch-to-buffer-other-window buf)
1074 (message (format "Command '%s' exited abnormally with code %d"
1075 shell-command exit-code))))))))
1077 (defun notmuch-show-add-tags-worker (current-tags add-tags)
1078 "Add to `current-tags' with any tags from `add-tags' not
1079 currently present and return the result."
1080 (let ((result-tags (copy-sequence current-tags)))
1081 (mapc (lambda (add-tag)
1082 (unless (member add-tag current-tags)
1083 (setq result-tags (push add-tag result-tags))))
1085 (sort result-tags 'string<)))
1087 (defun notmuch-show-del-tags-worker (current-tags del-tags)
1088 "Remove any tags in `del-tags' from `current-tags' and return
1090 (let ((result-tags (copy-sequence current-tags)))
1091 (mapc (lambda (del-tag)
1092 (setq result-tags (delete del-tag result-tags)))
1096 (defun notmuch-show-add-tag (&rest toadd)
1097 "Add a tag to the current message."
1099 (list (notmuch-select-tag-with-completion "Tag to add: ")))
1101 (let* ((current-tags (notmuch-show-get-tags))
1102 (new-tags (notmuch-show-add-tags-worker current-tags toadd)))
1104 (unless (equal current-tags new-tags)
1105 (apply 'notmuch-call-notmuch-process
1107 (mapcar (lambda (s) (concat "+" s)) toadd))
1108 (cons (notmuch-show-get-message-id) nil)))
1109 (notmuch-show-set-tags new-tags))))
1111 (defun notmuch-show-remove-tag (&rest toremove)
1112 "Remove a tag from the current message."
1114 (list (notmuch-select-tag-with-completion
1115 "Tag to remove: " (notmuch-show-get-message-id))))
1117 (let* ((current-tags (notmuch-show-get-tags))
1118 (new-tags (notmuch-show-del-tags-worker current-tags toremove)))
1120 (unless (equal current-tags new-tags)
1121 (apply 'notmuch-call-notmuch-process
1123 (mapcar (lambda (s) (concat "-" s)) toremove))
1124 (cons (notmuch-show-get-message-id) nil)))
1125 (notmuch-show-set-tags new-tags))))
1127 (defun notmuch-show-toggle-headers ()
1128 "Toggle the visibility of the current message headers."
1130 (let ((props (notmuch-show-get-message-properties)))
1131 (notmuch-show-headers-visible
1133 (not (plist-get props :headers-visible))))
1134 (force-window-update))
1136 (defun notmuch-show-toggle-message ()
1137 "Toggle the visibility of the current message."
1139 (let ((props (notmuch-show-get-message-properties)))
1140 (notmuch-show-message-visible
1142 (not (plist-get props :message-visible))))
1143 (force-window-update))
1145 (defun notmuch-show-open-or-close-all ()
1146 "Set the visibility all of the messages in the current thread.
1147 By default make all of the messages visible. With a prefix
1148 argument, hide all of the messages."
1151 (goto-char (point-min))
1152 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1153 (not current-prefix-arg))
1154 until (not (notmuch-show-goto-message-next))))
1155 (force-window-update))
1157 (defun notmuch-show-next-button ()
1158 "Advance point to the next button in the buffer."
1162 (defun notmuch-show-previous-button ()
1163 "Move point back to the previous button in the buffer."
1165 (backward-button 1))
1167 (defun notmuch-show-archive-thread-internal (show-next)
1168 ;; Remove the tag from the current set of messages.
1169 (goto-char (point-min))
1170 (loop do (notmuch-show-remove-tag "inbox")
1171 until (not (notmuch-show-goto-message-next)))
1172 ;; Move to the next item in the search results, if any.
1173 (let ((parent-buffer notmuch-show-parent-buffer))
1174 (notmuch-kill-this-buffer)
1177 (switch-to-buffer parent-buffer)
1180 (notmuch-search-show-thread))))))
1182 (defun notmuch-show-archive-thread ()
1183 "Archive each message in thread, then show next thread from search.
1185 Archive each message currently shown by removing the \"inbox\"
1186 tag from each. Then kill this buffer and show the next thread
1187 from the search from which this thread was originally shown.
1189 Note: This command is safe from any race condition of new messages
1190 being delivered to the same thread. It does not archive the
1191 entire thread, but only the messages shown in the current
1194 (notmuch-show-archive-thread-internal t))
1196 (defun notmuch-show-archive-thread-then-exit ()
1197 "Archive each message in thread, then exit back to search results."
1199 (notmuch-show-archive-thread-internal nil))
1201 (defun notmuch-show-stash-cc ()
1202 "Copy CC field of current message to kill-ring."
1204 (notmuch-common-do-stash (notmuch-show-get-cc)))
1206 (defun notmuch-show-stash-date ()
1207 "Copy date of current message to kill-ring."
1209 (notmuch-common-do-stash (notmuch-show-get-date)))
1211 (defun notmuch-show-stash-filename ()
1212 "Copy filename of current message to kill-ring."
1214 (notmuch-common-do-stash (notmuch-show-get-filename)))
1216 (defun notmuch-show-stash-from ()
1217 "Copy From address of current message to kill-ring."
1219 (notmuch-common-do-stash (notmuch-show-get-from)))
1221 (defun notmuch-show-stash-message-id ()
1222 "Copy message ID of current message to kill-ring."
1224 (notmuch-common-do-stash (notmuch-show-get-message-id)))
1226 (defun notmuch-show-stash-subject ()
1227 "Copy Subject field of current message to kill-ring."
1229 (notmuch-common-do-stash (notmuch-show-get-subject)))
1231 (defun notmuch-show-stash-tags ()
1232 "Copy tags of current message to kill-ring as a comma separated list."
1234 (notmuch-common-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1236 (defun notmuch-show-stash-to ()
1237 "Copy To address of current message to kill-ring."
1239 (notmuch-common-do-stash (notmuch-show-get-to)))
1241 ;; Commands typically bound to buttons.
1243 (defun notmuch-show-part-button-action (button)
1244 (let ((nth (button-get button :notmuch-part)))
1246 (notmuch-show-save-part (notmuch-show-get-message-id) nth
1247 (button-get button :notmuch-filename))
1248 (message "Not a valid part (is it a fake part?)."))))
1252 (provide 'notmuch-show)