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