[RFC PATCH 3/3] emacs: support limiting the number of messages shown in search results

Jani Nikula jani at nikula.org
Fri Oct 28 13:59:31 PDT 2011


Add support for limiting the maximum number of messages initially displayed
in search results. When enabled, the search results will contain push
buttons to double the number of messages displayed or to show unlimited
messages.

The approach is inspired by vc-print-log in Emacs vc.el.

Signed-off-by: Jani Nikula <jani at nikula.org>
---
 emacs/notmuch-hello.el |   19 ++++++++++++++++---
 emacs/notmuch.el       |   43 ++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 54 insertions(+), 8 deletions(-)

diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el
index 65fde75..16eecb6 100644
--- a/emacs/notmuch-hello.el
+++ b/emacs/notmuch-hello.el
@@ -26,7 +26,7 @@
 (require 'notmuch-lib)
 (require 'notmuch-mua)
 
-(declare-function notmuch-search "notmuch" (query &optional oldest-first target-thread target-line continuation))
+(declare-function notmuch-search "notmuch" (query &optional oldest-first target-thread target-line continuation maxitems))
 (declare-function notmuch-poll "notmuch" ())
 
 (defvar notmuch-hello-search-bar-marker nil
@@ -37,6 +37,18 @@
   :type 'integer
   :group 'notmuch)
 
+(defcustom notmuch-search-maxitems 100
+  "The maximum number of messages to show in search results.
+
+This variables controls the maximum number of messages to
+initially show in search results. Set to 0 to unlimited. If
+non-zero, the search results will contain push buttons to double
+the number (can be repeated) or show unlimited number of
+messages. Note that this controls the number of messages; in a
+threaded view this is not the number of lines to show."
+  :type 'integer
+  :group 'notmuch)
+
 (defcustom notmuch-show-empty-saved-searches nil
   "Should saved searches with no messages be listed?"
   :type 'boolean
@@ -151,7 +163,7 @@ Typically \",\" in the US and UK and \".\" in Europe."
 (defun notmuch-hello-search (search)
   (let ((search (notmuch-hello-trim search)))
     (notmuch-hello-remember-search search)
-    (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
+    (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation notmuch-search-maxitems)))
 
 (defun notmuch-hello-add-saved-search (widget)
   (interactive)
@@ -201,7 +213,8 @@ diagonal."
   (notmuch-search (widget-get widget
 			      :notmuch-search-terms)
 		  notmuch-search-oldest-first
-		  nil nil #'notmuch-hello-search-continuation))
+		  nil nil #'notmuch-hello-search-continuation
+		  notmuch-search-maxitems))
 
 (defun notmuch-saved-search-count (search)
   (car (process-lines notmuch-command "count" search)))
diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index f11ec24..bb805da 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -193,6 +193,7 @@ For a mouse binding, return nil."
 
 (defvar notmuch-search-mode-map
   (let ((map (make-sparse-keymap)))
+    (set-keymap-parent map widget-keymap)
     (define-key map "?" 'notmuch-help)
     (define-key map "q" 'notmuch-search-quit)
     (define-key map "x" 'notmuch-search-quit)
@@ -217,7 +218,13 @@ For a mouse binding, return nil."
     (define-key map "a" 'notmuch-search-archive-thread)
     (define-key map "-" 'notmuch-search-remove-tag)
     (define-key map "+" 'notmuch-search-add-tag)
-    (define-key map (kbd "RET") 'notmuch-search-show-thread)
+    ; Some hackery to allow RET both on buttons and messages. There's probably a
+    ; better way to do this...
+    (define-key map (kbd "RET") '(lambda (pos)
+				   (interactive "@d")
+				   (if (get-char-property pos 'button)
+				       (widget-button-press pos)
+				     (notmuch-search-show-thread))))
     (define-key map (kbd "M-RET") 'notmuch-search-show-thread-crypto-switch)
     map)
   "Keymap for \"notmuch search\" buffers.")
@@ -239,6 +246,7 @@ For a mouse binding, return nil."
 (defvar notmuch-search-target-thread)
 (defvar notmuch-search-target-line)
 (defvar notmuch-search-continuation)
+(defvar notmuch-search-maxitems)
 
 (defvar notmuch-search-disjunctive-regexp      "\\<[oO][rR]\\>")
 
@@ -373,6 +381,7 @@ Complete list of currently available key bindings:
   (make-local-variable 'notmuch-search-oldest-first)
   (make-local-variable 'notmuch-search-target-thread)
   (make-local-variable 'notmuch-search-target-line)
+  (make-local-variable 'notmuch-search-maxitems)
   (set (make-local-variable 'notmuch-search-continuation) nil)
   (set (make-local-variable 'scroll-preserve-screen-position) t)
   (add-to-invisibility-spec 'notmuch-search)
@@ -633,6 +642,8 @@ This function advances the next thread when finished."
 			(insert "End of search results.")
 			(if (not (= exit-status 0))
 			    (insert (format " (process returned %d)" exit-status)))
+			(if notmuch-search-maxitems
+			    (notmuch-search-setup-buttons))
 			(insert "\n")
 			(if (and atbob
 				 (not (string= notmuch-search-target-thread "found")))
@@ -883,7 +894,7 @@ characters as well as `_.+-'.
 	  )))
 
 ;;;###autoload
-(defun notmuch-search (query &optional oldest-first target-thread target-line continuation)
+(defun notmuch-search (query &optional oldest-first target-thread target-line continuation maxitems)
   "Run \"notmuch search\" with the given query string and display results.
 
 The optional parameters are used as follows:
@@ -899,6 +910,7 @@ The optional parameters are used as follows:
     (notmuch-search-mode)
     (set 'notmuch-search-query-string query)
     (set 'notmuch-search-oldest-first oldest-first)
+    (set 'notmuch-search-maxitems maxitems)
     (set 'notmuch-search-target-thread target-thread)
     (set 'notmuch-search-target-line target-line)
     (set 'notmuch-search-continuation continuation)
@@ -916,6 +928,8 @@ The optional parameters are used as follows:
 		     (if oldest-first
 			 "--sort=oldest-first"
 		       "--sort=newest-first")
+		     (if maxitems
+			 (format "--maxitems=%d" maxitems))
 		     query)))
 	  (set-process-sentinel proc 'notmuch-search-process-sentinel)
 	  (set-process-filter proc 'notmuch-search-process-filter))))
@@ -932,13 +946,32 @@ same relative position within the new buffer."
   (interactive)
   (let ((target-line (line-number-at-pos))
 	(oldest-first notmuch-search-oldest-first)
+	(maxitems notmuch-search-maxitems)
 	(target-thread (notmuch-search-find-thread-id))
 	(query notmuch-search-query-string)
 	(continuation notmuch-search-continuation))
     (notmuch-kill-this-buffer)
-    (notmuch-search query oldest-first target-thread target-line continuation)
+    (notmuch-search query oldest-first target-thread target-line continuation maxitems)
     (goto-char (point-min))))
 
+(defun notmuch-search-setup-buttons ()
+  (widget-insert "    ")
+  (widget-create 'push-button
+		 :notify (lambda (&rest ignore)
+			   (set 'notmuch-search-maxitems
+				(* 2 notmuch-search-maxitems))
+			   (notmuch-search-refresh-view))
+		 :help-echo "Double the number of messages shown"
+		 "Show 2X messages")
+  (widget-insert "    ")
+  (widget-create 'push-button
+		 :notify (lambda (&rest ignore)
+			   (set 'notmuch-search-maxitems 0)
+			   (notmuch-search-refresh-view))
+		 :help-echo "Show all search results"
+		 "Show unlimited messages")
+  (widget-setup))
+
 (defcustom notmuch-poll-script ""
   "An external script to incorporate new mail into the notmuch database.
 
@@ -997,7 +1030,7 @@ current search results AND the additional query string provided."
 			 query)))
     (notmuch-search (if (string= notmuch-search-query-string "*")
 			grouped-query
-		      (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first)))
+		      (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first notmuch-search-maxitems)))
 
 (defun notmuch-search-filter-by-tag (tag)
   "Filter the current search results based on a single tag.
@@ -1006,7 +1039,7 @@ Runs a new search matching only messages that match both the
 current search results AND that are tagged with the given tag."
   (interactive
    (list (notmuch-select-tag-with-completion "Filter by tag: ")))
-  (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
+  (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first notmuch-search-maxitems))
 
 ;;;###autoload
 (defun notmuch ()
-- 
1.7.5.4



More information about the notmuch mailing list