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))
30 (require 'notmuch-lib)
31 (require 'notmuch-query)
32 (require 'notmuch-wash)
33 (require 'notmuch-mua)
35 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
36 (declare-function notmuch-fontify-headers "notmuch" nil)
37 (declare-function notmuch-select-tag-with-completion "notmuch" (prompt &rest search-terms))
38 (declare-function notmuch-search-show-thread "notmuch" nil)
40 (defcustom notmuch-message-headers '("Subject" "To" "Cc" "Date")
41 "Headers that should be shown in a message, in this order.
43 For an open message, all of these headers will be made visible
44 according to `notmuch-message-headers-visible' or can be toggled
45 with `notmuch-show-toggle-headers'. For a closed message, only
46 the first header in the list will be visible."
48 :type '(repeat string))
50 (defcustom notmuch-message-headers-visible t
51 "Should the headers be visible by default?
53 If this value is non-nil, then all of the headers defined in
54 `notmuch-message-headers' will be visible by default in the display
55 of each message. Otherwise, these headers will be hidden and
56 `notmuch-show-toggle-headers' can be used to make the visible for
61 (defcustom notmuch-show-relative-dates t
62 "Display relative dates in the message summary line."
66 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
67 "A list of functions called to decorate the headers listed in
68 `notmuch-message-headers'.")
70 (defcustom notmuch-show-hook nil
71 "Functions called after populating a `notmuch-show' buffer."
75 (defcustom notmuch-show-insert-text/plain-hook '(notmuch-wash-excerpt-citations)
76 "Functions used to improve the display of text/plain parts."
79 :options '(notmuch-wash-convert-inline-patch-to-part
80 notmuch-wash-wrap-long-lines
81 notmuch-wash-tidy-citations
82 notmuch-wash-elide-blank-lines
83 notmuch-wash-excerpt-citations))
85 (defmacro with-current-notmuch-show-message (&rest body)
86 "Evaluate body with current buffer set to the text of current message"
88 (let ((id (notmuch-show-get-message-id)))
89 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
90 (with-current-buffer buf
91 (call-process notmuch-command nil t nil "show" "--format=raw" id)
95 (defun notmuch-show-view-all-mime-parts ()
96 "Use external viewers to view all attachments from the current message."
98 (with-current-notmuch-show-message
99 ; We ovverride the mm-inline-media-tests to indicate which message
100 ; parts are already sufficiently handled by the original
101 ; presentation of the message in notmuch-show mode. These parts
102 ; will be inserted directly into the temporary buffer of
103 ; with-current-notmuch-show-message and silently discarded.
105 ; Any MIME part not explicitly mentioned here will be handled by an
106 ; external viewer as configured in the various mailcap files.
107 (let ((mm-inline-media-tests '(
108 ("text/.*" ignore identity)
109 ("application/pgp-signature" ignore identity)
110 ("multipart/alternative" ignore identity)
111 ("multipart/mixed" ignore identity)
112 ("multipart/related" ignore identity)
114 (mm-display-parts (mm-dissect-buffer)))))
116 (defun notmuch-foreach-mime-part (function mm-handle)
117 (cond ((stringp (car mm-handle))
118 (dolist (part (cdr mm-handle))
119 (notmuch-foreach-mime-part function part)))
120 ((bufferp (car mm-handle))
121 (funcall function mm-handle))
122 (t (dolist (part mm-handle)
123 (notmuch-foreach-mime-part function part)))))
125 (defun notmuch-count-attachments (mm-handle)
127 (notmuch-foreach-mime-part
129 (let ((disposition (mm-handle-disposition p)))
130 (and (listp disposition)
131 (or (equal (car disposition) "attachment")
132 (and (equal (car disposition) "inline")
133 (assq 'filename disposition)))
138 (defun notmuch-save-attachments (mm-handle &optional queryp)
139 (notmuch-foreach-mime-part
141 (let ((disposition (mm-handle-disposition p)))
142 (and (listp disposition)
143 (or (equal (car disposition) "attachment")
144 (and (equal (car disposition) "inline")
145 (assq 'filename disposition)))
148 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
152 (defun notmuch-show-save-attachments ()
153 "Save all attachments from the current message."
155 (with-current-notmuch-show-message
156 (let ((mm-handle (mm-dissect-buffer)))
157 (notmuch-save-attachments
158 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
161 (defun notmuch-show-fontify-header ()
163 ((looking-at "[Tt]o:")
165 ((looking-at "[Bb]?[Cc][Cc]:")
167 ((looking-at "[Ss]ubject:")
168 'message-header-subject)
169 ((looking-at "[Ff]rom:")
170 'message-header-from)
172 'message-header-other))))
174 (overlay-put (make-overlay (point) (re-search-forward ":"))
175 'face 'message-header-name)
176 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
179 (defun notmuch-show-colour-headers ()
180 "Apply some colouring to the current headers."
181 (goto-char (point-min))
182 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
183 (notmuch-show-fontify-header)
186 (defun notmuch-show-spaces-n (n)
187 "Return a string comprised of `n' spaces."
190 (defun notmuch-show-update-tags (tags)
191 "Update the displayed tags of the current message."
193 (goto-char (notmuch-show-message-top))
194 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
195 (let ((inhibit-read-only t))
196 (replace-match (concat "("
197 (propertize (mapconcat 'identity tags " ")
198 'face 'notmuch-tag-face)
201 (defun notmuch-show-insert-headerline (headers date tags depth)
202 "Insert a notmuch style headerline based on HEADERS for a
203 message at DEPTH in the current thread."
204 (let ((start (point)))
205 (insert (notmuch-show-spaces-n depth)
206 (plist-get headers :From)
210 (propertize (mapconcat 'identity tags " ")
211 'face 'notmuch-tag-face)
213 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
215 (defun notmuch-show-insert-header (header header-value)
216 "Insert a single header."
217 (insert header ": " header-value "\n"))
219 (defun notmuch-show-insert-headers (headers)
220 "Insert the headers of the current message."
221 (let ((start (point)))
222 (mapc '(lambda (header)
223 (let* ((header-symbol (intern (concat ":" header)))
224 (header-value (plist-get headers header-symbol)))
225 (if (and header-value
226 (not (string-equal "" header-value)))
227 (notmuch-show-insert-header header header-value))))
228 notmuch-message-headers)
231 (narrow-to-region start (point-max))
232 (run-hooks 'notmuch-show-markup-headers-hook)))))
234 (define-button-type 'notmuch-show-part-button-type
235 'action 'notmuch-show-part-button-action
239 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name)
242 (if name (concat name ": ") "")
244 (if (not (string-equal declared-type content-type))
245 (concat " (as " content-type ")")
248 :type 'notmuch-show-part-button-type
250 :notmuch-filename name))
252 ;; Functions handling particular MIME parts.
254 (defun notmuch-show-save-part (message-id nth &optional filename)
256 ;; Always acquires the part via `notmuch part', even if it is
257 ;; available in the JSON output.
258 (insert (notmuch-show-get-bodypart-internal message-id nth))
259 (let ((file (read-file-name
260 "Filename to save as: "
261 (or mailcap-download-directory "~/")
264 (require-final-newline nil)
265 (coding-system-for-write 'no-conversion))
266 (write-region (point-min) (point-max) file))))
268 (defun notmuch-show-mm-display-part-inline (msg part content-type content)
269 "Use the mm-decode/mm-view functions to display a part in the
270 current buffer, if possible."
271 (let ((display-buffer (current-buffer)))
274 (let ((handle (mm-make-handle (current-buffer) (list content-type))))
275 (set-buffer display-buffer)
276 (if (and (mm-inlinable-p handle)
277 (mm-inlined-p handle))
279 (mm-display-part handle)
283 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
284 (let ((start (point)))
285 ;; If this text/plain part is not the first part in the message,
286 ;; insert a header to make this clear.
288 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
289 (insert (notmuch-show-get-bodypart-content msg part nth))
292 (narrow-to-region start (point-max))
293 (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
296 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
297 ;; If we can deduce a MIME type from the filename of the attachment,
298 ;; do so and pass it on to the handler for that type.
299 (if (plist-get part :filename)
300 (let ((extension (file-name-extension (plist-get part :filename)))
304 (mailcap-parse-mimetypes)
305 (setq mime-type (mailcap-extension-to-mime extension))
307 (not (string-equal mime-type "application/octet-stream")))
308 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
312 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
313 ;; This handler _must_ succeed - it is the handler of last resort.
314 (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
315 (let ((content (notmuch-show-get-bodypart-content msg part nth)))
317 (notmuch-show-mm-display-part-inline msg part content-type content)))
320 ;; Functions for determining how to handle MIME parts.
322 (defun notmuch-show-split-content-type (content-type)
323 (split-string content-type "/"))
325 (defun notmuch-show-handlers-for (content-type)
326 "Return a list of content handlers for a part of type CONTENT-TYPE."
331 ;; Reverse order of prefrence.
332 (list (intern (concat "notmuch-show-insert-part-*/*"))
334 "notmuch-show-insert-part-"
335 (car (notmuch-show-split-content-type content-type))
337 (intern (concat "notmuch-show-insert-part-" content-type))))
340 ;; Helper for parts which are generally not included in the default
343 (defun notmuch-show-get-bodypart-internal (message-id part-number)
345 (let ((coding-system-for-read 'no-conversion))
346 (call-process notmuch-command nil t nil
347 "part" (format "--part=%s" part-number) message-id)
350 (defun notmuch-show-get-bodypart-content (msg part nth)
351 (or (plist-get part :content)
352 (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
356 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
357 (let ((handlers (notmuch-show-handlers-for content-type)))
358 ;; Run the content handlers until one of them returns a non-nil
361 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
362 (setq handlers (cdr handlers))))
365 (defun notmuch-show-insert-bodypart (msg part depth)
366 "Insert the body part PART at depth DEPTH in the current thread."
367 (let ((content-type (downcase (plist-get part :content-type)))
368 (nth (plist-get part :id)))
369 (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
370 ;; Some of the body part handlers leave point somewhere up in the
371 ;; part, so we make sure that we're down at the end.
372 (goto-char (point-max))
373 ;; Ensure that the part ends with a carriage return.
377 (defun notmuch-show-insert-body (msg body depth)
378 "Insert the body BODY at depth DEPTH in the current thread."
379 (mapc '(lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
381 (defun notmuch-show-make-symbol (type)
382 (make-symbol (concat "notmuch-show-" type)))
384 (defun notmuch-show-strip-re (string)
385 (replace-regexp-in-string "^\\([Rr]e: *\\)+" "" string))
387 (defvar notmuch-show-previous-subject "")
388 (make-variable-buffer-local 'notmuch-show-previous-subject)
390 (defun notmuch-show-insert-msg (msg depth)
391 "Insert the message MSG at depth DEPTH in the current thread."
392 (let* ((headers (plist-get msg :headers))
393 ;; Indentation causes the buffer offset of the start/end
394 ;; points to move, so we must use markers.
395 message-start message-end
396 content-start content-end
397 headers-start headers-end
399 (headers-invis-spec (notmuch-show-make-symbol "header"))
400 (message-invis-spec (notmuch-show-make-symbol "message"))
401 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject))))
403 ;; Set `buffer-invisibility-spec' to `nil' (a list), otherwise
404 ;; removing items from `buffer-invisibility-spec' (which is what
405 ;; `notmuch-show-headers-visible' and
406 ;; `notmuch-show-message-visible' do) is a no-op and has no
407 ;; effect. This caused threads with only matching messages to have
408 ;; those messages hidden initially because
409 ;; `buffer-invisibility-spec' stayed `t'.
411 ;; This needs to be set here (rather than just above the call to
412 ;; `notmuch-show-headers-visible') because some of the part
413 ;; rendering or body washing functions
414 ;; (e.g. `notmuch-wash-text/plain-citations') manipulate
415 ;; `buffer-invisibility-spec').
416 (when (eq buffer-invisibility-spec t)
417 (setq buffer-invisibility-spec nil))
419 (setq message-start (point-marker))
421 (notmuch-show-insert-headerline headers
422 (or (if notmuch-show-relative-dates
423 (plist-get msg :date_relative)
425 (plist-get headers :Date))
426 (plist-get msg :tags) depth)
428 (setq content-start (point-marker))
430 ;; Set `headers-start' to point after the 'Subject:' header to be
431 ;; compatible with the existing implementation. This just sets it
432 ;; to after the first header.
433 (notmuch-show-insert-headers headers)
434 ;; Headers should include a blank line (backwards compatibility).
437 (goto-char content-start)
438 ;; If the subject of this message is the same as that of the
439 ;; previous message, don't display it when this message is
441 (when (not (string= notmuch-show-previous-subject
444 (setq headers-start (point-marker)))
445 (setq headers-end (point-marker))
447 (setq notmuch-show-previous-subject bare-subject)
449 (setq body-start (point-marker))
450 (notmuch-show-insert-body msg (plist-get msg :body) depth)
451 ;; Ensure that the body ends with a newline.
454 (setq body-end (point-marker))
455 (setq content-end (point-marker))
457 ;; Indent according to the depth in the thread.
458 (indent-rigidly content-start content-end depth)
460 (setq message-end (point-max-marker))
462 ;; Save the extents of this message over the whole text of the
464 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
466 (plist-put msg :headers-invis-spec headers-invis-spec)
467 (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
469 (plist-put msg :message-invis-spec message-invis-spec)
470 (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
472 ;; Save the properties for this message. Currently this saves the
473 ;; entire message (augmented it with other stuff), which seems
474 ;; like overkill. We might save a reduced subset (for example, not
476 (notmuch-show-set-message-properties msg)
478 ;; Set header visibility.
479 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
481 ;; Message visibility depends on whether it matched the search
483 (notmuch-show-message-visible msg (plist-get msg :match))))
485 (defun notmuch-show-insert-tree (tree depth)
486 "Insert the message tree TREE at depth DEPTH in the current thread."
487 (let ((msg (car tree))
488 (replies (cadr tree)))
489 (notmuch-show-insert-msg msg depth)
490 (notmuch-show-insert-thread replies (1+ depth))))
492 (defun notmuch-show-insert-thread (thread depth)
493 "Insert the thread THREAD at depth DEPTH in the current forest."
494 (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
496 (defun notmuch-show-insert-forest (forest)
497 "Insert the forest of threads FOREST."
498 (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
500 (defvar notmuch-show-parent-buffer nil)
503 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
504 "Run \"notmuch show\" with the given thread ID and display results.
506 The optional PARENT-BUFFER is the notmuch-search buffer from
507 which this notmuch-show command was executed, (so that the
508 next thread from that buffer can be show when done with this
511 The optional QUERY-CONTEXT is a notmuch search term. Only
512 messages from the thread matching this search term are shown if
515 The optional BUFFER-NAME provides the neame of the buffer in
516 which the message thread is shown. If it is nil (which occurs
517 when the command is called interactively) the argument to the
519 (interactive "sNotmuch show: ")
520 (let ((buffer (get-buffer-create (generate-new-buffer-name
522 (concat "*notmuch-" thread-id "*")))))
523 (inhibit-read-only t))
524 (switch-to-buffer buffer)
526 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
528 (goto-char (point-min))
530 (let* ((basic-args (list thread-id))
531 (args (if query-context
532 (append (list "\'") basic-args (list "and (" query-context ")\'"))
533 (append (list "\'") basic-args (list "\'")))))
534 (notmuch-show-insert-forest (notmuch-query-get-threads args))
535 ;; If the query context reduced the results to nothing, run
537 (when (and (eq (buffer-size) 0)
539 (notmuch-show-insert-forest
540 (notmuch-query-get-threads basic-args))))
542 ;; Enable buttonisation of URLs and email addresses in the
544 (goto-address-mode t)
545 ;; Act on visual lines rather than logical lines.
548 (run-hooks 'notmuch-show-hook))
550 ;; Move straight to the first open message
551 (if (not (notmuch-show-message-visible-p))
552 (notmuch-show-next-open-message))
553 (notmuch-show-mark-read)))
555 (defvar notmuch-show-stash-map
556 (let ((map (make-sparse-keymap)))
557 (define-key map "c" 'notmuch-show-stash-cc)
558 (define-key map "d" 'notmuch-show-stash-date)
559 (define-key map "F" 'notmuch-show-stash-filename)
560 (define-key map "f" 'notmuch-show-stash-from)
561 (define-key map "i" 'notmuch-show-stash-message-id)
562 (define-key map "s" 'notmuch-show-stash-subject)
563 (define-key map "T" 'notmuch-show-stash-tags)
564 (define-key map "t" 'notmuch-show-stash-to)
566 "Submap for stash commands")
567 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
569 (defvar notmuch-show-mode-map
570 (let ((map (make-sparse-keymap)))
571 (define-key map "?" 'notmuch-help)
572 (define-key map "q" 'notmuch-kill-this-buffer)
573 (define-key map (kbd "<C-tab>") 'widget-backward)
574 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
575 (define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
576 (define-key map (kbd "TAB") 'notmuch-show-next-button)
577 (define-key map "s" 'notmuch-search)
578 (define-key map "m" 'notmuch-mua-mail)
579 (define-key map "f" 'notmuch-show-forward-message)
580 (define-key map "r" 'notmuch-show-reply)
581 (define-key map "|" 'notmuch-show-pipe-message)
582 (define-key map "w" 'notmuch-show-save-attachments)
583 (define-key map "V" 'notmuch-show-view-raw-message)
584 (define-key map "v" 'notmuch-show-view-all-mime-parts)
585 (define-key map "c" 'notmuch-show-stash-map)
586 (define-key map "h" 'notmuch-show-toggle-headers)
587 (define-key map "-" 'notmuch-show-remove-tag)
588 (define-key map "+" 'notmuch-show-add-tag)
589 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
590 (define-key map "a" 'notmuch-show-archive-thread)
591 (define-key map "N" 'notmuch-show-next-message)
592 (define-key map "P" 'notmuch-show-previous-message)
593 (define-key map "n" 'notmuch-show-next-open-message)
594 (define-key map "p" 'notmuch-show-previous-open-message)
595 (define-key map (kbd "DEL") 'notmuch-show-rewind)
596 (define-key map " " 'notmuch-show-advance-and-archive)
597 (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
598 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
600 "Keymap for \"notmuch show\" buffers.")
601 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
603 (defun notmuch-show-mode ()
604 "Major mode for viewing a thread with notmuch.
606 This buffer contains the results of the \"notmuch show\" command
607 for displaying a single thread of email from your email archives.
609 By default, various components of email messages, (citations,
610 signatures, already-read messages), are hidden. You can make
611 these parts visible by clicking with the mouse button or by
612 pressing RET after positioning the cursor on a hidden part, (for
613 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
615 Reading the thread sequentially is well-supported by pressing
616 \\[notmuch-show-advance-and-archive]. This will scroll the current message (if necessary), advance
617 to the next message, or advance to the next thread (if already on
618 the last message of a thread).
620 Other commands are available to read or manipulate the thread
621 more selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages
622 without removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread
623 without scrolling through with \\[notmuch-show-advance-and-archive]).
625 You can add or remove arbitary tags from the current message with
626 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
628 All currently available key bindings:
630 \\{notmuch-show-mode-map}"
632 (kill-all-local-variables)
633 (use-local-map notmuch-show-mode-map)
634 (setq major-mode 'notmuch-show-mode
635 mode-name "notmuch-show")
636 (setq buffer-read-only t))
638 (defun notmuch-show-move-to-message-top ()
639 (goto-char (notmuch-show-message-top)))
641 (defun notmuch-show-move-to-message-bottom ()
642 (goto-char (notmuch-show-message-bottom)))
644 (defun notmuch-show-message-adjust ()
647 ;; Movement related functions.
649 ;; There's some strangeness here where a text property applied to a
650 ;; region a->b is not found when point is at b. We walk backwards
651 ;; until finding the property.
652 (defun notmuch-show-message-extent ()
655 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
659 (defun notmuch-show-message-top ()
660 (car (notmuch-show-message-extent)))
662 (defun notmuch-show-message-bottom ()
663 (cdr (notmuch-show-message-extent)))
665 (defun notmuch-show-goto-message-next ()
666 (let ((start (point)))
667 (notmuch-show-move-to-message-bottom)
673 (defun notmuch-show-goto-message-previous ()
674 (notmuch-show-move-to-message-top)
678 (notmuch-show-move-to-message-top)
681 (defun notmuch-show-move-past-invisible-forward ()
682 (while (point-invisible-p)
685 (defun notmuch-show-move-past-invisible-backward ()
686 (while (point-invisible-p)
689 ;; Functions relating to the visibility of messages and their
692 (defun notmuch-show-element-visible (props visible-p spec-property)
693 (let ((spec (plist-get props spec-property)))
695 (remove-from-invisibility-spec spec)
696 (add-to-invisibility-spec spec))))
698 (defun notmuch-show-message-visible (props visible-p)
700 ;; When making the message visible, the headers may or not be
701 ;; visible. So we check that property separately.
702 (let ((headers-visible (plist-get props :headers-visible)))
703 (notmuch-show-element-visible props headers-visible :headers-invis-spec)
704 (notmuch-show-element-visible props t :message-invis-spec))
705 (notmuch-show-element-visible props nil :headers-invis-spec)
706 (notmuch-show-element-visible props nil :message-invis-spec))
708 (notmuch-show-set-prop :message-visible visible-p props))
710 (defun notmuch-show-headers-visible (props visible-p)
711 (if (plist-get props :message-visible)
712 (notmuch-show-element-visible props visible-p :headers-invis-spec))
713 (notmuch-show-set-prop :headers-visible visible-p props))
715 ;; Functions for setting and getting attributes of the current
718 (defun notmuch-show-set-message-properties (props)
720 (notmuch-show-move-to-message-top)
721 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
723 (defun notmuch-show-get-message-properties ()
725 (notmuch-show-move-to-message-top)
726 (get-text-property (point) :notmuch-message-properties)))
728 (defun notmuch-show-set-prop (prop val &optional props)
729 (let ((inhibit-read-only t)
731 (notmuch-show-get-message-properties))))
732 (plist-put props prop val)
733 (notmuch-show-set-message-properties props)))
735 (defun notmuch-show-get-prop (prop &optional props)
736 (let ((props (or props
737 (notmuch-show-get-message-properties))))
738 (plist-get props prop)))
740 (defun notmuch-show-get-message-id ()
741 "Return the message id of the current message."
742 (concat "id:\"" (notmuch-show-get-prop :id) "\""))
744 ;; dme: Would it make sense to use a macro for many of these?
746 (defun notmuch-show-get-filename ()
747 "Return the filename of the current message."
748 (notmuch-show-get-prop :filename))
750 (defun notmuch-show-get-header (header)
751 "Return the named header of the current message, if any."
752 (plist-get (notmuch-show-get-prop :headers) header))
754 (defun notmuch-show-get-cc ()
755 (notmuch-show-get-header :Cc))
757 (defun notmuch-show-get-date ()
758 (notmuch-show-get-header :Date))
760 (defun notmuch-show-get-from ()
761 (notmuch-show-get-header :From))
763 (defun notmuch-show-get-subject ()
764 (notmuch-show-get-header :Subject))
766 (defun notmuch-show-get-to ()
767 (notmuch-show-get-header :To))
769 (defun notmuch-show-set-tags (tags)
770 "Set the tags of the current message."
771 (notmuch-show-set-prop :tags tags)
772 (notmuch-show-update-tags tags))
774 (defun notmuch-show-get-tags ()
775 "Return the tags of the current message."
776 (notmuch-show-get-prop :tags))
778 (defun notmuch-show-message-visible-p ()
779 "Is the current message visible?"
780 (notmuch-show-get-prop :message-visible))
782 (defun notmuch-show-headers-visible-p ()
783 "Are the headers of the current message visible?"
784 (notmuch-show-get-prop :headers-visible))
786 (defun notmuch-show-mark-read ()
787 "Mark the current message as read."
788 (notmuch-show-remove-tag "unread"))
790 ;; Functions for getting attributes of several messages in the current
793 (defun notmuch-show-get-message-ids-for-open-messages ()
794 "Return a list of all message IDs for open messages in the current thread."
796 (let (message-ids done)
797 (goto-char (point-min))
799 (if (notmuch-show-message-visible-p)
800 (setq message-ids (append message-ids (list (notmuch-show-get-message-id)))))
801 (setq done (not (notmuch-show-goto-message-next)))
806 ;; Commands typically bound to keys.
808 (defun notmuch-show-advance-and-archive ()
809 "Advance through thread and archive.
811 This command is intended to be one of the simplest ways to
812 process a thread of email. It does the following:
814 If the current message in the thread is not yet fully visible,
815 scroll by a near screenful to read more of the message.
817 Otherwise, (the end of the current message is already within the
818 current window), advance to the next open message.
820 Finally, if there is no further message to advance to, and this
821 last message is already read, then archive the entire current
822 thread, (remove the \"inbox\" tag from each message). Also kill
823 this buffer, and display the next thread from the search from
824 which this thread was originally shown."
826 (let ((end-of-this-message (notmuch-show-message-bottom)))
828 ;; Ideally we would test `end-of-this-message' against the result
829 ;; of `window-end', but that doesn't account for the fact that
830 ;; the end of the message might be hidden, so we have to actually
831 ;; go to the end, walk back over invisible text and then see if
834 (goto-char (- end-of-this-message 1))
835 (notmuch-show-move-past-invisible-backward)
836 (> (point) (window-end)))
837 ;; The bottom of this message is not visible - scroll.
840 ((not (= end-of-this-message (point-max)))
841 ;; This is not the last message - move to the next visible one.
842 (notmuch-show-next-open-message))
845 ;; This is the last message - archive the thread.
846 (notmuch-show-archive-thread)))))
848 (defun notmuch-show-rewind ()
849 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
851 Specifically, if the beginning of the previous email is fewer
852 than `window-height' lines from the current point, move to it
853 just like `notmuch-show-previous-message'.
855 Otherwise, just scroll down a screenful of the current message.
857 This command does not modify any message tags, (it does not undo
858 any effects from previous calls to
859 `notmuch-show-advance-and-archive'."
861 (let ((start-of-message (notmuch-show-message-top))
862 (start-of-window (window-start)))
864 ;; Either this message is properly aligned with the start of the
865 ;; window or the start of this message is not visible on the
867 ((or (= start-of-message start-of-window)
868 (< start-of-message start-of-window))
870 ;; If a small number of lines from the previous message are
871 ;; visible, realign so that the top of the current message is at
872 ;; the top of the screen.
873 (if (<= (count-screen-lines (window-start) start-of-message)
874 next-screen-context-lines)
876 (goto-char (notmuch-show-message-top))
877 (notmuch-show-message-adjust)))
878 ;; Move to the top left of the window.
879 (goto-char (window-start)))
881 ;; Move to the previous message.
882 (notmuch-show-previous-message)))))
884 (defun notmuch-show-reply ()
885 "Reply to the current message."
887 (notmuch-mua-reply (notmuch-show-get-message-id)))
889 (defun notmuch-show-forward-message ()
890 "Forward the current message."
892 (with-current-notmuch-show-message
893 (notmuch-mua-forward-message)))
895 (defun notmuch-show-next-message ()
896 "Show the next message."
898 (if (notmuch-show-goto-message-next)
900 (notmuch-show-mark-read)
901 (notmuch-show-message-adjust))
902 (goto-char (point-max))))
904 (defun notmuch-show-previous-message ()
905 "Show the previous message."
907 (notmuch-show-goto-message-previous)
908 (notmuch-show-mark-read)
909 (notmuch-show-message-adjust))
911 (defun notmuch-show-next-open-message ()
912 "Show the next message."
915 (while (and (setq r (notmuch-show-goto-message-next))
916 (not (notmuch-show-message-visible-p))))
919 (notmuch-show-mark-read)
920 (notmuch-show-message-adjust))
921 (goto-char (point-max)))))
923 (defun notmuch-show-previous-open-message ()
924 "Show the previous message."
926 (while (and (notmuch-show-goto-message-previous)
927 (not (notmuch-show-message-visible-p))))
928 (notmuch-show-mark-read)
929 (notmuch-show-message-adjust))
931 (defun notmuch-show-view-raw-message ()
932 "View the file holding the current message."
934 (let ((id (notmuch-show-get-message-id)))
935 (let ((buf (get-buffer-create (concat "*notmuch-raw-" id "*"))))
936 (switch-to-buffer buf)
938 (call-process notmuch-command nil t nil "show" "--format=raw" id)))))
940 (defun notmuch-show-pipe-message (entire-thread command)
941 "Pipe the contents of the current message (or thread) to the given command.
943 The given command will be executed with the raw contents of the
944 current email message as stdin. Anything printed by the command
945 to stdout or stderr will appear in the *Messages* buffer.
947 When invoked with a prefix argument, the command will receive all
948 open messages in the current thread (formatted as an mbox) rather
949 than only the current message."
950 (interactive "P\nsPipe message to command: ")
954 (concat notmuch-command " show --format=mbox "
955 (shell-quote-argument
956 (mapconcat 'identity (notmuch-show-get-message-ids-for-open-messages) " OR "))
959 (concat notmuch-command " show --format=raw "
960 (shell-quote-argument (notmuch-show-get-message-id)) " | " command)))
961 (start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*" shell-command)))
963 (defun notmuch-show-add-tags-worker (current-tags add-tags)
964 "Add to `current-tags' with any tags from `add-tags' not
965 currently present and return the result."
966 (let ((result-tags (copy-sequence current-tags)))
967 (mapc (lambda (add-tag)
968 (unless (member add-tag current-tags)
969 (setq result-tags (push add-tag result-tags))))
971 (sort result-tags 'string<)))
973 (defun notmuch-show-del-tags-worker (current-tags del-tags)
974 "Remove any tags in `del-tags' from `current-tags' and return
976 (let ((result-tags (copy-sequence current-tags)))
977 (mapc (lambda (del-tag)
978 (setq result-tags (delete del-tag result-tags)))
982 (defun notmuch-show-add-tag (&rest toadd)
983 "Add a tag to the current message."
985 (list (notmuch-select-tag-with-completion "Tag to add: ")))
987 (let* ((current-tags (notmuch-show-get-tags))
988 (new-tags (notmuch-show-add-tags-worker current-tags toadd)))
990 (unless (equal current-tags new-tags)
991 (apply 'notmuch-call-notmuch-process
993 (mapcar (lambda (s) (concat "+" s)) toadd))
994 (cons (notmuch-show-get-message-id) nil)))
995 (notmuch-show-set-tags new-tags))))
997 (defun notmuch-show-remove-tag (&rest toremove)
998 "Remove a tag from the current message."
1000 (list (notmuch-select-tag-with-completion
1001 "Tag to remove: " (notmuch-show-get-message-id))))
1003 (let* ((current-tags (notmuch-show-get-tags))
1004 (new-tags (notmuch-show-del-tags-worker current-tags toremove)))
1006 (unless (equal current-tags new-tags)
1007 (apply 'notmuch-call-notmuch-process
1009 (mapcar (lambda (s) (concat "-" s)) toremove))
1010 (cons (notmuch-show-get-message-id) nil)))
1011 (notmuch-show-set-tags new-tags))))
1013 (defun notmuch-show-toggle-headers ()
1014 "Toggle the visibility of the current message headers."
1016 (let ((props (notmuch-show-get-message-properties)))
1017 (notmuch-show-headers-visible
1019 (not (plist-get props :headers-visible))))
1020 (force-window-update))
1022 (defun notmuch-show-toggle-message ()
1023 "Toggle the visibility of the current message."
1025 (let ((props (notmuch-show-get-message-properties)))
1026 (notmuch-show-message-visible
1028 (not (plist-get props :message-visible))))
1029 (force-window-update))
1031 (defun notmuch-show-open-or-close-all ()
1032 "Set the visibility all of the messages in the current thread.
1033 By default make all of the messages visible. With a prefix
1034 argument, hide all of the messages."
1037 (goto-char (point-min))
1038 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
1039 (not current-prefix-arg))
1040 until (not (notmuch-show-goto-message-next))))
1041 (force-window-update))
1043 (defun notmuch-show-next-button ()
1044 "Advance point to the next button in the buffer."
1048 (defun notmuch-show-previous-button ()
1049 "Move point back to the previous button in the buffer."
1051 (backward-button 1))
1053 (defun notmuch-show-archive-thread-internal (show-next)
1054 ;; Remove the tag from the current set of messages.
1055 (goto-char (point-min))
1056 (loop do (notmuch-show-remove-tag "inbox")
1057 until (not (notmuch-show-goto-message-next)))
1058 ;; Move to the next item in the search results, if any.
1059 (let ((parent-buffer notmuch-show-parent-buffer))
1060 (notmuch-kill-this-buffer)
1063 (switch-to-buffer parent-buffer)
1066 (notmuch-search-show-thread))))))
1068 (defun notmuch-show-archive-thread ()
1069 "Archive each message in thread, then show next thread from search.
1071 Archive each message currently shown by removing the \"inbox\"
1072 tag from each. Then kill this buffer and show the next thread
1073 from the search from which this thread was originally shown.
1075 Note: This command is safe from any race condition of new messages
1076 being delivered to the same thread. It does not archive the
1077 entire thread, but only the messages shown in the current
1080 (notmuch-show-archive-thread-internal t))
1082 (defun notmuch-show-archive-thread-then-exit ()
1083 "Archive each message in thread, then exit back to search results."
1085 (notmuch-show-archive-thread-internal nil))
1087 (defun notmuch-show-do-stash (text)
1089 (message "Saved: %s" text))
1091 (defun notmuch-show-stash-cc ()
1092 "Copy CC field of current message to kill-ring."
1094 (notmuch-show-do-stash (notmuch-show-get-cc)))
1096 (defun notmuch-show-stash-date ()
1097 "Copy date of current message to kill-ring."
1099 (notmuch-show-do-stash (notmuch-show-get-date)))
1101 (defun notmuch-show-stash-filename ()
1102 "Copy filename of current message to kill-ring."
1104 (notmuch-show-do-stash (notmuch-show-get-filename)))
1106 (defun notmuch-show-stash-from ()
1107 "Copy From address of current message to kill-ring."
1109 (notmuch-show-do-stash (notmuch-show-get-from)))
1111 (defun notmuch-show-stash-message-id ()
1112 "Copy message ID of current message to kill-ring."
1114 (notmuch-show-do-stash (notmuch-show-get-message-id)))
1116 (defun notmuch-show-stash-subject ()
1117 "Copy Subject field of current message to kill-ring."
1119 (notmuch-show-do-stash (notmuch-show-get-subject)))
1121 (defun notmuch-show-stash-tags ()
1122 "Copy tags of current message to kill-ring as a comma separated list."
1124 (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1126 (defun notmuch-show-stash-to ()
1127 "Copy To address of current message to kill-ring."
1129 (notmuch-show-do-stash (notmuch-show-get-to)))
1131 ;; Commands typically bound to buttons.
1133 (defun notmuch-show-part-button-action (button)
1134 (let ((nth (button-get button :notmuch-part)))
1136 (notmuch-show-save-part (notmuch-show-get-message-id) nth
1137 (button-get button :notmuch-filename))
1138 (message "Not a valid part (is it a fake part?)."))))
1142 (provide 'notmuch-show)