]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
notmuch.el: Indent messages to show nested structure of thread.
[notmuch] / notmuch.el
1 ; notmuch.el --- run notmuch within emacs
2 ;
3 ; Copyright © Carl Worth
4 ;
5 ; This file is part of Notmuch.
6 ;
7 ; Notmuch is free software: you can redistribute it and/or modify it
8 ; under the terms of the GNU General Public License as published by
9 ; the Free Software Foundation, either version 3 of the License, or
10 ; (at your option) any later version.
11 ;
12 ; Notmuch is distributed in the hope that it will be useful, but
13 ; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 ; General Public License for more details.
16 ;
17 ; You should have received a copy of the GNU General Public License
18 ; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.
19 ;
20 ; Authors: Carl Worth <cworth@cworth.org>
21
22 (require 'cl)
23 (require 'mm-view)
24
25 (defvar notmuch-show-mode-map
26   (let ((map (make-sparse-keymap)))
27     ; I don't actually want all of these toggle commands occupying
28     ; keybindings. They steal valuable key-binding space, are hard
29     ; to remember, and act globally rather than locally.
30     ;
31     ; Will be much preferable to switch to direct manipulation for
32     ; toggling visibility of these components. Probably using
33     ; overlays-at to query and manipulate the current overlay.
34     (define-key map "a" 'notmuch-show-archive-thread)
35     (define-key map "A" 'notmuch-show-mark-read-then-archive-thread)
36     (define-key map "b" 'notmuch-show-toggle-body-read-visible)
37     (define-key map "c" 'notmuch-show-toggle-citations-visible)
38     (define-key map "h" 'notmuch-show-toggle-headers-visible)
39     (define-key map "m" 'message-mail)
40     (define-key map "n" 'notmuch-show-next-message)
41     (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
42     (define-key map "p" 'notmuch-show-previous-message)
43     (define-key map (kbd "C-n") 'notmuch-show-next-line)
44     (define-key map (kbd "C-p") 'notmuch-show-previous-line)
45     (define-key map "q" 'kill-this-buffer)
46     (define-key map "r" 'notmuch-show-reply)
47     (define-key map "s" 'notmuch-show-toggle-signatures-visible)
48     (define-key map "v" 'notmuch-show-view-all-mime-parts)
49     (define-key map "w" 'notmuch-show-view-raw-message)
50     (define-key map "x" 'kill-this-buffer)
51     (define-key map "+" 'notmuch-show-add-tag)
52     (define-key map "-" 'notmuch-show-remove-tag)
53     (define-key map (kbd "DEL") 'notmuch-show-rewind)
54     (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
55     (define-key map "|" 'notmuch-show-pipe-message)
56     map)
57   "Keymap for \"notmuch show\" buffers.")
58 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
59
60 (defvar notmuch-show-signature-lines-max 12
61   "Maximum length of signature that will be hidden by default.")
62
63 (set 'notmuch-show-message-begin-regexp    "\fmessage{")
64 (set 'notmuch-show-message-end-regexp      "\fmessage}")
65 (set 'notmuch-show-header-begin-regexp     "\fheader{")
66 (set 'notmuch-show-header-end-regexp       "\fheader}")
67 (set 'notmuch-show-body-begin-regexp       "\fbody{")
68 (set 'notmuch-show-body-end-regexp         "\fbody}")
69 (set 'notmuch-show-attachment-begin-regexp "\fattachment{")
70 (set 'notmuch-show-attachment-end-regexp   "\fattachment}")
71 (set 'notmuch-show-part-begin-regexp       "\fpart{")
72 (set 'notmuch-show-part-end-regexp         "\fpart}")
73 (set 'notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
74
75 (set 'notmuch-show-id-regexp "\\(id:[^ ]*\\)")
76 (set 'notmuch-show-depth-regexp " depth:\\([0-9]*\\) ")
77 (set 'notmuch-show-filename-regexp "filename:\\(.*\\)$")
78 (set 'notmuch-show-tags-regexp "(\\([^)]*\\))$")
79
80 ; XXX: This should be a generic function in emacs somewhere, not here
81 (defun point-invisible-p ()
82   "Return whether the character at point is invisible.
83
84 Here visibility is determined by `buffer-invisibility-spec' and
85 the invisible property of any overlays for point. It doesn't have
86 anything to do with whether point is currently being displayed
87 within the current window."
88   (let ((prop (get-char-property (point) 'invisible)))
89     (if (eq buffer-invisibility-spec t)
90         prop
91       (or (memq prop buffer-invisibility-spec)
92           (assq prop buffer-invisibility-spec)))))
93
94 (defun notmuch-show-next-line ()
95   "Like builtin `next-line' but ensuring we end on a visible character.
96
97 By advancing forward until reaching a visible character.
98
99 Unlike builtin `next-line' this version accepts no arguments."
100   (interactive)
101   (set 'this-command 'next-line)
102   (call-interactively 'next-line)
103   (while (point-invisible-p)
104     (forward-char)))
105
106 (defun notmuch-show-previous-line ()
107   "Like builtin `previous-line' but ensuring we end on a visible character.
108
109 By advancing forward until reaching a visible character.
110
111 Unlike builtin `next-line' this version accepts no arguments."
112   (interactive)
113   (set 'this-command 'previous-line)
114   (call-interactively 'previous-line)
115   (while (point-invisible-p)
116     (forward-char)))
117
118 (defun notmuch-show-get-message-id ()
119   (save-excursion
120     (beginning-of-line)
121     (if (not (looking-at notmuch-show-message-begin-regexp))
122         (re-search-backward notmuch-show-message-begin-regexp))
123     (re-search-forward notmuch-show-id-regexp)
124     (buffer-substring (match-beginning 1) (match-end 1))))
125
126 (defun notmuch-show-get-filename ()
127   (save-excursion
128     (beginning-of-line)
129     (if (not (looking-at notmuch-show-message-begin-regexp))
130         (re-search-backward notmuch-show-message-begin-regexp))
131     (re-search-forward notmuch-show-filename-regexp)
132     (buffer-substring (match-beginning 1) (match-end 1))))
133
134 (defun notmuch-show-set-tags (tags)
135   (save-excursion
136     (beginning-of-line)
137     (if (not (looking-at notmuch-show-message-begin-regexp))
138         (re-search-backward notmuch-show-message-begin-regexp))
139     (re-search-forward notmuch-show-tags-regexp)
140     (let ((inhibit-read-only t)
141           (beg (match-beginning 1))
142           (end (match-end 1)))
143       (delete-region beg end)
144       (goto-char beg)
145       (insert (mapconcat 'identity tags " ")))))
146
147 (defun notmuch-show-get-tags ()
148   (save-excursion
149     (beginning-of-line)
150     (if (not (looking-at notmuch-show-message-begin-regexp))
151         (re-search-backward notmuch-show-message-begin-regexp))
152     (re-search-forward notmuch-show-tags-regexp)
153     (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
154
155 (defun notmuch-show-add-tag (&rest toadd)
156   "Add a tag to the current message."
157   (interactive "sTag to add: ")
158   (apply 'notmuch-call-notmuch-process
159          (append (cons "tag"
160                        (mapcar (lambda (s) (concat "+" s)) toadd))
161                  (cons (notmuch-show-get-message-id) nil)))
162   (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
163
164 (defun notmuch-show-remove-tag (&rest toremove)
165   "Remove a tag from the current message."
166   (interactive "sTag to remove: ")
167   (let ((tags (notmuch-show-get-tags)))
168     (if (intersection tags toremove :test 'string=)
169         (progn
170           (apply 'notmuch-call-notmuch-process
171                  (append (cons "tag"
172                                (mapcar (lambda (s) (concat "-" s)) toremove))
173                          (cons (notmuch-show-get-message-id) nil)))
174           (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
175
176 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
177   (save-excursion
178     (goto-char (point-min))
179     (while (not (eobp))
180       (if markread
181           (notmuch-show-remove-tag "unread" "inbox")
182         (notmuch-show-remove-tag "inbox"))
183       (if (not (eobp))
184           (forward-char))
185       (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
186           (goto-char (point-max)))))
187   (let ((parent-buffer notmuch-show-parent-buffer))
188     (kill-this-buffer)
189     (if parent-buffer
190         (progn
191           (switch-to-buffer parent-buffer)
192           (forward-line)
193           (notmuch-search-show-thread)))))
194
195 (defun notmuch-show-mark-read-then-archive-thread ()
196   "Remove \"unread\" tag from each message, then archive and show next thread.
197
198 Archive each message currrently shown by removing the \"unread\"
199 and \"inbox\" tag from each. Then kill this buffer and show the
200 next thread from the search from which this thread was originally
201 shown.
202
203 Note: This command is safe from any race condition of new messages
204 being delivered to the same thread. It does not archive the
205 entire thread, but only the messages shown in the current
206 buffer."
207   (interactive)
208   (notmuch-show-archive-thread-maybe-mark-read t))
209
210 (defun notmuch-show-archive-thread ()
211   "Archive each message in thread, and show next thread from search.
212
213 Archive each message currrently shown by removing the \"inbox\"
214 tag from each. Then kill this buffer and show the next thread
215 from the search from which this thread was originally shown.
216
217 Note: This command is safe from any race condition of new messages
218 being delivered to the same thread. It does not archive the
219 entire thread, but only the messages shown in the current
220 buffer."
221   (interactive)
222   (notmuch-show-archive-thread-maybe-mark-read nil))
223
224 (defun notmuch-show-view-raw-message ()
225   "View the raw email of the current message."
226   (interactive)
227   (view-file (notmuch-show-get-filename)))
228
229 (defun notmuch-show-view-all-mime-parts ()
230   "Use external viewers (according to mailcap) to view all MIME-encoded parts."
231   (interactive)
232   (save-excursion
233     (let ((filename (notmuch-show-get-filename)))
234       (switch-to-buffer (generate-new-buffer (concat "*notmuch-mime-"
235                                                      filename
236                                                      "*")))
237       (insert-file-contents filename nil nil nil t)
238       (mm-display-parts (mm-dissect-buffer))
239       (kill-this-buffer))))
240
241 (defun notmuch-show-reply ()
242   "Begin composing a reply to the current message in a new buffer."
243   (interactive)
244   (let ((message-id (notmuch-show-get-message-id)))
245     (switch-to-buffer (generate-new-buffer "notmuch-draft"))
246     (call-process "notmuch" nil t nil "reply" message-id)
247     (goto-char (point-min))
248     (if (re-search-forward "^$" nil t)
249         (progn
250           (insert "--text follows this line--")
251           (forward-line)))
252     (message-mode)))
253
254 (defun notmuch-show-pipe-message (command)
255   "Pipe the contents of the current message to the given command.
256
257 The given command will be executed with the raw contents of the
258 current email message as stdin. Anything printed by the command
259 to stdout or stderr will appear in the *Messages* buffer."
260   (interactive "sPipe message to command: ")
261   (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*" (split-string (concat command " < " (notmuch-show-get-filename)))))
262
263 (defun notmuch-show-move-to-current-message-summary-line ()
264   "Move to the beginning of the one-line summary of the current message.
265
266 This gives us a stable place to move to and work from since the
267 summary line is always visible. This is important since moving to
268 an invisible location is unreliable, (the main command loop moves
269 point either forward or backward to the next visible character
270 when a command ends with point on an invisible character).
271
272 Emits an error if point is not within a valid message, (that is
273 not pattern of `notmuch-show-message-begin-regexp' could be found
274 by searching backward)."
275   (beginning-of-line)
276   (if (not (looking-at notmuch-show-message-begin-regexp))
277       (if (re-search-backward notmuch-show-message-begin-regexp nil t)
278           (forward-line 2)
279         (error "Not within a valid message."))
280     (forward-line 2)))
281
282 (defun notmuch-show-last-message-p ()
283   "Predicate testing whether point is within the last message."
284   (save-window-excursion
285     (save-excursion
286       (notmuch-show-move-to-current-message-summary-line)
287       (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
288
289 (defun notmuch-show-message-unread-p ()
290   "Preficate testing whether current message is unread."
291   (member "unread" (notmuch-show-get-tags)))
292
293 (defun notmuch-show-next-message ()
294   "Advance to the beginning of the next message in the buffer.
295
296 Moves to the last visible character of the current message if
297 already on the last message in the buffer."
298   (interactive)
299   (notmuch-show-move-to-current-message-summary-line)
300   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
301       (notmuch-show-move-to-current-message-summary-line)
302     (goto-char (- (point-max) 1))
303     (while (point-invisible-p)
304       (backward-char)))
305   (recenter 0))
306
307 (defun notmuch-show-find-next-message ()
308   "Returns the position of the next message in the buffer.
309
310 Or the position of the last visible character of the current
311 message if already within the last message in the buffer."
312   ; save-excursion doesn't save our window position
313   ; save-window-excursion doesn't save point
314   ; Looks like we have to use both.
315   (save-excursion
316     (save-window-excursion
317       (notmuch-show-next-message)
318       (point))))
319
320 (defun notmuch-show-next-unread-message ()
321   "Advance to the beginning of the next unread message in the buffer.
322
323 Moves to the last visible character of the current message if
324 there are no more unread messages past the current point."
325   (notmuch-show-next-message)
326   (while (and (not (notmuch-show-last-message-p))
327               (not (notmuch-show-message-unread-p)))
328     (notmuch-show-next-message))
329   (if (not (notmuch-show-message-unread-p))
330       (notmuch-show-next-message)))
331
332 (defun notmuch-show-next-open-message ()
333   "Advance to the the next message which is not hidden.
334
335 If read messages are currently hidden, advance to the next unread
336 message. Otherwise, advance to the next message."
337   (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
338           (assq 'notmuch-show-body-read buffer-invisibility-spec))
339       (notmuch-show-next-unread-message)
340     (notmuch-show-next-message)))
341
342 (defun notmuch-show-previous-message ()
343   "Backup to the beginning of the previous message in the buffer.
344
345 If within a message rather than at the beginning of it, then
346 simply move to the beginning of the current message."
347   (interactive)
348   (let ((start (point)))
349     (notmuch-show-move-to-current-message-summary-line)
350     (if (not (< (point) start))
351         ; Go backward twice to skip the current message's marker
352         (progn
353           (re-search-backward notmuch-show-message-begin-regexp nil t)
354           (re-search-backward notmuch-show-message-begin-regexp nil t)
355           (notmuch-show-move-to-current-message-summary-line)
356           ))
357     (recenter 0)))
358
359 (defun notmuch-show-find-previous-message ()
360   "Returns the position of the previous message in the buffer.
361
362 Or the position of the beginning of the current message if point
363 is originally within the message rather than at the beginning of
364 it."
365   ; save-excursion doesn't save our window position
366   ; save-window-excursion doesn't save point
367   ; Looks like we have to use both.
368   (save-excursion
369     (save-window-excursion
370       (notmuch-show-previous-message)
371       (point))))
372
373 (defun notmuch-show-mark-read-then-next-open-message ()
374   "Remove unread tag from current message, then advance to next unread message."
375   (interactive)
376   (notmuch-show-remove-tag "unread")
377   (notmuch-show-next-open-message))
378
379 (defun notmuch-show-rewind ()
380   "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
381
382 Specifically, if the beginning of the previous email is fewer
383 than `window-height' lines from the current point, move to it
384 just like `notmuch-show-previous-message'.
385
386 Otherwise, just scroll down a screenful of the current message.
387
388 This command does not modify any message tags, (it does not undo
389 any effects from previous calls to
390 `notmuch-show-advance-marking-read-and-archiving'."
391   (interactive)
392   (let ((previous (notmuch-show-find-previous-message)))
393     (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
394         (progn
395           (condition-case nil
396               (scroll-down nil)
397             ((beginning-of-buffer) nil))
398           (goto-char (window-start)))
399       (notmuch-show-previous-message))))
400
401 (defun notmuch-show-advance-marking-read-and-archiving ()
402   "Advance through buffer, marking read and archiving.
403
404 This command is intended to be one of the simplest ways to
405 process a thread of email. It does the following:
406
407 If the current message in the thread is not yet fully visible,
408 scroll by a near screenful to read more of the message.
409
410 Otherwise, (the end of the current message is already within the
411 current window), remove the \"unread\" tag (if present) from the
412 current message and advance to the next open message.
413
414 Finally, if there is no further message to advance to, and this
415 last message is already read, then archive the entire current
416 thread, (remove the \"inbox\" tag from each message). Also kill
417 this buffer, and display the next thread from the search from
418 which this thread was originally shown."
419   (interactive)
420   (let ((next (notmuch-show-find-next-message))
421         (unread (notmuch-show-message-unread-p)))
422     (if (> next (window-end))
423         (scroll-up nil)
424       (let ((last (notmuch-show-last-message-p)))
425         (notmuch-show-mark-read-then-next-open-message)
426         (if last
427             (notmuch-show-archive-thread))))))
428
429 (defun notmuch-show-markup-citations-region (beg end depth)
430   (goto-char beg)
431   (beginning-of-line)
432   (while (< (point) end)
433     (let ((beg-sub (point-marker))
434           (indent (make-string depth ? ))
435           (citation "[[:space:]]*>"))
436       (if (looking-at citation)
437           (progn
438             (while (looking-at citation)
439               (forward-line))
440             (let ((overlay (make-overlay beg-sub (point))))
441               (overlay-put overlay 'invisible 'notmuch-show-citation)
442               (overlay-put overlay 'before-string
443                            (concat indent
444                                    "[" (number-to-string (count-lines beg-sub (point)))
445                                    "-line citation. Press 'c' to show.]\n")))))
446       (if (looking-at "[[:space:]]*-- ?$")
447           (let ((sig-lines (- (count-lines beg-sub end) 1)))
448             (if (<= sig-lines notmuch-show-signature-lines-max)
449                 (progn
450                   (overlay-put (make-overlay beg-sub end)
451                                'invisible 'notmuch-show-signature)
452                   (overlay-put (make-overlay beg (- beg-sub 1))
453                                'after-string
454                                (concat "\n" indent
455                                        "[" (number-to-string sig-lines)
456                                        "-line signature. Press 's' to show.]"))
457                   (goto-char end)))))
458       (forward-line))))
459
460 (defun notmuch-show-markup-part (beg end depth)
461   (if (re-search-forward notmuch-show-part-begin-regexp nil t)
462       (progn
463         (forward-line)
464         (let ((beg (point-marker)))
465           (re-search-forward notmuch-show-part-end-regexp)
466           (let ((end (copy-marker (match-beginning 0))))
467             (indent-rigidly beg end depth)
468             (notmuch-show-markup-citations-region beg end depth)
469             ; Advance to the next part (if any) (so the outer loop can
470             ; determine whether we've left the current message.
471             (re-search-forward notmuch-show-part-begin-regexp nil t))))
472     (goto-char end)))
473
474 (defun notmuch-show-markup-parts-region (beg end depth)
475   (save-excursion
476     (goto-char beg)
477     (while (< (point) end)
478       (notmuch-show-markup-part beg end depth))))
479
480 (defun notmuch-show-markup-body (depth)
481   (re-search-forward notmuch-show-body-begin-regexp)
482   (forward-line)
483   (let ((beg (point-marker)))
484     (re-search-forward notmuch-show-body-end-regexp)
485     (let ((end (copy-marker (match-beginning 0))))
486       (notmuch-show-markup-parts-region beg end depth)
487       (if (not (notmuch-show-message-unread-p))
488           (overlay-put (make-overlay beg end)
489                        'invisible 'notmuch-show-body-read))
490       (set-marker beg nil)
491       (set-marker end nil)
492       )))
493
494 (defun notmuch-show-markup-header (depth)
495   (re-search-forward notmuch-show-header-begin-regexp)
496   (forward-line)
497   (let ((beg (point-marker)))
498     (end-of-line)
499     ; Inverse video for subject
500     (overlay-put (make-overlay beg (point)) 'face '((cons :inverse-video t)))
501     (forward-line 2)
502     (let ((beg-hidden (point-marker)))
503       (re-search-forward notmuch-show-header-end-regexp)
504       (beginning-of-line)
505       (let ((end (point-marker)))
506         (indent-rigidly beg end depth)
507         (overlay-put (make-overlay beg-hidden end)
508                      'invisible 'notmuch-show-header)
509         (set-marker beg nil)
510         (set-marker beg-hidden nil)
511         (set-marker end nil)
512         ))))
513
514 (defun notmuch-show-markup-message ()
515   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
516       (progn
517         (re-search-forward notmuch-show-depth-regexp)
518         (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1)))))
519           (notmuch-show-markup-header depth)
520           (notmuch-show-markup-body depth)))
521     (goto-char (point-max))))
522
523 (defun notmuch-show-hide-markers ()
524   (save-excursion
525     (goto-char (point-min))
526     (while (not (eobp))
527       (if (re-search-forward notmuch-show-marker-regexp nil t)
528           (progn
529             (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
530                          'invisible 'notmuch-show-marker))
531         (goto-char (point-max))))))
532
533 (defun notmuch-show-markup-messages ()
534   (save-excursion
535     (goto-char (point-min))
536     (while (not (eobp))
537       (notmuch-show-markup-message)))
538   (notmuch-show-hide-markers))
539
540 (defun notmuch-show-toggle-citations-visible ()
541   "Toggle visibility of citations"
542   (interactive)
543   (if notmuch-show-citations-visible
544       (add-to-invisibility-spec 'notmuch-show-citation)
545     (remove-from-invisibility-spec 'notmuch-show-citation))
546   (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
547   ; Need to force the redisplay for some reason
548   (force-window-update)
549   (redisplay t))
550
551 (defun notmuch-show-toggle-signatures-visible ()
552   "Toggle visibility of signatures"
553   (interactive)
554   (if notmuch-show-signatures-visible
555       (add-to-invisibility-spec 'notmuch-show-signature)
556     (remove-from-invisibility-spec 'notmuch-show-signature))
557   (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
558   ; Need to force the redisplay for some reason
559   (force-window-update)
560   (redisplay t))
561
562 (defun notmuch-show-toggle-headers-visible ()
563   "Toggle visibility of header fields"
564   (interactive)
565   (if notmuch-show-headers-visible
566       (add-to-invisibility-spec 'notmuch-show-header)
567     (remove-from-invisibility-spec 'notmuch-show-header))
568   (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
569   ; Need to force the redisplay for some reason
570   (force-window-update)
571   (redisplay t))
572
573 (defun notmuch-show-toggle-body-read-visible ()
574   "Toggle visibility of message bodies of read messages"
575   (interactive)
576   (if notmuch-show-body-read-visible
577       (add-to-invisibility-spec 'notmuch-show-body-read)
578     (remove-from-invisibility-spec 'notmuch-show-body-read))
579   (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
580   ; Need to force the redisplay for some reason
581   (force-window-update)
582   (redisplay t))
583
584 ;;;###autoload
585 (defun notmuch-show-mode ()
586   "Major mode for viewing a thread with notmuch.
587
588 This buffer contains the results of the \"notmuch show\" command
589 for displaying a single thread of email from your email archives.
590
591 By default, various components of email messages, (citations,
592 signatures, already-read messages), are invisible to help you
593 focus on the most important things, (new text from unread
594 messages). See the various commands below for toggling the
595 visibility of hidden components.
596
597 The `notmuch-show-next-message' and
598 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
599 default), allow you to navigate to the next and previous
600 messages. Each time you navigate away from a message with
601 `notmuch-show-next-message' the current message will have its
602 \"unread\" tag removed.
603
604 You can add or remove tags from the current message with '+' and
605 '-'.  You can also archive all messages in the current
606 view, (remove the \"inbox\" tag from each), with
607 `notmuch-show-archive-thread' (bound to 'a' by default).
608
609 \\{notmuch-show-mode-map}"
610   (interactive)
611   (kill-all-local-variables)
612   (set (make-local-variable 'notmuch-show-headers-visible) t)
613   (notmuch-show-toggle-headers-visible)
614   (set (make-local-variable 'notmuch-show-body-read-visible) t)
615   (notmuch-show-toggle-body-read-visible)
616   (set (make-local-variable 'notmuch-show-citations-visible) t)
617   (notmuch-show-toggle-citations-visible)
618   (set (make-local-variable 'notmuch-show-signatures-visible) t)
619   (notmuch-show-toggle-signatures-visible)
620   (add-to-invisibility-spec 'notmuch-show-marker)
621   (use-local-map notmuch-show-mode-map)
622   (setq major-mode 'notmuch-show-mode
623         mode-name "notmuch-show")
624   (setq buffer-read-only t))
625
626 (defun notmuch-show (thread-id &optional parent-buffer)
627   "Run \"notmuch show\" with the given thread ID and display results.
628
629 The optional PARENT-BUFFER is the notmuch-search buffer from
630 which this notmuch-show command was executed, (so that the next
631 thread from that buffer can be show when done with this one)."
632   (interactive "sNotmuch show: ")
633   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
634     (switch-to-buffer buffer)
635     (notmuch-show-mode)
636     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
637     (let ((proc (get-buffer-process (current-buffer)))
638           (inhibit-read-only t))
639       (if proc
640           (error "notmuch search process already running for query `%s'" query)
641         )
642       (erase-buffer)
643       (goto-char (point-min))
644       (save-excursion
645         (call-process "notmuch" nil t nil "show" thread-id)
646         (notmuch-show-markup-messages)
647         )
648       ; Move straight to the first unread message
649       (if (not (notmuch-show-message-unread-p))
650           (progn
651             (notmuch-show-next-unread-message)
652             ; But if there are no unread messages, go back to the
653             ; beginning of the buffer, and open up the bodies of all
654             ; read message.
655             (if (not (notmuch-show-message-unread-p))
656                 (progn
657                   (goto-char (point-min))
658                   (notmuch-show-toggle-body-read-visible)))))
659       )))
660
661 (defvar notmuch-search-mode-map
662   (let ((map (make-sparse-keymap)))
663     (define-key map "a" 'notmuch-search-archive-thread)
664     (define-key map "b" 'notmuch-search-scroll-down)
665     (define-key map "f" 'notmuch-search-filter)
666     (define-key map "m" 'message-mail)
667     (define-key map "n" 'next-line)
668     (define-key map "o" 'notmuch-search-toggle-order)
669     (define-key map "p" 'previous-line)
670     (define-key map "q" 'kill-this-buffer)
671     (define-key map "s" 'notmuch-search)
672     (define-key map "t" 'notmuch-search-filter-by-tag)
673     (define-key map "x" 'kill-this-buffer)
674     (define-key map (kbd "RET") 'notmuch-search-show-thread)
675     (define-key map "+" 'notmuch-search-add-tag)
676     (define-key map "-" 'notmuch-search-remove-tag)
677     (define-key map "<" 'beginning-of-buffer)
678     (define-key map ">" 'notmuch-search-goto-last-thread)
679     (define-key map "=" 'notmuch-search-refresh-view)
680     (define-key map "\M->" 'notmuch-search-goto-last-thread)
681     (define-key map " " 'notmuch-search-scroll-up)
682     (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
683     map)
684   "Keymap for \"notmuch search\" buffers.")
685 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
686
687 (defun notmuch-search-scroll-up ()
688   "Scroll up, moving point to last message in thread if at end."
689   (interactive)
690   (condition-case nil
691       (scroll-up nil)
692     ((end-of-buffer) (notmuch-search-goto-last-thread))))
693
694 (defun notmuch-search-scroll-down ()
695   "Scroll down, moving point to first message in thread if at beginning."
696   (interactive)
697   ; I don't know why scroll-down doesn't signal beginning-of-buffer
698   ; the way that scroll-up signals end-of-buffer, but c'est la vie.
699   ;
700   ; So instead of trapping a signal we instead check whether the
701   ; window begins on the first line of the buffer and if so, move
702   ; directly to that position. (We have to count lines since the
703   ; window-start position is not the same as point-min due to the
704   ; invisible thread-ID characters on the first line.
705   (if (equal (count-lines (point-min) (window-start)) 1)
706       (goto-char (window-start))
707     (scroll-down nil)))
708
709 (defun notmuch-search-goto-last-thread (&optional arg)
710   "Move point to the last thread in the buffer."
711   (interactive "^P")
712   (end-of-buffer arg)
713   (forward-line -1))
714
715 ;;;###autoload
716 (defun notmuch-search-mode ()
717   "Major mode for searching mail with notmuch.
718
719 This buffer contains the results of a \"notmuch search\" of your
720 email archives. Each line in the buffer represents a single
721 thread giving a relative date for the thread and a subject.
722
723 Pressing RET on any line displays that thread. The '+' and '-'
724 keys can be used to add or remove tags from a thread. The 'a' key
725 is a convenience key for archiving a thread (removing the
726 \"inbox\" tag).
727
728 Other useful commands are `notmuch-search-filter' for filtering
729 the current search based on an additional query string,
730 `notmuch-search-filter-by-tag' for filtering to include only
731 messages with a given tag, and `notmuch-search' to execute a new,
732 global search.
733
734 \\{notmuch-search-mode-map}"
735   (interactive)
736   (kill-all-local-variables)
737   (make-local-variable 'notmuch-search-query-string)
738   (make-local-variable 'notmuch-search-oldest-first)
739   (set (make-local-variable 'scroll-preserve-screen-position) t)
740   (add-to-invisibility-spec 'notmuch-search)
741   (use-local-map notmuch-search-mode-map)
742   (setq major-mode 'notmuch-search-mode
743         mode-name "notmuch-search")
744   (setq buffer-read-only t))
745
746 (defun notmuch-search-find-thread-id ()
747   (save-excursion
748     (beginning-of-line)
749     (let ((beg (point)))
750       (re-search-forward "thread:[a-fA-F0-9]*" nil t)
751       (filter-buffer-substring beg (point)))))
752
753 (defun notmuch-search-markup-this-thread-id ()
754   (beginning-of-line)
755   (let ((beg (point)))
756     (if (re-search-forward "thread:[a-fA-F0-9]*" nil t)
757         (progn
758           (forward-char)
759           (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))))
760
761 (defun notmuch-search-markup-thread-ids ()
762   (save-excursion
763     (goto-char (point-min))
764     (while (not (eobp))
765       (notmuch-search-markup-this-thread-id)
766       (next-line))))
767
768 (defun notmuch-search-show-thread ()
769   (interactive)
770   (let ((thread-id (notmuch-search-find-thread-id)))
771     (if (> (length thread-id) 0)
772         (notmuch-show thread-id (current-buffer))
773       (error "End of search results"))))
774
775 (defun notmuch-call-notmuch-process (&rest args)
776   "Synchronously invoke \"notmuch\" with the given list of arguments.
777
778 Output from the process will be presented to the user as an error
779 and will also appear in a buffer named \"*Notmuch errors*\"."
780   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
781     (with-current-buffer error-buffer
782         (erase-buffer))
783     (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
784         (point)
785       (progn
786         (with-current-buffer error-buffer
787           (let ((beg (point-min))
788                 (end (- (point-max) 1)))
789             (error (buffer-substring beg end))
790             ))))))
791
792 (defun notmuch-search-set-tags (tags)
793   (save-excursion
794     (end-of-line)
795     (re-search-backward "(")
796     (forward-char)
797     (let ((beg (point))
798           (inhibit-read-only t))
799       (re-search-forward ")")
800       (backward-char)
801       (let ((end (point)))
802         (delete-region beg end)
803         (insert (mapconcat  'identity tags " "))))))
804
805 (defun notmuch-search-get-tags ()
806   (save-excursion
807     (end-of-line)
808     (re-search-backward "(")
809     (let ((beg (+ (point) 1)))
810       (re-search-forward ")")
811       (let ((end (- (point) 1)))
812         (split-string (buffer-substring beg end))))))
813
814 (defun notmuch-search-add-tag (tag)
815   (interactive "sTag to add: ")
816   (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
817   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
818
819 (defun notmuch-search-remove-tag (tag)
820   (interactive "sTag to remove: ")
821   (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
822   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
823
824 (defun notmuch-search-archive-thread ()
825   "Archive the current thread (remove its \"inbox\" tag).
826
827 This function advances point to the next line when finished."
828   (interactive)
829   (notmuch-search-remove-tag "inbox")
830   (forward-line))
831
832 (defun notmuch-search (query &optional oldest-first)
833   "Run \"notmuch search\" with the given query string and display results."
834   (interactive "sNotmuch search: ")
835   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
836     (switch-to-buffer buffer)
837     (notmuch-search-mode)
838     (set 'notmuch-search-query-string query)
839     (set 'notmuch-search-oldest-first oldest-first)
840     (let ((proc (get-buffer-process (current-buffer)))
841           (inhibit-read-only t))
842       (if proc
843           (error "notmuch search process already running for query `%s'" query)
844         )
845       (erase-buffer)
846       (goto-char (point-min))
847       (save-excursion
848         (if oldest-first
849             (call-process "notmuch" nil t nil "search" query)
850           (call-process "notmuch" nil t nil "search" "--reverse" query))
851         (notmuch-search-markup-thread-ids)
852         ))))
853
854 (defun notmuch-search-refresh-view ()
855   "Refresh the current view.
856
857 Kills the current buffer and runs a new search with the same
858 query string as the current search. If the current thread is in
859 the new search results, then point will be placed on the same
860 thread. Otherwise, point will be moved to attempt to be in the
861 same relative position within the new buffer."
862   (interactive)
863   (let ((here (point))
864         (oldest-first notmuch-search-oldest-first)
865         (thread (notmuch-search-find-thread-id))
866         (query notmuch-search-query-string))
867     (kill-this-buffer)
868     (notmuch-search query oldest-first)
869     (goto-char (point-min))
870     (if (re-search-forward (concat "^" thread) nil t)
871         (beginning-of-line)
872       (goto-char here))))
873
874 (defun notmuch-search-toggle-order ()
875   "Toggle the current search order.
876
877 By default, the \"inbox\" view created by `notmuch' is displayed
878 in chronological order (oldest thread at the beginning of the
879 buffer), while any global searches created by `notmuch-search'
880 are displayed in reverse-chronological order (newest thread at
881 the beginning of the buffer).
882
883 This command toggles the sort order for the current search.
884
885 Note that any fitlered searches created by
886 `notmuch-search-filter' retain the search order of the parent
887 search."
888   (interactive)
889   (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
890   (notmuch-search-refresh-view))
891
892 (defun notmuch-search-filter (query)
893   "Filter the current search results based on an additional query string.
894
895 Runs a new search matching only messages that match both the
896 current search results AND the additional query string provided."
897   (interactive "sFilter search: ")
898   (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
899
900 (defun notmuch-search-filter-by-tag (tag)
901   "Filter the current search results based on a single tag.
902
903 Runs a new search matching only messages that match both the
904 current search results AND that are tagged with the given tag."
905   (interactive "sFilter by tag: ")
906   (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
907
908 (defun notmuch ()
909   "Run notmuch to display all mail with tag of 'inbox'"
910   (interactive)
911   (notmuch-search "tag:inbox" t))
912
913 (provide 'notmuch)