1 ; notmuch.el --- run notmuch within emacs
3 ; Copyright © Carl Worth
5 ; This file is part of Notmuch.
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.
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.
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/>.
20 ; Authors: Carl Worth <cworth@cworth.org>
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.
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 (kbd "DEL") 'notmuch-show-rewind)
46 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
48 "Keymap for \"notmuch show\" buffers.")
49 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
51 (defvar notmuch-show-signature-lines-max 6
52 "Maximum length of signature that will be hidden by default.")
54 (set 'notmuch-show-message-begin-regexp "
\fmessage{")
55 (set 'notmuch-show-message-end-regexp "
\fmessage}")
56 (set 'notmuch-show-header-begin-regexp "
\fheader{")
57 (set 'notmuch-show-header-end-regexp "
\fheader}")
58 (set 'notmuch-show-body-begin-regexp "
\fbody{")
59 (set 'notmuch-show-body-end-regexp "
\fbody}")
60 (set 'notmuch-show-attachment-begin-regexp "
\fattachment{")
61 (set 'notmuch-show-attachment-end-regexp "
\fattachment}")
62 (set 'notmuch-show-part-begin-regexp "
\fpart{")
63 (set 'notmuch-show-part-end-regexp "
\fpart}")
64 (set 'notmuch-show-marker-regexp "
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
66 (set 'notmuch-show-id-regexp "ID: \\([^ ]*\\)")
67 (set 'notmuch-show-filename-regexp "Filename: \\(.*\\)$")
68 (set 'notmuch-show-tags-regexp "(\\([^)]*\\))$")
70 ; XXX: This should be a generic function in emacs somewhere, not here
71 (defun point-invisible-p ()
72 "Return whether the character at point is invisible.
74 Here visibility is determined by `buffer-invisibility-spec' and
75 the invisible property of any overlays for point. It doesn't have
76 anything to do with whether point is currently being displayed
77 within the current window."
78 (let ((prop (get-char-property (point) 'invisible)))
79 (if (eq buffer-invisibility-spec t)
81 (or (memq prop buffer-invisibility-spec)
82 (assq prop buffer-invisibility-spec)))))
84 (defun notmuch-show-next-line ()
85 "Like builtin `next-line' but ensuring we end on a visible character.
87 By advancing forward until reaching a visible character.
89 Unlike builtin `next-line' this version accepts no arguments."
91 (set 'this-command 'next-line)
92 (call-interactively 'next-line)
93 (while (point-invisible-p)
96 (defun notmuch-show-previous-line ()
97 "Like builtin `previous-line' but ensuring we end on a visible character.
99 By advancing forward until reaching a visible character.
101 Unlike builtin `next-line' this version accepts no arguments."
103 (set 'this-command 'previous-line)
104 (call-interactively 'previous-line)
105 (while (point-invisible-p)
108 (defun notmuch-show-get-message-id ()
111 (if (not (looking-at notmuch-show-message-begin-regexp))
112 (re-search-backward notmuch-show-message-begin-regexp))
113 (re-search-forward notmuch-show-id-regexp)
114 (buffer-substring (match-beginning 1) (match-end 1))))
116 (defun notmuch-show-get-filename ()
119 (if (not (looking-at notmuch-show-message-begin-regexp))
120 (re-search-backward notmuch-show-message-begin-regexp))
121 (re-search-forward notmuch-show-filename-regexp)
122 (buffer-substring (match-beginning 1) (match-end 1))))
124 (defun notmuch-show-set-tags (tags)
127 (if (not (looking-at notmuch-show-message-begin-regexp))
128 (re-search-backward notmuch-show-message-begin-regexp))
129 (re-search-forward notmuch-show-tags-regexp)
130 (let ((inhibit-read-only t)
131 (beg (match-beginning 1))
133 (delete-region beg end)
135 (insert (mapconcat 'identity tags " ")))))
137 (defun notmuch-show-get-tags ()
140 (if (not (looking-at notmuch-show-message-begin-regexp))
141 (re-search-backward notmuch-show-message-begin-regexp))
142 (re-search-forward notmuch-show-tags-regexp)
143 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
145 (defun notmuch-show-add-tag (tag)
146 (interactive "sTag to add: ")
147 (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "id:" (notmuch-show-get-message-id)))
148 (notmuch-show-set-tags (delete-dups (sort (cons tag (notmuch-show-get-tags)) 'string<))))
150 (defun notmuch-show-remove-tag (tag)
151 (interactive "sTag to remove: ")
152 (let ((tags (notmuch-show-get-tags)))
153 (if (member tag tags)
155 (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "id:" (notmuch-show-get-message-id)))
156 (notmuch-show-set-tags (delete tag tags))))))
158 (defun notmuch-show-archive-thread ()
159 "Archive each message in thread, and show next thread from search.
161 Archive each message currrently shown by removing the \"inbox\"
162 tag from each. Then kill this buffer and show the next thread
163 from the search from which this thread was originally shown.
165 Note: This command is safe from any race condition of new messages
166 being delivered to the same thread. It does not archive the
167 entire thread, but only the messages shown in the current
171 (goto-char (point-min))
173 (notmuch-show-remove-tag "inbox")
176 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
177 (goto-char (point-max)))))
178 (let ((parent-buffer notmuch-show-parent-buffer))
182 (switch-to-buffer parent-buffer)
183 (notmuch-search-show-thread)))))
185 (defun notmuch-show-view-raw-message ()
186 "View the raw email of the current message."
188 (view-file (notmuch-show-get-filename)))
190 (defun notmuch-show-move-to-current-message-summary-line ()
191 "Move to the beginning of the one-line summary of the current message.
193 This gives us a stable place to move to and work from since the
194 summary line is always visible. This is important since moving to
195 an invisible location is unreliable, (the main command loop moves
196 point either forward or backward to the next visible character
197 when a command ends with point on an invisible character).
199 Emits an error if point is not within a valid message, (that is
200 not pattern of `notmuch-show-message-begin-regexp' could be found
201 by searching backward)."
203 (if (not (looking-at notmuch-show-message-begin-regexp))
204 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
206 (error "Not within a valid message."))
209 (defun notmuch-show-last-message-p ()
210 "Predicate testing whether point is within the last message."
211 (save-window-excursion
213 (notmuch-show-move-to-current-message-summary-line)
214 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
216 (defun notmuch-show-message-unread-p ()
217 "Preficate testing whether current message is unread."
218 (member "unread" (notmuch-show-get-tags)))
220 (defun notmuch-show-next-message ()
221 "Advance to the beginning of the next message in the buffer.
223 Moves to the last visible character of the current message if
224 already on the last message in the buffer."
226 (notmuch-show-move-to-current-message-summary-line)
227 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
228 (notmuch-show-move-to-current-message-summary-line)
229 (goto-char (- (point-max) 1))
230 (while (point-invisible-p)
234 (defun notmuch-show-find-next-message ()
235 "Returns the position of the next message in the buffer.
237 Or the position of the last visible character of the current
238 message if already within the last message in the buffer."
239 ; save-excursion doesn't save our window position
240 ; save-window-excursion doesn't save point
241 ; Looks like we have to use both.
243 (save-window-excursion
244 (notmuch-show-next-message)
247 (defun notmuch-show-next-unread-message ()
248 "Advance to the beginning of the next unread message in the buffer.
250 Moves to the last visible character of the current message if
251 there are no more unread messages past the current point."
252 (notmuch-show-next-message)
253 (while (and (not (notmuch-show-last-message-p))
254 (not (notmuch-show-message-unread-p)))
255 (notmuch-show-next-message))
256 (if (not (notmuch-show-message-unread-p))
257 (notmuch-show-next-message)))
259 (defun notmuch-show-next-open-message ()
260 "Advance to the the next message which is not hidden.
262 If read messages are currently hidden, advance to the next unread
263 message. Otherwise, advance to the next message."
264 (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
265 (assq 'notmuch-show-body-read buffer-invisibility-spec))
266 (notmuch-show-next-unread-message)
267 (notmuch-show-next-message)))
269 (defun notmuch-show-previous-message ()
270 "Backup to the beginning of the previous message in the buffer.
272 If within a message rather than at the beginning of it, then
273 simply move to the beginning of the current message."
275 (let ((start (point)))
276 (notmuch-show-move-to-current-message-summary-line)
277 (if (not (< (point) start))
278 ; Go backward twice to skip the current message's marker
280 (re-search-backward notmuch-show-message-begin-regexp nil t)
281 (re-search-backward notmuch-show-message-begin-regexp nil t)
282 (notmuch-show-move-to-current-message-summary-line)
286 (defun notmuch-show-find-previous-message ()
287 "Returns the position of the previous message in the buffer.
289 Or the position of the beginning of the current message if point
290 is originally within the message rather than at the beginning of
292 ; save-excursion doesn't save our window position
293 ; save-window-excursion doesn't save point
294 ; Looks like we have to use both.
296 (save-window-excursion
297 (notmuch-show-previous-message)
300 (defun notmuch-show-mark-read-then-next-open-message ()
301 "Remove unread tag from current message, then advance to next unread message."
303 (notmuch-show-remove-tag "unread")
304 (notmuch-show-next-open-message))
306 (defun notmuch-show-rewind ()
307 "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
309 Specifically, if the beginning of the previous email is fewer
310 than `window-height' lines from the current point, move to it
311 just like `notmuch-show-previous-message'.
313 Otherwise, just scroll down a screenful of the current message.
315 This command does not modify any message tags, (it does not undo
316 any effects from previous calls to
317 `notmuch-show-advance-marking-read-and-archiving'."
319 (let ((previous (notmuch-show-find-previous-message)))
320 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
324 ((beginning-of-buffer) nil))
325 (goto-char (window-start)))
326 (notmuch-show-previous-message))))
328 (defun notmuch-show-advance-marking-read-and-archiving ()
329 "Advance through buffer, marking read and archiving.
331 This command is intended to be one of the simplest ways to
332 process a thread of email. It does the following:
334 If the current message in the thread is not yet fully visible,
335 scroll by a near screenful to read more of the message.
337 Otherwise, (the end of the current message is already within the
338 current window), remove the \"unread\" tag (if present) from the
339 current message and advance to the next open message.
341 Finally, if there is no further message to advance to, and this
342 last message is already read, then archive the entire current
343 thread, (remove the \"inbox\" tag from each message). Also kill
344 this buffer, and display the next thread from the search from
345 which this thread was originally shown."
347 (let ((next (notmuch-show-find-next-message))
348 (unread (notmuch-show-message-unread-p)))
349 (if (> next (window-end))
352 (notmuch-show-mark-read-then-next-open-message)
353 (if (notmuch-show-last-message-p)
354 (notmuch-show-archive-thread)
355 (notmuch-show-next-open-message))))))
357 (defun notmuch-show-markup-citations-region (beg end)
360 (while (< (point) end)
361 (let ((beg-sub (point)))
364 (while (looking-at ">")
366 (let ((overlay (make-overlay beg-sub (point))))
367 (overlay-put overlay 'invisible 'notmuch-show-citation)
368 (overlay-put overlay 'before-string
369 (concat "[" (number-to-string (count-lines beg-sub (point)))
370 " quoted lines.]\n")))))
371 (if (looking-at "--[ ]?$")
372 (let ((sig-lines (count-lines beg-sub end)))
373 (if (<= sig-lines notmuch-show-signature-lines-max)
375 (overlay-put (make-overlay beg-sub (+ beg-sub 1))
377 (concat "[" (number-to-string sig-lines)
378 "-line signature.]"))
379 (overlay-put (make-overlay (+ beg-sub 2) end)
380 'invisible 'notmuch-show-signature)
384 (defun notmuch-show-markup-body ()
385 (re-search-forward notmuch-show-body-begin-regexp)
389 (re-search-forward notmuch-show-body-end-regexp)
390 (let ((end (match-beginning 0)))
391 (notmuch-show-markup-citations-region beg end)
392 (if (not (notmuch-show-message-unread-p))
393 (overlay-put (make-overlay beg end)
394 'invisible 'notmuch-show-body-read)))))
396 (defun notmuch-show-markup-header ()
397 (re-search-forward notmuch-show-header-begin-regexp)
401 (re-search-forward notmuch-show-header-end-regexp)
402 (overlay-put (make-overlay beg (match-beginning 0))
403 'invisible 'notmuch-show-header)))
405 (defun notmuch-show-markup-message ()
406 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
408 (notmuch-show-markup-header)
409 (notmuch-show-markup-body))
410 (goto-char (point-max))))
412 (defun notmuch-show-hide-markers ()
414 (goto-char (point-min))
416 (if (re-search-forward notmuch-show-marker-regexp nil t)
418 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
419 'invisible 'notmuch-show-marker))
420 (goto-char (point-max))))))
422 (defun notmuch-show-markup-messages ()
424 (goto-char (point-min))
426 (notmuch-show-markup-message)))
427 (notmuch-show-hide-markers))
429 (defun notmuch-show-toggle-citations-visible ()
430 "Toggle visibility of citations"
432 (if notmuch-show-citations-visible
433 (add-to-invisibility-spec 'notmuch-show-citation)
434 (remove-from-invisibility-spec 'notmuch-show-citation))
435 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
436 ; Need to force the redisplay for some reason
437 (force-window-update)
440 (defun notmuch-show-toggle-signatures-visible ()
441 "Toggle visibility of signatures"
443 (if notmuch-show-signatures-visible
444 (add-to-invisibility-spec 'notmuch-show-signature)
445 (remove-from-invisibility-spec 'notmuch-show-signature))
446 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
447 ; Need to force the redisplay for some reason
448 (force-window-update)
451 (defun notmuch-show-toggle-headers-visible ()
452 "Toggle visibility of header fields"
454 (if notmuch-show-headers-visible
455 (add-to-invisibility-spec 'notmuch-show-header)
456 (remove-from-invisibility-spec 'notmuch-show-header))
457 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
458 ; Need to force the redisplay for some reason
459 (force-window-update)
462 (defun notmuch-show-toggle-body-read-visible ()
463 "Toggle visibility of message bodies of read messages"
465 (if notmuch-show-body-read-visible
466 (add-to-invisibility-spec 'notmuch-show-body-read)
467 (remove-from-invisibility-spec 'notmuch-show-body-read))
468 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
469 ; Need to force the redisplay for some reason
470 (force-window-update)
474 (defun notmuch-show-mode ()
475 "Major mode for viewing a thread with notmuch.
477 This buffer contains the results of the \"notmuch show\" command
478 for displaying a single thread of email from your email archives.
480 By default, various components of email messages, (citations,
481 signatures, already-read messages), are invisible to help you
482 focus on the most important things, (new text from unread
483 messages). See the various commands below for toggling the
484 visibility of hidden components.
486 The `notmuch-show-next-message' and
487 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
488 default), allow you to navigate to the next and previous
489 messages. Each time you navigate away from a message with
490 `notmuch-show-next-message' the current message will have its
491 \"unread\" tag removed.
493 You can add or remove tags from the current message with '+' and
494 '-'. You can also archive all messages in the current
495 view, (remove the \"inbox\" tag from each), with
496 `notmuch-show-archive-thread' (bound to 'a' by default).
498 \\{notmuch-show-mode-map}"
500 (kill-all-local-variables)
501 (set (make-local-variable 'notmuch-show-headers-visible) t)
502 (notmuch-show-toggle-headers-visible)
503 (set (make-local-variable 'notmuch-show-body-read-visible) t)
504 (notmuch-show-toggle-body-read-visible)
505 (set (make-local-variable 'notmuch-show-citations-visible) t)
506 (notmuch-show-toggle-citations-visible)
507 (set (make-local-variable 'notmuch-show-signatures-visible) t)
508 (notmuch-show-toggle-signatures-visible)
509 (add-to-invisibility-spec 'notmuch-show-marker)
510 (use-local-map notmuch-show-mode-map)
511 (setq major-mode 'notmuch-show-mode
512 mode-name "notmuch-show")
513 (setq buffer-read-only t))
515 (defun notmuch-show (thread-id &optional parent-buffer)
516 "Run \"notmuch show\" with the given thread ID and display results.
518 The optional PARENT-BUFFER is the notmuch-search buffer from
519 which this notmuch-show command was executed, (so that the next
520 thread from that buffer can be show when done with this one)."
521 (interactive "sNotmuch show: ")
522 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
523 (switch-to-buffer buffer)
525 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
526 (let ((proc (get-buffer-process (current-buffer)))
527 (inhibit-read-only t))
529 (error "notmuch search process already running for query `%s'" query)
532 (goto-char (point-min))
534 (call-process "notmuch" nil t nil "show" thread-id)
535 (notmuch-show-markup-messages)
537 ; Move straight to the first unread message
538 (if (not (notmuch-show-message-unread-p))
540 (notmuch-show-next-unread-message)
541 ; But if there are no unread messages, go back to the
542 ; beginning of the buffer, and open up the bodies of all
544 (if (not (notmuch-show-message-unread-p))
546 (goto-char (point-min))
547 (notmuch-show-toggle-body-read-visible)))))
550 (defvar notmuch-search-mode-map
551 (let ((map (make-sparse-keymap)))
552 (define-key map "a" 'notmuch-search-archive-thread)
553 (define-key map "b" 'notmuch-search-scroll-down)
554 (define-key map "f" 'notmuch-search-filter)
555 (define-key map "n" 'next-line)
556 (define-key map "p" 'previous-line)
557 (define-key map "q" 'kill-this-buffer)
558 (define-key map "s" 'notmuch-search)
559 (define-key map "t" 'notmuch-search-filter-by-tag)
560 (define-key map "x" 'kill-this-buffer)
561 (define-key map (kbd "RET") 'notmuch-search-show-thread)
562 (define-key map "+" 'notmuch-search-add-tag)
563 (define-key map "-" 'notmuch-search-remove-tag)
564 (define-key map "<" 'beginning-of-buffer)
565 (define-key map ">" 'notmuch-search-goto-last-thread)
566 (define-key map "=" 'notmuch-search-refresh-view)
567 (define-key map "\M->" 'notmuch-search-goto-last-thread)
568 (define-key map " " 'notmuch-search-scroll-up)
569 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
571 "Keymap for \"notmuch search\" buffers.")
572 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
574 (defun notmuch-search-scroll-up ()
575 "Scroll up, moving point to last message in thread if at end."
579 ((end-of-buffer) (notmuch-search-goto-last-thread))))
581 (defun notmuch-search-scroll-down ()
582 "Scroll down, moving point to first message in thread if at beginning."
584 ; I don't know why scroll-down doesn't signal beginning-of-buffer
585 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
587 ; So instead of trapping a signal we instead check whether the
588 ; window begins on the first line of the buffer and if so, move
589 ; directly to that position. (We have to count lines since the
590 ; window-start position is not the same as point-min due to the
591 ; invisible thread-ID characters on the first line.
592 (if (equal (count-lines (point-min) (window-start)) 1)
593 (goto-char (window-start))
596 (defun notmuch-search-goto-last-thread (&optional arg)
597 "Move point to the last thread in the buffer."
603 (defun notmuch-search-mode ()
604 "Major mode for searching mail with notmuch.
606 This buffer contains the results of a \"notmuch search\" of your
607 email archives. Each line in the buffer represents a single
608 thread giving a relative date for the thread and a subject.
610 Pressing RET on any line displays that thread. The '+' and '-'
611 keys can be used to add or remove tags from a thread. The 'a' key
612 is a convenience key for archiving a thread (removing the
615 Other useful commands are `notmuch-search-filter' for filtering
616 the current search based on an additional query string,
617 `notmuch-search-filter-by-tag' for filtering to include only
618 messages with a given tag, and `notmuch-search' to execute a new,
621 \\{notmuch-search-mode-map}"
623 (kill-all-local-variables)
624 (make-local-variable 'notmuch-search-query-string)
625 (set (make-local-variable 'scroll-preserve-screen-position) t)
626 (add-to-invisibility-spec 'notmuch-search)
627 (use-local-map notmuch-search-mode-map)
628 (setq major-mode 'notmuch-search-mode
629 mode-name "notmuch-search")
630 (setq buffer-read-only t))
632 (defun notmuch-search-find-thread-id ()
636 (re-search-forward "[a-fA-F0-9]*")
637 (filter-buffer-substring beg (point)))))
639 (defun notmuch-search-markup-this-thread-id ()
642 (re-search-forward "[a-fA-F0-9]*")
644 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
646 (defun notmuch-search-markup-thread-ids ()
648 (goto-char (point-min))
650 (notmuch-search-markup-this-thread-id)
653 (defun notmuch-search-hide-thread-ids ()
655 (add-to-invisibility-spec 'notmuch-search)
656 (force-window-update)
659 (defun notmuch-search-show-thread-ids ()
661 (remove-from-invisibility-spec 'notmuch-search)
662 (force-window-update)
665 (defun notmuch-search-show-thread ()
667 (let ((thread-id (notmuch-search-find-thread-id)))
669 (if (> (length thread-id) 0)
670 (notmuch-show thread-id (current-buffer))
671 (error "End of search results"))))
673 (defun notmuch-call-notmuch-process (&rest args)
674 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
675 (with-current-buffer error-buffer
677 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
680 (with-current-buffer error-buffer
681 (let ((beg (point-min))
682 (end (- (point-max) 1)))
683 (error (buffer-substring beg end))
686 (defun notmuch-search-set-tags (tags)
689 (re-search-backward "(")
692 (inhibit-read-only t))
693 (re-search-forward ")")
696 (delete-region beg end)
697 (insert (mapconcat 'identity tags " "))))))
699 (defun notmuch-search-get-tags ()
702 (re-search-backward "(")
703 (let ((beg (+ (point) 1)))
704 (re-search-forward ")")
705 (let ((end (- (point) 1)))
706 (split-string (buffer-substring beg end))))))
708 (defun notmuch-search-add-tag (tag)
709 (interactive "sTag to add: ")
710 (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
711 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
713 (defun notmuch-search-remove-tag (tag)
714 (interactive "sTag to remove: ")
715 (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
716 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
718 (defun notmuch-search-archive-thread ()
719 "Archive the current thread (remove its \"inbox\" tag).
721 This function advances point to the next line when finished."
723 (notmuch-search-remove-tag "inbox")
726 (defun notmuch-search (query)
727 "Run \"notmuch search\" with the given query string and display results."
728 (interactive "sNotmuch search: ")
729 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
730 (switch-to-buffer buffer)
731 (notmuch-search-mode)
732 (set 'notmuch-search-query-string query)
733 (let ((proc (get-buffer-process (current-buffer)))
734 (inhibit-read-only t))
736 (error "notmuch search process already running for query `%s'" query)
739 (goto-char (point-min))
741 (call-process "notmuch" nil t nil "search" query)
742 (notmuch-search-markup-thread-ids)
745 (defun notmuch-search-refresh-view ()
746 "Refresh the current view.
748 Kills the current buffer and runs a new search with the same
749 query string as the current search. If the current thread is in
750 the new search results, then point will be placed on the same
751 thread. Otherwise, point will be moved to attempt to be in the
752 same relative position within the new buffer."
755 (thread (notmuch-search-find-thread-id))
756 (query notmuch-search-query-string))
758 (notmuch-search query)
759 (goto-char (point-min))
760 (if (re-search-forward (concat "^" thread) nil t)
764 (defun notmuch-search-filter (query)
765 "Filter the current search results based on an additional query string.
767 Runs a new search matching only messages that match both the
768 current search results AND the additional query string provided."
769 (interactive "sFilter search: ")
770 (notmuch-search (concat notmuch-search-query-string " and " query)))
772 (defun notmuch-search-filter-by-tag (tag)
773 "Filter the current search results based on a single tag.
775 Runs a new search matching only messages that match both the
776 current search results AND that are tagged with the given tag."
777 (interactive "sFilter by tag: ")
778 (notmuch-search (concat notmuch-search-query-string " and tag:" tag)))
781 "Run notmuch to display all mail with tag of 'inbox'"
783 (notmuch-search "tag:inbox"))