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))))
470 (indent-rigidly beg end depth)
471 (notmuch-show-markup-citations-region beg end depth)
472 ; Advance to the next part (if any) (so the outer loop can
473 ; determine whether we've left the current message.
474 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
475 (beginning-of-line)))))
478 (defun notmuch-show-markup-parts-region (beg end depth)
481 (while (< (point) end)
482 (notmuch-show-markup-part beg end depth))))
484 (defun notmuch-show-markup-body (depth)
485 (re-search-forward notmuch-show-body-begin-regexp)
487 (let ((beg (point-marker)))
488 (re-search-forward notmuch-show-body-end-regexp)
489 (let ((end (copy-marker (match-beginning 0))))
490 (notmuch-show-markup-parts-region beg end depth)
491 (if (not (notmuch-show-message-unread-p))
492 (overlay-put (make-overlay beg end)
493 'invisible 'notmuch-show-body-read))
498 (defun notmuch-show-markup-header (depth)
499 (re-search-forward notmuch-show-header-begin-regexp)
501 (let ((beg (point-marker)))
503 ; Inverse video for subject
504 (overlay-put (make-overlay beg (point)) 'face '((cons :inverse-video t)))
506 (let ((beg-hidden (point-marker)))
507 (re-search-forward notmuch-show-header-end-regexp)
509 (let ((end (point-marker)))
510 (indent-rigidly beg end depth)
511 (overlay-put (make-overlay beg-hidden end)
512 'invisible 'notmuch-show-header)
514 (set-marker beg-hidden nil)
518 (defun notmuch-show-markup-message ()
519 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
521 (re-search-forward notmuch-show-depth-regexp)
522 (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1)))))
523 (notmuch-show-markup-header depth)
524 (notmuch-show-markup-body depth)))
525 (goto-char (point-max))))
527 (defun notmuch-show-hide-markers ()
529 (goto-char (point-min))
531 (if (re-search-forward notmuch-show-marker-regexp nil t)
533 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
534 'invisible 'notmuch-show-marker))
535 (goto-char (point-max))))))
537 (defun notmuch-show-markup-messages ()
539 (goto-char (point-min))
541 (notmuch-show-markup-message)))
542 (notmuch-show-hide-markers))
544 (defun notmuch-show-toggle-citations-visible ()
545 "Toggle visibility of citations"
547 (if notmuch-show-citations-visible
548 (add-to-invisibility-spec 'notmuch-show-citation)
549 (remove-from-invisibility-spec 'notmuch-show-citation))
550 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
551 ; Need to force the redisplay for some reason
552 (force-window-update)
555 (defun notmuch-show-toggle-signatures-visible ()
556 "Toggle visibility of signatures"
558 (if notmuch-show-signatures-visible
559 (add-to-invisibility-spec 'notmuch-show-signature)
560 (remove-from-invisibility-spec 'notmuch-show-signature))
561 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
562 ; Need to force the redisplay for some reason
563 (force-window-update)
566 (defun notmuch-show-toggle-headers-visible ()
567 "Toggle visibility of header fields"
569 (if notmuch-show-headers-visible
570 (add-to-invisibility-spec 'notmuch-show-header)
571 (remove-from-invisibility-spec 'notmuch-show-header))
572 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
573 ; Need to force the redisplay for some reason
574 (force-window-update)
577 (defun notmuch-show-toggle-body-read-visible ()
578 "Toggle visibility of message bodies of read messages"
580 (if notmuch-show-body-read-visible
581 (add-to-invisibility-spec 'notmuch-show-body-read)
582 (remove-from-invisibility-spec 'notmuch-show-body-read))
583 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
584 ; Need to force the redisplay for some reason
585 (force-window-update)
589 (defun notmuch-show-mode ()
590 "Major mode for viewing a thread with notmuch.
592 This buffer contains the results of the \"notmuch show\" command
593 for displaying a single thread of email from your email archives.
595 By default, various components of email messages, (citations,
596 signatures, already-read messages), are invisible to help you
597 focus on the most important things, (new text from unread
598 messages). See the various commands below for toggling the
599 visibility of hidden components.
601 The `notmuch-show-next-message' and
602 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
603 default), allow you to navigate to the next and previous
604 messages. Each time you navigate away from a message with
605 `notmuch-show-next-message' the current message will have its
606 \"unread\" tag removed.
608 You can add or remove tags from the current message with '+' and
609 '-'. You can also archive all messages in the current
610 view, (remove the \"inbox\" tag from each), with
611 `notmuch-show-archive-thread' (bound to 'a' by default).
613 \\{notmuch-show-mode-map}"
615 (kill-all-local-variables)
616 (set (make-local-variable 'notmuch-show-headers-visible) t)
617 (notmuch-show-toggle-headers-visible)
618 (set (make-local-variable 'notmuch-show-body-read-visible) t)
619 (notmuch-show-toggle-body-read-visible)
620 (set (make-local-variable 'notmuch-show-citations-visible) t)
621 (notmuch-show-toggle-citations-visible)
622 (set (make-local-variable 'notmuch-show-signatures-visible) t)
623 (notmuch-show-toggle-signatures-visible)
624 (add-to-invisibility-spec 'notmuch-show-marker)
625 (use-local-map notmuch-show-mode-map)
626 (setq major-mode 'notmuch-show-mode
627 mode-name "notmuch-show")
628 (setq buffer-read-only t))
630 (defun notmuch-show (thread-id &optional parent-buffer)
631 "Run \"notmuch show\" with the given thread ID and display results.
633 The optional PARENT-BUFFER is the notmuch-search buffer from
634 which this notmuch-show command was executed, (so that the next
635 thread from that buffer can be show when done with this one)."
636 (interactive "sNotmuch show: ")
637 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
638 (switch-to-buffer buffer)
640 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
641 (let ((proc (get-buffer-process (current-buffer)))
642 (inhibit-read-only t))
644 (error "notmuch search process already running for query `%s'" query)
647 (goto-char (point-min))
649 (call-process "notmuch" nil t nil "show" thread-id)
650 (notmuch-show-markup-messages)
652 ; Move straight to the first unread message
653 (if (not (notmuch-show-message-unread-p))
655 (notmuch-show-next-unread-message)
656 ; But if there are no unread messages, go back to the
657 ; beginning of the buffer, and open up the bodies of all
659 (if (not (notmuch-show-message-unread-p))
661 (goto-char (point-min))
662 (notmuch-show-toggle-body-read-visible)))))
665 (defvar notmuch-search-authors-width 40
666 "Number of columns to use to diplay authors in a notmuch-search buffer.")
668 (defvar notmuch-search-mode-map
669 (let ((map (make-sparse-keymap)))
670 (define-key map "a" 'notmuch-search-archive-thread)
671 (define-key map "b" 'notmuch-search-scroll-down)
672 (define-key map "f" 'notmuch-search-filter)
673 (define-key map "m" 'message-mail)
674 (define-key map "n" 'next-line)
675 (define-key map "o" 'notmuch-search-toggle-order)
676 (define-key map "p" 'previous-line)
677 (define-key map "q" 'kill-this-buffer)
678 (define-key map "s" 'notmuch-search)
679 (define-key map "t" 'notmuch-search-filter-by-tag)
680 (define-key map "x" 'kill-this-buffer)
681 (define-key map (kbd "RET") 'notmuch-search-show-thread)
682 (define-key map "+" 'notmuch-search-add-tag)
683 (define-key map "-" 'notmuch-search-remove-tag)
684 (define-key map "<" 'beginning-of-buffer)
685 (define-key map ">" 'notmuch-search-goto-last-thread)
686 (define-key map "=" 'notmuch-search-refresh-view)
687 (define-key map "\M->" 'notmuch-search-goto-last-thread)
688 (define-key map " " 'notmuch-search-scroll-up)
689 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
691 "Keymap for \"notmuch search\" buffers.")
692 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
694 (defun notmuch-search-scroll-up ()
695 "Scroll up, moving point to last message in thread if at end."
699 ((end-of-buffer) (notmuch-search-goto-last-thread))))
701 (defun notmuch-search-scroll-down ()
702 "Scroll down, moving point to first message in thread if at beginning."
704 ; I don't know why scroll-down doesn't signal beginning-of-buffer
705 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
707 ; So instead of trapping a signal we instead check whether the
708 ; window begins on the first line of the buffer and if so, move
709 ; directly to that position. (We have to count lines since the
710 ; window-start position is not the same as point-min due to the
711 ; invisible thread-ID characters on the first line.
712 (if (equal (count-lines (point-min) (window-start)) 1)
713 (goto-char (window-start))
716 (defun notmuch-search-goto-last-thread (&optional arg)
717 "Move point to the last thread in the buffer."
723 (defun notmuch-search-mode ()
724 "Major mode for searching mail with notmuch.
726 This buffer contains the results of a \"notmuch search\" of your
727 email archives. Each line in the buffer represents a single
728 thread giving a relative date for the thread and a subject.
730 Pressing RET on any line displays that thread. The '+' and '-'
731 keys can be used to add or remove tags from a thread. The 'a' key
732 is a convenience key for archiving a thread (removing the
735 Other useful commands are `notmuch-search-filter' for filtering
736 the current search based on an additional query string,
737 `notmuch-search-filter-by-tag' for filtering to include only
738 messages with a given tag, and `notmuch-search' to execute a new,
741 \\{notmuch-search-mode-map}"
743 (kill-all-local-variables)
744 (make-local-variable 'notmuch-search-query-string)
745 (make-local-variable 'notmuch-search-oldest-first)
746 (set (make-local-variable 'scroll-preserve-screen-position) t)
747 (add-to-invisibility-spec 'notmuch-search)
748 (use-local-map notmuch-search-mode-map)
749 (setq major-mode 'notmuch-search-mode
750 mode-name "notmuch-search")
751 (setq buffer-read-only t))
753 (defun notmuch-search-find-thread-id ()
757 (re-search-forward "thread:[a-fA-F0-9]*" nil t)
758 (filter-buffer-substring beg (point)))))
760 (defun notmuch-search-markup-this-thread-id ()
763 (if (re-search-forward "thread:[a-fA-F0-9]*" nil t)
766 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)
767 (re-search-forward ".*\\[[0-9]*/[0-9]*\\] \\([^;]*\\)\\(;\\)")
768 (let* ((authors (buffer-substring (match-beginning 1) (match-end 1)))
769 (authors-length (length authors)))
770 ;; Drop the semi-colon
771 (replace-match "" t nil nil 2)
772 (if (<= authors-length notmuch-search-authors-width)
773 (replace-match (concat authors (make-string
774 (- notmuch-search-authors-width
775 authors-length) ? )) t t nil 1)
776 (replace-match (concat (substring authors 0 (- notmuch-search-authors-width 3)) "...") t t nil 1)))))))
778 (defun notmuch-search-markup-thread-ids ()
780 (goto-char (point-min))
782 (notmuch-search-markup-this-thread-id)
785 (defun notmuch-search-show-thread ()
787 (let ((thread-id (notmuch-search-find-thread-id)))
788 (if (> (length thread-id) 0)
789 (notmuch-show thread-id (current-buffer))
790 (error "End of search results"))))
792 (defun notmuch-call-notmuch-process (&rest args)
793 "Synchronously invoke \"notmuch\" with the given list of arguments.
795 Output from the process will be presented to the user as an error
796 and will also appear in a buffer named \"*Notmuch errors*\"."
797 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
798 (with-current-buffer error-buffer
800 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
803 (with-current-buffer error-buffer
804 (let ((beg (point-min))
805 (end (- (point-max) 1)))
806 (error (buffer-substring beg end))
809 (defun notmuch-search-set-tags (tags)
812 (re-search-backward "(")
815 (inhibit-read-only t))
816 (re-search-forward ")")
819 (delete-region beg end)
820 (insert (mapconcat 'identity tags " "))))))
822 (defun notmuch-search-get-tags ()
825 (re-search-backward "(")
826 (let ((beg (+ (point) 1)))
827 (re-search-forward ")")
828 (let ((end (- (point) 1)))
829 (split-string (buffer-substring beg end))))))
831 (defun notmuch-search-add-tag (tag)
832 (interactive "sTag to add: ")
833 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
834 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
836 (defun notmuch-search-remove-tag (tag)
837 (interactive "sTag to remove: ")
838 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
839 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
841 (defun notmuch-search-archive-thread ()
842 "Archive the current thread (remove its \"inbox\" tag).
844 This function advances point to the next line when finished."
846 (notmuch-search-remove-tag "inbox")
849 (defun notmuch-search (query &optional oldest-first)
850 "Run \"notmuch search\" with the given query string and display results."
851 (interactive "sNotmuch search: ")
852 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
853 (switch-to-buffer buffer)
854 (notmuch-search-mode)
855 (set 'notmuch-search-query-string query)
856 (set 'notmuch-search-oldest-first oldest-first)
857 (let ((proc (get-buffer-process (current-buffer)))
858 (inhibit-read-only t))
860 (error "notmuch search process already running for query `%s'" query)
863 (goto-char (point-min))
866 (call-process "notmuch" nil t nil "search" query)
867 (call-process "notmuch" nil t nil "search" "--reverse" query))
868 (notmuch-search-markup-thread-ids)
871 (defun notmuch-search-refresh-view ()
872 "Refresh the current view.
874 Kills the current buffer and runs a new search with the same
875 query string as the current search. If the current thread is in
876 the new search results, then point will be placed on the same
877 thread. Otherwise, point will be moved to attempt to be in the
878 same relative position within the new buffer."
881 (oldest-first notmuch-search-oldest-first)
882 (thread (notmuch-search-find-thread-id))
883 (query notmuch-search-query-string))
885 (notmuch-search query oldest-first)
886 (goto-char (point-min))
887 (if (re-search-forward (concat "^" thread) nil t)
891 (defun notmuch-search-toggle-order ()
892 "Toggle the current search order.
894 By default, the \"inbox\" view created by `notmuch' is displayed
895 in chronological order (oldest thread at the beginning of the
896 buffer), while any global searches created by `notmuch-search'
897 are displayed in reverse-chronological order (newest thread at
898 the beginning of the buffer).
900 This command toggles the sort order for the current search.
902 Note that any fitlered searches created by
903 `notmuch-search-filter' retain the search order of the parent
906 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
907 (notmuch-search-refresh-view))
909 (defun notmuch-search-filter (query)
910 "Filter the current search results based on an additional query string.
912 Runs a new search matching only messages that match both the
913 current search results AND the additional query string provided."
914 (interactive "sFilter search: ")
915 (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
917 (defun notmuch-search-filter-by-tag (tag)
918 "Filter the current search results based on a single tag.
920 Runs a new search matching only messages that match both the
921 current search results AND that are tagged with the given tag."
922 (interactive "sFilter by tag: ")
923 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
926 "Run notmuch to display all mail with tag of 'inbox'"
928 (notmuch-search "tag:inbox" t))