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)
464 ;; Message visibility depends on whether it matched the search
466 (notmuch-show-message-visible msg (plist-get msg :match))))
468 (defun notmuch-show-insert-tree (tree depth)
469 "Insert the message tree TREE at depth DEPTH in the current thread."
470 (let ((msg (car tree))
471 (replies (cadr tree)))
472 (notmuch-show-insert-msg msg depth)
473 (notmuch-show-insert-thread replies (1+ depth))))
475 (defun notmuch-show-insert-thread (thread depth)
476 "Insert the thread THREAD at depth DEPTH in the current forest."
477 (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
479 (defun notmuch-show-insert-forest (forest)
480 "Insert the forest of threads FOREST."
481 (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
483 (defvar notmuch-show-parent-buffer nil)
486 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
487 "Run \"notmuch show\" with the given thread ID and display results.
489 The optional PARENT-BUFFER is the notmuch-search buffer from
490 which this notmuch-show command was executed, (so that the
491 next thread from that buffer can be show when done with this
494 The optional QUERY-CONTEXT is a notmuch search term. Only
495 messages from the thread matching this search term are shown if
498 The optional BUFFER-NAME provides the neame of the buffer in
499 which the message thread is shown. If it is nil (which occurs
500 when the command is called interactively) the argument to the
502 (interactive "sNotmuch show: ")
503 (let ((buffer (get-buffer-create (generate-new-buffer-name
505 (concat "*notmuch-" thread-id "*")))))
506 (inhibit-read-only t))
507 (switch-to-buffer buffer)
509 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
511 (goto-char (point-min))
513 (let* ((basic-args (list thread-id))
514 (args (if query-context
515 (append basic-args (list "and (" query-context ")"))
517 (notmuch-show-insert-forest (notmuch-query-get-threads args))
518 ;; If the query context reduced the results to nothing, run
520 (when (and (eq (buffer-size) 0)
522 (notmuch-show-insert-forest
523 (notmuch-query-get-threads basic-args))))
524 (run-hooks 'notmuch-show-hook))
526 ;; Move straight to the first open message
527 (if (not (notmuch-show-message-visible-p))
528 (notmuch-show-next-open-message))
529 (notmuch-show-mark-read)))
531 (defvar notmuch-show-stash-map
532 (let ((map (make-sparse-keymap)))
533 (define-key map "c" 'notmuch-show-stash-cc)
534 (define-key map "d" 'notmuch-show-stash-date)
535 (define-key map "F" 'notmuch-show-stash-filename)
536 (define-key map "f" 'notmuch-show-stash-from)
537 (define-key map "i" 'notmuch-show-stash-message-id)
538 (define-key map "s" 'notmuch-show-stash-subject)
539 (define-key map "T" 'notmuch-show-stash-tags)
540 (define-key map "t" 'notmuch-show-stash-to)
542 "Submap for stash commands")
543 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
545 (defvar notmuch-show-mode-map
546 (let ((map (make-sparse-keymap)))
547 (define-key map "?" 'notmuch-help)
548 (define-key map "q" 'kill-this-buffer)
549 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
550 (define-key map (kbd "TAB") 'notmuch-show-next-button)
551 (define-key map "s" 'notmuch-search)
552 (define-key map "m" 'message-mail)
553 (define-key map "f" 'notmuch-show-forward-message)
554 (define-key map "r" 'notmuch-show-reply)
555 (define-key map "|" 'notmuch-show-pipe-message)
556 (define-key map "w" 'notmuch-show-save-attachments)
557 (define-key map "V" 'notmuch-show-view-raw-message)
558 (define-key map "v" 'notmuch-show-view-all-mime-parts)
559 (define-key map "c" 'notmuch-show-stash-map)
560 (define-key map "h" 'notmuch-show-toggle-headers)
561 (define-key map "-" 'notmuch-show-remove-tag)
562 (define-key map "+" 'notmuch-show-add-tag)
563 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
564 (define-key map "a" 'notmuch-show-archive-thread)
565 (define-key map "N" 'notmuch-show-next-message)
566 (define-key map "P" 'notmuch-show-previous-message)
567 (define-key map "n" 'notmuch-show-next-open-message)
568 (define-key map "p" 'notmuch-show-previous-open-message)
569 (define-key map (kbd "DEL") 'notmuch-show-rewind)
570 (define-key map " " 'notmuch-show-advance-and-archive)
571 (define-key map (kbd "RET") 'notmuch-show-toggle-message)
573 "Keymap for \"notmuch show\" buffers.")
574 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
577 (defun notmuch-show-mode ()
578 "Major mode for viewing a thread with notmuch.
580 This buffer contains the results of the \"notmuch show\" command
581 for displaying a single thread of email from your email archives.
583 By default, various components of email messages, (citations,
584 signatures, already-read messages), are hidden. You can make
585 these parts visible by clicking with the mouse button or by
586 pressing RET after positioning the cursor on a hidden part, (for
587 which \\[notmuch-show-next-button] and
588 \\[notmuch-show-previous-button] are helpful).
590 Reading the thread sequentially is well-supported by pressing
591 \\[notmuch-show-advance-and-archive]. This will scroll the
592 current message (if necessary), advance to the next message, or
593 advance to the next thread (if already on the last message of a
596 Other commands are available to read or manipulate the thread
597 more selectively, (such as '\\[notmuch-show-next-message]' and
598 '\\[notmuch-show-previous-message]' to advance to messages
599 without removing any tags, and '\\[notmuch-show-archive-thread]'
600 to archive an entire thread without scrolling through with
601 \\[notmuch-show-advance-and-archive]).
603 You can add or remove arbitary tags from the current message with
604 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
606 All currently available key bindings:
608 \\{notmuch-show-mode-map}"
610 (kill-all-local-variables)
611 (use-local-map notmuch-show-mode-map)
612 (setq major-mode 'notmuch-show-mode
613 mode-name "notmuch-show")
614 (setq buffer-read-only t))
616 (defun notmuch-show-move-to-message-top ()
617 (goto-char (notmuch-show-message-top)))
619 (defun notmuch-show-move-to-message-bottom ()
620 (goto-char (notmuch-show-message-bottom)))
622 (defun notmuch-show-message-adjust ()
625 ;; Movement related functions.
627 ;; There's some strangeness here where a text property applied to a
628 ;; region a->b is not found when point is at b. We walk backwards
629 ;; until finding the property.
630 (defun notmuch-show-message-extent ()
633 (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
637 (defun notmuch-show-message-top ()
638 (car (notmuch-show-message-extent)))
640 (defun notmuch-show-message-bottom ()
641 (cdr (notmuch-show-message-extent)))
643 (defun notmuch-show-goto-message-next ()
644 (let ((start (point)))
645 (notmuch-show-move-to-message-bottom)
651 (defun notmuch-show-goto-message-previous ()
652 (notmuch-show-move-to-message-top)
656 (notmuch-show-move-to-message-top)
659 (defun notmuch-show-move-past-invisible-forward ()
660 (while (point-invisible-p)
663 (defun notmuch-show-move-past-invisible-backward ()
664 (while (point-invisible-p)
667 ;; Functions relating to the visibility of messages and their
670 (defun notmuch-show-element-visible (props visible-p spec-property)
671 (let ((spec (plist-get props spec-property)))
673 (remove-from-invisibility-spec spec)
674 (add-to-invisibility-spec spec))))
676 (defun notmuch-show-message-visible (props visible-p)
678 ;; When making the message visible, the headers may or not be
679 ;; visible. So we check that property separately.
680 (let ((headers-visible (plist-get props :headers-visible)))
681 (notmuch-show-element-visible props headers-visible :headers-invis-spec)
682 (notmuch-show-element-visible props t :body-invis-spec))
683 (notmuch-show-element-visible props nil :headers-invis-spec)
684 (notmuch-show-element-visible props nil :body-invis-spec))
686 (notmuch-show-set-prop :message-visible visible-p props))
688 (defun notmuch-show-headers-visible (props visible-p)
689 (if (plist-get props :message-visible)
690 (notmuch-show-element-visible props visible-p :headers-invis-spec))
691 (notmuch-show-set-prop :headers-visible visible-p props))
693 ;; Functions for setting and getting attributes of the current
696 (defun notmuch-show-set-message-properties (props)
698 (notmuch-show-move-to-message-top)
699 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
701 (defun notmuch-show-get-message-properties ()
703 (notmuch-show-move-to-message-top)
704 (get-text-property (point) :notmuch-message-properties)))
706 (defun notmuch-show-set-prop (prop val &optional props)
707 (let ((inhibit-read-only t)
709 (notmuch-show-get-message-properties))))
710 (plist-put props prop val)
711 (notmuch-show-set-message-properties props)))
713 (defun notmuch-show-get-prop (prop &optional props)
714 (let ((props (or props
715 (notmuch-show-get-message-properties))))
716 (plist-get props prop)))
718 (defun notmuch-show-get-message-id ()
719 "Return the message id of the current message."
720 (concat "id:" (notmuch-show-get-prop :id)))
722 ;; dme: Would it make sense to use a macro for many of these?
724 (defun notmuch-show-get-filename ()
725 "Return the filename of the current message."
726 (notmuch-show-get-prop :filename))
728 (defun notmuch-show-get-header (header)
729 "Return the named header of the current message, if any."
730 (plist-get (notmuch-show-get-prop :headers) header))
732 (defun notmuch-show-get-cc ()
733 (notmuch-show-get-header :Cc))
735 (defun notmuch-show-get-date ()
736 (notmuch-show-get-header :Date))
738 (defun notmuch-show-get-from ()
739 (notmuch-show-get-header :From))
741 (defun notmuch-show-get-subject ()
742 (notmuch-show-get-header :Subject))
744 (defun notmuch-show-get-to ()
745 (notmuch-show-get-header :To))
747 (defun notmuch-show-set-tags (tags)
748 "Set the tags of the current message."
749 (notmuch-show-set-prop :tags tags)
750 (notmuch-show-update-tags tags))
752 (defun notmuch-show-get-tags ()
753 "Return the tags of the current message."
754 (notmuch-show-get-prop :tags))
756 (defun notmuch-show-message-visible-p ()
757 "Is the current message visible?"
758 (notmuch-show-get-prop :message-visible))
760 (defun notmuch-show-headers-visible-p ()
761 "Are the headers of the current message visible?"
762 (notmuch-show-get-prop :headers-visible))
764 (defun notmuch-show-mark-read ()
765 "Mark the current message as read."
766 (notmuch-show-remove-tag "unread"))
768 ;; Commands typically bound to keys.
770 (defun notmuch-show-advance-and-archive ()
771 "Advance through thread and archive.
773 This command is intended to be one of the simplest ways to
774 process a thread of email. It does the following:
776 If the current message in the thread is not yet fully visible,
777 scroll by a near screenful to read more of the message.
779 Otherwise, (the end of the current message is already within the
780 current window), advance to the next open message.
782 Finally, if there is no further message to advance to, and this
783 last message is already read, then archive the entire current
784 thread, (remove the \"inbox\" tag from each message). Also kill
785 this buffer, and display the next thread from the search from
786 which this thread was originally shown."
788 (let ((end-of-this-message (notmuch-show-message-bottom)))
790 ;; Ideally we would test `end-of-this-message' against the result
791 ;; of `window-end', but that doesn't account for the fact that
792 ;; the end of the message might be hidden, so we have to actually
793 ;; go to the end, walk back over invisible text and then see if
796 (goto-char (- end-of-this-message 1))
797 (notmuch-show-move-past-invisible-backward)
798 (> (point) (window-end)))
799 ;; The bottom of this message is not visible - scroll.
802 ((not (= end-of-this-message (point-max)))
803 ;; This is not the last message - move to the next visible one.
804 (notmuch-show-next-open-message))
807 ;; This is the last message - archive the thread.
808 (notmuch-show-archive-thread)))))
810 (defun notmuch-show-rewind ()
811 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
813 Specifically, if the beginning of the previous email is fewer
814 than `window-height' lines from the current point, move to it
815 just like `notmuch-show-previous-message'.
817 Otherwise, just scroll down a screenful of the current message.
819 This command does not modify any message tags, (it does not undo
820 any effects from previous calls to
821 `notmuch-show-advance-and-archive'."
823 (let ((start-of-message (notmuch-show-message-top))
824 (start-of-window (window-start)))
826 ;; Either this message is properly aligned with the start of the
827 ;; window or the start of this message is not visible on the
829 ((or (= start-of-message start-of-window)
830 (< start-of-message start-of-window))
832 ;; If a small number of lines from the previous message are
833 ;; visible, realign so that the top of the current message is at
834 ;; the top of the screen.
835 (if (< (count-lines (window-start) (notmuch-show-message-top))
836 next-screen-context-lines)
838 (goto-char (notmuch-show-message-top))
839 (notmuch-show-message-adjust)))
840 ;; Move to the top left of the window.
841 (goto-char (window-start)))
843 ;; Move to the previous message.
844 (notmuch-show-previous-message)))))
846 (defun notmuch-show-reply ()
847 "Reply to the current message."
849 (notmuch-reply (notmuch-show-get-message-id)))
851 (defun notmuch-show-forward-message ()
852 "Forward the current message."
854 (with-current-notmuch-show-message
857 (defun notmuch-show-next-message ()
858 "Show the next message."
860 (notmuch-show-goto-message-next)
861 (notmuch-show-mark-read)
862 (notmuch-show-message-adjust))
864 (defun notmuch-show-previous-message ()
865 "Show the previous message."
867 (notmuch-show-goto-message-previous)
868 (notmuch-show-mark-read)
869 (notmuch-show-message-adjust))
871 (defun notmuch-show-next-open-message ()
872 "Show the next message."
874 (while (and (notmuch-show-goto-message-next)
875 (not (notmuch-show-message-visible-p))))
876 (notmuch-show-mark-read)
877 (notmuch-show-message-adjust))
879 (defun notmuch-show-previous-open-message ()
880 "Show the previous message."
882 (while (and (notmuch-show-goto-message-previous)
883 (not (notmuch-show-message-visible-p))))
884 (notmuch-show-mark-read)
885 (notmuch-show-message-adjust))
887 (defun notmuch-show-view-raw-message ()
888 "View the file holding the current message."
890 (view-file (notmuch-show-get-filename)))
892 (defun notmuch-show-pipe-message (command)
893 "Pipe the contents of the current message to the given command.
895 The given command will be executed with the raw contents of the
896 current email message as stdin. Anything printed by the command
897 to stdout or stderr will appear in the *Messages* buffer."
898 (interactive "sPipe message to command: ")
899 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
901 (shell-quote-argument (notmuch-show-get-filename)))))
903 (defun notmuch-show-add-tag (&rest toadd)
904 "Add a tag to the current message."
906 (list (notmuch-select-tag-with-completion "Tag to add: ")))
907 (apply 'notmuch-call-notmuch-process
909 (mapcar (lambda (s) (concat "+" s)) toadd))
910 (cons (notmuch-show-get-message-id) nil)))
911 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
913 (defun notmuch-show-remove-tag (&rest toremove)
914 "Remove a tag from the current message."
916 (list (notmuch-select-tag-with-completion
917 "Tag to remove: " (notmuch-show-get-message-id))))
918 (let ((tags (notmuch-show-get-tags)))
919 (if (intersection tags toremove :test 'string=)
921 (apply 'notmuch-call-notmuch-process
923 (mapcar (lambda (s) (concat "-" s)) toremove))
924 (cons (notmuch-show-get-message-id) nil)))
925 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
927 (defun notmuch-show-toggle-headers ()
928 "Toggle the visibility of the current message headers."
930 (let ((props (notmuch-show-get-message-properties)))
931 (notmuch-show-headers-visible
933 (not (plist-get props :headers-visible))))
934 (force-window-update))
936 (defun notmuch-show-toggle-message ()
937 "Toggle the visibility of the current message."
939 (let ((props (notmuch-show-get-message-properties)))
940 (notmuch-show-message-visible
942 (not (plist-get props :message-visible))))
943 (force-window-update))
945 (defun notmuch-show-next-button ()
946 "Advance point to the next button in the buffer."
950 (defun notmuch-show-previous-button ()
951 "Move point back to the previous button in the buffer."
955 (defun notmuch-show-archive-thread-internal (show-next)
956 ;; Remove the tag from the current set of messages.
957 (goto-char (point-min))
958 (loop do (notmuch-show-remove-tag "inbox")
959 until (not (notmuch-show-goto-message-next)))
960 ;; Move to the next item in the search results, if any.
961 (let ((parent-buffer notmuch-show-parent-buffer))
965 (switch-to-buffer parent-buffer)
968 (notmuch-search-show-thread))))))
970 (defun notmuch-show-archive-thread ()
971 "Archive each message in thread, then show next thread from search.
973 Archive each message currently shown by removing the \"inbox\"
974 tag from each. Then kill this buffer and show the next thread
975 from the search from which this thread was originally shown.
977 Note: This command is safe from any race condition of new messages
978 being delivered to the same thread. It does not archive the
979 entire thread, but only the messages shown in the current
982 (notmuch-show-archive-thread-internal t))
984 (defun notmuch-show-archive-thread-then-exit ()
985 "Archive each message in thread, then exit back to search results."
987 (notmuch-show-archive-thread-internal nil))
989 (defun notmuch-show-do-stash (text)
991 (message "Saved: %s" text))
993 (defun notmuch-show-stash-cc ()
994 "Copy CC field of current message to kill-ring."
996 (notmuch-show-do-stash (notmuch-show-get-cc)))
998 (defun notmuch-show-stash-date ()
999 "Copy date of current message to kill-ring."
1001 (notmuch-show-do-stash (notmuch-show-get-date)))
1003 (defun notmuch-show-stash-filename ()
1004 "Copy filename of current message to kill-ring."
1006 (notmuch-show-do-stash (notmuch-show-get-filename)))
1008 (defun notmuch-show-stash-from ()
1009 "Copy From address of current message to kill-ring."
1011 (notmuch-show-do-stash (notmuch-show-get-from)))
1013 (defun notmuch-show-stash-message-id ()
1014 "Copy message ID of current message to kill-ring."
1016 (notmuch-show-do-stash (notmuch-show-get-message-id)))
1018 (defun notmuch-show-stash-subject ()
1019 "Copy Subject field of current message to kill-ring."
1021 (notmuch-show-do-stash (notmuch-show-get-subject)))
1023 (defun notmuch-show-stash-tags ()
1024 "Copy tags of current message to kill-ring as a comma separated list."
1026 (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1028 (defun notmuch-show-stash-to ()
1029 "Copy To address of current message to kill-ring."
1031 (notmuch-show-do-stash (notmuch-show-get-to)))
1035 (provide 'notmuch-show)