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