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)
31 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
32 (declare-function notmuch-reply "notmuch" (query-string))
33 (declare-function notmuch-fontify-headers "notmuch" nil)
34 (declare-function notmuch-select-tag-with-completion "notmuch" (prompt &rest search-terms))
35 (declare-function notmuch-search-show-thread "notmuch" nil)
37 (defvar notmuch-show-citation-regexp
38 "\\(?:^[[:space:]]>.*\n\\(?:[[:space:]]*\n[[:space:]]>.*\n\\)?\\)+"
39 "Pattern to match citation lines.")
41 (defvar notmuch-show-signature-regexp
43 "Pattern to match a line that separates content from signature.")
45 (defvar notmuch-show-signature-button-format
46 "[ %d-line signature. Click/Enter to toggle visibility. ]"
47 "String used to construct button text for hidden signatures
49 Can use up to one integer format parameter, i.e. %d")
51 (defvar notmuch-show-citation-button-format
52 "[ %d more citation lines. Click/Enter to toggle visibility. ]"
53 "String used to construct button text for hidden citations.
55 Can use up to one integer format parameter, i.e. %d")
57 (defvar notmuch-show-signature-lines-max 12
58 "Maximum length of signature that will be hidden by default.")
60 (defvar notmuch-show-citation-lines-prefix 3
61 "Always show at least this many lines at the start of a citation.
63 If there is one more line than the sum of
64 `notmuch-show-citation-lines-prefix' and
65 `notmuch-show-citation-lines-suffix', show that, otherwise
66 collapse remaining lines into a button.")
68 (defvar notmuch-show-citation-lines-suffix 3
69 "Always show at least this many lines at the end of a citation.
71 If there is one more line than the sum of
72 `notmuch-show-citation-lines-prefix' and
73 `notmuch-show-citation-lines-suffix', show that, otherwise
74 collapse remaining lines into a button.")
76 (defvar notmuch-show-headers '("Subject" "To" "Cc" "From" "Date")
77 "Headers that should be shown in a message, in this order. Note
78 that if this order is changed the headers shown when a message is
79 collapsed will change.")
81 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
82 "A list of functions called to decorate the headers listed in
83 `notmuch-show-headers'.")
85 (defvar notmuch-show-hook '(notmuch-show-pretty-hook)
86 "A list of functions called after populating a
87 `notmuch-show' buffer.")
89 (defun notmuch-show-pretty-hook ()
93 (defun notmuch-toggle-invisible-action (cite-button)
94 (let ((invis-spec (button-get cite-button 'invisibility-spec)))
95 (if (invisible-p invis-spec)
96 (remove-from-invisibility-spec invis-spec)
97 (add-to-invisibility-spec invis-spec)
102 (define-button-type 'notmuch-button-invisibility-toggle-type
103 'action 'notmuch-toggle-invisible-action
105 'face 'font-lock-comment-face)
106 (define-button-type 'notmuch-button-citation-toggle-type
107 'help-echo "mouse-1, RET: Show citation"
108 :supertype 'notmuch-button-invisibility-toggle-type)
109 (define-button-type 'notmuch-button-signature-toggle-type
110 'help-echo "mouse-1, RET: Show signature"
111 :supertype 'notmuch-button-invisibility-toggle-type)
112 (define-button-type 'notmuch-button-headers-toggle-type
113 'help-echo "mouse-1, RET: Show headers"
114 :supertype 'notmuch-button-invisibility-toggle-type)
116 (defun notmuch-show-region-to-button (beg end type prefix button-text)
117 "Auxilary function to do the actual making of overlays and buttons
119 BEG and END are buffer locations. TYPE should a string, either
120 \"citation\" or \"signature\". PREFIX is some arbitrary text to
121 insert before the button, probably for indentation. BUTTON-TEXT
122 is what to put on the button."
124 ;; This uses some slightly tricky conversions between strings and
125 ;; symbols because of the way the button code works. Note that
126 ;; replacing intern-soft with make-symbol will cause this to fail,
127 ;; since the newly created symbol has no plist.
129 (let ((overlay (make-overlay beg end))
130 (invis-spec (make-symbol (concat "notmuch-" type "-region")))
131 (button-type (intern-soft (concat "notmuch-button-"
132 type "-toggle-type"))))
133 (add-to-invisibility-spec invis-spec)
134 (overlay-put overlay 'invisible invis-spec)
139 (insert-button button-text
140 'invisibility-spec invis-spec
141 :type button-type))))
143 (defmacro with-current-notmuch-show-message (&rest body)
144 "Evaluate body with current buffer set to the text of current message"
146 (let ((filename (notmuch-show-get-filename)))
147 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
148 (with-current-buffer buf
149 (insert-file-contents filename nil nil nil t)
151 (kill-buffer buf)))))
153 (defun notmuch-show-view-all-mime-parts ()
154 "Use external viewers to view all attachments from the current message."
156 (with-current-notmuch-show-message
157 ; We ovverride the mm-inline-media-tests to indicate which message
158 ; parts are already sufficiently handled by the original
159 ; presentation of the message in notmuch-show mode. These parts
160 ; will be inserted directly into the temporary buffer of
161 ; with-current-notmuch-show-message and silently discarded.
163 ; Any MIME part not explicitly mentioned here will be handled by an
164 ; external viewer as configured in the various mailcap files.
165 (let ((mm-inline-media-tests '(
166 ("text/.*" ignore identity)
167 ("application/pgp-signature" ignore identity)
168 ("multipart/alternative" ignore identity)
169 ("multipart/mixed" ignore identity)
170 ("multipart/related" ignore identity)
172 (mm-display-parts (mm-dissect-buffer)))))
174 (defun notmuch-foreach-mime-part (function mm-handle)
175 (cond ((stringp (car mm-handle))
176 (dolist (part (cdr mm-handle))
177 (notmuch-foreach-mime-part function part)))
178 ((bufferp (car mm-handle))
179 (funcall function mm-handle))
180 (t (dolist (part mm-handle)
181 (notmuch-foreach-mime-part function part)))))
183 (defun notmuch-count-attachments (mm-handle)
185 (notmuch-foreach-mime-part
187 (let ((disposition (mm-handle-disposition p)))
188 (and (listp disposition)
189 (or (equal (car disposition) "attachment")
190 (and (equal (car disposition) "inline")
191 (assq 'filename disposition)))
196 (defun notmuch-save-attachments (mm-handle &optional queryp)
197 (notmuch-foreach-mime-part
199 (let ((disposition (mm-handle-disposition p)))
200 (and (listp disposition)
201 (or (equal (car disposition) "attachment")
202 (and (equal (car disposition) "inline")
203 (assq 'filename disposition)))
206 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
210 (defun notmuch-show-save-attachments ()
211 "Save all attachments from the current message."
213 (with-current-notmuch-show-message
214 (let ((mm-handle (mm-dissect-buffer)))
215 (notmuch-save-attachments
216 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
219 (defun notmuch-show-fontify-header ()
221 ((looking-at "[Tt]o:")
223 ((looking-at "[Bb]?[Cc][Cc]:")
225 ((looking-at "[Ss]ubject:")
226 'message-header-subject)
227 ((looking-at "[Ff]rom:")
228 'message-header-from)
230 'message-header-other))))
232 (overlay-put (make-overlay (point) (re-search-forward ":"))
233 'face 'message-header-name)
234 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
237 (defun notmuch-show-colour-headers ()
238 "Apply some colouring to the current headers."
239 (goto-char (point-min))
240 (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
241 (notmuch-show-fontify-header)
244 (defun notmuch-show-spaces-n (n)
245 "Return a string comprised of `n' spaces."
248 (defun notmuch-show-update-tags (tags)
249 "Update the displayed tags of the current message."
251 (goto-char (notmuch-show-message-top))
252 (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
253 (let ((inhibit-read-only t))
254 (replace-match (concat "("
255 (mapconcat 'identity tags " ")
258 (defun notmuch-show-insert-headerline (headers date tags depth)
259 "Insert a notmuch style headerline based on HEADERS for a
260 message at DEPTH in the current thread."
261 (let ((start (point)))
262 (insert (notmuch-show-spaces-n depth)
263 (plist-get headers :From)
267 (mapconcat 'identity tags " ")
269 (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
271 (defun notmuch-show-insert-header (header header-value)
272 "Insert a single header."
273 (insert header ": " header-value "\n"))
275 (defun notmuch-show-insert-headers (headers)
276 "Insert the headers of the current message."
277 (let ((start (point)))
278 (mapc '(lambda (header)
279 (let* ((header-symbol (intern (concat ":" header)))
280 (header-value (plist-get headers header-symbol)))
281 (if (and header-value
282 (not (string-equal "" header-value)))
283 (notmuch-show-insert-header header header-value))))
284 notmuch-show-headers)
287 (narrow-to-region start (point-max))
288 (run-hooks 'notmuch-show-markup-headers-hook)))))
290 (defun notmuch-show-insert-part-header (content-type)
291 (let ((start (point)))
292 ;; XXX dme: Make this a more useful button (save the part, display
294 (insert "[ Part of type " content-type ". ]\n")
295 (overlay-put (make-overlay start (point)) 'face 'bold)))
297 ;; Functions handling particular MIME parts.
299 (defun notmuch-show-markup-citations ()
300 "Markup citations, and up to one signature in the buffer."
303 (goto-char (point-min))
305 (while (and (< (point) (point-max))
306 (re-search-forward notmuch-show-citation-regexp nil t))
307 (let* ((cite-start (match-beginning 0))
308 (cite-end (match-end 0))
309 (cite-lines (count-lines cite-start cite-end)))
310 (when (> cite-lines (1+ notmuch-show-citation-lines-prefix))
311 (goto-char cite-start)
312 (forward-line notmuch-show-citation-lines-prefix)
313 (let ((hidden-start (point-marker)))
315 (notmuch-show-region-to-button
316 hidden-start (point-marker)
318 (format notmuch-show-citation-button-format
319 (- cite-lines notmuch-show-citation-lines-prefix)))))))
320 (if (and (not (eobp))
321 (re-search-forward notmuch-show-signature-regexp nil t))
322 (let* ((sig-start (match-beginning 0))
323 (sig-end (match-end 0))
324 (sig-lines (1- (count-lines sig-start (point-max)))))
325 (if (<= sig-lines notmuch-show-signature-lines-max)
326 (let ((sig-start-marker (make-marker))
327 (sig-end-marker (make-marker)))
328 (set-marker sig-start-marker sig-start)
329 (set-marker sig-end-marker (point-max))
330 (notmuch-show-region-to-button
331 sig-start-marker sig-end-marker
333 (format notmuch-show-signature-button-format sig-lines))))))))
335 (defun notmuch-show-insert-part-text/plain (part content-type depth)
336 (let ((start (point)))
337 (insert (plist-get part :content))
340 (narrow-to-region start (point-max))
341 (notmuch-show-markup-citations))))
344 (defun notmuch-show-insert-part-text/* (part content-type depth)
345 ;; Handle all text types other than text/html.
346 (if (string-equal "text/html" content-type)
348 (notmuch-show-insert-part-header content-type)
349 (insert (plist-get part :content))
352 (defun notmuch-show-insert-part-*/* (part content-type depth)
353 (notmuch-show-insert-part-header content-type)
356 ;; Functions for determining how to handle MIME parts.
358 (defun notmuch-show-split-content-type (content-type)
359 (split-string content-type "/"))
361 (defun notmuch-show-handlers-for (content-type)
362 "Return a list of content handlers for a part of type CONTENT-TYPE."
367 ;; Reverse order of prefrence.
368 (list (intern (concat "notmuch-show-insert-part-*/*"))
370 "notmuch-show-insert-part-"
371 (car (notmuch-show-split-content-type content-type))
373 (intern (concat "notmuch-show-insert-part-" content-type))))
378 (defun notmuch-show-insert-bodypart (part depth)
379 "Insert the body part PART at depth DEPTH in the current thread."
380 (let* ((content-type (downcase (plist-get part :content-type)))
381 (handlers (notmuch-show-handlers-for content-type)))
382 ;; Run the content handlers until one of them returns a non-nil
385 (not (funcall (car handlers) part content-type depth)))
386 (setq handlers (cdr handlers))))
387 ;; Ensure that the part ends with a carriage return.
392 (defun notmuch-show-insert-body (body depth)
393 "Insert the body BODY at depth DEPTH in the current thread."
394 (mapc '(lambda (part) (notmuch-show-insert-bodypart part depth)) body))
396 (defun notmuch-show-make-symbol (type)
397 (make-symbol (concat "notmuch-show-" type)))
399 (defun notmuch-show-insert-msg (msg depth)
400 "Insert the message MSG at depth DEPTH in the current thread."
401 (let ((headers (plist-get msg :headers))
402 ;; Indentation causes the buffer offset of the start/end
403 ;; points to move, so we must use markers.
404 message-start message-end
405 content-start content-end
406 headers-start headers-end
408 (headers-invis-spec (notmuch-show-make-symbol "header"))
409 (body-invis-spec (notmuch-show-make-symbol "body")))
411 (setq message-start (point-marker))
413 (notmuch-show-insert-headerline headers
414 (or (plist-get msg :date_relative)
415 (plist-get headers :Date))
416 (plist-get msg :tags) depth)
418 (setq content-start (point-marker))
420 ;; Set `headers-start' to point after the 'Subject:' header to be
421 ;; compatible with the existing implementation. This just sets it
422 ;; to after the first header.
423 (notmuch-show-insert-headers headers)
424 ;; Headers should include a blank line (backwards compatibility).
427 (goto-char content-start)
429 (setq headers-start (point-marker)))
430 (setq headers-end (point-marker))
432 (setq body-start (point-marker))
433 (notmuch-show-insert-body (plist-get msg :body) depth)
434 ;; Ensure that the body ends with a newline.
437 (setq body-end (point-marker))
438 (setq content-end (point-marker))
440 ;; Indent according to the depth in the thread.
441 (indent-rigidly content-start content-end depth)
443 (setq message-end (point-max-marker))
445 ;; Save the extents of this message over the whole text of the
447 (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
449 (plist-put msg :headers-invis-spec headers-invis-spec)
450 (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
452 (plist-put msg :body-invis-spec body-invis-spec)
453 (overlay-put (make-overlay body-start body-end) 'invisible body-invis-spec)
455 ;; Save the properties for this message. Currently this saves the
456 ;; entire message (augmented it with other stuff), which seems
457 ;; like overkill. We might save a reduced subset (for example, not
459 (notmuch-show-set-message-properties msg)
461 ;; Headers are hidden by default.
462 (notmuch-show-headers-visible msg nil)
463 ;; Bodies are visible by default.
464 (notmuch-show-body-visible msg t)
466 ;; Message visibility depends on whether it matched the search
468 (notmuch-show-message-visible msg (plist-get msg :match))))
470 (defun notmuch-show-insert-tree (tree depth)
471 "Insert the message tree TREE at depth DEPTH in the current thread."
472 (let ((msg (car tree))
473 (replies (cadr tree)))
474 (notmuch-show-insert-msg msg depth)
475 (notmuch-show-insert-thread replies (1+ depth))))
477 (defun notmuch-show-insert-thread (thread depth)
478 "Insert the thread THREAD at depth DEPTH in the current forest."
479 (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
481 (defun notmuch-show-insert-forest (forest)
482 "Insert the forest of threads FOREST."
483 (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
485 (defvar notmuch-show-parent-buffer nil)
488 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
489 "Run \"notmuch show\" with the given thread ID and display results.
491 The optional PARENT-BUFFER is the notmuch-search buffer from
492 which this notmuch-show command was executed, (so that the
493 next thread from that buffer can be show when done with this
496 The optional QUERY-CONTEXT is a notmuch search term. Only
497 messages from the thread matching this search term are shown if
500 The optional BUFFER-NAME provides the neame of the buffer in
501 which the message thread is shown. If it is nil (which occurs
502 when the command is called interactively) the argument to the
504 (interactive "sNotmuch show: ")
505 (let ((buffer (get-buffer-create (generate-new-buffer-name
507 (concat "*notmuch-" thread-id "*")))))
508 (inhibit-read-only t))
509 (switch-to-buffer buffer)
511 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
513 (goto-char (point-min))
515 (let* ((basic-args (list thread-id))
516 (args (if query-context
517 (append basic-args (list "and (" query-context ")"))
519 (notmuch-show-insert-forest (notmuch-query-get-threads args))
520 ;; If the query context reduced the results to nothing, run
522 (when (and (eq (buffer-size) 0)
524 (notmuch-show-insert-forest
525 (notmuch-query-get-threads basic-args))))
526 (run-hooks 'notmuch-show-hook))
528 ;; Move straight to the first open message
529 (if (not (notmuch-show-message-visible-p))
530 (notmuch-show-next-open-message))
531 (notmuch-show-mark-read)))
533 (defvar notmuch-show-stash-map
534 (let ((map (make-sparse-keymap)))
535 (define-key map "c" 'notmuch-show-stash-cc)
536 (define-key map "d" 'notmuch-show-stash-date)
537 (define-key map "F" 'notmuch-show-stash-filename)
538 (define-key map "f" 'notmuch-show-stash-from)
539 (define-key map "i" 'notmuch-show-stash-message-id)
540 (define-key map "s" 'notmuch-show-stash-subject)
541 (define-key map "T" 'notmuch-show-stash-tags)
542 (define-key map "t" 'notmuch-show-stash-to)
544 "Submap for stash commands")
545 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
547 (defvar notmuch-show-mode-map
548 (let ((map (make-sparse-keymap)))
549 (define-key map "?" 'notmuch-help)
550 (define-key map "q" 'kill-this-buffer)
551 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
552 (define-key map (kbd "TAB") 'notmuch-show-next-button)
553 (define-key map "s" 'notmuch-search)
554 (define-key map "m" 'message-mail)
555 (define-key map "f" 'notmuch-show-forward-message)
556 (define-key map "r" 'notmuch-show-reply)
557 (define-key map "|" 'notmuch-show-pipe-message)
558 (define-key map "w" 'notmuch-show-save-attachments)
559 (define-key map "V" 'notmuch-show-view-raw-message)
560 (define-key map "v" 'notmuch-show-view-all-mime-parts)
561 (define-key map "c" 'notmuch-show-stash-map)
562 (define-key map "b" 'notmuch-show-toggle-body)
563 (define-key map "h" 'notmuch-show-toggle-headers)
564 (define-key map "-" 'notmuch-show-remove-tag)
565 (define-key map "+" 'notmuch-show-add-tag)
566 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
567 (define-key map "a" 'notmuch-show-archive-thread)
568 (define-key map "N" 'notmuch-show-next-message)
569 (define-key map "P" 'notmuch-show-previous-message)
570 (define-key map "n" 'notmuch-show-next-open-message)
571 (define-key map "p" 'notmuch-show-previous-open-message)
572 (define-key map (kbd "DEL") 'notmuch-show-rewind)
573 (define-key map " " 'notmuch-show-advance-and-archive)
574 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
576 "Keymap for \"notmuch show\" buffers.")
577 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
580 (defun notmuch-show-mode ()
581 "Major mode for viewing a thread with notmuch.
583 This buffer contains the results of the \"notmuch show\" command
584 for displaying a single thread of email from your email archives.
586 By default, various components of email messages, (citations,
587 signatures, already-read messages), are hidden. You can make
588 these parts visible by clicking with the mouse button or by
589 pressing RET after positioning the cursor on a hidden part, (for
590 which \\[notmuch-show-next-button] and
591 \\[notmuch-show-previous-button] are helpful).
593 Reading the thread sequentially is well-supported by pressing
594 \\[notmuch-show-advance-and-archive]. This will scroll the
595 current message (if necessary), advance to the next message, or
596 advance to the next thread (if already on the last message of a
599 Other commands are available to read or manipulate the thread
600 more selectively, (such as '\\[notmuch-show-next-message]' and
601 '\\[notmuch-show-previous-message]' to advance to messages
602 without removing any tags, and '\\[notmuch-show-archive-thread]'
603 to archive an entire thread without scrolling through with
604 \\[notmuch-show-advance-and-archive]).
606 You can add or remove arbitary tags from the current message with
607 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
609 All currently available key bindings:
611 \\{notmuch-show-mode-map}"
613 (kill-all-local-variables)
614 (use-local-map notmuch-show-mode-map)
615 (setq major-mode 'notmuch-show-mode
616 mode-name "notmuch-show")
617 (setq buffer-read-only t))
619 (defun notmuch-show-move-to-message-top ()
620 (goto-char (notmuch-show-message-top)))
622 (defun notmuch-show-move-to-message-bottom ()
623 (goto-char (notmuch-show-message-bottom)))
625 (defun notmuch-show-message-adjust ()
628 ;; Movement related functions.
630 ;; There's some strangeness here where a text property applied to a
631 ;; region a->b is not found when point is at b. We walk backwards
632 ;; until finding the property.
633 (defun notmuch-show-message-extent ()
636 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
640 (defun notmuch-show-message-top ()
641 (car (notmuch-show-message-extent)))
643 (defun notmuch-show-message-bottom ()
644 (cdr (notmuch-show-message-extent)))
646 (defun notmuch-show-goto-message-next ()
647 (let ((start (point)))
648 (notmuch-show-move-to-message-bottom)
654 (defun notmuch-show-goto-message-previous ()
655 (notmuch-show-move-to-message-top)
659 (notmuch-show-move-to-message-top)
662 (defun notmuch-show-move-past-invisible-forward ()
663 (while (point-invisible-p)
666 (defun notmuch-show-move-past-invisible-backward ()
667 (while (point-invisible-p)
670 ;; Functions relating to the visibility of messages and their
673 (defun notmuch-show-element-visible (props visible-p spec-property)
674 (let ((spec (plist-get props spec-property)))
676 (remove-from-invisibility-spec spec)
677 (add-to-invisibility-spec spec))))
679 (defun notmuch-show-message-visible (props visible-p)
681 ;; If we're making the message visible then the visibility of
682 ;; the constituent elements depends on their own properties, not
683 ;; that of the message as a whole.
684 (let ((headers-visible (plist-get props :headers-visible))
685 (body-visible (plist-get props :body-visible)))
686 (notmuch-show-element-visible props headers-visible :headers-invis-spec)
687 (notmuch-show-element-visible props body-visible :body-invis-spec))
688 (notmuch-show-element-visible props nil :headers-invis-spec)
689 (notmuch-show-element-visible props nil :body-invis-spec))
691 (notmuch-show-set-prop :message-visible visible-p props))
693 (defun notmuch-show-headers-visible (props visible-p)
694 (if (plist-get props :message-visible)
695 (notmuch-show-element-visible props visible-p :headers-invis-spec))
696 (notmuch-show-set-prop :headers-visible visible-p props))
698 (defun notmuch-show-body-visible (props visible-p)
699 (if (plist-get props :message-visible)
700 (notmuch-show-element-visible props visible-p :body-invis-spec))
701 (notmuch-show-set-prop :body-visible visible-p))
703 ;; Functions for setting and getting attributes of the current
706 (defun notmuch-show-set-message-properties (props)
708 (notmuch-show-move-to-message-top)
709 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
711 (defun notmuch-show-get-message-properties ()
713 (notmuch-show-move-to-message-top)
714 (get-text-property (point) :notmuch-message-properties)))
716 (defun notmuch-show-set-prop (prop val &optional props)
717 (let ((inhibit-read-only t)
719 (notmuch-show-get-message-properties))))
720 (plist-put props prop val)
721 (notmuch-show-set-message-properties props)))
723 (defun notmuch-show-get-prop (prop &optional props)
724 (let ((props (or props
725 (notmuch-show-get-message-properties))))
726 (plist-get props prop)))
728 (defun notmuch-show-get-message-id ()
729 "Return the message id of the current message."
730 (concat "id:" (notmuch-show-get-prop :id)))
732 ;; dme: Would it make sense to use a macro for many of these?
734 (defun notmuch-show-get-filename ()
735 "Return the filename of the current message."
736 (notmuch-show-get-prop :filename))
738 (defun notmuch-show-get-header (header)
739 "Return the named header of the current message, if any."
740 (plist-get (notmuch-show-get-prop :headers) header))
742 (defun notmuch-show-get-cc ()
743 (notmuch-show-get-header :Cc))
745 (defun notmuch-show-get-date ()
746 (notmuch-show-get-header :Date))
748 (defun notmuch-show-get-from ()
749 (notmuch-show-get-header :From))
751 (defun notmuch-show-get-subject ()
752 (notmuch-show-get-header :Subject))
754 (defun notmuch-show-get-to ()
755 (notmuch-show-get-header :To))
757 (defun notmuch-show-set-tags (tags)
758 "Set the tags of the current message."
759 (notmuch-show-set-prop :tags tags)
760 (notmuch-show-update-tags tags))
762 (defun notmuch-show-get-tags ()
763 "Return the tags of the current message."
764 (notmuch-show-get-prop :tags))
766 (defun notmuch-show-message-visible-p ()
767 "Is the current message visible?"
768 (notmuch-show-get-prop :message-visible))
770 (defun notmuch-show-body-visible-p ()
771 "Is the body of the current message visible?"
772 (notmuch-show-get-prop :body-visible))
774 (defun notmuch-show-headers-visible-p ()
775 "Are the headers of the current message visible?"
776 (notmuch-show-get-prop :headers-visible))
778 (defun notmuch-show-mark-read ()
779 "Mark the current message as read."
780 (notmuch-show-remove-tag "unread"))
782 ;; Commands typically bound to keys.
784 (defun notmuch-show-advance-and-archive ()
785 "Advance through thread and archive.
787 This command is intended to be one of the simplest ways to
788 process a thread of email. It does the following:
790 If the current message in the thread is not yet fully visible,
791 scroll by a near screenful to read more of the message.
793 Otherwise, (the end of the current message is already within the
794 current window), advance to the next open message.
796 Finally, if there is no further message to advance to, and this
797 last message is already read, then archive the entire current
798 thread, (remove the \"inbox\" tag from each message). Also kill
799 this buffer, and display the next thread from the search from
800 which this thread was originally shown."
802 (let ((end-of-this-message (notmuch-show-message-bottom)))
804 ;; Ideally we would test `end-of-this-message' against the result
805 ;; of `window-end', but that doesn't account for the fact that
806 ;; the end of the message might be hidden, so we have to actually
807 ;; go to the end, walk back over invisible text and then see if
810 (goto-char (- end-of-this-message 1))
811 (notmuch-show-move-past-invisible-backward)
812 (> (point) (window-end)))
813 ;; The bottom of this message is not visible - scroll.
816 ((not (= end-of-this-message (point-max)))
817 ;; This is not the last message - move to the next visible one.
818 (notmuch-show-next-open-message))
821 ;; This is the last message - archive the thread.
822 (notmuch-show-archive-thread)))))
824 (defun notmuch-show-rewind ()
825 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
827 Specifically, if the beginning of the previous email is fewer
828 than `window-height' lines from the current point, move to it
829 just like `notmuch-show-previous-message'.
831 Otherwise, just scroll down a screenful of the current message.
833 This command does not modify any message tags, (it does not undo
834 any effects from previous calls to
835 `notmuch-show-advance-and-archive'."
837 (let ((start-of-message (notmuch-show-message-top))
838 (start-of-window (window-start)))
840 ;; Either this message is properly aligned with the start of the
841 ;; window or the start of this message is not visible on the
843 ((or (= start-of-message start-of-window)
844 (< start-of-message start-of-window))
846 ;; If a small number of lines from the previous message are
847 ;; visible, realign so that the top of the current message is at
848 ;; the top of the screen.
849 (if (< (count-lines (window-start) (notmuch-show-message-top))
850 next-screen-context-lines)
852 (goto-char (notmuch-show-message-top))
853 (notmuch-show-message-adjust)))
854 ;; Move to the top left of the window.
855 (goto-char (window-start)))
857 ;; Move to the previous message.
858 (notmuch-show-previous-message)))))
860 (defun notmuch-show-reply ()
861 "Reply to the current message."
863 (notmuch-reply (notmuch-show-get-message-id)))
865 (defun notmuch-show-forward-message ()
866 "Forward the current message."
868 (with-current-notmuch-show-message
871 (defun notmuch-show-next-message ()
872 "Show the next message."
874 (notmuch-show-goto-message-next)
875 (notmuch-show-mark-read)
876 (notmuch-show-message-adjust))
878 (defun notmuch-show-previous-message ()
879 "Show the previous message."
881 (notmuch-show-goto-message-previous)
882 (notmuch-show-mark-read)
883 (notmuch-show-message-adjust))
885 (defun notmuch-show-next-open-message ()
886 "Show the next message."
888 (while (and (notmuch-show-goto-message-next)
889 (not (notmuch-show-message-visible-p))))
890 (notmuch-show-mark-read)
891 (notmuch-show-message-adjust))
893 (defun notmuch-show-previous-open-message ()
894 "Show the previous message."
896 (while (and (notmuch-show-goto-message-previous)
897 (not (notmuch-show-message-visible-p))))
898 (notmuch-show-mark-read)
899 (notmuch-show-message-adjust))
901 (defun notmuch-show-view-raw-message ()
902 "View the file holding the current message."
904 (view-file (notmuch-show-get-filename)))
906 (defun notmuch-show-pipe-message (command)
907 "Pipe the contents of the current message to the given command.
909 The given command will be executed with the raw contents of the
910 current email message as stdin. Anything printed by the command
911 to stdout or stderr will appear in the *Messages* buffer."
912 (interactive "sPipe message to command: ")
913 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
915 (shell-quote-argument (notmuch-show-get-filename)))))
917 (defun notmuch-show-add-tag (&rest toadd)
918 "Add a tag to the current message."
920 (list (notmuch-select-tag-with-completion "Tag to add: ")))
921 (apply 'notmuch-call-notmuch-process
923 (mapcar (lambda (s) (concat "+" s)) toadd))
924 (cons (notmuch-show-get-message-id) nil)))
925 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
927 (defun notmuch-show-remove-tag (&rest toremove)
928 "Remove a tag from the current message."
930 (list (notmuch-select-tag-with-completion
931 "Tag to remove: " (notmuch-show-get-message-id))))
932 (let ((tags (notmuch-show-get-tags)))
933 (if (intersection tags toremove :test 'string=)
935 (apply 'notmuch-call-notmuch-process
937 (mapcar (lambda (s) (concat "-" s)) toremove))
938 (cons (notmuch-show-get-message-id) nil)))
939 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
941 (defun notmuch-show-toggle-headers ()
942 "Toggle the visibility of the current message headers."
944 (let ((props (notmuch-show-get-message-properties)))
945 (notmuch-show-headers-visible
947 (not (plist-get props :headers-visible))))
948 (force-window-update))
950 (defun notmuch-show-toggle-body ()
951 "Toggle the visibility of the current message body."
953 (let ((props (notmuch-show-get-message-properties)))
954 (notmuch-show-body-visible
956 (not (plist-get props :body-visible))))
957 (force-window-update))
959 (defun notmuch-show-toggle-message ()
960 "Toggle the visibility of the current message."
962 (let ((props (notmuch-show-get-message-properties)))
963 (notmuch-show-message-visible
965 (not (plist-get props :message-visible))))
966 (force-window-update))
968 (defun notmuch-show-next-button ()
969 "Advance point to the next button in the buffer."
973 (defun notmuch-show-previous-button ()
974 "Move point back to the previous button in the buffer."
978 (defun notmuch-show-archive-thread-internal (show-next)
979 ;; Remove the tag from the current set of messages.
980 (goto-char (point-min))
981 (loop do (notmuch-show-remove-tag "inbox")
982 until (not (notmuch-show-goto-message-next)))
983 ;; Move to the next item in the search results, if any.
984 (let ((parent-buffer notmuch-show-parent-buffer))
988 (switch-to-buffer parent-buffer)
991 (notmuch-search-show-thread))))))
993 (defun notmuch-show-archive-thread ()
994 "Archive each message in thread, then show next thread from search.
996 Archive each message currently shown by removing the \"inbox\"
997 tag from each. Then kill this buffer and show the next thread
998 from the search from which this thread was originally shown.
1000 Note: This command is safe from any race condition of new messages
1001 being delivered to the same thread. It does not archive the
1002 entire thread, but only the messages shown in the current
1005 (notmuch-show-archive-thread-internal t))
1007 (defun notmuch-show-archive-thread-then-exit ()
1008 "Archive each message in thread, then exit back to search results."
1010 (notmuch-show-archive-thread-internal nil))
1012 (defun notmuch-show-do-stash (text)
1014 (message "Saved: %s" text))
1016 (defun notmuch-show-stash-cc ()
1017 "Copy CC field of current message to kill-ring."
1019 (notmuch-show-do-stash (notmuch-show-get-cc)))
1021 (defun notmuch-show-stash-date ()
1022 "Copy date of current message to kill-ring."
1024 (notmuch-show-do-stash (notmuch-show-get-date)))
1026 (defun notmuch-show-stash-filename ()
1027 "Copy filename of current message to kill-ring."
1029 (notmuch-show-do-stash (notmuch-show-get-filename)))
1031 (defun notmuch-show-stash-from ()
1032 "Copy From address of current message to kill-ring."
1034 (notmuch-show-do-stash (notmuch-show-get-from)))
1036 (defun notmuch-show-stash-message-id ()
1037 "Copy message ID of current message to kill-ring."
1039 (notmuch-show-do-stash (notmuch-show-get-message-id)))
1041 (defun notmuch-show-stash-subject ()
1042 "Copy Subject field of current message to kill-ring."
1044 (notmuch-show-do-stash (notmuch-show-get-subject)))
1046 (defun notmuch-show-stash-tags ()
1047 "Copy tags of current message to kill-ring as a comma separated list."
1049 (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1051 (defun notmuch-show-stash-to ()
1052 "Copy To address of current message to kill-ring."
1054 (notmuch-show-do-stash (notmuch-show-get-to)))
1058 (provide 'notmuch-show)