]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
notmuch.el: Move to first unread message on notmuch-show.
[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-mark-read-then-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 beginning of the current message if already within the last
166 message in 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 Does nothing if there are no more unread messages past the
179 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-previous-message ()
185   "Backup to the beginning of the previous message in the buffer.
186
187 If within a message rather than at the beginning of it, then
188 simply move to the beginning of the current message."
189   (interactive)
190   (let ((start (point)))
191     (notmuch-show-move-to-current-message-summary-line)
192     (if (not (< (point) start))
193         ; Go backward twice to skip the current message's marker
194         (progn
195           (re-search-backward notmuch-show-message-begin-regexp nil t)
196           (re-search-backward notmuch-show-message-begin-regexp nil t)
197           (notmuch-show-move-to-current-message-summary-line)
198           ))
199     (recenter 0)))
200
201 (defun notmuch-show-mark-read-then-next-message ()
202   "Remove unread tag from current message, then advance to next message."
203   (interactive)
204   (notmuch-show-remove-tag "unread")
205   (notmuch-show-next-message))
206
207 (defun notmuch-show-advance-marking-read-and-archiving ()
208   "Advance through buffer, marking read and archiving.
209
210 This command is intended to be one of the simplest ways to
211 process a thread of email. It does the following:
212
213 If the current message in the thread is not yet fully visible,
214 scroll by a near screenful to read more of the message.
215
216 Otherwise, (the end of the current message is already within the
217 current window), remove the \"unread\" tag from the current
218 message and advance to the next message.
219
220 Finally, if there is no further message to advance to, and this
221 last message is already read, then archive the entire current
222 thread, (remove the \"inbox\" tag from each message). Also kill
223 this buffer, and display the next thread from the search from
224 which this thread was originally shown."
225   (interactive)
226   (let ((next (notmuch-show-find-next-message))
227         (unread (member "unread" (notmuch-show-get-tags))))
228     (if (and (not unread)
229              (equal next (point)))
230         (notmuch-show-archive-thread)
231       (if (and (> next (window-end))
232                (< next (point-max)))
233           (scroll-up nil)
234         (notmuch-show-mark-read-then-next-message)))))
235
236 (defun notmuch-show-markup-citations-region (beg end)
237   (goto-char beg)
238   (beginning-of-line)
239   (while (< (point) end)
240     (let ((beg-sub (point)))
241       (if (looking-at ">")
242           (progn
243             (while (looking-at ">")
244               (next-line))
245             (let ((overlay (make-overlay beg-sub (point))))
246               (overlay-put overlay 'invisible 'notmuch-show-citation)
247               (overlay-put overlay 'before-string
248                            (concat "[" (number-to-string (count-lines beg-sub (point)))
249                                    " quoted lines.]")))))
250       (if (looking-at "--[ ]?$")
251           (let ((overlay (make-overlay beg-sub end)))
252             (overlay-put overlay 'invisible 'notmuch-show-signature)
253             (overlay-put overlay 'before-string
254                          (concat "[" (number-to-string (count-lines beg-sub (point)))
255                                  "-line signature.]"))
256             (goto-char end)))
257       (next-line))))
258
259 (defun notmuch-show-markup-body ()
260   (re-search-forward notmuch-show-body-begin-regexp)
261   (next-line 1)
262   (beginning-of-line)
263   (let ((beg (point)))
264     (re-search-forward notmuch-show-body-end-regexp)
265     (let ((end (match-beginning 0)))
266       (notmuch-show-markup-citations-region beg end)
267       (if (not (member "unread" (notmuch-show-get-tags)))
268           (overlay-put (make-overlay beg end)
269                        'invisible 'notmuch-show-body-read)))))
270
271 (defun notmuch-show-markup-header ()
272   (re-search-forward notmuch-show-header-begin-regexp)
273   (next-line 2)
274   (beginning-of-line)
275   (let ((beg (point)))
276     (re-search-forward notmuch-show-header-end-regexp)
277     (overlay-put (make-overlay beg (match-beginning 0))
278                  'invisible 'notmuch-show-header)))
279
280 (defun notmuch-show-markup-message ()
281   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
282       (progn
283         (notmuch-show-markup-header)
284         (notmuch-show-markup-body))
285     (goto-char (point-max))))
286
287 (defun notmuch-show-hide-markers ()
288   (save-excursion
289     (goto-char (point-min))
290     (while (not (eobp))
291       (if (re-search-forward notmuch-show-marker-regexp nil t)
292           (progn
293             (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
294                          'invisible 'notmuch-show-marker))
295         (goto-char (point-max))))))
296
297 (defun notmuch-show-markup-messages ()
298   (save-excursion
299     (goto-char (point-min))
300     (while (not (eobp))
301       (notmuch-show-markup-message)))
302   (notmuch-show-hide-markers))
303
304 (defun notmuch-show-toggle-citations-visible ()
305   "Toggle visibility of citations"
306   (interactive)
307   (if notmuch-show-citations-visible
308       (add-to-invisibility-spec 'notmuch-show-citation)
309     (remove-from-invisibility-spec 'notmuch-show-citation))
310   (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
311   ; Need to force the redisplay for some reason
312   (force-window-update)
313   (redisplay t))
314
315 (defun notmuch-show-toggle-signatures-visible ()
316   "Toggle visibility of signatures"
317   (interactive)
318   (if notmuch-show-signatures-visible
319       (add-to-invisibility-spec 'notmuch-show-signature)
320     (remove-from-invisibility-spec 'notmuch-show-signature))
321   (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
322   ; Need to force the redisplay for some reason
323   (force-window-update)
324   (redisplay t))
325
326 (defun notmuch-show-toggle-headers-visible ()
327   "Toggle visibility of header fields"
328   (interactive)
329   (if notmuch-show-headers-visible
330       (add-to-invisibility-spec 'notmuch-show-header)
331     (remove-from-invisibility-spec 'notmuch-show-header))
332   (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
333   ; Need to force the redisplay for some reason
334   (force-window-update)
335   (redisplay t))
336
337 (defun notmuch-show-toggle-body-read-visible ()
338   "Toggle visibility of message bodies of read messages"
339   (interactive)
340   (if notmuch-show-body-read-visible
341       (add-to-invisibility-spec 'notmuch-show-body-read)
342     (remove-from-invisibility-spec 'notmuch-show-body-read))
343   (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
344   ; Need to force the redisplay for some reason
345   (force-window-update)
346   (redisplay t))
347
348 ;;;###autoload
349 (defun notmuch-show-mode ()
350   "Major mode for viewing a thread with notmuch.
351
352 This buffer contains the results of the \"notmuch show\" command
353 for displaying a single thread of email from your email archives.
354
355 By default, various components of email messages, (citations,
356 signatures, already-read messages), are invisible to help you
357 focus on the most important things, (new text from unread
358 messages). See the various commands below for toggling the
359 visibility of hidden components.
360
361 The `notmuch-show-next-message' and
362 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
363 default), allow you to navigate to the next and previous
364 messages. Each time you navigate away from a message with
365 `notmuch-show-next-message' the current message will have its
366 \"unread\" tag removed.
367
368 You can add or remove tags from the current message with '+' and
369 '-'.  You can also archive all messages in the current
370 view, (remove the \"inbox\" tag from each), with
371 `notmuch-show-archive-thread' (bound to 'a' by default).
372
373 \\{notmuch-show-mode-map}"
374   (interactive)
375   (kill-all-local-variables)
376   (set (make-local-variable 'notmuch-show-headers-visible) t)
377   (notmuch-show-toggle-headers-visible)
378   (set (make-local-variable 'notmuch-show-body-read-visible) t)
379   (notmuch-show-toggle-body-read-visible)
380   (set (make-local-variable 'notmuch-show-citations-visible) t)
381   (notmuch-show-toggle-citations-visible)
382   (set (make-local-variable 'notmuch-show-signatures-visible) t)
383   (notmuch-show-toggle-signatures-visible)
384   (add-to-invisibility-spec 'notmuch-show-marker)
385   (use-local-map notmuch-show-mode-map)
386   (setq major-mode 'notmuch-show-mode
387         mode-name "notmuch-show")
388   (setq buffer-read-only t))
389
390 (defun notmuch-show (thread-id &optional parent-buffer)
391   "Run \"notmuch show\" with the given thread ID and display results.
392
393 The optional PARENT-BUFFER is the notmuch-search buffer from
394 which this notmuch-show command was executed, (so that the next
395 thread from that buffer can be show when done with this one)."
396   (interactive "sNotmuch show: ")
397   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
398     (switch-to-buffer buffer)
399     (notmuch-show-mode)
400     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
401     (let ((proc (get-buffer-process (current-buffer)))
402           (inhibit-read-only t))
403       (if proc
404           (error "notmuch search process already running for query `%s'" query)
405         )
406       (erase-buffer)
407       (goto-char (point-min))
408       (save-excursion
409         (call-process "notmuch" nil t nil "show" thread-id)
410         (notmuch-show-markup-messages)
411         )
412       (notmuch-show-next-unread-message)
413       )))
414
415 (defvar notmuch-search-mode-map
416   (let ((map (make-sparse-keymap)))
417     (define-key map "a" 'notmuch-search-archive-thread)
418     (define-key map "b" 'scroll-down)
419     (define-key map "f" 'notmuch-search-filter)
420     (define-key map "n" 'next-line)
421     (define-key map "p" 'previous-line)
422     (define-key map "q" 'kill-this-buffer)
423     (define-key map "s" 'notmuch-search)
424     (define-key map "t" 'notmuch-search-filter-by-tag)
425     (define-key map "x" 'kill-this-buffer)
426     (define-key map (kbd "RET") 'notmuch-search-show-thread)
427     (define-key map "+" 'notmuch-search-add-tag)
428     (define-key map "-" 'notmuch-search-remove-tag)
429     (define-key map "<" 'beginning-of-buffer)
430     (define-key map ">" 'notmuch-search-goto-last-thread)
431     (define-key map "=" 'notmuch-search-refresh-view)
432     (define-key map "\M->" 'notmuch-search-goto-last-thread)
433     (define-key map " " 'scroll-up)
434     (define-key map (kbd "<DEL>") 'scroll-down)
435     map)
436   "Keymap for \"notmuch search\" buffers.")
437 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
438
439 (defun notmuch-search-goto-last-thread (&optional arg)
440   "Move point to the last thread in the buffer."
441   (interactive "^P")
442   (end-of-buffer arg)
443   (forward-line -1))
444
445 ;;;###autoload
446 (defun notmuch-search-mode ()
447   "Major mode for searching mail with notmuch.
448
449 This buffer contains the results of a \"notmuch search\" of your
450 email archives. Each line in the buffer represents a single
451 thread giving a relative date for the thread and a subject.
452
453 Pressing RET on any line displays that thread. The '+' and '-'
454 keys can be used to add or remove tags from a thread. The 'a' key
455 is a convenience key for archiving a thread (removing the
456 \"inbox\" tag).
457
458 Other useful commands are `notmuch-search-filter' for filtering
459 the current search based on an additional query string,
460 `notmuch-search-filter-by-tag' for filtering to include only
461 messages with a given tag, and `notmuch-search' to execute a new,
462 global search.
463
464 \\{notmuch-search-mode-map}"
465   (interactive)
466   (kill-all-local-variables)
467   (make-local-variable 'notmuch-search-query-string)
468   (set (make-local-variable 'scroll-preserve-screen-position) t)
469   (add-to-invisibility-spec 'notmuch-search)
470   (use-local-map notmuch-search-mode-map)
471   (setq major-mode 'notmuch-search-mode
472         mode-name "notmuch-search")
473   (setq buffer-read-only t))
474
475 (defun notmuch-search-find-thread-id ()
476   (save-excursion
477     (beginning-of-line)
478     (let ((beg (point)))
479       (re-search-forward "[a-fA-F0-9]*")
480       (filter-buffer-substring beg (point)))))
481
482 (defun notmuch-search-markup-this-thread-id ()
483   (beginning-of-line)
484   (let ((beg (point)))
485     (re-search-forward "[a-fA-F0-9]*")
486     (forward-char)
487     (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
488
489 (defun notmuch-search-markup-thread-ids ()
490   (save-excursion
491     (goto-char (point-min))
492     (while (not (eobp))
493       (notmuch-search-markup-this-thread-id)
494       (next-line))))
495
496 (defun notmuch-search-hide-thread-ids ()
497   (interactive)
498   (add-to-invisibility-spec 'notmuch-search)
499   (force-window-update)
500   (redisplay t))
501
502 (defun notmuch-search-show-thread-ids ()
503   (interactive)
504   (remove-from-invisibility-spec 'notmuch-search)
505   (force-window-update)
506   (redisplay t))
507
508 (defun notmuch-search-show-thread ()
509   (interactive)
510   (let ((thread-id (notmuch-search-find-thread-id)))
511     (forward-line)
512     (if (> (length thread-id) 0)
513         (notmuch-show thread-id (current-buffer))
514       (error "End of search results"))))
515
516 (defun notmuch-call-notmuch-process (&rest args)
517   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
518     (with-current-buffer error-buffer
519         (erase-buffer))
520     (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
521         (point)
522       (progn
523         (with-current-buffer error-buffer
524           (let ((beg (point-min))
525                 (end (- (point-max) 1)))
526             (error (buffer-substring beg end))
527             ))))))
528
529 (defun notmuch-search-set-tags (tags)
530   (save-excursion
531     (end-of-line)
532     (re-search-backward "(")
533     (forward-char)
534     (let ((beg (point))
535           (inhibit-read-only t))
536       (re-search-forward ")")
537       (backward-char)
538       (let ((end (point)))
539         (delete-region beg end)
540         (insert (mapconcat  'identity tags " "))))))
541
542 (defun notmuch-search-get-tags ()
543   (save-excursion
544     (end-of-line)
545     (re-search-backward "(")
546     (let ((beg (+ (point) 1)))
547       (re-search-forward ")")
548       (let ((end (- (point) 1)))
549         (split-string (buffer-substring beg end))))))
550
551 (defun notmuch-search-add-tag (tag)
552   (interactive "sTag to add: ")
553   (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
554   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
555
556 (defun notmuch-search-remove-tag (tag)
557   (interactive "sTag to remove: ")
558   (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
559   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
560
561 (defun notmuch-search-archive-thread ()
562   "Archive the current thread (remove its \"inbox\" tag).
563
564 This function advances point to the next line when finished."
565   (interactive)
566   (notmuch-search-remove-tag "inbox")
567   (forward-line))
568
569 (defun notmuch-search (query)
570   "Run \"notmuch search\" with the given query string and display results."
571   (interactive "sNotmuch search: ")
572   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
573     (switch-to-buffer buffer)
574     (notmuch-search-mode)
575     (set 'notmuch-search-query-string query)
576     (let ((proc (get-buffer-process (current-buffer)))
577           (inhibit-read-only t))
578       (if proc
579           (error "notmuch search process already running for query `%s'" query)
580         )
581       (erase-buffer)
582       (goto-char (point-min))
583       (save-excursion
584         (call-process "notmuch" nil t nil "search" query)
585         (notmuch-search-markup-thread-ids)
586         ))))
587
588 (defun notmuch-search-refresh-view ()
589   "Refresh the current view.
590
591 Kills the current buffer and runs a new search with the same
592 query string as the current search. If the current thread is in
593 the new search results, then point will be placed on the same
594 thread. Otherwise, point will be moved to attempt to be in the
595 same relative position within the new buffer."
596   (interactive)
597   (let ((here (point))
598         (thread (notmuch-search-find-thread-id))
599         (query notmuch-search-query-string))
600     (kill-this-buffer)
601     (notmuch-search query)
602     (goto-char (point-min))
603     (if (re-search-forward (concat "^" thread) nil t)
604         (beginning-of-line)
605       (goto-char here))))
606
607 (defun notmuch-search-filter (query)
608   "Filter the current search results based on an additional query string.
609
610 Runs a new search matching only messages that match both the
611 current search results AND the additional query string provided."
612   (interactive "sFilter search: ")
613   (notmuch-search (concat notmuch-search-query-string " and " query)))
614
615 (defun notmuch-search-filter-by-tag (tag)
616   "Filter the current search results based on a single tag.
617
618 Runs a new search matching only messages that match both the
619 current search results AND that are tagged with the given tag."
620   (interactive "sFilter by tag: ")
621   (notmuch-search (concat notmuch-search-query-string " and tag:" tag)))
622
623 (defun notmuch ()
624   "Run notmuch to display all mail with tag of 'inbox'"
625   (interactive)
626   (notmuch-search "tag:inbox"))
627
628 (provide 'notmuch)