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