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