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