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>
28 (require 'notmuch-lib)
29 (require 'notmuch-query)
30 (require 'notmuch-wash)
32 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
33 (declare-function notmuch-reply "notmuch" (query-string))
34 (declare-function notmuch-fontify-headers "notmuch" nil)
35 (declare-function notmuch-select-tag-with-completion "notmuch" (prompt &rest search-terms))
36 (declare-function notmuch-search-show-thread "notmuch" nil)
38 (defvar notmuch-show-headers '("Subject" "To" "Cc" "From" "Date")
39 "Headers that should be shown in a message, in this order. Note
40 that if this order is changed the headers shown when a message is
41 collapsed will change.")
43 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
44 "A list of functions called to decorate the headers listed in
45 `notmuch-show-headers'.")
47 (defvar notmuch-show-hook '(notmuch-show-pretty-hook)
48 "A list of functions called after populating a
49 `notmuch-show' buffer.")
51 (defvar notmuch-show-insert-text/plain-hook '(notmuch-wash-text/plain-citations)
52 "A list of functions called to clean up text/plain body parts.")
54 (defun notmuch-show-pretty-hook ()
58 (defmacro with-current-notmuch-show-message (&rest body)
59 "Evaluate body with current buffer set to the text of current message"
61 (let ((filename (notmuch-show-get-filename)))
62 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
63 (with-current-buffer buf
64 (insert-file-contents filename nil nil nil t)
68 (defun notmuch-show-view-all-mime-parts ()
69 "Use external viewers to view all attachments from the current message."
71 (with-current-notmuch-show-message
72 ; We ovverride the mm-inline-media-tests to indicate which message
73 ; parts are already sufficiently handled by the original
74 ; presentation of the message in notmuch-show mode. These parts
75 ; will be inserted directly into the temporary buffer of
76 ; with-current-notmuch-show-message and silently discarded.
78 ; Any MIME part not explicitly mentioned here will be handled by an
79 ; external viewer as configured in the various mailcap files.
80 (let ((mm-inline-media-tests '(
81 ("text/.*" ignore identity)
82 ("application/pgp-signature" ignore identity)
83 ("multipart/alternative" ignore identity)
84 ("multipart/mixed" ignore identity)
85 ("multipart/related" ignore identity)
87 (mm-display-parts (mm-dissect-buffer)))))
89 (defun notmuch-foreach-mime-part (function mm-handle)
90 (cond ((stringp (car mm-handle))
91 (dolist (part (cdr mm-handle))
92 (notmuch-foreach-mime-part function part)))
93 ((bufferp (car mm-handle))
94 (funcall function mm-handle))
95 (t (dolist (part mm-handle)
96 (notmuch-foreach-mime-part function part)))))
98 (defun notmuch-count-attachments (mm-handle)
100 (notmuch-foreach-mime-part
102 (let ((disposition (mm-handle-disposition p)))
103 (and (listp disposition)
104 (or (equal (car disposition) "attachment")
105 (and (equal (car disposition) "inline")
106 (assq 'filename disposition)))
111 (defun notmuch-save-attachments (mm-handle &optional queryp)
112 (notmuch-foreach-mime-part
114 (let ((disposition (mm-handle-disposition p)))
115 (and (listp disposition)
116 (or (equal (car disposition) "attachment")
117 (and (equal (car disposition) "inline")
118 (assq 'filename disposition)))
121 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
125 (defun notmuch-show-save-attachments ()
126 "Save all attachments from the current message."
128 (with-current-notmuch-show-message
129 (let ((mm-handle (mm-dissect-buffer)))
130 (notmuch-save-attachments
131 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
134 (defun notmuch-show-fontify-header ()
136 ((looking-at "[Tt]o:")
138 ((looking-at "[Bb]?[Cc][Cc]:")
140 ((looking-at "[Ss]ubject:")
141 'message-header-subject)
142 ((looking-at "[Ff]rom:")
143 'message-header-from)
145 'message-header-other))))
147 (overlay-put (make-overlay (point) (re-search-forward ":"))
148 'face 'message-header-name)
149 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
152 (defun notmuch-show-colour-headers ()
153 "Apply some colouring to the current headers."
154 (goto-char (point-min))
155 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
156 (notmuch-show-fontify-header)
159 (defun notmuch-show-spaces-n (n)
160 "Return a string comprised of `n' spaces."
163 (defun notmuch-show-update-tags (tags)
164 "Update the displayed tags of the current message."
166 (goto-char (notmuch-show-message-top))
167 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
168 (let ((inhibit-read-only t))
169 (replace-match (concat "("
170 (mapconcat 'identity tags " ")
173 (defun notmuch-show-insert-headerline (headers date tags depth)
174 "Insert a notmuch style headerline based on HEADERS for a
175 message at DEPTH in the current thread."
176 (let ((start (point)))
177 (insert (notmuch-show-spaces-n depth)
178 (plist-get headers :From)
182 (mapconcat 'identity tags " ")
184 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
186 (defun notmuch-show-insert-header (header header-value)
187 "Insert a single header."
188 (insert header ": " header-value "\n"))
190 (defun notmuch-show-insert-headers (headers)
191 "Insert the headers of the current message."
192 (let ((start (point)))
193 (mapc '(lambda (header)
194 (let* ((header-symbol (intern (concat ":" header)))
195 (header-value (plist-get headers header-symbol)))
196 (if (and header-value
197 (not (string-equal "" header-value)))
198 (notmuch-show-insert-header header header-value))))
199 notmuch-show-headers)
202 (narrow-to-region start (point-max))
203 (run-hooks 'notmuch-show-markup-headers-hook)))))
205 (defun notmuch-show-insert-part-header (content-type)
206 (let ((start (point)))
207 ;; XXX dme: Make this a more useful button (save the part, display
209 (insert "[ Part of type " content-type ". ]\n")
210 (overlay-put (make-overlay start (point)) 'face 'bold)))
212 ;; Functions handling particular MIME parts.
214 (defun notmuch-show-insert-part-text/plain (part content-type depth)
215 (let ((start (point)))
216 (insert (plist-get part :content))
219 (narrow-to-region start (point-max))
220 (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
223 (defun notmuch-show-insert-part-text/* (part content-type depth)
224 ;; Handle all text types other than text/html.
225 (if (string-equal "text/html" content-type)
227 (notmuch-show-insert-part-header content-type)
228 (insert (plist-get part :content))
231 (defun notmuch-show-insert-part-*/* (part content-type depth)
232 (notmuch-show-insert-part-header content-type)
235 ;; Functions for determining how to handle MIME parts.
237 (defun notmuch-show-split-content-type (content-type)
238 (split-string content-type "/"))
240 (defun notmuch-show-handlers-for (content-type)
241 "Return a list of content handlers for a part of type CONTENT-TYPE."
246 ;; Reverse order of prefrence.
247 (list (intern (concat "notmuch-show-insert-part-*/*"))
249 "notmuch-show-insert-part-"
250 (car (notmuch-show-split-content-type content-type))
252 (intern (concat "notmuch-show-insert-part-" content-type))))
257 (defun notmuch-show-insert-bodypart (part depth)
258 "Insert the body part PART at depth DEPTH in the current thread."
259 (let* ((content-type (downcase (plist-get part :content-type)))
260 (handlers (notmuch-show-handlers-for content-type)))
261 ;; Run the content handlers until one of them returns a non-nil
264 (not (funcall (car handlers) part content-type depth)))
265 (setq handlers (cdr handlers))))
266 ;; Ensure that the part ends with a carriage return.
271 (defun notmuch-show-insert-body (body depth)
272 "Insert the body BODY at depth DEPTH in the current thread."
273 (mapc '(lambda (part) (notmuch-show-insert-bodypart part depth)) body))
275 (defun notmuch-show-make-symbol (type)
276 (make-symbol (concat "notmuch-show-" type)))
278 (defun notmuch-show-insert-msg (msg depth)
279 "Insert the message MSG at depth DEPTH in the current thread."
280 (let ((headers (plist-get msg :headers))
281 ;; Indentation causes the buffer offset of the start/end
282 ;; points to move, so we must use markers.
283 message-start message-end
284 content-start content-end
285 headers-start headers-end
287 (headers-invis-spec (notmuch-show-make-symbol "header"))
288 (message-invis-spec (notmuch-show-make-symbol "message")))
290 (setq message-start (point-marker))
292 (notmuch-show-insert-headerline headers
293 (or (plist-get msg :date_relative)
294 (plist-get headers :Date))
295 (plist-get msg :tags) depth)
297 (setq content-start (point-marker))
299 ;; Set `headers-start' to point after the 'Subject:' header to be
300 ;; compatible with the existing implementation. This just sets it
301 ;; to after the first header.
302 (notmuch-show-insert-headers headers)
303 ;; Headers should include a blank line (backwards compatibility).
306 (goto-char content-start)
308 (setq headers-start (point-marker)))
309 (setq headers-end (point-marker))
311 (setq body-start (point-marker))
312 (notmuch-show-insert-body (plist-get msg :body) depth)
313 ;; Ensure that the body ends with a newline.
316 (setq body-end (point-marker))
317 (setq content-end (point-marker))
319 ;; Indent according to the depth in the thread.
320 (indent-rigidly content-start content-end depth)
322 (setq message-end (point-max-marker))
324 ;; Save the extents of this message over the whole text of the
326 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
328 (plist-put msg :headers-invis-spec headers-invis-spec)
329 (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
331 (plist-put msg :message-invis-spec message-invis-spec)
332 (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
334 ;; Save the properties for this message. Currently this saves the
335 ;; entire message (augmented it with other stuff), which seems
336 ;; like overkill. We might save a reduced subset (for example, not
338 (notmuch-show-set-message-properties msg)
340 ;; Headers are hidden by default.
341 (notmuch-show-headers-visible msg nil)
343 ;; Message visibility depends on whether it matched the search
345 (notmuch-show-message-visible msg (plist-get msg :match))))
347 (defun notmuch-show-insert-tree (tree depth)
348 "Insert the message tree TREE at depth DEPTH in the current thread."
349 (let ((msg (car tree))
350 (replies (cadr tree)))
351 (notmuch-show-insert-msg msg depth)
352 (notmuch-show-insert-thread replies (1+ depth))))
354 (defun notmuch-show-insert-thread (thread depth)
355 "Insert the thread THREAD at depth DEPTH in the current forest."
356 (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
358 (defun notmuch-show-insert-forest (forest)
359 "Insert the forest of threads FOREST."
360 (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
362 (defvar notmuch-show-parent-buffer nil)
365 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
366 "Run \"notmuch show\" with the given thread ID and display results.
368 The optional PARENT-BUFFER is the notmuch-search buffer from
369 which this notmuch-show command was executed, (so that the
370 next thread from that buffer can be show when done with this
373 The optional QUERY-CONTEXT is a notmuch search term. Only
374 messages from the thread matching this search term are shown if
377 The optional BUFFER-NAME provides the neame of the buffer in
378 which the message thread is shown. If it is nil (which occurs
379 when the command is called interactively) the argument to the
381 (interactive "sNotmuch show: ")
382 (let ((buffer (get-buffer-create (generate-new-buffer-name
384 (concat "*notmuch-" thread-id "*")))))
385 (inhibit-read-only t))
386 (switch-to-buffer buffer)
388 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
390 (goto-char (point-min))
392 (let* ((basic-args (list thread-id))
393 (args (if query-context
394 (append basic-args (list "and (" query-context ")"))
396 (notmuch-show-insert-forest (notmuch-query-get-threads args))
397 ;; If the query context reduced the results to nothing, run
399 (when (and (eq (buffer-size) 0)
401 (notmuch-show-insert-forest
402 (notmuch-query-get-threads basic-args))))
403 (run-hooks 'notmuch-show-hook))
405 ;; Move straight to the first open message
406 (if (not (notmuch-show-message-visible-p))
407 (notmuch-show-next-open-message))
408 (notmuch-show-mark-read)))
410 (defvar notmuch-show-stash-map
411 (let ((map (make-sparse-keymap)))
412 (define-key map "c" 'notmuch-show-stash-cc)
413 (define-key map "d" 'notmuch-show-stash-date)
414 (define-key map "F" 'notmuch-show-stash-filename)
415 (define-key map "f" 'notmuch-show-stash-from)
416 (define-key map "i" 'notmuch-show-stash-message-id)
417 (define-key map "s" 'notmuch-show-stash-subject)
418 (define-key map "T" 'notmuch-show-stash-tags)
419 (define-key map "t" 'notmuch-show-stash-to)
421 "Submap for stash commands")
422 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
424 (defvar notmuch-show-mode-map
425 (let ((map (make-sparse-keymap)))
426 (define-key map "?" 'notmuch-help)
427 (define-key map "q" 'kill-this-buffer)
428 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
429 (define-key map (kbd "TAB") 'notmuch-show-next-button)
430 (define-key map "s" 'notmuch-search)
431 (define-key map "m" 'message-mail)
432 (define-key map "f" 'notmuch-show-forward-message)
433 (define-key map "r" 'notmuch-show-reply)
434 (define-key map "|" 'notmuch-show-pipe-message)
435 (define-key map "w" 'notmuch-show-save-attachments)
436 (define-key map "V" 'notmuch-show-view-raw-message)
437 (define-key map "v" 'notmuch-show-view-all-mime-parts)
438 (define-key map "c" 'notmuch-show-stash-map)
439 (define-key map "h" 'notmuch-show-toggle-headers)
440 (define-key map "-" 'notmuch-show-remove-tag)
441 (define-key map "+" 'notmuch-show-add-tag)
442 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
443 (define-key map "a" 'notmuch-show-archive-thread)
444 (define-key map "N" 'notmuch-show-next-message)
445 (define-key map "P" 'notmuch-show-previous-message)
446 (define-key map "n" 'notmuch-show-next-open-message)
447 (define-key map "p" 'notmuch-show-previous-open-message)
448 (define-key map (kbd "DEL") 'notmuch-show-rewind)
449 (define-key map " " 'notmuch-show-advance-and-archive)
450 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
452 "Keymap for \"notmuch show\" buffers.")
453 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
456 (defun notmuch-show-mode ()
457 "Major mode for viewing a thread with notmuch.
459 This buffer contains the results of the \"notmuch show\" command
460 for displaying a single thread of email from your email archives.
462 By default, various components of email messages, (citations,
463 signatures, already-read messages), are hidden. You can make
464 these parts visible by clicking with the mouse button or by
465 pressing RET after positioning the cursor on a hidden part, (for
466 which \\[notmuch-show-next-button] and
467 \\[notmuch-show-previous-button] are helpful).
469 Reading the thread sequentially is well-supported by pressing
470 \\[notmuch-show-advance-and-archive]. This will scroll the
471 current message (if necessary), advance to the next message, or
472 advance to the next thread (if already on the last message of a
475 Other commands are available to read or manipulate the thread
476 more selectively, (such as '\\[notmuch-show-next-message]' and
477 '\\[notmuch-show-previous-message]' to advance to messages
478 without removing any tags, and '\\[notmuch-show-archive-thread]'
479 to archive an entire thread without scrolling through with
480 \\[notmuch-show-advance-and-archive]).
482 You can add or remove arbitary tags from the current message with
483 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
485 All currently available key bindings:
487 \\{notmuch-show-mode-map}"
489 (kill-all-local-variables)
490 (use-local-map notmuch-show-mode-map)
491 (setq major-mode 'notmuch-show-mode
492 mode-name "notmuch-show")
493 (setq buffer-read-only t))
495 (defun notmuch-show-move-to-message-top ()
496 (goto-char (notmuch-show-message-top)))
498 (defun notmuch-show-move-to-message-bottom ()
499 (goto-char (notmuch-show-message-bottom)))
501 (defun notmuch-show-message-adjust ()
504 ;; Movement related functions.
506 ;; There's some strangeness here where a text property applied to a
507 ;; region a->b is not found when point is at b. We walk backwards
508 ;; until finding the property.
509 (defun notmuch-show-message-extent ()
512 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
516 (defun notmuch-show-message-top ()
517 (car (notmuch-show-message-extent)))
519 (defun notmuch-show-message-bottom ()
520 (cdr (notmuch-show-message-extent)))
522 (defun notmuch-show-goto-message-next ()
523 (let ((start (point)))
524 (notmuch-show-move-to-message-bottom)
530 (defun notmuch-show-goto-message-previous ()
531 (notmuch-show-move-to-message-top)
535 (notmuch-show-move-to-message-top)
538 (defun notmuch-show-move-past-invisible-forward ()
539 (while (point-invisible-p)
542 (defun notmuch-show-move-past-invisible-backward ()
543 (while (point-invisible-p)
546 ;; Functions relating to the visibility of messages and their
549 (defun notmuch-show-element-visible (props visible-p spec-property)
550 (let ((spec (plist-get props spec-property)))
552 (remove-from-invisibility-spec spec)
553 (add-to-invisibility-spec spec))))
555 (defun notmuch-show-message-visible (props visible-p)
557 ;; When making the message visible, the headers may or not be
558 ;; visible. So we check that property separately.
559 (let ((headers-visible (plist-get props :headers-visible)))
560 (notmuch-show-element-visible props headers-visible :headers-invis-spec)
561 (notmuch-show-element-visible props t :message-invis-spec))
562 (notmuch-show-element-visible props nil :headers-invis-spec)
563 (notmuch-show-element-visible props nil :message-invis-spec))
565 (notmuch-show-set-prop :message-visible visible-p props))
567 (defun notmuch-show-headers-visible (props visible-p)
568 (if (plist-get props :message-visible)
569 (notmuch-show-element-visible props visible-p :headers-invis-spec))
570 (notmuch-show-set-prop :headers-visible visible-p props))
572 ;; Functions for setting and getting attributes of the current
575 (defun notmuch-show-set-message-properties (props)
577 (notmuch-show-move-to-message-top)
578 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
580 (defun notmuch-show-get-message-properties ()
582 (notmuch-show-move-to-message-top)
583 (get-text-property (point) :notmuch-message-properties)))
585 (defun notmuch-show-set-prop (prop val &optional props)
586 (let ((inhibit-read-only t)
588 (notmuch-show-get-message-properties))))
589 (plist-put props prop val)
590 (notmuch-show-set-message-properties props)))
592 (defun notmuch-show-get-prop (prop &optional props)
593 (let ((props (or props
594 (notmuch-show-get-message-properties))))
595 (plist-get props prop)))
597 (defun notmuch-show-get-message-id ()
598 "Return the message id of the current message."
599 (concat "id:" (notmuch-show-get-prop :id)))
601 ;; dme: Would it make sense to use a macro for many of these?
603 (defun notmuch-show-get-filename ()
604 "Return the filename of the current message."
605 (notmuch-show-get-prop :filename))
607 (defun notmuch-show-get-header (header)
608 "Return the named header of the current message, if any."
609 (plist-get (notmuch-show-get-prop :headers) header))
611 (defun notmuch-show-get-cc ()
612 (notmuch-show-get-header :Cc))
614 (defun notmuch-show-get-date ()
615 (notmuch-show-get-header :Date))
617 (defun notmuch-show-get-from ()
618 (notmuch-show-get-header :From))
620 (defun notmuch-show-get-subject ()
621 (notmuch-show-get-header :Subject))
623 (defun notmuch-show-get-to ()
624 (notmuch-show-get-header :To))
626 (defun notmuch-show-set-tags (tags)
627 "Set the tags of the current message."
628 (notmuch-show-set-prop :tags tags)
629 (notmuch-show-update-tags tags))
631 (defun notmuch-show-get-tags ()
632 "Return the tags of the current message."
633 (notmuch-show-get-prop :tags))
635 (defun notmuch-show-message-visible-p ()
636 "Is the current message visible?"
637 (notmuch-show-get-prop :message-visible))
639 (defun notmuch-show-headers-visible-p ()
640 "Are the headers of the current message visible?"
641 (notmuch-show-get-prop :headers-visible))
643 (defun notmuch-show-mark-read ()
644 "Mark the current message as read."
645 (notmuch-show-remove-tag "unread"))
647 ;; Commands typically bound to keys.
649 (defun notmuch-show-advance-and-archive ()
650 "Advance through thread and archive.
652 This command is intended to be one of the simplest ways to
653 process a thread of email. It does the following:
655 If the current message in the thread is not yet fully visible,
656 scroll by a near screenful to read more of the message.
658 Otherwise, (the end of the current message is already within the
659 current window), advance to the next open message.
661 Finally, if there is no further message to advance to, and this
662 last message is already read, then archive the entire current
663 thread, (remove the \"inbox\" tag from each message). Also kill
664 this buffer, and display the next thread from the search from
665 which this thread was originally shown."
667 (let ((end-of-this-message (notmuch-show-message-bottom)))
669 ;; Ideally we would test `end-of-this-message' against the result
670 ;; of `window-end', but that doesn't account for the fact that
671 ;; the end of the message might be hidden, so we have to actually
672 ;; go to the end, walk back over invisible text and then see if
675 (goto-char (- end-of-this-message 1))
676 (notmuch-show-move-past-invisible-backward)
677 (> (point) (window-end)))
678 ;; The bottom of this message is not visible - scroll.
681 ((not (= end-of-this-message (point-max)))
682 ;; This is not the last message - move to the next visible one.
683 (notmuch-show-next-open-message))
686 ;; This is the last message - archive the thread.
687 (notmuch-show-archive-thread)))))
689 (defun notmuch-show-rewind ()
690 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
692 Specifically, if the beginning of the previous email is fewer
693 than `window-height' lines from the current point, move to it
694 just like `notmuch-show-previous-message'.
696 Otherwise, just scroll down a screenful of the current message.
698 This command does not modify any message tags, (it does not undo
699 any effects from previous calls to
700 `notmuch-show-advance-and-archive'."
702 (let ((start-of-message (notmuch-show-message-top))
703 (start-of-window (window-start)))
705 ;; Either this message is properly aligned with the start of the
706 ;; window or the start of this message is not visible on the
708 ((or (= start-of-message start-of-window)
709 (< start-of-message start-of-window))
711 ;; If a small number of lines from the previous message are
712 ;; visible, realign so that the top of the current message is at
713 ;; the top of the screen.
714 (if (< (count-lines (window-start) (notmuch-show-message-top))
715 next-screen-context-lines)
717 (goto-char (notmuch-show-message-top))
718 (notmuch-show-message-adjust)))
719 ;; Move to the top left of the window.
720 (goto-char (window-start)))
722 ;; Move to the previous message.
723 (notmuch-show-previous-message)))))
725 (defun notmuch-show-reply ()
726 "Reply to the current message."
728 (notmuch-reply (notmuch-show-get-message-id)))
730 (defun notmuch-show-forward-message ()
731 "Forward the current message."
733 (with-current-notmuch-show-message
736 (defun notmuch-show-next-message ()
737 "Show the next message."
739 (notmuch-show-goto-message-next)
740 (notmuch-show-mark-read)
741 (notmuch-show-message-adjust))
743 (defun notmuch-show-previous-message ()
744 "Show the previous message."
746 (notmuch-show-goto-message-previous)
747 (notmuch-show-mark-read)
748 (notmuch-show-message-adjust))
750 (defun notmuch-show-next-open-message ()
751 "Show the next message."
753 (while (and (notmuch-show-goto-message-next)
754 (not (notmuch-show-message-visible-p))))
755 (notmuch-show-mark-read)
756 (notmuch-show-message-adjust))
758 (defun notmuch-show-previous-open-message ()
759 "Show the previous message."
761 (while (and (notmuch-show-goto-message-previous)
762 (not (notmuch-show-message-visible-p))))
763 (notmuch-show-mark-read)
764 (notmuch-show-message-adjust))
766 (defun notmuch-show-view-raw-message ()
767 "View the file holding the current message."
769 (view-file (notmuch-show-get-filename)))
771 (defun notmuch-show-pipe-message (command)
772 "Pipe the contents of the current message to the given command.
774 The given command will be executed with the raw contents of the
775 current email message as stdin. Anything printed by the command
776 to stdout or stderr will appear in the *Messages* buffer."
777 (interactive "sPipe message to command: ")
778 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
780 (shell-quote-argument (notmuch-show-get-filename)))))
782 (defun notmuch-show-add-tag (&rest toadd)
783 "Add a tag to the current message."
785 (list (notmuch-select-tag-with-completion "Tag to add: ")))
786 (apply 'notmuch-call-notmuch-process
788 (mapcar (lambda (s) (concat "+" s)) toadd))
789 (cons (notmuch-show-get-message-id) nil)))
790 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
792 (defun notmuch-show-remove-tag (&rest toremove)
793 "Remove a tag from the current message."
795 (list (notmuch-select-tag-with-completion
796 "Tag to remove: " (notmuch-show-get-message-id))))
797 (let ((tags (notmuch-show-get-tags)))
798 (if (intersection tags toremove :test 'string=)
800 (apply 'notmuch-call-notmuch-process
802 (mapcar (lambda (s) (concat "-" s)) toremove))
803 (cons (notmuch-show-get-message-id) nil)))
804 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
806 (defun notmuch-show-toggle-headers ()
807 "Toggle the visibility of the current message headers."
809 (let ((props (notmuch-show-get-message-properties)))
810 (notmuch-show-headers-visible
812 (not (plist-get props :headers-visible))))
813 (force-window-update))
815 (defun notmuch-show-toggle-message ()
816 "Toggle the visibility of the current message."
818 (let ((props (notmuch-show-get-message-properties)))
819 (notmuch-show-message-visible
821 (not (plist-get props :message-visible))))
822 (force-window-update))
824 (defun notmuch-show-next-button ()
825 "Advance point to the next button in the buffer."
829 (defun notmuch-show-previous-button ()
830 "Move point back to the previous button in the buffer."
834 (defun notmuch-show-archive-thread-internal (show-next)
835 ;; Remove the tag from the current set of messages.
836 (goto-char (point-min))
837 (loop do (notmuch-show-remove-tag "inbox")
838 until (not (notmuch-show-goto-message-next)))
839 ;; Move to the next item in the search results, if any.
840 (let ((parent-buffer notmuch-show-parent-buffer))
844 (switch-to-buffer parent-buffer)
847 (notmuch-search-show-thread))))))
849 (defun notmuch-show-archive-thread ()
850 "Archive each message in thread, then show next thread from search.
852 Archive each message currently shown by removing the \"inbox\"
853 tag from each. Then kill this buffer and show the next thread
854 from the search from which this thread was originally shown.
856 Note: This command is safe from any race condition of new messages
857 being delivered to the same thread. It does not archive the
858 entire thread, but only the messages shown in the current
861 (notmuch-show-archive-thread-internal t))
863 (defun notmuch-show-archive-thread-then-exit ()
864 "Archive each message in thread, then exit back to search results."
866 (notmuch-show-archive-thread-internal nil))
868 (defun notmuch-show-do-stash (text)
870 (message "Saved: %s" text))
872 (defun notmuch-show-stash-cc ()
873 "Copy CC field of current message to kill-ring."
875 (notmuch-show-do-stash (notmuch-show-get-cc)))
877 (defun notmuch-show-stash-date ()
878 "Copy date of current message to kill-ring."
880 (notmuch-show-do-stash (notmuch-show-get-date)))
882 (defun notmuch-show-stash-filename ()
883 "Copy filename of current message to kill-ring."
885 (notmuch-show-do-stash (notmuch-show-get-filename)))
887 (defun notmuch-show-stash-from ()
888 "Copy From address of current message to kill-ring."
890 (notmuch-show-do-stash (notmuch-show-get-from)))
892 (defun notmuch-show-stash-message-id ()
893 "Copy message ID of current message to kill-ring."
895 (notmuch-show-do-stash (notmuch-show-get-message-id)))
897 (defun notmuch-show-stash-subject ()
898 "Copy Subject field of current message to kill-ring."
900 (notmuch-show-do-stash (notmuch-show-get-subject)))
902 (defun notmuch-show-stash-tags ()
903 "Copy tags of current message to kill-ring as a comma separated list."
905 (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
907 (defun notmuch-show-stash-to ()
908 "Copy To address of current message to kill-ring."
910 (notmuch-show-do-stash (notmuch-show-get-to)))
914 (provide 'notmuch-show)