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 (require 'notmuch-lib)
55 (require 'notmuch-show)
57 (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
59 (with-output-to-string
60 (with-current-buffer standard-output
61 (apply 'call-process notmuch-command nil t nil "search-tags" search-terms)))))
62 (completing-read prompt (split-string tag-list "\n+" t) nil nil nil)))
64 (defun notmuch-foreach-mime-part (function mm-handle)
65 (cond ((stringp (car mm-handle))
66 (dolist (part (cdr mm-handle))
67 (notmuch-foreach-mime-part function part)))
68 ((bufferp (car mm-handle))
69 (funcall function mm-handle))
70 (t (dolist (part mm-handle)
71 (notmuch-foreach-mime-part function part)))))
73 (defun notmuch-count-attachments (mm-handle)
75 (notmuch-foreach-mime-part
77 (let ((disposition (mm-handle-disposition p)))
78 (and (listp disposition)
79 (or (equal (car disposition) "attachment")
80 (and (equal (car disposition) "inline")
81 (assq 'filename disposition)))
86 (defun notmuch-save-attachments (mm-handle &optional queryp)
87 (notmuch-foreach-mime-part
89 (let ((disposition (mm-handle-disposition p)))
90 (and (listp disposition)
91 (or (equal (car disposition) "attachment")
92 (and (equal (car disposition) "inline")
93 (assq 'filename disposition)))
96 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
100 (defun notmuch-reply (query-string)
101 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
102 (call-process notmuch-command nil t nil "reply" query-string)
103 (message-insert-signature)
104 (goto-char (point-min))
105 (if (re-search-forward "^$" nil t)
107 (insert "--text follows this line--")
111 (defun notmuch-toggle-invisible-action (cite-button)
112 (let ((invis-spec (button-get cite-button 'invisibility-spec)))
113 (if (invisible-p invis-spec)
114 (remove-from-invisibility-spec invis-spec)
115 (add-to-invisibility-spec invis-spec)
117 (force-window-update)
120 (define-button-type 'notmuch-button-citation-toggle-type 'help-echo "mouse-1, RET: Show citation"
121 :supertype 'notmuch-button-invisibility-toggle-type)
122 (define-button-type 'notmuch-button-signature-toggle-type 'help-echo "mouse-1, RET: Show signature"
123 :supertype 'notmuch-button-invisibility-toggle-type)
124 (define-button-type 'notmuch-button-body-toggle-type
125 'help-echo "mouse-1, RET: Show message"
126 'face 'notmuch-message-summary-face
127 :supertype 'notmuch-button-invisibility-toggle-type)
129 (defun notmuch-fontify-headers ()
130 (while (looking-at "[[:space:]]")
132 (if (looking-at "[Tt]o:")
134 (overlay-put (make-overlay (point) (re-search-forward ":"))
135 'face 'message-header-name)
136 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
137 'face 'message-header-to))
138 (if (looking-at "[B]?[Cc][Cc]:")
140 (overlay-put (make-overlay (point) (re-search-forward ":"))
141 'face 'message-header-name)
142 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
143 'face 'message-header-cc))
144 (if (looking-at "[Ss]ubject:")
146 (overlay-put (make-overlay (point) (re-search-forward ":"))
147 'face 'message-header-name)
148 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
149 'face 'message-header-subject))
150 (if (looking-at "[Ff]rom:")
152 (overlay-put (make-overlay (point) (re-search-forward ":"))
153 'face 'message-header-name)
154 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
155 'face 'message-header-other)))))))
157 (defun notmuch-documentation-first-line (symbol)
158 "Return the first line of the documentation string for SYMBOL."
159 (let ((doc (documentation symbol)))
162 (insert (documentation symbol t))
163 (goto-char (point-min))
166 (buffer-substring beg (point))))
169 (defun notmuch-prefix-key-description (key)
170 "Given a prefix key code, return a human-readable string representation.
172 This is basically just `format-kbd-macro' but we also convert ESC to M-."
173 (let ((desc (format-kbd-macro (vector key))))
174 (if (string= desc "ESC")
178 ; I would think that emacs would have code handy for walking a keymap
179 ; and generating strings for each key, and I would prefer to just call
180 ; that. But I couldn't find any (could be all implemented in C I
181 ; suppose), so I wrote my own here.
182 (defun notmuch-substitute-one-command-key-with-prefix (prefix binding)
183 "For a key binding, return a string showing a human-readable
184 representation of the prefixed key as well as the first line of
185 documentation from the bound function.
187 For a mouse binding, return nil."
188 (let ((key (car binding))
189 (action (cdr binding)))
190 (if (mouse-event-p key)
193 (let ((substitute (apply-partially 'notmuch-substitute-one-command-key-with-prefix (notmuch-prefix-key-description key)))
195 (map-keymap (lambda (a b)
196 (push (cons a b) as-list))
198 (mapconcat substitute as-list "\n"))
199 (concat prefix (format-kbd-macro (vector key))
201 (notmuch-documentation-first-line action))))))
203 (defalias 'notmuch-substitute-one-command-key
204 (apply-partially 'notmuch-substitute-one-command-key-with-prefix nil))
206 (defun notmuch-substitute-command-keys (doc)
207 "Like `substitute-command-keys' but with documentation, not function names."
209 (while (string-match "\\\\{\\([^}[:space:]]*\\)}" doc beg)
210 (let ((map (substring doc (match-beginning 1) (match-end 1))))
211 (setq doc (replace-match (mapconcat 'notmuch-substitute-one-command-key
212 (cdr (symbol-value (intern map))) "\n") 1 1 doc)))
213 (setq beg (match-end 0)))
216 (defun notmuch-help ()
217 "Display help for the current notmuch mode."
219 (let* ((mode major-mode)
220 (doc (substitute-command-keys (notmuch-substitute-command-keys (documentation mode t)))))
221 (with-current-buffer (generate-new-buffer "*notmuch-help*")
223 (goto-char (point-min))
224 (set-buffer-modified-p nil)
225 (view-buffer (current-buffer) 'kill-buffer-if-not-modified))))
227 (defgroup notmuch nil
228 "Notmuch mail reader for Emacs."
231 (defcustom notmuch-search-hook nil
232 "List of functions to call when notmuch displays the search results."
234 :options '(hl-line-mode)
237 (defvar notmuch-search-authors-width 20
238 "Number of columns to use to display authors in a notmuch-search buffer.")
240 (defvar notmuch-search-mode-map
241 (let ((map (make-sparse-keymap)))
242 (define-key map "?" 'notmuch-help)
243 (define-key map "q" 'kill-this-buffer)
244 (define-key map "x" 'kill-this-buffer)
245 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
246 (define-key map "b" 'notmuch-search-scroll-down)
247 (define-key map " " 'notmuch-search-scroll-up)
248 (define-key map "<" 'notmuch-search-first-thread)
249 (define-key map ">" 'notmuch-search-last-thread)
250 (define-key map "p" 'notmuch-search-previous-thread)
251 (define-key map "n" 'notmuch-search-next-thread)
252 (define-key map "r" 'notmuch-search-reply-to-thread)
253 (define-key map "m" 'message-mail)
254 (define-key map "s" 'notmuch-search)
255 (define-key map "o" 'notmuch-search-toggle-order)
256 (define-key map "=" 'notmuch-search-refresh-view)
257 (define-key map "t" 'notmuch-search-filter-by-tag)
258 (define-key map "f" 'notmuch-search-filter)
259 (define-key map [mouse-1] 'notmuch-search-show-thread)
260 (define-key map "*" 'notmuch-search-operate-all)
261 (define-key map "a" 'notmuch-search-archive-thread)
262 (define-key map "-" 'notmuch-search-remove-tag)
263 (define-key map "+" 'notmuch-search-add-tag)
264 (define-key map (kbd "RET") 'notmuch-search-show-thread)
266 "Keymap for \"notmuch search\" buffers.")
267 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
269 (defvar notmuch-search-query-string)
270 (defvar notmuch-search-target-thread)
271 (defvar notmuch-search-target-line)
272 (defvar notmuch-search-oldest-first t
273 "Show the oldest mail first in the search-mode")
275 (defvar notmuch-search-disjunctive-regexp "\\<[oO][rR]\\>")
277 (defun notmuch-search-scroll-up ()
278 "Move forward through search results by one window's worth."
282 ((end-of-buffer) (notmuch-search-last-thread))))
284 (defun notmuch-search-scroll-down ()
285 "Move backward through the search results by one window's worth."
287 ; I don't know why scroll-down doesn't signal beginning-of-buffer
288 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
290 ; So instead of trapping a signal we instead check whether the
291 ; window begins on the first line of the buffer and if so, move
292 ; directly to that position. (We have to count lines since the
293 ; window-start position is not the same as point-min due to the
294 ; invisible thread-ID characters on the first line.
295 (if (equal (count-lines (point-min) (window-start)) 0)
296 (goto-char (point-min))
299 (defun notmuch-search-next-thread ()
300 "Select the next thread in the search results."
304 (defun notmuch-search-previous-thread ()
305 "Select the previous thread in the search results."
309 (defun notmuch-search-last-thread ()
310 "Select the last thread in the search results."
312 (goto-char (point-max))
315 (defun notmuch-search-first-thread ()
316 "Select the first thread in the search results."
318 (goto-char (point-min)))
320 (defface notmuch-message-summary-face
321 '((((class color) (background light)) (:background "#f0f0f0"))
322 (((class color) (background dark)) (:background "#303030")))
323 "Face for the single-line message summary in notmuch-show-mode."
326 (defface notmuch-tag-face
329 (:foreground "OliveDrab1"))
332 (:foreground "navy blue" :bold t))
335 "Notmuch search mode face used to highligh tags."
338 (defvar notmuch-tag-face-alist nil
339 "List containing the tag list that need to be highlighed")
341 (defvar notmuch-search-font-lock-keywords nil)
344 (defun notmuch-search-mode ()
345 "Major mode displaying results of a notmuch search.
347 This buffer contains the results of a \"notmuch search\" of your
348 email archives. Each line in the buffer represents a single
349 thread giving a summary of the thread (a relative date, the
350 number of matched messages and total messages in the thread,
351 participants in the thread, a representative subject line, and
354 Pressing \\[notmuch-search-show-thread] on any line displays that thread. The '\\[notmuch-search-add-tag]' and '\\[notmuch-search-remove-tag]'
355 keys can be used to add or remove tags from a thread. The '\\[notmuch-search-archive-thread]' key
356 is a convenience for archiving a thread (removing the \"inbox\"
357 tag). The '\\[notmuch-search-operate-all]' key can be used to add or remove a tag from all
358 threads in the current buffer.
360 Other useful commands are '\\[notmuch-search-filter]' for filtering the current search
361 based on an additional query string, '\\[notmuch-search-filter-by-tag]' for filtering to include
362 only messages with a given tag, and '\\[notmuch-search]' to execute a new, global
365 Complete list of currently available key bindings:
367 \\{notmuch-search-mode-map}"
369 (kill-all-local-variables)
370 (make-local-variable 'notmuch-search-query-string)
371 (make-local-variable 'notmuch-search-oldest-first)
372 (make-local-variable 'notmuch-search-target-thread)
373 (make-local-variable 'notmuch-search-target-line)
374 (set (make-local-variable 'scroll-preserve-screen-position) t)
375 (add-to-invisibility-spec 'notmuch-search)
376 (use-local-map notmuch-search-mode-map)
377 (setq truncate-lines t)
378 (setq major-mode 'notmuch-search-mode
379 mode-name "notmuch-search")
380 (setq buffer-read-only t)
381 (if (not notmuch-tag-face-alist)
382 (add-to-list 'notmuch-search-font-lock-keywords (list
383 "(\\([^()]*\\))$" '(1 'notmuch-tag-face)))
384 (let ((notmuch-search-tags (mapcar 'car notmuch-tag-face-alist)))
385 (loop for notmuch-search-tag in notmuch-search-tags
386 do (add-to-list 'notmuch-search-font-lock-keywords (list
387 (concat "([^)]*\\(" notmuch-search-tag "\\)[^)]*)$")
388 `(1 ,(cdr (assoc notmuch-search-tag notmuch-tag-face-alist))))))))
389 (set (make-local-variable 'font-lock-defaults)
390 '(notmuch-search-font-lock-keywords t)))
392 (defun notmuch-search-find-thread-id ()
393 "Return the thread for the current thread"
394 (get-text-property (point) 'notmuch-search-thread-id))
396 (defun notmuch-search-find-authors ()
397 "Return the authors for the current thread"
398 (get-text-property (point) 'notmuch-search-authors))
400 (defun notmuch-search-find-subject ()
401 "Return the subject for the current thread"
402 (get-text-property (point) 'notmuch-search-subject))
404 (defun notmuch-search-show-thread ()
405 "Display the currently selected thread."
407 (let ((thread-id (notmuch-search-find-thread-id)))
408 (if (> (length thread-id) 0)
409 (notmuch-show thread-id (current-buffer) notmuch-search-query-string)
410 (error "End of search results"))))
412 (defun notmuch-search-reply-to-thread ()
413 "Begin composing a reply to the entire current thread in a new buffer."
415 (let ((message-id (notmuch-search-find-thread-id)))
416 (notmuch-reply message-id)))
418 (defun notmuch-call-notmuch-process (&rest args)
419 "Synchronously invoke \"notmuch\" with the given list of arguments.
421 Output from the process will be presented to the user as an error
422 and will also appear in a buffer named \"*Notmuch errors*\"."
423 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
424 (with-current-buffer error-buffer
426 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
429 (with-current-buffer error-buffer
430 (let ((beg (point-min))
431 (end (- (point-max) 1)))
432 (error (buffer-substring beg end))
435 (defun notmuch-search-set-tags (tags)
438 (re-search-backward "(")
441 (inhibit-read-only t))
442 (re-search-forward ")")
445 (delete-region beg end)
446 (insert (mapconcat 'identity tags " "))))))
448 (defun notmuch-search-get-tags ()
451 (re-search-backward "(")
452 (let ((beg (+ (point) 1)))
453 (re-search-forward ")")
454 (let ((end (- (point) 1)))
455 (split-string (buffer-substring beg end))))))
457 (defun notmuch-search-add-tag (tag)
458 "Add a tag to the currently selected thread.
460 The tag is added to messages in the currently selected thread
461 which match the current search terms."
463 (list (notmuch-select-tag-with-completion "Tag to add: ")))
464 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
465 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
467 (defun notmuch-search-remove-tag (tag)
468 "Remove a tag from the currently selected thread.
470 The tag is removed from all messages in the currently selected thread."
472 (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-search-find-thread-id))))
473 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
474 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
476 (defun notmuch-search-archive-thread ()
477 "Archive the currently selected thread (remove its \"inbox\" tag).
479 This function advances the next thread when finished."
481 (notmuch-search-remove-tag "inbox")
484 (defun notmuch-search-process-sentinel (proc msg)
485 "Add a message to let user know when \"notmuch search\" exits"
486 (let ((buffer (process-buffer proc))
487 (status (process-status proc))
488 (exit-status (process-exit-status proc))
489 (never-found-target-thread nil))
490 (if (memq status '(exit signal))
491 (if (buffer-live-p buffer)
492 (with-current-buffer buffer
494 (let ((inhibit-read-only t)
496 (goto-char (point-max))
497 (if (eq status 'signal)
498 (insert "Incomplete search results (search process was killed).\n"))
499 (if (eq status 'exit)
501 (insert "End of search results.")
502 (if (not (= exit-status 0))
503 (insert (format " (process returned %d)" exit-status)))
506 (not (string= notmuch-search-target-thread "found")))
507 (set 'never-found-target-thread t))))))
508 (if (and never-found-target-thread
509 notmuch-search-target-line)
510 (goto-line notmuch-search-target-line)))))))
512 (defun notmuch-search-process-filter (proc string)
513 "Process and filter the output of \"notmuch search\""
514 (let ((buffer (process-buffer proc))
516 (if (buffer-live-p buffer)
517 (with-current-buffer buffer
521 (inhibit-read-only t))
523 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\(.*\\) \\(\\[[0-9/]*\\]\\) \\([^;]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
524 (let* ((thread-id (match-string 1 string))
525 (date (match-string 2 string))
526 (count (match-string 3 string))
527 (authors (match-string 4 string))
528 (authors-length (length authors))
529 (subject (match-string 5 string))
530 (tags (match-string 6 string)))
531 (if (> authors-length notmuch-search-authors-width)
532 (set 'authors (concat (substring authors 0 (- notmuch-search-authors-width 3)) "...")))
533 (goto-char (point-max))
534 (let ((beg (point-marker))
535 (format-string (format "%%s %%-7s %%-%ds %%s (%%s)\n" notmuch-search-authors-width)))
536 (insert (format format-string date count authors subject tags))
537 (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id)
538 (put-text-property beg (point-marker) 'notmuch-search-authors authors)
539 (put-text-property beg (point-marker) 'notmuch-search-subject subject)
540 (if (string= thread-id notmuch-search-target-thread)
542 (set 'found-target beg)
543 (set 'notmuch-search-target-thread "found"))))
544 (set 'line (match-end 0)))
547 (goto-char found-target)))
548 (delete-process proc))))
550 (defun notmuch-search-operate-all (action)
551 "Add/remove tags from all matching messages.
553 Tis command adds or removes tags from all messages matching the
554 current search terms. When called interactively, this command
555 will prompt for tags to be added or removed. Tags prefixed with
556 '+' will be added and tags prefixed with '-' will be removed.
558 Each character of the tag name may consist of alphanumeric
559 characters as well as `_.+-'.
561 (interactive "sOperation (+add -drop): notmuch tag ")
562 (let ((action-split (split-string action " +")))
563 ;; Perform some validation
564 (let ((words action-split))
565 (when (null words) (error "No operation given"))
567 (unless (string-match-p "^[-+][-+_.[:word:]]+$" (car words))
568 (error "Action must be of the form `+thistag -that_tag'"))
569 (setq words (cdr words))))
570 (apply 'notmuch-call-notmuch-process "tag"
571 (append action-split (list notmuch-search-query-string) nil))))
574 (defun notmuch-search (query &optional oldest-first target-thread target-line)
575 "Run \"notmuch search\" with the given query string and display results.
577 The optional parameters are used as follows:
579 oldest-first: A Boolean controlling the sort order of returned threads
580 target-thread: A thread ID (with the thread: prefix) that will be made
581 current if it appears in the search results.
582 target-line: The line number to move to if the target thread does not
583 appear in the search results."
584 (interactive "sNotmuch search: ")
585 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
586 (switch-to-buffer buffer)
587 (notmuch-search-mode)
588 (set 'notmuch-search-query-string query)
589 (set 'notmuch-search-oldest-first oldest-first)
590 (set 'notmuch-search-target-thread target-thread)
591 (set 'notmuch-search-target-line target-line)
592 (let ((proc (get-buffer-process (current-buffer)))
593 (inhibit-read-only t))
595 (error "notmuch search process already running for query `%s'" query)
598 (goto-char (point-min))
600 (let ((proc (start-process-shell-command
601 "notmuch-search" buffer notmuch-command "search"
602 (if oldest-first "--sort=oldest-first" "--sort=newest-first")
603 (shell-quote-argument query))))
604 (set-process-sentinel proc 'notmuch-search-process-sentinel)
605 (set-process-filter proc 'notmuch-search-process-filter))))
606 (run-hooks 'notmuch-search-hook)))
608 (defun notmuch-search-refresh-view ()
609 "Refresh the current view.
611 Kills the current buffer and runs a new search with the same
612 query string as the current search. If the current thread is in
613 the new search results, then point will be placed on the same
614 thread. Otherwise, point will be moved to attempt to be in the
615 same relative position within the new buffer."
617 (let ((target-line (line-number-at-pos))
618 (oldest-first notmuch-search-oldest-first)
619 (target-thread (notmuch-search-find-thread-id))
620 (query notmuch-search-query-string))
622 (notmuch-search query oldest-first target-thread target-line)
623 (goto-char (point-min))
626 (defun notmuch-search-toggle-order ()
627 "Toggle the current search order.
629 By default, the \"inbox\" view created by `notmuch' is displayed
630 in chronological order (oldest thread at the beginning of the
631 buffer), while any global searches created by `notmuch-search'
632 are displayed in reverse-chronological order (newest thread at
633 the beginning of the buffer).
635 This command toggles the sort order for the current search.
637 Note that any filtered searches created by
638 `notmuch-search-filter' retain the search order of the parent
641 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
642 (notmuch-search-refresh-view))
644 (defun notmuch-search-filter (query)
645 "Filter the current search results based on an additional query string.
647 Runs a new search matching only messages that match both the
648 current search results AND the additional query string provided."
649 (interactive "sFilter search: ")
650 (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query) (concat "( " query " )") query)))
651 (notmuch-search (concat notmuch-search-query-string " and " grouped-query) notmuch-search-oldest-first)))
653 (defun notmuch-search-filter-by-tag (tag)
654 "Filter the current search results based on a single tag.
656 Runs a new search matching only messages that match both the
657 current search results AND that are tagged with the given tag."
659 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
660 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
664 "Run notmuch to display all mail with tag of 'inbox'"
666 (notmuch-search "tag:inbox" notmuch-search-oldest-first))
668 (setq mail-user-agent 'message-user-agent)
670 (defvar notmuch-folder-mode-map
671 (let ((map (make-sparse-keymap)))
672 (define-key map "?" 'notmuch-help)
673 (define-key map "x" 'kill-this-buffer)
674 (define-key map "q" 'kill-this-buffer)
675 (define-key map "m" 'message-mail)
676 (define-key map "e" 'notmuch-folder-show-empty-toggle)
677 (define-key map ">" 'notmuch-folder-last)
678 (define-key map "<" 'notmuch-folder-first)
679 (define-key map "=" 'notmuch-folder)
680 (define-key map "s" 'notmuch-search)
681 (define-key map [mouse-1] 'notmuch-folder-show-search)
682 (define-key map (kbd "RET") 'notmuch-folder-show-search)
683 (define-key map " " 'notmuch-folder-show-search)
684 (define-key map "p" 'notmuch-folder-previous)
685 (define-key map "n" 'notmuch-folder-next)
687 "Keymap for \"notmuch folder\" buffers.")
689 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
691 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
692 "List of searches for the notmuch folder view"
693 :type '(alist :key-type (string) :value-type (string))
696 (defun notmuch-folder-mode ()
697 "Major mode for showing notmuch 'folders'.
699 This buffer contains a list of message counts returned by a
700 customizable set of searches of your email archives. Each line in
701 the buffer shows the name of a saved search and the resulting
704 Pressing RET on any line opens a search window containing the
705 results for the saved search on that line.
707 Here is an example of how the search list could be
708 customized, (the following text would be placed in your ~/.emacs
711 (setq notmuch-folders '((\"inbox\" . \"tag:inbox\")
712 (\"unread\" . \"tag:inbox AND tag:unread\")
713 (\"notmuch\" . \"tag:inbox AND to:notmuchmail.org\")))
715 Of course, you can have any number of folders, each configured
716 with any supported search terms (see \"notmuch help search-terms\").
718 Currently available key bindings:
720 \\{notmuch-folder-mode-map}"
722 (kill-all-local-variables)
723 (use-local-map 'notmuch-folder-mode-map)
724 (setq truncate-lines t)
726 (setq major-mode 'notmuch-folder-mode
727 mode-name "notmuch-folder")
728 (setq buffer-read-only t))
730 (defun notmuch-folder-next ()
731 "Select the next folder in the list."
737 (defun notmuch-folder-previous ()
738 "Select the previous folder in the list."
742 (defun notmuch-folder-first ()
743 "Select the first folder in the list."
745 (goto-char (point-min)))
747 (defun notmuch-folder-last ()
748 "Select the last folder in the list."
750 (goto-char (point-max))
753 (defun notmuch-folder-count (search)
754 (car (process-lines notmuch-command "count" search)))
756 (defvar notmuch-folder-show-empty t
757 "Whether `notmuch-folder-mode' should display empty folders.")
759 (defun notmuch-folder-show-empty-toggle ()
760 "Toggle the listing of empty folders"
762 (setq notmuch-folder-show-empty (not notmuch-folder-show-empty))
765 (defun notmuch-folder-add (folders)
767 (let* ((name (car (car folders)))
768 (inhibit-read-only t)
769 (search (cdr (car folders)))
770 (count (notmuch-folder-count search)))
771 (if (or notmuch-folder-show-empty
772 (not (equal count "0")))
780 (notmuch-folder-add (cdr folders)))))
782 (defun notmuch-folder-find-name ()
786 (re-search-forward "\\([ \t]*[^ \t]+\\)")
787 (filter-buffer-substring (match-beginning 1) (match-end 1)))))
789 (defun notmuch-folder-show-search (&optional folder)
790 "Show a search window for the search related to the specified folder."
793 (setq folder (notmuch-folder-find-name)))
794 (let ((search (assoc folder notmuch-folders)))
796 (notmuch-search (cdr search) notmuch-search-oldest-first))))
799 (defun notmuch-folder ()
800 "Show the notmuch folder view and update the displayed counts."
802 (let ((buffer (get-buffer-create "*notmuch-folders*")))
803 (switch-to-buffer buffer)
804 (let ((inhibit-read-only t)
805 (n (line-number-at-pos)))
807 (notmuch-folder-mode)
808 (notmuch-folder-add notmuch-folders)
809 (goto-char (point-min))