]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-show.el
emacs: Bind <backtab> (shift-TAB) to notmuch-show-previous-button
[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 (require 'mm-decode)
28 (require 'mailcap)
29
30 (require 'notmuch-lib)
31 (require 'notmuch-query)
32 (require 'notmuch-wash)
33 (require 'notmuch-mua)
34
35 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
36 (declare-function notmuch-fontify-headers "notmuch" nil)
37 (declare-function notmuch-select-tag-with-completion "notmuch" (prompt &rest search-terms))
38 (declare-function notmuch-search-show-thread "notmuch" nil)
39
40 (defcustom notmuch-message-headers '("Subject" "To" "Cc" "Date")
41   "Headers that should be shown in a message, in this order.
42
43 For an open message, all of these headers will be made visible
44 according to `notmuch-message-headers-visible' or can be toggled
45 with `notmuch-show-toggle-headers'. For a closed message, only
46 the first header in the list will be visible."
47   :group 'notmuch
48   :type '(repeat string))
49
50 (defcustom notmuch-message-headers-visible t
51   "Should the headers be visible by default?
52
53 If this value is non-nil, then all of the headers defined in
54 `notmuch-message-headers' will be visible by default in the display
55 of each message. Otherwise, these headers will be hidden and
56 `notmuch-show-toggle-headers' can be used to make the visible for
57 any given message."
58   :group 'notmuch
59   :type 'boolean)
60
61 (defvar notmuch-show-markup-headers-hook '(notmuch-show-colour-headers)
62   "A list of functions called to decorate the headers listed in
63 `notmuch-message-headers'.")
64
65 (defcustom notmuch-show-hook nil
66   "Functions called after populating a `notmuch-show' buffer."
67   :group 'notmuch
68   :type 'hook)
69
70 (defcustom notmuch-show-insert-text/plain-hook '(notmuch-wash-excerpt-citations)
71   "Functions used to improve the display of text/plain parts."
72   :group 'notmuch
73   :type 'hook
74   :options '(notmuch-wash-convert-inline-patch-to-part
75              notmuch-wash-wrap-long-lines
76              notmuch-wash-tidy-citations
77              notmuch-wash-elide-blank-lines
78              notmuch-wash-excerpt-citations))
79
80 (defmacro with-current-notmuch-show-message (&rest body)
81   "Evaluate body with current buffer set to the text of current message"
82   `(save-excursion
83      (let ((filename (notmuch-show-get-filename)))
84        (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
85          (with-current-buffer buf
86            (insert-file-contents filename nil nil nil t)
87            ,@body)
88          (kill-buffer buf)))))
89
90 (defun notmuch-show-view-all-mime-parts ()
91   "Use external viewers to view all attachments from the current message."
92   (interactive)
93   (with-current-notmuch-show-message
94    ; We ovverride the mm-inline-media-tests to indicate which message
95    ; parts are already sufficiently handled by the original
96    ; presentation of the message in notmuch-show mode. These parts
97    ; will be inserted directly into the temporary buffer of
98    ; with-current-notmuch-show-message and silently discarded.
99    ;
100    ; Any MIME part not explicitly mentioned here will be handled by an
101    ; external viewer as configured in the various mailcap files.
102    (let ((mm-inline-media-tests '(
103                                   ("text/.*" ignore identity)
104                                   ("application/pgp-signature" ignore identity)
105                                   ("multipart/alternative" ignore identity)
106                                   ("multipart/mixed" ignore identity)
107                                   ("multipart/related" ignore identity)
108                                  )))
109      (mm-display-parts (mm-dissect-buffer)))))
110
111 (defun notmuch-foreach-mime-part (function mm-handle)
112   (cond ((stringp (car mm-handle))
113          (dolist (part (cdr mm-handle))
114            (notmuch-foreach-mime-part function part)))
115         ((bufferp (car mm-handle))
116          (funcall function mm-handle))
117         (t (dolist (part mm-handle)
118              (notmuch-foreach-mime-part function part)))))
119
120 (defun notmuch-count-attachments (mm-handle)
121   (let ((count 0))
122     (notmuch-foreach-mime-part
123      (lambda (p)
124        (let ((disposition (mm-handle-disposition p)))
125          (and (listp disposition)
126               (or (equal (car disposition) "attachment")
127                   (and (equal (car disposition) "inline")
128                        (assq 'filename disposition)))
129               (incf count))))
130      mm-handle)
131     count))
132
133 (defun notmuch-save-attachments (mm-handle &optional queryp)
134   (notmuch-foreach-mime-part
135    (lambda (p)
136      (let ((disposition (mm-handle-disposition p)))
137        (and (listp disposition)
138             (or (equal (car disposition) "attachment")
139                 (and (equal (car disposition) "inline")
140                      (assq 'filename disposition)))
141             (or (not queryp)
142                 (y-or-n-p
143                  (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
144             (mm-save-part p))))
145    mm-handle))
146
147 (defun notmuch-show-save-attachments ()
148   "Save all attachments from the current message."
149   (interactive)
150   (with-current-notmuch-show-message
151    (let ((mm-handle (mm-dissect-buffer)))
152      (notmuch-save-attachments
153       mm-handle (> (notmuch-count-attachments mm-handle) 1))))
154   (message "Done"))
155
156 (defun notmuch-show-fontify-header ()
157   (let ((face (cond
158                ((looking-at "[Tt]o:")
159                 'message-header-to)
160                ((looking-at "[Bb]?[Cc][Cc]:")
161                 'message-header-cc)
162                ((looking-at "[Ss]ubject:")
163                 'message-header-subject)
164                ((looking-at "[Ff]rom:")
165                 'message-header-from)
166                (t
167                 'message-header-other))))
168
169     (overlay-put (make-overlay (point) (re-search-forward ":"))
170                  'face 'message-header-name)
171     (overlay-put (make-overlay (point) (re-search-forward ".*$"))
172                  'face face)))
173
174 (defun notmuch-show-colour-headers ()
175   "Apply some colouring to the current headers."
176   (goto-char (point-min))
177   (while (looking-at "^[A-Za-z][-A-Za-z0-9]*:")
178     (notmuch-show-fontify-header)
179     (forward-line)))
180
181 (defun notmuch-show-spaces-n (n)
182   "Return a string comprised of `n' spaces."
183   (make-string n ? ))
184
185 (defun notmuch-show-update-tags (tags)
186   "Update the displayed tags of the current message."
187   (save-excursion
188     (goto-char (notmuch-show-message-top))
189     (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)
190         (let ((inhibit-read-only t))
191           (replace-match (concat "("
192                                  (mapconcat 'identity tags " ")
193                                  ")"))))))
194
195 (defun notmuch-show-insert-headerline (headers date tags depth)
196   "Insert a notmuch style headerline based on HEADERS for a
197 message at DEPTH in the current thread."
198   (let ((start (point)))
199     (insert (notmuch-show-spaces-n depth)
200             (plist-get headers :From)
201             " ("
202             date
203             ") ("
204             (mapconcat 'identity tags " ")
205             ")\n")
206     (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))
207
208 (defun notmuch-show-insert-header (header header-value)
209   "Insert a single header."
210   (insert header ": " header-value "\n"))
211
212 (defun notmuch-show-insert-headers (headers)
213   "Insert the headers of the current message."
214   (let ((start (point)))
215     (mapc '(lambda (header)
216              (let* ((header-symbol (intern (concat ":" header)))
217                     (header-value (plist-get headers header-symbol)))
218                (if (and header-value
219                         (not (string-equal "" header-value)))
220                    (notmuch-show-insert-header header header-value))))
221           notmuch-message-headers)
222     (save-excursion
223       (save-restriction
224         (narrow-to-region start (point-max))
225         (run-hooks 'notmuch-show-markup-headers-hook)))))
226
227 (define-button-type 'notmuch-show-part-button-type
228   'action 'notmuch-show-part-button-action
229   'follow-link t
230   'face 'message-mml)
231
232 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name)
233   (insert-button
234    (concat "[ "
235            (if name (concat name ": ") "")
236            declared-type
237            (if (not (string-equal declared-type content-type))
238                (concat " (as " content-type ")")
239              "")
240            " ]\n")
241    :type 'notmuch-show-part-button-type
242    :notmuch-part nth
243    :notmuch-filename name))
244
245 ;; Functions handling particular MIME parts.
246
247 (defun notmuch-show-save-part (message-id nth &optional filename)
248   (with-temp-buffer
249     ;; Always acquires the part via `notmuch part', even if it is
250     ;; available in the JSON output.
251     (insert (notmuch-show-get-bodypart-internal message-id nth))
252     (let ((file (read-file-name
253                  "Filename to save as: "
254                  (or mailcap-download-directory "~/")
255                  nil nil
256                  filename))
257           (require-final-newline nil)
258           (coding-system-for-write 'no-conversion))
259       (write-region (point-min) (point-max) file))))
260
261 (defun notmuch-show-mm-display-part-inline (msg part content-type content)
262   "Use the mm-decode/mm-view functions to display a part in the
263 current buffer, if possible."
264   (let ((display-buffer (current-buffer)))
265     (with-temp-buffer
266       (insert content)
267       (let ((handle (mm-make-handle (current-buffer) (list content-type))))
268         (set-buffer display-buffer)
269         (if (and (mm-inlinable-p handle)
270                  (mm-inlined-p handle))
271             (progn
272               (mm-display-part handle)
273               t)
274           nil)))))
275
276 (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
277   (let ((start (point)))
278     ;; If this text/plain part is not the first part in the message,
279     ;; insert a header to make this clear.
280     (if (> nth 1)
281         (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
282     (insert (notmuch-show-get-bodypart-content msg part nth))
283     (save-excursion
284       (save-restriction
285         (narrow-to-region start (point-max))
286         (run-hook-with-args 'notmuch-show-insert-text/plain-hook depth))))
287   t)
288
289 (defun notmuch-show-insert-part-application/octet-stream (msg part content-type nth depth declared-type)
290   ;; If we can deduce a MIME type from the filename of the attachment,
291   ;; do so and pass it on to the handler for that type.
292   (if (plist-get part :filename)
293       (let ((extension (file-name-extension (plist-get part :filename)))
294             mime-type)
295         (if extension
296             (progn
297               (mailcap-parse-mimetypes)
298               (setq mime-type (mailcap-extension-to-mime extension))
299               (if (and mime-type
300                        (not (string-equal mime-type "application/octet-stream")))
301                   (notmuch-show-insert-bodypart-internal msg part mime-type nth depth content-type)
302                 nil))
303           nil))))
304
305 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
306   ;; This handler _must_ succeed - it is the handler of last resort.
307   (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
308   (let ((content (notmuch-show-get-bodypart-content msg part nth)))
309     (if content
310         (notmuch-show-mm-display-part-inline msg part content-type content)))
311   t)
312
313 ;; Functions for determining how to handle MIME parts.
314
315 (defun notmuch-show-split-content-type (content-type)
316   (split-string content-type "/"))
317
318 (defun notmuch-show-handlers-for (content-type)
319   "Return a list of content handlers for a part of type CONTENT-TYPE."
320   (let (result)
321     (mapc (lambda (func)
322             (if (functionp func)
323                 (push func result)))
324           ;; Reverse order of prefrence.
325           (list (intern (concat "notmuch-show-insert-part-*/*"))
326                 (intern (concat
327                          "notmuch-show-insert-part-"
328                          (car (notmuch-show-split-content-type content-type))
329                          "/*"))
330                 (intern (concat "notmuch-show-insert-part-" content-type))))
331     result))
332
333 ;; Helper for parts which are generally not included in the default
334 ;; JSON output.
335
336 (defun notmuch-show-get-bodypart-internal (message-id part-number)
337   (with-temp-buffer
338     (let ((coding-system-for-read 'no-conversion))
339       (call-process notmuch-command nil t nil
340                     "part" (format "--part=%s" part-number) message-id)
341       (buffer-string))))
342
343 (defun notmuch-show-get-bodypart-content (msg part nth)
344   (or (plist-get part :content)
345       (notmuch-show-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth)))
346
347 ;; \f
348
349 (defun notmuch-show-insert-bodypart-internal (msg part content-type nth depth declared-type)
350   (let ((handlers (notmuch-show-handlers-for content-type)))
351     ;; Run the content handlers until one of them returns a non-nil
352     ;; value.
353     (while (and handlers
354                 (not (funcall (car handlers) msg part content-type nth depth declared-type)))
355       (setq handlers (cdr handlers))))
356   t)
357
358 (defun notmuch-show-insert-bodypart (msg part depth)
359   "Insert the body part PART at depth DEPTH in the current thread."
360   (let ((content-type (downcase (plist-get part :content-type)))
361         (nth (plist-get part :id)))
362     (notmuch-show-insert-bodypart-internal msg part content-type nth depth content-type))
363   ;; Some of the body part handlers leave point somewhere up in the
364   ;; part, so we make sure that we're down at the end.
365   (goto-char (point-max))
366   ;; Ensure that the part ends with a carriage return.
367   (if (not (bolp))
368       (insert "\n")))
369
370 (defun notmuch-show-insert-body (msg body depth)
371   "Insert the body BODY at depth DEPTH in the current thread."
372   (mapc '(lambda (part) (notmuch-show-insert-bodypart msg part depth)) body))
373
374 (defun notmuch-show-make-symbol (type)
375   (make-symbol (concat "notmuch-show-" type)))
376
377 (defun notmuch-show-insert-msg (msg depth)
378   "Insert the message MSG at depth DEPTH in the current thread."
379   (let ((headers (plist-get msg :headers))
380         ;; Indentation causes the buffer offset of the start/end
381         ;; points to move, so we must use markers.
382         message-start message-end
383         content-start content-end
384         headers-start headers-end
385         body-start body-end
386         (headers-invis-spec (notmuch-show-make-symbol "header"))
387         (message-invis-spec (notmuch-show-make-symbol "message")))
388
389     ;; Set `buffer-invisibility-spec' to `nil' (a list), otherwise
390     ;; removing items from `buffer-invisibility-spec' (which is what
391     ;; `notmuch-show-headers-visible' and
392     ;; `notmuch-show-message-visible' do) is a no-op and has no
393     ;; effect. This caused threads with only matching messages to have
394     ;; those messages hidden initially because
395     ;; `buffer-invisibility-spec' stayed `t'.
396     ;;
397     ;; This needs to be set here (rather than just above the call to
398     ;; `notmuch-show-headers-visible') because some of the part
399     ;; rendering or body washing functions
400     ;; (e.g. `notmuch-wash-text/plain-citations') manipulate
401     ;; `buffer-invisibility-spec').
402     (when (eq buffer-invisibility-spec t)
403       (setq buffer-invisibility-spec nil))
404
405     (setq message-start (point-marker))
406
407     (notmuch-show-insert-headerline headers
408                                     (or (plist-get msg :date_relative)
409                                         (plist-get headers :Date))
410                                     (plist-get msg :tags) depth)
411
412     (setq content-start (point-marker))
413
414     ;; Set `headers-start' to point after the 'Subject:' header to be
415     ;; compatible with the existing implementation. This just sets it
416     ;; to after the first header.
417     (notmuch-show-insert-headers headers)
418     ;; Headers should include a blank line (backwards compatibility).
419     (insert "\n")
420     (save-excursion
421       (goto-char content-start)
422       (forward-line 1)
423       (setq headers-start (point-marker)))
424     (setq headers-end (point-marker))
425
426     (setq body-start (point-marker))
427     (notmuch-show-insert-body msg (plist-get msg :body) depth)
428     ;; Ensure that the body ends with a newline.
429     (if (not (bolp))
430         (insert "\n"))
431     (setq body-end (point-marker))
432     (setq content-end (point-marker))
433
434     ;; Indent according to the depth in the thread.
435     (indent-rigidly content-start content-end depth)
436
437     (setq message-end (point-max-marker))
438
439     ;; Save the extents of this message over the whole text of the
440     ;; message.
441     (put-text-property message-start message-end :notmuch-message-extent (cons message-start message-end))
442
443     (plist-put msg :headers-invis-spec headers-invis-spec)
444     (overlay-put (make-overlay headers-start headers-end) 'invisible headers-invis-spec)
445
446     (plist-put msg :message-invis-spec message-invis-spec)
447     (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)
448
449     ;; Save the properties for this message. Currently this saves the
450     ;; entire message (augmented it with other stuff), which seems
451     ;; like overkill. We might save a reduced subset (for example, not
452     ;; the content).
453     (notmuch-show-set-message-properties msg)
454
455     ;; Set header visibility.
456     (notmuch-show-headers-visible msg notmuch-message-headers-visible)
457
458     ;; Message visibility depends on whether it matched the search
459     ;; criteria.
460     (notmuch-show-message-visible msg (plist-get msg :match))))
461
462 (defun notmuch-show-insert-tree (tree depth)
463   "Insert the message tree TREE at depth DEPTH in the current thread."
464   (let ((msg (car tree))
465         (replies (cadr tree)))
466     (notmuch-show-insert-msg msg depth)
467     (notmuch-show-insert-thread replies (1+ depth))))
468
469 (defun notmuch-show-insert-thread (thread depth)
470   "Insert the thread THREAD at depth DEPTH in the current forest."
471   (mapc '(lambda (tree) (notmuch-show-insert-tree tree depth)) thread))
472
473 (defun notmuch-show-insert-forest (forest)
474   "Insert the forest of threads FOREST."
475   (mapc '(lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
476
477 (defvar notmuch-show-parent-buffer nil)
478
479 ;;;###autoload
480 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
481   "Run \"notmuch show\" with the given thread ID and display results.
482
483 The optional PARENT-BUFFER is the notmuch-search buffer from
484 which this notmuch-show command was executed, (so that the
485 next thread from that buffer can be show when done with this
486 one).
487
488 The optional QUERY-CONTEXT is a notmuch search term. Only
489 messages from the thread matching this search term are shown if
490 non-nil.
491
492 The optional BUFFER-NAME provides the neame of the buffer in
493 which the message thread is shown. If it is nil (which occurs
494 when the command is called interactively) the argument to the
495 function is used. "
496   (interactive "sNotmuch show: ")
497   (let ((buffer (get-buffer-create (generate-new-buffer-name
498                                     (or buffer-name
499                                         (concat "*notmuch-" thread-id "*")))))
500         (inhibit-read-only t))
501     (switch-to-buffer buffer)
502     (notmuch-show-mode)
503     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
504     (erase-buffer)
505     (goto-char (point-min))
506     (save-excursion
507       (let* ((basic-args (list thread-id))
508              (args (if query-context
509                        (append (list "\'") basic-args (list "and (" query-context ")\'"))
510                      (append (list "\'") basic-args (list "\'")))))
511         (notmuch-show-insert-forest (notmuch-query-get-threads args))
512         ;; If the query context reduced the results to nothing, run
513         ;; the basic query.
514         (when (and (eq (buffer-size) 0)
515                    query-context)
516           (notmuch-show-insert-forest
517            (notmuch-query-get-threads basic-args))))
518
519       ;; Enable buttonisation of URLs and email addresses in the
520       ;; buffer.
521       (goto-address-mode t)
522       ;; Act on visual lines rather than logical lines.
523       (visual-line-mode t)
524
525       (run-hooks 'notmuch-show-hook))
526
527     ;; Move straight to the first open message
528     (if (not (notmuch-show-message-visible-p))
529         (notmuch-show-next-open-message))
530     (notmuch-show-mark-read)))
531
532 (defvar notmuch-show-stash-map
533   (let ((map (make-sparse-keymap)))
534     (define-key map "c" 'notmuch-show-stash-cc)
535     (define-key map "d" 'notmuch-show-stash-date)
536     (define-key map "F" 'notmuch-show-stash-filename)
537     (define-key map "f" 'notmuch-show-stash-from)
538     (define-key map "i" 'notmuch-show-stash-message-id)
539     (define-key map "s" 'notmuch-show-stash-subject)
540     (define-key map "T" 'notmuch-show-stash-tags)
541     (define-key map "t" 'notmuch-show-stash-to)
542     map)
543   "Submap for stash commands")
544 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
545
546 (defvar notmuch-show-mode-map
547       (let ((map (make-sparse-keymap)))
548         (define-key map "?" 'notmuch-help)
549         (define-key map "q" 'kill-this-buffer)
550         (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
551         (define-key map (kbd "<backtab>") '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" 'notmuch-mua-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 "h" 'notmuch-show-toggle-headers)
563         (define-key map "-" 'notmuch-show-remove-tag)
564         (define-key map "+" 'notmuch-show-add-tag)
565         (define-key map "x" 'notmuch-show-archive-thread-then-exit)
566         (define-key map "a" 'notmuch-show-archive-thread)
567         (define-key map "N" 'notmuch-show-next-message)
568         (define-key map "P" 'notmuch-show-previous-message)
569         (define-key map "n" 'notmuch-show-next-open-message)
570         (define-key map "p" 'notmuch-show-previous-open-message)
571         (define-key map (kbd "DEL") 'notmuch-show-rewind)
572         (define-key map " " 'notmuch-show-advance-and-archive)
573         (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
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       ;; When making the message visible, the headers may or not be
682       ;; visible. So we check that property separately.
683       (let ((headers-visible (plist-get props :headers-visible)))
684         (notmuch-show-element-visible props headers-visible :headers-invis-spec)
685         (notmuch-show-element-visible props t :message-invis-spec))
686     (notmuch-show-element-visible props nil :headers-invis-spec)
687     (notmuch-show-element-visible props nil :message-invis-spec))
688
689   (notmuch-show-set-prop :message-visible visible-p props))
690
691 (defun notmuch-show-headers-visible (props visible-p)
692   (if (plist-get props :message-visible)
693       (notmuch-show-element-visible props visible-p :headers-invis-spec))
694   (notmuch-show-set-prop :headers-visible visible-p props))
695
696 ;; Functions for setting and getting attributes of the current
697 ;; message.
698
699 (defun notmuch-show-set-message-properties (props)
700   (save-excursion
701     (notmuch-show-move-to-message-top)
702     (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
703
704 (defun notmuch-show-get-message-properties ()
705   (save-excursion
706     (notmuch-show-move-to-message-top)
707     (get-text-property (point) :notmuch-message-properties)))
708
709 (defun notmuch-show-set-prop (prop val &optional props)
710   (let ((inhibit-read-only t)
711         (props (or props
712                    (notmuch-show-get-message-properties))))
713     (plist-put props prop val)
714     (notmuch-show-set-message-properties props)))
715
716 (defun notmuch-show-get-prop (prop &optional props)
717   (let ((props (or props
718                    (notmuch-show-get-message-properties))))
719     (plist-get props prop)))
720
721 (defun notmuch-show-get-message-id ()
722   "Return the message id of the current message."
723   (concat "id:" (notmuch-show-get-prop :id)))
724
725 ;; dme: Would it make sense to use a macro for many of these?
726
727 (defun notmuch-show-get-filename ()
728   "Return the filename of the current message."
729   (notmuch-show-get-prop :filename))
730
731 (defun notmuch-show-get-header (header)
732   "Return the named header of the current message, if any."
733   (plist-get (notmuch-show-get-prop :headers) header))
734
735 (defun notmuch-show-get-cc ()
736   (notmuch-show-get-header :Cc))
737
738 (defun notmuch-show-get-date ()
739   (notmuch-show-get-header :Date))
740
741 (defun notmuch-show-get-from ()
742   (notmuch-show-get-header :From))
743
744 (defun notmuch-show-get-subject ()
745   (notmuch-show-get-header :Subject))
746
747 (defun notmuch-show-get-to ()
748   (notmuch-show-get-header :To))
749
750 (defun notmuch-show-set-tags (tags)
751   "Set the tags of the current message."
752   (notmuch-show-set-prop :tags tags)
753   (notmuch-show-update-tags tags))
754
755 (defun notmuch-show-get-tags ()
756   "Return the tags of the current message."
757   (notmuch-show-get-prop :tags))
758
759 (defun notmuch-show-message-visible-p ()
760   "Is the current message visible?"
761   (notmuch-show-get-prop :message-visible))
762
763 (defun notmuch-show-headers-visible-p ()
764   "Are the headers of the current message visible?"
765   (notmuch-show-get-prop :headers-visible))
766
767 (defun notmuch-show-mark-read ()
768   "Mark the current message as read."
769   (notmuch-show-remove-tag "unread"))
770
771 ;; Commands typically bound to keys.
772
773 (defun notmuch-show-advance-and-archive ()
774   "Advance through thread and archive.
775
776 This command is intended to be one of the simplest ways to
777 process a thread of email. It does the following:
778
779 If the current message in the thread is not yet fully visible,
780 scroll by a near screenful to read more of the message.
781
782 Otherwise, (the end of the current message is already within the
783 current window), advance to the next open message.
784
785 Finally, if there is no further message to advance to, and this
786 last message is already read, then archive the entire current
787 thread, (remove the \"inbox\" tag from each message). Also kill
788 this buffer, and display the next thread from the search from
789 which this thread was originally shown."
790   (interactive)
791   (let ((end-of-this-message (notmuch-show-message-bottom)))
792     (cond
793      ;; Ideally we would test `end-of-this-message' against the result
794      ;; of `window-end', but that doesn't account for the fact that
795      ;; the end of the message might be hidden, so we have to actually
796      ;; go to the end, walk back over invisible text and then see if
797      ;; point is visible.
798      ((save-excursion
799         (goto-char (- end-of-this-message 1))
800         (notmuch-show-move-past-invisible-backward)
801         (> (point) (window-end)))
802       ;; The bottom of this message is not visible - scroll.
803       (scroll-up nil))
804
805      ((not (= end-of-this-message (point-max)))
806       ;; This is not the last message - move to the next visible one.
807       (notmuch-show-next-open-message))
808
809      (t
810       ;; This is the last message - archive the thread.
811       (notmuch-show-archive-thread)))))
812
813 (defun notmuch-show-rewind ()
814   "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
815
816 Specifically, if the beginning of the previous email is fewer
817 than `window-height' lines from the current point, move to it
818 just like `notmuch-show-previous-message'.
819
820 Otherwise, just scroll down a screenful of the current message.
821
822 This command does not modify any message tags, (it does not undo
823 any effects from previous calls to
824 `notmuch-show-advance-and-archive'."
825   (interactive)
826   (let ((start-of-message (notmuch-show-message-top))
827         (start-of-window (window-start)))
828     (cond
829       ;; Either this message is properly aligned with the start of the
830       ;; window or the start of this message is not visible on the
831       ;; screen - scroll.
832      ((or (= start-of-message start-of-window)
833           (< start-of-message start-of-window))
834       (scroll-down)
835       ;; If a small number of lines from the previous message are
836       ;; visible, realign so that the top of the current message is at
837       ;; the top of the screen.
838       (if (<= (count-screen-lines (window-start) start-of-message)
839               next-screen-context-lines)
840           (progn
841             (goto-char (notmuch-show-message-top))
842             (notmuch-show-message-adjust)))
843       ;; Move to the top left of the window.
844       (goto-char (window-start)))
845      (t
846       ;; Move to the previous message.
847       (notmuch-show-previous-message)))))
848
849 (defun notmuch-show-reply ()
850   "Reply to the current message."
851   (interactive)
852   (notmuch-mua-reply (notmuch-show-get-message-id)))
853
854 (defun notmuch-show-forward-message ()
855   "Forward the current message."
856   (interactive)
857   (with-current-notmuch-show-message
858    (notmuch-mua-forward-message)))
859
860 (defun notmuch-show-next-message ()
861   "Show the next message."
862   (interactive)
863   (if (notmuch-show-goto-message-next)
864       (progn
865         (notmuch-show-mark-read)
866         (notmuch-show-message-adjust))
867     (goto-char (point-max))))
868
869 (defun notmuch-show-previous-message ()
870   "Show the previous message."
871   (interactive)
872   (notmuch-show-goto-message-previous)
873   (notmuch-show-mark-read)
874   (notmuch-show-message-adjust))
875
876 (defun notmuch-show-next-open-message ()
877   "Show the next message."
878   (interactive)
879   (let (r)
880     (while (and (setq r (notmuch-show-goto-message-next))
881                 (not (notmuch-show-message-visible-p))))
882     (if r
883         (progn
884           (notmuch-show-mark-read)
885           (notmuch-show-message-adjust))
886       (goto-char (point-max)))))
887
888 (defun notmuch-show-previous-open-message ()
889   "Show the previous message."
890   (interactive)
891   (while (and (notmuch-show-goto-message-previous)
892               (not (notmuch-show-message-visible-p))))
893   (notmuch-show-mark-read)
894   (notmuch-show-message-adjust))
895
896 (defun notmuch-show-view-raw-message ()
897   "View the file holding the current message."
898   (interactive)
899   (view-file (notmuch-show-get-filename)))
900
901 (defun notmuch-show-pipe-message (command)
902   "Pipe the contents of the current message to the given command.
903
904 The given command will be executed with the raw contents of the
905 current email message as stdin. Anything printed by the command
906 to stdout or stderr will appear in the *Messages* buffer."
907   (interactive "sPipe message to command: ")
908   (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
909          (list command " < "
910                (shell-quote-argument (notmuch-show-get-filename)))))
911
912 (defun notmuch-show-add-tag (&rest toadd)
913   "Add a tag to the current message."
914   (interactive
915    (list (notmuch-select-tag-with-completion "Tag to add: ")))
916   (apply 'notmuch-call-notmuch-process
917          (append (cons "tag"
918                        (mapcar (lambda (s) (concat "+" s)) toadd))
919                  (cons (notmuch-show-get-message-id) nil)))
920   (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
921
922 (defun notmuch-show-remove-tag (&rest toremove)
923   "Remove a tag from the current message."
924   (interactive
925    (list (notmuch-select-tag-with-completion
926           "Tag to remove: " (notmuch-show-get-message-id))))
927   (let ((tags (notmuch-show-get-tags)))
928     (if (intersection tags toremove :test 'string=)
929         (progn
930           (apply 'notmuch-call-notmuch-process
931                  (append (cons "tag"
932                                (mapcar (lambda (s) (concat "-" s)) toremove))
933                          (cons (notmuch-show-get-message-id) nil)))
934           (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
935
936 (defun notmuch-show-toggle-headers ()
937   "Toggle the visibility of the current message headers."
938   (interactive)
939   (let ((props (notmuch-show-get-message-properties)))
940     (notmuch-show-headers-visible
941      props
942      (not (plist-get props :headers-visible))))
943   (force-window-update))
944
945 (defun notmuch-show-toggle-message ()
946   "Toggle the visibility of the current message."
947   (interactive)
948   (let ((props (notmuch-show-get-message-properties)))
949     (notmuch-show-message-visible
950      props
951      (not (plist-get props :message-visible))))
952   (force-window-update))
953
954 (defun notmuch-show-open-or-close-all ()
955   "Set the visibility all of the messages in the current thread.
956 By default make all of the messages visible. With a prefix
957 argument, hide all of the messages."
958   (interactive)
959   (save-excursion
960     (goto-char (point-min))
961     (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
962                                            (not current-prefix-arg))
963           until (not (notmuch-show-goto-message-next))))
964   (force-window-update))
965
966 (defun notmuch-show-next-button ()
967   "Advance point to the next button in the buffer."
968   (interactive)
969   (forward-button 1))
970
971 (defun notmuch-show-previous-button ()
972   "Move point back to the previous button in the buffer."
973   (interactive)
974   (backward-button 1))
975
976 (defun notmuch-show-archive-thread-internal (show-next)
977   ;; Remove the tag from the current set of messages.
978   (goto-char (point-min))
979   (loop do (notmuch-show-remove-tag "inbox")
980         until (not (notmuch-show-goto-message-next)))
981   ;; Move to the next item in the search results, if any.
982   (let ((parent-buffer notmuch-show-parent-buffer))
983     (kill-this-buffer)
984     (if parent-buffer
985         (progn
986           (switch-to-buffer parent-buffer)
987           (forward-line)
988           (if show-next
989               (notmuch-search-show-thread))))))
990
991 (defun notmuch-show-archive-thread ()
992   "Archive each message in thread, then show next thread from search.
993
994 Archive each message currently shown by removing the \"inbox\"
995 tag from each. Then kill this buffer and show the next thread
996 from the search from which this thread was originally shown.
997
998 Note: This command is safe from any race condition of new messages
999 being delivered to the same thread. It does not archive the
1000 entire thread, but only the messages shown in the current
1001 buffer."
1002   (interactive)
1003   (notmuch-show-archive-thread-internal t))
1004
1005 (defun notmuch-show-archive-thread-then-exit ()
1006   "Archive each message in thread, then exit back to search results."
1007   (interactive)
1008   (notmuch-show-archive-thread-internal nil))
1009
1010 (defun notmuch-show-do-stash (text)
1011   (kill-new text)
1012   (message "Saved: %s" text))
1013
1014 (defun notmuch-show-stash-cc ()
1015   "Copy CC field of current message to kill-ring."
1016   (interactive)
1017   (notmuch-show-do-stash (notmuch-show-get-cc)))
1018
1019 (defun notmuch-show-stash-date ()
1020   "Copy date of current message to kill-ring."
1021   (interactive)
1022   (notmuch-show-do-stash (notmuch-show-get-date)))
1023
1024 (defun notmuch-show-stash-filename ()
1025   "Copy filename of current message to kill-ring."
1026   (interactive)
1027   (notmuch-show-do-stash (notmuch-show-get-filename)))
1028
1029 (defun notmuch-show-stash-from ()
1030   "Copy From address of current message to kill-ring."
1031   (interactive)
1032   (notmuch-show-do-stash (notmuch-show-get-from)))
1033
1034 (defun notmuch-show-stash-message-id ()
1035   "Copy message ID of current message to kill-ring."
1036   (interactive)
1037   (notmuch-show-do-stash (notmuch-show-get-message-id)))
1038
1039 (defun notmuch-show-stash-subject ()
1040   "Copy Subject field of current message to kill-ring."
1041   (interactive)
1042   (notmuch-show-do-stash (notmuch-show-get-subject)))
1043
1044 (defun notmuch-show-stash-tags ()
1045   "Copy tags of current message to kill-ring as a comma separated list."
1046   (interactive)
1047   (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1048
1049 (defun notmuch-show-stash-to ()
1050   "Copy To address of current message to kill-ring."
1051   (interactive)
1052   (notmuch-show-do-stash (notmuch-show-get-to)))
1053
1054 ;; Commands typically bound to buttons.
1055
1056 (defun notmuch-show-part-button-action (button)
1057   (let ((nth (button-get button :notmuch-part)))
1058     (if nth
1059         (notmuch-show-save-part (notmuch-show-get-message-id) nth
1060                                 (button-get button :notmuch-filename))
1061       (message "Not a valid part (is it a fake part?)."))))
1062
1063 ;;
1064
1065 (provide 'notmuch-show)