]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-show.el
c17261692bc30c03eef9f720a9c0f5fa48668813
[notmuch] / emacs / notmuch-show.el
1 ;; notmuch-show.el --- displaying notmuch forests.
2 ;;
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
5 ;;
6 ;; This file is part of Notmuch.
7 ;;
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.
12 ;;
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.
17 ;;
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/>.
20 ;;
21 ;; Authors: Carl Worth <cworth@cworth.org>
22 ;;          David Edmondson <dme@dme.org>
23
24 (require 'cl)
25 (require 'mm-view)
26 (require 'message)
27
28 (require 'notmuch-lib)
29 (require 'notmuch-query)
30
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)
36
37 (defvar notmuch-show-citation-regexp
38   "\\(?:^[[:space:]]>.*\n\\(?:[[:space:]]*\n[[:space:]]>.*\n\\)?\\)+"
39   "Pattern to match citation lines.")
40
41 (defvar notmuch-show-signature-regexp
42   "^\\(-- ?\\|_+\\)$"
43   "Pattern to match a line that separates content from signature.")
44
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
48
49 Can use up to one integer format parameter, i.e. %d")
50
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.
54
55 Can use up to one integer format parameter, i.e. %d")
56
57 (defvar notmuch-show-signature-lines-max 12
58   "Maximum length of signature that will be hidden by default.")
59
60 (defvar notmuch-show-citation-lines-prefix 3
61   "Always show at least this many lines at the start of a citation.
62
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.")
67
68 (defvar notmuch-show-citation-lines-suffix 3
69   "Always show at least this many lines at the end of a citation.
70
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.")
75
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.")
80
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'.")
84
85 (defvar notmuch-show-hook '(notmuch-show-pretty-hook)
86   "A list of functions called after populating a
87 `notmuch-show' buffer.")
88
89 (defun notmuch-show-pretty-hook ()
90   (goto-address-mode 1)
91   (visual-line-mode))
92
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)
98           ))
99   (force-window-update)
100   (redisplay t))
101
102 (define-button-type 'notmuch-button-invisibility-toggle-type
103   'action 'notmuch-toggle-invisible-action
104   'follow-link t
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)
115
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
118
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."
123
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.
128
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)
135     (goto-char (1+ end))
136     (save-excursion
137       (goto-char (1- beg))
138       (insert prefix)
139       (insert-button button-text
140                      'invisibility-spec invis-spec
141                      :type button-type))))
142
143 (defmacro with-current-notmuch-show-message (&rest body)
144   "Evaluate body with current buffer set to the text of current message"
145   `(save-excursion
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)
150            ,@body)
151          (kill-buffer buf)))))
152
153 (defun notmuch-show-view-all-mime-parts ()
154   "Use external viewers to view all attachments from the current message."
155   (interactive)
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.
162    ;
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)
171                                  )))
172      (mm-display-parts (mm-dissect-buffer)))))
173
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)))))
182
183 (defun notmuch-count-attachments (mm-handle)
184   (let ((count 0))
185     (notmuch-foreach-mime-part
186      (lambda (p)
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)))
192               (incf count))))
193      mm-handle)
194     count))
195
196 (defun notmuch-save-attachments (mm-handle &optional queryp)
197   (notmuch-foreach-mime-part
198    (lambda (p)
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)))
204             (or (not queryp)
205                 (y-or-n-p
206                  (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
207             (mm-save-part p))))
208    mm-handle))
209
210 (defun notmuch-show-save-attachments ()
211   "Save all attachments from the current message."
212   (interactive)
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))))
217   (message "Done"))
218
219 (defun notmuch-show-fontify-header ()
220   (let ((face (cond
221                ((looking-at "[Tt]o:")
222                 'message-header-to)
223                ((looking-at "[Bb]?[Cc][Cc]:")
224                 'message-header-cc)
225                ((looking-at "[Ss]ubject:")
226                 'message-header-subject)
227                ((looking-at "[Ff]rom:")
228                 'message-header-from)
229                (t
230                 'message-header-other))))
231
232     (overlay-put (make-overlay (point) (re-search-forward ":"))
233                  'face 'message-header-name)
234     (overlay-put (make-overlay (point) (re-search-forward ".*$"))
235                  'face face)))
236
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)
242     (forward-line)))
243
244 (defun notmuch-show-spaces-n (n)
245   "Return a string comprised of `n' spaces."
246   (make-string n ? ))
247
248 (defun notmuch-show-update-tags (tags)
249   "Update the displayed tags of the current message."
250   (save-excursion
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 " ")
256                                  ")"))))))
257
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)
264             " ("
265             date
266             ") ("
267             (mapconcat 'identity tags " ")
268             ")\n")
269     (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
270
271 (defun notmuch-show-insert-header (header header-value)
272   "Insert a single header."
273   (insert header ": " header-value "\n"))
274
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)
285     (save-excursion
286       (save-restriction
287         (narrow-to-region start (point-max))
288         (run-hooks 'notmuch-show-markup-headers-hook)))))
289
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
293     ;; external, etc.)
294     (insert "[ Part of type " content-type ". ]\n")
295     (overlay-put (make-overlay start (point)) 'face 'bold)))
296
297 ;; Functions handling particular MIME parts.
298
299 (defun notmuch-show-markup-citations ()
300   "Markup citations, and up to one signature in the buffer."
301   (let ((depth 0)
302         (indent "\n"))
303     (goto-char (point-min))
304     (beginning-of-line)
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)))
314             (goto-char cite-end)
315             (notmuch-show-region-to-button
316              hidden-start (point-marker)
317              "citation" indent
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
332                  "signature" indent
333                  (format notmuch-show-signature-button-format sig-lines))))))))
334
335 (defun notmuch-show-insert-part-text/plain (part content-type depth)
336   (let ((start (point)))
337     (insert (plist-get part :content))
338     (save-excursion
339       (save-restriction
340         (narrow-to-region start (point-max))
341         (notmuch-show-markup-citations))))
342   t)
343
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)
347       nil
348     (notmuch-show-insert-part-header content-type)
349     (insert (plist-get part :content))
350     t))
351
352 (defun notmuch-show-insert-part-*/* (part content-type depth)
353   (notmuch-show-insert-part-header content-type)
354   t)
355
356 ;; Functions for determining how to handle MIME parts.
357
358 (defun notmuch-show-split-content-type (content-type)
359   (split-string content-type "/"))
360
361 (defun notmuch-show-handlers-for (content-type)
362   "Return a list of content handlers for a part of type CONTENT-TYPE."
363   (let (result)
364     (mapc (lambda (func)
365             (if (functionp func)
366                 (push func result)))
367           ;; Reverse order of prefrence.
368           (list (intern (concat "notmuch-show-insert-part-*/*"))
369                 (intern (concat
370                          "notmuch-show-insert-part-"
371                          (car (notmuch-show-split-content-type content-type))
372                          "/*"))
373                 (intern (concat "notmuch-show-insert-part-" content-type))))
374     result))
375
376 ;; \f
377
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
383     ;; value.
384     (while (and handlers
385                 (not (funcall (car handlers) part content-type depth)))
386       (setq handlers (cdr handlers))))
387   ;; Ensure that the part ends with a carriage return.
388   (if (not (bolp))
389       (insert "\n"))
390   )
391
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))
395
396 (defun notmuch-show-make-symbol (type)
397   (make-symbol (concat "notmuch-show-" type)))
398
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
407         body-start body-end
408         (headers-invis-spec (notmuch-show-make-symbol "header"))
409         (body-invis-spec (notmuch-show-make-symbol "body")))
410
411     (setq message-start (point-marker))
412
413     (notmuch-show-insert-headerline headers
414                                     (or (plist-get msg :date_relative)
415                                         (plist-get headers :Date))
416                                     (plist-get msg :tags) depth)
417
418     (setq content-start (point-marker))
419
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).
425     (insert "\n")
426     (save-excursion
427       (goto-char content-start)
428       (forward-line 1)
429       (setq headers-start (point-marker)))
430     (setq headers-end (point-marker))
431
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.
435     (if (not (bolp))
436         (insert "\n"))
437     (setq body-end (point-marker))
438     (setq content-end (point-marker))
439
440     ;; Indent according to the depth in the thread.
441     (indent-rigidly content-start content-end depth)
442
443     (setq message-end (point-max-marker))
444
445     ;; Save the extents of this message over the whole text of the
446     ;; message.
447     (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
448
449     (plist-put msg :headers-invis-spec headers-invis-spec)
450     (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
451
452     (plist-put msg :body-invis-spec body-invis-spec)
453     (overlay-put (make-overlay body-start body-end) 'invisible body-invis-spec)
454
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
458     ;; the content).
459     (notmuch-show-set-message-properties msg)
460
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)
465
466     ;; Message visibility depends on whether it matched the search
467     ;; criteria.
468     (notmuch-show-message-visible msg (plist-get msg :match))))
469
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))))
476
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))
480
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))
484
485 (defvar notmuch-show-parent-buffer nil)
486
487 ;;;###autoload
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.
490
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
494 one).
495
496 The optional QUERY-CONTEXT is a notmuch search term. Only
497 messages from the thread matching this search term are shown if
498 non-nil.
499
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
503 function is used. "
504   (interactive "sNotmuch show: ")
505   (let ((buffer (get-buffer-create (generate-new-buffer-name
506                                     (or buffer-name
507                                         (concat "*notmuch-" thread-id "*")))))
508         (inhibit-read-only t))
509     (switch-to-buffer buffer)
510     (notmuch-show-mode)
511     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
512     (erase-buffer)
513     (goto-char (point-min))
514     (save-excursion
515       (let* ((basic-args (list thread-id))
516              (args (if query-context
517                        (append basic-args (list "and (" query-context ")"))
518                      basic-args)))
519         (notmuch-show-insert-forest (notmuch-query-get-threads args))
520         ;; If the query context reduced the results to nothing, run
521         ;; the basic query.
522         (when (and (eq (buffer-size) 0)
523                    query-context)
524           (notmuch-show-insert-forest
525            (notmuch-query-get-threads basic-args))))
526       (run-hooks 'notmuch-show-hook))
527
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)))
532
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)
543     map)
544   "Submap for stash commands")
545 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
546
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)
575         map)
576       "Keymap for \"notmuch show\" buffers.")
577 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
578
579 ;;;###autoload
580 (defun notmuch-show-mode ()
581   "Major mode for viewing a thread with notmuch.
582
583 This buffer contains the results of the \"notmuch show\" command
584 for displaying a single thread of email from your email archives.
585
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).
592
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
597 thread).
598
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]).
605
606 You can add or remove arbitary tags from the current message with
607 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
608
609 All currently available key bindings:
610
611 \\{notmuch-show-mode-map}"
612   (interactive)
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))
618
619 (defun notmuch-show-move-to-message-top ()
620   (goto-char (notmuch-show-message-top)))
621
622 (defun notmuch-show-move-to-message-bottom ()
623   (goto-char (notmuch-show-message-bottom)))
624
625 (defun notmuch-show-message-adjust ()
626   (recenter 0))
627
628 ;; Movement related functions.
629
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 ()
634   (let (r)
635     (save-excursion
636       (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
637         (backward-char)))
638     r))
639
640 (defun notmuch-show-message-top ()
641   (car (notmuch-show-message-extent)))
642
643 (defun notmuch-show-message-bottom ()
644   (cdr (notmuch-show-message-extent)))
645
646 (defun notmuch-show-goto-message-next ()
647   (let ((start (point)))
648     (notmuch-show-move-to-message-bottom)
649     (if (not (eobp))
650         t
651       (goto-char start)
652       nil)))
653
654 (defun notmuch-show-goto-message-previous ()
655   (notmuch-show-move-to-message-top)
656   (if (bobp)
657       nil
658     (backward-char)
659     (notmuch-show-move-to-message-top)
660     t))
661
662 (defun notmuch-show-move-past-invisible-forward ()
663   (while (point-invisible-p)
664     (forward-char)))
665
666 (defun notmuch-show-move-past-invisible-backward ()
667   (while (point-invisible-p)
668     (backward-char)))
669
670 ;; Functions relating to the visibility of messages and their
671 ;; components.
672
673 (defun notmuch-show-element-visible (props visible-p spec-property)
674   (let ((spec (plist-get props spec-property)))
675     (if visible-p
676         (remove-from-invisibility-spec spec)
677       (add-to-invisibility-spec spec))))
678
679 (defun notmuch-show-message-visible (props visible-p)
680   (if 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))
690
691   (notmuch-show-set-prop :message-visible visible-p props))
692
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))
697
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))
702
703 ;; Functions for setting and getting attributes of the current
704 ;; message.
705
706 (defun notmuch-show-set-message-properties (props)
707   (save-excursion
708     (notmuch-show-move-to-message-top)
709     (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
710
711 (defun notmuch-show-get-message-properties ()
712   (save-excursion
713     (notmuch-show-move-to-message-top)
714     (get-text-property (point) :notmuch-message-properties)))
715
716 (defun notmuch-show-set-prop (prop val &optional props)
717   (let ((inhibit-read-only t)
718         (props (or props
719                    (notmuch-show-get-message-properties))))
720     (plist-put props prop val)
721     (notmuch-show-set-message-properties props)))
722
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)))
727
728 (defun notmuch-show-get-message-id ()
729   "Return the message id of the current message."
730   (concat "id:" (notmuch-show-get-prop :id)))
731
732 ;; dme: Would it make sense to use a macro for many of these?
733
734 (defun notmuch-show-get-filename ()
735   "Return the filename of the current message."
736   (notmuch-show-get-prop :filename))
737
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))
741
742 (defun notmuch-show-get-cc ()
743   (notmuch-show-get-header :Cc))
744
745 (defun notmuch-show-get-date ()
746   (notmuch-show-get-header :Date))
747
748 (defun notmuch-show-get-from ()
749   (notmuch-show-get-header :From))
750
751 (defun notmuch-show-get-subject ()
752   (notmuch-show-get-header :Subject))
753
754 (defun notmuch-show-get-to ()
755   (notmuch-show-get-header :To))
756
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))
761
762 (defun notmuch-show-get-tags ()
763   "Return the tags of the current message."
764   (notmuch-show-get-prop :tags))
765
766 (defun notmuch-show-message-visible-p ()
767   "Is the current message visible?"
768   (notmuch-show-get-prop :message-visible))
769
770 (defun notmuch-show-body-visible-p ()
771   "Is the body of the current message visible?"
772   (notmuch-show-get-prop :body-visible))
773
774 (defun notmuch-show-headers-visible-p ()
775   "Are the headers of the current message visible?"
776   (notmuch-show-get-prop :headers-visible))
777
778 (defun notmuch-show-mark-read ()
779   "Mark the current message as read."
780   (notmuch-show-remove-tag "unread"))
781
782 ;; Commands typically bound to keys.
783
784 (defun notmuch-show-advance-and-archive ()
785   "Advance through thread and archive.
786
787 This command is intended to be one of the simplest ways to
788 process a thread of email. It does the following:
789
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.
792
793 Otherwise, (the end of the current message is already within the
794 current window), advance to the next open message.
795
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."
801   (interactive)
802   (let ((end-of-this-message (notmuch-show-message-bottom)))
803     (cond
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
808      ;; point is visible.
809      ((save-excursion
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.
814       (scroll-up nil))
815
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))
819
820      (t
821       ;; This is the last message - archive the thread.
822       (notmuch-show-archive-thread)))))
823
824 (defun notmuch-show-rewind ()
825   "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
826
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'.
830
831 Otherwise, just scroll down a screenful of the current message.
832
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'."
836   (interactive)
837   (let ((start-of-message (notmuch-show-message-top))
838         (start-of-window (window-start)))
839     (cond
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
842       ;; screen - scroll.
843      ((or (= start-of-message start-of-window)
844           (< start-of-message start-of-window))
845       (scroll-down)
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)
851           (progn
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)))
856      (t
857       ;; Move to the previous message.
858       (notmuch-show-previous-message)))))
859
860 (defun notmuch-show-reply ()
861   "Reply to the current message."
862   (interactive)
863   (notmuch-reply (notmuch-show-get-message-id)))
864
865 (defun notmuch-show-forward-message ()
866   "Forward the current message."
867   (interactive)
868   (with-current-notmuch-show-message
869    (message-forward)))
870
871 (defun notmuch-show-next-message ()
872   "Show the next message."
873   (interactive)
874   (notmuch-show-goto-message-next)
875   (notmuch-show-mark-read)
876   (notmuch-show-message-adjust))
877
878 (defun notmuch-show-previous-message ()
879   "Show the previous message."
880   (interactive)
881   (notmuch-show-goto-message-previous)
882   (notmuch-show-mark-read)
883   (notmuch-show-message-adjust))
884
885 (defun notmuch-show-next-open-message ()
886   "Show the next message."
887   (interactive)
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))
892
893 (defun notmuch-show-previous-open-message ()
894   "Show the previous message."
895   (interactive)
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))
900
901 (defun notmuch-show-view-raw-message ()
902   "View the file holding the current message."
903   (interactive)
904   (view-file (notmuch-show-get-filename)))
905
906 (defun notmuch-show-pipe-message (command)
907   "Pipe the contents of the current message to the given command.
908
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*"
914          (list command " < "
915                (shell-quote-argument (notmuch-show-get-filename)))))
916
917 (defun notmuch-show-add-tag (&rest toadd)
918   "Add a tag to the current message."
919   (interactive
920    (list (notmuch-select-tag-with-completion "Tag to add: ")))
921   (apply 'notmuch-call-notmuch-process
922          (append (cons "tag"
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<)))
926
927 (defun notmuch-show-remove-tag (&rest toremove)
928   "Remove a tag from the current message."
929   (interactive
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=)
934         (progn
935           (apply 'notmuch-call-notmuch-process
936                  (append (cons "tag"
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<))))))
940
941 (defun notmuch-show-toggle-headers ()
942   "Toggle the visibility of the current message headers."
943   (interactive)
944   (let ((props (notmuch-show-get-message-properties)))
945     (notmuch-show-headers-visible
946      props
947      (not (plist-get props :headers-visible))))
948   (force-window-update))
949
950 (defun notmuch-show-toggle-body ()
951   "Toggle the visibility of the current message body."
952   (interactive)
953   (let ((props (notmuch-show-get-message-properties)))
954     (notmuch-show-body-visible
955      props
956      (not (plist-get props :body-visible))))
957   (force-window-update))
958
959 (defun notmuch-show-toggle-message ()
960   "Toggle the visibility of the current message."
961   (interactive)
962   (let ((props (notmuch-show-get-message-properties)))
963     (notmuch-show-message-visible
964      props
965      (not (plist-get props :message-visible))))
966   (force-window-update))
967
968 (defun notmuch-show-next-button ()
969   "Advance point to the next button in the buffer."
970   (interactive)
971   (forward-button 1))
972
973 (defun notmuch-show-previous-button ()
974   "Move point back to the previous button in the buffer."
975   (interactive)
976   (backward-button 1))
977
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))
985     (kill-this-buffer)
986     (if parent-buffer
987         (progn
988           (switch-to-buffer parent-buffer)
989           (forward-line)
990           (if show-next
991               (notmuch-search-show-thread))))))
992
993 (defun notmuch-show-archive-thread ()
994   "Archive each message in thread, then show next thread from search.
995
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.
999
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
1003 buffer."
1004   (interactive)
1005   (notmuch-show-archive-thread-internal t))
1006
1007 (defun notmuch-show-archive-thread-then-exit ()
1008   "Archive each message in thread, then exit back to search results."
1009   (interactive)
1010   (notmuch-show-archive-thread-internal nil))
1011
1012 (defun notmuch-show-do-stash (text)
1013   (kill-new text)
1014   (message "Saved: %s" text))
1015
1016 (defun notmuch-show-stash-cc ()
1017   "Copy CC field of current message to kill-ring."
1018   (interactive)
1019   (notmuch-show-do-stash (notmuch-show-get-cc)))
1020
1021 (defun notmuch-show-stash-date ()
1022   "Copy date of current message to kill-ring."
1023   (interactive)
1024   (notmuch-show-do-stash (notmuch-show-get-date)))
1025
1026 (defun notmuch-show-stash-filename ()
1027   "Copy filename of current message to kill-ring."
1028   (interactive)
1029   (notmuch-show-do-stash (notmuch-show-get-filename)))
1030
1031 (defun notmuch-show-stash-from ()
1032   "Copy From address of current message to kill-ring."
1033   (interactive)
1034   (notmuch-show-do-stash (notmuch-show-get-from)))
1035
1036 (defun notmuch-show-stash-message-id ()
1037   "Copy message ID of current message to kill-ring."
1038   (interactive)
1039   (notmuch-show-do-stash (notmuch-show-get-message-id)))
1040
1041 (defun notmuch-show-stash-subject ()
1042   "Copy Subject field of current message to kill-ring."
1043   (interactive)
1044   (notmuch-show-do-stash (notmuch-show-get-subject)))
1045
1046 (defun notmuch-show-stash-tags ()
1047   "Copy tags of current message to kill-ring as a comma separated list."
1048   (interactive)
1049   (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1050
1051 (defun notmuch-show-stash-to ()
1052   "Copy To address of current message to kill-ring."
1053   (interactive)
1054   (notmuch-show-do-stash (notmuch-show-get-to)))
1055
1056 ;;
1057
1058 (provide 'notmuch-show)