[notmuch] [PATCH 3/3] Add notmuch-index mode to display message counts

Keith Packard keithp at keithp.com
Fri Nov 20 23:15:08 PST 2009


Index mode takes a (user-configurable) list of search patterns and
produces a list of those patterns and the count of messages that they
match. When an entry in this list is selected, a search window with
the defined search is opened.  The set of indexes is defined as a
list, each element contains the name of the index and the query string
to count.

This provides a view similar to a folder list in a more traditional
mail client.

Signed-off-by: Keith Packard <keithp at keithp.com>
---
 notmuch.el |   81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 81 insertions(+), 0 deletions(-)

diff --git a/notmuch.el b/notmuch.el
index 4f369de..454320e 100644
--- a/notmuch.el
+++ b/notmuch.el
@@ -1046,4 +1046,85 @@ current search results AND that are tagged with the given tag."
 
 (setq mail-user-agent 'message-user-agent)
 
+(defvar notmuch-index-mode-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "n" 'next-line)
+    (define-key map "p" 'previous-line)
+    (define-key map "x" 'kill-this-buffer)
+    (define-key map "q" 'kill-this-buffer)
+    (define-key map "s" 'notmuch-search)
+    (define-key map (kbd "RET") 'notmuch-index-show-search)
+    (define-key map "<" 'beginning-of-buffer)
+    (define-key map "=" 'notmuch-index)
+    (define-key map "?" 'describe-mode)
+    (define-key map [mouse-1] 'notmuch-index-show-search)
+    map)
+  "Keymap for \"notmuch index\" buffers.")
+
+(fset 'notmuch-index-mode-map notmuch-index-mode-map)
+
+(defcustom notmuch-indexes (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
+  "List of searches for the notmuch index view"
+  :type '(alist :key-type (string) :value-type (string))
+  :group 'notmuch)
+  
+(defun notmuch-index-mode ()
+  "Major mode for showing index of notmuch tags.
+
+This buffer contains a list of messages counts returned by a
+customizable set of searches of your email archives. Each line
+in the buffer shows the search terms and the resulting message count.
+
+Pressing RET on any line opens a search window containing the search
+results for the search terms in that line.
+
+\\{notmuch-index-mode-map}"
+  (interactive)
+  (kill-all-local-variables)
+  (use-local-map 'notmuch-index-mode-map)
+  (setq truncate-lines t)
+  (hl-line-mode 1)
+  (setq major-mode 'notmuch-index-mode
+	mode-name "notmuch-index")
+  (setq buffer-read-only t))
+
+(defun notmuch-index-add (indexes)
+  (if indexes
+      (let ((name (car (car indexes)))
+	    (inhibit-read-only t)
+	    (search (cdr (car indexes))))
+	(insert name)
+	(indent-to 16 1)
+	(call-process notmuch-command nil t nil "count" search)
+	(notmuch-index-add (cdr indexes)))))
+
+(defun notmuch-index-find-name ()
+  (save-excursion
+    (beginning-of-line)
+    (let ((beg (point)))
+      (forward-word)
+      (filter-buffer-substring beg (point)))))
+
+(defun notmuch-index-show-search (&optional index)
+  "Show a search window for the search related to the specified index."
+  (interactive)
+  (if (null index)
+      (setq index (notmuch-index-find-name)))
+  (let ((search (assoc index notmuch-indexes)))
+    (if search
+	(notmuch-search (cdr search) t))))
+
+(defun notmuch-index ()
+  "Show the message index and update the displayed counts."
+  (interactive)
+  (let ((buffer (get-buffer-create "*notmuch-index*")))
+    (switch-to-buffer buffer)
+    (let ((inhibit-read-only t)
+	  (n (line-number-at-pos)))
+      (erase-buffer)
+      (notmuch-index-mode)
+      (notmuch-index-add notmuch-indexes)
+      (goto-char (point-min))
+      (goto-line n))))
+
 (provide 'notmuch)
-- 
1.6.5.2



More information about the notmuch mailing list