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)
34 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
35 (declare-function notmuch-reply "notmuch" (query-string))
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 (defvar notmuch-show-headers '("Subject" "To" "Cc" "From" "Date")
41 "Headers that should be shown in a message, in this order. Note
42 that if this order is changed the headers shown when a message is
43 collapsed will change.")
45 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
46 "A list of functions called to decorate the headers listed in
47 `notmuch-show-headers'.")
49 (defvar notmuch-show-hook '(notmuch-show-pretty-hook)
50 "A list of functions called after populating a
51 `notmuch-show' buffer.")
53 (defvar notmuch-show-insert-text/plain-hook '(notmuch-wash-text/plain-citations)
54 "A list of functions called to clean up text/plain body parts.")
56 (defun notmuch-show-pretty-hook ()
60 (defmacro with-current-notmuch-show-message (&rest body)
61 "Evaluate body with current buffer set to the text of current message"
63 (let ((filename (notmuch-show-get-filename)))
64 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
65 (with-current-buffer buf
66 (insert-file-contents filename nil nil nil t)
70 (defun notmuch-show-view-all-mime-parts ()
71 "Use external viewers to view all attachments from the current message."
73 (with-current-notmuch-show-message
74 ; We ovverride the mm-inline-media-tests to indicate which message
75 ; parts are already sufficiently handled by the original
76 ; presentation of the message in notmuch-show mode. These parts
77 ; will be inserted directly into the temporary buffer of
78 ; with-current-notmuch-show-message and silently discarded.
80 ; Any MIME part not explicitly mentioned here will be handled by an
81 ; external viewer as configured in the various mailcap files.
82 (let ((mm-inline-media-tests '(
83 ("text/.*" ignore identity)
84 ("application/pgp-signature" ignore identity)
85 ("multipart/alternative" ignore identity)
86 ("multipart/mixed" ignore identity)
87 ("multipart/related" ignore identity)
89 (mm-display-parts (mm-dissect-buffer)))))
91 (defun notmuch-foreach-mime-part (function mm-handle)
92 (cond ((stringp (car mm-handle))
93 (dolist (part (cdr mm-handle))
94 (notmuch-foreach-mime-part function part)))
95 ((bufferp (car mm-handle))
96 (funcall function mm-handle))
97 (t (dolist (part mm-handle)
98 (notmuch-foreach-mime-part function part)))))
100 (defun notmuch-count-attachments (mm-handle)
102 (notmuch-foreach-mime-part
104 (let ((disposition (mm-handle-disposition p)))
105 (and (listp disposition)
106 (or (equal (car disposition) "attachment")
107 (and (equal (car disposition) "inline")
108 (assq 'filename disposition)))
113 (defun notmuch-save-attachments (mm-handle &optional queryp)
114 (notmuch-foreach-mime-part
116 (let ((disposition (mm-handle-disposition p)))
117 (and (listp disposition)
118 (or (equal (car disposition) "attachment")
119 (and (equal (car disposition) "inline")
120 (assq 'filename disposition)))
123 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
127 (defun notmuch-show-save-attachments ()
128 "Save all attachments from the current message."
130 (with-current-notmuch-show-message
131 (let ((mm-handle (mm-dissect-buffer)))
132 (notmuch-save-attachments
133 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
136 (defun notmuch-show-fontify-header ()
138 ((looking-at "[Tt]o:")
140 ((looking-at "[Bb]?[Cc][Cc]:")
142 ((looking-at "[Ss]ubject:")
143 'message-header-subject)
144 ((looking-at "[Ff]rom:")
145 'message-header-from)
147 'message-header-other))))
149 (overlay-put (make-overlay (point) (re-search-forward ":"))
150 'face 'message-header-name)
151 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
154 (defun notmuch-show-colour-headers ()
155 "Apply some colouring to the current headers."
156 (goto-char (point-min))
157 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
158 (notmuch-show-fontify-header)
161 (defun notmuch-show-spaces-n (n)
162 "Return a string comprised of `n' spaces."
165 (defun notmuch-show-update-tags (tags)
166 "Update the displayed tags of the current message."
168 (goto-char (notmuch-show-message-top))
169 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
170 (let ((inhibit-read-only t))
171 (replace-match (concat "("
172 (mapconcat 'identity tags " ")
175 (defun notmuch-show-insert-headerline (headers date tags depth)
176 "Insert a notmuch style headerline based on HEADERS for a
177 message at DEPTH in the current thread."
178 (let ((start (point)))
179 (insert (notmuch-show-spaces-n depth)
180 (plist-get headers :From)
184 (mapconcat 'identity tags " ")
186 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
188 (defun notmuch-show-insert-header (header header-value)
189 "Insert a single header."
190 (insert header ": " header-value "\n"))
192 (defun notmuch-show-insert-headers (headers)
193 "Insert the headers of the current message."
194 (let ((start (point)))
195 (mapc '(lambda (header)
196 (let* ((header-symbol (intern (concat ":" header)))
197 (header-value (plist-get headers header-symbol)))
198 (if (and header-value
199 (not (string-equal "" header-value)))
200 (notmuch-show-insert-header header header-value))))
201 notmuch-show-headers)
204 (narrow-to-region start (point-max))
205 (run-hooks 'notmuch-show-markup-headers-hook)))))
207 (defun notmuch-show-insert-part-header (content-type declared-type &optional name)
208 (let ((start (point)))
209 ;; XXX dme: Make this a more useful button (save the part, display
212 (if name (concat name ": ") "")
214 (if (not (string-equal declared-type content-type))
215 (concat " (as " content-type ")")
218 (overlay-put (make-overlay start (point)) 'face 'bold)))
220 ;; Functions handling particular MIME parts.
222 (defun notmuch-show-mm-display-part-inline (part content-type)
223 "Use the mm-decode/mm-view functions to display a part inline, if possible."
224 (let ((handle (mm-make-handle nil (list content-type))))
225 (if (and (mm-inlinable-p handle)
226 (mm-inlined-p handle))
228 (insert (with-temp-buffer
229 (let ((display-buffer (current-buffer)))
231 (let ((work-buffer (current-buffer)))
232 (insert (plist-get part :content))
233 (set-buffer display-buffer)
234 (mm-display-part (mm-make-handle work-buffer
235 (list content-type)))
240 (defun notmuch-show-insert-part-text/plain (part content-type nth depth declared-type)
241 (let ((start (point)))
242 ;; If this text/plain part is not the first part in the message,
243 ;; insert a header to make this clear.
245 (notmuch-show-insert-part-header declared-type content-type (plist-get part :filename)))
246 (insert (plist-get part :content))
249 (narrow-to-region start (point-max))
250 (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
253 (defun notmuch-show-insert-part-application/octet-stream (part content-type nth depth declared-type)
254 ;; If we can deduce a MIME type from the filename of the attachment,
255 ;; do so and pass it on to the handler for that type.
256 (if (plist-get part :filename)
257 (let ((extension (file-name-extension (plist-get part :filename)))
261 (mailcap-parse-mimetypes)
262 (setq mime-type (mailcap-extension-to-mime extension))
264 (not (string-equal mime-type "application/octet-stream")))
265 (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
269 (defun notmuch-show-insert-part-*/* (part content-type nth depth declared-type)
270 (notmuch-show-insert-part-header content-type declared-type (plist-get part :filename))
271 ;; If we have the content for the part, attempt to inline it.
272 (if (plist-get part :content)
273 (notmuch-show-mm-display-part-inline part content-type))
276 ;; Functions for determining how to handle MIME parts.
278 (defun notmuch-show-split-content-type (content-type)
279 (split-string content-type "/"))
281 (defun notmuch-show-handlers-for (content-type)
282 "Return a list of content handlers for a part of type CONTENT-TYPE."
287 ;; Reverse order of prefrence.
288 (list (intern (concat "notmuch-show-insert-part-*/*"))
290 "notmuch-show-insert-part-"
291 (car (notmuch-show-split-content-type content-type))
293 (intern (concat "notmuch-show-insert-part-" content-type))))
298 (defun notmuch-show-insert-bodypart-internal (part content-type nth depth declared-type)
299 (let ((handlers (notmuch-show-handlers-for content-type)))
300 ;; Run the content handlers until one of them returns a non-nil
303 (not (funcall (car handlers) part content-type nth depth declared-type)))
304 (setq handlers (cdr handlers))))
307 (defun notmuch-show-insert-bodypart (part depth)
308 "Insert the body part PART at depth DEPTH in the current thread."
309 (let ((content-type (downcase (plist-get part :content-type)))
310 (nth (plist-get part :id)))
311 (notmuch-show-insert-bodypart-internal part content-type nth depth content-type))
312 ;; Ensure that the part ends with a carriage return.
317 (defun notmuch-show-insert-body (body depth)
318 "Insert the body BODY at depth DEPTH in the current thread."
319 (mapc '(lambda (part) (notmuch-show-insert-bodypart part depth)) body))
321 (defun notmuch-show-make-symbol (type)
322 (make-symbol (concat "notmuch-show-" type)))
324 (defun notmuch-show-insert-msg (msg depth)
325 "Insert the message MSG at depth DEPTH in the current thread."
326 (let ((headers (plist-get msg :headers))
327 ;; Indentation causes the buffer offset of the start/end
328 ;; points to move, so we must use markers.
329 message-start message-end
330 content-start content-end
331 headers-start headers-end
333 (headers-invis-spec (notmuch-show-make-symbol "header"))
334 (message-invis-spec (notmuch-show-make-symbol "message")))
336 (setq message-start (point-marker))
338 (notmuch-show-insert-headerline headers
339 (or (plist-get msg :date_relative)
340 (plist-get headers :Date))
341 (plist-get msg :tags) depth)
343 (setq content-start (point-marker))
345 ;; Set `headers-start' to point after the 'Subject:' header to be
346 ;; compatible with the existing implementation. This just sets it
347 ;; to after the first header.
348 (notmuch-show-insert-headers headers)
349 ;; Headers should include a blank line (backwards compatibility).
352 (goto-char content-start)
354 (setq headers-start (point-marker)))
355 (setq headers-end (point-marker))
357 (setq body-start (point-marker))
358 (notmuch-show-insert-body (plist-get msg :body) depth)
359 ;; Ensure that the body ends with a newline.
362 (setq body-end (point-marker))
363 (setq content-end (point-marker))
365 ;; Indent according to the depth in the thread.
366 (indent-rigidly content-start content-end depth)
368 (setq message-end (point-max-marker))
370 ;; Save the extents of this message over the whole text of the
372 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
374 (plist-put msg :headers-invis-spec headers-invis-spec)
375 (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
377 (plist-put msg :message-invis-spec message-invis-spec)
378 (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
380 ;; Save the properties for this message. Currently this saves the
381 ;; entire message (augmented it with other stuff), which seems
382 ;; like overkill. We might save a reduced subset (for example, not
384 (notmuch-show-set-message-properties msg)
386 ;; Headers are hidden by default.
387 (notmuch-show-headers-visible msg nil)
389 ;; Message visibility depends on whether it matched the search
391 (notmuch-show-message-visible msg (plist-get msg :match))))
393 (defun notmuch-show-insert-tree (tree depth)
394 "Insert the message tree TREE at depth DEPTH in the current thread."
395 (let ((msg (car tree))
396 (replies (cadr tree)))
397 (notmuch-show-insert-msg msg depth)
398 (notmuch-show-insert-thread replies (1+ depth))))
400 (defun notmuch-show-insert-thread (thread depth)
401 "Insert the thread THREAD at depth DEPTH in the current forest."
402 (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
404 (defun notmuch-show-insert-forest (forest)
405 "Insert the forest of threads FOREST."
406 (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
408 (defvar notmuch-show-parent-buffer nil)
411 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
412 "Run \"notmuch show\" with the given thread ID and display results.
414 The optional PARENT-BUFFER is the notmuch-search buffer from
415 which this notmuch-show command was executed, (so that the
416 next thread from that buffer can be show when done with this
419 The optional QUERY-CONTEXT is a notmuch search term. Only
420 messages from the thread matching this search term are shown if
423 The optional BUFFER-NAME provides the neame of the buffer in
424 which the message thread is shown. If it is nil (which occurs
425 when the command is called interactively) the argument to the
427 (interactive "sNotmuch show: ")
428 (let ((buffer (get-buffer-create (generate-new-buffer-name
430 (concat "*notmuch-" thread-id "*")))))
431 (inhibit-read-only t))
432 (switch-to-buffer buffer)
434 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
436 (goto-char (point-min))
438 (let* ((basic-args (list thread-id))
439 (args (if query-context
440 (append basic-args (list "and (" query-context ")"))
442 (notmuch-show-insert-forest (notmuch-query-get-threads args))
443 ;; If the query context reduced the results to nothing, run
445 (when (and (eq (buffer-size) 0)
447 (notmuch-show-insert-forest
448 (notmuch-query-get-threads basic-args))))
449 (run-hooks 'notmuch-show-hook))
451 ;; Move straight to the first open message
452 (if (not (notmuch-show-message-visible-p))
453 (notmuch-show-next-open-message))
454 (notmuch-show-mark-read)))
456 (defvar notmuch-show-stash-map
457 (let ((map (make-sparse-keymap)))
458 (define-key map "c" 'notmuch-show-stash-cc)
459 (define-key map "d" 'notmuch-show-stash-date)
460 (define-key map "F" 'notmuch-show-stash-filename)
461 (define-key map "f" 'notmuch-show-stash-from)
462 (define-key map "i" 'notmuch-show-stash-message-id)
463 (define-key map "s" 'notmuch-show-stash-subject)
464 (define-key map "T" 'notmuch-show-stash-tags)
465 (define-key map "t" 'notmuch-show-stash-to)
467 "Submap for stash commands")
468 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
470 (defvar notmuch-show-mode-map
471 (let ((map (make-sparse-keymap)))
472 (define-key map "?" 'notmuch-help)
473 (define-key map "q" 'kill-this-buffer)
474 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
475 (define-key map (kbd "TAB") 'notmuch-show-next-button)
476 (define-key map "s" 'notmuch-search)
477 (define-key map "m" 'message-mail)
478 (define-key map "f" 'notmuch-show-forward-message)
479 (define-key map "r" 'notmuch-show-reply)
480 (define-key map "|" 'notmuch-show-pipe-message)
481 (define-key map "w" 'notmuch-show-save-attachments)
482 (define-key map "V" 'notmuch-show-view-raw-message)
483 (define-key map "v" 'notmuch-show-view-all-mime-parts)
484 (define-key map "c" 'notmuch-show-stash-map)
485 (define-key map "h" 'notmuch-show-toggle-headers)
486 (define-key map "-" 'notmuch-show-remove-tag)
487 (define-key map "+" 'notmuch-show-add-tag)
488 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
489 (define-key map "a" 'notmuch-show-archive-thread)
490 (define-key map "N" 'notmuch-show-next-message)
491 (define-key map "P" 'notmuch-show-previous-message)
492 (define-key map "n" 'notmuch-show-next-open-message)
493 (define-key map "p" 'notmuch-show-previous-open-message)
494 (define-key map (kbd "DEL") 'notmuch-show-rewind)
495 (define-key map " " 'notmuch-show-advance-and-archive)
496 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
498 "Keymap for \"notmuch show\" buffers.")
499 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
502 (defun notmuch-show-mode ()
503 "Major mode for viewing a thread with notmuch.
505 This buffer contains the results of the \"notmuch show\" command
506 for displaying a single thread of email from your email archives.
508 By default, various components of email messages, (citations,
509 signatures, already-read messages), are hidden. You can make
510 these parts visible by clicking with the mouse button or by
511 pressing RET after positioning the cursor on a hidden part, (for
512 which \\[notmuch-show-next-button] and
513 \\[notmuch-show-previous-button] are helpful).
515 Reading the thread sequentially is well-supported by pressing
516 \\[notmuch-show-advance-and-archive]. This will scroll the
517 current message (if necessary), advance to the next message, or
518 advance to the next thread (if already on the last message of a
521 Other commands are available to read or manipulate the thread
522 more selectively, (such as '\\[notmuch-show-next-message]' and
523 '\\[notmuch-show-previous-message]' to advance to messages
524 without removing any tags, and '\\[notmuch-show-archive-thread]'
525 to archive an entire thread without scrolling through with
526 \\[notmuch-show-advance-and-archive]).
528 You can add or remove arbitary tags from the current message with
529 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
531 All currently available key bindings:
533 \\{notmuch-show-mode-map}"
535 (kill-all-local-variables)
536 (use-local-map notmuch-show-mode-map)
537 (setq major-mode 'notmuch-show-mode
538 mode-name "notmuch-show")
539 (setq buffer-read-only t))
541 (defun notmuch-show-move-to-message-top ()
542 (goto-char (notmuch-show-message-top)))
544 (defun notmuch-show-move-to-message-bottom ()
545 (goto-char (notmuch-show-message-bottom)))
547 (defun notmuch-show-message-adjust ()
550 ;; Movement related functions.
552 ;; There's some strangeness here where a text property applied to a
553 ;; region a->b is not found when point is at b. We walk backwards
554 ;; until finding the property.
555 (defun notmuch-show-message-extent ()
558 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
562 (defun notmuch-show-message-top ()
563 (car (notmuch-show-message-extent)))
565 (defun notmuch-show-message-bottom ()
566 (cdr (notmuch-show-message-extent)))
568 (defun notmuch-show-goto-message-next ()
569 (let ((start (point)))
570 (notmuch-show-move-to-message-bottom)
576 (defun notmuch-show-goto-message-previous ()
577 (notmuch-show-move-to-message-top)
581 (notmuch-show-move-to-message-top)
584 (defun notmuch-show-move-past-invisible-forward ()
585 (while (point-invisible-p)
588 (defun notmuch-show-move-past-invisible-backward ()
589 (while (point-invisible-p)
592 ;; Functions relating to the visibility of messages and their
595 (defun notmuch-show-element-visible (props visible-p spec-property)
596 (let ((spec (plist-get props spec-property)))
598 (remove-from-invisibility-spec spec)
599 (add-to-invisibility-spec spec))))
601 (defun notmuch-show-message-visible (props visible-p)
603 ;; When making the message visible, the headers may or not be
604 ;; visible. So we check that property separately.
605 (let ((headers-visible (plist-get props :headers-visible)))
606 (notmuch-show-element-visible props headers-visible :headers-invis-spec)
607 (notmuch-show-element-visible props t :message-invis-spec))
608 (notmuch-show-element-visible props nil :headers-invis-spec)
609 (notmuch-show-element-visible props nil :message-invis-spec))
611 (notmuch-show-set-prop :message-visible visible-p props))
613 (defun notmuch-show-headers-visible (props visible-p)
614 (if (plist-get props :message-visible)
615 (notmuch-show-element-visible props visible-p :headers-invis-spec))
616 (notmuch-show-set-prop :headers-visible visible-p props))
618 ;; Functions for setting and getting attributes of the current
621 (defun notmuch-show-set-message-properties (props)
623 (notmuch-show-move-to-message-top)
624 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
626 (defun notmuch-show-get-message-properties ()
628 (notmuch-show-move-to-message-top)
629 (get-text-property (point) :notmuch-message-properties)))
631 (defun notmuch-show-set-prop (prop val &optional props)
632 (let ((inhibit-read-only t)
634 (notmuch-show-get-message-properties))))
635 (plist-put props prop val)
636 (notmuch-show-set-message-properties props)))
638 (defun notmuch-show-get-prop (prop &optional props)
639 (let ((props (or props
640 (notmuch-show-get-message-properties))))
641 (plist-get props prop)))
643 (defun notmuch-show-get-message-id ()
644 "Return the message id of the current message."
645 (concat "id:" (notmuch-show-get-prop :id)))
647 ;; dme: Would it make sense to use a macro for many of these?
649 (defun notmuch-show-get-filename ()
650 "Return the filename of the current message."
651 (notmuch-show-get-prop :filename))
653 (defun notmuch-show-get-header (header)
654 "Return the named header of the current message, if any."
655 (plist-get (notmuch-show-get-prop :headers) header))
657 (defun notmuch-show-get-cc ()
658 (notmuch-show-get-header :Cc))
660 (defun notmuch-show-get-date ()
661 (notmuch-show-get-header :Date))
663 (defun notmuch-show-get-from ()
664 (notmuch-show-get-header :From))
666 (defun notmuch-show-get-subject ()
667 (notmuch-show-get-header :Subject))
669 (defun notmuch-show-get-to ()
670 (notmuch-show-get-header :To))
672 (defun notmuch-show-set-tags (tags)
673 "Set the tags of the current message."
674 (notmuch-show-set-prop :tags tags)
675 (notmuch-show-update-tags tags))
677 (defun notmuch-show-get-tags ()
678 "Return the tags of the current message."
679 (notmuch-show-get-prop :tags))
681 (defun notmuch-show-message-visible-p ()
682 "Is the current message visible?"
683 (notmuch-show-get-prop :message-visible))
685 (defun notmuch-show-headers-visible-p ()
686 "Are the headers of the current message visible?"
687 (notmuch-show-get-prop :headers-visible))
689 (defun notmuch-show-mark-read ()
690 "Mark the current message as read."
691 (notmuch-show-remove-tag "unread"))
693 ;; Commands typically bound to keys.
695 (defun notmuch-show-advance-and-archive ()
696 "Advance through thread and archive.
698 This command is intended to be one of the simplest ways to
699 process a thread of email. It does the following:
701 If the current message in the thread is not yet fully visible,
702 scroll by a near screenful to read more of the message.
704 Otherwise, (the end of the current message is already within the
705 current window), advance to the next open message.
707 Finally, if there is no further message to advance to, and this
708 last message is already read, then archive the entire current
709 thread, (remove the \"inbox\" tag from each message). Also kill
710 this buffer, and display the next thread from the search from
711 which this thread was originally shown."
713 (let ((end-of-this-message (notmuch-show-message-bottom)))
715 ;; Ideally we would test `end-of-this-message' against the result
716 ;; of `window-end', but that doesn't account for the fact that
717 ;; the end of the message might be hidden, so we have to actually
718 ;; go to the end, walk back over invisible text and then see if
721 (goto-char (- end-of-this-message 1))
722 (notmuch-show-move-past-invisible-backward)
723 (> (point) (window-end)))
724 ;; The bottom of this message is not visible - scroll.
727 ((not (= end-of-this-message (point-max)))
728 ;; This is not the last message - move to the next visible one.
729 (notmuch-show-next-open-message))
732 ;; This is the last message - archive the thread.
733 (notmuch-show-archive-thread)))))
735 (defun notmuch-show-rewind ()
736 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
738 Specifically, if the beginning of the previous email is fewer
739 than `window-height' lines from the current point, move to it
740 just like `notmuch-show-previous-message'.
742 Otherwise, just scroll down a screenful of the current message.
744 This command does not modify any message tags, (it does not undo
745 any effects from previous calls to
746 `notmuch-show-advance-and-archive'."
748 (let ((start-of-message (notmuch-show-message-top))
749 (start-of-window (window-start)))
751 ;; Either this message is properly aligned with the start of the
752 ;; window or the start of this message is not visible on the
754 ((or (= start-of-message start-of-window)
755 (< start-of-message start-of-window))
757 ;; If a small number of lines from the previous message are
758 ;; visible, realign so that the top of the current message is at
759 ;; the top of the screen.
760 (if (< (count-lines (window-start) (notmuch-show-message-top))
761 next-screen-context-lines)
763 (goto-char (notmuch-show-message-top))
764 (notmuch-show-message-adjust)))
765 ;; Move to the top left of the window.
766 (goto-char (window-start)))
768 ;; Move to the previous message.
769 (notmuch-show-previous-message)))))
771 (defun notmuch-show-reply ()
772 "Reply to the current message."
774 (notmuch-reply (notmuch-show-get-message-id)))
776 (defun notmuch-show-forward-message ()
777 "Forward the current message."
779 (with-current-notmuch-show-message
782 (defun notmuch-show-next-message ()
783 "Show the next message."
785 (notmuch-show-goto-message-next)
786 (notmuch-show-mark-read)
787 (notmuch-show-message-adjust))
789 (defun notmuch-show-previous-message ()
790 "Show the previous message."
792 (notmuch-show-goto-message-previous)
793 (notmuch-show-mark-read)
794 (notmuch-show-message-adjust))
796 (defun notmuch-show-next-open-message ()
797 "Show the next message."
799 (while (and (notmuch-show-goto-message-next)
800 (not (notmuch-show-message-visible-p))))
801 (notmuch-show-mark-read)
802 (notmuch-show-message-adjust))
804 (defun notmuch-show-previous-open-message ()
805 "Show the previous message."
807 (while (and (notmuch-show-goto-message-previous)
808 (not (notmuch-show-message-visible-p))))
809 (notmuch-show-mark-read)
810 (notmuch-show-message-adjust))
812 (defun notmuch-show-view-raw-message ()
813 "View the file holding the current message."
815 (view-file (notmuch-show-get-filename)))
817 (defun notmuch-show-pipe-message (command)
818 "Pipe the contents of the current message to the given command.
820 The given command will be executed with the raw contents of the
821 current email message as stdin. Anything printed by the command
822 to stdout or stderr will appear in the *Messages* buffer."
823 (interactive "sPipe message to command: ")
824 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
826 (shell-quote-argument (notmuch-show-get-filename)))))
828 (defun notmuch-show-add-tag (&rest toadd)
829 "Add a tag to the current message."
831 (list (notmuch-select-tag-with-completion "Tag to add: ")))
832 (apply 'notmuch-call-notmuch-process
834 (mapcar (lambda (s) (concat "+" s)) toadd))
835 (cons (notmuch-show-get-message-id) nil)))
836 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
838 (defun notmuch-show-remove-tag (&rest toremove)
839 "Remove a tag from the current message."
841 (list (notmuch-select-tag-with-completion
842 "Tag to remove: " (notmuch-show-get-message-id))))
843 (let ((tags (notmuch-show-get-tags)))
844 (if (intersection tags toremove :test 'string=)
846 (apply 'notmuch-call-notmuch-process
848 (mapcar (lambda (s) (concat "-" s)) toremove))
849 (cons (notmuch-show-get-message-id) nil)))
850 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
852 (defun notmuch-show-toggle-headers ()
853 "Toggle the visibility of the current message headers."
855 (let ((props (notmuch-show-get-message-properties)))
856 (notmuch-show-headers-visible
858 (not (plist-get props :headers-visible))))
859 (force-window-update))
861 (defun notmuch-show-toggle-message ()
862 "Toggle the visibility of the current message."
864 (let ((props (notmuch-show-get-message-properties)))
865 (notmuch-show-message-visible
867 (not (plist-get props :message-visible))))
868 (force-window-update))
870 (defun notmuch-show-next-button ()
871 "Advance point to the next button in the buffer."
875 (defun notmuch-show-previous-button ()
876 "Move point back to the previous button in the buffer."
880 (defun notmuch-show-archive-thread-internal (show-next)
881 ;; Remove the tag from the current set of messages.
882 (goto-char (point-min))
883 (loop do (notmuch-show-remove-tag "inbox")
884 until (not (notmuch-show-goto-message-next)))
885 ;; Move to the next item in the search results, if any.
886 (let ((parent-buffer notmuch-show-parent-buffer))
890 (switch-to-buffer parent-buffer)
893 (notmuch-search-show-thread))))))
895 (defun notmuch-show-archive-thread ()
896 "Archive each message in thread, then show next thread from search.
898 Archive each message currently shown by removing the \"inbox\"
899 tag from each. Then kill this buffer and show the next thread
900 from the search from which this thread was originally shown.
902 Note: This command is safe from any race condition of new messages
903 being delivered to the same thread. It does not archive the
904 entire thread, but only the messages shown in the current
907 (notmuch-show-archive-thread-internal t))
909 (defun notmuch-show-archive-thread-then-exit ()
910 "Archive each message in thread, then exit back to search results."
912 (notmuch-show-archive-thread-internal nil))
914 (defun notmuch-show-do-stash (text)
916 (message "Saved: %s" text))
918 (defun notmuch-show-stash-cc ()
919 "Copy CC field of current message to kill-ring."
921 (notmuch-show-do-stash (notmuch-show-get-cc)))
923 (defun notmuch-show-stash-date ()
924 "Copy date of current message to kill-ring."
926 (notmuch-show-do-stash (notmuch-show-get-date)))
928 (defun notmuch-show-stash-filename ()
929 "Copy filename of current message to kill-ring."
931 (notmuch-show-do-stash (notmuch-show-get-filename)))
933 (defun notmuch-show-stash-from ()
934 "Copy From address of current message to kill-ring."
936 (notmuch-show-do-stash (notmuch-show-get-from)))
938 (defun notmuch-show-stash-message-id ()
939 "Copy message ID of current message to kill-ring."
941 (notmuch-show-do-stash (notmuch-show-get-message-id)))
943 (defun notmuch-show-stash-subject ()
944 "Copy Subject field of current message to kill-ring."
946 (notmuch-show-do-stash (notmuch-show-get-subject)))
948 (defun notmuch-show-stash-tags ()
949 "Copy tags of current message to kill-ring as a comma separated list."
951 (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
953 (defun notmuch-show-stash-to ()
954 "Copy To address of current message to kill-ring."
956 (notmuch-show-do-stash (notmuch-show-get-to)))
960 (provide 'notmuch-show)