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