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 "m" 'message-mail)
39 (define-key map "n" 'notmuch-show-next-message)
40 (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
41 (define-key map "p" 'notmuch-show-previous-message)
42 (define-key map (kbd "C-n") 'notmuch-show-next-line)
43 (define-key map (kbd "C-p") 'notmuch-show-previous-line)
44 (define-key map "q" 'kill-this-buffer)
45 (define-key map "r" 'notmuch-show-reply)
46 (define-key map "s" 'notmuch-show-toggle-signatures-visible)
47 (define-key map "w" 'notmuch-show-view-raw-message)
48 (define-key map "x" 'kill-this-buffer)
49 (define-key map "+" 'notmuch-show-add-tag)
50 (define-key map "-" 'notmuch-show-remove-tag)
51 (define-key map (kbd "DEL") 'notmuch-show-rewind)
52 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
53 (define-key map "|" 'notmuch-show-pipe-message)
55 "Keymap for \"notmuch show\" buffers.")
56 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
58 (defvar notmuch-show-signature-lines-max 6
59 "Maximum length of signature that will be hidden by default.")
61 (set 'notmuch-show-message-begin-regexp "
\fmessage{")
62 (set 'notmuch-show-message-end-regexp "
\fmessage}")
63 (set 'notmuch-show-header-begin-regexp "
\fheader{")
64 (set 'notmuch-show-header-end-regexp "
\fheader}")
65 (set 'notmuch-show-body-begin-regexp "
\fbody{")
66 (set 'notmuch-show-body-end-regexp "
\fbody}")
67 (set 'notmuch-show-attachment-begin-regexp "
\fattachment{")
68 (set 'notmuch-show-attachment-end-regexp "
\fattachment}")
69 (set 'notmuch-show-part-begin-regexp "
\fpart{")
70 (set 'notmuch-show-part-end-regexp "
\fpart}")
71 (set 'notmuch-show-marker-regexp "
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
73 (set 'notmuch-show-id-regexp "\\(id:[^ ]*\\)")
74 (set 'notmuch-show-filename-regexp "filename:\\(.*\\)$")
75 (set 'notmuch-show-tags-regexp "(\\([^)]*\\))$")
77 ; XXX: This should be a generic function in emacs somewhere, not here
78 (defun point-invisible-p ()
79 "Return whether the character at point is invisible.
81 Here visibility is determined by `buffer-invisibility-spec' and
82 the invisible property of any overlays for point. It doesn't have
83 anything to do with whether point is currently being displayed
84 within the current window."
85 (let ((prop (get-char-property (point) 'invisible)))
86 (if (eq buffer-invisibility-spec t)
88 (or (memq prop buffer-invisibility-spec)
89 (assq prop buffer-invisibility-spec)))))
91 (defun notmuch-show-next-line ()
92 "Like builtin `next-line' but ensuring we end on a visible character.
94 By advancing forward until reaching a visible character.
96 Unlike builtin `next-line' this version accepts no arguments."
98 (set 'this-command 'next-line)
99 (call-interactively 'next-line)
100 (while (point-invisible-p)
103 (defun notmuch-show-previous-line ()
104 "Like builtin `previous-line' but ensuring we end on a visible character.
106 By advancing forward until reaching a visible character.
108 Unlike builtin `next-line' this version accepts no arguments."
110 (set 'this-command 'previous-line)
111 (call-interactively 'previous-line)
112 (while (point-invisible-p)
115 (defun notmuch-show-get-message-id ()
118 (if (not (looking-at notmuch-show-message-begin-regexp))
119 (re-search-backward notmuch-show-message-begin-regexp))
120 (re-search-forward notmuch-show-id-regexp)
121 (buffer-substring (match-beginning 1) (match-end 1))))
123 (defun notmuch-show-get-filename ()
126 (if (not (looking-at notmuch-show-message-begin-regexp))
127 (re-search-backward notmuch-show-message-begin-regexp))
128 (re-search-forward notmuch-show-filename-regexp)
129 (buffer-substring (match-beginning 1) (match-end 1))))
131 (defun notmuch-show-set-tags (tags)
134 (if (not (looking-at notmuch-show-message-begin-regexp))
135 (re-search-backward notmuch-show-message-begin-regexp))
136 (re-search-forward notmuch-show-tags-regexp)
137 (let ((inhibit-read-only t)
138 (beg (match-beginning 1))
140 (delete-region beg end)
142 (insert (mapconcat 'identity tags " ")))))
144 (defun notmuch-show-get-tags ()
147 (if (not (looking-at notmuch-show-message-begin-regexp))
148 (re-search-backward notmuch-show-message-begin-regexp))
149 (re-search-forward notmuch-show-tags-regexp)
150 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
152 (defun notmuch-show-add-tag (&rest toadd)
153 "Add a tag to the current message."
154 (interactive "sTag to add: ")
155 (apply 'notmuch-call-notmuch-process
157 (mapcar (lambda (s) (concat "+" s)) toadd))
158 (cons (notmuch-show-get-message-id) nil)))
159 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
161 (defun notmuch-show-remove-tag (&rest toremove)
162 "Remove a tag from the current message."
163 (interactive "sTag to remove: ")
164 (let ((tags (notmuch-show-get-tags)))
165 (if (intersection tags toremove :test 'string=)
167 (apply 'notmuch-call-notmuch-process
169 (mapcar (lambda (s) (concat "-" s)) toremove))
170 (cons (notmuch-show-get-message-id) nil)))
171 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
173 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
175 (goto-char (point-min))
178 (notmuch-show-remove-tag "unread" "inbox")
179 (notmuch-show-remove-tag "inbox"))
182 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
183 (goto-char (point-max)))))
184 (let ((parent-buffer notmuch-show-parent-buffer))
188 (switch-to-buffer parent-buffer)
190 (notmuch-search-show-thread)))))
192 (defun notmuch-show-mark-read-then-archive-thread ()
193 "Remove \"unread\" tag from each message, then archive and show next thread.
195 Archive each message currrently shown by removing the \"unread\"
196 and \"inbox\" tag from each. Then kill this buffer and show the
197 next thread from the search from which this thread was originally
200 Note: This command is safe from any race condition of new messages
201 being delivered to the same thread. It does not archive the
202 entire thread, but only the messages shown in the current
205 (notmuch-show-archive-thread-maybe-mark-read t))
207 (defun notmuch-show-archive-thread ()
208 "Archive each message in thread, and show next thread from search.
210 Archive each message currrently shown by removing the \"inbox\"
211 tag from each. Then kill this buffer and show the next thread
212 from the search from which this thread was originally shown.
214 Note: This command is safe from any race condition of new messages
215 being delivered to the same thread. It does not archive the
216 entire thread, but only the messages shown in the current
219 (notmuch-show-archive-thread-maybe-mark-read nil))
221 (defun notmuch-show-view-raw-message ()
222 "View the raw email of the current message."
224 (view-file (notmuch-show-get-filename)))
226 (defun notmuch-show-reply ()
227 "Begin composing a reply to the current message in a new buffer."
229 (let ((message-id (notmuch-show-get-message-id)))
230 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
231 (call-process "notmuch" nil t nil "reply" message-id)
232 (goto-char (point-min))
233 (if (re-search-forward "^$" nil t)
235 (insert "--text follows this line--")
239 (defun notmuch-show-pipe-message (command)
240 "Pipe the contents of the current message to the given command.
242 The given command will be executed with the raw contents of the
243 current email message as stdin. Anything printed by the command
244 to stdout or stderr will appear in the *Messages* buffer."
245 (interactive "sPipe message to command: ")
246 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*" (split-string (concat command " < " (notmuch-show-get-filename)))))
248 (defun notmuch-show-move-to-current-message-summary-line ()
249 "Move to the beginning of the one-line summary of the current message.
251 This gives us a stable place to move to and work from since the
252 summary line is always visible. This is important since moving to
253 an invisible location is unreliable, (the main command loop moves
254 point either forward or backward to the next visible character
255 when a command ends with point on an invisible character).
257 Emits an error if point is not within a valid message, (that is
258 not pattern of `notmuch-show-message-begin-regexp' could be found
259 by searching backward)."
261 (if (not (looking-at notmuch-show-message-begin-regexp))
262 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
264 (error "Not within a valid message."))
267 (defun notmuch-show-last-message-p ()
268 "Predicate testing whether point is within the last message."
269 (save-window-excursion
271 (notmuch-show-move-to-current-message-summary-line)
272 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
274 (defun notmuch-show-message-unread-p ()
275 "Preficate testing whether current message is unread."
276 (member "unread" (notmuch-show-get-tags)))
278 (defun notmuch-show-next-message ()
279 "Advance to the beginning of the next message in the buffer.
281 Moves to the last visible character of the current message if
282 already on the last message in the buffer."
284 (notmuch-show-move-to-current-message-summary-line)
285 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
286 (notmuch-show-move-to-current-message-summary-line)
287 (goto-char (- (point-max) 1))
288 (while (point-invisible-p)
292 (defun notmuch-show-find-next-message ()
293 "Returns the position of the next message in the buffer.
295 Or the position of the last visible character of the current
296 message if already within the last message in the buffer."
297 ; save-excursion doesn't save our window position
298 ; save-window-excursion doesn't save point
299 ; Looks like we have to use both.
301 (save-window-excursion
302 (notmuch-show-next-message)
305 (defun notmuch-show-next-unread-message ()
306 "Advance to the beginning of the next unread message in the buffer.
308 Moves to the last visible character of the current message if
309 there are no more unread messages past the current point."
310 (notmuch-show-next-message)
311 (while (and (not (notmuch-show-last-message-p))
312 (not (notmuch-show-message-unread-p)))
313 (notmuch-show-next-message))
314 (if (not (notmuch-show-message-unread-p))
315 (notmuch-show-next-message)))
317 (defun notmuch-show-next-open-message ()
318 "Advance to the the next message which is not hidden.
320 If read messages are currently hidden, advance to the next unread
321 message. Otherwise, advance to the next message."
322 (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
323 (assq 'notmuch-show-body-read buffer-invisibility-spec))
324 (notmuch-show-next-unread-message)
325 (notmuch-show-next-message)))
327 (defun notmuch-show-previous-message ()
328 "Backup to the beginning of the previous message in the buffer.
330 If within a message rather than at the beginning of it, then
331 simply move to the beginning of the current message."
333 (let ((start (point)))
334 (notmuch-show-move-to-current-message-summary-line)
335 (if (not (< (point) start))
336 ; Go backward twice to skip the current message's marker
338 (re-search-backward notmuch-show-message-begin-regexp nil t)
339 (re-search-backward notmuch-show-message-begin-regexp nil t)
340 (notmuch-show-move-to-current-message-summary-line)
344 (defun notmuch-show-find-previous-message ()
345 "Returns the position of the previous message in the buffer.
347 Or the position of the beginning of the current message if point
348 is originally within the message rather than at the beginning of
350 ; save-excursion doesn't save our window position
351 ; save-window-excursion doesn't save point
352 ; Looks like we have to use both.
354 (save-window-excursion
355 (notmuch-show-previous-message)
358 (defun notmuch-show-mark-read-then-next-open-message ()
359 "Remove unread tag from current message, then advance to next unread message."
361 (notmuch-show-remove-tag "unread")
362 (notmuch-show-next-open-message))
364 (defun notmuch-show-rewind ()
365 "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
367 Specifically, if the beginning of the previous email is fewer
368 than `window-height' lines from the current point, move to it
369 just like `notmuch-show-previous-message'.
371 Otherwise, just scroll down a screenful of the current message.
373 This command does not modify any message tags, (it does not undo
374 any effects from previous calls to
375 `notmuch-show-advance-marking-read-and-archiving'."
377 (let ((previous (notmuch-show-find-previous-message)))
378 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
382 ((beginning-of-buffer) nil))
383 (goto-char (window-start)))
384 (notmuch-show-previous-message))))
386 (defun notmuch-show-advance-marking-read-and-archiving ()
387 "Advance through buffer, marking read and archiving.
389 This command is intended to be one of the simplest ways to
390 process a thread of email. It does the following:
392 If the current message in the thread is not yet fully visible,
393 scroll by a near screenful to read more of the message.
395 Otherwise, (the end of the current message is already within the
396 current window), remove the \"unread\" tag (if present) from the
397 current message and advance to the next open message.
399 Finally, if there is no further message to advance to, and this
400 last message is already read, then archive the entire current
401 thread, (remove the \"inbox\" tag from each message). Also kill
402 this buffer, and display the next thread from the search from
403 which this thread was originally shown."
405 (let ((next (notmuch-show-find-next-message))
406 (unread (notmuch-show-message-unread-p)))
407 (if (> next (window-end))
409 (let ((last (notmuch-show-last-message-p)))
410 (notmuch-show-mark-read-then-next-open-message)
412 (notmuch-show-archive-thread))))))
414 (defun notmuch-show-markup-citations-region (beg end)
417 (while (< (point) end)
418 (let ((beg-sub (point)))
421 (while (looking-at ">")
423 (let ((overlay (make-overlay beg-sub (point))))
424 (overlay-put overlay 'invisible 'notmuch-show-citation)
425 (overlay-put overlay 'before-string
426 (concat "[" (number-to-string (count-lines beg-sub (point)))
427 "-line citation. Press 'c' to show.]\n")))))
428 (if (looking-at "--[ ]?$")
429 (let ((sig-lines (count-lines beg-sub end)))
430 (if (<= sig-lines notmuch-show-signature-lines-max)
432 (overlay-put (make-overlay beg-sub (+ beg-sub 1))
434 (concat "[" (number-to-string sig-lines)
435 "-line signature. Press 's' to show.]"))
436 (overlay-put (make-overlay (+ beg-sub 2) end)
437 'invisible 'notmuch-show-signature)
441 (defun notmuch-show-markup-body ()
442 (re-search-forward notmuch-show-body-begin-regexp)
446 (re-search-forward notmuch-show-body-end-regexp)
447 (let ((end (match-beginning 0)))
448 (notmuch-show-markup-citations-region beg end)
449 (if (not (notmuch-show-message-unread-p))
450 (overlay-put (make-overlay beg end)
451 'invisible 'notmuch-show-body-read)))))
453 (defun notmuch-show-markup-header ()
454 (re-search-forward notmuch-show-header-begin-regexp)
459 ; Inverse video for subject
460 (overlay-put (make-overlay beg (point)) 'face '((cons :inverse-video t)))
464 (re-search-forward notmuch-show-header-end-regexp)
465 (overlay-put (make-overlay beg (match-beginning 0))
466 'invisible 'notmuch-show-header)))
468 (defun notmuch-show-markup-message ()
469 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
471 (notmuch-show-markup-header)
472 (notmuch-show-markup-body))
473 (goto-char (point-max))))
475 (defun notmuch-show-hide-markers ()
477 (goto-char (point-min))
479 (if (re-search-forward notmuch-show-marker-regexp nil t)
481 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
482 'invisible 'notmuch-show-marker))
483 (goto-char (point-max))))))
485 (defun notmuch-show-markup-messages ()
487 (goto-char (point-min))
489 (notmuch-show-markup-message)))
490 (notmuch-show-hide-markers))
492 (defun notmuch-show-toggle-citations-visible ()
493 "Toggle visibility of citations"
495 (if notmuch-show-citations-visible
496 (add-to-invisibility-spec 'notmuch-show-citation)
497 (remove-from-invisibility-spec 'notmuch-show-citation))
498 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
499 ; Need to force the redisplay for some reason
500 (force-window-update)
503 (defun notmuch-show-toggle-signatures-visible ()
504 "Toggle visibility of signatures"
506 (if notmuch-show-signatures-visible
507 (add-to-invisibility-spec 'notmuch-show-signature)
508 (remove-from-invisibility-spec 'notmuch-show-signature))
509 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
510 ; Need to force the redisplay for some reason
511 (force-window-update)
514 (defun notmuch-show-toggle-headers-visible ()
515 "Toggle visibility of header fields"
517 (if notmuch-show-headers-visible
518 (add-to-invisibility-spec 'notmuch-show-header)
519 (remove-from-invisibility-spec 'notmuch-show-header))
520 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
521 ; Need to force the redisplay for some reason
522 (force-window-update)
525 (defun notmuch-show-toggle-body-read-visible ()
526 "Toggle visibility of message bodies of read messages"
528 (if notmuch-show-body-read-visible
529 (add-to-invisibility-spec 'notmuch-show-body-read)
530 (remove-from-invisibility-spec 'notmuch-show-body-read))
531 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
532 ; Need to force the redisplay for some reason
533 (force-window-update)
537 (defun notmuch-show-mode ()
538 "Major mode for viewing a thread with notmuch.
540 This buffer contains the results of the \"notmuch show\" command
541 for displaying a single thread of email from your email archives.
543 By default, various components of email messages, (citations,
544 signatures, already-read messages), are invisible to help you
545 focus on the most important things, (new text from unread
546 messages). See the various commands below for toggling the
547 visibility of hidden components.
549 The `notmuch-show-next-message' and
550 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
551 default), allow you to navigate to the next and previous
552 messages. Each time you navigate away from a message with
553 `notmuch-show-next-message' the current message will have its
554 \"unread\" tag removed.
556 You can add or remove tags from the current message with '+' and
557 '-'. You can also archive all messages in the current
558 view, (remove the \"inbox\" tag from each), with
559 `notmuch-show-archive-thread' (bound to 'a' by default).
561 \\{notmuch-show-mode-map}"
563 (kill-all-local-variables)
564 (set (make-local-variable 'notmuch-show-headers-visible) t)
565 (notmuch-show-toggle-headers-visible)
566 (set (make-local-variable 'notmuch-show-body-read-visible) t)
567 (notmuch-show-toggle-body-read-visible)
568 (set (make-local-variable 'notmuch-show-citations-visible) t)
569 (notmuch-show-toggle-citations-visible)
570 (set (make-local-variable 'notmuch-show-signatures-visible) t)
571 (notmuch-show-toggle-signatures-visible)
572 (add-to-invisibility-spec 'notmuch-show-marker)
573 (use-local-map notmuch-show-mode-map)
574 (setq major-mode 'notmuch-show-mode
575 mode-name "notmuch-show")
576 (setq buffer-read-only t))
578 (defun notmuch-show (thread-id &optional parent-buffer)
579 "Run \"notmuch show\" with the given thread ID and display results.
581 The optional PARENT-BUFFER is the notmuch-search buffer from
582 which this notmuch-show command was executed, (so that the next
583 thread from that buffer can be show when done with this one)."
584 (interactive "sNotmuch show: ")
585 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
586 (switch-to-buffer buffer)
588 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
589 (let ((proc (get-buffer-process (current-buffer)))
590 (inhibit-read-only t))
592 (error "notmuch search process already running for query `%s'" query)
595 (goto-char (point-min))
597 (call-process "notmuch" nil t nil "show" thread-id)
598 (notmuch-show-markup-messages)
600 ; Move straight to the first unread message
601 (if (not (notmuch-show-message-unread-p))
603 (notmuch-show-next-unread-message)
604 ; But if there are no unread messages, go back to the
605 ; beginning of the buffer, and open up the bodies of all
607 (if (not (notmuch-show-message-unread-p))
609 (goto-char (point-min))
610 (notmuch-show-toggle-body-read-visible)))))
613 (defvar notmuch-search-mode-map
614 (let ((map (make-sparse-keymap)))
615 (define-key map "a" 'notmuch-search-archive-thread)
616 (define-key map "b" 'notmuch-search-scroll-down)
617 (define-key map "f" 'notmuch-search-filter)
618 (define-key map "m" 'message-mail)
619 (define-key map "n" 'next-line)
620 (define-key map "o" 'notmuch-search-toggle-order)
621 (define-key map "p" 'previous-line)
622 (define-key map "q" 'kill-this-buffer)
623 (define-key map "s" 'notmuch-search)
624 (define-key map "t" 'notmuch-search-filter-by-tag)
625 (define-key map "x" 'kill-this-buffer)
626 (define-key map (kbd "RET") 'notmuch-search-show-thread)
627 (define-key map "+" 'notmuch-search-add-tag)
628 (define-key map "-" 'notmuch-search-remove-tag)
629 (define-key map "<" 'beginning-of-buffer)
630 (define-key map ">" 'notmuch-search-goto-last-thread)
631 (define-key map "=" 'notmuch-search-refresh-view)
632 (define-key map "\M->" 'notmuch-search-goto-last-thread)
633 (define-key map " " 'notmuch-search-scroll-up)
634 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
636 "Keymap for \"notmuch search\" buffers.")
637 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
639 (defun notmuch-search-scroll-up ()
640 "Scroll up, moving point to last message in thread if at end."
644 ((end-of-buffer) (notmuch-search-goto-last-thread))))
646 (defun notmuch-search-scroll-down ()
647 "Scroll down, moving point to first message in thread if at beginning."
649 ; I don't know why scroll-down doesn't signal beginning-of-buffer
650 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
652 ; So instead of trapping a signal we instead check whether the
653 ; window begins on the first line of the buffer and if so, move
654 ; directly to that position. (We have to count lines since the
655 ; window-start position is not the same as point-min due to the
656 ; invisible thread-ID characters on the first line.
657 (if (equal (count-lines (point-min) (window-start)) 1)
658 (goto-char (window-start))
661 (defun notmuch-search-goto-last-thread (&optional arg)
662 "Move point to the last thread in the buffer."
668 (defun notmuch-search-mode ()
669 "Major mode for searching mail with notmuch.
671 This buffer contains the results of a \"notmuch search\" of your
672 email archives. Each line in the buffer represents a single
673 thread giving a relative date for the thread and a subject.
675 Pressing RET on any line displays that thread. The '+' and '-'
676 keys can be used to add or remove tags from a thread. The 'a' key
677 is a convenience key for archiving a thread (removing the
680 Other useful commands are `notmuch-search-filter' for filtering
681 the current search based on an additional query string,
682 `notmuch-search-filter-by-tag' for filtering to include only
683 messages with a given tag, and `notmuch-search' to execute a new,
686 \\{notmuch-search-mode-map}"
688 (kill-all-local-variables)
689 (make-local-variable 'notmuch-search-query-string)
690 (make-local-variable 'notmuch-search-oldest-first)
691 (set (make-local-variable 'scroll-preserve-screen-position) t)
692 (add-to-invisibility-spec 'notmuch-search)
693 (use-local-map notmuch-search-mode-map)
694 (setq major-mode 'notmuch-search-mode
695 mode-name "notmuch-search")
696 (setq buffer-read-only t))
698 (defun notmuch-search-find-thread-id ()
702 (re-search-forward "thread:[a-fA-F0-9]*" nil t)
703 (filter-buffer-substring beg (point)))))
705 (defun notmuch-search-markup-this-thread-id ()
708 (if (re-search-forward "thread:[a-fA-F0-9]*" nil t)
711 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))))
713 (defun notmuch-search-markup-thread-ids ()
715 (goto-char (point-min))
717 (notmuch-search-markup-this-thread-id)
720 (defun notmuch-search-show-thread ()
722 (let ((thread-id (notmuch-search-find-thread-id)))
723 (if (> (length thread-id) 0)
724 (notmuch-show thread-id (current-buffer))
725 (error "End of search results"))))
727 (defun notmuch-call-notmuch-process (&rest args)
728 "Synchronously invoke \"notmuch\" with the given list of arguments.
730 Output from the process will be presented to the user as an error
731 and will also appear in a buffer named \"*Notmuch errors*\"."
732 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
733 (with-current-buffer error-buffer
735 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
738 (with-current-buffer error-buffer
739 (let ((beg (point-min))
740 (end (- (point-max) 1)))
741 (error (buffer-substring beg end))
744 (defun notmuch-search-set-tags (tags)
747 (re-search-backward "(")
750 (inhibit-read-only t))
751 (re-search-forward ")")
754 (delete-region beg end)
755 (insert (mapconcat 'identity tags " "))))))
757 (defun notmuch-search-get-tags ()
760 (re-search-backward "(")
761 (let ((beg (+ (point) 1)))
762 (re-search-forward ")")
763 (let ((end (- (point) 1)))
764 (split-string (buffer-substring beg end))))))
766 (defun notmuch-search-add-tag (tag)
767 (interactive "sTag to add: ")
768 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
769 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
771 (defun notmuch-search-remove-tag (tag)
772 (interactive "sTag to remove: ")
773 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
774 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
776 (defun notmuch-search-archive-thread ()
777 "Archive the current thread (remove its \"inbox\" tag).
779 This function advances point to the next line when finished."
781 (notmuch-search-remove-tag "inbox")
784 (defun notmuch-search (query &optional oldest-first)
785 "Run \"notmuch search\" with the given query string and display results."
786 (interactive "sNotmuch search: ")
787 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
788 (switch-to-buffer buffer)
789 (notmuch-search-mode)
790 (set 'notmuch-search-query-string query)
791 (set 'notmuch-search-oldest-first oldest-first)
792 (let ((proc (get-buffer-process (current-buffer)))
793 (inhibit-read-only t))
795 (error "notmuch search process already running for query `%s'" query)
798 (goto-char (point-min))
801 (call-process "notmuch" nil t nil "search" query)
802 (call-process "notmuch" nil t nil "search" "--reverse" query))
803 (notmuch-search-markup-thread-ids)
806 (defun notmuch-search-refresh-view ()
807 "Refresh the current view.
809 Kills the current buffer and runs a new search with the same
810 query string as the current search. If the current thread is in
811 the new search results, then point will be placed on the same
812 thread. Otherwise, point will be moved to attempt to be in the
813 same relative position within the new buffer."
816 (oldest-first notmuch-search-oldest-first)
817 (thread (notmuch-search-find-thread-id))
818 (query notmuch-search-query-string))
820 (notmuch-search query oldest-first)
821 (goto-char (point-min))
822 (if (re-search-forward (concat "^" thread) nil t)
826 (defun notmuch-search-toggle-order ()
827 "Toggle the current search order.
829 By default, the \"inbox\" view created by `notmuch' is displayed
830 in chronological order (oldest thread at the beginning of the
831 buffer), while any global searches created by `notmuch-search'
832 are displayed in reverse-chronological order (newest thread at
833 the beginning of the buffer).
835 This command toggles the sort order for the current search.
837 Note that any fitlered searches created by
838 `notmuch-search-filter' retain the search order of the parent
841 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
842 (notmuch-search-refresh-view))
844 (defun notmuch-search-filter (query)
845 "Filter the current search results based on an additional query string.
847 Runs a new search matching only messages that match both the
848 current search results AND the additional query string provided."
849 (interactive "sFilter search: ")
850 (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
852 (defun notmuch-search-filter-by-tag (tag)
853 "Filter the current search results based on a single tag.
855 Runs a new search matching only messages that match both the
856 current search results AND that are tagged with the given tag."
857 (interactive "sFilter by tag: ")
858 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
861 "Run notmuch to display all mail with tag of 'inbox'"
863 (notmuch-search "tag:inbox" t))