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 ;; Mostly useful for debugging.
87 (defcustom notmuch-show-all-multipart/alternative-parts nil
88 "Should all parts of multipart/alternative parts be shown?"
92 (defcustom notmuch-show-indent-multipart nil
93 "Should the sub-parts of a multipart/* part be indented?"
94 ;; dme: Not sure which is a good default.
98 (defmacro with-current-notmuch-show-message (&rest body)
99 "Evaluate body with current buffer set to the text of current message"
101 (let ((id (notmuch-show-get-message-id)))
102 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
103 (with-current-buffer buf
104 (call-process notmuch-command nil t nil "show" "--format=raw" id)
106 (kill-buffer buf)))))
108 (defun notmuch-show-view-all-mime-parts ()
109 "Use external viewers to view all attachments from the current message."
111 (with-current-notmuch-show-message
112 ; We ovverride the mm-inline-media-tests to indicate which message
113 ; parts are already sufficiently handled by the original
114 ; presentation of the message in notmuch-show mode. These parts
115 ; will be inserted directly into the temporary buffer of
116 ; with-current-notmuch-show-message and silently discarded.
118 ; Any MIME part not explicitly mentioned here will be handled by an
119 ; external viewer as configured in the various mailcap files.
120 (let ((mm-inline-media-tests '(
121 ("text/.*" ignore identity)
122 ("application/pgp-signature" ignore identity)
123 ("multipart/alternative" ignore identity)
124 ("multipart/mixed" ignore identity)
125 ("multipart/related" ignore identity)
127 (mm-display-parts (mm-dissect-buffer)))))
129 (defun notmuch-foreach-mime-part (function mm-handle)
130 (cond ((stringp (car mm-handle))
131 (dolist (part (cdr mm-handle))
132 (notmuch-foreach-mime-part function part)))
133 ((bufferp (car mm-handle))
134 (funcall function mm-handle))
135 (t (dolist (part mm-handle)
136 (notmuch-foreach-mime-part function part)))))
138 (defun notmuch-count-attachments (mm-handle)
140 (notmuch-foreach-mime-part
142 (let ((disposition (mm-handle-disposition p)))
143 (and (listp disposition)
144 (or (equal (car disposition) "attachment")
145 (and (equal (car disposition) "inline")
146 (assq 'filename disposition)))
151 (defun notmuch-save-attachments (mm-handle &optional queryp)
152 (notmuch-foreach-mime-part
154 (let ((disposition (mm-handle-disposition p)))
155 (and (listp disposition)
156 (or (equal (car disposition) "attachment")
157 (and (equal (car disposition) "inline")
158 (assq 'filename disposition)))
161 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
165 (defun notmuch-show-save-attachments ()
166 "Save all attachments from the current message."
168 (with-current-notmuch-show-message
169 (let ((mm-handle (mm-dissect-buffer)))
170 (notmuch-save-attachments
171 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
174 (defun notmuch-show-fontify-header ()
176 ((looking-at "[Tt]o:")
178 ((looking-at "[Bb]?[Cc][Cc]:")
180 ((looking-at "[Ss]ubject:")
181 'message-header-subject)
182 ((looking-at "[Ff]rom:")
183 'message-header-from)
185 'message-header-other))))
187 (overlay-put (make-overlay (point) (re-search-forward ":"))
188 'face 'message-header-name)
189 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
192 (defun notmuch-show-colour-headers ()
193 "Apply some colouring to the current headers."
194 (goto-char (point-min))
195 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
196 (notmuch-show-fontify-header)
199 (defun notmuch-show-spaces-n (n)
200 "Return a string comprised of `n' spaces."
203 (defun notmuch-show-update-tags (tags)
204 "Update the displayed tags of the current message."
206 (goto-char (notmuch-show-message-top))
207 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
208 (let ((inhibit-read-only t))
209 (replace-match (concat "("
210 (propertize (mapconcat 'identity tags " ")
211 'face 'notmuch-tag-face)
214 (defun notmuch-show-insert-headerline (headers date tags depth)
215 "Insert a notmuch style headerline based on HEADERS for a
216 message at DEPTH in the current thread."
217 (let ((start (point)))
218 (insert (notmuch-show-spaces-n depth)
219 (plist-get headers :From)
223 (propertize (mapconcat 'identity tags " ")
224 'face 'notmuch-tag-face)
226 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
228 (defun notmuch-show-insert-header (header header-value)
229 "Insert a single header."
230 (insert header ": " header-value "\n"))
232 (defun notmuch-show-insert-headers (headers)
233 "Insert the headers of the current message."
234 (let ((start (point)))
235 (mapc '(lambda (header)
236 (let* ((header-symbol (intern (concat ":" header)))
237 (header-value (plist-get headers header-symbol)))
238 (if (and header-value
239 (not (string-equal "" header-value)))
240 (notmuch-show-insert-header header header-value))))
241 notmuch-message-headers)
244 (narrow-to-region start (point-max))
245 (run-hooks 'notmuch-show-markup-headers-hook)))))
247 (define-button-type 'notmuch-show-part-button-type
248 'action 'notmuch-show-part-button-action
252 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name comment)
255 (if name (concat name ": ") "")
257 (if (not (string-equal declared-type content-type))
258 (concat " (as " content-type ")")
262 :type 'notmuch-show-part-button-type
264 :notmuch-filename name))
266 ;; Functions handling particular MIME parts.
268 (defun notmuch-show-save-part (message-id nth &optional filename)
270 ;; Always acquires the part via `notmuch part', even if it is
271 ;; available in the JSON output.
272 (insert (notmuch-show-get-bodypart-internal message-id nth))
273 (let ((file (read-file-name
274 "Filename to save as: "
275 (or mailcap-download-directory "~/")
278 (require-final-newline nil)
279 (coding-system-for-write 'no-conversion))
280 (write-region (point-min) (point-max) file))))
282 (defun notmuch-show-mm-display-part-inline (msg part content-type content)
283 "Use the mm-decode/mm-view functions to display a part in the
284 current buffer, if possible."
285 (let ((display-buffer (current-buffer)))
288 (let ((handle (mm-make-handle (current-buffer) (list content-type))))
289 (set-buffer display-buffer)
290 (if (and (mm-inlinable-p handle)
291 (mm-inlined-p handle))
293 (mm-display-part handle)
297 (defvar notmuch-show-multipart/alternative-discouraged
301 ;; multipart/related usually contain a text/html part and some associated graphics.
305 (defun notmuch-show-multipart/*-to-list (part)
306 (mapcar '(lambda (inner-part) (plist-get inner-part :content-type))
307 (plist-get part :content)))
309 (defun notmuch-show-multipart/alternative-choose (types)
310 ;; Based on `mm-preferred-alternative-precedence'.
312 (dolist (pref (reverse notmuch-show-multipart/alternative-discouraged))
313 (dolist (elem (copy-sequence seq))
314 (when (string-match pref elem)
315 (setq seq (nconc (delete elem seq) (list elem))))))
318 (defun notmuch-show-insert-part-multipart/alternative (msg part content-type nth depth declared-type)
319 (notmuch-show-insert-part-header nth declared-type content-type nil)
320 (let ((chosen-type (car (notmuch-show-multipart/alternative-choose (notmuch-show-multipart/*-to-list part))))
321 (inner-parts (plist-get part :content))
323 ;; This inserts all parts of the chosen type rather than just one,
324 ;; but it's not clear that this is the wrong thing to do - which
325 ;; should be chosen if there are more than one that match?
326 (mapc (lambda (inner-part)
327 (let ((inner-type (plist-get inner-part :content-type)))
328 (if (or notmuch-show-all-multipart/alternative-parts
329 (string= chosen-type inner-type))
330 (notmuch-show-insert-bodypart msg inner-part depth)
331 (notmuch-show-insert-part-header (plist-get inner-part :id) inner-type inner-type nil " (not shown)"))))
334 (when notmuch-show-indent-multipart
335 (indent-rigidly start (point) 1)))
338 (defun notmuch-show-insert-part-multipart/* (msg part content-type nth depth declared-type)
339 (notmuch-show-insert-part-header nth declared-type content-type nil)
340 (let ((inner-parts (plist-get part :content))
342 ;; Show all of the parts.
343 (mapc (lambda (inner-part)
344 (notmuch-show-insert-bodypart msg inner-part depth))
347 (when notmuch-show-indent-multipart
348 (indent-rigidly start (point) 1)))
351 (defun notmuch-show-insert-part-message/rfc822 (msg part content-type nth depth declared-type)
352 (let* ((message-part (plist-get part :content))
353 (inner-parts (plist-get message-part :content)))
354 (notmuch-show-insert-part-header nth declared-type content-type nil)
355 ;; Override `notmuch-message-headers' to force `From' to be
357 (let ((notmuch-message-headers '("From" "Subject" "To" "Cc" "Date")))
358 (notmuch-show-insert-headers (plist-get part :headers)))
359 ;; Blank line after headers to be compatible with the normal
363 ;; Show all of the parts.
364 (mapc (lambda (inner-part)
365 (notmuch-show-insert-bodypart msg inner-part depth))
369 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
370 (let ((start (point)))
371 ;; If this text/plain part is not the first part in the message,
372 ;; insert a header to make this clear.
374 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
375 (insert (notmuch-show-get-bodypart-content msg part nth))
378 (narrow-to-region start (point-max))
379 (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
382 (defun notmuch-show-insert-part-text/x-vcalendar (msg part content-type nth depth declared-type)
383 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename))
384 (insert (with-temp-buffer
385 (insert (notmuch-show-get-bodypart-content msg part nth))
386 (goto-char (point-min))
387 (let ((file (make-temp-file "notmuch-ical"))
389 (icalendar--convert-ical-to-diary
390 (icalendar--read-element nil nil)
392 (set-buffer (get-file-buffer file))
393 (setq result (buffer-substring (point-min) (point-max)))
394 (set-buffer-modified-p nil)
395 (kill-buffer (current-buffer))
400 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
401 ;; If we can deduce a MIME type from the filename of the attachment,
402 ;; do so and pass it on to the handler for that type.
403 (if (plist-get part :filename)
404 (let ((extension (file-name-extension (plist-get part :filename)))
408 (mailcap-parse-mimetypes)
409 (setq mime-type (mailcap-extension-to-mime extension))
411 (not (string-equal mime-type "application/octet-stream")))
412 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
416 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
417 ;; This handler _must_ succeed - it is the handler of last resort.
418 (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
419 (let ((content (notmuch-show-get-bodypart-content msg part nth)))
421 (notmuch-show-mm-display-part-inline msg part content-type content)))
424 ;; Functions for determining how to handle MIME parts.
426 (defun notmuch-show-split-content-type (content-type)
427 (split-string content-type "/"))
429 (defun notmuch-show-handlers-for (content-type)
430 "Return a list of content handlers for a part of type CONTENT-TYPE."
435 ;; Reverse order of prefrence.
436 (list (intern (concat "notmuch-show-insert-part-*/*"))
438 "notmuch-show-insert-part-"
439 (car (notmuch-show-split-content-type content-type))
441 (intern (concat "notmuch-show-insert-part-" content-type))))
444 ;; Helper for parts which are generally not included in the default
447 (defun notmuch-show-get-bodypart-internal (message-id part-number)
449 (let ((coding-system-for-read 'no-conversion))
450 (call-process notmuch-command nil t nil
451 "part" (format "--part=%s" part-number) message-id)
454 (defun notmuch-show-get-bodypart-content (msg part nth)
455 (or (plist-get part :content)
456 (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
460 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
461 (let ((handlers (notmuch-show-handlers-for content-type)))
462 ;; Run the content handlers until one of them returns a non-nil
465 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
466 (setq handlers (cdr handlers))))
469 (defun notmuch-show-insert-bodypart (msg part depth)
470 "Insert the body part PART at depth DEPTH in the current thread."
471 (let ((content-type (downcase (plist-get part :content-type)))
472 (nth (plist-get part :id)))
473 (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
474 ;; Some of the body part handlers leave point somewhere up in the
475 ;; part, so we make sure that we're down at the end.
476 (goto-char (point-max))
477 ;; Ensure that the part ends with a carriage return.
481 (defun notmuch-show-insert-body (msg body depth)
482 "Insert the body BODY at depth DEPTH in the current thread."
483 (mapc '(lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
485 (defun notmuch-show-make-symbol (type)
486 (make-symbol (concat "notmuch-show-" type)))
488 (defun notmuch-show-strip-re (string)
489 (replace-regexp-in-string "\\([Rr]e: *\\)+" "" string))
491 (defvar notmuch-show-previous-subject "")
492 (make-variable-buffer-local 'notmuch-show-previous-subject)
494 (defun notmuch-show-insert-msg (msg depth)
495 "Insert the message MSG at depth DEPTH in the current thread."
496 (let* ((headers (plist-get msg :headers))
497 ;; Indentation causes the buffer offset of the start/end
498 ;; points to move, so we must use markers.
499 message-start message-end
500 content-start content-end
501 headers-start headers-end
503 (headers-invis-spec (notmuch-show-make-symbol "header"))
504 (message-invis-spec (notmuch-show-make-symbol "message"))
505 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
507 ;; Set `buffer-invisibility-spec' to `nil' (a list), otherwise
508 ;; removing items from `buffer-invisibility-spec' (which is what
509 ;; `notmuch-show-headers-visible' and
510 ;; `notmuch-show-message-visible' do) is a no-op and has no
511 ;; effect. This caused threads with only matching messages to have
512 ;; those messages hidden initially because
513 ;; `buffer-invisibility-spec' stayed `t'.
515 ;; This needs to be set here (rather than just above the call to
516 ;; `notmuch-show-headers-visible') because some of the part
517 ;; rendering or body washing functions
518 ;; (e.g. `notmuch-wash-text/plain-citations') manipulate
519 ;; `buffer-invisibility-spec').
520 (when (eq buffer-invisibility-spec t)
521 (setq buffer-invisibility-spec nil))
523 (setq message-start (point-marker))
525 (notmuch-show-insert-headerline headers
526 (or (if notmuch-show-relative-dates
527 (plist-get msg :date_relative)
529 (plist-get headers :Date))
530 (plist-get msg :tags) depth)
532 (setq content-start (point-marker))
534 ;; Set `headers-start' to point after the 'Subject:' header to be
535 ;; compatible with the existing implementation. This just sets it
536 ;; to after the first header.
537 (notmuch-show-insert-headers headers)
538 ;; Headers should include a blank line (backwards compatibility).
541 (goto-char content-start)
542 ;; If the subject of this message is the same as that of the
543 ;; previous message, don't display it when this message is
545 (when (not (string= notmuch-show-previous-subject
548 (setq headers-start (point-marker)))
549 (setq headers-end (point-marker))
551 (setq notmuch-show-previous-subject bare-subject)
553 (setq body-start (point-marker))
554 (notmuch-show-insert-body msg (plist-get msg :body) depth)
555 ;; Ensure that the body ends with a newline.
558 (setq body-end (point-marker))
559 (setq content-end (point-marker))
561 ;; Indent according to the depth in the thread.
562 (indent-rigidly content-start content-end depth)
564 (setq message-end (point-max-marker))
566 ;; Save the extents of this message over the whole text of the
568 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
570 (plist-put msg :headers-invis-spec headers-invis-spec)
571 (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
573 (plist-put msg :message-invis-spec message-invis-spec)
574 (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
576 ;; Save the properties for this message. Currently this saves the
577 ;; entire message (augmented it with other stuff), which seems
578 ;; like overkill. We might save a reduced subset (for example, not
580 (notmuch-show-set-message-properties msg)
582 ;; Set header visibility.
583 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
585 ;; Message visibility depends on whether it matched the search
587 (notmuch-show-message-visible msg (plist-get msg :match))))
589 (defun notmuch-show-insert-tree (tree depth)
590 "Insert the message tree TREE at depth DEPTH in the current thread."
591 (let ((msg (car tree))
592 (replies (cadr tree)))
593 (notmuch-show-insert-msg msg depth)
594 (notmuch-show-insert-thread replies (1+ depth))))
596 (defun notmuch-show-insert-thread (thread depth)
597 "Insert the thread THREAD at depth DEPTH in the current forest."
598 (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
600 (defun notmuch-show-insert-forest (forest)
601 "Insert the forest of threads FOREST."
602 (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
604 (defvar notmuch-show-parent-buffer nil)
607 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
608 "Run \"notmuch show\" with the given thread ID and display results.
610 The optional PARENT-BUFFER is the notmuch-search buffer from
611 which this notmuch-show command was executed, (so that the
612 next thread from that buffer can be show when done with this
615 The optional QUERY-CONTEXT is a notmuch search term. Only
616 messages from the thread matching this search term are shown if
619 The optional BUFFER-NAME provides the neame of the buffer in
620 which the message thread is shown. If it is nil (which occurs
621 when the command is called interactively) the argument to the
623 (interactive "sNotmuch show: ")
624 (let ((buffer (get-buffer-create (generate-new-buffer-name
626 (concat "*notmuch-" thread-id "*")))))
627 (inhibit-read-only t))
628 (switch-to-buffer buffer)
630 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
632 (goto-char (point-min))
634 (let* ((basic-args (list thread-id))
635 (args (if query-context
636 (append (list "\'") basic-args (list "and (" query-context ")\'"))
637 (append (list "\'") basic-args (list "\'")))))
638 (notmuch-show-insert-forest (notmuch-query-get-threads args))
639 ;; If the query context reduced the results to nothing, run
641 (when (and (eq (buffer-size) 0)
643 (notmuch-show-insert-forest
644 (notmuch-query-get-threads basic-args))))
646 ;; Enable buttonisation of URLs and email addresses in the
648 (goto-address-mode t)
649 ;; Act on visual lines rather than logical lines.
652 (run-hooks 'notmuch-show-hook))
654 ;; Move straight to the first open message
655 (if (not (notmuch-show-message-visible-p))
656 (notmuch-show-next-open-message))
658 ;; Set the header line to the subject of the first open message.
659 (setq header-line-format (notmuch-show-strip-re (notmuch-show-get-subject)))
661 (notmuch-show-mark-read)))
663 (defvar notmuch-show-stash-map
664 (let ((map (make-sparse-keymap)))
665 (define-key map "c" 'notmuch-show-stash-cc)
666 (define-key map "d" 'notmuch-show-stash-date)
667 (define-key map "F" 'notmuch-show-stash-filename)
668 (define-key map "f" 'notmuch-show-stash-from)
669 (define-key map "i" 'notmuch-show-stash-message-id)
670 (define-key map "s" 'notmuch-show-stash-subject)
671 (define-key map "T" 'notmuch-show-stash-tags)
672 (define-key map "t" 'notmuch-show-stash-to)
674 "Submap for stash commands")
675 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
677 (defvar notmuch-show-mode-map
678 (let ((map (make-sparse-keymap)))
679 (define-key map "?" 'notmuch-help)
680 (define-key map "q" 'notmuch-kill-this-buffer)
681 (define-key map (kbd "<C-tab>") 'widget-backward)
682 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
683 (define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
684 (define-key map (kbd "TAB") 'notmuch-show-next-button)
685 (define-key map "s" 'notmuch-search)
686 (define-key map "m" 'notmuch-mua-mail)
687 (define-key map "f" 'notmuch-show-forward-message)
688 (define-key map "r" 'notmuch-show-reply)
689 (define-key map "|" 'notmuch-show-pipe-message)
690 (define-key map "w" 'notmuch-show-save-attachments)
691 (define-key map "V" 'notmuch-show-view-raw-message)
692 (define-key map "v" 'notmuch-show-view-all-mime-parts)
693 (define-key map "c" 'notmuch-show-stash-map)
694 (define-key map "h" 'notmuch-show-toggle-headers)
695 (define-key map "-" 'notmuch-show-remove-tag)
696 (define-key map "+" 'notmuch-show-add-tag)
697 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
698 (define-key map "a" 'notmuch-show-archive-thread)
699 (define-key map "N" 'notmuch-show-next-message)
700 (define-key map "P" 'notmuch-show-previous-message)
701 (define-key map "n" 'notmuch-show-next-open-message)
702 (define-key map "p" 'notmuch-show-previous-open-message)
703 (define-key map (kbd "DEL") 'notmuch-show-rewind)
704 (define-key map " " 'notmuch-show-advance-and-archive)
705 (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
706 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
708 "Keymap for \"notmuch show\" buffers.")
709 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
711 (defun notmuch-show-mode ()
712 "Major mode for viewing a thread with notmuch.
714 This buffer contains the results of the \"notmuch show\" command
715 for displaying a single thread of email from your email archives.
717 By default, various components of email messages, (citations,
718 signatures, already-read messages), are hidden. You can make
719 these parts visible by clicking with the mouse button or by
720 pressing RET after positioning the cursor on a hidden part, (for
721 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
723 Reading the thread sequentially is well-supported by pressing
724 \\[notmuch-show-advance-and-archive]. This will scroll the current message (if necessary), advance
725 to the next message, or advance to the next thread (if already on
726 the last message of a thread).
728 Other commands are available to read or manipulate the thread
729 more selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages
730 without removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread
731 without scrolling through with \\[notmuch-show-advance-and-archive]).
733 You can add or remove arbitary tags from the current message with
734 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
736 All currently available key bindings:
738 \\{notmuch-show-mode-map}"
740 (kill-all-local-variables)
741 (use-local-map notmuch-show-mode-map)
742 (setq major-mode 'notmuch-show-mode
743 mode-name "notmuch-show")
744 (setq buffer-read-only t))
746 (defun notmuch-show-move-to-message-top ()
747 (goto-char (notmuch-show-message-top)))
749 (defun notmuch-show-move-to-message-bottom ()
750 (goto-char (notmuch-show-message-bottom)))
752 (defun notmuch-show-message-adjust ()
755 ;; Movement related functions.
757 ;; There's some strangeness here where a text property applied to a
758 ;; region a->b is not found when point is at b. We walk backwards
759 ;; until finding the property.
760 (defun notmuch-show-message-extent ()
763 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
767 (defun notmuch-show-message-top ()
768 (car (notmuch-show-message-extent)))
770 (defun notmuch-show-message-bottom ()
771 (cdr (notmuch-show-message-extent)))
773 (defun notmuch-show-goto-message-next ()
774 (let ((start (point)))
775 (notmuch-show-move-to-message-bottom)
781 (defun notmuch-show-goto-message-previous ()
782 (notmuch-show-move-to-message-top)
786 (notmuch-show-move-to-message-top)
789 (defun notmuch-show-move-past-invisible-forward ()
790 (while (point-invisible-p)
793 (defun notmuch-show-move-past-invisible-backward ()
794 (while (point-invisible-p)
797 ;; Functions relating to the visibility of messages and their
800 (defun notmuch-show-element-visible (props visible-p spec-property)
801 (let ((spec (plist-get props spec-property)))
803 (remove-from-invisibility-spec spec)
804 (add-to-invisibility-spec spec))))
806 (defun notmuch-show-message-visible (props visible-p)
808 ;; When making the message visible, the headers may or not be
809 ;; visible. So we check that property separately.
810 (let ((headers-visible (plist-get props :headers-visible)))
811 (notmuch-show-element-visible props headers-visible :headers-invis-spec)
812 (notmuch-show-element-visible props t :message-invis-spec))
813 (notmuch-show-element-visible props nil :headers-invis-spec)
814 (notmuch-show-element-visible props nil :message-invis-spec))
816 (notmuch-show-set-prop :message-visible visible-p props))
818 (defun notmuch-show-headers-visible (props visible-p)
819 (if (plist-get props :message-visible)
820 (notmuch-show-element-visible props visible-p :headers-invis-spec))
821 (notmuch-show-set-prop :headers-visible visible-p props))
823 ;; Functions for setting and getting attributes of the current
826 (defun notmuch-show-set-message-properties (props)
828 (notmuch-show-move-to-message-top)
829 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
831 (defun notmuch-show-get-message-properties ()
833 (notmuch-show-move-to-message-top)
834 (get-text-property (point) :notmuch-message-properties)))
836 (defun notmuch-show-set-prop (prop val &optional props)
837 (let ((inhibit-read-only t)
839 (notmuch-show-get-message-properties))))
840 (plist-put props prop val)
841 (notmuch-show-set-message-properties props)))
843 (defun notmuch-show-get-prop (prop &optional props)
844 (let ((props (or props
845 (notmuch-show-get-message-properties))))
846 (plist-get props prop)))
848 (defun notmuch-show-get-message-id ()
849 "Return the message id of the current message."
850 (concat "id:\"" (notmuch-show-get-prop :id) "\""))
852 ;; dme: Would it make sense to use a macro for many of these?
854 (defun notmuch-show-get-filename ()
855 "Return the filename of the current message."
856 (notmuch-show-get-prop :filename))
858 (defun notmuch-show-get-header (header)
859 "Return the named header of the current message, if any."
860 (plist-get (notmuch-show-get-prop :headers) header))
862 (defun notmuch-show-get-cc ()
863 (notmuch-show-get-header :Cc))
865 (defun notmuch-show-get-date ()
866 (notmuch-show-get-header :Date))
868 (defun notmuch-show-get-from ()
869 (notmuch-show-get-header :From))
871 (defun notmuch-show-get-subject ()
872 (notmuch-show-get-header :Subject))
874 (defun notmuch-show-get-to ()
875 (notmuch-show-get-header :To))
877 (defun notmuch-show-set-tags (tags)
878 "Set the tags of the current message."
879 (notmuch-show-set-prop :tags tags)
880 (notmuch-show-update-tags tags))
882 (defun notmuch-show-get-tags ()
883 "Return the tags of the current message."
884 (notmuch-show-get-prop :tags))
886 (defun notmuch-show-message-visible-p ()
887 "Is the current message visible?"
888 (notmuch-show-get-prop :message-visible))
890 (defun notmuch-show-headers-visible-p ()
891 "Are the headers of the current message visible?"
892 (notmuch-show-get-prop :headers-visible))
894 (defun notmuch-show-mark-read ()
895 "Mark the current message as read."
896 (notmuch-show-remove-tag "unread"))
898 ;; Functions for getting attributes of several messages in the current
901 (defun notmuch-show-get-message-ids-for-open-messages ()
902 "Return a list of all message IDs for open messages in the current thread."
904 (let (message-ids done)
905 (goto-char (point-min))
907 (if (notmuch-show-message-visible-p)
908 (setq message-ids (append message-ids (list (notmuch-show-get-message-id)))))
909 (setq done (not (notmuch-show-goto-message-next)))
914 ;; Commands typically bound to keys.
916 (defun notmuch-show-advance-and-archive ()
917 "Advance through thread and archive.
919 This command is intended to be one of the simplest ways to
920 process a thread of email. It does the following:
922 If the current message in the thread is not yet fully visible,
923 scroll by a near screenful to read more of the message.
925 Otherwise, (the end of the current message is already within the
926 current window), advance to the next open message.
928 Finally, if there is no further message to advance to, and this
929 last message is already read, then archive the entire current
930 thread, (remove the \"inbox\" tag from each message). Also kill
931 this buffer, and display the next thread from the search from
932 which this thread was originally shown."
934 (let ((end-of-this-message (notmuch-show-message-bottom)))
936 ;; Ideally we would test `end-of-this-message' against the result
937 ;; of `window-end', but that doesn't account for the fact that
938 ;; the end of the message might be hidden, so we have to actually
939 ;; go to the end, walk back over invisible text and then see if
942 (goto-char (- end-of-this-message 1))
943 (notmuch-show-move-past-invisible-backward)
944 (> (point) (window-end)))
945 ;; The bottom of this message is not visible - scroll.
948 ((not (= end-of-this-message (point-max)))
949 ;; This is not the last message - move to the next visible one.
950 (notmuch-show-next-open-message))
953 ;; This is the last message - archive the thread.
954 (notmuch-show-archive-thread)))))
956 (defun notmuch-show-rewind ()
957 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
959 Specifically, if the beginning of the previous email is fewer
960 than `window-height' lines from the current point, move to it
961 just like `notmuch-show-previous-message'.
963 Otherwise, just scroll down a screenful of the current message.
965 This command does not modify any message tags, (it does not undo
966 any effects from previous calls to
967 `notmuch-show-advance-and-archive'."
969 (let ((start-of-message (notmuch-show-message-top))
970 (start-of-window (window-start)))
972 ;; Either this message is properly aligned with the start of the
973 ;; window or the start of this message is not visible on the
975 ((or (= start-of-message start-of-window)
976 (< start-of-message start-of-window))
978 ;; If a small number of lines from the previous message are
979 ;; visible, realign so that the top of the current message is at
980 ;; the top of the screen.
981 (if (<= (count-screen-lines (window-start) start-of-message)
982 next-screen-context-lines)
984 (goto-char (notmuch-show-message-top))
985 (notmuch-show-message-adjust)))
986 ;; Move to the top left of the window.
987 (goto-char (window-start)))
989 ;; Move to the previous message.
990 (notmuch-show-previous-message)))))
992 (defun notmuch-show-reply ()
993 "Reply to the current message."
995 (notmuch-mua-reply (notmuch-show-get-message-id)))
997 (defun notmuch-show-forward-message ()
998 "Forward the current message."
1000 (with-current-notmuch-show-message
1001 (notmuch-mua-forward-message)))
1003 (defun notmuch-show-next-message ()
1004 "Show the next message."
1006 (if (notmuch-show-goto-message-next)
1008 (notmuch-show-mark-read)
1009 (notmuch-show-message-adjust))
1010 (goto-char (point-max))))
1012 (defun notmuch-show-previous-message ()
1013 "Show the previous message."
1015 (notmuch-show-goto-message-previous)
1016 (notmuch-show-mark-read)
1017 (notmuch-show-message-adjust))
1019 (defun notmuch-show-next-open-message ()
1020 "Show the next message."
1023 (while (and (setq r (notmuch-show-goto-message-next))
1024 (not (notmuch-show-message-visible-p))))
1027 (notmuch-show-mark-read)
1028 (notmuch-show-message-adjust))
1029 (goto-char (point-max)))))
1031 (defun notmuch-show-previous-open-message ()
1032 "Show the previous message."
1034 (while (and (notmuch-show-goto-message-previous)
1035 (not (notmuch-show-message-visible-p))))
1036 (notmuch-show-mark-read)
1037 (notmuch-show-message-adjust))
1039 (defun notmuch-show-view-raw-message ()
1040 "View the file holding the current message."
1042 (let* ((id (notmuch-show-get-message-id))
1043 (buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
1044 (call-process notmuch-command nil buf nil "show" "--format=raw" id)
1045 (switch-to-buffer buf)
1046 (goto-char (point-min))
1047 (set-buffer-modified-p nil)
1048 (view-buffer buf 'kill-buffer-if-not-modified)))
1050 (defun notmuch-show-pipe-message (entire-thread command)
1051 "Pipe the contents of the current message (or thread) to the given command.
1053 The given command will be executed with the raw contents of the
1054 current email message as stdin. Anything printed by the command
1055 to stdout or stderr will appear in the *notmuch-pipe* buffer.
1057 When invoked with a prefix argument, the command will receive all
1058 open messages in the current thread (formatted as an mbox) rather
1059 than only the current message."
1060 (interactive "P\nsPipe message to command: ")
1061 (let (shell-command)
1064 (concat notmuch-command " show --format=mbox "
1065 (shell-quote-argument
1066 (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
1069 (concat notmuch-command " show --format=raw "
1070 (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
1071 (let ((buf (get-buffer-create (concat "*notmuch-pipe*"))))
1072 (with-current-buffer buf
1073 (setq buffer-read-only nil)
1075 (let ((exit-code (call-process-shell-command shell-command nil buf)))
1076 (goto-char (point-max))
1077 (set-buffer-modified-p nil)
1078 (setq buffer-read-only t)
1079 (unless (zerop exit-code)
1080 (switch-to-buffer-other-window buf)
1081 (message (format "Command '%s' exited abnormally with code %d"
1082 shell-command exit-code))))))))
1084 (defun notmuch-show-add-tags-worker (current-tags add-tags)
1085 "Add to `current-tags' with any tags from `add-tags' not
1086 currently present and return the result."
1087 (let ((result-tags (copy-sequence current-tags)))
1088 (mapc (lambda (add-tag)
1089 (unless (member add-tag current-tags)
1090 (setq result-tags (push add-tag result-tags))))
1092 (sort result-tags 'string<)))
1094 (defun notmuch-show-del-tags-worker (current-tags del-tags)
1095 "Remove any tags in `del-tags' from `current-tags' and return
1097 (let ((result-tags (copy-sequence current-tags)))
1098 (mapc (lambda (del-tag)
1099 (setq result-tags (delete del-tag result-tags)))
1103 (defun notmuch-show-add-tag (&rest toadd)
1104 "Add a tag to the current message."
1106 (list (notmuch-select-tag-with-completion "Tag to add: ")))
1108 (let* ((current-tags (notmuch-show-get-tags))
1109 (new-tags (notmuch-show-add-tags-worker current-tags toadd)))
1111 (unless (equal current-tags new-tags)
1112 (apply 'notmuch-call-notmuch-process
1114 (mapcar (lambda (s) (concat "+" s)) toadd))
1115 (cons (notmuch-show-get-message-id) nil)))
1116 (notmuch-show-set-tags new-tags))))
1118 (defun notmuch-show-remove-tag (&rest toremove)
1119 "Remove a tag from the current message."
1121 (list (notmuch-select-tag-with-completion
1122 "Tag to remove: " (notmuch-show-get-message-id))))
1124 (let* ((current-tags (notmuch-show-get-tags))
1125 (new-tags (notmuch-show-del-tags-worker current-tags toremove)))
1127 (unless (equal current-tags new-tags)
1128 (apply 'notmuch-call-notmuch-process
1130 (mapcar (lambda (s) (concat "-" s)) toremove))
1131 (cons (notmuch-show-get-message-id) nil)))
1132 (notmuch-show-set-tags new-tags))))
1134 (defun notmuch-show-toggle-headers ()
1135 "Toggle the visibility of the current message headers."
1137 (let ((props (notmuch-show-get-message-properties)))
1138 (notmuch-show-headers-visible
1140 (not (plist-get props :headers-visible))))
1141 (force-window-update))
1143 (defun notmuch-show-toggle-message ()
1144 "Toggle the visibility of the current message."
1146 (let ((props (notmuch-show-get-message-properties)))
1147 (notmuch-show-message-visible
1149 (not (plist-get props :message-visible))))
1150 (force-window-update))
1152 (defun notmuch-show-open-or-close-all ()
1153 "Set the visibility all of the messages in the current thread.
1154 By default make all of the messages visible. With a prefix
1155 argument, hide all of the messages."
1158 (goto-char (point-min))
1159 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1160 (not current-prefix-arg))
1161 until (not (notmuch-show-goto-message-next))))
1162 (force-window-update))
1164 (defun notmuch-show-next-button ()
1165 "Advance point to the next button in the buffer."
1169 (defun notmuch-show-previous-button ()
1170 "Move point back to the previous button in the buffer."
1172 (backward-button 1))
1174 (defun notmuch-show-archive-thread-internal (show-next)
1175 ;; Remove the tag from the current set of messages.
1176 (goto-char (point-min))
1177 (loop do (notmuch-show-remove-tag "inbox")
1178 until (not (notmuch-show-goto-message-next)))
1179 ;; Move to the next item in the search results, if any.
1180 (let ((parent-buffer notmuch-show-parent-buffer))
1181 (notmuch-kill-this-buffer)
1184 (switch-to-buffer parent-buffer)
1187 (notmuch-search-show-thread))))))
1189 (defun notmuch-show-archive-thread ()
1190 "Archive each message in thread, then show next thread from search.
1192 Archive each message currently shown by removing the \"inbox\"
1193 tag from each. Then kill this buffer and show the next thread
1194 from the search from which this thread was originally shown.
1196 Note: This command is safe from any race condition of new messages
1197 being delivered to the same thread. It does not archive the
1198 entire thread, but only the messages shown in the current
1201 (notmuch-show-archive-thread-internal t))
1203 (defun notmuch-show-archive-thread-then-exit ()
1204 "Archive each message in thread, then exit back to search results."
1206 (notmuch-show-archive-thread-internal nil))
1208 (defun notmuch-show-stash-cc ()
1209 "Copy CC field of current message to kill-ring."
1211 (notmuch-common-do-stash (notmuch-show-get-cc)))
1213 (defun notmuch-show-stash-date ()
1214 "Copy date of current message to kill-ring."
1216 (notmuch-common-do-stash (notmuch-show-get-date)))
1218 (defun notmuch-show-stash-filename ()
1219 "Copy filename of current message to kill-ring."
1221 (notmuch-common-do-stash (notmuch-show-get-filename)))
1223 (defun notmuch-show-stash-from ()
1224 "Copy From address of current message to kill-ring."
1226 (notmuch-common-do-stash (notmuch-show-get-from)))
1228 (defun notmuch-show-stash-message-id ()
1229 "Copy message ID of current message to kill-ring."
1231 (notmuch-common-do-stash (notmuch-show-get-message-id)))
1233 (defun notmuch-show-stash-subject ()
1234 "Copy Subject field of current message to kill-ring."
1236 (notmuch-common-do-stash (notmuch-show-get-subject)))
1238 (defun notmuch-show-stash-tags ()
1239 "Copy tags of current message to kill-ring as a comma separated list."
1241 (notmuch-common-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1243 (defun notmuch-show-stash-to ()
1244 "Copy To address of current message to kill-ring."
1246 (notmuch-common-do-stash (notmuch-show-get-to)))
1248 ;; Commands typically bound to buttons.
1250 (defun notmuch-show-part-button-action (button)
1251 (let ((nth (button-get button :notmuch-part)))
1253 (notmuch-show-save-part (notmuch-show-get-message-id) nth
1254 (button-get button :notmuch-filename))
1255 (message "Not a valid part (is it a fake part?)."))))
1259 (provide 'notmuch-show)