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