[PATCH] emacs: hello: allow arbitrary lisp for generating the count.

Mark Walters markwalters1009 at gmail.com
Tue May 6 11:48:52 PDT 2014


This allows a function to be given for the count-query of a saved
search. The function will be called with the query plist as an
argument to generate the count shown and should return either a string
or a number to be displayed as the count.

If this option is a function then its query will not be part of the
normal bacth query used so it may make notmuch-hello slower to display.
---
There was some discussion on irc today about notmuch hello being slow
(because it can make a lot of queries). This extends the new
saved-searches :count-query option to allow aribtrary lisp functions.

Thus a user could configure some searches to be (lambda (elem) "--")
so that these searches do not have a count executed and just display
"--". Alternatively (and at the risk of some slow down) they could
configure them to be my-notmuch-count where

(defun my-notmuch-count (elem)
  (concat
   (notmuch-hello-nice-number
    (string-to-number
     (car
      (process-lines notmuch-command "count" "--output=messages"
      		          (plist-get elem :query)))))
   "/"
   (notmuch-hello-nice-number
    (string-to-number
     (car
      (process-lines notmuch-command "count" "--output=threads"
      		          (plist-get elem :query)))))))

which would display messages/threads for that particular query.

Maybe the interface is too complicated but I can actually imagine
using this (possibly even both of the above for different of my saved
searches!)

Best wishes

Mark



 emacs/notmuch-hello.el |   41 ++++++++++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 15 deletions(-)

diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el
index 3de5238..877c84f 100644
--- a/emacs/notmuch-hello.el
+++ b/emacs/notmuch-hello.el
@@ -85,7 +85,7 @@ (define-widget 'notmuch-saved-search-plist 'list
 		(group :format "%v" :inline t (const :format "  Query: " :query) (string :format "%v")))
 	  (checklist :inline t
 		     :format "%v"
-		     (group :format "%v" :inline t (const :format "Count-Query: " :count-query) (string :format "%v"))
+		     (group :format "%v" :inline t (const :format "Count-Query: " :count-query) (sexp :format "%v"))
 		     (group :format "%v" :inline t (const :format "" :sort-order)
 			    (choice :tag " Sort Order"
 				    (const :tag "Default" nil)
@@ -101,9 +101,12 @@ (defcustom notmuch-saved-searches '((:name "inbox" :query "tag:inbox")
 
   :name            Name of the search (required).
   :query           Search to run (required).
-  :count-query     Optional extra query to generate the count
-                   shown. If not present then the :query property
-                   is used.
+  :count-query     Optional extra lisp to generate the count
+                   shown. If it is a string then it is a query
+                   string for generating the count. If it is a
+                   function then the function is called with the
+                   query plist as a parameter. If it is nil or not
+                   present then the :query property is used.
   :sort-order      Specify the sort order to be used for the search.
                    Possible values are 'oldest-first 'newest-first or
                    nil. Nil means use the default sort order.
@@ -493,13 +496,14 @@ (defun notmuch-hello-query-counts (query-list &rest options)
     (dolist (elem query-list nil)
       (let ((count-query (or (notmuch-saved-search-get elem :count-query)
 			     (notmuch-saved-search-get elem :query))))
-	(insert
-	 (replace-regexp-in-string
-	  "\n" " "
-	  (notmuch-hello-filtered-query count-query
-					(or (plist-get options :filter-count)
-					    (plist-get options :filter))))
-	  "\n")))
+	(unless (functionp count-query)
+	  (insert
+	   (replace-regexp-in-string
+	    "\n" " "
+	    (notmuch-hello-filtered-query count-query
+					  (or (plist-get options :filter-count)
+					      (plist-get options :filter))))
+	   "\n"))))
 
     (unless (= (call-process-region (point-min) (point-max) notmuch-command
 				    t t nil "count" "--batch") 0)
@@ -515,12 +519,17 @@ (defun notmuch-hello-query-counts (query-list &rest options)
      (mapcar
       (lambda (elem)
 	(let* ((elem-plist (notmuch-hello-saved-search-to-plist elem))
+	       (count-query (plist-get elem-plist :count-query))
 	       (search-query (plist-get elem-plist :query))
 	       (filtered-query (notmuch-hello-filtered-query
 				search-query (plist-get options :filter)))
-	       (message-count (prog1 (read (current-buffer))
-				(forward-line 1))))
-	  (when (and filtered-query (or (plist-get options :show-empty-searches) (> message-count 0)))
+	       (message-count (if (functionp count-query)
+				  (funcall count-query elem-plist)
+				(prog1 (read (current-buffer))
+				  (forward-line 1)))))
+	  (when (and filtered-query (or (plist-get options :show-empty-searches)
+					(not (integerp message-count))
+					(> message-count 0)))
 	    (setq elem-plist (plist-put elem-plist :query filtered-query))
 	    (plist-put elem-plist :count message-count))))
       query-list))))
@@ -559,7 +568,9 @@ (defun notmuch-hello-insert-buttons (searches)
 				     (otherwise notmuch-search-oldest-first)))
 		     (msg-count (plist-get elem :count)))
 		(widget-insert (format "%8s "
-				       (notmuch-hello-nice-number msg-count)))
+				       (if (stringp msg-count)
+					   msg-count
+					 (notmuch-hello-nice-number msg-count))))
 		(widget-create 'push-button
 			       :notify #'notmuch-hello-widget-search
 			       :notmuch-search-terms query
-- 
1.7.10.4



More information about the notmuch mailing list