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)
47 (define-key map "|" 'notmuch-show-pipe-message)
49 "Keymap for \"notmuch show\" buffers.")
50 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
52 (defvar notmuch-show-signature-lines-max 6
53 "Maximum length of signature that will be hidden by default.")
55 (set 'notmuch-show-message-begin-regexp "
\fmessage{")
56 (set 'notmuch-show-message-end-regexp "
\fmessage}")
57 (set 'notmuch-show-header-begin-regexp "
\fheader{")
58 (set 'notmuch-show-header-end-regexp "
\fheader}")
59 (set 'notmuch-show-body-begin-regexp "
\fbody{")
60 (set 'notmuch-show-body-end-regexp "
\fbody}")
61 (set 'notmuch-show-attachment-begin-regexp "
\fattachment{")
62 (set 'notmuch-show-attachment-end-regexp "
\fattachment}")
63 (set 'notmuch-show-part-begin-regexp "
\fpart{")
64 (set 'notmuch-show-part-end-regexp "
\fpart}")
65 (set 'notmuch-show-marker-regexp "
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
67 (set 'notmuch-show-id-regexp "ID: \\([^ ]*\\)")
68 (set 'notmuch-show-filename-regexp "Filename: \\(.*\\)$")
69 (set 'notmuch-show-tags-regexp "(\\([^)]*\\))$")
71 ; XXX: This should be a generic function in emacs somewhere, not here
72 (defun point-invisible-p ()
73 "Return whether the character at point is invisible.
75 Here visibility is determined by `buffer-invisibility-spec' and
76 the invisible property of any overlays for point. It doesn't have
77 anything to do with whether point is currently being displayed
78 within the current window."
79 (let ((prop (get-char-property (point) 'invisible)))
80 (if (eq buffer-invisibility-spec t)
82 (or (memq prop buffer-invisibility-spec)
83 (assq prop buffer-invisibility-spec)))))
85 (defun notmuch-show-next-line ()
86 "Like builtin `next-line' but ensuring we end on a visible character.
88 By advancing forward until reaching a visible character.
90 Unlike builtin `next-line' this version accepts no arguments."
92 (set 'this-command 'next-line)
93 (call-interactively 'next-line)
94 (while (point-invisible-p)
97 (defun notmuch-show-previous-line ()
98 "Like builtin `previous-line' but ensuring we end on a visible character.
100 By advancing forward until reaching a visible character.
102 Unlike builtin `next-line' this version accepts no arguments."
104 (set 'this-command 'previous-line)
105 (call-interactively 'previous-line)
106 (while (point-invisible-p)
109 (defun notmuch-show-get-message-id ()
112 (if (not (looking-at notmuch-show-message-begin-regexp))
113 (re-search-backward notmuch-show-message-begin-regexp))
114 (re-search-forward notmuch-show-id-regexp)
115 (buffer-substring (match-beginning 1) (match-end 1))))
117 (defun notmuch-show-get-filename ()
120 (if (not (looking-at notmuch-show-message-begin-regexp))
121 (re-search-backward notmuch-show-message-begin-regexp))
122 (re-search-forward notmuch-show-filename-regexp)
123 (buffer-substring (match-beginning 1) (match-end 1))))
125 (defun notmuch-show-set-tags (tags)
128 (if (not (looking-at notmuch-show-message-begin-regexp))
129 (re-search-backward notmuch-show-message-begin-regexp))
130 (re-search-forward notmuch-show-tags-regexp)
131 (let ((inhibit-read-only t)
132 (beg (match-beginning 1))
134 (delete-region beg end)
136 (insert (mapconcat 'identity tags " ")))))
138 (defun notmuch-show-get-tags ()
141 (if (not (looking-at notmuch-show-message-begin-regexp))
142 (re-search-backward notmuch-show-message-begin-regexp))
143 (re-search-forward notmuch-show-tags-regexp)
144 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
146 (defun notmuch-show-add-tag (tag)
147 (interactive "sTag to add: ")
148 (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "id:" (notmuch-show-get-message-id)))
149 (notmuch-show-set-tags (delete-dups (sort (cons tag (notmuch-show-get-tags)) 'string<))))
151 (defun notmuch-show-remove-tag (tag)
152 (interactive "sTag to remove: ")
153 (let ((tags (notmuch-show-get-tags)))
154 (if (member tag tags)
156 (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "id:" (notmuch-show-get-message-id)))
157 (notmuch-show-set-tags (delete tag tags))))))
159 (defun notmuch-show-archive-thread ()
160 "Archive each message in thread, and show next thread from search.
162 Archive each message currrently shown by removing the \"inbox\"
163 tag from each. Then kill this buffer and show the next thread
164 from the search from which this thread was originally shown.
166 Note: This command is safe from any race condition of new messages
167 being delivered to the same thread. It does not archive the
168 entire thread, but only the messages shown in the current
172 (goto-char (point-min))
174 (notmuch-show-remove-tag "inbox")
177 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
178 (goto-char (point-max)))))
179 (let ((parent-buffer notmuch-show-parent-buffer))
183 (switch-to-buffer parent-buffer)
184 (notmuch-search-show-thread)))))
186 (defun notmuch-show-view-raw-message ()
187 "View the raw email of the current message."
189 (view-file (notmuch-show-get-filename)))
191 (defun notmuch-show-pipe-message (command)
192 "Pipe the contents of the current message to the given command.
194 The given command will be executed with the raw contents of the
195 current email message as stdin. Anything printed by the command
196 to stdout or stderr will appear in the *Messages* buffer."
197 (interactive "sPipe message to command: ")
198 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*" (split-string (concat command " < " (notmuch-show-get-filename)))))
200 (defun notmuch-show-move-to-current-message-summary-line ()
201 "Move to the beginning of the one-line summary of the current message.
203 This gives us a stable place to move to and work from since the
204 summary line is always visible. This is important since moving to
205 an invisible location is unreliable, (the main command loop moves
206 point either forward or backward to the next visible character
207 when a command ends with point on an invisible character).
209 Emits an error if point is not within a valid message, (that is
210 not pattern of `notmuch-show-message-begin-regexp' could be found
211 by searching backward)."
213 (if (not (looking-at notmuch-show-message-begin-regexp))
214 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
216 (error "Not within a valid message."))
219 (defun notmuch-show-last-message-p ()
220 "Predicate testing whether point is within the last message."
221 (save-window-excursion
223 (notmuch-show-move-to-current-message-summary-line)
224 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
226 (defun notmuch-show-message-unread-p ()
227 "Preficate testing whether current message is unread."
228 (member "unread" (notmuch-show-get-tags)))
230 (defun notmuch-show-next-message ()
231 "Advance to the beginning of the next message in the buffer.
233 Moves to the last visible character of the current message if
234 already on the last message in the buffer."
236 (notmuch-show-move-to-current-message-summary-line)
237 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
238 (notmuch-show-move-to-current-message-summary-line)
239 (goto-char (- (point-max) 1))
240 (while (point-invisible-p)
244 (defun notmuch-show-find-next-message ()
245 "Returns the position of the next message in the buffer.
247 Or the position of the last visible character of the current
248 message if already within the last message in the buffer."
249 ; save-excursion doesn't save our window position
250 ; save-window-excursion doesn't save point
251 ; Looks like we have to use both.
253 (save-window-excursion
254 (notmuch-show-next-message)
257 (defun notmuch-show-next-unread-message ()
258 "Advance to the beginning of the next unread message in the buffer.
260 Moves to the last visible character of the current message if
261 there are no more unread messages past the current point."
262 (notmuch-show-next-message)
263 (while (and (not (notmuch-show-last-message-p))
264 (not (notmuch-show-message-unread-p)))
265 (notmuch-show-next-message))
266 (if (not (notmuch-show-message-unread-p))
267 (notmuch-show-next-message)))
269 (defun notmuch-show-next-open-message ()
270 "Advance to the the next message which is not hidden.
272 If read messages are currently hidden, advance to the next unread
273 message. Otherwise, advance to the next message."
274 (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
275 (assq 'notmuch-show-body-read buffer-invisibility-spec))
276 (notmuch-show-next-unread-message)
277 (notmuch-show-next-message)))
279 (defun notmuch-show-previous-message ()
280 "Backup to the beginning of the previous message in the buffer.
282 If within a message rather than at the beginning of it, then
283 simply move to the beginning of the current message."
285 (let ((start (point)))
286 (notmuch-show-move-to-current-message-summary-line)
287 (if (not (< (point) start))
288 ; Go backward twice to skip the current message's marker
290 (re-search-backward notmuch-show-message-begin-regexp nil t)
291 (re-search-backward notmuch-show-message-begin-regexp nil t)
292 (notmuch-show-move-to-current-message-summary-line)
296 (defun notmuch-show-find-previous-message ()
297 "Returns the position of the previous message in the buffer.
299 Or the position of the beginning of the current message if point
300 is originally within the message rather than at the beginning of
302 ; save-excursion doesn't save our window position
303 ; save-window-excursion doesn't save point
304 ; Looks like we have to use both.
306 (save-window-excursion
307 (notmuch-show-previous-message)
310 (defun notmuch-show-mark-read-then-next-open-message ()
311 "Remove unread tag from current message, then advance to next unread message."
313 (notmuch-show-remove-tag "unread")
314 (notmuch-show-next-open-message))
316 (defun notmuch-show-rewind ()
317 "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
319 Specifically, if the beginning of the previous email is fewer
320 than `window-height' lines from the current point, move to it
321 just like `notmuch-show-previous-message'.
323 Otherwise, just scroll down a screenful of the current message.
325 This command does not modify any message tags, (it does not undo
326 any effects from previous calls to
327 `notmuch-show-advance-marking-read-and-archiving'."
329 (let ((previous (notmuch-show-find-previous-message)))
330 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
334 ((beginning-of-buffer) nil))
335 (goto-char (window-start)))
336 (notmuch-show-previous-message))))
338 (defun notmuch-show-advance-marking-read-and-archiving ()
339 "Advance through buffer, marking read and archiving.
341 This command is intended to be one of the simplest ways to
342 process a thread of email. It does the following:
344 If the current message in the thread is not yet fully visible,
345 scroll by a near screenful to read more of the message.
347 Otherwise, (the end of the current message is already within the
348 current window), remove the \"unread\" tag (if present) from the
349 current message and advance to the next open message.
351 Finally, if there is no further message to advance to, and this
352 last message is already read, then archive the entire current
353 thread, (remove the \"inbox\" tag from each message). Also kill
354 this buffer, and display the next thread from the search from
355 which this thread was originally shown."
357 (let ((next (notmuch-show-find-next-message))
358 (unread (notmuch-show-message-unread-p)))
359 (if (> next (window-end))
362 (notmuch-show-mark-read-then-next-open-message)
363 (if (notmuch-show-last-message-p)
364 (notmuch-show-archive-thread)
365 (notmuch-show-next-open-message))))))
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"))