]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-show.el
emacs/notmuch-show.el: Improved part labelling
[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 (require 'notmuch-wash)
31
32 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
33 (declare-function notmuch-reply "notmuch" (query-string))
34 (declare-function notmuch-fontify-headers "notmuch" nil)
35 (declare-function notmuch-select-tag-with-completion "notmuch" (prompt &rest search-terms))
36 (declare-function notmuch-search-show-thread "notmuch" nil)
37
38 (defvar notmuch-show-headers '("Subject" "To" "Cc" "From" "Date")
39   "Headers that should be shown in a message, in this order. Note
40 that if this order is changed the headers shown when a message is
41 collapsed will change.")
42
43 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
44   "A list of functions called to decorate the headers listed in
45 `notmuch-show-headers'.")
46
47 (defvar notmuch-show-hook '(notmuch-show-pretty-hook)
48   "A list of functions called after populating a
49 `notmuch-show' buffer.")
50
51 (defvar notmuch-show-insert-text/plain-hook '(notmuch-wash-text/plain-citations)
52   "A list of functions called to clean up text/plain body parts.")
53
54 (defun notmuch-show-pretty-hook ()
55   (goto-address-mode 1)
56   (visual-line-mode))
57
58 (defmacro with-current-notmuch-show-message (&rest body)
59   "Evaluate body with current buffer set to the text of current message"
60   `(save-excursion
61      (let ((filename (notmuch-show-get-filename)))
62        (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
63          (with-current-buffer buf
64            (insert-file-contents filename nil nil nil t)
65            ,@body)
66          (kill-buffer buf)))))
67
68 (defun notmuch-show-view-all-mime-parts ()
69   "Use external viewers to view all attachments from the current message."
70   (interactive)
71   (with-current-notmuch-show-message
72    ; We ovverride the mm-inline-media-tests to indicate which message
73    ; parts are already sufficiently handled by the original
74    ; presentation of the message in notmuch-show mode. These parts
75    ; will be inserted directly into the temporary buffer of
76    ; with-current-notmuch-show-message and silently discarded.
77    ;
78    ; Any MIME part not explicitly mentioned here will be handled by an
79    ; external viewer as configured in the various mailcap files.
80    (let ((mm-inline-media-tests '(
81                                   ("text/.*" ignore identity)
82                                   ("application/pgp-signature" ignore identity)
83                                   ("multipart/alternative" ignore identity)
84                                   ("multipart/mixed" ignore identity)
85                                   ("multipart/related" ignore identity)
86                                  )))
87      (mm-display-parts (mm-dissect-buffer)))))
88
89 (defun notmuch-foreach-mime-part (function mm-handle)
90   (cond ((stringp (car mm-handle))
91          (dolist (part (cdr mm-handle))
92            (notmuch-foreach-mime-part function part)))
93         ((bufferp (car mm-handle))
94          (funcall function mm-handle))
95         (t (dolist (part mm-handle)
96              (notmuch-foreach-mime-part function part)))))
97
98 (defun notmuch-count-attachments (mm-handle)
99   (let ((count 0))
100     (notmuch-foreach-mime-part
101      (lambda (p)
102        (let ((disposition (mm-handle-disposition p)))
103          (and (listp disposition)
104               (or (equal (car disposition) "attachment")
105                   (and (equal (car disposition) "inline")
106                        (assq 'filename disposition)))
107               (incf count))))
108      mm-handle)
109     count))
110
111 (defun notmuch-save-attachments (mm-handle &optional queryp)
112   (notmuch-foreach-mime-part
113    (lambda (p)
114      (let ((disposition (mm-handle-disposition p)))
115        (and (listp disposition)
116             (or (equal (car disposition) "attachment")
117                 (and (equal (car disposition) "inline")
118                      (assq 'filename disposition)))
119             (or (not queryp)
120                 (y-or-n-p
121                  (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
122             (mm-save-part p))))
123    mm-handle))
124
125 (defun notmuch-show-save-attachments ()
126   "Save all attachments from the current message."
127   (interactive)
128   (with-current-notmuch-show-message
129    (let ((mm-handle (mm-dissect-buffer)))
130      (notmuch-save-attachments
131       mm-handle (> (notmuch-count-attachments mm-handle) 1))))
132   (message "Done"))
133
134 (defun notmuch-show-fontify-header ()
135   (let ((face (cond
136                ((looking-at "[Tt]o:")
137                 'message-header-to)
138                ((looking-at "[Bb]?[Cc][Cc]:")
139                 'message-header-cc)
140                ((looking-at "[Ss]ubject:")
141                 'message-header-subject)
142                ((looking-at "[Ff]rom:")
143                 'message-header-from)
144                (t
145                 'message-header-other))))
146
147     (overlay-put (make-overlay (point) (re-search-forward ":"))
148                  'face 'message-header-name)
149     (overlay-put (make-overlay (point) (re-search-forward ".*$"))
150                  'face face)))
151
152 (defun notmuch-show-colour-headers ()
153   "Apply some colouring to the current headers."
154   (goto-char (point-min))
155   (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
156     (notmuch-show-fontify-header)
157     (forward-line)))
158
159 (defun notmuch-show-spaces-n (n)
160   "Return a string comprised of `n' spaces."
161   (make-string n ? ))
162
163 (defun notmuch-show-update-tags (tags)
164   "Update the displayed tags of the current message."
165   (save-excursion
166     (goto-char (notmuch-show-message-top))
167     (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
168         (let ((inhibit-read-only t))
169           (replace-match (concat "("
170                                  (mapconcat 'identity tags " ")
171                                  ")"))))))
172
173 (defun notmuch-show-insert-headerline (headers date tags depth)
174   "Insert a notmuch style headerline based on HEADERS for a
175 message at DEPTH in the current thread."
176   (let ((start (point)))
177     (insert (notmuch-show-spaces-n depth)
178             (plist-get headers :From)
179             " ("
180             date
181             ") ("
182             (mapconcat 'identity tags " ")
183             ")\n")
184     (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
185
186 (defun notmuch-show-insert-header (header header-value)
187   "Insert a single header."
188   (insert header ": " header-value "\n"))
189
190 (defun notmuch-show-insert-headers (headers)
191   "Insert the headers of the current message."
192   (let ((start (point)))
193     (mapc '(lambda (header)
194              (let* ((header-symbol (intern (concat ":" header)))
195                     (header-value (plist-get headers header-symbol)))
196                (if (and header-value
197                         (not (string-equal "" header-value)))
198                    (notmuch-show-insert-header header header-value))))
199           notmuch-show-headers)
200     (save-excursion
201       (save-restriction
202         (narrow-to-region start (point-max))
203         (run-hooks 'notmuch-show-markup-headers-hook)))))
204
205 (defun notmuch-show-insert-part-header (content-type &optional name)
206   (let ((start (point)))
207     ;; XXX dme: Make this a more useful button (save the part, display
208     ;; external, etc.)
209     (insert "[ Part of type "
210             content-type
211             (if name (concat " named " name) "")
212             ". ]\n")
213     (overlay-put (make-overlay start (point)) 'face 'bold)))
214
215 ;; Functions handling particular MIME parts.
216
217 (defun notmuch-show-insert-part-text/plain (part content-type nth depth)
218   (let ((start (point)))
219     ;; If this text/plain part is not the first part in the message,
220     ;; insert a header to make this clear.
221     (if (> nth 1)
222         (notmuch-show-insert-part-header content-type (plist-get part :filename)))
223     (insert (plist-get part :content))
224     (save-excursion
225       (save-restriction
226         (narrow-to-region start (point-max))
227         (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
228   t)
229
230 (defun notmuch-show-insert-part-text/* (part content-type nth depth)
231   ;; Handle all text types other than text/html.
232   (if (string-equal "text/html" content-type)
233       nil
234     (notmuch-show-insert-part-header content-type (plist-get part :filename))
235     (insert (plist-get part :content))
236     t))
237
238 (defun notmuch-show-insert-part-*/* (part content-type nth depth)
239   (notmuch-show-insert-part-header content-type (plist-get part :filename))
240   t)
241
242 ;; Functions for determining how to handle MIME parts.
243
244 (defun notmuch-show-split-content-type (content-type)
245   (split-string content-type "/"))
246
247 (defun notmuch-show-handlers-for (content-type)
248   "Return a list of content handlers for a part of type CONTENT-TYPE."
249   (let (result)
250     (mapc (lambda (func)
251             (if (functionp func)
252                 (push func result)))
253           ;; Reverse order of prefrence.
254           (list (intern (concat "notmuch-show-insert-part-*/*"))
255                 (intern (concat
256                          "notmuch-show-insert-part-"
257                          (car (notmuch-show-split-content-type content-type))
258                          "/*"))
259                 (intern (concat "notmuch-show-insert-part-" content-type))))
260     result))
261
262 ;; \f
263
264 (defun notmuch-show-insert-bodypart (part depth)
265   "Insert the body part PART at depth DEPTH in the current thread."
266   (let* ((content-type (downcase (plist-get part :content-type)))
267          (handlers (notmuch-show-handlers-for content-type))
268          (nth (plist-get part :id)))
269     ;; Run the content handlers until one of them returns a non-nil
270     ;; value.
271     (while (and handlers
272                 (not (funcall (car handlers) part content-type nth depth)))
273       (setq handlers (cdr handlers))))
274   ;; Ensure that the part ends with a carriage return.
275   (if (not (bolp))
276       (insert "\n"))
277   )
278
279 (defun notmuch-show-insert-body (body depth)
280   "Insert the body BODY at depth DEPTH in the current thread."
281   (mapc '(lambda (part) (notmuch-show-insert-bodypart part depth)) body))
282
283 (defun notmuch-show-make-symbol (type)
284   (make-symbol (concat "notmuch-show-" type)))
285
286 (defun notmuch-show-insert-msg (msg depth)
287   "Insert the message MSG at depth DEPTH in the current thread."
288   (let ((headers (plist-get msg :headers))
289         ;; Indentation causes the buffer offset of the start/end
290         ;; points to move, so we must use markers.
291         message-start message-end
292         content-start content-end
293         headers-start headers-end
294         body-start body-end
295         (headers-invis-spec (notmuch-show-make-symbol "header"))
296         (message-invis-spec (notmuch-show-make-symbol "message")))
297
298     (setq message-start (point-marker))
299
300     (notmuch-show-insert-headerline headers
301                                     (or (plist-get msg :date_relative)
302                                         (plist-get headers :Date))
303                                     (plist-get msg :tags) depth)
304
305     (setq content-start (point-marker))
306
307     ;; Set `headers-start' to point after the 'Subject:' header to be
308     ;; compatible with the existing implementation. This just sets it
309     ;; to after the first header.
310     (notmuch-show-insert-headers headers)
311     ;; Headers should include a blank line (backwards compatibility).
312     (insert "\n")
313     (save-excursion
314       (goto-char content-start)
315       (forward-line 1)
316       (setq headers-start (point-marker)))
317     (setq headers-end (point-marker))
318
319     (setq body-start (point-marker))
320     (notmuch-show-insert-body (plist-get msg :body) depth)
321     ;; Ensure that the body ends with a newline.
322     (if (not (bolp))
323         (insert "\n"))
324     (setq body-end (point-marker))
325     (setq content-end (point-marker))
326
327     ;; Indent according to the depth in the thread.
328     (indent-rigidly content-start content-end depth)
329
330     (setq message-end (point-max-marker))
331
332     ;; Save the extents of this message over the whole text of the
333     ;; message.
334     (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
335
336     (plist-put msg :headers-invis-spec headers-invis-spec)
337     (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
338
339     (plist-put msg :message-invis-spec message-invis-spec)
340     (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
341
342     ;; Save the properties for this message. Currently this saves the
343     ;; entire message (augmented it with other stuff), which seems
344     ;; like overkill. We might save a reduced subset (for example, not
345     ;; the content).
346     (notmuch-show-set-message-properties msg)
347
348     ;; Headers are hidden by default.
349     (notmuch-show-headers-visible msg nil)
350
351     ;; Message visibility depends on whether it matched the search
352     ;; criteria.
353     (notmuch-show-message-visible msg (plist-get msg :match))))
354
355 (defun notmuch-show-insert-tree (tree depth)
356   "Insert the message tree TREE at depth DEPTH in the current thread."
357   (let ((msg (car tree))
358         (replies (cadr tree)))
359     (notmuch-show-insert-msg msg depth)
360     (notmuch-show-insert-thread replies (1+ depth))))
361
362 (defun notmuch-show-insert-thread (thread depth)
363   "Insert the thread THREAD at depth DEPTH in the current forest."
364   (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
365
366 (defun notmuch-show-insert-forest (forest)
367   "Insert the forest of threads FOREST."
368   (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
369
370 (defvar notmuch-show-parent-buffer nil)
371
372 ;;;###autoload
373 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
374   "Run \"notmuch show\" with the given thread ID and display results.
375
376 The optional PARENT-BUFFER is the notmuch-search buffer from
377 which this notmuch-show command was executed, (so that the
378 next thread from that buffer can be show when done with this
379 one).
380
381 The optional QUERY-CONTEXT is a notmuch search term. Only
382 messages from the thread matching this search term are shown if
383 non-nil.
384
385 The optional BUFFER-NAME provides the neame of the buffer in
386 which the message thread is shown. If it is nil (which occurs
387 when the command is called interactively) the argument to the
388 function is used. "
389   (interactive "sNotmuch show: ")
390   (let ((buffer (get-buffer-create (generate-new-buffer-name
391                                     (or buffer-name
392                                         (concat "*notmuch-" thread-id "*")))))
393         (inhibit-read-only t))
394     (switch-to-buffer buffer)
395     (notmuch-show-mode)
396     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
397     (erase-buffer)
398     (goto-char (point-min))
399     (save-excursion
400       (let* ((basic-args (list thread-id))
401              (args (if query-context
402                        (append basic-args (list "and (" query-context ")"))
403                      basic-args)))
404         (notmuch-show-insert-forest (notmuch-query-get-threads args))
405         ;; If the query context reduced the results to nothing, run
406         ;; the basic query.
407         (when (and (eq (buffer-size) 0)
408                    query-context)
409           (notmuch-show-insert-forest
410            (notmuch-query-get-threads basic-args))))
411       (run-hooks 'notmuch-show-hook))
412
413     ;; Move straight to the first open message
414     (if (not (notmuch-show-message-visible-p))
415         (notmuch-show-next-open-message))
416     (notmuch-show-mark-read)))
417
418 (defvar notmuch-show-stash-map
419   (let ((map (make-sparse-keymap)))
420     (define-key map "c" 'notmuch-show-stash-cc)
421     (define-key map "d" 'notmuch-show-stash-date)
422     (define-key map "F" 'notmuch-show-stash-filename)
423     (define-key map "f" 'notmuch-show-stash-from)
424     (define-key map "i" 'notmuch-show-stash-message-id)
425     (define-key map "s" 'notmuch-show-stash-subject)
426     (define-key map "T" 'notmuch-show-stash-tags)
427     (define-key map "t" 'notmuch-show-stash-to)
428     map)
429   "Submap for stash commands")
430 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
431
432 (defvar notmuch-show-mode-map
433       (let ((map (make-sparse-keymap)))
434         (define-key map "?" 'notmuch-help)
435         (define-key map "q" 'kill-this-buffer)
436         (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
437         (define-key map (kbd "TAB") 'notmuch-show-next-button)
438         (define-key map "s" 'notmuch-search)
439         (define-key map "m" 'message-mail)
440         (define-key map "f" 'notmuch-show-forward-message)
441         (define-key map "r" 'notmuch-show-reply)
442         (define-key map "|" 'notmuch-show-pipe-message)
443         (define-key map "w" 'notmuch-show-save-attachments)
444         (define-key map "V" 'notmuch-show-view-raw-message)
445         (define-key map "v" 'notmuch-show-view-all-mime-parts)
446         (define-key map "c" 'notmuch-show-stash-map)
447         (define-key map "h" 'notmuch-show-toggle-headers)
448         (define-key map "-" 'notmuch-show-remove-tag)
449         (define-key map "+" 'notmuch-show-add-tag)
450         (define-key map "x" 'notmuch-show-archive-thread-then-exit)
451         (define-key map "a" 'notmuch-show-archive-thread)
452         (define-key map "N" 'notmuch-show-next-message)
453         (define-key map "P" 'notmuch-show-previous-message)
454         (define-key map "n" 'notmuch-show-next-open-message)
455         (define-key map "p" 'notmuch-show-previous-open-message)
456         (define-key map (kbd "DEL") 'notmuch-show-rewind)
457         (define-key map " " 'notmuch-show-advance-and-archive)
458         (define-key map (kbd "RET") 'notmuch-show-toggle-message)
459         map)
460       "Keymap for \"notmuch show\" buffers.")
461 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
462
463 ;;;###autoload
464 (defun notmuch-show-mode ()
465   "Major mode for viewing a thread with notmuch.
466
467 This buffer contains the results of the \"notmuch show\" command
468 for displaying a single thread of email from your email archives.
469
470 By default, various components of email messages, (citations,
471 signatures, already-read messages), are hidden. You can make
472 these parts visible by clicking with the mouse button or by
473 pressing RET after positioning the cursor on a hidden part, (for
474 which \\[notmuch-show-next-button] and
475 \\[notmuch-show-previous-button] are helpful).
476
477 Reading the thread sequentially is well-supported by pressing
478 \\[notmuch-show-advance-and-archive]. This will scroll the
479 current message (if necessary), advance to the next message, or
480 advance to the next thread (if already on the last message of a
481 thread).
482
483 Other commands are available to read or manipulate the thread
484 more selectively, (such as '\\[notmuch-show-next-message]' and
485 '\\[notmuch-show-previous-message]' to advance to messages
486 without removing any tags, and '\\[notmuch-show-archive-thread]'
487 to archive an entire thread without scrolling through with
488 \\[notmuch-show-advance-and-archive]).
489
490 You can add or remove arbitary tags from the current message with
491 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
492
493 All currently available key bindings:
494
495 \\{notmuch-show-mode-map}"
496   (interactive)
497   (kill-all-local-variables)
498   (use-local-map notmuch-show-mode-map)
499   (setq major-mode 'notmuch-show-mode
500         mode-name "notmuch-show")
501   (setq buffer-read-only t))
502
503 (defun notmuch-show-move-to-message-top ()
504   (goto-char (notmuch-show-message-top)))
505
506 (defun notmuch-show-move-to-message-bottom ()
507   (goto-char (notmuch-show-message-bottom)))
508
509 (defun notmuch-show-message-adjust ()
510   (recenter 0))
511
512 ;; Movement related functions.
513
514 ;; There's some strangeness here where a text property applied to a
515 ;; region a->b is not found when point is at b. We walk backwards
516 ;; until finding the property.
517 (defun notmuch-show-message-extent ()
518   (let (r)
519     (save-excursion
520       (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
521         (backward-char)))
522     r))
523
524 (defun notmuch-show-message-top ()
525   (car (notmuch-show-message-extent)))
526
527 (defun notmuch-show-message-bottom ()
528   (cdr (notmuch-show-message-extent)))
529
530 (defun notmuch-show-goto-message-next ()
531   (let ((start (point)))
532     (notmuch-show-move-to-message-bottom)
533     (if (not (eobp))
534         t
535       (goto-char start)
536       nil)))
537
538 (defun notmuch-show-goto-message-previous ()
539   (notmuch-show-move-to-message-top)
540   (if (bobp)
541       nil
542     (backward-char)
543     (notmuch-show-move-to-message-top)
544     t))
545
546 (defun notmuch-show-move-past-invisible-forward ()
547   (while (point-invisible-p)
548     (forward-char)))
549
550 (defun notmuch-show-move-past-invisible-backward ()
551   (while (point-invisible-p)
552     (backward-char)))
553
554 ;; Functions relating to the visibility of messages and their
555 ;; components.
556
557 (defun notmuch-show-element-visible (props visible-p spec-property)
558   (let ((spec (plist-get props spec-property)))
559     (if visible-p
560         (remove-from-invisibility-spec spec)
561       (add-to-invisibility-spec spec))))
562
563 (defun notmuch-show-message-visible (props visible-p)
564   (if visible-p
565       ;; When making the message visible, the headers may or not be
566       ;; visible. So we check that property separately.
567       (let ((headers-visible (plist-get props :headers-visible)))
568         (notmuch-show-element-visible props headers-visible :headers-invis-spec)
569         (notmuch-show-element-visible props t :message-invis-spec))
570     (notmuch-show-element-visible props nil :headers-invis-spec)
571     (notmuch-show-element-visible props nil :message-invis-spec))
572
573   (notmuch-show-set-prop :message-visible visible-p props))
574
575 (defun notmuch-show-headers-visible (props visible-p)
576   (if (plist-get props :message-visible)
577       (notmuch-show-element-visible props visible-p :headers-invis-spec))
578   (notmuch-show-set-prop :headers-visible visible-p props))
579
580 ;; Functions for setting and getting attributes of the current
581 ;; message.
582
583 (defun notmuch-show-set-message-properties (props)
584   (save-excursion
585     (notmuch-show-move-to-message-top)
586     (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
587
588 (defun notmuch-show-get-message-properties ()
589   (save-excursion
590     (notmuch-show-move-to-message-top)
591     (get-text-property (point) :notmuch-message-properties)))
592
593 (defun notmuch-show-set-prop (prop val &optional props)
594   (let ((inhibit-read-only t)
595         (props (or props
596                    (notmuch-show-get-message-properties))))
597     (plist-put props prop val)
598     (notmuch-show-set-message-properties props)))
599
600 (defun notmuch-show-get-prop (prop &optional props)
601   (let ((props (or props
602                    (notmuch-show-get-message-properties))))
603     (plist-get props prop)))
604
605 (defun notmuch-show-get-message-id ()
606   "Return the message id of the current message."
607   (concat "id:" (notmuch-show-get-prop :id)))
608
609 ;; dme: Would it make sense to use a macro for many of these?
610
611 (defun notmuch-show-get-filename ()
612   "Return the filename of the current message."
613   (notmuch-show-get-prop :filename))
614
615 (defun notmuch-show-get-header (header)
616   "Return the named header of the current message, if any."
617   (plist-get (notmuch-show-get-prop :headers) header))
618
619 (defun notmuch-show-get-cc ()
620   (notmuch-show-get-header :Cc))
621
622 (defun notmuch-show-get-date ()
623   (notmuch-show-get-header :Date))
624
625 (defun notmuch-show-get-from ()
626   (notmuch-show-get-header :From))
627
628 (defun notmuch-show-get-subject ()
629   (notmuch-show-get-header :Subject))
630
631 (defun notmuch-show-get-to ()
632   (notmuch-show-get-header :To))
633
634 (defun notmuch-show-set-tags (tags)
635   "Set the tags of the current message."
636   (notmuch-show-set-prop :tags tags)
637   (notmuch-show-update-tags tags))
638
639 (defun notmuch-show-get-tags ()
640   "Return the tags of the current message."
641   (notmuch-show-get-prop :tags))
642
643 (defun notmuch-show-message-visible-p ()
644   "Is the current message visible?"
645   (notmuch-show-get-prop :message-visible))
646
647 (defun notmuch-show-headers-visible-p ()
648   "Are the headers of the current message visible?"
649   (notmuch-show-get-prop :headers-visible))
650
651 (defun notmuch-show-mark-read ()
652   "Mark the current message as read."
653   (notmuch-show-remove-tag "unread"))
654
655 ;; Commands typically bound to keys.
656
657 (defun notmuch-show-advance-and-archive ()
658   "Advance through thread and archive.
659
660 This command is intended to be one of the simplest ways to
661 process a thread of email. It does the following:
662
663 If the current message in the thread is not yet fully visible,
664 scroll by a near screenful to read more of the message.
665
666 Otherwise, (the end of the current message is already within the
667 current window), advance to the next open message.
668
669 Finally, if there is no further message to advance to, and this
670 last message is already read, then archive the entire current
671 thread, (remove the \"inbox\" tag from each message). Also kill
672 this buffer, and display the next thread from the search from
673 which this thread was originally shown."
674   (interactive)
675   (let ((end-of-this-message (notmuch-show-message-bottom)))
676     (cond
677      ;; Ideally we would test `end-of-this-message' against the result
678      ;; of `window-end', but that doesn't account for the fact that
679      ;; the end of the message might be hidden, so we have to actually
680      ;; go to the end, walk back over invisible text and then see if
681      ;; point is visible.
682      ((save-excursion
683         (goto-char (- end-of-this-message 1))
684         (notmuch-show-move-past-invisible-backward)
685         (> (point) (window-end)))
686       ;; The bottom of this message is not visible - scroll.
687       (scroll-up nil))
688
689      ((not (= end-of-this-message (point-max)))
690       ;; This is not the last message - move to the next visible one.
691       (notmuch-show-next-open-message))
692
693      (t
694       ;; This is the last message - archive the thread.
695       (notmuch-show-archive-thread)))))
696
697 (defun notmuch-show-rewind ()
698   "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
699
700 Specifically, if the beginning of the previous email is fewer
701 than `window-height' lines from the current point, move to it
702 just like `notmuch-show-previous-message'.
703
704 Otherwise, just scroll down a screenful of the current message.
705
706 This command does not modify any message tags, (it does not undo
707 any effects from previous calls to
708 `notmuch-show-advance-and-archive'."
709   (interactive)
710   (let ((start-of-message (notmuch-show-message-top))
711         (start-of-window (window-start)))
712     (cond
713       ;; Either this message is properly aligned with the start of the
714       ;; window or the start of this message is not visible on the
715       ;; screen - scroll.
716      ((or (= start-of-message start-of-window)
717           (< start-of-message start-of-window))
718       (scroll-down)
719       ;; If a small number of lines from the previous message are
720       ;; visible, realign so that the top of the current message is at
721       ;; the top of the screen.
722       (if (< (count-lines (window-start) (notmuch-show-message-top))
723              next-screen-context-lines)
724           (progn
725             (goto-char (notmuch-show-message-top))
726             (notmuch-show-message-adjust)))
727       ;; Move to the top left of the window.
728       (goto-char (window-start)))
729      (t
730       ;; Move to the previous message.
731       (notmuch-show-previous-message)))))
732
733 (defun notmuch-show-reply ()
734   "Reply to the current message."
735   (interactive)
736   (notmuch-reply (notmuch-show-get-message-id)))
737
738 (defun notmuch-show-forward-message ()
739   "Forward the current message."
740   (interactive)
741   (with-current-notmuch-show-message
742    (message-forward)))
743
744 (defun notmuch-show-next-message ()
745   "Show the next message."
746   (interactive)
747   (notmuch-show-goto-message-next)
748   (notmuch-show-mark-read)
749   (notmuch-show-message-adjust))
750
751 (defun notmuch-show-previous-message ()
752   "Show the previous message."
753   (interactive)
754   (notmuch-show-goto-message-previous)
755   (notmuch-show-mark-read)
756   (notmuch-show-message-adjust))
757
758 (defun notmuch-show-next-open-message ()
759   "Show the next message."
760   (interactive)
761   (while (and (notmuch-show-goto-message-next)
762               (not (notmuch-show-message-visible-p))))
763   (notmuch-show-mark-read)
764   (notmuch-show-message-adjust))
765
766 (defun notmuch-show-previous-open-message ()
767   "Show the previous message."
768   (interactive)
769   (while (and (notmuch-show-goto-message-previous)
770               (not (notmuch-show-message-visible-p))))
771   (notmuch-show-mark-read)
772   (notmuch-show-message-adjust))
773
774 (defun notmuch-show-view-raw-message ()
775   "View the file holding the current message."
776   (interactive)
777   (view-file (notmuch-show-get-filename)))
778
779 (defun notmuch-show-pipe-message (command)
780   "Pipe the contents of the current message to the given command.
781
782 The given command will be executed with the raw contents of the
783 current email message as stdin. Anything printed by the command
784 to stdout or stderr will appear in the *Messages* buffer."
785   (interactive "sPipe message to command: ")
786   (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
787          (list command " < "
788                (shell-quote-argument (notmuch-show-get-filename)))))
789
790 (defun notmuch-show-add-tag (&rest toadd)
791   "Add a tag to the current message."
792   (interactive
793    (list (notmuch-select-tag-with-completion "Tag to add: ")))
794   (apply 'notmuch-call-notmuch-process
795          (append (cons "tag"
796                        (mapcar (lambda (s) (concat "+" s)) toadd))
797                  (cons (notmuch-show-get-message-id) nil)))
798   (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
799
800 (defun notmuch-show-remove-tag (&rest toremove)
801   "Remove a tag from the current message."
802   (interactive
803    (list (notmuch-select-tag-with-completion
804           "Tag to remove: " (notmuch-show-get-message-id))))
805   (let ((tags (notmuch-show-get-tags)))
806     (if (intersection tags toremove :test 'string=)
807         (progn
808           (apply 'notmuch-call-notmuch-process
809                  (append (cons "tag"
810                                (mapcar (lambda (s) (concat "-" s)) toremove))
811                          (cons (notmuch-show-get-message-id) nil)))
812           (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
813
814 (defun notmuch-show-toggle-headers ()
815   "Toggle the visibility of the current message headers."
816   (interactive)
817   (let ((props (notmuch-show-get-message-properties)))
818     (notmuch-show-headers-visible
819      props
820      (not (plist-get props :headers-visible))))
821   (force-window-update))
822
823 (defun notmuch-show-toggle-message ()
824   "Toggle the visibility of the current message."
825   (interactive)
826   (let ((props (notmuch-show-get-message-properties)))
827     (notmuch-show-message-visible
828      props
829      (not (plist-get props :message-visible))))
830   (force-window-update))
831
832 (defun notmuch-show-next-button ()
833   "Advance point to the next button in the buffer."
834   (interactive)
835   (forward-button 1))
836
837 (defun notmuch-show-previous-button ()
838   "Move point back to the previous button in the buffer."
839   (interactive)
840   (backward-button 1))
841
842 (defun notmuch-show-archive-thread-internal (show-next)
843   ;; Remove the tag from the current set of messages.
844   (goto-char (point-min))
845   (loop do (notmuch-show-remove-tag "inbox")
846         until (not (notmuch-show-goto-message-next)))
847   ;; Move to the next item in the search results, if any.
848   (let ((parent-buffer notmuch-show-parent-buffer))
849     (kill-this-buffer)
850     (if parent-buffer
851         (progn
852           (switch-to-buffer parent-buffer)
853           (forward-line)
854           (if show-next
855               (notmuch-search-show-thread))))))
856
857 (defun notmuch-show-archive-thread ()
858   "Archive each message in thread, then show next thread from search.
859
860 Archive each message currently shown by removing the \"inbox\"
861 tag from each. Then kill this buffer and show the next thread
862 from the search from which this thread was originally shown.
863
864 Note: This command is safe from any race condition of new messages
865 being delivered to the same thread. It does not archive the
866 entire thread, but only the messages shown in the current
867 buffer."
868   (interactive)
869   (notmuch-show-archive-thread-internal t))
870
871 (defun notmuch-show-archive-thread-then-exit ()
872   "Archive each message in thread, then exit back to search results."
873   (interactive)
874   (notmuch-show-archive-thread-internal nil))
875
876 (defun notmuch-show-do-stash (text)
877   (kill-new text)
878   (message "Saved: %s" text))
879
880 (defun notmuch-show-stash-cc ()
881   "Copy CC field of current message to kill-ring."
882   (interactive)
883   (notmuch-show-do-stash (notmuch-show-get-cc)))
884
885 (defun notmuch-show-stash-date ()
886   "Copy date of current message to kill-ring."
887   (interactive)
888   (notmuch-show-do-stash (notmuch-show-get-date)))
889
890 (defun notmuch-show-stash-filename ()
891   "Copy filename of current message to kill-ring."
892   (interactive)
893   (notmuch-show-do-stash (notmuch-show-get-filename)))
894
895 (defun notmuch-show-stash-from ()
896   "Copy From address of current message to kill-ring."
897   (interactive)
898   (notmuch-show-do-stash (notmuch-show-get-from)))
899
900 (defun notmuch-show-stash-message-id ()
901   "Copy message ID of current message to kill-ring."
902   (interactive)
903   (notmuch-show-do-stash (notmuch-show-get-message-id)))
904
905 (defun notmuch-show-stash-subject ()
906   "Copy Subject field of current message to kill-ring."
907   (interactive)
908   (notmuch-show-do-stash (notmuch-show-get-subject)))
909
910 (defun notmuch-show-stash-tags ()
911   "Copy tags of current message to kill-ring as a comma separated list."
912   (interactive)
913   (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
914
915 (defun notmuch-show-stash-to ()
916   "Copy To address of current message to kill-ring."
917   (interactive)
918   (notmuch-show-do-stash (notmuch-show-get-to)))
919
920 ;;
921
922 (provide 'notmuch-show)