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>
25 (defvar notmuch-show-mode-map
26 (let ((map (make-sparse-keymap)))
27 ; I don't actually want all of these toggle commands occupying
28 ; keybindings. They steal valuable key-binding space, are hard
29 ; to remember, and act globally rather than locally.
31 ; Will be much preferable to switch to direct manipulation for
32 ; toggling visibility of these components. Probably using
33 ; overlays-at to query and manipulate the current overlay.
34 (define-key map "a" 'notmuch-show-archive-thread)
35 (define-key map "A" 'notmuch-show-mark-read-then-archive-thread)
36 (define-key map "b" 'notmuch-show-toggle-body-read-visible)
37 (define-key map "c" 'notmuch-show-toggle-citations-visible)
38 (define-key map "h" 'notmuch-show-toggle-headers-visible)
39 (define-key map "m" 'message-mail)
40 (define-key map "n" 'notmuch-show-next-message)
41 (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
42 (define-key map "p" 'notmuch-show-previous-message)
43 (define-key map (kbd "C-n") 'notmuch-show-next-line)
44 (define-key map (kbd "C-p") 'notmuch-show-previous-line)
45 (define-key map "q" 'kill-this-buffer)
46 (define-key map "r" 'notmuch-show-reply)
47 (define-key map "s" 'notmuch-show-toggle-signatures-visible)
48 (define-key map "v" 'notmuch-show-view-all-mime-parts)
49 (define-key map "w" 'notmuch-show-view-raw-message)
50 (define-key map "x" 'kill-this-buffer)
51 (define-key map "+" 'notmuch-show-add-tag)
52 (define-key map "-" 'notmuch-show-remove-tag)
53 (define-key map (kbd "DEL") 'notmuch-show-rewind)
54 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
55 (define-key map "|" 'notmuch-show-pipe-message)
57 "Keymap for \"notmuch show\" buffers.")
58 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
60 (defvar notmuch-show-signature-lines-max 12
61 "Maximum length of signature that will be hidden by default.")
63 (set 'notmuch-show-message-begin-regexp "
\fmessage{")
64 (set 'notmuch-show-message-end-regexp "
\fmessage}")
65 (set 'notmuch-show-header-begin-regexp "
\fheader{")
66 (set 'notmuch-show-header-end-regexp "
\fheader}")
67 (set 'notmuch-show-body-begin-regexp "
\fbody{")
68 (set 'notmuch-show-body-end-regexp "
\fbody}")
69 (set 'notmuch-show-attachment-begin-regexp "
\fattachment{")
70 (set 'notmuch-show-attachment-end-regexp "
\fattachment}")
71 (set 'notmuch-show-part-begin-regexp "
\fpart{")
72 (set 'notmuch-show-part-end-regexp "
\fpart}")
73 (set 'notmuch-show-marker-regexp "
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
75 (set 'notmuch-show-id-regexp "\\(id:[^ ]*\\)")
76 (set 'notmuch-show-depth-regexp " depth:\\([0-9]*\\) ")
77 (set 'notmuch-show-filename-regexp "filename:\\(.*\\)$")
78 (set 'notmuch-show-tags-regexp "(\\([^)]*\\))$")
80 ; XXX: This should be a generic function in emacs somewhere, not here
81 (defun point-invisible-p ()
82 "Return whether the character at point is invisible.
84 Here visibility is determined by `buffer-invisibility-spec' and
85 the invisible property of any overlays for point. It doesn't have
86 anything to do with whether point is currently being displayed
87 within the current window."
88 (let ((prop (get-char-property (point) 'invisible)))
89 (if (eq buffer-invisibility-spec t)
91 (or (memq prop buffer-invisibility-spec)
92 (assq prop buffer-invisibility-spec)))))
94 (defun notmuch-show-next-line ()
95 "Like builtin `next-line' but ensuring we end on a visible character.
97 By advancing forward until reaching a visible character.
99 Unlike builtin `next-line' this version accepts no arguments."
101 (set 'this-command 'next-line)
102 (call-interactively 'next-line)
103 (while (point-invisible-p)
106 (defun notmuch-show-previous-line ()
107 "Like builtin `previous-line' but ensuring we end on a visible character.
109 By advancing forward until reaching a visible character.
111 Unlike builtin `next-line' this version accepts no arguments."
113 (set 'this-command 'previous-line)
114 (call-interactively 'previous-line)
115 (while (point-invisible-p)
118 (defun notmuch-show-get-message-id ()
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-id-regexp)
124 (buffer-substring (match-beginning 1) (match-end 1))))
126 (defun notmuch-show-get-filename ()
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-filename-regexp)
132 (buffer-substring (match-beginning 1) (match-end 1))))
134 (defun notmuch-show-set-tags (tags)
137 (if (not (looking-at notmuch-show-message-begin-regexp))
138 (re-search-backward notmuch-show-message-begin-regexp))
139 (re-search-forward notmuch-show-tags-regexp)
140 (let ((inhibit-read-only t)
141 (beg (match-beginning 1))
143 (delete-region beg end)
145 (insert (mapconcat 'identity tags " ")))))
147 (defun notmuch-show-get-tags ()
150 (if (not (looking-at notmuch-show-message-begin-regexp))
151 (re-search-backward notmuch-show-message-begin-regexp))
152 (re-search-forward notmuch-show-tags-regexp)
153 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
155 (defun notmuch-show-add-tag (&rest toadd)
156 "Add a tag to the current message."
157 (interactive "sTag to add: ")
158 (apply 'notmuch-call-notmuch-process
160 (mapcar (lambda (s) (concat "+" s)) toadd))
161 (cons (notmuch-show-get-message-id) nil)))
162 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
164 (defun notmuch-show-remove-tag (&rest toremove)
165 "Remove a tag from the current message."
166 (interactive "sTag to remove: ")
167 (let ((tags (notmuch-show-get-tags)))
168 (if (intersection tags toremove :test 'string=)
170 (apply 'notmuch-call-notmuch-process
172 (mapcar (lambda (s) (concat "-" s)) toremove))
173 (cons (notmuch-show-get-message-id) nil)))
174 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
176 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
178 (goto-char (point-min))
181 (notmuch-show-remove-tag "unread" "inbox")
182 (notmuch-show-remove-tag "inbox"))
185 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
186 (goto-char (point-max)))))
187 (let ((parent-buffer notmuch-show-parent-buffer))
191 (switch-to-buffer parent-buffer)
193 (notmuch-search-show-thread)))))
195 (defun notmuch-show-mark-read-then-archive-thread ()
196 "Remove \"unread\" tag from each message, then archive and show next thread.
198 Archive each message currrently shown by removing the \"unread\"
199 and \"inbox\" tag from each. Then kill this buffer and show the
200 next thread from the search from which this thread was originally
203 Note: This command is safe from any race condition of new messages
204 being delivered to the same thread. It does not archive the
205 entire thread, but only the messages shown in the current
208 (notmuch-show-archive-thread-maybe-mark-read t))
210 (defun notmuch-show-archive-thread ()
211 "Archive each message in thread, and show next thread from search.
213 Archive each message currrently shown by removing the \"inbox\"
214 tag from each. Then kill this buffer and show the next thread
215 from the search from which this thread was originally shown.
217 Note: This command is safe from any race condition of new messages
218 being delivered to the same thread. It does not archive the
219 entire thread, but only the messages shown in the current
222 (notmuch-show-archive-thread-maybe-mark-read nil))
224 (defun notmuch-show-view-raw-message ()
225 "View the raw email of the current message."
227 (view-file (notmuch-show-get-filename)))
229 (defun notmuch-show-view-all-mime-parts ()
230 "Use external viewers (according to mailcap) to view all MIME-encoded parts."
233 (let ((filename (notmuch-show-get-filename)))
234 (switch-to-buffer (generate-new-buffer (concat "*notmuch-mime-"
237 (insert-file-contents filename nil nil nil t)
238 (mm-display-parts (mm-dissect-buffer))
239 (kill-this-buffer))))
241 (defun notmuch-show-reply ()
242 "Begin composing a reply to the current message in a new buffer."
244 (let ((message-id (notmuch-show-get-message-id)))
245 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
246 (call-process "notmuch" nil t nil "reply" message-id)
247 (goto-char (point-min))
248 (if (re-search-forward "^$" nil t)
250 (insert "--text follows this line--")
254 (defun notmuch-show-pipe-message (command)
255 "Pipe the contents of the current message to the given command.
257 The given command will be executed with the raw contents of the
258 current email message as stdin. Anything printed by the command
259 to stdout or stderr will appear in the *Messages* buffer."
260 (interactive "sPipe message to command: ")
261 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*" (split-string (concat command " < " (notmuch-show-get-filename)))))
263 (defun notmuch-show-move-to-current-message-summary-line ()
264 "Move to the beginning of the one-line summary of the current message.
266 This gives us a stable place to move to and work from since the
267 summary line is always visible. This is important since moving to
268 an invisible location is unreliable, (the main command loop moves
269 point either forward or backward to the next visible character
270 when a command ends with point on an invisible character).
272 Emits an error if point is not within a valid message, (that is
273 not pattern of `notmuch-show-message-begin-regexp' could be found
274 by searching backward)."
276 (if (not (looking-at notmuch-show-message-begin-regexp))
277 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
279 (error "Not within a valid message."))
282 (defun notmuch-show-last-message-p ()
283 "Predicate testing whether point is within the last message."
284 (save-window-excursion
286 (notmuch-show-move-to-current-message-summary-line)
287 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
289 (defun notmuch-show-message-unread-p ()
290 "Preficate testing whether current message is unread."
291 (member "unread" (notmuch-show-get-tags)))
293 (defun notmuch-show-next-message ()
294 "Advance to the beginning of the next message in the buffer.
296 Moves to the last visible character of the current message if
297 already on the last message in the buffer."
299 (notmuch-show-move-to-current-message-summary-line)
300 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
301 (notmuch-show-move-to-current-message-summary-line)
302 (goto-char (- (point-max) 1))
303 (while (point-invisible-p)
307 (defun notmuch-show-find-next-message ()
308 "Returns the position of the next message in the buffer.
310 Or the position of the last visible character of the current
311 message if already within the last message in the buffer."
312 ; save-excursion doesn't save our window position
313 ; save-window-excursion doesn't save point
314 ; Looks like we have to use both.
316 (save-window-excursion
317 (notmuch-show-next-message)
320 (defun notmuch-show-next-unread-message ()
321 "Advance to the beginning of the next unread message in the buffer.
323 Moves to the last visible character of the current message if
324 there are no more unread messages past the current point."
325 (notmuch-show-next-message)
326 (while (and (not (notmuch-show-last-message-p))
327 (not (notmuch-show-message-unread-p)))
328 (notmuch-show-next-message))
329 (if (not (notmuch-show-message-unread-p))
330 (notmuch-show-next-message)))
332 (defun notmuch-show-next-open-message ()
333 "Advance to the the next message which is not hidden.
335 If read messages are currently hidden, advance to the next unread
336 message. Otherwise, advance to the next message."
337 (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
338 (assq 'notmuch-show-body-read buffer-invisibility-spec))
339 (notmuch-show-next-unread-message)
340 (notmuch-show-next-message)))
342 (defun notmuch-show-previous-message ()
343 "Backup to the beginning of the previous message in the buffer.
345 If within a message rather than at the beginning of it, then
346 simply move to the beginning of the current message."
348 (let ((start (point)))
349 (notmuch-show-move-to-current-message-summary-line)
350 (if (not (< (point) start))
351 ; Go backward twice to skip the current message's marker
353 (re-search-backward notmuch-show-message-begin-regexp nil t)
354 (re-search-backward notmuch-show-message-begin-regexp nil t)
355 (notmuch-show-move-to-current-message-summary-line)
359 (defun notmuch-show-find-previous-message ()
360 "Returns the position of the previous message in the buffer.
362 Or the position of the beginning of the current message if point
363 is originally within the message rather than at the beginning of
365 ; save-excursion doesn't save our window position
366 ; save-window-excursion doesn't save point
367 ; Looks like we have to use both.
369 (save-window-excursion
370 (notmuch-show-previous-message)
373 (defun notmuch-show-mark-read-then-next-open-message ()
374 "Remove unread tag from current message, then advance to next unread message."
376 (notmuch-show-remove-tag "unread")
377 (notmuch-show-next-open-message))
379 (defun notmuch-show-rewind ()
380 "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
382 Specifically, if the beginning of the previous email is fewer
383 than `window-height' lines from the current point, move to it
384 just like `notmuch-show-previous-message'.
386 Otherwise, just scroll down a screenful of the current message.
388 This command does not modify any message tags, (it does not undo
389 any effects from previous calls to
390 `notmuch-show-advance-marking-read-and-archiving'."
392 (let ((previous (notmuch-show-find-previous-message)))
393 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
397 ((beginning-of-buffer) nil))
398 (goto-char (window-start)))
399 (notmuch-show-previous-message))))
401 (defun notmuch-show-advance-marking-read-and-archiving ()
402 "Advance through buffer, marking read and archiving.
404 This command is intended to be one of the simplest ways to
405 process a thread of email. It does the following:
407 If the current message in the thread is not yet fully visible,
408 scroll by a near screenful to read more of the message.
410 Otherwise, (the end of the current message is already within the
411 current window), remove the \"unread\" tag (if present) from the
412 current message and advance to the next open message.
414 Finally, if there is no further message to advance to, and this
415 last message is already read, then archive the entire current
416 thread, (remove the \"inbox\" tag from each message). Also kill
417 this buffer, and display the next thread from the search from
418 which this thread was originally shown."
420 (let ((next (notmuch-show-find-next-message))
421 (unread (notmuch-show-message-unread-p)))
422 (if (> next (window-end))
424 (let ((last (notmuch-show-last-message-p)))
425 (notmuch-show-mark-read-then-next-open-message)
427 (notmuch-show-archive-thread))))))
429 (defun notmuch-show-markup-citations-region (beg end depth)
432 (while (< (point) end)
433 (let ((beg-sub (point-marker))
434 (indent (make-string depth ? ))
435 (citation "[[:space:]]*>"))
436 (if (looking-at citation)
438 (while (looking-at citation)
440 (let ((overlay (make-overlay beg-sub (point))))
441 (overlay-put overlay 'invisible 'notmuch-show-citation)
442 (overlay-put overlay 'before-string
444 "[" (number-to-string (count-lines beg-sub (point)))
445 "-line citation. Press 'c' to show.]\n")))))
446 (if (looking-at "[[:space:]]*-- ?$")
447 (let ((sig-lines (- (count-lines beg-sub end) 1)))
448 (if (<= sig-lines notmuch-show-signature-lines-max)
450 (overlay-put (make-overlay beg-sub end)
451 'invisible 'notmuch-show-signature)
452 (overlay-put (make-overlay beg (- beg-sub 1))
455 "[" (number-to-string sig-lines)
456 "-line signature. Press 's' to show.]"))
460 (defun notmuch-show-markup-part (beg end depth)
461 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
464 (let ((beg (point-marker)))
465 (re-search-forward notmuch-show-part-end-regexp)
466 (let ((end (copy-marker (match-beginning 0))))
467 (indent-rigidly beg end depth)
468 (notmuch-show-markup-citations-region beg end depth)
469 ; Advance to the next part (if any) (so the outer loop can
470 ; determine whether we've left the current message.
471 (re-search-forward notmuch-show-part-begin-regexp nil t))))
474 (defun notmuch-show-markup-parts-region (beg end depth)
477 (while (< (point) end)
478 (notmuch-show-markup-part beg end depth))))
480 (defun notmuch-show-markup-body (depth)
481 (re-search-forward notmuch-show-body-begin-regexp)
483 (let ((beg (point-marker)))
484 (re-search-forward notmuch-show-body-end-regexp)
485 (let ((end (copy-marker (match-beginning 0))))
486 (notmuch-show-markup-parts-region beg end depth)
487 (if (not (notmuch-show-message-unread-p))
488 (overlay-put (make-overlay beg end)
489 'invisible 'notmuch-show-body-read))
494 (defun notmuch-show-markup-header (depth)
495 (re-search-forward notmuch-show-header-begin-regexp)
497 (let ((beg (point-marker)))
499 ; Inverse video for subject
500 (overlay-put (make-overlay beg (point)) 'face '((cons :inverse-video t)))
502 (let ((beg-hidden (point-marker)))
503 (re-search-forward notmuch-show-header-end-regexp)
505 (let ((end (point-marker)))
506 (indent-rigidly beg end depth)
507 (overlay-put (make-overlay beg-hidden end)
508 'invisible 'notmuch-show-header)
510 (set-marker beg-hidden nil)
514 (defun notmuch-show-markup-message ()
515 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
517 (re-search-forward notmuch-show-depth-regexp)
518 (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1)))))
519 (notmuch-show-markup-header depth)
520 (notmuch-show-markup-body depth)))
521 (goto-char (point-max))))
523 (defun notmuch-show-hide-markers ()
525 (goto-char (point-min))
527 (if (re-search-forward notmuch-show-marker-regexp nil t)
529 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
530 'invisible 'notmuch-show-marker))
531 (goto-char (point-max))))))
533 (defun notmuch-show-markup-messages ()
535 (goto-char (point-min))
537 (notmuch-show-markup-message)))
538 (notmuch-show-hide-markers))
540 (defun notmuch-show-toggle-citations-visible ()
541 "Toggle visibility of citations"
543 (if notmuch-show-citations-visible
544 (add-to-invisibility-spec 'notmuch-show-citation)
545 (remove-from-invisibility-spec 'notmuch-show-citation))
546 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
547 ; Need to force the redisplay for some reason
548 (force-window-update)
551 (defun notmuch-show-toggle-signatures-visible ()
552 "Toggle visibility of signatures"
554 (if notmuch-show-signatures-visible
555 (add-to-invisibility-spec 'notmuch-show-signature)
556 (remove-from-invisibility-spec 'notmuch-show-signature))
557 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
558 ; Need to force the redisplay for some reason
559 (force-window-update)
562 (defun notmuch-show-toggle-headers-visible ()
563 "Toggle visibility of header fields"
565 (if notmuch-show-headers-visible
566 (add-to-invisibility-spec 'notmuch-show-header)
567 (remove-from-invisibility-spec 'notmuch-show-header))
568 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
569 ; Need to force the redisplay for some reason
570 (force-window-update)
573 (defun notmuch-show-toggle-body-read-visible ()
574 "Toggle visibility of message bodies of read messages"
576 (if notmuch-show-body-read-visible
577 (add-to-invisibility-spec 'notmuch-show-body-read)
578 (remove-from-invisibility-spec 'notmuch-show-body-read))
579 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
580 ; Need to force the redisplay for some reason
581 (force-window-update)
585 (defun notmuch-show-mode ()
586 "Major mode for viewing a thread with notmuch.
588 This buffer contains the results of the \"notmuch show\" command
589 for displaying a single thread of email from your email archives.
591 By default, various components of email messages, (citations,
592 signatures, already-read messages), are invisible to help you
593 focus on the most important things, (new text from unread
594 messages). See the various commands below for toggling the
595 visibility of hidden components.
597 The `notmuch-show-next-message' and
598 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
599 default), allow you to navigate to the next and previous
600 messages. Each time you navigate away from a message with
601 `notmuch-show-next-message' the current message will have its
602 \"unread\" tag removed.
604 You can add or remove tags from the current message with '+' and
605 '-'. You can also archive all messages in the current
606 view, (remove the \"inbox\" tag from each), with
607 `notmuch-show-archive-thread' (bound to 'a' by default).
609 \\{notmuch-show-mode-map}"
611 (kill-all-local-variables)
612 (set (make-local-variable 'notmuch-show-headers-visible) t)
613 (notmuch-show-toggle-headers-visible)
614 (set (make-local-variable 'notmuch-show-body-read-visible) t)
615 (notmuch-show-toggle-body-read-visible)
616 (set (make-local-variable 'notmuch-show-citations-visible) t)
617 (notmuch-show-toggle-citations-visible)
618 (set (make-local-variable 'notmuch-show-signatures-visible) t)
619 (notmuch-show-toggle-signatures-visible)
620 (add-to-invisibility-spec 'notmuch-show-marker)
621 (use-local-map notmuch-show-mode-map)
622 (setq major-mode 'notmuch-show-mode
623 mode-name "notmuch-show")
624 (setq buffer-read-only t))
626 (defun notmuch-show (thread-id &optional parent-buffer)
627 "Run \"notmuch show\" with the given thread ID and display results.
629 The optional PARENT-BUFFER is the notmuch-search buffer from
630 which this notmuch-show command was executed, (so that the next
631 thread from that buffer can be show when done with this one)."
632 (interactive "sNotmuch show: ")
633 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
634 (switch-to-buffer buffer)
636 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
637 (let ((proc (get-buffer-process (current-buffer)))
638 (inhibit-read-only t))
640 (error "notmuch search process already running for query `%s'" query)
643 (goto-char (point-min))
645 (call-process "notmuch" nil t nil "show" thread-id)
646 (notmuch-show-markup-messages)
648 ; Move straight to the first unread message
649 (if (not (notmuch-show-message-unread-p))
651 (notmuch-show-next-unread-message)
652 ; But if there are no unread messages, go back to the
653 ; beginning of the buffer, and open up the bodies of all
655 (if (not (notmuch-show-message-unread-p))
657 (goto-char (point-min))
658 (notmuch-show-toggle-body-read-visible)))))
661 (defvar notmuch-search-mode-map
662 (let ((map (make-sparse-keymap)))
663 (define-key map "a" 'notmuch-search-archive-thread)
664 (define-key map "b" 'notmuch-search-scroll-down)
665 (define-key map "f" 'notmuch-search-filter)
666 (define-key map "m" 'message-mail)
667 (define-key map "n" 'next-line)
668 (define-key map "o" 'notmuch-search-toggle-order)
669 (define-key map "p" 'previous-line)
670 (define-key map "q" 'kill-this-buffer)
671 (define-key map "s" 'notmuch-search)
672 (define-key map "t" 'notmuch-search-filter-by-tag)
673 (define-key map "x" 'kill-this-buffer)
674 (define-key map (kbd "RET") 'notmuch-search-show-thread)
675 (define-key map "+" 'notmuch-search-add-tag)
676 (define-key map "-" 'notmuch-search-remove-tag)
677 (define-key map "<" 'beginning-of-buffer)
678 (define-key map ">" 'notmuch-search-goto-last-thread)
679 (define-key map "=" 'notmuch-search-refresh-view)
680 (define-key map "\M->" 'notmuch-search-goto-last-thread)
681 (define-key map " " 'notmuch-search-scroll-up)
682 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
684 "Keymap for \"notmuch search\" buffers.")
685 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
687 (defun notmuch-search-scroll-up ()
688 "Scroll up, moving point to last message in thread if at end."
692 ((end-of-buffer) (notmuch-search-goto-last-thread))))
694 (defun notmuch-search-scroll-down ()
695 "Scroll down, moving point to first message in thread if at beginning."
697 ; I don't know why scroll-down doesn't signal beginning-of-buffer
698 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
700 ; So instead of trapping a signal we instead check whether the
701 ; window begins on the first line of the buffer and if so, move
702 ; directly to that position. (We have to count lines since the
703 ; window-start position is not the same as point-min due to the
704 ; invisible thread-ID characters on the first line.
705 (if (equal (count-lines (point-min) (window-start)) 1)
706 (goto-char (window-start))
709 (defun notmuch-search-goto-last-thread (&optional arg)
710 "Move point to the last thread in the buffer."
716 (defun notmuch-search-mode ()
717 "Major mode for searching mail with notmuch.
719 This buffer contains the results of a \"notmuch search\" of your
720 email archives. Each line in the buffer represents a single
721 thread giving a relative date for the thread and a subject.
723 Pressing RET on any line displays that thread. The '+' and '-'
724 keys can be used to add or remove tags from a thread. The 'a' key
725 is a convenience key for archiving a thread (removing the
728 Other useful commands are `notmuch-search-filter' for filtering
729 the current search based on an additional query string,
730 `notmuch-search-filter-by-tag' for filtering to include only
731 messages with a given tag, and `notmuch-search' to execute a new,
734 \\{notmuch-search-mode-map}"
736 (kill-all-local-variables)
737 (make-local-variable 'notmuch-search-query-string)
738 (make-local-variable 'notmuch-search-oldest-first)
739 (set (make-local-variable 'scroll-preserve-screen-position) t)
740 (add-to-invisibility-spec 'notmuch-search)
741 (use-local-map notmuch-search-mode-map)
742 (setq major-mode 'notmuch-search-mode
743 mode-name "notmuch-search")
744 (setq buffer-read-only t))
746 (defun notmuch-search-find-thread-id ()
750 (re-search-forward "thread:[a-fA-F0-9]*" nil t)
751 (filter-buffer-substring beg (point)))))
753 (defun notmuch-search-markup-this-thread-id ()
756 (if (re-search-forward "thread:[a-fA-F0-9]*" nil t)
759 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))))
761 (defun notmuch-search-markup-thread-ids ()
763 (goto-char (point-min))
765 (notmuch-search-markup-this-thread-id)
768 (defun notmuch-search-show-thread ()
770 (let ((thread-id (notmuch-search-find-thread-id)))
771 (if (> (length thread-id) 0)
772 (notmuch-show thread-id (current-buffer))
773 (error "End of search results"))))
775 (defun notmuch-call-notmuch-process (&rest args)
776 "Synchronously invoke \"notmuch\" with the given list of arguments.
778 Output from the process will be presented to the user as an error
779 and will also appear in a buffer named \"*Notmuch errors*\"."
780 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
781 (with-current-buffer error-buffer
783 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
786 (with-current-buffer error-buffer
787 (let ((beg (point-min))
788 (end (- (point-max) 1)))
789 (error (buffer-substring beg end))
792 (defun notmuch-search-set-tags (tags)
795 (re-search-backward "(")
798 (inhibit-read-only t))
799 (re-search-forward ")")
802 (delete-region beg end)
803 (insert (mapconcat 'identity tags " "))))))
805 (defun notmuch-search-get-tags ()
808 (re-search-backward "(")
809 (let ((beg (+ (point) 1)))
810 (re-search-forward ")")
811 (let ((end (- (point) 1)))
812 (split-string (buffer-substring beg end))))))
814 (defun notmuch-search-add-tag (tag)
815 (interactive "sTag to add: ")
816 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
817 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
819 (defun notmuch-search-remove-tag (tag)
820 (interactive "sTag to remove: ")
821 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
822 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
824 (defun notmuch-search-archive-thread ()
825 "Archive the current thread (remove its \"inbox\" tag).
827 This function advances point to the next line when finished."
829 (notmuch-search-remove-tag "inbox")
832 (defun notmuch-search (query &optional oldest-first)
833 "Run \"notmuch search\" with the given query string and display results."
834 (interactive "sNotmuch search: ")
835 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
836 (switch-to-buffer buffer)
837 (notmuch-search-mode)
838 (set 'notmuch-search-query-string query)
839 (set 'notmuch-search-oldest-first oldest-first)
840 (let ((proc (get-buffer-process (current-buffer)))
841 (inhibit-read-only t))
843 (error "notmuch search process already running for query `%s'" query)
846 (goto-char (point-min))
849 (call-process "notmuch" nil t nil "search" query)
850 (call-process "notmuch" nil t nil "search" "--reverse" query))
851 (notmuch-search-markup-thread-ids)
854 (defun notmuch-search-refresh-view ()
855 "Refresh the current view.
857 Kills the current buffer and runs a new search with the same
858 query string as the current search. If the current thread is in
859 the new search results, then point will be placed on the same
860 thread. Otherwise, point will be moved to attempt to be in the
861 same relative position within the new buffer."
864 (oldest-first notmuch-search-oldest-first)
865 (thread (notmuch-search-find-thread-id))
866 (query notmuch-search-query-string))
868 (notmuch-search query oldest-first)
869 (goto-char (point-min))
870 (if (re-search-forward (concat "^" thread) nil t)
874 (defun notmuch-search-toggle-order ()
875 "Toggle the current search order.
877 By default, the \"inbox\" view created by `notmuch' is displayed
878 in chronological order (oldest thread at the beginning of the
879 buffer), while any global searches created by `notmuch-search'
880 are displayed in reverse-chronological order (newest thread at
881 the beginning of the buffer).
883 This command toggles the sort order for the current search.
885 Note that any fitlered searches created by
886 `notmuch-search-filter' retain the search order of the parent
889 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
890 (notmuch-search-refresh-view))
892 (defun notmuch-search-filter (query)
893 "Filter the current search results based on an additional query string.
895 Runs a new search matching only messages that match both the
896 current search results AND the additional query string provided."
897 (interactive "sFilter search: ")
898 (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
900 (defun notmuch-search-filter-by-tag (tag)
901 "Filter the current search results based on a single tag.
903 Runs a new search matching only messages that match both the
904 current search results AND that are tagged with the given tag."
905 (interactive "sFilter by tag: ")
906 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
909 "Run notmuch to display all mail with tag of 'inbox'"
911 (notmuch-search "tag:inbox" t))