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>
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 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
62 "A list of functions called to decorate the headers listed in
63 `notmuch-message-headers'.")
65 (defcustom notmuch-show-hook nil
66 "Functions called after populating a `notmuch-show' buffer."
70 (defcustom notmuch-show-insert-text/plain-hook '(notmuch-wash-excerpt-citations)
71 "Functions used to improve the display of text/plain parts."
74 :options '(notmuch-wash-convert-inline-patch-to-part
75 notmuch-wash-wrap-long-lines
76 notmuch-wash-tidy-citations
77 notmuch-wash-elide-blank-lines
78 notmuch-wash-excerpt-citations))
80 (defmacro with-current-notmuch-show-message (&rest body)
81 "Evaluate body with current buffer set to the text of current message"
83 (let ((filename (notmuch-show-get-filename)))
84 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
85 (with-current-buffer buf
86 (insert-file-contents filename nil nil nil t)
90 (defun notmuch-show-view-all-mime-parts ()
91 "Use external viewers to view all attachments from the current message."
93 (with-current-notmuch-show-message
94 ; We ovverride the mm-inline-media-tests to indicate which message
95 ; parts are already sufficiently handled by the original
96 ; presentation of the message in notmuch-show mode. These parts
97 ; will be inserted directly into the temporary buffer of
98 ; with-current-notmuch-show-message and silently discarded.
100 ; Any MIME part not explicitly mentioned here will be handled by an
101 ; external viewer as configured in the various mailcap files.
102 (let ((mm-inline-media-tests '(
103 ("text/.*" ignore identity)
104 ("application/pgp-signature" ignore identity)
105 ("multipart/alternative" ignore identity)
106 ("multipart/mixed" ignore identity)
107 ("multipart/related" ignore identity)
109 (mm-display-parts (mm-dissect-buffer)))))
111 (defun notmuch-foreach-mime-part (function mm-handle)
112 (cond ((stringp (car mm-handle))
113 (dolist (part (cdr mm-handle))
114 (notmuch-foreach-mime-part function part)))
115 ((bufferp (car mm-handle))
116 (funcall function mm-handle))
117 (t (dolist (part mm-handle)
118 (notmuch-foreach-mime-part function part)))))
120 (defun notmuch-count-attachments (mm-handle)
122 (notmuch-foreach-mime-part
124 (let ((disposition (mm-handle-disposition p)))
125 (and (listp disposition)
126 (or (equal (car disposition) "attachment")
127 (and (equal (car disposition) "inline")
128 (assq 'filename disposition)))
133 (defun notmuch-save-attachments (mm-handle &optional queryp)
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)))
143 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
147 (defun notmuch-show-save-attachments ()
148 "Save all attachments from the current message."
150 (with-current-notmuch-show-message
151 (let ((mm-handle (mm-dissect-buffer)))
152 (notmuch-save-attachments
153 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
156 (defun notmuch-show-fontify-header ()
158 ((looking-at "[Tt]o:")
160 ((looking-at "[Bb]?[Cc][Cc]:")
162 ((looking-at "[Ss]ubject:")
163 'message-header-subject)
164 ((looking-at "[Ff]rom:")
165 'message-header-from)
167 'message-header-other))))
169 (overlay-put (make-overlay (point) (re-search-forward ":"))
170 'face 'message-header-name)
171 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
174 (defun notmuch-show-colour-headers ()
175 "Apply some colouring to the current headers."
176 (goto-char (point-min))
177 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
178 (notmuch-show-fontify-header)
181 (defun notmuch-show-spaces-n (n)
182 "Return a string comprised of `n' spaces."
185 (defun notmuch-show-update-tags (tags)
186 "Update the displayed tags of the current message."
188 (goto-char (notmuch-show-message-top))
189 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
190 (let ((inhibit-read-only t))
191 (replace-match (concat "("
192 (mapconcat 'identity tags " ")
195 (defun notmuch-show-insert-headerline (headers date tags depth)
196 "Insert a notmuch style headerline based on HEADERS for a
197 message at DEPTH in the current thread."
198 (let ((start (point)))
199 (insert (notmuch-show-spaces-n depth)
200 (plist-get headers :From)
204 (mapconcat 'identity tags " ")
206 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
208 (defun notmuch-show-insert-header (header header-value)
209 "Insert a single header."
210 (insert header ": " header-value "\n"))
212 (defun notmuch-show-insert-headers (headers)
213 "Insert the headers of the current message."
214 (let ((start (point)))
215 (mapc '(lambda (header)
216 (let* ((header-symbol (intern (concat ":" header)))
217 (header-value (plist-get headers header-symbol)))
218 (if (and header-value
219 (not (string-equal "" header-value)))
220 (notmuch-show-insert-header header header-value))))
221 notmuch-message-headers)
224 (narrow-to-region start (point-max))
225 (run-hooks 'notmuch-show-markup-headers-hook)))))
227 (define-button-type 'notmuch-show-part-button-type
228 'action 'notmuch-show-part-button-action
232 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name)
235 (if name (concat name ": ") "")
237 (if (not (string-equal declared-type content-type))
238 (concat " (as " content-type ")")
241 :type 'notmuch-show-part-button-type
243 :notmuch-filename name))
245 ;; Functions handling particular MIME parts.
247 (defun notmuch-show-save-part (message-id nth &optional filename)
249 ;; Always acquires the part via `notmuch part', even if it is
250 ;; available in the JSON output.
251 (insert (notmuch-show-get-bodypart-internal message-id nth))
252 (let ((file (read-file-name
253 "Filename to save as: "
254 (or mailcap-download-directory "~/")
257 (require-final-newline nil)
258 (coding-system-for-write 'no-conversion))
259 (write-region (point-min) (point-max) file))))
261 (defun notmuch-show-mm-display-part-inline (msg part content-type content)
262 "Use the mm-decode/mm-view functions to display a part in the
263 current buffer, if possible."
264 (let ((display-buffer (current-buffer)))
267 (let ((handle (mm-make-handle (current-buffer) (list content-type))))
268 (set-buffer display-buffer)
269 (if (and (mm-inlinable-p handle)
270 (mm-inlined-p handle))
272 (mm-display-part handle)
276 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
277 (let ((start (point)))
278 ;; If this text/plain part is not the first part in the message,
279 ;; insert a header to make this clear.
281 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
282 (insert (notmuch-show-get-bodypart-content msg part nth))
285 (narrow-to-region start (point-max))
286 (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
289 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
290 ;; If we can deduce a MIME type from the filename of the attachment,
291 ;; do so and pass it on to the handler for that type.
292 (if (plist-get part :filename)
293 (let ((extension (file-name-extension (plist-get part :filename)))
297 (mailcap-parse-mimetypes)
298 (setq mime-type (mailcap-extension-to-mime extension))
300 (not (string-equal mime-type "application/octet-stream")))
301 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
305 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
306 ;; This handler _must_ succeed - it is the handler of last resort.
307 (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
308 (let ((content (notmuch-show-get-bodypart-content msg part nth)))
310 (notmuch-show-mm-display-part-inline msg part content-type content)))
313 ;; Functions for determining how to handle MIME parts.
315 (defun notmuch-show-split-content-type (content-type)
316 (split-string content-type "/"))
318 (defun notmuch-show-handlers-for (content-type)
319 "Return a list of content handlers for a part of type CONTENT-TYPE."
324 ;; Reverse order of prefrence.
325 (list (intern (concat "notmuch-show-insert-part-*/*"))
327 "notmuch-show-insert-part-"
328 (car (notmuch-show-split-content-type content-type))
330 (intern (concat "notmuch-show-insert-part-" content-type))))
333 ;; Helper for parts which are generally not included in the default
336 (defun notmuch-show-get-bodypart-internal (message-id part-number)
338 (let ((coding-system-for-read 'no-conversion))
339 (call-process notmuch-command nil t nil
340 "part" (format "--part=%s" part-number) message-id)
343 (defun notmuch-show-get-bodypart-content (msg part nth)
344 (or (plist-get part :content)
345 (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
349 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
350 (let ((handlers (notmuch-show-handlers-for content-type)))
351 ;; Run the content handlers until one of them returns a non-nil
354 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
355 (setq handlers (cdr handlers))))
358 (defun notmuch-show-insert-bodypart (msg part depth)
359 "Insert the body part PART at depth DEPTH in the current thread."
360 (let ((content-type (downcase (plist-get part :content-type)))
361 (nth (plist-get part :id)))
362 (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
363 ;; Some of the body part handlers leave point somewhere up in the
364 ;; part, so we make sure that we're down at the end.
365 (goto-char (point-max))
366 ;; Ensure that the part ends with a carriage return.
370 (defun notmuch-show-insert-body (msg body depth)
371 "Insert the body BODY at depth DEPTH in the current thread."
372 (mapc '(lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
374 (defun notmuch-show-make-symbol (type)
375 (make-symbol (concat "notmuch-show-" type)))
377 (defun notmuch-show-insert-msg (msg depth)
378 "Insert the message MSG at depth DEPTH in the current thread."
379 (let ((headers (plist-get msg :headers))
380 ;; Indentation causes the buffer offset of the start/end
381 ;; points to move, so we must use markers.
382 message-start message-end
383 content-start content-end
384 headers-start headers-end
386 (headers-invis-spec (notmuch-show-make-symbol "header"))
387 (message-invis-spec (notmuch-show-make-symbol "message")))
389 ;; Set `buffer-invisibility-spec' to `nil' (a list), otherwise
390 ;; removing items from `buffer-invisibility-spec' (which is what
391 ;; `notmuch-show-headers-visible' and
392 ;; `notmuch-show-message-visible' do) is a no-op and has no
393 ;; effect. This caused threads with only matching messages to have
394 ;; those messages hidden initially because
395 ;; `buffer-invisibility-spec' stayed `t'.
397 ;; This needs to be set here (rather than just above the call to
398 ;; `notmuch-show-headers-visible') because some of the part
399 ;; rendering or body washing functions
400 ;; (e.g. `notmuch-wash-text/plain-citations') manipulate
401 ;; `buffer-invisibility-spec').
402 (when (eq buffer-invisibility-spec t)
403 (setq buffer-invisibility-spec nil))
405 (setq message-start (point-marker))
407 (notmuch-show-insert-headerline headers
408 (or (plist-get msg :date_relative)
409 (plist-get headers :Date))
410 (plist-get msg :tags) depth)
412 (setq content-start (point-marker))
414 ;; Set `headers-start' to point after the 'Subject:' header to be
415 ;; compatible with the existing implementation. This just sets it
416 ;; to after the first header.
417 (notmuch-show-insert-headers headers)
418 ;; Headers should include a blank line (backwards compatibility).
421 (goto-char content-start)
423 (setq headers-start (point-marker)))
424 (setq headers-end (point-marker))
426 (setq body-start (point-marker))
427 (notmuch-show-insert-body msg (plist-get msg :body) depth)
428 ;; Ensure that the body ends with a newline.
431 (setq body-end (point-marker))
432 (setq content-end (point-marker))
434 ;; Indent according to the depth in the thread.
435 (indent-rigidly content-start content-end depth)
437 (setq message-end (point-max-marker))
439 ;; Save the extents of this message over the whole text of the
441 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
443 (plist-put msg :headers-invis-spec headers-invis-spec)
444 (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
446 (plist-put msg :message-invis-spec message-invis-spec)
447 (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
449 ;; Save the properties for this message. Currently this saves the
450 ;; entire message (augmented it with other stuff), which seems
451 ;; like overkill. We might save a reduced subset (for example, not
453 (notmuch-show-set-message-properties msg)
455 ;; Set header visibility.
456 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
458 ;; Message visibility depends on whether it matched the search
460 (notmuch-show-message-visible msg (plist-get msg :match))))
462 (defun notmuch-show-insert-tree (tree depth)
463 "Insert the message tree TREE at depth DEPTH in the current thread."
464 (let ((msg (car tree))
465 (replies (cadr tree)))
466 (notmuch-show-insert-msg msg depth)
467 (notmuch-show-insert-thread replies (1+ depth))))
469 (defun notmuch-show-insert-thread (thread depth)
470 "Insert the thread THREAD at depth DEPTH in the current forest."
471 (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
473 (defun notmuch-show-insert-forest (forest)
474 "Insert the forest of threads FOREST."
475 (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
477 (defvar notmuch-show-parent-buffer nil)
480 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
481 "Run \"notmuch show\" with the given thread ID and display results.
483 The optional PARENT-BUFFER is the notmuch-search buffer from
484 which this notmuch-show command was executed, (so that the
485 next thread from that buffer can be show when done with this
488 The optional QUERY-CONTEXT is a notmuch search term. Only
489 messages from the thread matching this search term are shown if
492 The optional BUFFER-NAME provides the neame of the buffer in
493 which the message thread is shown. If it is nil (which occurs
494 when the command is called interactively) the argument to the
496 (interactive "sNotmuch show: ")
497 (let ((buffer (get-buffer-create (generate-new-buffer-name
499 (concat "*notmuch-" thread-id "*")))))
500 (inhibit-read-only t))
501 (switch-to-buffer buffer)
503 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
505 (goto-char (point-min))
507 (let* ((basic-args (list thread-id))
508 (args (if query-context
509 (append (list "\'") basic-args (list "and (" query-context ")\'"))
510 (append (list "\'") basic-args (list "\'")))))
511 (notmuch-show-insert-forest (notmuch-query-get-threads args))
512 ;; If the query context reduced the results to nothing, run
514 (when (and (eq (buffer-size) 0)
516 (notmuch-show-insert-forest
517 (notmuch-query-get-threads basic-args))))
519 ;; Enable buttonisation of URLs and email addresses in the
521 (goto-address-mode t)
522 ;; Act on visual lines rather than logical lines.
525 (run-hooks 'notmuch-show-hook))
527 ;; Move straight to the first open message
528 (if (not (notmuch-show-message-visible-p))
529 (notmuch-show-next-open-message))
530 (notmuch-show-mark-read)))
532 (defvar notmuch-show-stash-map
533 (let ((map (make-sparse-keymap)))
534 (define-key map "c" 'notmuch-show-stash-cc)
535 (define-key map "d" 'notmuch-show-stash-date)
536 (define-key map "F" 'notmuch-show-stash-filename)
537 (define-key map "f" 'notmuch-show-stash-from)
538 (define-key map "i" 'notmuch-show-stash-message-id)
539 (define-key map "s" 'notmuch-show-stash-subject)
540 (define-key map "T" 'notmuch-show-stash-tags)
541 (define-key map "t" 'notmuch-show-stash-to)
543 "Submap for stash commands")
544 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
546 (defvar notmuch-show-mode-map
547 (let ((map (make-sparse-keymap)))
548 (define-key map "?" 'notmuch-help)
549 (define-key map "q" 'kill-this-buffer)
550 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
551 (define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
552 (define-key map (kbd "TAB") 'notmuch-show-next-button)
553 (define-key map "s" 'notmuch-search)
554 (define-key map "m" 'notmuch-mua-mail)
555 (define-key map "f" 'notmuch-show-forward-message)
556 (define-key map "r" 'notmuch-show-reply)
557 (define-key map "|" 'notmuch-show-pipe-message)
558 (define-key map "w" 'notmuch-show-save-attachments)
559 (define-key map "V" 'notmuch-show-view-raw-message)
560 (define-key map "v" 'notmuch-show-view-all-mime-parts)
561 (define-key map "c" 'notmuch-show-stash-map)
562 (define-key map "h" 'notmuch-show-toggle-headers)
563 (define-key map "-" 'notmuch-show-remove-tag)
564 (define-key map "+" 'notmuch-show-add-tag)
565 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
566 (define-key map "a" 'notmuch-show-archive-thread)
567 (define-key map "N" 'notmuch-show-next-message)
568 (define-key map "P" 'notmuch-show-previous-message)
569 (define-key map "n" 'notmuch-show-next-open-message)
570 (define-key map "p" 'notmuch-show-previous-open-message)
571 (define-key map (kbd "DEL") 'notmuch-show-rewind)
572 (define-key map " " 'notmuch-show-advance-and-archive)
573 (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
574 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
576 "Keymap for \"notmuch show\" buffers.")
577 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
580 (defun notmuch-show-mode ()
581 "Major mode for viewing a thread with notmuch.
583 This buffer contains the results of the \"notmuch show\" command
584 for displaying a single thread of email from your email archives.
586 By default, various components of email messages, (citations,
587 signatures, already-read messages), are hidden. You can make
588 these parts visible by clicking with the mouse button or by
589 pressing RET after positioning the cursor on a hidden part, (for
590 which \\[notmuch-show-next-button] and
591 \\[notmuch-show-previous-button] are helpful).
593 Reading the thread sequentially is well-supported by pressing
594 \\[notmuch-show-advance-and-archive]. This will scroll the
595 current message (if necessary), advance to the next message, or
596 advance to the next thread (if already on the last message of a
599 Other commands are available to read or manipulate the thread
600 more selectively, (such as '\\[notmuch-show-next-message]' and
601 '\\[notmuch-show-previous-message]' to advance to messages
602 without removing any tags, and '\\[notmuch-show-archive-thread]'
603 to archive an entire thread without scrolling through with
604 \\[notmuch-show-advance-and-archive]).
606 You can add or remove arbitary tags from the current message with
607 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
609 All currently available key bindings:
611 \\{notmuch-show-mode-map}"
613 (kill-all-local-variables)
614 (use-local-map notmuch-show-mode-map)
615 (setq major-mode 'notmuch-show-mode
616 mode-name "notmuch-show")
617 (setq buffer-read-only t))
619 (defun notmuch-show-move-to-message-top ()
620 (goto-char (notmuch-show-message-top)))
622 (defun notmuch-show-move-to-message-bottom ()
623 (goto-char (notmuch-show-message-bottom)))
625 (defun notmuch-show-message-adjust ()
628 ;; Movement related functions.
630 ;; There's some strangeness here where a text property applied to a
631 ;; region a->b is not found when point is at b. We walk backwards
632 ;; until finding the property.
633 (defun notmuch-show-message-extent ()
636 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
640 (defun notmuch-show-message-top ()
641 (car (notmuch-show-message-extent)))
643 (defun notmuch-show-message-bottom ()
644 (cdr (notmuch-show-message-extent)))
646 (defun notmuch-show-goto-message-next ()
647 (let ((start (point)))
648 (notmuch-show-move-to-message-bottom)
654 (defun notmuch-show-goto-message-previous ()
655 (notmuch-show-move-to-message-top)
659 (notmuch-show-move-to-message-top)
662 (defun notmuch-show-move-past-invisible-forward ()
663 (while (point-invisible-p)
666 (defun notmuch-show-move-past-invisible-backward ()
667 (while (point-invisible-p)
670 ;; Functions relating to the visibility of messages and their
673 (defun notmuch-show-element-visible (props visible-p spec-property)
674 (let ((spec (plist-get props spec-property)))
676 (remove-from-invisibility-spec spec)
677 (add-to-invisibility-spec spec))))
679 (defun notmuch-show-message-visible (props visible-p)
681 ;; When making the message visible, the headers may or not be
682 ;; visible. So we check that property separately.
683 (let ((headers-visible (plist-get props :headers-visible)))
684 (notmuch-show-element-visible props headers-visible :headers-invis-spec)
685 (notmuch-show-element-visible props t :message-invis-spec))
686 (notmuch-show-element-visible props nil :headers-invis-spec)
687 (notmuch-show-element-visible props nil :message-invis-spec))
689 (notmuch-show-set-prop :message-visible visible-p props))
691 (defun notmuch-show-headers-visible (props visible-p)
692 (if (plist-get props :message-visible)
693 (notmuch-show-element-visible props visible-p :headers-invis-spec))
694 (notmuch-show-set-prop :headers-visible visible-p props))
696 ;; Functions for setting and getting attributes of the current
699 (defun notmuch-show-set-message-properties (props)
701 (notmuch-show-move-to-message-top)
702 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
704 (defun notmuch-show-get-message-properties ()
706 (notmuch-show-move-to-message-top)
707 (get-text-property (point) :notmuch-message-properties)))
709 (defun notmuch-show-set-prop (prop val &optional props)
710 (let ((inhibit-read-only t)
712 (notmuch-show-get-message-properties))))
713 (plist-put props prop val)
714 (notmuch-show-set-message-properties props)))
716 (defun notmuch-show-get-prop (prop &optional props)
717 (let ((props (or props
718 (notmuch-show-get-message-properties))))
719 (plist-get props prop)))
721 (defun notmuch-show-get-message-id ()
722 "Return the message id of the current message."
723 (concat "id:" (notmuch-show-get-prop :id)))
725 ;; dme: Would it make sense to use a macro for many of these?
727 (defun notmuch-show-get-filename ()
728 "Return the filename of the current message."
729 (notmuch-show-get-prop :filename))
731 (defun notmuch-show-get-header (header)
732 "Return the named header of the current message, if any."
733 (plist-get (notmuch-show-get-prop :headers) header))
735 (defun notmuch-show-get-cc ()
736 (notmuch-show-get-header :Cc))
738 (defun notmuch-show-get-date ()
739 (notmuch-show-get-header :Date))
741 (defun notmuch-show-get-from ()
742 (notmuch-show-get-header :From))
744 (defun notmuch-show-get-subject ()
745 (notmuch-show-get-header :Subject))
747 (defun notmuch-show-get-to ()
748 (notmuch-show-get-header :To))
750 (defun notmuch-show-set-tags (tags)
751 "Set the tags of the current message."
752 (notmuch-show-set-prop :tags tags)
753 (notmuch-show-update-tags tags))
755 (defun notmuch-show-get-tags ()
756 "Return the tags of the current message."
757 (notmuch-show-get-prop :tags))
759 (defun notmuch-show-message-visible-p ()
760 "Is the current message visible?"
761 (notmuch-show-get-prop :message-visible))
763 (defun notmuch-show-headers-visible-p ()
764 "Are the headers of the current message visible?"
765 (notmuch-show-get-prop :headers-visible))
767 (defun notmuch-show-mark-read ()
768 "Mark the current message as read."
769 (notmuch-show-remove-tag "unread"))
771 ;; Commands typically bound to keys.
773 (defun notmuch-show-advance-and-archive ()
774 "Advance through thread and archive.
776 This command is intended to be one of the simplest ways to
777 process a thread of email. It does the following:
779 If the current message in the thread is not yet fully visible,
780 scroll by a near screenful to read more of the message.
782 Otherwise, (the end of the current message is already within the
783 current window), advance to the next open message.
785 Finally, if there is no further message to advance to, and this
786 last message is already read, then archive the entire current
787 thread, (remove the \"inbox\" tag from each message). Also kill
788 this buffer, and display the next thread from the search from
789 which this thread was originally shown."
791 (let ((end-of-this-message (notmuch-show-message-bottom)))
793 ;; Ideally we would test `end-of-this-message' against the result
794 ;; of `window-end', but that doesn't account for the fact that
795 ;; the end of the message might be hidden, so we have to actually
796 ;; go to the end, walk back over invisible text and then see if
799 (goto-char (- end-of-this-message 1))
800 (notmuch-show-move-past-invisible-backward)
801 (> (point) (window-end)))
802 ;; The bottom of this message is not visible - scroll.
805 ((not (= end-of-this-message (point-max)))
806 ;; This is not the last message - move to the next visible one.
807 (notmuch-show-next-open-message))
810 ;; This is the last message - archive the thread.
811 (notmuch-show-archive-thread)))))
813 (defun notmuch-show-rewind ()
814 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
816 Specifically, if the beginning of the previous email is fewer
817 than `window-height' lines from the current point, move to it
818 just like `notmuch-show-previous-message'.
820 Otherwise, just scroll down a screenful of the current message.
822 This command does not modify any message tags, (it does not undo
823 any effects from previous calls to
824 `notmuch-show-advance-and-archive'."
826 (let ((start-of-message (notmuch-show-message-top))
827 (start-of-window (window-start)))
829 ;; Either this message is properly aligned with the start of the
830 ;; window or the start of this message is not visible on the
832 ((or (= start-of-message start-of-window)
833 (< start-of-message start-of-window))
835 ;; If a small number of lines from the previous message are
836 ;; visible, realign so that the top of the current message is at
837 ;; the top of the screen.
838 (if (<= (count-screen-lines (window-start) start-of-message)
839 next-screen-context-lines)
841 (goto-char (notmuch-show-message-top))
842 (notmuch-show-message-adjust)))
843 ;; Move to the top left of the window.
844 (goto-char (window-start)))
846 ;; Move to the previous message.
847 (notmuch-show-previous-message)))))
849 (defun notmuch-show-reply ()
850 "Reply to the current message."
852 (notmuch-mua-reply (notmuch-show-get-message-id)))
854 (defun notmuch-show-forward-message ()
855 "Forward the current message."
857 (with-current-notmuch-show-message
858 (notmuch-mua-forward-message)))
860 (defun notmuch-show-next-message ()
861 "Show the next message."
863 (if (notmuch-show-goto-message-next)
865 (notmuch-show-mark-read)
866 (notmuch-show-message-adjust))
867 (goto-char (point-max))))
869 (defun notmuch-show-previous-message ()
870 "Show the previous message."
872 (notmuch-show-goto-message-previous)
873 (notmuch-show-mark-read)
874 (notmuch-show-message-adjust))
876 (defun notmuch-show-next-open-message ()
877 "Show the next message."
880 (while (and (setq r (notmuch-show-goto-message-next))
881 (not (notmuch-show-message-visible-p))))
884 (notmuch-show-mark-read)
885 (notmuch-show-message-adjust))
886 (goto-char (point-max)))))
888 (defun notmuch-show-previous-open-message ()
889 "Show the previous message."
891 (while (and (notmuch-show-goto-message-previous)
892 (not (notmuch-show-message-visible-p))))
893 (notmuch-show-mark-read)
894 (notmuch-show-message-adjust))
896 (defun notmuch-show-view-raw-message ()
897 "View the file holding the current message."
899 (view-file (notmuch-show-get-filename)))
901 (defun notmuch-show-pipe-message (command)
902 "Pipe the contents of the current message to the given command.
904 The given command will be executed with the raw contents of the
905 current email message as stdin. Anything printed by the command
906 to stdout or stderr will appear in the *Messages* buffer."
907 (interactive "sPipe message to command: ")
908 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
910 (shell-quote-argument (notmuch-show-get-filename)))))
912 (defun notmuch-show-add-tag (&rest toadd)
913 "Add a tag to the current message."
915 (list (notmuch-select-tag-with-completion "Tag to add: ")))
916 (apply 'notmuch-call-notmuch-process
918 (mapcar (lambda (s) (concat "+" s)) toadd))
919 (cons (notmuch-show-get-message-id) nil)))
920 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
922 (defun notmuch-show-remove-tag (&rest toremove)
923 "Remove a tag from the current message."
925 (list (notmuch-select-tag-with-completion
926 "Tag to remove: " (notmuch-show-get-message-id))))
927 (let ((tags (notmuch-show-get-tags)))
928 (if (intersection tags toremove :test 'string=)
930 (apply 'notmuch-call-notmuch-process
932 (mapcar (lambda (s) (concat "-" s)) toremove))
933 (cons (notmuch-show-get-message-id) nil)))
934 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
936 (defun notmuch-show-toggle-headers ()
937 "Toggle the visibility of the current message headers."
939 (let ((props (notmuch-show-get-message-properties)))
940 (notmuch-show-headers-visible
942 (not (plist-get props :headers-visible))))
943 (force-window-update))
945 (defun notmuch-show-toggle-message ()
946 "Toggle the visibility of the current message."
948 (let ((props (notmuch-show-get-message-properties)))
949 (notmuch-show-message-visible
951 (not (plist-get props :message-visible))))
952 (force-window-update))
954 (defun notmuch-show-open-or-close-all ()
955 "Set the visibility all of the messages in the current thread.
956 By default make all of the messages visible. With a prefix
957 argument, hide all of the messages."
960 (goto-char (point-min))
961 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
962 (not current-prefix-arg))
963 until (not (notmuch-show-goto-message-next))))
964 (force-window-update))
966 (defun notmuch-show-next-button ()
967 "Advance point to the next button in the buffer."
971 (defun notmuch-show-previous-button ()
972 "Move point back to the previous button in the buffer."
976 (defun notmuch-show-archive-thread-internal (show-next)
977 ;; Remove the tag from the current set of messages.
978 (goto-char (point-min))
979 (loop do (notmuch-show-remove-tag "inbox")
980 until (not (notmuch-show-goto-message-next)))
981 ;; Move to the next item in the search results, if any.
982 (let ((parent-buffer notmuch-show-parent-buffer))
986 (switch-to-buffer parent-buffer)
989 (notmuch-search-show-thread))))))
991 (defun notmuch-show-archive-thread ()
992 "Archive each message in thread, then show next thread from search.
994 Archive each message currently shown by removing the \"inbox\"
995 tag from each. Then kill this buffer and show the next thread
996 from the search from which this thread was originally shown.
998 Note: This command is safe from any race condition of new messages
999 being delivered to the same thread. It does not archive the
1000 entire thread, but only the messages shown in the current
1003 (notmuch-show-archive-thread-internal t))
1005 (defun notmuch-show-archive-thread-then-exit ()
1006 "Archive each message in thread, then exit back to search results."
1008 (notmuch-show-archive-thread-internal nil))
1010 (defun notmuch-show-do-stash (text)
1012 (message "Saved: %s" text))
1014 (defun notmuch-show-stash-cc ()
1015 "Copy CC field of current message to kill-ring."
1017 (notmuch-show-do-stash (notmuch-show-get-cc)))
1019 (defun notmuch-show-stash-date ()
1020 "Copy date of current message to kill-ring."
1022 (notmuch-show-do-stash (notmuch-show-get-date)))
1024 (defun notmuch-show-stash-filename ()
1025 "Copy filename of current message to kill-ring."
1027 (notmuch-show-do-stash (notmuch-show-get-filename)))
1029 (defun notmuch-show-stash-from ()
1030 "Copy From address of current message to kill-ring."
1032 (notmuch-show-do-stash (notmuch-show-get-from)))
1034 (defun notmuch-show-stash-message-id ()
1035 "Copy message ID of current message to kill-ring."
1037 (notmuch-show-do-stash (notmuch-show-get-message-id)))
1039 (defun notmuch-show-stash-subject ()
1040 "Copy Subject field of current message to kill-ring."
1042 (notmuch-show-do-stash (notmuch-show-get-subject)))
1044 (defun notmuch-show-stash-tags ()
1045 "Copy tags of current message to kill-ring as a comma separated list."
1047 (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1049 (defun notmuch-show-stash-to ()
1050 "Copy To address of current message to kill-ring."
1052 (notmuch-show-do-stash (notmuch-show-get-to)))
1054 ;; Commands typically bound to buttons.
1056 (defun notmuch-show-part-button-action (button)
1057 (let ((nth (button-get button :notmuch-part)))
1059 (notmuch-show-save-part (notmuch-show-get-message-id) nth
1060 (button-get button :notmuch-filename))
1061 (message "Not a valid part (is it a fake part?)."))))
1065 (provide 'notmuch-show)