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