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