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