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 &optional name)
206 (let ((start (point)))
207 ;; XXX dme: Make this a more useful button (save the part, display
209 (insert "[ Part of type "
211 (if name (concat " named " name) "")
213 (overlay-put (make-overlay start (point)) 'face 'bold)))
215 ;; Functions handling particular MIME parts.
217 (defun notmuch-show-insert-part-text/plain (part content-type nth depth)
218 (let ((start (point)))
219 ;; If this text/plain part is not the first part in the message,
220 ;; insert a header to make this clear.
222 (notmuch-show-insert-part-header content-type (plist-get part :filename)))
223 (insert (plist-get part :content))
226 (narrow-to-region start (point-max))
227 (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
230 (defun notmuch-show-insert-part-text/* (part content-type nth depth)
231 ;; Handle all text types other than text/html.
232 (if (string-equal "text/html" content-type)
234 (notmuch-show-insert-part-header content-type (plist-get part :filename))
235 (insert (plist-get part :content))
238 (defun notmuch-show-insert-part-*/* (part content-type nth depth)
239 (notmuch-show-insert-part-header content-type (plist-get part :filename))
242 ;; Functions for determining how to handle MIME parts.
244 (defun notmuch-show-split-content-type (content-type)
245 (split-string content-type "/"))
247 (defun notmuch-show-handlers-for (content-type)
248 "Return a list of content handlers for a part of type CONTENT-TYPE."
253 ;; Reverse order of prefrence.
254 (list (intern (concat "notmuch-show-insert-part-*/*"))
256 "notmuch-show-insert-part-"
257 (car (notmuch-show-split-content-type content-type))
259 (intern (concat "notmuch-show-insert-part-" content-type))))
264 (defun notmuch-show-insert-bodypart (part depth)
265 "Insert the body part PART at depth DEPTH in the current thread."
266 (let* ((content-type (downcase (plist-get part :content-type)))
267 (handlers (notmuch-show-handlers-for content-type))
268 (nth (plist-get part :id)))
269 ;; Run the content handlers until one of them returns a non-nil
272 (not (funcall (car handlers) part content-type nth depth)))
273 (setq handlers (cdr handlers))))
274 ;; Ensure that the part ends with a carriage return.
279 (defun notmuch-show-insert-body (body depth)
280 "Insert the body BODY at depth DEPTH in the current thread."
281 (mapc '(lambda (part) (notmuch-show-insert-bodypart part depth)) body))
283 (defun notmuch-show-make-symbol (type)
284 (make-symbol (concat "notmuch-show-" type)))
286 (defun notmuch-show-insert-msg (msg depth)
287 "Insert the message MSG at depth DEPTH in the current thread."
288 (let ((headers (plist-get msg :headers))
289 ;; Indentation causes the buffer offset of the start/end
290 ;; points to move, so we must use markers.
291 message-start message-end
292 content-start content-end
293 headers-start headers-end
295 (headers-invis-spec (notmuch-show-make-symbol "header"))
296 (message-invis-spec (notmuch-show-make-symbol "message")))
298 (setq message-start (point-marker))
300 (notmuch-show-insert-headerline headers
301 (or (plist-get msg :date_relative)
302 (plist-get headers :Date))
303 (plist-get msg :tags) depth)
305 (setq content-start (point-marker))
307 ;; Set `headers-start' to point after the 'Subject:' header to be
308 ;; compatible with the existing implementation. This just sets it
309 ;; to after the first header.
310 (notmuch-show-insert-headers headers)
311 ;; Headers should include a blank line (backwards compatibility).
314 (goto-char content-start)
316 (setq headers-start (point-marker)))
317 (setq headers-end (point-marker))
319 (setq body-start (point-marker))
320 (notmuch-show-insert-body (plist-get msg :body) depth)
321 ;; Ensure that the body ends with a newline.
324 (setq body-end (point-marker))
325 (setq content-end (point-marker))
327 ;; Indent according to the depth in the thread.
328 (indent-rigidly content-start content-end depth)
330 (setq message-end (point-max-marker))
332 ;; Save the extents of this message over the whole text of the
334 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
336 (plist-put msg :headers-invis-spec headers-invis-spec)
337 (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
339 (plist-put msg :message-invis-spec message-invis-spec)
340 (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
342 ;; Save the properties for this message. Currently this saves the
343 ;; entire message (augmented it with other stuff), which seems
344 ;; like overkill. We might save a reduced subset (for example, not
346 (notmuch-show-set-message-properties msg)
348 ;; Headers are hidden by default.
349 (notmuch-show-headers-visible msg nil)
351 ;; Message visibility depends on whether it matched the search
353 (notmuch-show-message-visible msg (plist-get msg :match))))
355 (defun notmuch-show-insert-tree (tree depth)
356 "Insert the message tree TREE at depth DEPTH in the current thread."
357 (let ((msg (car tree))
358 (replies (cadr tree)))
359 (notmuch-show-insert-msg msg depth)
360 (notmuch-show-insert-thread replies (1+ depth))))
362 (defun notmuch-show-insert-thread (thread depth)
363 "Insert the thread THREAD at depth DEPTH in the current forest."
364 (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
366 (defun notmuch-show-insert-forest (forest)
367 "Insert the forest of threads FOREST."
368 (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
370 (defvar notmuch-show-parent-buffer nil)
373 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
374 "Run \"notmuch show\" with the given thread ID and display results.
376 The optional PARENT-BUFFER is the notmuch-search buffer from
377 which this notmuch-show command was executed, (so that the
378 next thread from that buffer can be show when done with this
381 The optional QUERY-CONTEXT is a notmuch search term. Only
382 messages from the thread matching this search term are shown if
385 The optional BUFFER-NAME provides the neame of the buffer in
386 which the message thread is shown. If it is nil (which occurs
387 when the command is called interactively) the argument to the
389 (interactive "sNotmuch show: ")
390 (let ((buffer (get-buffer-create (generate-new-buffer-name
392 (concat "*notmuch-" thread-id "*")))))
393 (inhibit-read-only t))
394 (switch-to-buffer buffer)
396 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
398 (goto-char (point-min))
400 (let* ((basic-args (list thread-id))
401 (args (if query-context
402 (append basic-args (list "and (" query-context ")"))
404 (notmuch-show-insert-forest (notmuch-query-get-threads args))
405 ;; If the query context reduced the results to nothing, run
407 (when (and (eq (buffer-size) 0)
409 (notmuch-show-insert-forest
410 (notmuch-query-get-threads basic-args))))
411 (run-hooks 'notmuch-show-hook))
413 ;; Move straight to the first open message
414 (if (not (notmuch-show-message-visible-p))
415 (notmuch-show-next-open-message))
416 (notmuch-show-mark-read)))
418 (defvar notmuch-show-stash-map
419 (let ((map (make-sparse-keymap)))
420 (define-key map "c" 'notmuch-show-stash-cc)
421 (define-key map "d" 'notmuch-show-stash-date)
422 (define-key map "F" 'notmuch-show-stash-filename)
423 (define-key map "f" 'notmuch-show-stash-from)
424 (define-key map "i" 'notmuch-show-stash-message-id)
425 (define-key map "s" 'notmuch-show-stash-subject)
426 (define-key map "T" 'notmuch-show-stash-tags)
427 (define-key map "t" 'notmuch-show-stash-to)
429 "Submap for stash commands")
430 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
432 (defvar notmuch-show-mode-map
433 (let ((map (make-sparse-keymap)))
434 (define-key map "?" 'notmuch-help)
435 (define-key map "q" 'kill-this-buffer)
436 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
437 (define-key map (kbd "TAB") 'notmuch-show-next-button)
438 (define-key map "s" 'notmuch-search)
439 (define-key map "m" 'message-mail)
440 (define-key map "f" 'notmuch-show-forward-message)
441 (define-key map "r" 'notmuch-show-reply)
442 (define-key map "|" 'notmuch-show-pipe-message)
443 (define-key map "w" 'notmuch-show-save-attachments)
444 (define-key map "V" 'notmuch-show-view-raw-message)
445 (define-key map "v" 'notmuch-show-view-all-mime-parts)
446 (define-key map "c" 'notmuch-show-stash-map)
447 (define-key map "h" 'notmuch-show-toggle-headers)
448 (define-key map "-" 'notmuch-show-remove-tag)
449 (define-key map "+" 'notmuch-show-add-tag)
450 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
451 (define-key map "a" 'notmuch-show-archive-thread)
452 (define-key map "N" 'notmuch-show-next-message)
453 (define-key map "P" 'notmuch-show-previous-message)
454 (define-key map "n" 'notmuch-show-next-open-message)
455 (define-key map "p" 'notmuch-show-previous-open-message)
456 (define-key map (kbd "DEL") 'notmuch-show-rewind)
457 (define-key map " " 'notmuch-show-advance-and-archive)
458 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
460 "Keymap for \"notmuch show\" buffers.")
461 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
464 (defun notmuch-show-mode ()
465 "Major mode for viewing a thread with notmuch.
467 This buffer contains the results of the \"notmuch show\" command
468 for displaying a single thread of email from your email archives.
470 By default, various components of email messages, (citations,
471 signatures, already-read messages), are hidden. You can make
472 these parts visible by clicking with the mouse button or by
473 pressing RET after positioning the cursor on a hidden part, (for
474 which \\[notmuch-show-next-button] and
475 \\[notmuch-show-previous-button] are helpful).
477 Reading the thread sequentially is well-supported by pressing
478 \\[notmuch-show-advance-and-archive]. This will scroll the
479 current message (if necessary), advance to the next message, or
480 advance to the next thread (if already on the last message of a
483 Other commands are available to read or manipulate the thread
484 more selectively, (such as '\\[notmuch-show-next-message]' and
485 '\\[notmuch-show-previous-message]' to advance to messages
486 without removing any tags, and '\\[notmuch-show-archive-thread]'
487 to archive an entire thread without scrolling through with
488 \\[notmuch-show-advance-and-archive]).
490 You can add or remove arbitary tags from the current message with
491 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
493 All currently available key bindings:
495 \\{notmuch-show-mode-map}"
497 (kill-all-local-variables)
498 (use-local-map notmuch-show-mode-map)
499 (setq major-mode 'notmuch-show-mode
500 mode-name "notmuch-show")
501 (setq buffer-read-only t))
503 (defun notmuch-show-move-to-message-top ()
504 (goto-char (notmuch-show-message-top)))
506 (defun notmuch-show-move-to-message-bottom ()
507 (goto-char (notmuch-show-message-bottom)))
509 (defun notmuch-show-message-adjust ()
512 ;; Movement related functions.
514 ;; There's some strangeness here where a text property applied to a
515 ;; region a->b is not found when point is at b. We walk backwards
516 ;; until finding the property.
517 (defun notmuch-show-message-extent ()
520 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
524 (defun notmuch-show-message-top ()
525 (car (notmuch-show-message-extent)))
527 (defun notmuch-show-message-bottom ()
528 (cdr (notmuch-show-message-extent)))
530 (defun notmuch-show-goto-message-next ()
531 (let ((start (point)))
532 (notmuch-show-move-to-message-bottom)
538 (defun notmuch-show-goto-message-previous ()
539 (notmuch-show-move-to-message-top)
543 (notmuch-show-move-to-message-top)
546 (defun notmuch-show-move-past-invisible-forward ()
547 (while (point-invisible-p)
550 (defun notmuch-show-move-past-invisible-backward ()
551 (while (point-invisible-p)
554 ;; Functions relating to the visibility of messages and their
557 (defun notmuch-show-element-visible (props visible-p spec-property)
558 (let ((spec (plist-get props spec-property)))
560 (remove-from-invisibility-spec spec)
561 (add-to-invisibility-spec spec))))
563 (defun notmuch-show-message-visible (props visible-p)
565 ;; When making the message visible, the headers may or not be
566 ;; visible. So we check that property separately.
567 (let ((headers-visible (plist-get props :headers-visible)))
568 (notmuch-show-element-visible props headers-visible :headers-invis-spec)
569 (notmuch-show-element-visible props t :message-invis-spec))
570 (notmuch-show-element-visible props nil :headers-invis-spec)
571 (notmuch-show-element-visible props nil :message-invis-spec))
573 (notmuch-show-set-prop :message-visible visible-p props))
575 (defun notmuch-show-headers-visible (props visible-p)
576 (if (plist-get props :message-visible)
577 (notmuch-show-element-visible props visible-p :headers-invis-spec))
578 (notmuch-show-set-prop :headers-visible visible-p props))
580 ;; Functions for setting and getting attributes of the current
583 (defun notmuch-show-set-message-properties (props)
585 (notmuch-show-move-to-message-top)
586 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
588 (defun notmuch-show-get-message-properties ()
590 (notmuch-show-move-to-message-top)
591 (get-text-property (point) :notmuch-message-properties)))
593 (defun notmuch-show-set-prop (prop val &optional props)
594 (let ((inhibit-read-only t)
596 (notmuch-show-get-message-properties))))
597 (plist-put props prop val)
598 (notmuch-show-set-message-properties props)))
600 (defun notmuch-show-get-prop (prop &optional props)
601 (let ((props (or props
602 (notmuch-show-get-message-properties))))
603 (plist-get props prop)))
605 (defun notmuch-show-get-message-id ()
606 "Return the message id of the current message."
607 (concat "id:" (notmuch-show-get-prop :id)))
609 ;; dme: Would it make sense to use a macro for many of these?
611 (defun notmuch-show-get-filename ()
612 "Return the filename of the current message."
613 (notmuch-show-get-prop :filename))
615 (defun notmuch-show-get-header (header)
616 "Return the named header of the current message, if any."
617 (plist-get (notmuch-show-get-prop :headers) header))
619 (defun notmuch-show-get-cc ()
620 (notmuch-show-get-header :Cc))
622 (defun notmuch-show-get-date ()
623 (notmuch-show-get-header :Date))
625 (defun notmuch-show-get-from ()
626 (notmuch-show-get-header :From))
628 (defun notmuch-show-get-subject ()
629 (notmuch-show-get-header :Subject))
631 (defun notmuch-show-get-to ()
632 (notmuch-show-get-header :To))
634 (defun notmuch-show-set-tags (tags)
635 "Set the tags of the current message."
636 (notmuch-show-set-prop :tags tags)
637 (notmuch-show-update-tags tags))
639 (defun notmuch-show-get-tags ()
640 "Return the tags of the current message."
641 (notmuch-show-get-prop :tags))
643 (defun notmuch-show-message-visible-p ()
644 "Is the current message visible?"
645 (notmuch-show-get-prop :message-visible))
647 (defun notmuch-show-headers-visible-p ()
648 "Are the headers of the current message visible?"
649 (notmuch-show-get-prop :headers-visible))
651 (defun notmuch-show-mark-read ()
652 "Mark the current message as read."
653 (notmuch-show-remove-tag "unread"))
655 ;; Commands typically bound to keys.
657 (defun notmuch-show-advance-and-archive ()
658 "Advance through thread and archive.
660 This command is intended to be one of the simplest ways to
661 process a thread of email. It does the following:
663 If the current message in the thread is not yet fully visible,
664 scroll by a near screenful to read more of the message.
666 Otherwise, (the end of the current message is already within the
667 current window), advance to the next open message.
669 Finally, if there is no further message to advance to, and this
670 last message is already read, then archive the entire current
671 thread, (remove the \"inbox\" tag from each message). Also kill
672 this buffer, and display the next thread from the search from
673 which this thread was originally shown."
675 (let ((end-of-this-message (notmuch-show-message-bottom)))
677 ;; Ideally we would test `end-of-this-message' against the result
678 ;; of `window-end', but that doesn't account for the fact that
679 ;; the end of the message might be hidden, so we have to actually
680 ;; go to the end, walk back over invisible text and then see if
683 (goto-char (- end-of-this-message 1))
684 (notmuch-show-move-past-invisible-backward)
685 (> (point) (window-end)))
686 ;; The bottom of this message is not visible - scroll.
689 ((not (= end-of-this-message (point-max)))
690 ;; This is not the last message - move to the next visible one.
691 (notmuch-show-next-open-message))
694 ;; This is the last message - archive the thread.
695 (notmuch-show-archive-thread)))))
697 (defun notmuch-show-rewind ()
698 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
700 Specifically, if the beginning of the previous email is fewer
701 than `window-height' lines from the current point, move to it
702 just like `notmuch-show-previous-message'.
704 Otherwise, just scroll down a screenful of the current message.
706 This command does not modify any message tags, (it does not undo
707 any effects from previous calls to
708 `notmuch-show-advance-and-archive'."
710 (let ((start-of-message (notmuch-show-message-top))
711 (start-of-window (window-start)))
713 ;; Either this message is properly aligned with the start of the
714 ;; window or the start of this message is not visible on the
716 ((or (= start-of-message start-of-window)
717 (< start-of-message start-of-window))
719 ;; If a small number of lines from the previous message are
720 ;; visible, realign so that the top of the current message is at
721 ;; the top of the screen.
722 (if (< (count-lines (window-start) (notmuch-show-message-top))
723 next-screen-context-lines)
725 (goto-char (notmuch-show-message-top))
726 (notmuch-show-message-adjust)))
727 ;; Move to the top left of the window.
728 (goto-char (window-start)))
730 ;; Move to the previous message.
731 (notmuch-show-previous-message)))))
733 (defun notmuch-show-reply ()
734 "Reply to the current message."
736 (notmuch-reply (notmuch-show-get-message-id)))
738 (defun notmuch-show-forward-message ()
739 "Forward the current message."
741 (with-current-notmuch-show-message
744 (defun notmuch-show-next-message ()
745 "Show the next message."
747 (notmuch-show-goto-message-next)
748 (notmuch-show-mark-read)
749 (notmuch-show-message-adjust))
751 (defun notmuch-show-previous-message ()
752 "Show the previous message."
754 (notmuch-show-goto-message-previous)
755 (notmuch-show-mark-read)
756 (notmuch-show-message-adjust))
758 (defun notmuch-show-next-open-message ()
759 "Show the next message."
761 (while (and (notmuch-show-goto-message-next)
762 (not (notmuch-show-message-visible-p))))
763 (notmuch-show-mark-read)
764 (notmuch-show-message-adjust))
766 (defun notmuch-show-previous-open-message ()
767 "Show the previous message."
769 (while (and (notmuch-show-goto-message-previous)
770 (not (notmuch-show-message-visible-p))))
771 (notmuch-show-mark-read)
772 (notmuch-show-message-adjust))
774 (defun notmuch-show-view-raw-message ()
775 "View the file holding the current message."
777 (view-file (notmuch-show-get-filename)))
779 (defun notmuch-show-pipe-message (command)
780 "Pipe the contents of the current message to the given command.
782 The given command will be executed with the raw contents of the
783 current email message as stdin. Anything printed by the command
784 to stdout or stderr will appear in the *Messages* buffer."
785 (interactive "sPipe message to command: ")
786 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
788 (shell-quote-argument (notmuch-show-get-filename)))))
790 (defun notmuch-show-add-tag (&rest toadd)
791 "Add a tag to the current message."
793 (list (notmuch-select-tag-with-completion "Tag to add: ")))
794 (apply 'notmuch-call-notmuch-process
796 (mapcar (lambda (s) (concat "+" s)) toadd))
797 (cons (notmuch-show-get-message-id) nil)))
798 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
800 (defun notmuch-show-remove-tag (&rest toremove)
801 "Remove a tag from the current message."
803 (list (notmuch-select-tag-with-completion
804 "Tag to remove: " (notmuch-show-get-message-id))))
805 (let ((tags (notmuch-show-get-tags)))
806 (if (intersection tags toremove :test 'string=)
808 (apply 'notmuch-call-notmuch-process
810 (mapcar (lambda (s) (concat "-" s)) toremove))
811 (cons (notmuch-show-get-message-id) nil)))
812 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
814 (defun notmuch-show-toggle-headers ()
815 "Toggle the visibility of the current message headers."
817 (let ((props (notmuch-show-get-message-properties)))
818 (notmuch-show-headers-visible
820 (not (plist-get props :headers-visible))))
821 (force-window-update))
823 (defun notmuch-show-toggle-message ()
824 "Toggle the visibility of the current message."
826 (let ((props (notmuch-show-get-message-properties)))
827 (notmuch-show-message-visible
829 (not (plist-get props :message-visible))))
830 (force-window-update))
832 (defun notmuch-show-next-button ()
833 "Advance point to the next button in the buffer."
837 (defun notmuch-show-previous-button ()
838 "Move point back to the previous button in the buffer."
842 (defun notmuch-show-archive-thread-internal (show-next)
843 ;; Remove the tag from the current set of messages.
844 (goto-char (point-min))
845 (loop do (notmuch-show-remove-tag "inbox")
846 until (not (notmuch-show-goto-message-next)))
847 ;; Move to the next item in the search results, if any.
848 (let ((parent-buffer notmuch-show-parent-buffer))
852 (switch-to-buffer parent-buffer)
855 (notmuch-search-show-thread))))))
857 (defun notmuch-show-archive-thread ()
858 "Archive each message in thread, then show next thread from search.
860 Archive each message currently shown by removing the \"inbox\"
861 tag from each. Then kill this buffer and show the next thread
862 from the search from which this thread was originally shown.
864 Note: This command is safe from any race condition of new messages
865 being delivered to the same thread. It does not archive the
866 entire thread, but only the messages shown in the current
869 (notmuch-show-archive-thread-internal t))
871 (defun notmuch-show-archive-thread-then-exit ()
872 "Archive each message in thread, then exit back to search results."
874 (notmuch-show-archive-thread-internal nil))
876 (defun notmuch-show-do-stash (text)
878 (message "Saved: %s" text))
880 (defun notmuch-show-stash-cc ()
881 "Copy CC field of current message to kill-ring."
883 (notmuch-show-do-stash (notmuch-show-get-cc)))
885 (defun notmuch-show-stash-date ()
886 "Copy date of current message to kill-ring."
888 (notmuch-show-do-stash (notmuch-show-get-date)))
890 (defun notmuch-show-stash-filename ()
891 "Copy filename of current message to kill-ring."
893 (notmuch-show-do-stash (notmuch-show-get-filename)))
895 (defun notmuch-show-stash-from ()
896 "Copy From address of current message to kill-ring."
898 (notmuch-show-do-stash (notmuch-show-get-from)))
900 (defun notmuch-show-stash-message-id ()
901 "Copy message ID of current message to kill-ring."
903 (notmuch-show-do-stash (notmuch-show-get-message-id)))
905 (defun notmuch-show-stash-subject ()
906 "Copy Subject field of current message to kill-ring."
908 (notmuch-show-do-stash (notmuch-show-get-subject)))
910 (defun notmuch-show-stash-tags ()
911 "Copy tags of current message to kill-ring as a comma separated list."
913 (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
915 (defun notmuch-show-stash-to ()
916 "Copy To address of current message to kill-ring."
918 (notmuch-show-do-stash (notmuch-show-get-to)))
922 (provide 'notmuch-show)