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>
24 (defvar notmuch-show-mode-map
25 (let ((map (make-sparse-keymap)))
26 ; I don't actually want all of these toggle commands occupying
27 ; keybindings. They steal valuable key-binding space, are hard
28 ; to remember, and act globally rather than locally.
30 ; Will be much preferable to switch to direct manipulation for
31 ; toggling visibility of these components. Probably using
32 ; overlays-at to query and manipulate the current overlay.
33 (define-key map "a" 'notmuch-show-archive-thread)
34 (define-key map "A" 'notmuch-show-mark-read-then-archive-thread)
35 (define-key map "b" 'notmuch-show-toggle-body-read-visible)
36 (define-key map "c" 'notmuch-show-toggle-citations-visible)
37 (define-key map "h" 'notmuch-show-toggle-headers-visible)
38 (define-key map "n" 'notmuch-show-next-message)
39 (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
40 (define-key map "p" 'notmuch-show-previous-message)
41 (define-key map (kbd "C-n") 'notmuch-show-next-line)
42 (define-key map (kbd "C-p") 'notmuch-show-previous-line)
43 (define-key map "q" 'kill-this-buffer)
44 (define-key map "r" 'notmuch-show-reply)
45 (define-key map "s" 'notmuch-show-toggle-signatures-visible)
46 (define-key map "w" 'notmuch-show-view-raw-message)
47 (define-key map "x" 'kill-this-buffer)
48 (define-key map "+" 'notmuch-show-add-tag)
49 (define-key map "-" 'notmuch-show-remove-tag)
50 (define-key map (kbd "DEL") 'notmuch-show-rewind)
51 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
52 (define-key map "|" 'notmuch-show-pipe-message)
54 "Keymap for \"notmuch show\" buffers.")
55 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
57 (defvar notmuch-show-signature-lines-max 6
58 "Maximum length of signature that will be hidden by default.")
60 (set 'notmuch-show-message-begin-regexp "
\fmessage{")
61 (set 'notmuch-show-message-end-regexp "
\fmessage}")
62 (set 'notmuch-show-header-begin-regexp "
\fheader{")
63 (set 'notmuch-show-header-end-regexp "
\fheader}")
64 (set 'notmuch-show-body-begin-regexp "
\fbody{")
65 (set 'notmuch-show-body-end-regexp "
\fbody}")
66 (set 'notmuch-show-attachment-begin-regexp "
\fattachment{")
67 (set 'notmuch-show-attachment-end-regexp "
\fattachment}")
68 (set 'notmuch-show-part-begin-regexp "
\fpart{")
69 (set 'notmuch-show-part-end-regexp "
\fpart}")
70 (set 'notmuch-show-marker-regexp "
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
72 (set 'notmuch-show-id-regexp "\\(id:[^ ]*\\)")
73 (set 'notmuch-show-filename-regexp "filename:\\(.*\\)$")
74 (set 'notmuch-show-tags-regexp "(\\([^)]*\\))$")
76 ; XXX: This should be a generic function in emacs somewhere, not here
77 (defun point-invisible-p ()
78 "Return whether the character at point is invisible.
80 Here visibility is determined by `buffer-invisibility-spec' and
81 the invisible property of any overlays for point. It doesn't have
82 anything to do with whether point is currently being displayed
83 within the current window."
84 (let ((prop (get-char-property (point) 'invisible)))
85 (if (eq buffer-invisibility-spec t)
87 (or (memq prop buffer-invisibility-spec)
88 (assq prop buffer-invisibility-spec)))))
90 (defun notmuch-show-next-line ()
91 "Like builtin `next-line' but ensuring we end on a visible character.
93 By advancing forward until reaching a visible character.
95 Unlike builtin `next-line' this version accepts no arguments."
97 (set 'this-command 'next-line)
98 (call-interactively 'next-line)
99 (while (point-invisible-p)
102 (defun notmuch-show-previous-line ()
103 "Like builtin `previous-line' but ensuring we end on a visible character.
105 By advancing forward until reaching a visible character.
107 Unlike builtin `next-line' this version accepts no arguments."
109 (set 'this-command 'previous-line)
110 (call-interactively 'previous-line)
111 (while (point-invisible-p)
114 (defun notmuch-show-get-message-id ()
117 (if (not (looking-at notmuch-show-message-begin-regexp))
118 (re-search-backward notmuch-show-message-begin-regexp))
119 (re-search-forward notmuch-show-id-regexp)
120 (buffer-substring (match-beginning 1) (match-end 1))))
122 (defun notmuch-show-get-filename ()
125 (if (not (looking-at notmuch-show-message-begin-regexp))
126 (re-search-backward notmuch-show-message-begin-regexp))
127 (re-search-forward notmuch-show-filename-regexp)
128 (buffer-substring (match-beginning 1) (match-end 1))))
130 (defun notmuch-show-set-tags (tags)
133 (if (not (looking-at notmuch-show-message-begin-regexp))
134 (re-search-backward notmuch-show-message-begin-regexp))
135 (re-search-forward notmuch-show-tags-regexp)
136 (let ((inhibit-read-only t)
137 (beg (match-beginning 1))
139 (delete-region beg end)
141 (insert (mapconcat 'identity tags " ")))))
143 (defun notmuch-show-get-tags ()
146 (if (not (looking-at notmuch-show-message-begin-regexp))
147 (re-search-backward notmuch-show-message-begin-regexp))
148 (re-search-forward notmuch-show-tags-regexp)
149 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
151 (defun notmuch-show-add-tag (&rest toadd)
152 "Add a tag to the current message."
153 (interactive "sTag to add: ")
154 (apply 'notmuch-call-notmuch-process
156 (mapcar (lambda (s) (concat "+" s)) toadd))
157 (cons (notmuch-show-get-message-id) nil)))
158 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
160 (defun notmuch-show-remove-tag (&rest toremove)
161 "Remove a tag from the current message."
162 (interactive "sTag to remove: ")
163 (let ((tags (notmuch-show-get-tags)))
164 (if (intersection tags toremove :test 'string=)
166 (apply 'notmuch-call-notmuch-process
168 (mapcar (lambda (s) (concat "-" s)) toremove))
169 (cons (notmuch-show-get-message-id) nil)))
170 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
172 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
174 (goto-char (point-min))
177 (notmuch-show-remove-tag "unread" "inbox")
178 (notmuch-show-remove-tag "inbox"))
181 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
182 (goto-char (point-max)))))
183 (let ((parent-buffer notmuch-show-parent-buffer))
187 (switch-to-buffer parent-buffer)
189 (notmuch-search-show-thread)))))
191 (defun notmuch-show-mark-read-then-archive-thread ()
192 "Remove \"unread\" tag from each message, then archive and show next thread.
194 Archive each message currrently shown by removing the \"unread\"
195 and \"inbox\" tag from each. Then kill this buffer and show the
196 next thread from the search from which this thread was originally
199 Note: This command is safe from any race condition of new messages
200 being delivered to the same thread. It does not archive the
201 entire thread, but only the messages shown in the current
204 (notmuch-show-archive-thread-maybe-mark-read t))
206 (defun notmuch-show-archive-thread ()
207 "Archive each message in thread, and show next thread from search.
209 Archive each message currrently shown by removing the \"inbox\"
210 tag from each. Then kill this buffer and show the next thread
211 from the search from which this thread was originally shown.
213 Note: This command is safe from any race condition of new messages
214 being delivered to the same thread. It does not archive the
215 entire thread, but only the messages shown in the current
218 (notmuch-show-archive-thread-maybe-mark-read nil))
220 (defun notmuch-show-view-raw-message ()
221 "View the raw email of the current message."
223 (view-file (notmuch-show-get-filename)))
225 (defun notmuch-show-reply ()
226 "Begin composing a reply to the current message in a new buffer."
228 (let ((message-id (notmuch-show-get-message-id)))
229 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
230 (call-process "notmuch" nil t nil "reply" message-id)
231 (goto-char (point-min))
232 (if (re-search-forward "^$" nil t)
234 (insert "--text follows this line--")
238 (defun notmuch-show-pipe-message (command)
239 "Pipe the contents of the current message to the given command.
241 The given command will be executed with the raw contents of the
242 current email message as stdin. Anything printed by the command
243 to stdout or stderr will appear in the *Messages* buffer."
244 (interactive "sPipe message to command: ")
245 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*" (split-string (concat command " < " (notmuch-show-get-filename)))))
247 (defun notmuch-show-move-to-current-message-summary-line ()
248 "Move to the beginning of the one-line summary of the current message.
250 This gives us a stable place to move to and work from since the
251 summary line is always visible. This is important since moving to
252 an invisible location is unreliable, (the main command loop moves
253 point either forward or backward to the next visible character
254 when a command ends with point on an invisible character).
256 Emits an error if point is not within a valid message, (that is
257 not pattern of `notmuch-show-message-begin-regexp' could be found
258 by searching backward)."
260 (if (not (looking-at notmuch-show-message-begin-regexp))
261 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
263 (error "Not within a valid message."))
266 (defun notmuch-show-last-message-p ()
267 "Predicate testing whether point is within the last message."
268 (save-window-excursion
270 (notmuch-show-move-to-current-message-summary-line)
271 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
273 (defun notmuch-show-message-unread-p ()
274 "Preficate testing whether current message is unread."
275 (member "unread" (notmuch-show-get-tags)))
277 (defun notmuch-show-next-message ()
278 "Advance to the beginning of the next message in the buffer.
280 Moves to the last visible character of the current message if
281 already on the last message in the buffer."
283 (notmuch-show-move-to-current-message-summary-line)
284 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
285 (notmuch-show-move-to-current-message-summary-line)
286 (goto-char (- (point-max) 1))
287 (while (point-invisible-p)
291 (defun notmuch-show-find-next-message ()
292 "Returns the position of the next message in the buffer.
294 Or the position of the last visible character of the current
295 message if already within the last message in the buffer."
296 ; save-excursion doesn't save our window position
297 ; save-window-excursion doesn't save point
298 ; Looks like we have to use both.
300 (save-window-excursion
301 (notmuch-show-next-message)
304 (defun notmuch-show-next-unread-message ()
305 "Advance to the beginning of the next unread message in the buffer.
307 Moves to the last visible character of the current message if
308 there are no more unread messages past the current point."
309 (notmuch-show-next-message)
310 (while (and (not (notmuch-show-last-message-p))
311 (not (notmuch-show-message-unread-p)))
312 (notmuch-show-next-message))
313 (if (not (notmuch-show-message-unread-p))
314 (notmuch-show-next-message)))
316 (defun notmuch-show-next-open-message ()
317 "Advance to the the next message which is not hidden.
319 If read messages are currently hidden, advance to the next unread
320 message. Otherwise, advance to the next message."
321 (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
322 (assq 'notmuch-show-body-read buffer-invisibility-spec))
323 (notmuch-show-next-unread-message)
324 (notmuch-show-next-message)))
326 (defun notmuch-show-previous-message ()
327 "Backup to the beginning of the previous message in the buffer.
329 If within a message rather than at the beginning of it, then
330 simply move to the beginning of the current message."
332 (let ((start (point)))
333 (notmuch-show-move-to-current-message-summary-line)
334 (if (not (< (point) start))
335 ; Go backward twice to skip the current message's marker
337 (re-search-backward notmuch-show-message-begin-regexp nil t)
338 (re-search-backward notmuch-show-message-begin-regexp nil t)
339 (notmuch-show-move-to-current-message-summary-line)
343 (defun notmuch-show-find-previous-message ()
344 "Returns the position of the previous message in the buffer.
346 Or the position of the beginning of the current message if point
347 is originally within the message rather than at the beginning of
349 ; save-excursion doesn't save our window position
350 ; save-window-excursion doesn't save point
351 ; Looks like we have to use both.
353 (save-window-excursion
354 (notmuch-show-previous-message)
357 (defun notmuch-show-mark-read-then-next-open-message ()
358 "Remove unread tag from current message, then advance to next unread message."
360 (notmuch-show-remove-tag "unread")
361 (notmuch-show-next-open-message))
363 (defun notmuch-show-rewind ()
364 "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
366 Specifically, if the beginning of the previous email is fewer
367 than `window-height' lines from the current point, move to it
368 just like `notmuch-show-previous-message'.
370 Otherwise, just scroll down a screenful of the current message.
372 This command does not modify any message tags, (it does not undo
373 any effects from previous calls to
374 `notmuch-show-advance-marking-read-and-archiving'."
376 (let ((previous (notmuch-show-find-previous-message)))
377 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
381 ((beginning-of-buffer) nil))
382 (goto-char (window-start)))
383 (notmuch-show-previous-message))))
385 (defun notmuch-show-advance-marking-read-and-archiving ()
386 "Advance through buffer, marking read and archiving.
388 This command is intended to be one of the simplest ways to
389 process a thread of email. It does the following:
391 If the current message in the thread is not yet fully visible,
392 scroll by a near screenful to read more of the message.
394 Otherwise, (the end of the current message is already within the
395 current window), remove the \"unread\" tag (if present) from the
396 current message and advance to the next open message.
398 Finally, if there is no further message to advance to, and this
399 last message is already read, then archive the entire current
400 thread, (remove the \"inbox\" tag from each message). Also kill
401 this buffer, and display the next thread from the search from
402 which this thread was originally shown."
404 (let ((next (notmuch-show-find-next-message))
405 (unread (notmuch-show-message-unread-p)))
406 (if (> next (window-end))
408 (let ((last (notmuch-show-last-message-p)))
409 (notmuch-show-mark-read-then-next-open-message)
411 (notmuch-show-archive-thread))))))
413 (defun notmuch-show-markup-citations-region (beg end)
416 (while (< (point) end)
417 (let ((beg-sub (point)))
420 (while (looking-at ">")
422 (let ((overlay (make-overlay beg-sub (point))))
423 (overlay-put overlay 'invisible 'notmuch-show-citation)
424 (overlay-put overlay 'before-string
425 (concat "[" (number-to-string (count-lines beg-sub (point)))
426 "-line citation. Press 'c' to show.]\n")))))
427 (if (looking-at "--[ ]?$")
428 (let ((sig-lines (count-lines beg-sub end)))
429 (if (<= sig-lines notmuch-show-signature-lines-max)
431 (overlay-put (make-overlay beg-sub (+ beg-sub 1))
433 (concat "[" (number-to-string sig-lines)
434 "-line signature. Press 's' to show.]"))
435 (overlay-put (make-overlay (+ beg-sub 2) end)
436 'invisible 'notmuch-show-signature)
440 (defun notmuch-show-markup-body ()
441 (re-search-forward notmuch-show-body-begin-regexp)
445 (re-search-forward notmuch-show-body-end-regexp)
446 (let ((end (match-beginning 0)))
447 (notmuch-show-markup-citations-region beg end)
448 (if (not (notmuch-show-message-unread-p))
449 (overlay-put (make-overlay beg end)
450 'invisible 'notmuch-show-body-read)))))
452 (defun notmuch-show-markup-header ()
453 (re-search-forward notmuch-show-header-begin-regexp)
458 ; Inverse video for subject
459 (overlay-put (make-overlay beg (point)) 'face '((cons :inverse-video t)))
463 (re-search-forward notmuch-show-header-end-regexp)
464 (overlay-put (make-overlay beg (match-beginning 0))
465 'invisible 'notmuch-show-header)))
467 (defun notmuch-show-markup-message ()
468 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
470 (notmuch-show-markup-header)
471 (notmuch-show-markup-body))
472 (goto-char (point-max))))
474 (defun notmuch-show-hide-markers ()
476 (goto-char (point-min))
478 (if (re-search-forward notmuch-show-marker-regexp nil t)
480 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
481 'invisible 'notmuch-show-marker))
482 (goto-char (point-max))))))
484 (defun notmuch-show-markup-messages ()
486 (goto-char (point-min))
488 (notmuch-show-markup-message)))
489 (notmuch-show-hide-markers))
491 (defun notmuch-show-toggle-citations-visible ()
492 "Toggle visibility of citations"
494 (if notmuch-show-citations-visible
495 (add-to-invisibility-spec 'notmuch-show-citation)
496 (remove-from-invisibility-spec 'notmuch-show-citation))
497 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
498 ; Need to force the redisplay for some reason
499 (force-window-update)
502 (defun notmuch-show-toggle-signatures-visible ()
503 "Toggle visibility of signatures"
505 (if notmuch-show-signatures-visible
506 (add-to-invisibility-spec 'notmuch-show-signature)
507 (remove-from-invisibility-spec 'notmuch-show-signature))
508 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
509 ; Need to force the redisplay for some reason
510 (force-window-update)
513 (defun notmuch-show-toggle-headers-visible ()
514 "Toggle visibility of header fields"
516 (if notmuch-show-headers-visible
517 (add-to-invisibility-spec 'notmuch-show-header)
518 (remove-from-invisibility-spec 'notmuch-show-header))
519 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
520 ; Need to force the redisplay for some reason
521 (force-window-update)
524 (defun notmuch-show-toggle-body-read-visible ()
525 "Toggle visibility of message bodies of read messages"
527 (if notmuch-show-body-read-visible
528 (add-to-invisibility-spec 'notmuch-show-body-read)
529 (remove-from-invisibility-spec 'notmuch-show-body-read))
530 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
531 ; Need to force the redisplay for some reason
532 (force-window-update)
536 (defun notmuch-show-mode ()
537 "Major mode for viewing a thread with notmuch.
539 This buffer contains the results of the \"notmuch show\" command
540 for displaying a single thread of email from your email archives.
542 By default, various components of email messages, (citations,
543 signatures, already-read messages), are invisible to help you
544 focus on the most important things, (new text from unread
545 messages). See the various commands below for toggling the
546 visibility of hidden components.
548 The `notmuch-show-next-message' and
549 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
550 default), allow you to navigate to the next and previous
551 messages. Each time you navigate away from a message with
552 `notmuch-show-next-message' the current message will have its
553 \"unread\" tag removed.
555 You can add or remove tags from the current message with '+' and
556 '-'. You can also archive all messages in the current
557 view, (remove the \"inbox\" tag from each), with
558 `notmuch-show-archive-thread' (bound to 'a' by default).
560 \\{notmuch-show-mode-map}"
562 (kill-all-local-variables)
563 (set (make-local-variable 'notmuch-show-headers-visible) t)
564 (notmuch-show-toggle-headers-visible)
565 (set (make-local-variable 'notmuch-show-body-read-visible) t)
566 (notmuch-show-toggle-body-read-visible)
567 (set (make-local-variable 'notmuch-show-citations-visible) t)
568 (notmuch-show-toggle-citations-visible)
569 (set (make-local-variable 'notmuch-show-signatures-visible) t)
570 (notmuch-show-toggle-signatures-visible)
571 (add-to-invisibility-spec 'notmuch-show-marker)
572 (use-local-map notmuch-show-mode-map)
573 (setq major-mode 'notmuch-show-mode
574 mode-name "notmuch-show")
575 (setq buffer-read-only t))
577 (defun notmuch-show (thread-id &optional parent-buffer)
578 "Run \"notmuch show\" with the given thread ID and display results.
580 The optional PARENT-BUFFER is the notmuch-search buffer from
581 which this notmuch-show command was executed, (so that the next
582 thread from that buffer can be show when done with this one)."
583 (interactive "sNotmuch show: ")
584 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
585 (switch-to-buffer buffer)
587 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
588 (let ((proc (get-buffer-process (current-buffer)))
589 (inhibit-read-only t))
591 (error "notmuch search process already running for query `%s'" query)
594 (goto-char (point-min))
596 (call-process "notmuch" nil t nil "show" thread-id)
597 (notmuch-show-markup-messages)
599 ; Move straight to the first unread message
600 (if (not (notmuch-show-message-unread-p))
602 (notmuch-show-next-unread-message)
603 ; But if there are no unread messages, go back to the
604 ; beginning of the buffer, and open up the bodies of all
606 (if (not (notmuch-show-message-unread-p))
608 (goto-char (point-min))
609 (notmuch-show-toggle-body-read-visible)))))
612 (defvar notmuch-search-mode-map
613 (let ((map (make-sparse-keymap)))
614 (define-key map "a" 'notmuch-search-archive-thread)
615 (define-key map "b" 'notmuch-search-scroll-down)
616 (define-key map "f" 'notmuch-search-filter)
617 (define-key map "n" 'next-line)
618 (define-key map "p" 'previous-line)
619 (define-key map "q" 'kill-this-buffer)
620 (define-key map "s" 'notmuch-search)
621 (define-key map "t" 'notmuch-search-filter-by-tag)
622 (define-key map "x" 'kill-this-buffer)
623 (define-key map (kbd "RET") 'notmuch-search-show-thread)
624 (define-key map "+" 'notmuch-search-add-tag)
625 (define-key map "-" 'notmuch-search-remove-tag)
626 (define-key map "<" 'beginning-of-buffer)
627 (define-key map ">" 'notmuch-search-goto-last-thread)
628 (define-key map "=" 'notmuch-search-refresh-view)
629 (define-key map "\M->" 'notmuch-search-goto-last-thread)
630 (define-key map " " 'notmuch-search-scroll-up)
631 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
633 "Keymap for \"notmuch search\" buffers.")
634 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
636 (defun notmuch-search-scroll-up ()
637 "Scroll up, moving point to last message in thread if at end."
641 ((end-of-buffer) (notmuch-search-goto-last-thread))))
643 (defun notmuch-search-scroll-down ()
644 "Scroll down, moving point to first message in thread if at beginning."
646 ; I don't know why scroll-down doesn't signal beginning-of-buffer
647 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
649 ; So instead of trapping a signal we instead check whether the
650 ; window begins on the first line of the buffer and if so, move
651 ; directly to that position. (We have to count lines since the
652 ; window-start position is not the same as point-min due to the
653 ; invisible thread-ID characters on the first line.
654 (if (equal (count-lines (point-min) (window-start)) 1)
655 (goto-char (window-start))
658 (defun notmuch-search-goto-last-thread (&optional arg)
659 "Move point to the last thread in the buffer."
665 (defun notmuch-search-mode ()
666 "Major mode for searching mail with notmuch.
668 This buffer contains the results of a \"notmuch search\" of your
669 email archives. Each line in the buffer represents a single
670 thread giving a relative date for the thread and a subject.
672 Pressing RET on any line displays that thread. The '+' and '-'
673 keys can be used to add or remove tags from a thread. The 'a' key
674 is a convenience key for archiving a thread (removing the
677 Other useful commands are `notmuch-search-filter' for filtering
678 the current search based on an additional query string,
679 `notmuch-search-filter-by-tag' for filtering to include only
680 messages with a given tag, and `notmuch-search' to execute a new,
683 \\{notmuch-search-mode-map}"
685 (kill-all-local-variables)
686 (make-local-variable 'notmuch-search-query-string)
687 (set (make-local-variable 'scroll-preserve-screen-position) t)
688 (add-to-invisibility-spec 'notmuch-search)
689 (use-local-map notmuch-search-mode-map)
690 (setq major-mode 'notmuch-search-mode
691 mode-name "notmuch-search")
692 (setq buffer-read-only t))
694 (defun notmuch-search-find-thread-id ()
698 (re-search-forward "thread:[a-fA-F0-9]*" nil t)
699 (filter-buffer-substring beg (point)))))
701 (defun notmuch-search-markup-this-thread-id ()
704 (if (re-search-forward "thread:[a-fA-F0-9]*" nil t)
707 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))))
709 (defun notmuch-search-markup-thread-ids ()
711 (goto-char (point-min))
713 (notmuch-search-markup-this-thread-id)
716 (defun notmuch-search-hide-thread-ids ()
718 (add-to-invisibility-spec 'notmuch-search)
719 (force-window-update)
722 (defun notmuch-search-show-thread-ids ()
724 (remove-from-invisibility-spec 'notmuch-search)
725 (force-window-update)
728 (defun notmuch-search-show-thread ()
730 (let ((thread-id (notmuch-search-find-thread-id)))
731 (if (> (length thread-id) 0)
732 (notmuch-show thread-id (current-buffer))
733 (error "End of search results"))))
735 (defun notmuch-call-notmuch-process (&rest args)
736 "Synchronously invoke \"notmuch\" with the given list of arguments.
738 Output from the process will be presented to the user as an error
739 and will also appear in a buffer named \"*Notmuch errors*\"."
740 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
741 (with-current-buffer error-buffer
743 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
746 (with-current-buffer error-buffer
747 (let ((beg (point-min))
748 (end (- (point-max) 1)))
749 (error (buffer-substring beg end))
752 (defun notmuch-search-set-tags (tags)
755 (re-search-backward "(")
758 (inhibit-read-only t))
759 (re-search-forward ")")
762 (delete-region beg end)
763 (insert (mapconcat 'identity tags " "))))))
765 (defun notmuch-search-get-tags ()
768 (re-search-backward "(")
769 (let ((beg (+ (point) 1)))
770 (re-search-forward ")")
771 (let ((end (- (point) 1)))
772 (split-string (buffer-substring beg end))))))
774 (defun notmuch-search-add-tag (tag)
775 (interactive "sTag to add: ")
776 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
777 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
779 (defun notmuch-search-remove-tag (tag)
780 (interactive "sTag to remove: ")
781 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
782 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
784 (defun notmuch-search-archive-thread ()
785 "Archive the current thread (remove its \"inbox\" tag).
787 This function advances point to the next line when finished."
789 (notmuch-search-remove-tag "inbox")
792 (defun notmuch-search (query)
793 "Run \"notmuch search\" with the given query string and display results."
794 (interactive "sNotmuch search: ")
795 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
796 (switch-to-buffer buffer)
797 (notmuch-search-mode)
798 (set 'notmuch-search-query-string query)
799 (let ((proc (get-buffer-process (current-buffer)))
800 (inhibit-read-only t))
802 (error "notmuch search process already running for query `%s'" query)
805 (goto-char (point-min))
807 (call-process "notmuch" nil t nil "search" query)
808 (notmuch-search-markup-thread-ids)
811 (defun notmuch-search-refresh-view ()
812 "Refresh the current view.
814 Kills the current buffer and runs a new search with the same
815 query string as the current search. If the current thread is in
816 the new search results, then point will be placed on the same
817 thread. Otherwise, point will be moved to attempt to be in the
818 same relative position within the new buffer."
821 (thread (notmuch-search-find-thread-id))
822 (query notmuch-search-query-string))
824 (notmuch-search query)
825 (goto-char (point-min))
826 (if (re-search-forward (concat "^" thread) nil t)
830 (defun notmuch-search-filter (query)
831 "Filter the current search results based on an additional query string.
833 Runs a new search matching only messages that match both the
834 current search results AND the additional query string provided."
835 (interactive "sFilter search: ")
836 (notmuch-search (concat notmuch-search-query-string " and " query)))
838 (defun notmuch-search-filter-by-tag (tag)
839 "Filter the current search results based on a single tag.
841 Runs a new search matching only messages that match both the
842 current search results AND that are tagged with the given tag."
843 (interactive "sFilter by tag: ")
844 (notmuch-search (concat notmuch-search-query-string " and tag:" tag)))
847 "Run notmuch to display all mail with tag of 'inbox'"
849 (notmuch-search "tag:inbox"))