]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-show.el
Merge branch '0.3.x'
[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 "TAB") 'notmuch-show-next-button)
552         (define-key map "s" 'notmuch-search)
553         (define-key map "m" 'notmuch-mua-mail)
554         (define-key map "f" 'notmuch-show-forward-message)
555         (define-key map "r" 'notmuch-show-reply)
556         (define-key map "|" 'notmuch-show-pipe-message)
557         (define-key map "w" 'notmuch-show-save-attachments)
558         (define-key map "V" 'notmuch-show-view-raw-message)
559         (define-key map "v" 'notmuch-show-view-all-mime-parts)
560         (define-key map "c" 'notmuch-show-stash-map)
561         (define-key map "h" 'notmuch-show-toggle-headers)
562         (define-key map "-" 'notmuch-show-remove-tag)
563         (define-key map "+" 'notmuch-show-add-tag)
564         (define-key map "x" 'notmuch-show-archive-thread-then-exit)
565         (define-key map "a" 'notmuch-show-archive-thread)
566         (define-key map "N" 'notmuch-show-next-message)
567         (define-key map "P" 'notmuch-show-previous-message)
568         (define-key map "n" 'notmuch-show-next-open-message)
569         (define-key map "p" 'notmuch-show-previous-open-message)
570         (define-key map (kbd "DEL") 'notmuch-show-rewind)
571         (define-key map " " 'notmuch-show-advance-and-archive)
572         (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)
573         (define-key map (kbd "RET") 'notmuch-show-toggle-message)
574         map)
575       "Keymap for \"notmuch show\" buffers.")
576 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
577
578 ;;;###autoload
579 (defun notmuch-show-mode ()
580   "Major mode for viewing a thread with notmuch.
581
582 This buffer contains the results of the \"notmuch show\" command
583 for displaying a single thread of email from your email archives.
584
585 By default, various components of email messages, (citations,
586 signatures, already-read messages), are hidden. You can make
587 these parts visible by clicking with the mouse button or by
588 pressing RET after positioning the cursor on a hidden part, (for
589 which \\[notmuch-show-next-button] and
590 \\[notmuch-show-previous-button] are helpful).
591
592 Reading the thread sequentially is well-supported by pressing
593 \\[notmuch-show-advance-and-archive]. This will scroll the
594 current message (if necessary), advance to the next message, or
595 advance to the next thread (if already on the last message of a
596 thread).
597
598 Other commands are available to read or manipulate the thread
599 more selectively, (such as '\\[notmuch-show-next-message]' and
600 '\\[notmuch-show-previous-message]' to advance to messages
601 without removing any tags, and '\\[notmuch-show-archive-thread]'
602 to archive an entire thread without scrolling through with
603 \\[notmuch-show-advance-and-archive]).
604
605 You can add or remove arbitary tags from the current message with
606 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
607
608 All currently available key bindings:
609
610 \\{notmuch-show-mode-map}"
611   (interactive)
612   (kill-all-local-variables)
613   (use-local-map notmuch-show-mode-map)
614   (setq major-mode 'notmuch-show-mode
615         mode-name "notmuch-show")
616   (setq buffer-read-only t))
617
618 (defun notmuch-show-move-to-message-top ()
619   (goto-char (notmuch-show-message-top)))
620
621 (defun notmuch-show-move-to-message-bottom ()
622   (goto-char (notmuch-show-message-bottom)))
623
624 (defun notmuch-show-message-adjust ()
625   (recenter 0))
626
627 ;; Movement related functions.
628
629 ;; There's some strangeness here where a text property applied to a
630 ;; region a->b is not found when point is at b. We walk backwards
631 ;; until finding the property.
632 (defun notmuch-show-message-extent ()
633   (let (r)
634     (save-excursion
635       (while (not (setq r (get-text-property (point) :notmuch-message-extent)))
636         (backward-char)))
637     r))
638
639 (defun notmuch-show-message-top ()
640   (car (notmuch-show-message-extent)))
641
642 (defun notmuch-show-message-bottom ()
643   (cdr (notmuch-show-message-extent)))
644
645 (defun notmuch-show-goto-message-next ()
646   (let ((start (point)))
647     (notmuch-show-move-to-message-bottom)
648     (if (not (eobp))
649         t
650       (goto-char start)
651       nil)))
652
653 (defun notmuch-show-goto-message-previous ()
654   (notmuch-show-move-to-message-top)
655   (if (bobp)
656       nil
657     (backward-char)
658     (notmuch-show-move-to-message-top)
659     t))
660
661 (defun notmuch-show-move-past-invisible-forward ()
662   (while (point-invisible-p)
663     (forward-char)))
664
665 (defun notmuch-show-move-past-invisible-backward ()
666   (while (point-invisible-p)
667     (backward-char)))
668
669 ;; Functions relating to the visibility of messages and their
670 ;; components.
671
672 (defun notmuch-show-element-visible (props visible-p spec-property)
673   (let ((spec (plist-get props spec-property)))
674     (if visible-p
675         (remove-from-invisibility-spec spec)
676       (add-to-invisibility-spec spec))))
677
678 (defun notmuch-show-message-visible (props visible-p)
679   (if visible-p
680       ;; When making the message visible, the headers may or not be
681       ;; visible. So we check that property separately.
682       (let ((headers-visible (plist-get props :headers-visible)))
683         (notmuch-show-element-visible props headers-visible :headers-invis-spec)
684         (notmuch-show-element-visible props t :message-invis-spec))
685     (notmuch-show-element-visible props nil :headers-invis-spec)
686     (notmuch-show-element-visible props nil :message-invis-spec))
687
688   (notmuch-show-set-prop :message-visible visible-p props))
689
690 (defun notmuch-show-headers-visible (props visible-p)
691   (if (plist-get props :message-visible)
692       (notmuch-show-element-visible props visible-p :headers-invis-spec))
693   (notmuch-show-set-prop :headers-visible visible-p props))
694
695 ;; Functions for setting and getting attributes of the current
696 ;; message.
697
698 (defun notmuch-show-set-message-properties (props)
699   (save-excursion
700     (notmuch-show-move-to-message-top)
701     (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
702
703 (defun notmuch-show-get-message-properties ()
704   (save-excursion
705     (notmuch-show-move-to-message-top)
706     (get-text-property (point) :notmuch-message-properties)))
707
708 (defun notmuch-show-set-prop (prop val &optional props)
709   (let ((inhibit-read-only t)
710         (props (or props
711                    (notmuch-show-get-message-properties))))
712     (plist-put props prop val)
713     (notmuch-show-set-message-properties props)))
714
715 (defun notmuch-show-get-prop (prop &optional props)
716   (let ((props (or props
717                    (notmuch-show-get-message-properties))))
718     (plist-get props prop)))
719
720 (defun notmuch-show-get-message-id ()
721   "Return the message id of the current message."
722   (concat "id:" (notmuch-show-get-prop :id)))
723
724 ;; dme: Would it make sense to use a macro for many of these?
725
726 (defun notmuch-show-get-filename ()
727   "Return the filename of the current message."
728   (notmuch-show-get-prop :filename))
729
730 (defun notmuch-show-get-header (header)
731   "Return the named header of the current message, if any."
732   (plist-get (notmuch-show-get-prop :headers) header))
733
734 (defun notmuch-show-get-cc ()
735   (notmuch-show-get-header :Cc))
736
737 (defun notmuch-show-get-date ()
738   (notmuch-show-get-header :Date))
739
740 (defun notmuch-show-get-from ()
741   (notmuch-show-get-header :From))
742
743 (defun notmuch-show-get-subject ()
744   (notmuch-show-get-header :Subject))
745
746 (defun notmuch-show-get-to ()
747   (notmuch-show-get-header :To))
748
749 (defun notmuch-show-set-tags (tags)
750   "Set the tags of the current message."
751   (notmuch-show-set-prop :tags tags)
752   (notmuch-show-update-tags tags))
753
754 (defun notmuch-show-get-tags ()
755   "Return the tags of the current message."
756   (notmuch-show-get-prop :tags))
757
758 (defun notmuch-show-message-visible-p ()
759   "Is the current message visible?"
760   (notmuch-show-get-prop :message-visible))
761
762 (defun notmuch-show-headers-visible-p ()
763   "Are the headers of the current message visible?"
764   (notmuch-show-get-prop :headers-visible))
765
766 (defun notmuch-show-mark-read ()
767   "Mark the current message as read."
768   (notmuch-show-remove-tag "unread"))
769
770 ;; Commands typically bound to keys.
771
772 (defun notmuch-show-advance-and-archive ()
773   "Advance through thread and archive.
774
775 This command is intended to be one of the simplest ways to
776 process a thread of email. It does the following:
777
778 If the current message in the thread is not yet fully visible,
779 scroll by a near screenful to read more of the message.
780
781 Otherwise, (the end of the current message is already within the
782 current window), advance to the next open message.
783
784 Finally, if there is no further message to advance to, and this
785 last message is already read, then archive the entire current
786 thread, (remove the \"inbox\" tag from each message). Also kill
787 this buffer, and display the next thread from the search from
788 which this thread was originally shown."
789   (interactive)
790   (let ((end-of-this-message (notmuch-show-message-bottom)))
791     (cond
792      ;; Ideally we would test `end-of-this-message' against the result
793      ;; of `window-end', but that doesn't account for the fact that
794      ;; the end of the message might be hidden, so we have to actually
795      ;; go to the end, walk back over invisible text and then see if
796      ;; point is visible.
797      ((save-excursion
798         (goto-char (- end-of-this-message 1))
799         (notmuch-show-move-past-invisible-backward)
800         (> (point) (window-end)))
801       ;; The bottom of this message is not visible - scroll.
802       (scroll-up nil))
803
804      ((not (= end-of-this-message (point-max)))
805       ;; This is not the last message - move to the next visible one.
806       (notmuch-show-next-open-message))
807
808      (t
809       ;; This is the last message - archive the thread.
810       (notmuch-show-archive-thread)))))
811
812 (defun notmuch-show-rewind ()
813   "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
814
815 Specifically, if the beginning of the previous email is fewer
816 than `window-height' lines from the current point, move to it
817 just like `notmuch-show-previous-message'.
818
819 Otherwise, just scroll down a screenful of the current message.
820
821 This command does not modify any message tags, (it does not undo
822 any effects from previous calls to
823 `notmuch-show-advance-and-archive'."
824   (interactive)
825   (let ((start-of-message (notmuch-show-message-top))
826         (start-of-window (window-start)))
827     (cond
828       ;; Either this message is properly aligned with the start of the
829       ;; window or the start of this message is not visible on the
830       ;; screen - scroll.
831      ((or (= start-of-message start-of-window)
832           (< start-of-message start-of-window))
833       (scroll-down)
834       ;; If a small number of lines from the previous message are
835       ;; visible, realign so that the top of the current message is at
836       ;; the top of the screen.
837       (if (<= (count-screen-lines (window-start) start-of-message)
838               next-screen-context-lines)
839           (progn
840             (goto-char (notmuch-show-message-top))
841             (notmuch-show-message-adjust)))
842       ;; Move to the top left of the window.
843       (goto-char (window-start)))
844      (t
845       ;; Move to the previous message.
846       (notmuch-show-previous-message)))))
847
848 (defun notmuch-show-reply ()
849   "Reply to the current message."
850   (interactive)
851   (notmuch-mua-reply (notmuch-show-get-message-id)))
852
853 (defun notmuch-show-forward-message ()
854   "Forward the current message."
855   (interactive)
856   (with-current-notmuch-show-message
857    (notmuch-mua-forward-message)))
858
859 (defun notmuch-show-next-message ()
860   "Show the next message."
861   (interactive)
862   (if (notmuch-show-goto-message-next)
863       (progn
864         (notmuch-show-mark-read)
865         (notmuch-show-message-adjust))
866     (goto-char (point-max))))
867
868 (defun notmuch-show-previous-message ()
869   "Show the previous message."
870   (interactive)
871   (notmuch-show-goto-message-previous)
872   (notmuch-show-mark-read)
873   (notmuch-show-message-adjust))
874
875 (defun notmuch-show-next-open-message ()
876   "Show the next message."
877   (interactive)
878   (let (r)
879     (while (and (setq r (notmuch-show-goto-message-next))
880                 (not (notmuch-show-message-visible-p))))
881     (if r
882         (progn
883           (notmuch-show-mark-read)
884           (notmuch-show-message-adjust))
885       (goto-char (point-max)))))
886
887 (defun notmuch-show-previous-open-message ()
888   "Show the previous message."
889   (interactive)
890   (while (and (notmuch-show-goto-message-previous)
891               (not (notmuch-show-message-visible-p))))
892   (notmuch-show-mark-read)
893   (notmuch-show-message-adjust))
894
895 (defun notmuch-show-view-raw-message ()
896   "View the file holding the current message."
897   (interactive)
898   (view-file (notmuch-show-get-filename)))
899
900 (defun notmuch-show-pipe-message (command)
901   "Pipe the contents of the current message to the given command.
902
903 The given command will be executed with the raw contents of the
904 current email message as stdin. Anything printed by the command
905 to stdout or stderr will appear in the *Messages* buffer."
906   (interactive "sPipe message to command: ")
907   (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
908          (list command " < "
909                (shell-quote-argument (notmuch-show-get-filename)))))
910
911 (defun notmuch-show-add-tag (&rest toadd)
912   "Add a tag to the current message."
913   (interactive
914    (list (notmuch-select-tag-with-completion "Tag to add: ")))
915   (apply 'notmuch-call-notmuch-process
916          (append (cons "tag"
917                        (mapcar (lambda (s) (concat "+" s)) toadd))
918                  (cons (notmuch-show-get-message-id) nil)))
919   (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
920
921 (defun notmuch-show-remove-tag (&rest toremove)
922   "Remove a tag from the current message."
923   (interactive
924    (list (notmuch-select-tag-with-completion
925           "Tag to remove: " (notmuch-show-get-message-id))))
926   (let ((tags (notmuch-show-get-tags)))
927     (if (intersection tags toremove :test 'string=)
928         (progn
929           (apply 'notmuch-call-notmuch-process
930                  (append (cons "tag"
931                                (mapcar (lambda (s) (concat "-" s)) toremove))
932                          (cons (notmuch-show-get-message-id) nil)))
933           (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
934
935 (defun notmuch-show-toggle-headers ()
936   "Toggle the visibility of the current message headers."
937   (interactive)
938   (let ((props (notmuch-show-get-message-properties)))
939     (notmuch-show-headers-visible
940      props
941      (not (plist-get props :headers-visible))))
942   (force-window-update))
943
944 (defun notmuch-show-toggle-message ()
945   "Toggle the visibility of the current message."
946   (interactive)
947   (let ((props (notmuch-show-get-message-properties)))
948     (notmuch-show-message-visible
949      props
950      (not (plist-get props :message-visible))))
951   (force-window-update))
952
953 (defun notmuch-show-open-or-close-all ()
954   "Set the visibility all of the messages in the current thread.
955 By default make all of the messages visible. With a prefix
956 argument, hide all of the messages."
957   (interactive)
958   (save-excursion
959     (goto-char (point-min))
960     (loop do (notmuch-show-message-visible (notmuch-show-get-message-properties)
961                                            (not current-prefix-arg))
962           until (not (notmuch-show-goto-message-next))))
963   (force-window-update))
964
965 (defun notmuch-show-next-button ()
966   "Advance point to the next button in the buffer."
967   (interactive)
968   (forward-button 1))
969
970 (defun notmuch-show-previous-button ()
971   "Move point back to the previous button in the buffer."
972   (interactive)
973   (backward-button 1))
974
975 (defun notmuch-show-archive-thread-internal (show-next)
976   ;; Remove the tag from the current set of messages.
977   (goto-char (point-min))
978   (loop do (notmuch-show-remove-tag "inbox")
979         until (not (notmuch-show-goto-message-next)))
980   ;; Move to the next item in the search results, if any.
981   (let ((parent-buffer notmuch-show-parent-buffer))
982     (kill-this-buffer)
983     (if parent-buffer
984         (progn
985           (switch-to-buffer parent-buffer)
986           (forward-line)
987           (if show-next
988               (notmuch-search-show-thread))))))
989
990 (defun notmuch-show-archive-thread ()
991   "Archive each message in thread, then show next thread from search.
992
993 Archive each message currently shown by removing the \"inbox\"
994 tag from each. Then kill this buffer and show the next thread
995 from the search from which this thread was originally shown.
996
997 Note: This command is safe from any race condition of new messages
998 being delivered to the same thread. It does not archive the
999 entire thread, but only the messages shown in the current
1000 buffer."
1001   (interactive)
1002   (notmuch-show-archive-thread-internal t))
1003
1004 (defun notmuch-show-archive-thread-then-exit ()
1005   "Archive each message in thread, then exit back to search results."
1006   (interactive)
1007   (notmuch-show-archive-thread-internal nil))
1008
1009 (defun notmuch-show-do-stash (text)
1010   (kill-new text)
1011   (message "Saved: %s" text))
1012
1013 (defun notmuch-show-stash-cc ()
1014   "Copy CC field of current message to kill-ring."
1015   (interactive)
1016   (notmuch-show-do-stash (notmuch-show-get-cc)))
1017
1018 (defun notmuch-show-stash-date ()
1019   "Copy date of current message to kill-ring."
1020   (interactive)
1021   (notmuch-show-do-stash (notmuch-show-get-date)))
1022
1023 (defun notmuch-show-stash-filename ()
1024   "Copy filename of current message to kill-ring."
1025   (interactive)
1026   (notmuch-show-do-stash (notmuch-show-get-filename)))
1027
1028 (defun notmuch-show-stash-from ()
1029   "Copy From address of current message to kill-ring."
1030   (interactive)
1031   (notmuch-show-do-stash (notmuch-show-get-from)))
1032
1033 (defun notmuch-show-stash-message-id ()
1034   "Copy message ID of current message to kill-ring."
1035   (interactive)
1036   (notmuch-show-do-stash (notmuch-show-get-message-id)))
1037
1038 (defun notmuch-show-stash-subject ()
1039   "Copy Subject field of current message to kill-ring."
1040   (interactive)
1041   (notmuch-show-do-stash (notmuch-show-get-subject)))
1042
1043 (defun notmuch-show-stash-tags ()
1044   "Copy tags of current message to kill-ring as a comma separated list."
1045   (interactive)
1046   (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1047
1048 (defun notmuch-show-stash-to ()
1049   "Copy To address of current message to kill-ring."
1050   (interactive)
1051   (notmuch-show-do-stash (notmuch-show-get-to)))
1052
1053 ;; Commands typically bound to buttons.
1054
1055 (defun notmuch-show-part-button-action (button)
1056   (let ((nth (button-get button :notmuch-part)))
1057     (if nth
1058         (notmuch-show-save-part (notmuch-show-get-message-id) nth
1059                                 (button-get button :notmuch-filename))
1060       (message "Not a valid part (is it a fake part?)."))))
1061
1062 ;;
1063
1064 (provide 'notmuch-show)