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 "N" 'notmuch-show-mark-read-then-next-open-message)
37 (define-key map "p" 'notmuch-show-previous-message)
38 (define-key map (kbd "C-n") 'notmuch-show-next-line)
39 (define-key map (kbd "C-p") 'notmuch-show-previous-line)
40 (define-key map "q" 'kill-this-buffer)
41 (define-key map "s" 'notmuch-show-toggle-signatures-visible)
42 (define-key map "w" 'notmuch-show-view-raw-message)
43 (define-key map "x" 'kill-this-buffer)
44 (define-key map "+" 'notmuch-show-add-tag)
45 (define-key map "-" 'notmuch-show-remove-tag)
46 (define-key map (kbd "DEL") 'notmuch-show-rewind)
47 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
48 (define-key map "|" 'notmuch-show-pipe-message)
50 "Keymap for \"notmuch show\" buffers.")
51 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
53 (defvar notmuch-show-signature-lines-max 6
54 "Maximum length of signature that will be hidden by default.")
56 (set 'notmuch-show-message-begin-regexp "
\fmessage{")
57 (set 'notmuch-show-message-end-regexp "
\fmessage}")
58 (set 'notmuch-show-header-begin-regexp "
\fheader{")
59 (set 'notmuch-show-header-end-regexp "
\fheader}")
60 (set 'notmuch-show-body-begin-regexp "
\fbody{")
61 (set 'notmuch-show-body-end-regexp "
\fbody}")
62 (set 'notmuch-show-attachment-begin-regexp "
\fattachment{")
63 (set 'notmuch-show-attachment-end-regexp "
\fattachment}")
64 (set 'notmuch-show-part-begin-regexp "
\fpart{")
65 (set 'notmuch-show-part-end-regexp "
\fpart}")
66 (set 'notmuch-show-marker-regexp "
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
68 (set 'notmuch-show-id-regexp "ID: \\([^ ]*\\)")
69 (set 'notmuch-show-filename-regexp "Filename: \\(.*\\)$")
70 (set 'notmuch-show-tags-regexp "(\\([^)]*\\))$")
72 ; XXX: This should be a generic function in emacs somewhere, not here
73 (defun point-invisible-p ()
74 "Return whether the character at point is invisible.
76 Here visibility is determined by `buffer-invisibility-spec' and
77 the invisible property of any overlays for point. It doesn't have
78 anything to do with whether point is currently being displayed
79 within the current window."
80 (let ((prop (get-char-property (point) 'invisible)))
81 (if (eq buffer-invisibility-spec t)
83 (or (memq prop buffer-invisibility-spec)
84 (assq prop buffer-invisibility-spec)))))
86 (defun notmuch-show-next-line ()
87 "Like builtin `next-line' but ensuring we end on a visible character.
89 By advancing forward until reaching a visible character.
91 Unlike builtin `next-line' this version accepts no arguments."
93 (set 'this-command 'next-line)
94 (call-interactively 'next-line)
95 (while (point-invisible-p)
98 (defun notmuch-show-previous-line ()
99 "Like builtin `previous-line' but ensuring we end on a visible character.
101 By advancing forward until reaching a visible character.
103 Unlike builtin `next-line' this version accepts no arguments."
105 (set 'this-command 'previous-line)
106 (call-interactively 'previous-line)
107 (while (point-invisible-p)
110 (defun notmuch-show-get-message-id ()
113 (if (not (looking-at notmuch-show-message-begin-regexp))
114 (re-search-backward notmuch-show-message-begin-regexp))
115 (re-search-forward notmuch-show-id-regexp)
116 (buffer-substring (match-beginning 1) (match-end 1))))
118 (defun notmuch-show-get-filename ()
121 (if (not (looking-at notmuch-show-message-begin-regexp))
122 (re-search-backward notmuch-show-message-begin-regexp))
123 (re-search-forward notmuch-show-filename-regexp)
124 (buffer-substring (match-beginning 1) (match-end 1))))
126 (defun notmuch-show-set-tags (tags)
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 (let ((inhibit-read-only t)
133 (beg (match-beginning 1))
135 (delete-region beg end)
137 (insert (mapconcat 'identity tags " ")))))
139 (defun notmuch-show-get-tags ()
142 (if (not (looking-at notmuch-show-message-begin-regexp))
143 (re-search-backward notmuch-show-message-begin-regexp))
144 (re-search-forward notmuch-show-tags-regexp)
145 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
147 (defun notmuch-show-add-tag (tag)
148 (interactive "sTag to add: ")
149 (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "id:" (notmuch-show-get-message-id)))
150 (notmuch-show-set-tags (delete-dups (sort (cons tag (notmuch-show-get-tags)) 'string<))))
152 (defun notmuch-show-remove-tag (tag)
153 (interactive "sTag to remove: ")
154 (let ((tags (notmuch-show-get-tags)))
155 (if (member tag tags)
157 (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "id:" (notmuch-show-get-message-id)))
158 (notmuch-show-set-tags (delete tag tags))))))
160 (defun notmuch-show-archive-thread ()
161 "Archive each message in thread, and show next thread from search.
163 Archive each message currrently shown by removing the \"inbox\"
164 tag from each. Then kill this buffer and show the next thread
165 from the search from which this thread was originally shown.
167 Note: This command is safe from any race condition of new messages
168 being delivered to the same thread. It does not archive the
169 entire thread, but only the messages shown in the current
173 (goto-char (point-min))
175 (notmuch-show-remove-tag "inbox")
178 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
179 (goto-char (point-max)))))
180 (let ((parent-buffer notmuch-show-parent-buffer))
184 (switch-to-buffer parent-buffer)
185 (notmuch-search-show-thread)))))
187 (defun notmuch-show-view-raw-message ()
188 "View the raw email of the current message."
190 (view-file (notmuch-show-get-filename)))
192 (defun notmuch-show-pipe-message (command)
193 "Pipe the contents of the current message to the given command.
195 The given command will be executed with the raw contents of the
196 current email message as stdin. Anything printed by the command
197 to stdout or stderr will appear in the *Messages* buffer."
198 (interactive "sPipe message to command: ")
199 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*" (split-string (concat command " < " (notmuch-show-get-filename)))))
201 (defun notmuch-show-move-to-current-message-summary-line ()
202 "Move to the beginning of the one-line summary of the current message.
204 This gives us a stable place to move to and work from since the
205 summary line is always visible. This is important since moving to
206 an invisible location is unreliable, (the main command loop moves
207 point either forward or backward to the next visible character
208 when a command ends with point on an invisible character).
210 Emits an error if point is not within a valid message, (that is
211 not pattern of `notmuch-show-message-begin-regexp' could be found
212 by searching backward)."
214 (if (not (looking-at notmuch-show-message-begin-regexp))
215 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
217 (error "Not within a valid message."))
220 (defun notmuch-show-last-message-p ()
221 "Predicate testing whether point is within the last message."
222 (save-window-excursion
224 (notmuch-show-move-to-current-message-summary-line)
225 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
227 (defun notmuch-show-message-unread-p ()
228 "Preficate testing whether current message is unread."
229 (member "unread" (notmuch-show-get-tags)))
231 (defun notmuch-show-next-message ()
232 "Advance to the beginning of the next message in the buffer.
234 Moves to the last visible character of the current message if
235 already on the last message in the buffer."
237 (notmuch-show-move-to-current-message-summary-line)
238 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
239 (notmuch-show-move-to-current-message-summary-line)
240 (goto-char (- (point-max) 1))
241 (while (point-invisible-p)
245 (defun notmuch-show-find-next-message ()
246 "Returns the position of the next message in the buffer.
248 Or the position of the last visible character of the current
249 message if already within the last message in the buffer."
250 ; save-excursion doesn't save our window position
251 ; save-window-excursion doesn't save point
252 ; Looks like we have to use both.
254 (save-window-excursion
255 (notmuch-show-next-message)
258 (defun notmuch-show-next-unread-message ()
259 "Advance to the beginning of the next unread message in the buffer.
261 Moves to the last visible character of the current message if
262 there are no more unread messages past the current point."
263 (notmuch-show-next-message)
264 (while (and (not (notmuch-show-last-message-p))
265 (not (notmuch-show-message-unread-p)))
266 (notmuch-show-next-message))
267 (if (not (notmuch-show-message-unread-p))
268 (notmuch-show-next-message)))
270 (defun notmuch-show-next-open-message ()
271 "Advance to the the next message which is not hidden.
273 If read messages are currently hidden, advance to the next unread
274 message. Otherwise, advance to the next message."
275 (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
276 (assq 'notmuch-show-body-read buffer-invisibility-spec))
277 (notmuch-show-next-unread-message)
278 (notmuch-show-next-message)))
280 (defun notmuch-show-previous-message ()
281 "Backup to the beginning of the previous message in the buffer.
283 If within a message rather than at the beginning of it, then
284 simply move to the beginning of the current message."
286 (let ((start (point)))
287 (notmuch-show-move-to-current-message-summary-line)
288 (if (not (< (point) start))
289 ; Go backward twice to skip the current message's marker
291 (re-search-backward notmuch-show-message-begin-regexp nil t)
292 (re-search-backward notmuch-show-message-begin-regexp nil t)
293 (notmuch-show-move-to-current-message-summary-line)
297 (defun notmuch-show-find-previous-message ()
298 "Returns the position of the previous message in the buffer.
300 Or the position of the beginning of the current message if point
301 is originally within the message rather than at the beginning of
303 ; save-excursion doesn't save our window position
304 ; save-window-excursion doesn't save point
305 ; Looks like we have to use both.
307 (save-window-excursion
308 (notmuch-show-previous-message)
311 (defun notmuch-show-mark-read-then-next-open-message ()
312 "Remove unread tag from current message, then advance to next unread message."
314 (notmuch-show-remove-tag "unread")
315 (notmuch-show-next-open-message))
317 (defun notmuch-show-rewind ()
318 "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
320 Specifically, if the beginning of the previous email is fewer
321 than `window-height' lines from the current point, move to it
322 just like `notmuch-show-previous-message'.
324 Otherwise, just scroll down a screenful of the current message.
326 This command does not modify any message tags, (it does not undo
327 any effects from previous calls to
328 `notmuch-show-advance-marking-read-and-archiving'."
330 (let ((previous (notmuch-show-find-previous-message)))
331 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
335 ((beginning-of-buffer) nil))
336 (goto-char (window-start)))
337 (notmuch-show-previous-message))))
339 (defun notmuch-show-advance-marking-read-and-archiving ()
340 "Advance through buffer, marking read and archiving.
342 This command is intended to be one of the simplest ways to
343 process a thread of email. It does the following:
345 If the current message in the thread is not yet fully visible,
346 scroll by a near screenful to read more of the message.
348 Otherwise, (the end of the current message is already within the
349 current window), remove the \"unread\" tag (if present) from the
350 current message and advance to the next open message.
352 Finally, if there is no further message to advance to, and this
353 last message is already read, then archive the entire current
354 thread, (remove the \"inbox\" tag from each message). Also kill
355 this buffer, and display the next thread from the search from
356 which this thread was originally shown."
358 (let ((next (notmuch-show-find-next-message))
359 (unread (notmuch-show-message-unread-p)))
360 (if (> next (window-end))
362 (let ((last (notmuch-show-last-message-p)))
363 (notmuch-show-mark-read-then-next-open-message)
365 (notmuch-show-archive-thread))))))
367 (defun notmuch-show-markup-citations-region (beg end)
370 (while (< (point) end)
371 (let ((beg-sub (point)))
374 (while (looking-at ">")
376 (let ((overlay (make-overlay beg-sub (point))))
377 (overlay-put overlay 'invisible 'notmuch-show-citation)
378 (overlay-put overlay 'before-string
379 (concat "[" (number-to-string (count-lines beg-sub (point)))
380 " quoted lines.]\n")))))
381 (if (looking-at "--[ ]?$")
382 (let ((sig-lines (count-lines beg-sub end)))
383 (if (<= sig-lines notmuch-show-signature-lines-max)
385 (overlay-put (make-overlay beg-sub (+ beg-sub 1))
387 (concat "[" (number-to-string sig-lines)
388 "-line signature.]"))
389 (overlay-put (make-overlay (+ beg-sub 2) end)
390 'invisible 'notmuch-show-signature)
394 (defun notmuch-show-markup-body ()
395 (re-search-forward notmuch-show-body-begin-regexp)
399 (re-search-forward notmuch-show-body-end-regexp)
400 (let ((end (match-beginning 0)))
401 (notmuch-show-markup-citations-region beg end)
402 (if (not (notmuch-show-message-unread-p))
403 (overlay-put (make-overlay beg end)
404 'invisible 'notmuch-show-body-read)))))
406 (defun notmuch-show-markup-header ()
407 (re-search-forward notmuch-show-header-begin-regexp)
411 (re-search-forward notmuch-show-header-end-regexp)
412 (overlay-put (make-overlay beg (match-beginning 0))
413 'invisible 'notmuch-show-header)))
415 (defun notmuch-show-markup-message ()
416 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
418 (notmuch-show-markup-header)
419 (notmuch-show-markup-body))
420 (goto-char (point-max))))
422 (defun notmuch-show-hide-markers ()
424 (goto-char (point-min))
426 (if (re-search-forward notmuch-show-marker-regexp nil t)
428 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
429 'invisible 'notmuch-show-marker))
430 (goto-char (point-max))))))
432 (defun notmuch-show-markup-messages ()
434 (goto-char (point-min))
436 (notmuch-show-markup-message)))
437 (notmuch-show-hide-markers))
439 (defun notmuch-show-toggle-citations-visible ()
440 "Toggle visibility of citations"
442 (if notmuch-show-citations-visible
443 (add-to-invisibility-spec 'notmuch-show-citation)
444 (remove-from-invisibility-spec 'notmuch-show-citation))
445 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
446 ; Need to force the redisplay for some reason
447 (force-window-update)
450 (defun notmuch-show-toggle-signatures-visible ()
451 "Toggle visibility of signatures"
453 (if notmuch-show-signatures-visible
454 (add-to-invisibility-spec 'notmuch-show-signature)
455 (remove-from-invisibility-spec 'notmuch-show-signature))
456 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
457 ; Need to force the redisplay for some reason
458 (force-window-update)
461 (defun notmuch-show-toggle-headers-visible ()
462 "Toggle visibility of header fields"
464 (if notmuch-show-headers-visible
465 (add-to-invisibility-spec 'notmuch-show-header)
466 (remove-from-invisibility-spec 'notmuch-show-header))
467 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
468 ; Need to force the redisplay for some reason
469 (force-window-update)
472 (defun notmuch-show-toggle-body-read-visible ()
473 "Toggle visibility of message bodies of read messages"
475 (if notmuch-show-body-read-visible
476 (add-to-invisibility-spec 'notmuch-show-body-read)
477 (remove-from-invisibility-spec 'notmuch-show-body-read))
478 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
479 ; Need to force the redisplay for some reason
480 (force-window-update)
484 (defun notmuch-show-mode ()
485 "Major mode for viewing a thread with notmuch.
487 This buffer contains the results of the \"notmuch show\" command
488 for displaying a single thread of email from your email archives.
490 By default, various components of email messages, (citations,
491 signatures, already-read messages), are invisible to help you
492 focus on the most important things, (new text from unread
493 messages). See the various commands below for toggling the
494 visibility of hidden components.
496 The `notmuch-show-next-message' and
497 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
498 default), allow you to navigate to the next and previous
499 messages. Each time you navigate away from a message with
500 `notmuch-show-next-message' the current message will have its
501 \"unread\" tag removed.
503 You can add or remove tags from the current message with '+' and
504 '-'. You can also archive all messages in the current
505 view, (remove the \"inbox\" tag from each), with
506 `notmuch-show-archive-thread' (bound to 'a' by default).
508 \\{notmuch-show-mode-map}"
510 (kill-all-local-variables)
511 (set (make-local-variable 'notmuch-show-headers-visible) t)
512 (notmuch-show-toggle-headers-visible)
513 (set (make-local-variable 'notmuch-show-body-read-visible) t)
514 (notmuch-show-toggle-body-read-visible)
515 (set (make-local-variable 'notmuch-show-citations-visible) t)
516 (notmuch-show-toggle-citations-visible)
517 (set (make-local-variable 'notmuch-show-signatures-visible) t)
518 (notmuch-show-toggle-signatures-visible)
519 (add-to-invisibility-spec 'notmuch-show-marker)
520 (use-local-map notmuch-show-mode-map)
521 (setq major-mode 'notmuch-show-mode
522 mode-name "notmuch-show")
523 (setq buffer-read-only t))
525 (defun notmuch-show (thread-id &optional parent-buffer)
526 "Run \"notmuch show\" with the given thread ID and display results.
528 The optional PARENT-BUFFER is the notmuch-search buffer from
529 which this notmuch-show command was executed, (so that the next
530 thread from that buffer can be show when done with this one)."
531 (interactive "sNotmuch show: ")
532 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
533 (switch-to-buffer buffer)
535 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
536 (let ((proc (get-buffer-process (current-buffer)))
537 (inhibit-read-only t))
539 (error "notmuch search process already running for query `%s'" query)
542 (goto-char (point-min))
544 (call-process "notmuch" nil t nil "show" thread-id)
545 (notmuch-show-markup-messages)
547 ; Move straight to the first unread message
548 (if (not (notmuch-show-message-unread-p))
550 (notmuch-show-next-unread-message)
551 ; But if there are no unread messages, go back to the
552 ; beginning of the buffer, and open up the bodies of all
554 (if (not (notmuch-show-message-unread-p))
556 (goto-char (point-min))
557 (notmuch-show-toggle-body-read-visible)))))
560 (defvar notmuch-search-mode-map
561 (let ((map (make-sparse-keymap)))
562 (define-key map "a" 'notmuch-search-archive-thread)
563 (define-key map "b" 'notmuch-search-scroll-down)
564 (define-key map "f" 'notmuch-search-filter)
565 (define-key map "n" 'next-line)
566 (define-key map "p" 'previous-line)
567 (define-key map "q" 'kill-this-buffer)
568 (define-key map "s" 'notmuch-search)
569 (define-key map "t" 'notmuch-search-filter-by-tag)
570 (define-key map "x" 'kill-this-buffer)
571 (define-key map (kbd "RET") 'notmuch-search-show-thread)
572 (define-key map "+" 'notmuch-search-add-tag)
573 (define-key map "-" 'notmuch-search-remove-tag)
574 (define-key map "<" 'beginning-of-buffer)
575 (define-key map ">" 'notmuch-search-goto-last-thread)
576 (define-key map "=" 'notmuch-search-refresh-view)
577 (define-key map "\M->" 'notmuch-search-goto-last-thread)
578 (define-key map " " 'notmuch-search-scroll-up)
579 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
581 "Keymap for \"notmuch search\" buffers.")
582 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
584 (defun notmuch-search-scroll-up ()
585 "Scroll up, moving point to last message in thread if at end."
589 ((end-of-buffer) (notmuch-search-goto-last-thread))))
591 (defun notmuch-search-scroll-down ()
592 "Scroll down, moving point to first message in thread if at beginning."
594 ; I don't know why scroll-down doesn't signal beginning-of-buffer
595 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
597 ; So instead of trapping a signal we instead check whether the
598 ; window begins on the first line of the buffer and if so, move
599 ; directly to that position. (We have to count lines since the
600 ; window-start position is not the same as point-min due to the
601 ; invisible thread-ID characters on the first line.
602 (if (equal (count-lines (point-min) (window-start)) 1)
603 (goto-char (window-start))
606 (defun notmuch-search-goto-last-thread (&optional arg)
607 "Move point to the last thread in the buffer."
613 (defun notmuch-search-mode ()
614 "Major mode for searching mail with notmuch.
616 This buffer contains the results of a \"notmuch search\" of your
617 email archives. Each line in the buffer represents a single
618 thread giving a relative date for the thread and a subject.
620 Pressing RET on any line displays that thread. The '+' and '-'
621 keys can be used to add or remove tags from a thread. The 'a' key
622 is a convenience key for archiving a thread (removing the
625 Other useful commands are `notmuch-search-filter' for filtering
626 the current search based on an additional query string,
627 `notmuch-search-filter-by-tag' for filtering to include only
628 messages with a given tag, and `notmuch-search' to execute a new,
631 \\{notmuch-search-mode-map}"
633 (kill-all-local-variables)
634 (make-local-variable 'notmuch-search-query-string)
635 (set (make-local-variable 'scroll-preserve-screen-position) t)
636 (add-to-invisibility-spec 'notmuch-search)
637 (use-local-map notmuch-search-mode-map)
638 (setq major-mode 'notmuch-search-mode
639 mode-name "notmuch-search")
640 (setq buffer-read-only t))
642 (defun notmuch-search-find-thread-id ()
646 (re-search-forward "[a-fA-F0-9]*")
647 (filter-buffer-substring beg (point)))))
649 (defun notmuch-search-markup-this-thread-id ()
652 (re-search-forward "[a-fA-F0-9]*")
654 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
656 (defun notmuch-search-markup-thread-ids ()
658 (goto-char (point-min))
660 (notmuch-search-markup-this-thread-id)
663 (defun notmuch-search-hide-thread-ids ()
665 (add-to-invisibility-spec 'notmuch-search)
666 (force-window-update)
669 (defun notmuch-search-show-thread-ids ()
671 (remove-from-invisibility-spec 'notmuch-search)
672 (force-window-update)
675 (defun notmuch-search-show-thread ()
677 (let ((thread-id (notmuch-search-find-thread-id)))
679 (if (> (length thread-id) 0)
680 (notmuch-show thread-id (current-buffer))
681 (error "End of search results"))))
683 (defun notmuch-call-notmuch-process (&rest args)
684 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
685 (with-current-buffer error-buffer
687 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
690 (with-current-buffer error-buffer
691 (let ((beg (point-min))
692 (end (- (point-max) 1)))
693 (error (buffer-substring beg end))
696 (defun notmuch-search-set-tags (tags)
699 (re-search-backward "(")
702 (inhibit-read-only t))
703 (re-search-forward ")")
706 (delete-region beg end)
707 (insert (mapconcat 'identity tags " "))))))
709 (defun notmuch-search-get-tags ()
712 (re-search-backward "(")
713 (let ((beg (+ (point) 1)))
714 (re-search-forward ")")
715 (let ((end (- (point) 1)))
716 (split-string (buffer-substring beg end))))))
718 (defun notmuch-search-add-tag (tag)
719 (interactive "sTag to add: ")
720 (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
721 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
723 (defun notmuch-search-remove-tag (tag)
724 (interactive "sTag to remove: ")
725 (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
726 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
728 (defun notmuch-search-archive-thread ()
729 "Archive the current thread (remove its \"inbox\" tag).
731 This function advances point to the next line when finished."
733 (notmuch-search-remove-tag "inbox")
736 (defun notmuch-search (query)
737 "Run \"notmuch search\" with the given query string and display results."
738 (interactive "sNotmuch search: ")
739 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
740 (switch-to-buffer buffer)
741 (notmuch-search-mode)
742 (set 'notmuch-search-query-string query)
743 (let ((proc (get-buffer-process (current-buffer)))
744 (inhibit-read-only t))
746 (error "notmuch search process already running for query `%s'" query)
749 (goto-char (point-min))
751 (call-process "notmuch" nil t nil "search" query)
752 (notmuch-search-markup-thread-ids)
755 (defun notmuch-search-refresh-view ()
756 "Refresh the current view.
758 Kills the current buffer and runs a new search with the same
759 query string as the current search. If the current thread is in
760 the new search results, then point will be placed on the same
761 thread. Otherwise, point will be moved to attempt to be in the
762 same relative position within the new buffer."
765 (thread (notmuch-search-find-thread-id))
766 (query notmuch-search-query-string))
768 (notmuch-search query)
769 (goto-char (point-min))
770 (if (re-search-forward (concat "^" thread) nil t)
774 (defun notmuch-search-filter (query)
775 "Filter the current search results based on an additional query string.
777 Runs a new search matching only messages that match both the
778 current search results AND the additional query string provided."
779 (interactive "sFilter search: ")
780 (notmuch-search (concat notmuch-search-query-string " and " query)))
782 (defun notmuch-search-filter-by-tag (tag)
783 "Filter the current search results based on a single tag.
785 Runs a new search matching only messages that match both the
786 current search results AND that are tagged with the given tag."
787 (interactive "sFilter by tag: ")
788 (notmuch-search (concat notmuch-search-query-string " and tag:" tag)))
791 "Run notmuch to display all mail with tag of 'inbox'"
793 (notmuch-search "tag:inbox"))