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