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 (defvar notmuch-show-hook '(notmuch-show-pretty-hook)
66 "A list of functions called after populating a
67 `notmuch-show' buffer.")
69 (defvar notmuch-show-insert-text/plain-hook '(notmuch-wash-text/plain-citations)
70 "A list of functions called to clean up text/plain body parts.")
72 (defun notmuch-show-pretty-hook ()
76 (defmacro with-current-notmuch-show-message (&rest body)
77 "Evaluate body with current buffer set to the text of current message"
79 (let ((filename (notmuch-show-get-filename)))
80 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
81 (with-current-buffer buf
82 (insert-file-contents filename nil nil nil t)
86 (defun notmuch-show-view-all-mime-parts ()
87 "Use external viewers to view all attachments from the current message."
89 (with-current-notmuch-show-message
90 ; We ovverride the mm-inline-media-tests to indicate which message
91 ; parts are already sufficiently handled by the original
92 ; presentation of the message in notmuch-show mode. These parts
93 ; will be inserted directly into the temporary buffer of
94 ; with-current-notmuch-show-message and silently discarded.
96 ; Any MIME part not explicitly mentioned here will be handled by an
97 ; external viewer as configured in the various mailcap files.
98 (let ((mm-inline-media-tests '(
99 ("text/.*" ignore identity)
100 ("application/pgp-signature" ignore identity)
101 ("multipart/alternative" ignore identity)
102 ("multipart/mixed" ignore identity)
103 ("multipart/related" ignore identity)
105 (mm-display-parts (mm-dissect-buffer)))))
107 (defun notmuch-foreach-mime-part (function mm-handle)
108 (cond ((stringp (car mm-handle))
109 (dolist (part (cdr mm-handle))
110 (notmuch-foreach-mime-part function part)))
111 ((bufferp (car mm-handle))
112 (funcall function mm-handle))
113 (t (dolist (part mm-handle)
114 (notmuch-foreach-mime-part function part)))))
116 (defun notmuch-count-attachments (mm-handle)
118 (notmuch-foreach-mime-part
120 (let ((disposition (mm-handle-disposition p)))
121 (and (listp disposition)
122 (or (equal (car disposition) "attachment")
123 (and (equal (car disposition) "inline")
124 (assq 'filename disposition)))
129 (defun notmuch-save-attachments (mm-handle &optional queryp)
130 (notmuch-foreach-mime-part
132 (let ((disposition (mm-handle-disposition p)))
133 (and (listp disposition)
134 (or (equal (car disposition) "attachment")
135 (and (equal (car disposition) "inline")
136 (assq 'filename disposition)))
139 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
143 (defun notmuch-show-save-attachments ()
144 "Save all attachments from the current message."
146 (with-current-notmuch-show-message
147 (let ((mm-handle (mm-dissect-buffer)))
148 (notmuch-save-attachments
149 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
152 (defun notmuch-show-fontify-header ()
154 ((looking-at "[Tt]o:")
156 ((looking-at "[Bb]?[Cc][Cc]:")
158 ((looking-at "[Ss]ubject:")
159 'message-header-subject)
160 ((looking-at "[Ff]rom:")
161 'message-header-from)
163 'message-header-other))))
165 (overlay-put (make-overlay (point) (re-search-forward ":"))
166 'face 'message-header-name)
167 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
170 (defun notmuch-show-colour-headers ()
171 "Apply some colouring to the current headers."
172 (goto-char (point-min))
173 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
174 (notmuch-show-fontify-header)
177 (defun notmuch-show-spaces-n (n)
178 "Return a string comprised of `n' spaces."
181 (defun notmuch-show-update-tags (tags)
182 "Update the displayed tags of the current message."
184 (goto-char (notmuch-show-message-top))
185 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
186 (let ((inhibit-read-only t))
187 (replace-match (concat "("
188 (mapconcat 'identity tags " ")
191 (defun notmuch-show-insert-headerline (headers date tags depth)
192 "Insert a notmuch style headerline based on HEADERS for a
193 message at DEPTH in the current thread."
194 (let ((start (point)))
195 (insert (notmuch-show-spaces-n depth)
196 (plist-get headers :From)
200 (mapconcat 'identity tags " ")
202 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
204 (defun notmuch-show-insert-header (header header-value)
205 "Insert a single header."
206 (insert header ": " header-value "\n"))
208 (defun notmuch-show-insert-headers (headers)
209 "Insert the headers of the current message."
210 (let ((start (point)))
211 (mapc '(lambda (header)
212 (let* ((header-symbol (intern (concat ":" header)))
213 (header-value (plist-get headers header-symbol)))
214 (if (and header-value
215 (not (string-equal "" header-value)))
216 (notmuch-show-insert-header header header-value))))
217 notmuch-message-headers)
220 (narrow-to-region start (point-max))
221 (run-hooks 'notmuch-show-markup-headers-hook)))))
223 (define-button-type 'notmuch-show-part-button-type
224 'action 'notmuch-show-part-button-action
228 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name)
231 (if name (concat name ": ") "")
233 (if (not (string-equal declared-type content-type))
234 (concat " (as " content-type ")")
237 :type 'notmuch-show-part-button-type
239 :notmuch-filename name))
241 ;; Functions handling particular MIME parts.
243 (defun notmuch-show-save-part (message-id nth &optional filename)
245 ;; Always acquires the part via `notmuch part', even if it is
246 ;; available in the JSON output.
247 (insert (notmuch-show-get-bodypart-internal message-id nth))
248 (let ((file (read-file-name
249 "Filename to save as: "
250 (or mailcap-download-directory "~/")
253 (require-final-newline nil)
254 (coding-system-for-write 'no-conversion))
255 (write-region (point-min) (point-max) file))))
257 (defun notmuch-show-mm-display-part-inline (msg part content-type content)
258 "Use the mm-decode/mm-view functions to display a part in the
259 current buffer, if possible."
260 (let ((display-buffer (current-buffer)))
263 (let ((handle (mm-make-handle (current-buffer) (list content-type))))
264 (set-buffer display-buffer)
265 (if (and (mm-inlinable-p handle)
266 (mm-inlined-p handle))
268 (mm-display-part handle)
272 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
273 (let ((start (point)))
274 ;; If this text/plain part is not the first part in the message,
275 ;; insert a header to make this clear.
277 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
278 (insert (notmuch-show-get-bodypart-content msg part nth))
281 (narrow-to-region start (point-max))
282 (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
285 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
286 ;; If we can deduce a MIME type from the filename of the attachment,
287 ;; do so and pass it on to the handler for that type.
288 (if (plist-get part :filename)
289 (let ((extension (file-name-extension (plist-get part :filename)))
293 (mailcap-parse-mimetypes)
294 (setq mime-type (mailcap-extension-to-mime extension))
296 (not (string-equal mime-type "application/octet-stream")))
297 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
301 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
302 ;; This handler _must_ succeed - it is the handler of last resort.
303 (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
304 (let ((content (notmuch-show-get-bodypart-content msg part nth)))
306 (notmuch-show-mm-display-part-inline msg part content-type content)))
309 ;; Functions for determining how to handle MIME parts.
311 (defun notmuch-show-split-content-type (content-type)
312 (split-string content-type "/"))
314 (defun notmuch-show-handlers-for (content-type)
315 "Return a list of content handlers for a part of type CONTENT-TYPE."
320 ;; Reverse order of prefrence.
321 (list (intern (concat "notmuch-show-insert-part-*/*"))
323 "notmuch-show-insert-part-"
324 (car (notmuch-show-split-content-type content-type))
326 (intern (concat "notmuch-show-insert-part-" content-type))))
329 ;; Helper for parts which are generally not included in the default
332 (defun notmuch-show-get-bodypart-internal (message-id part-number)
334 (let ((coding-system-for-read 'no-conversion))
335 (call-process notmuch-command nil t nil
336 "part" (format "--part=%s" part-number) message-id)
339 (defun notmuch-show-get-bodypart-content (msg part nth)
340 (or (plist-get part :content)
341 (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
345 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
346 (let ((handlers (notmuch-show-handlers-for content-type)))
347 ;; Run the content handlers until one of them returns a non-nil
350 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
351 (setq handlers (cdr handlers))))
354 (defun notmuch-show-insert-bodypart (msg part depth)
355 "Insert the body part PART at depth DEPTH in the current thread."
356 (let ((content-type (downcase (plist-get part :content-type)))
357 (nth (plist-get part :id)))
358 (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
359 ;; Some of the body part handlers leave point somewhere up in the
360 ;; part, so we make sure that we're down at the end.
361 (goto-char (point-max))
362 ;; Ensure that the part ends with a carriage return.
366 (defun notmuch-show-insert-body (msg body depth)
367 "Insert the body BODY at depth DEPTH in the current thread."
368 (mapc '(lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
370 (defun notmuch-show-make-symbol (type)
371 (make-symbol (concat "notmuch-show-" type)))
373 (defun notmuch-show-insert-msg (msg depth)
374 "Insert the message MSG at depth DEPTH in the current thread."
375 (let ((headers (plist-get msg :headers))
376 ;; Indentation causes the buffer offset of the start/end
377 ;; points to move, so we must use markers.
378 message-start message-end
379 content-start content-end
380 headers-start headers-end
382 (headers-invis-spec (notmuch-show-make-symbol "header"))
383 (message-invis-spec (notmuch-show-make-symbol "message")))
385 (setq message-start (point-marker))
387 (notmuch-show-insert-headerline headers
388 (or (plist-get msg :date_relative)
389 (plist-get headers :Date))
390 (plist-get msg :tags) depth)
392 (setq content-start (point-marker))
394 ;; Set `headers-start' to point after the 'Subject:' header to be
395 ;; compatible with the existing implementation. This just sets it
396 ;; to after the first header.
397 (notmuch-show-insert-headers headers)
398 ;; Headers should include a blank line (backwards compatibility).
401 (goto-char content-start)
403 (setq headers-start (point-marker)))
404 (setq headers-end (point-marker))
406 (setq body-start (point-marker))
407 (notmuch-show-insert-body msg (plist-get msg :body) depth)
408 ;; Ensure that the body ends with a newline.
411 (setq body-end (point-marker))
412 (setq content-end (point-marker))
414 ;; Indent according to the depth in the thread.
415 (indent-rigidly content-start content-end depth)
417 (setq message-end (point-max-marker))
419 ;; Save the extents of this message over the whole text of the
421 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
423 (plist-put msg :headers-invis-spec headers-invis-spec)
424 (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
426 (plist-put msg :message-invis-spec message-invis-spec)
427 (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
429 ;; Save the properties for this message. Currently this saves the
430 ;; entire message (augmented it with other stuff), which seems
431 ;; like overkill. We might save a reduced subset (for example, not
433 (notmuch-show-set-message-properties msg)
435 ;; Set header visibility.
436 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
438 ;; Message visibility depends on whether it matched the search
440 (notmuch-show-message-visible msg (plist-get msg :match))))
442 (defun notmuch-show-insert-tree (tree depth)
443 "Insert the message tree TREE at depth DEPTH in the current thread."
444 (let ((msg (car tree))
445 (replies (cadr tree)))
446 (notmuch-show-insert-msg msg depth)
447 (notmuch-show-insert-thread replies (1+ depth))))
449 (defun notmuch-show-insert-thread (thread depth)
450 "Insert the thread THREAD at depth DEPTH in the current forest."
451 (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
453 (defun notmuch-show-insert-forest (forest)
454 "Insert the forest of threads FOREST."
455 (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
457 (defvar notmuch-show-parent-buffer nil)
460 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
461 "Run \"notmuch show\" with the given thread ID and display results.
463 The optional PARENT-BUFFER is the notmuch-search buffer from
464 which this notmuch-show command was executed, (so that the
465 next thread from that buffer can be show when done with this
468 The optional QUERY-CONTEXT is a notmuch search term. Only
469 messages from the thread matching this search term are shown if
472 The optional BUFFER-NAME provides the neame of the buffer in
473 which the message thread is shown. If it is nil (which occurs
474 when the command is called interactively) the argument to the
476 (interactive "sNotmuch show: ")
477 (let ((buffer (get-buffer-create (generate-new-buffer-name
479 (concat "*notmuch-" thread-id "*")))))
480 (inhibit-read-only t))
481 (switch-to-buffer buffer)
483 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
485 (goto-char (point-min))
487 (let* ((basic-args (list thread-id))
488 (args (if query-context
489 (append (list "\'") basic-args (list "and (" query-context ")\'"))
490 (append (list "\'") basic-args (list "\'")))))
491 (notmuch-show-insert-forest (notmuch-query-get-threads args))
492 ;; If the query context reduced the results to nothing, run
494 (when (and (eq (buffer-size) 0)
496 (notmuch-show-insert-forest
497 (notmuch-query-get-threads basic-args))))
498 (run-hooks 'notmuch-show-hook))
500 ;; Move straight to the first open message
501 (if (not (notmuch-show-message-visible-p))
502 (notmuch-show-next-open-message))
503 (notmuch-show-mark-read)))
505 (defvar notmuch-show-stash-map
506 (let ((map (make-sparse-keymap)))
507 (define-key map "c" 'notmuch-show-stash-cc)
508 (define-key map "d" 'notmuch-show-stash-date)
509 (define-key map "F" 'notmuch-show-stash-filename)
510 (define-key map "f" 'notmuch-show-stash-from)
511 (define-key map "i" 'notmuch-show-stash-message-id)
512 (define-key map "s" 'notmuch-show-stash-subject)
513 (define-key map "T" 'notmuch-show-stash-tags)
514 (define-key map "t" 'notmuch-show-stash-to)
516 "Submap for stash commands")
517 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
519 (defvar notmuch-show-mode-map
520 (let ((map (make-sparse-keymap)))
521 (define-key map "?" 'notmuch-help)
522 (define-key map "q" 'kill-this-buffer)
523 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
524 (define-key map (kbd "TAB") 'notmuch-show-next-button)
525 (define-key map "s" 'notmuch-search)
526 (define-key map "m" 'notmuch-mua-mail)
527 (define-key map "f" 'notmuch-show-forward-message)
528 (define-key map "r" 'notmuch-show-reply)
529 (define-key map "|" 'notmuch-show-pipe-message)
530 (define-key map "w" 'notmuch-show-save-attachments)
531 (define-key map "V" 'notmuch-show-view-raw-message)
532 (define-key map "v" 'notmuch-show-view-all-mime-parts)
533 (define-key map "c" 'notmuch-show-stash-map)
534 (define-key map "h" 'notmuch-show-toggle-headers)
535 (define-key map "-" 'notmuch-show-remove-tag)
536 (define-key map "+" 'notmuch-show-add-tag)
537 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
538 (define-key map "a" 'notmuch-show-archive-thread)
539 (define-key map "N" 'notmuch-show-next-message)
540 (define-key map "P" 'notmuch-show-previous-message)
541 (define-key map "n" 'notmuch-show-next-open-message)
542 (define-key map "p" 'notmuch-show-previous-open-message)
543 (define-key map (kbd "DEL") 'notmuch-show-rewind)
544 (define-key map " " 'notmuch-show-advance-and-archive)
545 (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
546 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
548 "Keymap for \"notmuch show\" buffers.")
549 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
552 (defun notmuch-show-mode ()
553 "Major mode for viewing a thread with notmuch.
555 This buffer contains the results of the \"notmuch show\" command
556 for displaying a single thread of email from your email archives.
558 By default, various components of email messages, (citations,
559 signatures, already-read messages), are hidden. You can make
560 these parts visible by clicking with the mouse button or by
561 pressing RET after positioning the cursor on a hidden part, (for
562 which \\[notmuch-show-next-button] and
563 \\[notmuch-show-previous-button] are helpful).
565 Reading the thread sequentially is well-supported by pressing
566 \\[notmuch-show-advance-and-archive]. This will scroll the
567 current message (if necessary), advance to the next message, or
568 advance to the next thread (if already on the last message of a
571 Other commands are available to read or manipulate the thread
572 more selectively, (such as '\\[notmuch-show-next-message]' and
573 '\\[notmuch-show-previous-message]' to advance to messages
574 without removing any tags, and '\\[notmuch-show-archive-thread]'
575 to archive an entire thread without scrolling through with
576 \\[notmuch-show-advance-and-archive]).
578 You can add or remove arbitary tags from the current message with
579 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
581 All currently available key bindings:
583 \\{notmuch-show-mode-map}"
585 (kill-all-local-variables)
586 (use-local-map notmuch-show-mode-map)
587 (setq major-mode 'notmuch-show-mode
588 mode-name "notmuch-show")
589 (setq buffer-read-only t))
591 (defun notmuch-show-move-to-message-top ()
592 (goto-char (notmuch-show-message-top)))
594 (defun notmuch-show-move-to-message-bottom ()
595 (goto-char (notmuch-show-message-bottom)))
597 (defun notmuch-show-message-adjust ()
600 ;; Movement related functions.
602 ;; There's some strangeness here where a text property applied to a
603 ;; region a->b is not found when point is at b. We walk backwards
604 ;; until finding the property.
605 (defun notmuch-show-message-extent ()
608 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
612 (defun notmuch-show-message-top ()
613 (car (notmuch-show-message-extent)))
615 (defun notmuch-show-message-bottom ()
616 (cdr (notmuch-show-message-extent)))
618 (defun notmuch-show-goto-message-next ()
619 (let ((start (point)))
620 (notmuch-show-move-to-message-bottom)
626 (defun notmuch-show-goto-message-previous ()
627 (notmuch-show-move-to-message-top)
631 (notmuch-show-move-to-message-top)
634 (defun notmuch-show-move-past-invisible-forward ()
635 (while (point-invisible-p)
638 (defun notmuch-show-move-past-invisible-backward ()
639 (while (point-invisible-p)
642 ;; Functions relating to the visibility of messages and their
645 (defun notmuch-show-element-visible (props visible-p spec-property)
646 (let ((spec (plist-get props spec-property)))
648 (remove-from-invisibility-spec spec)
649 (add-to-invisibility-spec spec))))
651 (defun notmuch-show-message-visible (props visible-p)
653 ;; When making the message visible, the headers may or not be
654 ;; visible. So we check that property separately.
655 (let ((headers-visible (plist-get props :headers-visible)))
656 (notmuch-show-element-visible props headers-visible :headers-invis-spec)
657 (notmuch-show-element-visible props t :message-invis-spec))
658 (notmuch-show-element-visible props nil :headers-invis-spec)
659 (notmuch-show-element-visible props nil :message-invis-spec))
661 (notmuch-show-set-prop :message-visible visible-p props))
663 (defun notmuch-show-headers-visible (props visible-p)
664 (if (plist-get props :message-visible)
665 (notmuch-show-element-visible props visible-p :headers-invis-spec))
666 (notmuch-show-set-prop :headers-visible visible-p props))
668 ;; Functions for setting and getting attributes of the current
671 (defun notmuch-show-set-message-properties (props)
673 (notmuch-show-move-to-message-top)
674 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
676 (defun notmuch-show-get-message-properties ()
678 (notmuch-show-move-to-message-top)
679 (get-text-property (point) :notmuch-message-properties)))
681 (defun notmuch-show-set-prop (prop val &optional props)
682 (let ((inhibit-read-only t)
684 (notmuch-show-get-message-properties))))
685 (plist-put props prop val)
686 (notmuch-show-set-message-properties props)))
688 (defun notmuch-show-get-prop (prop &optional props)
689 (let ((props (or props
690 (notmuch-show-get-message-properties))))
691 (plist-get props prop)))
693 (defun notmuch-show-get-message-id ()
694 "Return the message id of the current message."
695 (concat "id:" (notmuch-show-get-prop :id)))
697 ;; dme: Would it make sense to use a macro for many of these?
699 (defun notmuch-show-get-filename ()
700 "Return the filename of the current message."
701 (notmuch-show-get-prop :filename))
703 (defun notmuch-show-get-header (header)
704 "Return the named header of the current message, if any."
705 (plist-get (notmuch-show-get-prop :headers) header))
707 (defun notmuch-show-get-cc ()
708 (notmuch-show-get-header :Cc))
710 (defun notmuch-show-get-date ()
711 (notmuch-show-get-header :Date))
713 (defun notmuch-show-get-from ()
714 (notmuch-show-get-header :From))
716 (defun notmuch-show-get-subject ()
717 (notmuch-show-get-header :Subject))
719 (defun notmuch-show-get-to ()
720 (notmuch-show-get-header :To))
722 (defun notmuch-show-set-tags (tags)
723 "Set the tags of the current message."
724 (notmuch-show-set-prop :tags tags)
725 (notmuch-show-update-tags tags))
727 (defun notmuch-show-get-tags ()
728 "Return the tags of the current message."
729 (notmuch-show-get-prop :tags))
731 (defun notmuch-show-message-visible-p ()
732 "Is the current message visible?"
733 (notmuch-show-get-prop :message-visible))
735 (defun notmuch-show-headers-visible-p ()
736 "Are the headers of the current message visible?"
737 (notmuch-show-get-prop :headers-visible))
739 (defun notmuch-show-mark-read ()
740 "Mark the current message as read."
741 (notmuch-show-remove-tag "unread"))
743 ;; Commands typically bound to keys.
745 (defun notmuch-show-advance-and-archive ()
746 "Advance through thread and archive.
748 This command is intended to be one of the simplest ways to
749 process a thread of email. It does the following:
751 If the current message in the thread is not yet fully visible,
752 scroll by a near screenful to read more of the message.
754 Otherwise, (the end of the current message is already within the
755 current window), advance to the next open message.
757 Finally, if there is no further message to advance to, and this
758 last message is already read, then archive the entire current
759 thread, (remove the \"inbox\" tag from each message). Also kill
760 this buffer, and display the next thread from the search from
761 which this thread was originally shown."
763 (let ((end-of-this-message (notmuch-show-message-bottom)))
765 ;; Ideally we would test `end-of-this-message' against the result
766 ;; of `window-end', but that doesn't account for the fact that
767 ;; the end of the message might be hidden, so we have to actually
768 ;; go to the end, walk back over invisible text and then see if
771 (goto-char (- end-of-this-message 1))
772 (notmuch-show-move-past-invisible-backward)
773 (> (point) (window-end)))
774 ;; The bottom of this message is not visible - scroll.
777 ((not (= end-of-this-message (point-max)))
778 ;; This is not the last message - move to the next visible one.
779 (notmuch-show-next-open-message))
782 ;; This is the last message - archive the thread.
783 (notmuch-show-archive-thread)))))
785 (defun notmuch-show-rewind ()
786 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
788 Specifically, if the beginning of the previous email is fewer
789 than `window-height' lines from the current point, move to it
790 just like `notmuch-show-previous-message'.
792 Otherwise, just scroll down a screenful of the current message.
794 This command does not modify any message tags, (it does not undo
795 any effects from previous calls to
796 `notmuch-show-advance-and-archive'."
798 (let ((start-of-message (notmuch-show-message-top))
799 (start-of-window (window-start)))
801 ;; Either this message is properly aligned with the start of the
802 ;; window or the start of this message is not visible on the
804 ((or (= start-of-message start-of-window)
805 (< start-of-message start-of-window))
807 ;; If a small number of lines from the previous message are
808 ;; visible, realign so that the top of the current message is at
809 ;; the top of the screen.
810 (if (< (count-lines (window-start) (notmuch-show-message-top))
811 next-screen-context-lines)
813 (goto-char (notmuch-show-message-top))
814 (notmuch-show-message-adjust)))
815 ;; Move to the top left of the window.
816 (goto-char (window-start)))
818 ;; Move to the previous message.
819 (notmuch-show-previous-message)))))
821 (defun notmuch-show-reply ()
822 "Reply to the current message."
824 (notmuch-mua-reply (notmuch-show-get-message-id)))
826 (defun notmuch-show-forward-message ()
827 "Forward the current message."
829 (with-current-notmuch-show-message
830 (notmuch-mua-forward-message)))
832 (defun notmuch-show-next-message ()
833 "Show the next message."
835 (if (notmuch-show-goto-message-next)
837 (notmuch-show-mark-read)
838 (notmuch-show-message-adjust))
839 (goto-char (point-max))))
841 (defun notmuch-show-previous-message ()
842 "Show the previous message."
844 (notmuch-show-goto-message-previous)
845 (notmuch-show-mark-read)
846 (notmuch-show-message-adjust))
848 (defun notmuch-show-next-open-message ()
849 "Show the next message."
852 (while (and (setq r (notmuch-show-goto-message-next))
853 (not (notmuch-show-message-visible-p))))
856 (notmuch-show-mark-read)
857 (notmuch-show-message-adjust))
858 (goto-char (point-max)))))
860 (defun notmuch-show-previous-open-message ()
861 "Show the previous message."
863 (while (and (notmuch-show-goto-message-previous)
864 (not (notmuch-show-message-visible-p))))
865 (notmuch-show-mark-read)
866 (notmuch-show-message-adjust))
868 (defun notmuch-show-view-raw-message ()
869 "View the file holding the current message."
871 (view-file (notmuch-show-get-filename)))
873 (defun notmuch-show-pipe-message (command)
874 "Pipe the contents of the current message to the given command.
876 The given command will be executed with the raw contents of the
877 current email message as stdin. Anything printed by the command
878 to stdout or stderr will appear in the *Messages* buffer."
879 (interactive "sPipe message to command: ")
880 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
882 (shell-quote-argument (notmuch-show-get-filename)))))
884 (defun notmuch-show-add-tag (&rest toadd)
885 "Add a tag to the current message."
887 (list (notmuch-select-tag-with-completion "Tag to add: ")))
888 (apply 'notmuch-call-notmuch-process
890 (mapcar (lambda (s) (concat "+" s)) toadd))
891 (cons (notmuch-show-get-message-id) nil)))
892 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
894 (defun notmuch-show-remove-tag (&rest toremove)
895 "Remove a tag from the current message."
897 (list (notmuch-select-tag-with-completion
898 "Tag to remove: " (notmuch-show-get-message-id))))
899 (let ((tags (notmuch-show-get-tags)))
900 (if (intersection tags toremove :test 'string=)
902 (apply 'notmuch-call-notmuch-process
904 (mapcar (lambda (s) (concat "-" s)) toremove))
905 (cons (notmuch-show-get-message-id) nil)))
906 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
908 (defun notmuch-show-toggle-headers ()
909 "Toggle the visibility of the current message headers."
911 (let ((props (notmuch-show-get-message-properties)))
912 (notmuch-show-headers-visible
914 (not (plist-get props :headers-visible))))
915 (force-window-update))
917 (defun notmuch-show-toggle-message ()
918 "Toggle the visibility of the current message."
920 (let ((props (notmuch-show-get-message-properties)))
921 (notmuch-show-message-visible
923 (not (plist-get props :message-visible))))
924 (force-window-update))
926 (defun notmuch-show-open-or-close-all ()
927 "Set the visibility all of the messages in the current thread.
928 By default make all of the messages visible. With a prefix
929 argument, hide all of the messages."
932 (goto-char (point-min))
933 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
934 (not current-prefix-arg))
935 until (not (notmuch-show-goto-message-next))))
936 (force-window-update))
938 (defun notmuch-show-next-button ()
939 "Advance point to the next button in the buffer."
943 (defun notmuch-show-previous-button ()
944 "Move point back to the previous button in the buffer."
948 (defun notmuch-show-archive-thread-internal (show-next)
949 ;; Remove the tag from the current set of messages.
950 (goto-char (point-min))
951 (loop do (notmuch-show-remove-tag "inbox")
952 until (not (notmuch-show-goto-message-next)))
953 ;; Move to the next item in the search results, if any.
954 (let ((parent-buffer notmuch-show-parent-buffer))
958 (switch-to-buffer parent-buffer)
961 (notmuch-search-show-thread))))))
963 (defun notmuch-show-archive-thread ()
964 "Archive each message in thread, then show next thread from search.
966 Archive each message currently shown by removing the \"inbox\"
967 tag from each. Then kill this buffer and show the next thread
968 from the search from which this thread was originally shown.
970 Note: This command is safe from any race condition of new messages
971 being delivered to the same thread. It does not archive the
972 entire thread, but only the messages shown in the current
975 (notmuch-show-archive-thread-internal t))
977 (defun notmuch-show-archive-thread-then-exit ()
978 "Archive each message in thread, then exit back to search results."
980 (notmuch-show-archive-thread-internal nil))
982 (defun notmuch-show-do-stash (text)
984 (message "Saved: %s" text))
986 (defun notmuch-show-stash-cc ()
987 "Copy CC field of current message to kill-ring."
989 (notmuch-show-do-stash (notmuch-show-get-cc)))
991 (defun notmuch-show-stash-date ()
992 "Copy date of current message to kill-ring."
994 (notmuch-show-do-stash (notmuch-show-get-date)))
996 (defun notmuch-show-stash-filename ()
997 "Copy filename of current message to kill-ring."
999 (notmuch-show-do-stash (notmuch-show-get-filename)))
1001 (defun notmuch-show-stash-from ()
1002 "Copy From address of current message to kill-ring."
1004 (notmuch-show-do-stash (notmuch-show-get-from)))
1006 (defun notmuch-show-stash-message-id ()
1007 "Copy message ID of current message to kill-ring."
1009 (notmuch-show-do-stash (notmuch-show-get-message-id)))
1011 (defun notmuch-show-stash-subject ()
1012 "Copy Subject field of current message to kill-ring."
1014 (notmuch-show-do-stash (notmuch-show-get-subject)))
1016 (defun notmuch-show-stash-tags ()
1017 "Copy tags of current message to kill-ring as a comma separated list."
1019 (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1021 (defun notmuch-show-stash-to ()
1022 "Copy To address of current message to kill-ring."
1024 (notmuch-show-do-stash (notmuch-show-get-to)))
1026 ;; Commands typically bound to buttons.
1028 (defun notmuch-show-part-button-action (button)
1029 (let ((nth (button-get button :notmuch-part)))
1031 (notmuch-show-save-part (notmuch-show-get-message-id) nth
1032 (button-get button :notmuch-filename))
1033 (message "Not a valid part (is it a fake part?)."))))
1037 (provide 'notmuch-show)