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