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-wrap-long-lines
75 notmuch-wash-tidy-citations
76 notmuch-wash-elide-blank-lines
77 notmuch-wash-excerpt-citations))
79 (defmacro with-current-notmuch-show-message (&rest body)
80 "Evaluate body with current buffer set to the text of current message"
82 (let ((filename (notmuch-show-get-filename)))
83 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
84 (with-current-buffer buf
85 (insert-file-contents filename nil nil nil t)
89 (defun notmuch-show-view-all-mime-parts ()
90 "Use external viewers to view all attachments from the current message."
92 (with-current-notmuch-show-message
93 ; We ovverride the mm-inline-media-tests to indicate which message
94 ; parts are already sufficiently handled by the original
95 ; presentation of the message in notmuch-show mode. These parts
96 ; will be inserted directly into the temporary buffer of
97 ; with-current-notmuch-show-message and silently discarded.
99 ; Any MIME part not explicitly mentioned here will be handled by an
100 ; external viewer as configured in the various mailcap files.
101 (let ((mm-inline-media-tests '(
102 ("text/.*" ignore identity)
103 ("application/pgp-signature" ignore identity)
104 ("multipart/alternative" ignore identity)
105 ("multipart/mixed" ignore identity)
106 ("multipart/related" ignore identity)
108 (mm-display-parts (mm-dissect-buffer)))))
110 (defun notmuch-foreach-mime-part (function mm-handle)
111 (cond ((stringp (car mm-handle))
112 (dolist (part (cdr mm-handle))
113 (notmuch-foreach-mime-part function part)))
114 ((bufferp (car mm-handle))
115 (funcall function mm-handle))
116 (t (dolist (part mm-handle)
117 (notmuch-foreach-mime-part function part)))))
119 (defun notmuch-count-attachments (mm-handle)
121 (notmuch-foreach-mime-part
123 (let ((disposition (mm-handle-disposition p)))
124 (and (listp disposition)
125 (or (equal (car disposition) "attachment")
126 (and (equal (car disposition) "inline")
127 (assq 'filename disposition)))
132 (defun notmuch-save-attachments (mm-handle &optional queryp)
133 (notmuch-foreach-mime-part
135 (let ((disposition (mm-handle-disposition p)))
136 (and (listp disposition)
137 (or (equal (car disposition) "attachment")
138 (and (equal (car disposition) "inline")
139 (assq 'filename disposition)))
142 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
146 (defun notmuch-show-save-attachments ()
147 "Save all attachments from the current message."
149 (with-current-notmuch-show-message
150 (let ((mm-handle (mm-dissect-buffer)))
151 (notmuch-save-attachments
152 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
155 (defun notmuch-show-fontify-header ()
157 ((looking-at "[Tt]o:")
159 ((looking-at "[Bb]?[Cc][Cc]:")
161 ((looking-at "[Ss]ubject:")
162 'message-header-subject)
163 ((looking-at "[Ff]rom:")
164 'message-header-from)
166 'message-header-other))))
168 (overlay-put (make-overlay (point) (re-search-forward ":"))
169 'face 'message-header-name)
170 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
173 (defun notmuch-show-colour-headers ()
174 "Apply some colouring to the current headers."
175 (goto-char (point-min))
176 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
177 (notmuch-show-fontify-header)
180 (defun notmuch-show-spaces-n (n)
181 "Return a string comprised of `n' spaces."
184 (defun notmuch-show-update-tags (tags)
185 "Update the displayed tags of the current message."
187 (goto-char (notmuch-show-message-top))
188 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
189 (let ((inhibit-read-only t))
190 (replace-match (concat "("
191 (mapconcat 'identity tags " ")
194 (defun notmuch-show-insert-headerline (headers date tags depth)
195 "Insert a notmuch style headerline based on HEADERS for a
196 message at DEPTH in the current thread."
197 (let ((start (point)))
198 (insert (notmuch-show-spaces-n depth)
199 (plist-get headers :From)
203 (mapconcat 'identity tags " ")
205 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
207 (defun notmuch-show-insert-header (header header-value)
208 "Insert a single header."
209 (insert header ": " header-value "\n"))
211 (defun notmuch-show-insert-headers (headers)
212 "Insert the headers of the current message."
213 (let ((start (point)))
214 (mapc '(lambda (header)
215 (let* ((header-symbol (intern (concat ":" header)))
216 (header-value (plist-get headers header-symbol)))
217 (if (and header-value
218 (not (string-equal "" header-value)))
219 (notmuch-show-insert-header header header-value))))
220 notmuch-message-headers)
223 (narrow-to-region start (point-max))
224 (run-hooks 'notmuch-show-markup-headers-hook)))))
226 (define-button-type 'notmuch-show-part-button-type
227 'action 'notmuch-show-part-button-action
231 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name)
234 (if name (concat name ": ") "")
236 (if (not (string-equal declared-type content-type))
237 (concat " (as " content-type ")")
240 :type 'notmuch-show-part-button-type
242 :notmuch-filename name))
244 ;; Functions handling particular MIME parts.
246 (defun notmuch-show-save-part (message-id nth &optional filename)
248 ;; Always acquires the part via `notmuch part', even if it is
249 ;; available in the JSON output.
250 (insert (notmuch-show-get-bodypart-internal message-id nth))
251 (let ((file (read-file-name
252 "Filename to save as: "
253 (or mailcap-download-directory "~/")
256 (require-final-newline nil)
257 (coding-system-for-write 'no-conversion))
258 (write-region (point-min) (point-max) file))))
260 (defun notmuch-show-mm-display-part-inline (msg part content-type content)
261 "Use the mm-decode/mm-view functions to display a part in the
262 current buffer, if possible."
263 (let ((display-buffer (current-buffer)))
266 (let ((handle (mm-make-handle (current-buffer) (list content-type))))
267 (set-buffer display-buffer)
268 (if (and (mm-inlinable-p handle)
269 (mm-inlined-p handle))
271 (mm-display-part handle)
275 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
276 (let ((start (point)))
277 ;; If this text/plain part is not the first part in the message,
278 ;; insert a header to make this clear.
280 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
281 (insert (notmuch-show-get-bodypart-content msg part nth))
284 (narrow-to-region start (point-max))
285 (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
288 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
289 ;; If we can deduce a MIME type from the filename of the attachment,
290 ;; do so and pass it on to the handler for that type.
291 (if (plist-get part :filename)
292 (let ((extension (file-name-extension (plist-get part :filename)))
296 (mailcap-parse-mimetypes)
297 (setq mime-type (mailcap-extension-to-mime extension))
299 (not (string-equal mime-type "application/octet-stream")))
300 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
304 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
305 ;; This handler _must_ succeed - it is the handler of last resort.
306 (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
307 (let ((content (notmuch-show-get-bodypart-content msg part nth)))
309 (notmuch-show-mm-display-part-inline msg part content-type content)))
312 ;; Functions for determining how to handle MIME parts.
314 (defun notmuch-show-split-content-type (content-type)
315 (split-string content-type "/"))
317 (defun notmuch-show-handlers-for (content-type)
318 "Return a list of content handlers for a part of type CONTENT-TYPE."
323 ;; Reverse order of prefrence.
324 (list (intern (concat "notmuch-show-insert-part-*/*"))
326 "notmuch-show-insert-part-"
327 (car (notmuch-show-split-content-type content-type))
329 (intern (concat "notmuch-show-insert-part-" content-type))))
332 ;; Helper for parts which are generally not included in the default
335 (defun notmuch-show-get-bodypart-internal (message-id part-number)
337 (let ((coding-system-for-read 'no-conversion))
338 (call-process notmuch-command nil t nil
339 "part" (format "--part=%s" part-number) message-id)
342 (defun notmuch-show-get-bodypart-content (msg part nth)
343 (or (plist-get part :content)
344 (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
348 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
349 (let ((handlers (notmuch-show-handlers-for content-type)))
350 ;; Run the content handlers until one of them returns a non-nil
353 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
354 (setq handlers (cdr handlers))))
357 (defun notmuch-show-insert-bodypart (msg part depth)
358 "Insert the body part PART at depth DEPTH in the current thread."
359 (let ((content-type (downcase (plist-get part :content-type)))
360 (nth (plist-get part :id)))
361 (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
362 ;; Some of the body part handlers leave point somewhere up in the
363 ;; part, so we make sure that we're down at the end.
364 (goto-char (point-max))
365 ;; Ensure that the part ends with a carriage return.
369 (defun notmuch-show-insert-body (msg body depth)
370 "Insert the body BODY at depth DEPTH in the current thread."
371 (mapc '(lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
373 (defun notmuch-show-make-symbol (type)
374 (make-symbol (concat "notmuch-show-" type)))
376 (defun notmuch-show-insert-msg (msg depth)
377 "Insert the message MSG at depth DEPTH in the current thread."
378 (let ((headers (plist-get msg :headers))
379 ;; Indentation causes the buffer offset of the start/end
380 ;; points to move, so we must use markers.
381 message-start message-end
382 content-start content-end
383 headers-start headers-end
385 (headers-invis-spec (notmuch-show-make-symbol "header"))
386 (message-invis-spec (notmuch-show-make-symbol "message")))
388 ;; Set `buffer-invisibility-spec' to `nil' (a list), otherwise
389 ;; removing items from `buffer-invisibility-spec' (which is what
390 ;; `notmuch-show-headers-visible' and
391 ;; `notmuch-show-message-visible' do) is a no-op and has no
392 ;; effect. This caused threads with only matching messages to have
393 ;; those messages hidden initially because
394 ;; `buffer-invisibility-spec' stayed `t'.
396 ;; This needs to be set here (rather than just above the call to
397 ;; `notmuch-show-headers-visible') because some of the part
398 ;; rendering or body washing functions
399 ;; (e.g. `notmuch-wash-text/plain-citations') manipulate
400 ;; `buffer-invisibility-spec').
401 (when (eq buffer-invisibility-spec t)
402 (setq buffer-invisibility-spec nil))
404 (setq message-start (point-marker))
406 (notmuch-show-insert-headerline headers
407 (or (plist-get msg :date_relative)
408 (plist-get headers :Date))
409 (plist-get msg :tags) depth)
411 (setq content-start (point-marker))
413 ;; Set `headers-start' to point after the 'Subject:' header to be
414 ;; compatible with the existing implementation. This just sets it
415 ;; to after the first header.
416 (notmuch-show-insert-headers headers)
417 ;; Headers should include a blank line (backwards compatibility).
420 (goto-char content-start)
422 (setq headers-start (point-marker)))
423 (setq headers-end (point-marker))
425 (setq body-start (point-marker))
426 (notmuch-show-insert-body msg (plist-get msg :body) depth)
427 ;; Ensure that the body ends with a newline.
430 (setq body-end (point-marker))
431 (setq content-end (point-marker))
433 ;; Indent according to the depth in the thread.
434 (indent-rigidly content-start content-end depth)
436 (setq message-end (point-max-marker))
438 ;; Save the extents of this message over the whole text of the
440 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
442 (plist-put msg :headers-invis-spec headers-invis-spec)
443 (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
445 (plist-put msg :message-invis-spec message-invis-spec)
446 (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
448 ;; Save the properties for this message. Currently this saves the
449 ;; entire message (augmented it with other stuff), which seems
450 ;; like overkill. We might save a reduced subset (for example, not
452 (notmuch-show-set-message-properties msg)
454 ;; Set header visibility.
455 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
457 ;; Message visibility depends on whether it matched the search
459 (notmuch-show-message-visible msg (plist-get msg :match))))
461 (defun notmuch-show-insert-tree (tree depth)
462 "Insert the message tree TREE at depth DEPTH in the current thread."
463 (let ((msg (car tree))
464 (replies (cadr tree)))
465 (notmuch-show-insert-msg msg depth)
466 (notmuch-show-insert-thread replies (1+ depth))))
468 (defun notmuch-show-insert-thread (thread depth)
469 "Insert the thread THREAD at depth DEPTH in the current forest."
470 (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
472 (defun notmuch-show-insert-forest (forest)
473 "Insert the forest of threads FOREST."
474 (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
476 (defvar notmuch-show-parent-buffer nil)
479 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
480 "Run \"notmuch show\" with the given thread ID and display results.
482 The optional PARENT-BUFFER is the notmuch-search buffer from
483 which this notmuch-show command was executed, (so that the
484 next thread from that buffer can be show when done with this
487 The optional QUERY-CONTEXT is a notmuch search term. Only
488 messages from the thread matching this search term are shown if
491 The optional BUFFER-NAME provides the neame of the buffer in
492 which the message thread is shown. If it is nil (which occurs
493 when the command is called interactively) the argument to the
495 (interactive "sNotmuch show: ")
496 (let ((buffer (get-buffer-create (generate-new-buffer-name
498 (concat "*notmuch-" thread-id "*")))))
499 (inhibit-read-only t))
500 (switch-to-buffer buffer)
502 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
504 (goto-char (point-min))
506 (let* ((basic-args (list thread-id))
507 (args (if query-context
508 (append (list "\'") basic-args (list "and (" query-context ")\'"))
509 (append (list "\'") basic-args (list "\'")))))
510 (notmuch-show-insert-forest (notmuch-query-get-threads args))
511 ;; If the query context reduced the results to nothing, run
513 (when (and (eq (buffer-size) 0)
515 (notmuch-show-insert-forest
516 (notmuch-query-get-threads basic-args))))
518 ;; Enable buttonisation of URLs and email addresses in the
520 (goto-address-mode t)
521 ;; Act on visual lines rather than logical lines.
524 (run-hooks 'notmuch-show-hook))
526 ;; Move straight to the first open message
527 (if (not (notmuch-show-message-visible-p))
528 (notmuch-show-next-open-message))
529 (notmuch-show-mark-read)))
531 (defvar notmuch-show-stash-map
532 (let ((map (make-sparse-keymap)))
533 (define-key map "c" 'notmuch-show-stash-cc)
534 (define-key map "d" 'notmuch-show-stash-date)
535 (define-key map "F" 'notmuch-show-stash-filename)
536 (define-key map "f" 'notmuch-show-stash-from)
537 (define-key map "i" 'notmuch-show-stash-message-id)
538 (define-key map "s" 'notmuch-show-stash-subject)
539 (define-key map "T" 'notmuch-show-stash-tags)
540 (define-key map "t" 'notmuch-show-stash-to)
542 "Submap for stash commands")
543 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
545 (defvar notmuch-show-mode-map
546 (let ((map (make-sparse-keymap)))
547 (define-key map "?" 'notmuch-help)
548 (define-key map "q" 'kill-this-buffer)
549 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
550 (define-key map (kbd "TAB") 'notmuch-show-next-button)
551 (define-key map "s" 'notmuch-search)
552 (define-key map "m" 'notmuch-mua-mail)
553 (define-key map "f" 'notmuch-show-forward-message)
554 (define-key map "r" 'notmuch-show-reply)
555 (define-key map "|" 'notmuch-show-pipe-message)
556 (define-key map "w" 'notmuch-show-save-attachments)
557 (define-key map "V" 'notmuch-show-view-raw-message)
558 (define-key map "v" 'notmuch-show-view-all-mime-parts)
559 (define-key map "c" 'notmuch-show-stash-map)
560 (define-key map "h" 'notmuch-show-toggle-headers)
561 (define-key map "-" 'notmuch-show-remove-tag)
562 (define-key map "+" 'notmuch-show-add-tag)
563 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
564 (define-key map "a" 'notmuch-show-archive-thread)
565 (define-key map "N" 'notmuch-show-next-message)
566 (define-key map "P" 'notmuch-show-previous-message)
567 (define-key map "n" 'notmuch-show-next-open-message)
568 (define-key map "p" 'notmuch-show-previous-open-message)
569 (define-key map (kbd "DEL") 'notmuch-show-rewind)
570 (define-key map " " 'notmuch-show-advance-and-archive)
571 (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
572 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
574 "Keymap for \"notmuch show\" buffers.")
575 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
578 (defun notmuch-show-mode ()
579 "Major mode for viewing a thread with notmuch.
581 This buffer contains the results of the \"notmuch show\" command
582 for displaying a single thread of email from your email archives.
584 By default, various components of email messages, (citations,
585 signatures, already-read messages), are hidden. You can make
586 these parts visible by clicking with the mouse button or by
587 pressing RET after positioning the cursor on a hidden part, (for
588 which \\[notmuch-show-next-button] and
589 \\[notmuch-show-previous-button] are helpful).
591 Reading the thread sequentially is well-supported by pressing
592 \\[notmuch-show-advance-and-archive]. This will scroll the
593 current message (if necessary), advance to the next message, or
594 advance to the next thread (if already on the last message of a
597 Other commands are available to read or manipulate the thread
598 more selectively, (such as '\\[notmuch-show-next-message]' and
599 '\\[notmuch-show-previous-message]' to advance to messages
600 without removing any tags, and '\\[notmuch-show-archive-thread]'
601 to archive an entire thread without scrolling through with
602 \\[notmuch-show-advance-and-archive]).
604 You can add or remove arbitary tags from the current message with
605 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
607 All currently available key bindings:
609 \\{notmuch-show-mode-map}"
611 (kill-all-local-variables)
612 (use-local-map notmuch-show-mode-map)
613 (setq major-mode 'notmuch-show-mode
614 mode-name "notmuch-show")
615 (setq buffer-read-only t))
617 (defun notmuch-show-move-to-message-top ()
618 (goto-char (notmuch-show-message-top)))
620 (defun notmuch-show-move-to-message-bottom ()
621 (goto-char (notmuch-show-message-bottom)))
623 (defun notmuch-show-message-adjust ()
626 ;; Movement related functions.
628 ;; There's some strangeness here where a text property applied to a
629 ;; region a->b is not found when point is at b. We walk backwards
630 ;; until finding the property.
631 (defun notmuch-show-message-extent ()
634 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
638 (defun notmuch-show-message-top ()
639 (car (notmuch-show-message-extent)))
641 (defun notmuch-show-message-bottom ()
642 (cdr (notmuch-show-message-extent)))
644 (defun notmuch-show-goto-message-next ()
645 (let ((start (point)))
646 (notmuch-show-move-to-message-bottom)
652 (defun notmuch-show-goto-message-previous ()
653 (notmuch-show-move-to-message-top)
657 (notmuch-show-move-to-message-top)
660 (defun notmuch-show-move-past-invisible-forward ()
661 (while (point-invisible-p)
664 (defun notmuch-show-move-past-invisible-backward ()
665 (while (point-invisible-p)
668 ;; Functions relating to the visibility of messages and their
671 (defun notmuch-show-element-visible (props visible-p spec-property)
672 (let ((spec (plist-get props spec-property)))
674 (remove-from-invisibility-spec spec)
675 (add-to-invisibility-spec spec))))
677 (defun notmuch-show-message-visible (props visible-p)
679 ;; When making the message visible, the headers may or not be
680 ;; visible. So we check that property separately.
681 (let ((headers-visible (plist-get props :headers-visible)))
682 (notmuch-show-element-visible props headers-visible :headers-invis-spec)
683 (notmuch-show-element-visible props t :message-invis-spec))
684 (notmuch-show-element-visible props nil :headers-invis-spec)
685 (notmuch-show-element-visible props nil :message-invis-spec))
687 (notmuch-show-set-prop :message-visible visible-p props))
689 (defun notmuch-show-headers-visible (props visible-p)
690 (if (plist-get props :message-visible)
691 (notmuch-show-element-visible props visible-p :headers-invis-spec))
692 (notmuch-show-set-prop :headers-visible visible-p props))
694 ;; Functions for setting and getting attributes of the current
697 (defun notmuch-show-set-message-properties (props)
699 (notmuch-show-move-to-message-top)
700 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
702 (defun notmuch-show-get-message-properties ()
704 (notmuch-show-move-to-message-top)
705 (get-text-property (point) :notmuch-message-properties)))
707 (defun notmuch-show-set-prop (prop val &optional props)
708 (let ((inhibit-read-only t)
710 (notmuch-show-get-message-properties))))
711 (plist-put props prop val)
712 (notmuch-show-set-message-properties props)))
714 (defun notmuch-show-get-prop (prop &optional props)
715 (let ((props (or props
716 (notmuch-show-get-message-properties))))
717 (plist-get props prop)))
719 (defun notmuch-show-get-message-id ()
720 "Return the message id of the current message."
721 (concat "id:" (notmuch-show-get-prop :id)))
723 ;; dme: Would it make sense to use a macro for many of these?
725 (defun notmuch-show-get-filename ()
726 "Return the filename of the current message."
727 (notmuch-show-get-prop :filename))
729 (defun notmuch-show-get-header (header)
730 "Return the named header of the current message, if any."
731 (plist-get (notmuch-show-get-prop :headers) header))
733 (defun notmuch-show-get-cc ()
734 (notmuch-show-get-header :Cc))
736 (defun notmuch-show-get-date ()
737 (notmuch-show-get-header :Date))
739 (defun notmuch-show-get-from ()
740 (notmuch-show-get-header :From))
742 (defun notmuch-show-get-subject ()
743 (notmuch-show-get-header :Subject))
745 (defun notmuch-show-get-to ()
746 (notmuch-show-get-header :To))
748 (defun notmuch-show-set-tags (tags)
749 "Set the tags of the current message."
750 (notmuch-show-set-prop :tags tags)
751 (notmuch-show-update-tags tags))
753 (defun notmuch-show-get-tags ()
754 "Return the tags of the current message."
755 (notmuch-show-get-prop :tags))
757 (defun notmuch-show-message-visible-p ()
758 "Is the current message visible?"
759 (notmuch-show-get-prop :message-visible))
761 (defun notmuch-show-headers-visible-p ()
762 "Are the headers of the current message visible?"
763 (notmuch-show-get-prop :headers-visible))
765 (defun notmuch-show-mark-read ()
766 "Mark the current message as read."
767 (notmuch-show-remove-tag "unread"))
769 ;; Commands typically bound to keys.
771 (defun notmuch-show-advance-and-archive ()
772 "Advance through thread and archive.
774 This command is intended to be one of the simplest ways to
775 process a thread of email. It does the following:
777 If the current message in the thread is not yet fully visible,
778 scroll by a near screenful to read more of the message.
780 Otherwise, (the end of the current message is already within the
781 current window), advance to the next open message.
783 Finally, if there is no further message to advance to, and this
784 last message is already read, then archive the entire current
785 thread, (remove the \"inbox\" tag from each message). Also kill
786 this buffer, and display the next thread from the search from
787 which this thread was originally shown."
789 (let ((end-of-this-message (notmuch-show-message-bottom)))
791 ;; Ideally we would test `end-of-this-message' against the result
792 ;; of `window-end', but that doesn't account for the fact that
793 ;; the end of the message might be hidden, so we have to actually
794 ;; go to the end, walk back over invisible text and then see if
797 (goto-char (- end-of-this-message 1))
798 (notmuch-show-move-past-invisible-backward)
799 (> (point) (window-end)))
800 ;; The bottom of this message is not visible - scroll.
803 ((not (= end-of-this-message (point-max)))
804 ;; This is not the last message - move to the next visible one.
805 (notmuch-show-next-open-message))
808 ;; This is the last message - archive the thread.
809 (notmuch-show-archive-thread)))))
811 (defun notmuch-show-rewind ()
812 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
814 Specifically, if the beginning of the previous email is fewer
815 than `window-height' lines from the current point, move to it
816 just like `notmuch-show-previous-message'.
818 Otherwise, just scroll down a screenful of the current message.
820 This command does not modify any message tags, (it does not undo
821 any effects from previous calls to
822 `notmuch-show-advance-and-archive'."
824 (let ((start-of-message (notmuch-show-message-top))
825 (start-of-window (window-start)))
827 ;; Either this message is properly aligned with the start of the
828 ;; window or the start of this message is not visible on the
830 ((or (= start-of-message start-of-window)
831 (< start-of-message start-of-window))
833 ;; If a small number of lines from the previous message are
834 ;; visible, realign so that the top of the current message is at
835 ;; the top of the screen.
836 (if (<= (count-screen-lines (window-start) start-of-message)
837 next-screen-context-lines)
839 (goto-char (notmuch-show-message-top))
840 (notmuch-show-message-adjust)))
841 ;; Move to the top left of the window.
842 (goto-char (window-start)))
844 ;; Move to the previous message.
845 (notmuch-show-previous-message)))))
847 (defun notmuch-show-reply ()
848 "Reply to the current message."
850 (notmuch-mua-reply (notmuch-show-get-message-id)))
852 (defun notmuch-show-forward-message ()
853 "Forward the current message."
855 (with-current-notmuch-show-message
856 (notmuch-mua-forward-message)))
858 (defun notmuch-show-next-message ()
859 "Show the next message."
861 (if (notmuch-show-goto-message-next)
863 (notmuch-show-mark-read)
864 (notmuch-show-message-adjust))
865 (goto-char (point-max))))
867 (defun notmuch-show-previous-message ()
868 "Show the previous message."
870 (notmuch-show-goto-message-previous)
871 (notmuch-show-mark-read)
872 (notmuch-show-message-adjust))
874 (defun notmuch-show-next-open-message ()
875 "Show the next message."
878 (while (and (setq r (notmuch-show-goto-message-next))
879 (not (notmuch-show-message-visible-p))))
882 (notmuch-show-mark-read)
883 (notmuch-show-message-adjust))
884 (goto-char (point-max)))))
886 (defun notmuch-show-previous-open-message ()
887 "Show the previous message."
889 (while (and (notmuch-show-goto-message-previous)
890 (not (notmuch-show-message-visible-p))))
891 (notmuch-show-mark-read)
892 (notmuch-show-message-adjust))
894 (defun notmuch-show-view-raw-message ()
895 "View the file holding the current message."
897 (view-file (notmuch-show-get-filename)))
899 (defun notmuch-show-pipe-message (command)
900 "Pipe the contents of the current message to the given command.
902 The given command will be executed with the raw contents of the
903 current email message as stdin. Anything printed by the command
904 to stdout or stderr will appear in the *Messages* buffer."
905 (interactive "sPipe message to command: ")
906 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
908 (shell-quote-argument (notmuch-show-get-filename)))))
910 (defun notmuch-show-add-tag (&rest toadd)
911 "Add a tag to the current message."
913 (list (notmuch-select-tag-with-completion "Tag to add: ")))
914 (apply 'notmuch-call-notmuch-process
916 (mapcar (lambda (s) (concat "+" s)) toadd))
917 (cons (notmuch-show-get-message-id) nil)))
918 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
920 (defun notmuch-show-remove-tag (&rest toremove)
921 "Remove a tag from the current message."
923 (list (notmuch-select-tag-with-completion
924 "Tag to remove: " (notmuch-show-get-message-id))))
925 (let ((tags (notmuch-show-get-tags)))
926 (if (intersection tags toremove :test 'string=)
928 (apply 'notmuch-call-notmuch-process
930 (mapcar (lambda (s) (concat "-" s)) toremove))
931 (cons (notmuch-show-get-message-id) nil)))
932 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
934 (defun notmuch-show-toggle-headers ()
935 "Toggle the visibility of the current message headers."
937 (let ((props (notmuch-show-get-message-properties)))
938 (notmuch-show-headers-visible
940 (not (plist-get props :headers-visible))))
941 (force-window-update))
943 (defun notmuch-show-toggle-message ()
944 "Toggle the visibility of the current message."
946 (let ((props (notmuch-show-get-message-properties)))
947 (notmuch-show-message-visible
949 (not (plist-get props :message-visible))))
950 (force-window-update))
952 (defun notmuch-show-open-or-close-all ()
953 "Set the visibility all of the messages in the current thread.
954 By default make all of the messages visible. With a prefix
955 argument, hide all of the messages."
958 (goto-char (point-min))
959 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
960 (not current-prefix-arg))
961 until (not (notmuch-show-goto-message-next))))
962 (force-window-update))
964 (defun notmuch-show-next-button ()
965 "Advance point to the next button in the buffer."
969 (defun notmuch-show-previous-button ()
970 "Move point back to the previous button in the buffer."
974 (defun notmuch-show-archive-thread-internal (show-next)
975 ;; Remove the tag from the current set of messages.
976 (goto-char (point-min))
977 (loop do (notmuch-show-remove-tag "inbox")
978 until (not (notmuch-show-goto-message-next)))
979 ;; Move to the next item in the search results, if any.
980 (let ((parent-buffer notmuch-show-parent-buffer))
984 (switch-to-buffer parent-buffer)
987 (notmuch-search-show-thread))))))
989 (defun notmuch-show-archive-thread ()
990 "Archive each message in thread, then show next thread from search.
992 Archive each message currently shown by removing the \"inbox\"
993 tag from each. Then kill this buffer and show the next thread
994 from the search from which this thread was originally shown.
996 Note: This command is safe from any race condition of new messages
997 being delivered to the same thread. It does not archive the
998 entire thread, but only the messages shown in the current
1001 (notmuch-show-archive-thread-internal t))
1003 (defun notmuch-show-archive-thread-then-exit ()
1004 "Archive each message in thread, then exit back to search results."
1006 (notmuch-show-archive-thread-internal nil))
1008 (defun notmuch-show-do-stash (text)
1010 (message "Saved: %s" text))
1012 (defun notmuch-show-stash-cc ()
1013 "Copy CC field of current message to kill-ring."
1015 (notmuch-show-do-stash (notmuch-show-get-cc)))
1017 (defun notmuch-show-stash-date ()
1018 "Copy date of current message to kill-ring."
1020 (notmuch-show-do-stash (notmuch-show-get-date)))
1022 (defun notmuch-show-stash-filename ()
1023 "Copy filename of current message to kill-ring."
1025 (notmuch-show-do-stash (notmuch-show-get-filename)))
1027 (defun notmuch-show-stash-from ()
1028 "Copy From address of current message to kill-ring."
1030 (notmuch-show-do-stash (notmuch-show-get-from)))
1032 (defun notmuch-show-stash-message-id ()
1033 "Copy message ID of current message to kill-ring."
1035 (notmuch-show-do-stash (notmuch-show-get-message-id)))
1037 (defun notmuch-show-stash-subject ()
1038 "Copy Subject field of current message to kill-ring."
1040 (notmuch-show-do-stash (notmuch-show-get-subject)))
1042 (defun notmuch-show-stash-tags ()
1043 "Copy tags of current message to kill-ring as a comma separated list."
1045 (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1047 (defun notmuch-show-stash-to ()
1048 "Copy To address of current message to kill-ring."
1050 (notmuch-show-do-stash (notmuch-show-get-to)))
1052 ;; Commands typically bound to buttons.
1054 (defun notmuch-show-part-button-action (button)
1055 (let ((nth (button-get button :notmuch-part)))
1057 (notmuch-show-save-part (notmuch-show-get-message-id) nth
1058 (button-get button :notmuch-filename))
1059 (message "Not a valid part (is it a fake part?)."))))
1063 (provide 'notmuch-show)