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