1 ; notmuch.el --- run notmuch within emacs
3 ; Copyright © Carl Worth
5 ; This file is part of Notmuch.
7 ; Notmuch is free software: you can redistribute it and/or modify it
8 ; under the terms of the GNU General Public License as published by
9 ; the Free Software Foundation, either version 3 of the License, or
10 ; (at your option) any later version.
12 ; Notmuch is distributed in the hope that it will be useful, but
13 ; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ; General Public License for more details.
17 ; You should have received a copy of the GNU General Public License
18 ; along with Notmuch. If not, see <http://www.gnu.org/licenses/>.
20 ; Authors: Carl Worth <cworth@cworth.org>
22 ; This is an emacs-based interface to the notmuch mail system.
24 ; You will first need to have the notmuch program installed and have a
25 ; notmuch database built in order to use this. See
26 ; http://notmuchmail.org for details.
28 ; To install this software, copy it to a directory that is on the
29 ; `load-path' variable within emacs (a good candidate is
30 ; /usr/local/share/emacs/site-lisp). If you are viewing this from the
31 ; notmuch source distribution then you can simply run:
33 ; sudo make install-emacs
37 ; Then, to actually run it, add:
41 ; to your ~/.emacs file, and then run "M-x notmuch" from within emacs,
46 ; Have fun, and let us know if you have any comment, questions, or
47 ; kudos: Notmuch list <notmuch@notmuchmail.org> (subscription is not
48 ; required, but is available from http://notmuchmail.org).
54 (defvar notmuch-show-mode-map
55 (let ((map (make-sparse-keymap)))
56 (define-key map "?" 'notmuch-help)
57 (define-key map "q" 'kill-this-buffer)
58 (define-key map (kbd "C-p") 'notmuch-show-previous-line)
59 (define-key map (kbd "C-n") 'notmuch-show-next-line)
60 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
61 (define-key map (kbd "TAB") 'notmuch-show-next-button)
62 (define-key map "s" 'notmuch-search)
63 (define-key map "m" 'message-mail)
64 (define-key map "f" 'notmuch-show-forward-current)
65 (define-key map "r" 'notmuch-show-reply)
66 (define-key map "|" 'notmuch-show-pipe-message)
67 (define-key map "w" 'notmuch-show-save-attachments)
68 (define-key map "V" 'notmuch-show-view-raw-message)
69 (define-key map "v" 'notmuch-show-view-all-mime-parts)
70 (define-key map "-" 'notmuch-show-remove-tag)
71 (define-key map "+" 'notmuch-show-add-tag)
72 (define-key map "X" 'notmuch-show-mark-read-then-archive-then-exit)
73 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
74 (define-key map "A" 'notmuch-show-mark-read-then-archive-thread)
75 (define-key map "a" 'notmuch-show-archive-thread)
76 (define-key map "p" 'notmuch-show-previous-message)
77 (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
78 (define-key map "n" 'notmuch-show-next-message)
79 (define-key map (kbd "DEL") 'notmuch-show-rewind)
80 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
82 "Keymap for \"notmuch show\" buffers.")
83 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
85 (defvar notmuch-show-signature-regexp "\\(-- ?\\|_+\\)$"
86 "Pattern to match a line that separates content from signature.
88 The regexp can (and should) include $ to match the end of the
89 line, but should not include ^ to match the beginning of the
90 line. This is because notmuch may have inserted additional space
91 for indentation at the beginning of the line. But notmuch will
92 move past the indentation when testing this pattern, (so that the
93 pattern can still test against the entire line).")
95 (defvar notmuch-show-signature-lines-max 12
96 "Maximum length of signature that will be hidden by default.")
98 (defvar notmuch-command "notmuch"
99 "Command to run the notmuch binary.")
101 (defvar notmuch-show-message-begin-regexp "\fmessage{")
102 (defvar notmuch-show-message-end-regexp "\fmessage}")
103 (defvar notmuch-show-header-begin-regexp "\fheader{")
104 (defvar notmuch-show-header-end-regexp "\fheader}")
105 (defvar notmuch-show-body-begin-regexp "\fbody{")
106 (defvar notmuch-show-body-end-regexp "\fbody}")
107 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
108 (defvar notmuch-show-attachment-end-regexp "\fattachment}")
109 (defvar notmuch-show-part-begin-regexp "\fpart{")
110 (defvar notmuch-show-part-end-regexp "\fpart}")
111 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
113 (defvar notmuch-show-id-regexp "\\(id:[^ ]*\\)")
114 (defvar notmuch-show-depth-match-regexp " depth:\\([0-9]*\\).*match:\\([01]\\) ")
115 (defvar notmuch-show-filename-regexp "filename:\\(.*\\)$")
116 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
118 (defvar notmuch-show-parent-buffer nil)
119 (defvar notmuch-show-body-read-visible nil)
120 (defvar notmuch-show-citations-visible nil)
121 (defvar notmuch-show-signatures-visible nil)
122 (defvar notmuch-show-headers-visible nil)
124 ; XXX: This should be a generic function in emacs somewhere, not here
125 (defun point-invisible-p ()
126 "Return whether the character at point is invisible.
128 Here visibility is determined by `buffer-invisibility-spec' and
129 the invisible property of any overlays for point. It doesn't have
130 anything to do with whether point is currently being displayed
131 within the current window."
132 (let ((prop (get-char-property (point) 'invisible)))
133 (if (eq buffer-invisibility-spec t)
135 (or (memq prop buffer-invisibility-spec)
136 (assq prop buffer-invisibility-spec)))))
138 (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
140 (with-output-to-string
141 (with-current-buffer standard-output
142 (apply 'call-process notmuch-command nil t nil "search-tags" search-terms)))))
143 (completing-read prompt (split-string tag-list "\n+" t) nil nil nil)))
145 (defun notmuch-show-next-line ()
146 "Like builtin `next-line' but ensuring we end on a visible character.
148 By advancing forward until reaching a visible character.
150 Unlike builtin `next-line' this version accepts no arguments."
152 (set 'this-command 'next-line)
153 (call-interactively 'next-line)
154 (while (point-invisible-p)
157 (defun notmuch-show-previous-line ()
158 "Like builtin `previous-line' but ensuring we end on a visible character.
160 By advancing forward until reaching a visible character.
162 Unlike builtin `previous-line' this version accepts no arguments."
164 (set 'this-command 'previous-line)
165 (call-interactively 'previous-line)
166 (while (point-invisible-p)
169 (defun notmuch-show-get-message-id ()
172 (if (not (looking-at notmuch-show-message-begin-regexp))
173 (re-search-backward notmuch-show-message-begin-regexp))
174 (re-search-forward notmuch-show-id-regexp)
175 (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
177 (defun notmuch-show-get-filename ()
180 (if (not (looking-at notmuch-show-message-begin-regexp))
181 (re-search-backward notmuch-show-message-begin-regexp))
182 (re-search-forward notmuch-show-filename-regexp)
183 (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
185 (defun notmuch-show-set-tags (tags)
188 (if (not (looking-at notmuch-show-message-begin-regexp))
189 (re-search-backward notmuch-show-message-begin-regexp))
190 (re-search-forward notmuch-show-tags-regexp)
191 (let ((inhibit-read-only t)
192 (beg (match-beginning 1))
194 (delete-region beg end)
196 (insert (mapconcat 'identity tags " ")))))
198 (defun notmuch-show-get-tags ()
201 (if (not (looking-at notmuch-show-message-begin-regexp))
202 (re-search-backward notmuch-show-message-begin-regexp))
203 (re-search-forward notmuch-show-tags-regexp)
204 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
206 (defun notmuch-show-add-tag (&rest toadd)
207 "Add a tag to the current message."
209 (list (notmuch-select-tag-with-completion "Tag to add: ")))
210 (apply 'notmuch-call-notmuch-process
212 (mapcar (lambda (s) (concat "+" s)) toadd))
213 (cons (notmuch-show-get-message-id) nil)))
214 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
216 (defun notmuch-show-remove-tag (&rest toremove)
217 "Remove a tag from the current message."
219 (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-show-get-message-id))))
220 (let ((tags (notmuch-show-get-tags)))
221 (if (intersection tags toremove :test 'string=)
223 (apply 'notmuch-call-notmuch-process
225 (mapcar (lambda (s) (concat "-" s)) toremove))
226 (cons (notmuch-show-get-message-id) nil)))
227 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
229 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
231 (goto-char (point-min))
234 (notmuch-show-remove-tag "unread" "inbox")
235 (notmuch-show-remove-tag "inbox"))
238 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
239 (goto-char (point-max)))))
240 (let ((parent-buffer notmuch-show-parent-buffer))
244 (switch-to-buffer parent-buffer)
246 (notmuch-search-show-thread)))))
248 (defun notmuch-show-mark-read-then-archive-thread ()
249 "Remove unread tags from thread, then archive and show next thread.
251 Archive each message currently shown by removing the \"unread\"
252 and \"inbox\" tag from each. Then kill this buffer and show the
253 next thread from the search from which this thread was originally
256 Note: This command is safe from any race condition of new messages
257 being delivered to the same thread. It does not archive the
258 entire thread, but only the messages shown in the current
261 (notmuch-show-archive-thread-maybe-mark-read t))
263 (defun notmuch-show-archive-thread ()
264 "Archive each message in thread, then show next thread from search.
266 Archive each message currently shown by removing the \"inbox\"
267 tag from each. Then kill this buffer and show the next thread
268 from the search from which this thread was originally shown.
270 Note: This command is safe from any race condition of new messages
271 being delivered to the same thread. It does not archive the
272 entire thread, but only the messages shown in the current
275 (notmuch-show-archive-thread-maybe-mark-read nil))
277 (defun notmuch-show-archive-thread-then-exit ()
278 "Archive each message in thread, then exit back to search results."
280 (notmuch-show-archive-thread)
283 (defun notmuch-show-mark-read-then-archive-then-exit ()
284 "Remove unread tags from thread, then archive and exit to search results."
286 (notmuch-show-mark-read-then-archive-thread)
289 (defun notmuch-show-view-raw-message ()
290 "View the raw email of the current message."
292 (view-file (notmuch-show-get-filename)))
294 (defmacro with-current-notmuch-show-message (&rest body)
295 "Evaluate body with current buffer set to the text of current message"
297 (let ((filename (notmuch-show-get-filename)))
298 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
299 (with-current-buffer buf
300 (insert-file-contents filename nil nil nil t)
302 (kill-buffer buf)))))
304 (defun notmuch-show-view-all-mime-parts ()
305 "Use external viewers to view all attachments from the current message."
307 (with-current-notmuch-show-message
308 (mm-display-parts (mm-dissect-buffer))))
310 (defun notmuch-foreach-mime-part (function mm-handle)
311 (cond ((stringp (car mm-handle))
312 (dolist (part (cdr mm-handle))
313 (notmuch-foreach-mime-part function part)))
314 ((bufferp (car mm-handle))
315 (funcall function mm-handle))
316 (t (dolist (part mm-handle)
317 (notmuch-foreach-mime-part function part)))))
319 (defun notmuch-count-attachments (mm-handle)
321 (notmuch-foreach-mime-part
323 (let ((disposition (mm-handle-disposition p)))
324 (and (listp disposition)
325 (equal (car disposition) "attachment")
330 (defun notmuch-save-attachments (mm-handle &optional queryp)
331 (notmuch-foreach-mime-part
333 (let ((disposition (mm-handle-disposition p)))
334 (and (listp disposition)
335 (equal (car disposition) "attachment")
338 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
342 (defun notmuch-show-save-attachments ()
343 "Save all attachments from the current message."
345 (with-current-notmuch-show-message
346 (let ((mm-handle (mm-dissect-buffer)))
347 (notmuch-save-attachments
348 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
351 (defun notmuch-reply (query-string)
352 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
353 (call-process notmuch-command nil t nil "reply" query-string)
354 (message-insert-signature)
355 (goto-char (point-min))
356 (if (re-search-forward "^$" nil t)
358 (insert "--text follows this line--")
362 (defun notmuch-show-reply ()
363 "Begin composing a reply to the current message in a new buffer."
365 (let ((message-id (notmuch-show-get-message-id)))
366 (notmuch-reply message-id)))
368 (defun notmuch-show-forward-current ()
369 "Forward the current message."
371 (with-current-notmuch-show-message
374 (defun notmuch-show-pipe-message (command)
375 "Pipe the contents of the current message to the given command.
377 The given command will be executed with the raw contents of the
378 current email message as stdin. Anything printed by the command
379 to stdout or stderr will appear in the *Messages* buffer."
380 (interactive "sPipe message to command: ")
381 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
382 (list command " < " (shell-quote-argument (notmuch-show-get-filename)))))
384 (defun notmuch-show-move-to-current-message-summary-line ()
385 "Move to the beginning of the one-line summary of the current message.
387 This gives us a stable place to move to and work from since the
388 summary line is always visible. This is important since moving to
389 an invisible location is unreliable, (the main command loop moves
390 point either forward or backward to the next visible character
391 when a command ends with point on an invisible character).
393 Emits an error if point is not within a valid message, (that is
394 no pattern of `notmuch-show-message-begin-regexp' could be found
395 by searching backward)."
397 (if (not (looking-at notmuch-show-message-begin-regexp))
398 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
400 (error "Not within a valid message."))
403 (defun notmuch-show-last-message-p ()
404 "Predicate testing whether point is within the last message."
405 (save-window-excursion
407 (notmuch-show-move-to-current-message-summary-line)
408 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
410 (defun notmuch-show-message-unread-p ()
411 "Predicate testing whether current message is unread."
412 (member "unread" (notmuch-show-get-tags)))
414 (defun notmuch-show-message-open-p ()
415 "Predicate testing whether current message is open (body is visible)."
416 (let ((btn (previous-button (point) t)))
417 (while (not (button-has-type-p btn 'notmuch-button-body-toggle-type))
418 (setq btn (previous-button (button-start btn))))
419 (not (invisible-p (button-get btn 'invisibility-spec)))))
421 (defun notmuch-show-next-message ()
422 "Advance to the beginning of the next message in the buffer.
424 Moves to the last visible character of the current message if
425 already on the last message in the buffer.
427 Returns nil if already on the last message in the buffer."
429 (notmuch-show-move-to-current-message-summary-line)
430 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
432 (notmuch-show-move-to-current-message-summary-line)
435 (goto-char (- (point-max) 1))
436 (while (point-invisible-p)
441 (defun notmuch-show-find-next-message ()
442 "Returns the position of the next message in the buffer.
444 Or the position of the last visible character of the current
445 message if already within the last message in the buffer."
446 ; save-excursion doesn't save our window position
447 ; save-window-excursion doesn't save point
448 ; Looks like we have to use both.
450 (save-window-excursion
451 (notmuch-show-next-message)
454 (defun notmuch-show-next-unread-message ()
455 "Advance to the beginning of the next unread message in the buffer.
457 Moves to the last visible character of the current message if
458 there are no more unread messages past the current point."
459 (notmuch-show-next-message)
460 (while (and (not (notmuch-show-last-message-p))
461 (not (notmuch-show-message-unread-p)))
462 (notmuch-show-next-message))
463 (if (not (notmuch-show-message-unread-p))
464 (notmuch-show-next-message)))
466 (defun notmuch-show-next-open-message ()
467 "Advance to the next open message (that is, body is not invisible)."
468 (while (and (notmuch-show-next-message)
469 (not (notmuch-show-message-open-p)))))
471 (defun notmuch-show-previous-message ()
472 "Backup to the beginning of the previous message in the buffer.
474 If within a message rather than at the beginning of it, then
475 simply move to the beginning of the current message."
477 (let ((start (point)))
478 (notmuch-show-move-to-current-message-summary-line)
479 (if (not (< (point) start))
480 ; Go backward twice to skip the current message's marker
482 (re-search-backward notmuch-show-message-begin-regexp nil t)
483 (re-search-backward notmuch-show-message-begin-regexp nil t)
484 (notmuch-show-move-to-current-message-summary-line)
488 (defun notmuch-show-find-previous-message ()
489 "Returns the position of the previous message in the buffer.
491 Or the position of the beginning of the current message if point
492 is originally within the message rather than at the beginning of
494 ; save-excursion doesn't save our window position
495 ; save-window-excursion doesn't save point
496 ; Looks like we have to use both.
498 (save-window-excursion
499 (notmuch-show-previous-message)
502 (defun notmuch-show-mark-read-then-next-open-message ()
503 "Remove unread tag from this message, then advance to next open message."
505 (notmuch-show-remove-tag "unread")
506 (notmuch-show-next-open-message))
508 (defun notmuch-show-rewind ()
509 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-marking-read-and-archiving]).
511 Specifically, if the beginning of the previous email is fewer
512 than `window-height' lines from the current point, move to it
513 just like `notmuch-show-previous-message'.
515 Otherwise, just scroll down a screenful of the current message.
517 This command does not modify any message tags, (it does not undo
518 any effects from previous calls to
519 `notmuch-show-advance-marking-read-and-archiving'."
521 (let ((previous (notmuch-show-find-previous-message)))
522 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
526 ((beginning-of-buffer) nil))
527 (goto-char (window-start)))
528 (notmuch-show-previous-message))))
530 (defun notmuch-show-advance-marking-read-and-archiving ()
531 "Advance through thread, marking read and archiving.
533 This command is intended to be one of the simplest ways to
534 process a thread of email. It does the following:
536 If the current message in the thread is not yet fully visible,
537 scroll by a near screenful to read more of the message.
539 Otherwise, (the end of the current message is already within the
540 current window), remove the \"unread\" tag (if present) from the
541 current message and advance to the next open message.
543 Finally, if there is no further message to advance to, and this
544 last message is already read, then archive the entire current
545 thread, (remove the \"inbox\" tag from each message). Also kill
546 this buffer, and display the next thread from the search from
547 which this thread was originally shown."
549 (let ((next (notmuch-show-find-next-message))
550 (unread (notmuch-show-message-unread-p)))
551 (if (> next (window-end))
553 (let ((last (notmuch-show-last-message-p)))
554 (notmuch-show-mark-read-then-next-open-message)
556 (notmuch-show-archive-thread))))))
558 (defun notmuch-show-next-button ()
559 "Advance point to the next button in the buffer."
561 (goto-char (button-start (next-button (point)))))
563 (defun notmuch-show-previous-button ()
564 "Move point back to the previous button in the buffer."
566 (goto-char (button-start (previous-button (point)))))
568 (defun notmuch-toggle-invisible-action (cite-button)
569 (let ((invis-spec (button-get cite-button 'invisibility-spec)))
570 (if (invisible-p invis-spec)
571 (remove-from-invisibility-spec invis-spec)
572 (add-to-invisibility-spec invis-spec)
574 (force-window-update)
577 (define-button-type 'notmuch-button-invisibility-toggle-type
578 'action 'notmuch-toggle-invisible-action
581 (define-button-type 'notmuch-button-citation-toggle-type 'help-echo "mouse-1, RET: Show citation"
582 :supertype 'notmuch-button-invisibility-toggle-type)
583 (define-button-type 'notmuch-button-signature-toggle-type 'help-echo "mouse-1, RET: Show signature"
584 :supertype 'notmuch-button-invisibility-toggle-type)
585 (define-button-type 'notmuch-button-headers-toggle-type 'help-echo "mouse-1, RET: Show headers"
586 :supertype 'notmuch-button-invisibility-toggle-type)
587 (define-button-type 'notmuch-button-body-toggle-type
588 'help-echo "mouse-1, RET: Show message"
589 'face 'notmuch-message-summary-face
590 :supertype 'notmuch-button-invisibility-toggle-type)
592 (defun notmuch-show-markup-citations-region (beg end depth)
595 (while (< (point) end)
596 (let ((beg-sub (point-marker))
597 (indent (make-string depth ? ))
598 (citation "[[:space:]]*>"))
599 (if (looking-at citation)
601 (while (looking-at citation)
603 (let ((overlay (make-overlay beg-sub (point)))
604 (invis-spec (make-symbol "notmuch-citation-region")))
605 (add-to-invisibility-spec invis-spec)
606 (overlay-put overlay 'invisible invis-spec)
609 (concat "[" (number-to-string (count-lines beg-sub (point)))
610 "-line citation.]")))
611 (goto-char (- beg-sub 1))
612 (insert (concat "\n" indent))
613 (insert-button cite-button-text
614 'invisibility-spec invis-spec
615 :type 'notmuch-button-citation-toggle-type)
617 (goto-char (+ (length cite-button-text) p))
619 (move-to-column depth)
620 (if (looking-at notmuch-show-signature-regexp)
621 (let ((sig-lines (- (count-lines beg-sub end) 1)))
622 (if (<= sig-lines notmuch-show-signature-lines-max)
624 (let ((invis-spec (make-symbol "notmuch-signature-region")))
625 (add-to-invisibility-spec invis-spec)
626 (overlay-put (make-overlay beg-sub end)
627 'invisible invis-spec)
629 (goto-char (- beg-sub 1))
630 (insert (concat "\n" indent))
631 (let ((sig-button-text (concat "[" (number-to-string sig-lines)
632 "-line signature.]")))
633 (insert-button sig-button-text 'invisibility-spec invis-spec
634 :type 'notmuch-button-signature-toggle-type)
640 (defun notmuch-show-markup-part (beg end depth)
641 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
644 (let ((beg (point-marker)))
645 (re-search-forward notmuch-show-part-end-regexp)
646 (let ((end (copy-marker (match-beginning 0))))
650 (indent-rigidly beg end depth)
651 (notmuch-show-markup-citations-region beg end depth)
652 ; Advance to the next part (if any) (so the outer loop can
653 ; determine whether we've left the current message.
654 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
655 (beginning-of-line)))))
658 (defun notmuch-show-markup-parts-region (beg end depth)
661 (while (< (point) end)
662 (notmuch-show-markup-part beg end depth))))
664 (defun notmuch-show-markup-body (depth match btn)
665 "Markup a message body, (indenting, buttonizing citations,
666 etc.), and conditionally hiding the body itself if the message
667 has been read and does not match the current search.
669 DEPTH specifies the depth at which this message appears in the
670 tree of the current thread, (the top-level messages have depth 0
671 and each reply increases depth by 1). MATCH indicates whether
672 this message is regarded as matching the current search. BTN is
673 the button which is used to toggle the visibility of this
676 When this function is called, point must be within the message, but
677 before the delimiter marking the beginning of the body."
678 (re-search-forward notmuch-show-body-begin-regexp)
680 (let ((beg (point-marker)))
681 (re-search-forward notmuch-show-body-end-regexp)
682 (let ((end (copy-marker (match-beginning 0))))
683 (notmuch-show-markup-parts-region beg end depth)
684 (let ((invis-spec (make-symbol "notmuch-show-body-read")))
685 (overlay-put (make-overlay beg end)
686 'invisible invis-spec)
687 (button-put btn 'invisibility-spec invis-spec)
688 (if (not (or (notmuch-show-message-unread-p) match))
689 (add-to-invisibility-spec invis-spec)))
694 (defun notmuch-fontify-headers ()
695 (while (looking-at "[[:space:]]")
697 (if (looking-at "[Tt]o:")
699 (overlay-put (make-overlay (point) (re-search-forward ":"))
700 'face 'message-header-name)
701 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
702 'face 'message-header-to))
703 (if (looking-at "[B]?[Cc][Cc]:")
705 (overlay-put (make-overlay (point) (re-search-forward ":"))
706 'face 'message-header-name)
707 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
708 'face 'message-header-cc))
709 (if (looking-at "[Ss]ubject:")
711 (overlay-put (make-overlay (point) (re-search-forward ":"))
712 'face 'message-header-name)
713 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
714 'face 'message-header-subject))
715 (if (looking-at "[Ff]rom:")
717 (overlay-put (make-overlay (point) (re-search-forward ":"))
718 'face 'message-header-name)
719 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
720 'face 'message-header-other)))))))
722 (defun notmuch-show-markup-header (message-begin depth)
723 "Buttonize and decorate faces in a message header.
725 MESSAGE-BEGIN is the position of the absolute first character in
726 the message (including all delimiters that will end up being
727 invisible etc.). This is to allow a button to reliably extend to
728 the beginning of the message even if point is positioned at an
729 invisible character (such as the beginning of the buffer).
731 DEPTH specifies the depth at which this message appears in the
732 tree of the current thread, (the top-level messages have depth 0
733 and each reply increases depth by 1)."
734 (re-search-forward notmuch-show-header-begin-regexp)
736 (let ((beg (point-marker))
737 (summary-end (copy-marker (line-beginning-position 2)))
738 (subject-end (copy-marker (line-end-position 2)))
739 (invis-spec (make-symbol "notmuch-show-header"))
741 (re-search-forward notmuch-show-header-end-regexp)
743 (let ((end (point-marker)))
744 (indent-rigidly beg end depth)
746 (setq btn (make-button message-begin summary-end :type 'notmuch-button-body-toggle-type))
748 (add-to-invisibility-spec invis-spec)
749 (overlay-put (make-overlay subject-end end)
750 'invisible invis-spec)
751 (make-button (line-beginning-position) subject-end
752 'invisibility-spec invis-spec
753 :type 'notmuch-button-headers-toggle-type)
754 (while (looking-at "[[:space:]]*[A-Za-z][-A-Za-z0-9]*:")
756 (notmuch-fontify-headers)
762 (set-marker summary-end nil)
763 (set-marker subject-end nil)
768 (defun notmuch-show-markup-message ()
769 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
770 (let ((message-begin (match-beginning 0)))
771 (re-search-forward notmuch-show-depth-match-regexp)
772 (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1))))
773 (match (string= "1" (buffer-substring (match-beginning 2) (match-end 2))))
775 (setq btn (notmuch-show-markup-header message-begin depth))
776 (notmuch-show-markup-body depth match btn)))
777 (goto-char (point-max))))
779 (defun notmuch-show-hide-markers ()
781 (goto-char (point-min))
783 (if (re-search-forward notmuch-show-marker-regexp nil t)
785 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
786 'invisible 'notmuch-show-marker))
787 (goto-char (point-max))))))
789 (defun notmuch-show-markup-messages ()
791 (goto-char (point-min))
793 (notmuch-show-markup-message)))
794 (notmuch-show-hide-markers))
796 (defun notmuch-documentation-first-line (symbol)
797 "Return the first line of the documentation string for SYMBOL."
798 (let ((doc (documentation symbol)))
801 (insert (documentation symbol t))
802 (goto-char (point-min))
805 (buffer-substring beg (point))))
808 (defun notmuch-prefix-key-description (key)
809 "Given a prefix key code, return a human-readable string representation.
811 This is basically just `format-kbd-macro' but we also convert ESC to M-."
812 (let ((desc (format-kbd-macro (vector key))))
813 (if (string= desc "ESC")
817 ; I would think that emacs would have code handy for walking a keymap
818 ; and generating strings for each key, and I would prefer to just call
819 ; that. But I couldn't find any (could be all implemented in C I
820 ; suppose), so I wrote my own here.
821 (defun notmuch-substitute-one-command-key-with-prefix (prefix binding)
822 "For a key binding, return a string showing a human-readable
823 representation of the prefixed key as well as the first line of
824 documentation from the bound function.
826 For a mouse binding, return nil."
827 (let ((key (car binding))
828 (action (cdr binding)))
829 (if (mouse-event-p key)
832 (let ((substitute (apply-partially 'notmuch-substitute-one-command-key-with-prefix (notmuch-prefix-key-description key))))
833 (mapconcat substitute (cdr action) "\n"))
834 (concat prefix (format-kbd-macro (vector key))
836 (notmuch-documentation-first-line action))))))
838 (defalias 'notmuch-substitute-one-command-key
839 (apply-partially 'notmuch-substitute-one-command-key-with-prefix nil))
841 (defun notmuch-substitute-command-keys (doc)
842 "Like `substitute-command-keys' but with documentation, not function names."
844 (while (string-match "\\\\{\\([^}[:space:]]*\\)}" doc beg)
845 (let ((map (substring doc (match-beginning 1) (match-end 1))))
846 (setq doc (replace-match (mapconcat 'notmuch-substitute-one-command-key
847 (cdr (symbol-value (intern map))) "\n") 1 1 doc)))
848 (setq beg (match-end 0)))
851 (defun notmuch-help ()
852 "Display help for the current notmuch mode."
854 (let* ((mode major-mode)
855 (doc (substitute-command-keys (notmuch-substitute-command-keys (documentation mode t)))))
856 (with-current-buffer (generate-new-buffer "*notmuch-help*")
858 (goto-char (point-min))
859 (set-buffer-modified-p nil)
860 (view-buffer (current-buffer) 'kill-buffer-if-not-modified))))
863 (defun notmuch-show-mode ()
864 "Major mode for viewing a thread with notmuch.
866 This buffer contains the results of the \"notmuch show\" command
867 for displaying a single thread of email from your email archives.
869 By default, various components of email messages, (citations,
870 signatures, already-read messages), are hidden. You can make
871 these parts visible by clicking with the mouse button or by
872 pressing RET after positioning the cursor on a hidden part, (for
873 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
875 Reading the thread sequentially is well-supported by pressing
876 \\[notmuch-show-advance-marking-read-and-archiving]. This will scroll the current message (if necessary),
877 advance to the next message, or advance to the next thread (if
878 already on the last message of a thread). As each message is
879 scrolled away its \"unread\" tag will be removed, and as each
880 thread is scrolled away the \"inbox\" tag will be removed from
881 each message in the thread.
883 Other commands are available to read or manipulate the thread more
884 selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages without
885 removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread without
886 scrolling through with \\[notmuch-show-advance-marking-read-and-archiving]).
888 You can add or remove arbitary tags from the current message with
889 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
891 All currently available key bindings:
893 \\{notmuch-show-mode-map}"
895 (kill-all-local-variables)
896 (add-to-invisibility-spec 'notmuch-show-marker)
897 (use-local-map notmuch-show-mode-map)
898 (setq major-mode 'notmuch-show-mode
899 mode-name "notmuch-show")
900 (setq buffer-read-only t))
902 (defgroup notmuch nil
903 "Notmuch mail reader for Emacs."
906 (defcustom notmuch-show-hook nil
907 "List of functions to call when notmuch displays a message."
909 :options '(goto-address)
912 (defcustom notmuch-search-hook nil
913 "List of functions to call when notmuch displays the search results."
915 :options '(hl-line-mode)
918 ; Make show mode a bit prettier, highlighting URLs and using word wrap
920 (defun notmuch-show-pretty-hook ()
921 (goto-address-mode 1)
924 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
925 (add-hook 'notmuch-search-hook
929 (defun notmuch-show (thread-id &optional parent-buffer)
930 "Run \"notmuch show\" with the given thread ID and display results.
932 The optional PARENT-BUFFER is the notmuch-search buffer from
933 which this notmuch-show command was executed, (so that the next
934 thread from that buffer can be show when done with this one)."
935 (interactive "sNotmuch show: ")
936 (let ((query notmuch-search-query-string)
937 (buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
938 (switch-to-buffer buffer)
940 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
941 (let ((proc (get-buffer-process (current-buffer)))
942 (inhibit-read-only t))
944 (error "notmuch search process already running for query `%s'" thread-id)
947 (goto-char (point-min))
949 (call-process notmuch-command nil t nil "show" "--entire-thread" thread-id "and (" query ")")
950 (notmuch-show-markup-messages)
952 (run-hooks 'notmuch-show-hook)
953 ; Move straight to the first open message
954 (if (not (notmuch-show-message-open-p))
955 (notmuch-show-next-open-message))
958 (defvar notmuch-search-authors-width 40
959 "Number of columns to use to display authors in a notmuch-search buffer.")
961 (defvar notmuch-search-mode-map
962 (let ((map (make-sparse-keymap)))
963 (define-key map "?" 'notmuch-help)
964 (define-key map "q" 'kill-this-buffer)
965 (define-key map "x" 'kill-this-buffer)
966 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
967 (define-key map "b" 'notmuch-search-scroll-down)
968 (define-key map " " 'notmuch-search-scroll-up)
969 (define-key map "<" 'notmuch-search-first-thread)
970 (define-key map ">" 'notmuch-search-last-thread)
971 (define-key map "p" 'notmuch-search-previous-thread)
972 (define-key map "n" 'notmuch-search-next-thread)
973 (define-key map "r" 'notmuch-search-reply-to-thread)
974 (define-key map "m" 'message-mail)
975 (define-key map "s" 'notmuch-search)
976 (define-key map "o" 'notmuch-search-toggle-order)
977 (define-key map "=" 'notmuch-search-refresh-view)
978 (define-key map "t" 'notmuch-search-filter-by-tag)
979 (define-key map "f" 'notmuch-search-filter)
980 (define-key map [mouse-1] 'notmuch-search-show-thread)
981 (define-key map "*" 'notmuch-search-operate-all)
982 (define-key map "a" 'notmuch-search-archive-thread)
983 (define-key map "-" 'notmuch-search-remove-tag)
984 (define-key map "+" 'notmuch-search-add-tag)
985 (define-key map (kbd "RET") 'notmuch-search-show-thread)
987 "Keymap for \"notmuch search\" buffers.")
988 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
990 (defvar notmuch-search-query-string)
991 (defvar notmuch-search-oldest-first t
992 "Show the oldest mail first in the search-mode")
994 (defvar notmuch-search-disjunctive-regexp "\\<[oO][rR]\\>")
996 (defun notmuch-search-scroll-up ()
997 "Move forward through search results by one window's worth."
1001 ((end-of-buffer) (notmuch-search-last-thread))))
1003 (defun notmuch-search-scroll-down ()
1004 "Move backward through the search results by one window's worth."
1006 ; I don't know why scroll-down doesn't signal beginning-of-buffer
1007 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
1009 ; So instead of trapping a signal we instead check whether the
1010 ; window begins on the first line of the buffer and if so, move
1011 ; directly to that position. (We have to count lines since the
1012 ; window-start position is not the same as point-min due to the
1013 ; invisible thread-ID characters on the first line.
1014 (if (equal (count-lines (point-min) (window-start)) 0)
1015 (goto-char (point-min))
1018 (defun notmuch-search-next-thread ()
1019 "Select the next thread in the search results."
1023 (defun notmuch-search-previous-thread ()
1024 "Select the previous thread in the search results."
1028 (defun notmuch-search-last-thread ()
1029 "Select the last thread in the search results."
1031 (goto-char (point-max))
1034 (defun notmuch-search-first-thread ()
1035 "Select the first thread in the search results."
1037 (goto-char (point-min)))
1039 (defface notmuch-message-summary-face
1040 '((((class color) (background light)) (:background "#f0f0f0"))
1041 (((class color) (background dark)) (:background "#303030")))
1042 "Face for the single-line message summary in notmuch-show-mode."
1045 (defface notmuch-tag-face
1048 (:foreground "OliveDrab1"))
1051 (:foreground "navy blue" :bold t))
1054 "Notmuch search mode face used to highligh tags."
1057 (defvar notmuch-tag-face-alist nil
1058 "List containing the tag list that need to be highlighed")
1060 (defvar notmuch-search-font-lock-keywords nil)
1063 (defun notmuch-search-mode ()
1064 "Major mode displaying results of a notmuch search.
1066 This buffer contains the results of a \"notmuch search\" of your
1067 email archives. Each line in the buffer represents a single
1068 thread giving a summary of the thread (a relative date, the
1069 number of matched messages and total messages in the thread,
1070 participants in the thread, a representative subject line, and
1073 Pressing \\[notmuch-search-show-thread] on any line displays that thread. The '\\[notmuch-search-add-tag]' and '\\[notmuch-search-remove-tag]'
1074 keys can be used to add or remove tags from a thread. The '\\[notmuch-search-archive-thread]' key
1075 is a convenience for archiving a thread (removing the \"inbox\"
1076 tag). The '\\[notmuch-search-operate-all]' key can be used to add or remove a tag from all
1077 threads in the current buffer.
1079 Other useful commands are '\\[notmuch-search-filter]' for filtering the current search
1080 based on an additional query string, '\\[notmuch-search-filter-by-tag]' for filtering to include
1081 only messages with a given tag, and '\\[notmuch-search]' to execute a new, global
1084 Complete list of currently available key bindings:
1086 \\{notmuch-search-mode-map}"
1088 (kill-all-local-variables)
1089 (make-local-variable 'notmuch-search-query-string)
1090 (make-local-variable 'notmuch-search-oldest-first)
1091 (set (make-local-variable 'scroll-preserve-screen-position) t)
1092 (add-to-invisibility-spec 'notmuch-search)
1093 (use-local-map notmuch-search-mode-map)
1094 (setq truncate-lines t)
1095 (setq major-mode 'notmuch-search-mode
1096 mode-name "notmuch-search")
1097 (setq buffer-read-only t)
1098 (if (not notmuch-tag-face-alist)
1099 (add-to-list 'notmuch-search-font-lock-keywords (list
1100 "(\\([^)]*\\))$" '(1 'notmuch-tag-face)))
1101 (let ((notmuch-search-tags (mapcar 'car notmuch-tag-face-alist)))
1102 (loop for notmuch-search-tag in notmuch-search-tags
1103 do (add-to-list 'notmuch-search-font-lock-keywords (list
1104 (concat "([^)]*\\(" notmuch-search-tag "\\)[^)]*)$")
1105 `(1 ,(cdr (assoc notmuch-search-tag notmuch-tag-face-alist))))))))
1106 (set (make-local-variable 'font-lock-defaults)
1107 '(notmuch-search-font-lock-keywords t)))
1109 (defun notmuch-search-find-thread-id ()
1110 "Return the thread for the current thread"
1111 (get-text-property (point) 'notmuch-search-thread-id))
1113 (defun notmuch-search-show-thread ()
1114 "Display the currently selected thread."
1116 (let ((thread-id (notmuch-search-find-thread-id)))
1117 (if (> (length thread-id) 0)
1118 (notmuch-show thread-id (current-buffer))
1119 (error "End of search results"))))
1121 (defun notmuch-search-reply-to-thread ()
1122 "Begin composing a reply to the entire current thread in a new buffer."
1124 (let ((message-id (notmuch-search-find-thread-id)))
1125 (notmuch-reply message-id)))
1127 (defun notmuch-call-notmuch-process (&rest args)
1128 "Synchronously invoke \"notmuch\" with the given list of arguments.
1130 Output from the process will be presented to the user as an error
1131 and will also appear in a buffer named \"*Notmuch errors*\"."
1132 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
1133 (with-current-buffer error-buffer
1135 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
1138 (with-current-buffer error-buffer
1139 (let ((beg (point-min))
1140 (end (- (point-max) 1)))
1141 (error (buffer-substring beg end))
1144 (defun notmuch-search-set-tags (tags)
1147 (re-search-backward "(")
1150 (inhibit-read-only t))
1151 (re-search-forward ")")
1153 (let ((end (point)))
1154 (delete-region beg end)
1155 (insert (mapconcat 'identity tags " "))))))
1157 (defun notmuch-search-get-tags ()
1160 (re-search-backward "(")
1161 (let ((beg (+ (point) 1)))
1162 (re-search-forward ")")
1163 (let ((end (- (point) 1)))
1164 (split-string (buffer-substring beg end))))))
1166 (defun notmuch-search-add-tag (tag)
1167 "Add a tag to the currently selected thread.
1169 The tag is added to messages in the currently selected thread
1170 which match the current search terms."
1172 (list (notmuch-select-tag-with-completion "Tag to add: ")))
1173 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id) " and " notmuch-search-query-string)
1174 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
1176 (defun notmuch-search-remove-tag (tag)
1177 "Remove a tag from the currently selected thread.
1179 The tag is removed from messages in the currently selected thread
1180 which match the current search terms."
1182 (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-search-find-thread-id))))
1183 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id) " and " notmuch-search-query-string)
1184 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
1186 (defun notmuch-search-archive-thread ()
1187 "Archive the currently selected thread (remove its \"inbox\" tag).
1189 This function advances the next thread when finished."
1191 (notmuch-search-remove-tag "inbox")
1194 (defun notmuch-search-process-sentinel (proc msg)
1195 "Add a message to let user know when \"notmuch search\" exits"
1196 (let ((buffer (process-buffer proc))
1197 (status (process-status proc))
1198 (exit-status (process-exit-status proc)))
1199 (if (memq status '(exit signal))
1200 (if (buffer-live-p buffer)
1201 (with-current-buffer buffer
1203 (let ((inhibit-read-only t))
1204 (goto-char (point-max))
1205 (if (eq status 'signal)
1206 (insert "Incomplete search results (search process was killed).\n"))
1207 (if (eq status 'exit)
1209 (insert "End of search results.")
1210 (if (not (= exit-status 0))
1211 (insert (format " (process returned %d)" exit-status)))
1212 (insert "\n"))))))))))
1214 (defun notmuch-search-process-filter (proc string)
1215 "Process and filter the output of \"notmuch search\""
1216 (let ((buffer (process-buffer proc)))
1217 (if (buffer-live-p buffer)
1218 (with-current-buffer buffer
1222 (inhibit-read-only t))
1224 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\(.*\\) \\(\\[[0-9/]*\\]\\) \\([^:]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
1225 (let* ((thread-id (match-string 1 string))
1226 (date (match-string 2 string))
1227 (count (match-string 3 string))
1228 (authors (match-string 4 string))
1229 (authors-length (length authors))
1230 (subject (match-string 5 string))
1231 (tags (match-string 6 string)))
1232 (if (> authors-length 40)
1233 (set 'authors (concat (substring authors 0 (- 40 3)) "...")))
1234 (goto-char (point-max))
1235 (let ((beg (point-marker)))
1236 (insert (format "%s %-7s %-40s %s (%s)\n" date count authors subject tags))
1237 (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id))
1238 (set 'line (match-end 0)))
1239 (set 'more nil))))))
1240 (delete-process proc))))
1242 (defun notmuch-search-operate-all (action)
1243 "Add/remove tags from all matching messages.
1245 Tis command adds or removes tags from all messages matching the
1246 current search terms. When called interactively, this command
1247 will prompt for tags to be added or removed. Tags prefixed with
1248 '+' will be added and tags prefixed with '-' will be removed.
1250 Each character of the tag name may consist of alphanumeric
1251 characters as well as `_.+-'.
1253 (interactive "sOperation (+add -drop): notmuch tag ")
1254 (let ((action-split (split-string action " +")))
1255 ;; Perform some validation
1256 (let ((words action-split))
1257 (when (null words) (error "No operation given"))
1259 (unless (string-match-p "^[-+][-+_.[:word:]]+$" (car words))
1260 (error "Action must be of the form `+thistag -that_tag'"))
1261 (setq words (cdr words))))
1262 (apply 'notmuch-call-notmuch-process "tag"
1263 (append action-split (list notmuch-search-query-string) nil))))
1266 (defun notmuch-search (query &optional oldest-first)
1267 "Run \"notmuch search\" with the given query string and display results."
1268 (interactive "sNotmuch search: ")
1269 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
1270 (switch-to-buffer buffer)
1271 (notmuch-search-mode)
1272 (set 'notmuch-search-query-string query)
1273 (set 'notmuch-search-oldest-first oldest-first)
1274 (let ((proc (get-buffer-process (current-buffer)))
1275 (inhibit-read-only t))
1277 (error "notmuch search process already running for query `%s'" query)
1280 (goto-char (point-min))
1282 (let ((proc (start-process-shell-command
1283 "notmuch-search" buffer notmuch-command "search"
1284 (if oldest-first "--sort=oldest-first" "--sort=newest-first")
1285 (shell-quote-argument query))))
1286 (set-process-sentinel proc 'notmuch-search-process-sentinel)
1287 (set-process-filter proc 'notmuch-search-process-filter))))
1288 (run-hooks 'notmuch-search-hook)))
1290 (defun notmuch-search-refresh-view ()
1291 "Refresh the current view.
1293 Kills the current buffer and runs a new search with the same
1294 query string as the current search. If the current thread is in
1295 the new search results, then point will be placed on the same
1296 thread. Otherwise, point will be moved to attempt to be in the
1297 same relative position within the new buffer."
1299 (let ((here (point))
1300 (oldest-first notmuch-search-oldest-first)
1301 (thread (notmuch-search-find-thread-id))
1302 (query notmuch-search-query-string))
1304 (notmuch-search query oldest-first)
1305 (goto-char (point-min))
1306 (if (re-search-forward (concat "^" thread) nil t)
1310 (defun notmuch-search-toggle-order ()
1311 "Toggle the current search order.
1313 By default, the \"inbox\" view created by `notmuch' is displayed
1314 in chronological order (oldest thread at the beginning of the
1315 buffer), while any global searches created by `notmuch-search'
1316 are displayed in reverse-chronological order (newest thread at
1317 the beginning of the buffer).
1319 This command toggles the sort order for the current search.
1321 Note that any filtered searches created by
1322 `notmuch-search-filter' retain the search order of the parent
1325 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1326 (notmuch-search-refresh-view))
1328 (defun notmuch-search-filter (query)
1329 "Filter the current search results based on an additional query string.
1331 Runs a new search matching only messages that match both the
1332 current search results AND the additional query string provided."
1333 (interactive "sFilter search: ")
1334 (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query) (concat "( " query " )") query)))
1335 (notmuch-search (concat notmuch-search-query-string " and " grouped-query) notmuch-search-oldest-first)))
1337 (defun notmuch-search-filter-by-tag (tag)
1338 "Filter the current search results based on a single tag.
1340 Runs a new search matching only messages that match both the
1341 current search results AND that are tagged with the given tag."
1343 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
1344 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1349 "Run notmuch to display all mail with tag of 'inbox'"
1351 (notmuch-search "tag:inbox" notmuch-search-oldest-first))
1353 (setq mail-user-agent 'message-user-agent)
1355 (defvar notmuch-folder-mode-map
1356 (let ((map (make-sparse-keymap)))
1357 (define-key map "?" 'notmuch-help)
1358 (define-key map "x" 'kill-this-buffer)
1359 (define-key map "q" 'kill-this-buffer)
1360 (define-key map ">" 'notmuch-folder-last)
1361 (define-key map "<" 'notmuch-folder-first)
1362 (define-key map "=" 'notmuch-folder)
1363 (define-key map "s" 'notmuch-search)
1364 (define-key map [mouse-1] 'notmuch-folder-show-search)
1365 (define-key map (kbd "RET") 'notmuch-folder-show-search)
1366 (define-key map "p" 'notmuch-folder-previous)
1367 (define-key map "n" 'notmuch-folder-next)
1369 "Keymap for \"notmuch folder\" buffers.")
1371 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
1373 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
1374 "List of searches for the notmuch folder view"
1375 :type '(alist :key-type (string) :value-type (string))
1378 (defun notmuch-folder-mode ()
1379 "Major mode for showing notmuch 'folders'.
1381 This buffer contains a list of message counts returned by a
1382 customizable set of searches of your email archives. Each line in
1383 the buffer shows the name of a saved search and the resulting
1386 Pressing RET on any line opens a search window containing the
1387 results for the saved search on that line.
1389 Here is an example of how the search list could be
1390 customized, (the following text would be placed in your ~/.emacs
1393 (setq notmuch-folders '((\"inbox\" . \"tag:inbox\")
1394 (\"unread\" . \"tag:inbox AND tag:unread\")
1395 (\"notmuch\" . \"tag:inbox AND to:notmuchmail.org\")))
1397 Of course, you can have any number of folders, each configured
1398 with any supported search terms (see \"notmuch help search-terms\").
1400 Currently available key bindings:
1402 \\{notmuch-folder-mode-map}"
1404 (kill-all-local-variables)
1405 (use-local-map 'notmuch-folder-mode-map)
1406 (setq truncate-lines t)
1408 (setq major-mode 'notmuch-folder-mode
1409 mode-name "notmuch-folder")
1410 (setq buffer-read-only t))
1412 (defun notmuch-folder-next ()
1413 "Select the next folder in the list."
1419 (defun notmuch-folder-previous ()
1420 "Select the previous folder in the list."
1424 (defun notmuch-folder-first ()
1425 "Select the first folder in the list."
1427 (goto-char (point-min)))
1429 (defun notmuch-folder-last ()
1430 "Select the last folder in the list."
1432 (goto-char (point-max))
1435 (defun notmuch-folder-add (folders)
1437 (let ((name (car (car folders)))
1438 (inhibit-read-only t)
1439 (search (cdr (car folders))))
1442 (call-process notmuch-command nil t nil "count" search)
1443 (notmuch-folder-add (cdr folders)))))
1445 (defun notmuch-folder-find-name ()
1448 (let ((beg (point)))
1450 (filter-buffer-substring beg (point)))))
1452 (defun notmuch-folder-show-search (&optional folder)
1453 "Show a search window for the search related to the specified folder."
1456 (setq folder (notmuch-folder-find-name)))
1457 (let ((search (assoc folder notmuch-folders)))
1459 (notmuch-search (cdr search) notmuch-search-oldest-first))))
1462 (defun notmuch-folder ()
1463 "Show the notmuch folder view and update the displayed counts."
1465 (let ((buffer (get-buffer-create "*notmuch-folders*")))
1466 (switch-to-buffer buffer)
1467 (let ((inhibit-read-only t)
1468 (n (line-number-at-pos)))
1470 (notmuch-folder-mode)
1471 (notmuch-folder-add notmuch-folders)
1472 (goto-char (point-min))