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