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