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