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 '(notmuch-show-pretty-hook)
66 "A list of functions called after populating a
67 `notmuch-show' buffer."
70 :options '(notmuch-show-pretty-hook
71 notmuch-show-turn-off-word-wrap))
73 (defcustom notmuch-show-insert-text/plain-hook
74 '(notmuch-wash-tidy-citations
75 notmuch-wash-compress-blanks
76 notmuch-wash-markup-citations)
77 "A list of functions called to clean up text/plain body parts."
80 :options '(notmuch-wash-wrap-long-lines
81 notmuch-wash-tidy-citations
82 notmuch-wash-compress-blanks
83 notmuch-wash-markup-citations))
85 (defun notmuch-show-pretty-hook ()
89 (defun notmuch-show-turn-off-word-wrap ()
90 ;; `toggle-word-wrap' outputs a message, which is distracting.
93 (defmacro with-current-notmuch-show-message (&rest body)
94 "Evaluate body with current buffer set to the text of current message"
96 (let ((filename (notmuch-show-get-filename)))
97 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
98 (with-current-buffer buf
99 (insert-file-contents filename nil nil nil t)
101 (kill-buffer buf)))))
103 (defun notmuch-show-view-all-mime-parts ()
104 "Use external viewers to view all attachments from the current message."
106 (with-current-notmuch-show-message
107 ; We ovverride the mm-inline-media-tests to indicate which message
108 ; parts are already sufficiently handled by the original
109 ; presentation of the message in notmuch-show mode. These parts
110 ; will be inserted directly into the temporary buffer of
111 ; with-current-notmuch-show-message and silently discarded.
113 ; Any MIME part not explicitly mentioned here will be handled by an
114 ; external viewer as configured in the various mailcap files.
115 (let ((mm-inline-media-tests '(
116 ("text/.*" ignore identity)
117 ("application/pgp-signature" ignore identity)
118 ("multipart/alternative" ignore identity)
119 ("multipart/mixed" ignore identity)
120 ("multipart/related" ignore identity)
122 (mm-display-parts (mm-dissect-buffer)))))
124 (defun notmuch-foreach-mime-part (function mm-handle)
125 (cond ((stringp (car mm-handle))
126 (dolist (part (cdr mm-handle))
127 (notmuch-foreach-mime-part function part)))
128 ((bufferp (car mm-handle))
129 (funcall function mm-handle))
130 (t (dolist (part mm-handle)
131 (notmuch-foreach-mime-part function part)))))
133 (defun notmuch-count-attachments (mm-handle)
135 (notmuch-foreach-mime-part
137 (let ((disposition (mm-handle-disposition p)))
138 (and (listp disposition)
139 (or (equal (car disposition) "attachment")
140 (and (equal (car disposition) "inline")
141 (assq 'filename disposition)))
146 (defun notmuch-save-attachments (mm-handle &optional queryp)
147 (notmuch-foreach-mime-part
149 (let ((disposition (mm-handle-disposition p)))
150 (and (listp disposition)
151 (or (equal (car disposition) "attachment")
152 (and (equal (car disposition) "inline")
153 (assq 'filename disposition)))
156 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
160 (defun notmuch-show-save-attachments ()
161 "Save all attachments from the current message."
163 (with-current-notmuch-show-message
164 (let ((mm-handle (mm-dissect-buffer)))
165 (notmuch-save-attachments
166 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
169 (defun notmuch-show-fontify-header ()
171 ((looking-at "[Tt]o:")
173 ((looking-at "[Bb]?[Cc][Cc]:")
175 ((looking-at "[Ss]ubject:")
176 'message-header-subject)
177 ((looking-at "[Ff]rom:")
178 'message-header-from)
180 'message-header-other))))
182 (overlay-put (make-overlay (point) (re-search-forward ":"))
183 'face 'message-header-name)
184 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
187 (defun notmuch-show-colour-headers ()
188 "Apply some colouring to the current headers."
189 (goto-char (point-min))
190 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
191 (notmuch-show-fontify-header)
194 (defun notmuch-show-spaces-n (n)
195 "Return a string comprised of `n' spaces."
198 (defun notmuch-show-update-tags (tags)
199 "Update the displayed tags of the current message."
201 (goto-char (notmuch-show-message-top))
202 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
203 (let ((inhibit-read-only t))
204 (replace-match (concat "("
205 (mapconcat 'identity tags " ")
208 (defun notmuch-show-insert-headerline (headers date tags depth)
209 "Insert a notmuch style headerline based on HEADERS for a
210 message at DEPTH in the current thread."
211 (let ((start (point)))
212 (insert (notmuch-show-spaces-n depth)
213 (plist-get headers :From)
217 (mapconcat 'identity tags " ")
219 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
221 (defun notmuch-show-insert-header (header header-value)
222 "Insert a single header."
223 (insert header ": " header-value "\n"))
225 (defun notmuch-show-insert-headers (headers)
226 "Insert the headers of the current message."
227 (let ((start (point)))
228 (mapc '(lambda (header)
229 (let* ((header-symbol (intern (concat ":" header)))
230 (header-value (plist-get headers header-symbol)))
231 (if (and header-value
232 (not (string-equal "" header-value)))
233 (notmuch-show-insert-header header header-value))))
234 notmuch-message-headers)
237 (narrow-to-region start (point-max))
238 (run-hooks 'notmuch-show-markup-headers-hook)))))
240 (define-button-type 'notmuch-show-part-button-type
241 'action 'notmuch-show-part-button-action
245 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name)
248 (if name (concat name ": ") "")
250 (if (not (string-equal declared-type content-type))
251 (concat " (as " content-type ")")
254 :type 'notmuch-show-part-button-type
256 :notmuch-filename name))
258 ;; Functions handling particular MIME parts.
260 (defun notmuch-show-save-part (message-id nth &optional filename)
262 ;; Always acquires the part via `notmuch part', even if it is
263 ;; available in the JSON output.
264 (insert (notmuch-show-get-bodypart-internal message-id nth))
265 (let ((file (read-file-name
266 "Filename to save as: "
267 (or mailcap-download-directory "~/")
270 (require-final-newline nil)
271 (coding-system-for-write 'no-conversion))
272 (write-region (point-min) (point-max) file))))
274 (defun notmuch-show-mm-display-part-inline (msg part content-type content)
275 "Use the mm-decode/mm-view functions to display a part in the
276 current buffer, if possible."
277 (let ((display-buffer (current-buffer)))
280 (let ((handle (mm-make-handle (current-buffer) (list content-type))))
281 (set-buffer display-buffer)
282 (if (and (mm-inlinable-p handle)
283 (mm-inlined-p handle))
285 (mm-display-part handle)
289 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
290 (let ((start (point)))
291 ;; If this text/plain part is not the first part in the message,
292 ;; insert a header to make this clear.
294 (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
295 (insert (notmuch-show-get-bodypart-content msg part nth))
298 (narrow-to-region start (point-max))
299 (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
302 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
303 ;; If we can deduce a MIME type from the filename of the attachment,
304 ;; do so and pass it on to the handler for that type.
305 (if (plist-get part :filename)
306 (let ((extension (file-name-extension (plist-get part :filename)))
310 (mailcap-parse-mimetypes)
311 (setq mime-type (mailcap-extension-to-mime extension))
313 (not (string-equal mime-type "application/octet-stream")))
314 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
318 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
319 ;; This handler _must_ succeed - it is the handler of last resort.
320 (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
321 (let ((content (notmuch-show-get-bodypart-content msg part nth)))
323 (notmuch-show-mm-display-part-inline msg part content-type content)))
326 ;; Functions for determining how to handle MIME parts.
328 (defun notmuch-show-split-content-type (content-type)
329 (split-string content-type "/"))
331 (defun notmuch-show-handlers-for (content-type)
332 "Return a list of content handlers for a part of type CONTENT-TYPE."
337 ;; Reverse order of prefrence.
338 (list (intern (concat "notmuch-show-insert-part-*/*"))
340 "notmuch-show-insert-part-"
341 (car (notmuch-show-split-content-type content-type))
343 (intern (concat "notmuch-show-insert-part-" content-type))))
346 ;; Helper for parts which are generally not included in the default
349 (defun notmuch-show-get-bodypart-internal (message-id part-number)
351 (let ((coding-system-for-read 'no-conversion))
352 (call-process notmuch-command nil t nil
353 "part" (format "--part=%s" part-number) message-id)
356 (defun notmuch-show-get-bodypart-content (msg part nth)
357 (or (plist-get part :content)
358 (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
362 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
363 (let ((handlers (notmuch-show-handlers-for content-type)))
364 ;; Run the content handlers until one of them returns a non-nil
367 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
368 (setq handlers (cdr handlers))))
371 (defun notmuch-show-insert-bodypart (msg part depth)
372 "Insert the body part PART at depth DEPTH in the current thread."
373 (let ((content-type (downcase (plist-get part :content-type)))
374 (nth (plist-get part :id)))
375 (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
376 ;; Some of the body part handlers leave point somewhere up in the
377 ;; part, so we make sure that we're down at the end.
378 (goto-char (point-max))
379 ;; Ensure that the part ends with a carriage return.
383 (defun notmuch-show-insert-body (msg body depth)
384 "Insert the body BODY at depth DEPTH in the current thread."
385 (mapc '(lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
387 (defun notmuch-show-make-symbol (type)
388 (make-symbol (concat "notmuch-show-" type)))
390 (defun notmuch-show-insert-msg (msg depth)
391 "Insert the message MSG at depth DEPTH in the current thread."
392 (let ((headers (plist-get msg :headers))
393 ;; Indentation causes the buffer offset of the start/end
394 ;; points to move, so we must use markers.
395 message-start message-end
396 content-start content-end
397 headers-start headers-end
399 (headers-invis-spec (notmuch-show-make-symbol "header"))
400 (message-invis-spec (notmuch-show-make-symbol "message")))
402 ;; Set `buffer-invisibility-spec' to `nil' (a list), otherwise
403 ;; removing items from `buffer-invisibility-spec' (which is what
404 ;; `notmuch-show-headers-visible' and
405 ;; `notmuch-show-message-visible' do) is a no-op and has no
406 ;; effect. This caused threads with only matching messages to have
407 ;; those messages hidden initially because
408 ;; `buffer-invisibility-spec' stayed `t'.
410 ;; This needs to be set here (rather than just above the call to
411 ;; `notmuch-show-headers-visible') because some of the part
412 ;; rendering or body washing functions
413 ;; (e.g. `notmuch-wash-text/plain-citations') manipulate
414 ;; `buffer-invisibility-spec').
415 (when (eq buffer-invisibility-spec t)
416 (setq buffer-invisibility-spec nil))
418 (setq message-start (point-marker))
420 (notmuch-show-insert-headerline headers
421 (or (plist-get msg :date_relative)
422 (plist-get headers :Date))
423 (plist-get msg :tags) depth)
425 (setq content-start (point-marker))
427 ;; Set `headers-start' to point after the 'Subject:' header to be
428 ;; compatible with the existing implementation. This just sets it
429 ;; to after the first header.
430 (notmuch-show-insert-headers headers)
431 ;; Headers should include a blank line (backwards compatibility).
434 (goto-char content-start)
436 (setq headers-start (point-marker)))
437 (setq headers-end (point-marker))
439 (setq body-start (point-marker))
440 (notmuch-show-insert-body msg (plist-get msg :body) depth)
441 ;; Ensure that the body ends with a newline.
444 (setq body-end (point-marker))
445 (setq content-end (point-marker))
447 ;; Indent according to the depth in the thread.
448 (indent-rigidly content-start content-end depth)
450 (setq message-end (point-max-marker))
452 ;; Save the extents of this message over the whole text of the
454 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
456 (plist-put msg :headers-invis-spec headers-invis-spec)
457 (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
459 (plist-put msg :message-invis-spec message-invis-spec)
460 (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
462 ;; Save the properties for this message. Currently this saves the
463 ;; entire message (augmented it with other stuff), which seems
464 ;; like overkill. We might save a reduced subset (for example, not
466 (notmuch-show-set-message-properties msg)
468 ;; Set header visibility.
469 (notmuch-show-headers-visible msg notmuch-message-headers-visible)
471 ;; Message visibility depends on whether it matched the search
473 (notmuch-show-message-visible msg (plist-get msg :match))))
475 (defun notmuch-show-insert-tree (tree depth)
476 "Insert the message tree TREE at depth DEPTH in the current thread."
477 (let ((msg (car tree))
478 (replies (cadr tree)))
479 (notmuch-show-insert-msg msg depth)
480 (notmuch-show-insert-thread replies (1+ depth))))
482 (defun notmuch-show-insert-thread (thread depth)
483 "Insert the thread THREAD at depth DEPTH in the current forest."
484 (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
486 (defun notmuch-show-insert-forest (forest)
487 "Insert the forest of threads FOREST."
488 (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
490 (defvar notmuch-show-parent-buffer nil)
493 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
494 "Run \"notmuch show\" with the given thread ID and display results.
496 The optional PARENT-BUFFER is the notmuch-search buffer from
497 which this notmuch-show command was executed, (so that the
498 next thread from that buffer can be show when done with this
501 The optional QUERY-CONTEXT is a notmuch search term. Only
502 messages from the thread matching this search term are shown if
505 The optional BUFFER-NAME provides the neame of the buffer in
506 which the message thread is shown. If it is nil (which occurs
507 when the command is called interactively) the argument to the
509 (interactive "sNotmuch show: ")
510 (let ((buffer (get-buffer-create (generate-new-buffer-name
512 (concat "*notmuch-" thread-id "*")))))
513 (inhibit-read-only t))
514 (switch-to-buffer buffer)
516 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
518 (goto-char (point-min))
520 (let* ((basic-args (list thread-id))
521 (args (if query-context
522 (append (list "\'") basic-args (list "and (" query-context ")\'"))
523 (append (list "\'") basic-args (list "\'")))))
524 (notmuch-show-insert-forest (notmuch-query-get-threads args))
525 ;; If the query context reduced the results to nothing, run
527 (when (and (eq (buffer-size) 0)
529 (notmuch-show-insert-forest
530 (notmuch-query-get-threads basic-args))))
531 (run-hooks 'notmuch-show-hook))
533 ;; Move straight to the first open message
534 (if (not (notmuch-show-message-visible-p))
535 (notmuch-show-next-open-message))
536 (notmuch-show-mark-read)))
538 (defvar notmuch-show-stash-map
539 (let ((map (make-sparse-keymap)))
540 (define-key map "c" 'notmuch-show-stash-cc)
541 (define-key map "d" 'notmuch-show-stash-date)
542 (define-key map "F" 'notmuch-show-stash-filename)
543 (define-key map "f" 'notmuch-show-stash-from)
544 (define-key map "i" 'notmuch-show-stash-message-id)
545 (define-key map "s" 'notmuch-show-stash-subject)
546 (define-key map "T" 'notmuch-show-stash-tags)
547 (define-key map "t" 'notmuch-show-stash-to)
549 "Submap for stash commands")
550 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
552 (defvar notmuch-show-mode-map
553 (let ((map (make-sparse-keymap)))
554 (define-key map "?" 'notmuch-help)
555 (define-key map "q" 'kill-this-buffer)
556 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
557 (define-key map (kbd "TAB") 'notmuch-show-next-button)
558 (define-key map "s" 'notmuch-search)
559 (define-key map "m" 'notmuch-mua-mail)
560 (define-key map "f" 'notmuch-show-forward-message)
561 (define-key map "r" 'notmuch-show-reply)
562 (define-key map "|" 'notmuch-show-pipe-message)
563 (define-key map "w" 'notmuch-show-save-attachments)
564 (define-key map "V" 'notmuch-show-view-raw-message)
565 (define-key map "v" 'notmuch-show-view-all-mime-parts)
566 (define-key map "c" 'notmuch-show-stash-map)
567 (define-key map "h" 'notmuch-show-toggle-headers)
568 (define-key map "-" 'notmuch-show-remove-tag)
569 (define-key map "+" 'notmuch-show-add-tag)
570 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
571 (define-key map "a" 'notmuch-show-archive-thread)
572 (define-key map "N" 'notmuch-show-next-message)
573 (define-key map "P" 'notmuch-show-previous-message)
574 (define-key map "n" 'notmuch-show-next-open-message)
575 (define-key map "p" 'notmuch-show-previous-open-message)
576 (define-key map (kbd "DEL") 'notmuch-show-rewind)
577 (define-key map " " 'notmuch-show-advance-and-archive)
578 (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
579 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
581 "Keymap for \"notmuch show\" buffers.")
582 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
585 (defun notmuch-show-mode ()
586 "Major mode for viewing a thread with notmuch.
588 This buffer contains the results of the \"notmuch show\" command
589 for displaying a single thread of email from your email archives.
591 By default, various components of email messages, (citations,
592 signatures, already-read messages), are hidden. You can make
593 these parts visible by clicking with the mouse button or by
594 pressing RET after positioning the cursor on a hidden part, (for
595 which \\[notmuch-show-next-button] and
596 \\[notmuch-show-previous-button] are helpful).
598 Reading the thread sequentially is well-supported by pressing
599 \\[notmuch-show-advance-and-archive]. This will scroll the
600 current message (if necessary), advance to the next message, or
601 advance to the next thread (if already on the last message of a
604 Other commands are available to read or manipulate the thread
605 more selectively, (such as '\\[notmuch-show-next-message]' and
606 '\\[notmuch-show-previous-message]' to advance to messages
607 without removing any tags, and '\\[notmuch-show-archive-thread]'
608 to archive an entire thread without scrolling through with
609 \\[notmuch-show-advance-and-archive]).
611 You can add or remove arbitary tags from the current message with
612 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
614 All currently available key bindings:
616 \\{notmuch-show-mode-map}"
618 (kill-all-local-variables)
619 (use-local-map notmuch-show-mode-map)
620 (setq major-mode 'notmuch-show-mode
621 mode-name "notmuch-show")
622 (setq buffer-read-only t))
624 (defun notmuch-show-move-to-message-top ()
625 (goto-char (notmuch-show-message-top)))
627 (defun notmuch-show-move-to-message-bottom ()
628 (goto-char (notmuch-show-message-bottom)))
630 (defun notmuch-show-message-adjust ()
633 ;; Movement related functions.
635 ;; There's some strangeness here where a text property applied to a
636 ;; region a->b is not found when point is at b. We walk backwards
637 ;; until finding the property.
638 (defun notmuch-show-message-extent ()
641 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
645 (defun notmuch-show-message-top ()
646 (car (notmuch-show-message-extent)))
648 (defun notmuch-show-message-bottom ()
649 (cdr (notmuch-show-message-extent)))
651 (defun notmuch-show-goto-message-next ()
652 (let ((start (point)))
653 (notmuch-show-move-to-message-bottom)
659 (defun notmuch-show-goto-message-previous ()
660 (notmuch-show-move-to-message-top)
664 (notmuch-show-move-to-message-top)
667 (defun notmuch-show-move-past-invisible-forward ()
668 (while (point-invisible-p)
671 (defun notmuch-show-move-past-invisible-backward ()
672 (while (point-invisible-p)
675 ;; Functions relating to the visibility of messages and their
678 (defun notmuch-show-element-visible (props visible-p spec-property)
679 (let ((spec (plist-get props spec-property)))
681 (remove-from-invisibility-spec spec)
682 (add-to-invisibility-spec spec))))
684 (defun notmuch-show-message-visible (props visible-p)
686 ;; When making the message visible, the headers may or not be
687 ;; visible. So we check that property separately.
688 (let ((headers-visible (plist-get props :headers-visible)))
689 (notmuch-show-element-visible props headers-visible :headers-invis-spec)
690 (notmuch-show-element-visible props t :message-invis-spec))
691 (notmuch-show-element-visible props nil :headers-invis-spec)
692 (notmuch-show-element-visible props nil :message-invis-spec))
694 (notmuch-show-set-prop :message-visible visible-p props))
696 (defun notmuch-show-headers-visible (props visible-p)
697 (if (plist-get props :message-visible)
698 (notmuch-show-element-visible props visible-p :headers-invis-spec))
699 (notmuch-show-set-prop :headers-visible visible-p props))
701 ;; Functions for setting and getting attributes of the current
704 (defun notmuch-show-set-message-properties (props)
706 (notmuch-show-move-to-message-top)
707 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
709 (defun notmuch-show-get-message-properties ()
711 (notmuch-show-move-to-message-top)
712 (get-text-property (point) :notmuch-message-properties)))
714 (defun notmuch-show-set-prop (prop val &optional props)
715 (let ((inhibit-read-only t)
717 (notmuch-show-get-message-properties))))
718 (plist-put props prop val)
719 (notmuch-show-set-message-properties props)))
721 (defun notmuch-show-get-prop (prop &optional props)
722 (let ((props (or props
723 (notmuch-show-get-message-properties))))
724 (plist-get props prop)))
726 (defun notmuch-show-get-message-id ()
727 "Return the message id of the current message."
728 (concat "id:" (notmuch-show-get-prop :id)))
730 ;; dme: Would it make sense to use a macro for many of these?
732 (defun notmuch-show-get-filename ()
733 "Return the filename of the current message."
734 (notmuch-show-get-prop :filename))
736 (defun notmuch-show-get-header (header)
737 "Return the named header of the current message, if any."
738 (plist-get (notmuch-show-get-prop :headers) header))
740 (defun notmuch-show-get-cc ()
741 (notmuch-show-get-header :Cc))
743 (defun notmuch-show-get-date ()
744 (notmuch-show-get-header :Date))
746 (defun notmuch-show-get-from ()
747 (notmuch-show-get-header :From))
749 (defun notmuch-show-get-subject ()
750 (notmuch-show-get-header :Subject))
752 (defun notmuch-show-get-to ()
753 (notmuch-show-get-header :To))
755 (defun notmuch-show-set-tags (tags)
756 "Set the tags of the current message."
757 (notmuch-show-set-prop :tags tags)
758 (notmuch-show-update-tags tags))
760 (defun notmuch-show-get-tags ()
761 "Return the tags of the current message."
762 (notmuch-show-get-prop :tags))
764 (defun notmuch-show-message-visible-p ()
765 "Is the current message visible?"
766 (notmuch-show-get-prop :message-visible))
768 (defun notmuch-show-headers-visible-p ()
769 "Are the headers of the current message visible?"
770 (notmuch-show-get-prop :headers-visible))
772 (defun notmuch-show-mark-read ()
773 "Mark the current message as read."
774 (notmuch-show-remove-tag "unread"))
776 ;; Commands typically bound to keys.
778 (defun notmuch-show-advance-and-archive ()
779 "Advance through thread and archive.
781 This command is intended to be one of the simplest ways to
782 process a thread of email. It does the following:
784 If the current message in the thread is not yet fully visible,
785 scroll by a near screenful to read more of the message.
787 Otherwise, (the end of the current message is already within the
788 current window), advance to the next open message.
790 Finally, if there is no further message to advance to, and this
791 last message is already read, then archive the entire current
792 thread, (remove the \"inbox\" tag from each message). Also kill
793 this buffer, and display the next thread from the search from
794 which this thread was originally shown."
796 (let ((end-of-this-message (notmuch-show-message-bottom)))
798 ;; Ideally we would test `end-of-this-message' against the result
799 ;; of `window-end', but that doesn't account for the fact that
800 ;; the end of the message might be hidden, so we have to actually
801 ;; go to the end, walk back over invisible text and then see if
804 (goto-char (- end-of-this-message 1))
805 (notmuch-show-move-past-invisible-backward)
806 (> (point) (window-end)))
807 ;; The bottom of this message is not visible - scroll.
810 ((not (= end-of-this-message (point-max)))
811 ;; This is not the last message - move to the next visible one.
812 (notmuch-show-next-open-message))
815 ;; This is the last message - archive the thread.
816 (notmuch-show-archive-thread)))))
818 (defun notmuch-show-rewind ()
819 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
821 Specifically, if the beginning of the previous email is fewer
822 than `window-height' lines from the current point, move to it
823 just like `notmuch-show-previous-message'.
825 Otherwise, just scroll down a screenful of the current message.
827 This command does not modify any message tags, (it does not undo
828 any effects from previous calls to
829 `notmuch-show-advance-and-archive'."
831 (let ((start-of-message (notmuch-show-message-top))
832 (start-of-window (window-start)))
834 ;; Either this message is properly aligned with the start of the
835 ;; window or the start of this message is not visible on the
837 ((or (= start-of-message start-of-window)
838 (< start-of-message start-of-window))
840 ;; If a small number of lines from the previous message are
841 ;; visible, realign so that the top of the current message is at
842 ;; the top of the screen.
843 (if (< (count-lines (window-start) (notmuch-show-message-top))
844 next-screen-context-lines)
846 (goto-char (notmuch-show-message-top))
847 (notmuch-show-message-adjust)))
848 ;; Move to the top left of the window.
849 (goto-char (window-start)))
851 ;; Move to the previous message.
852 (notmuch-show-previous-message)))))
854 (defun notmuch-show-reply ()
855 "Reply to the current message."
857 (notmuch-mua-reply (notmuch-show-get-message-id)))
859 (defun notmuch-show-forward-message ()
860 "Forward the current message."
862 (with-current-notmuch-show-message
863 (notmuch-mua-forward-message)))
865 (defun notmuch-show-next-message ()
866 "Show the next message."
868 (if (notmuch-show-goto-message-next)
870 (notmuch-show-mark-read)
871 (notmuch-show-message-adjust))
872 (goto-char (point-max))))
874 (defun notmuch-show-previous-message ()
875 "Show the previous message."
877 (notmuch-show-goto-message-previous)
878 (notmuch-show-mark-read)
879 (notmuch-show-message-adjust))
881 (defun notmuch-show-next-open-message ()
882 "Show the next message."
885 (while (and (setq r (notmuch-show-goto-message-next))
886 (not (notmuch-show-message-visible-p))))
889 (notmuch-show-mark-read)
890 (notmuch-show-message-adjust))
891 (goto-char (point-max)))))
893 (defun notmuch-show-previous-open-message ()
894 "Show the previous message."
896 (while (and (notmuch-show-goto-message-previous)
897 (not (notmuch-show-message-visible-p))))
898 (notmuch-show-mark-read)
899 (notmuch-show-message-adjust))
901 (defun notmuch-show-view-raw-message ()
902 "View the file holding the current message."
904 (view-file (notmuch-show-get-filename)))
906 (defun notmuch-show-pipe-message (command)
907 "Pipe the contents of the current message to the given command.
909 The given command will be executed with the raw contents of the
910 current email message as stdin. Anything printed by the command
911 to stdout or stderr will appear in the *Messages* buffer."
912 (interactive "sPipe message to command: ")
913 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
915 (shell-quote-argument (notmuch-show-get-filename)))))
917 (defun notmuch-show-add-tag (&rest toadd)
918 "Add a tag to the current message."
920 (list (notmuch-select-tag-with-completion "Tag to add: ")))
921 (apply 'notmuch-call-notmuch-process
923 (mapcar (lambda (s) (concat "+" s)) toadd))
924 (cons (notmuch-show-get-message-id) nil)))
925 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
927 (defun notmuch-show-remove-tag (&rest toremove)
928 "Remove a tag from the current message."
930 (list (notmuch-select-tag-with-completion
931 "Tag to remove: " (notmuch-show-get-message-id))))
932 (let ((tags (notmuch-show-get-tags)))
933 (if (intersection tags toremove :test 'string=)
935 (apply 'notmuch-call-notmuch-process
937 (mapcar (lambda (s) (concat "-" s)) toremove))
938 (cons (notmuch-show-get-message-id) nil)))
939 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
941 (defun notmuch-show-toggle-headers ()
942 "Toggle the visibility of the current message headers."
944 (let ((props (notmuch-show-get-message-properties)))
945 (notmuch-show-headers-visible
947 (not (plist-get props :headers-visible))))
948 (force-window-update))
950 (defun notmuch-show-toggle-message ()
951 "Toggle the visibility of the current message."
953 (let ((props (notmuch-show-get-message-properties)))
954 (notmuch-show-message-visible
956 (not (plist-get props :message-visible))))
957 (force-window-update))
959 (defun notmuch-show-open-or-close-all ()
960 "Set the visibility all of the messages in the current thread.
961 By default make all of the messages visible. With a prefix
962 argument, hide all of the messages."
965 (goto-char (point-min))
966 (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
967 (not current-prefix-arg))
968 until (not (notmuch-show-goto-message-next))))
969 (force-window-update))
971 (defun notmuch-show-next-button ()
972 "Advance point to the next button in the buffer."
976 (defun notmuch-show-previous-button ()
977 "Move point back to the previous button in the buffer."
981 (defun notmuch-show-archive-thread-internal (show-next)
982 ;; Remove the tag from the current set of messages.
983 (goto-char (point-min))
984 (loop do (notmuch-show-remove-tag "inbox")
985 until (not (notmuch-show-goto-message-next)))
986 ;; Move to the next item in the search results, if any.
987 (let ((parent-buffer notmuch-show-parent-buffer))
991 (switch-to-buffer parent-buffer)
994 (notmuch-search-show-thread))))))
996 (defun notmuch-show-archive-thread ()
997 "Archive each message in thread, then show next thread from search.
999 Archive each message currently shown by removing the \"inbox\"
1000 tag from each. Then kill this buffer and show the next thread
1001 from the search from which this thread was originally shown.
1003 Note: This command is safe from any race condition of new messages
1004 being delivered to the same thread. It does not archive the
1005 entire thread, but only the messages shown in the current
1008 (notmuch-show-archive-thread-internal t))
1010 (defun notmuch-show-archive-thread-then-exit ()
1011 "Archive each message in thread, then exit back to search results."
1013 (notmuch-show-archive-thread-internal nil))
1015 (defun notmuch-show-do-stash (text)
1017 (message "Saved: %s" text))
1019 (defun notmuch-show-stash-cc ()
1020 "Copy CC field of current message to kill-ring."
1022 (notmuch-show-do-stash (notmuch-show-get-cc)))
1024 (defun notmuch-show-stash-date ()
1025 "Copy date of current message to kill-ring."
1027 (notmuch-show-do-stash (notmuch-show-get-date)))
1029 (defun notmuch-show-stash-filename ()
1030 "Copy filename of current message to kill-ring."
1032 (notmuch-show-do-stash (notmuch-show-get-filename)))
1034 (defun notmuch-show-stash-from ()
1035 "Copy From address of current message to kill-ring."
1037 (notmuch-show-do-stash (notmuch-show-get-from)))
1039 (defun notmuch-show-stash-message-id ()
1040 "Copy message ID of current message to kill-ring."
1042 (notmuch-show-do-stash (notmuch-show-get-message-id)))
1044 (defun notmuch-show-stash-subject ()
1045 "Copy Subject field of current message to kill-ring."
1047 (notmuch-show-do-stash (notmuch-show-get-subject)))
1049 (defun notmuch-show-stash-tags ()
1050 "Copy tags of current message to kill-ring as a comma separated list."
1052 (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1054 (defun notmuch-show-stash-to ()
1055 "Copy To address of current message to kill-ring."
1057 (notmuch-show-do-stash (notmuch-show-get-to)))
1059 ;; Commands typically bound to buttons.
1061 (defun notmuch-show-part-button-action (button)
1062 (let ((nth (button-get button :notmuch-part)))
1064 (notmuch-show-save-part (notmuch-show-get-message-id) nth
1065 (button-get button :notmuch-filename))
1066 (message "Not a valid part (is it a fake part?)."))))
1070 (provide 'notmuch-show)