[DRAFT PATCH] emacs: support limiting the number of results shown in search results

Jani Nikula jani at nikula.org
Thu Nov 17 12:07:38 PST 2011

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

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

Signed-off-by: Jani Nikula <jani at nikula.org>


Note: This is not intended for inclusion yet. It works, but I'd like to try
to simplify things a bit (in this patch and otherwise) by keeping the
relevant variables buffer local over notmuch-search-refresh-view. This
would mean not killing the search buffer in between.

I just wanted to share the work-in-progress, rebased against the current
master and the merged lib/cli dependencies. The previous versions of the
emacs part is at:
id:"0b937eb1103dbb24142a783eff599a7cb08f195e.1320093940.git.jani at nikula.org"
 emacs/notmuch-hello.el |   18 +++++++++++--
 emacs/notmuch.el       |   62 +++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 66 insertions(+), 14 deletions(-)

diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el
index 1a76c30..0e165e7 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 limit target-thread target-line continuation))
 (declare-function notmuch-poll "notmuch" ())
 (defvar notmuch-hello-search-bar-marker nil
@@ -37,6 +37,18 @@
   :type 'integer
   :group 'notmuch)
+(defcustom notmuch-search-limit nil
+  "The maximum number of results to show in search results.
+This variables controls the maximum number of results to
+initially show in search results. If nil, the number of results
+is not limited. If non-nil, the search results will contain push
+buttons to double the number (can be repeated) or show unlimited
+number of results."
+  :type '(choice (const :tag "Unlimited" nil)
+		 (integer :tag "Limit"))
+  :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 notmuch-search-limit nil nil #'notmuch-hello-search-continuation)))
 (defun notmuch-hello-add-saved-search (widget)
@@ -200,7 +212,7 @@ diagonal."
 (defun notmuch-hello-widget-search (widget &rest ignore)
   (notmuch-search (widget-get widget
-		  notmuch-search-oldest-first
+		  notmuch-search-oldest-first notmuch-search-limit
 		  nil nil #'notmuch-hello-search-continuation))
 (defun notmuch-saved-search-count (search)
diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index c1827cc..a644cef 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -196,6 +196,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)
@@ -241,6 +242,7 @@ For a mouse binding, return nil."
 (defvar notmuch-search-target-thread)
 (defvar notmuch-search-target-line)
 (defvar notmuch-search-continuation)
+(defvar notmuch-search-limit)
 (defvar notmuch-search-disjunctive-regexp      "\\<[oO][rR]\\>")
@@ -375,6 +377,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-limit)
   (set (make-local-variable 'notmuch-search-continuation) nil)
   (set (make-local-variable 'scroll-preserve-screen-position) t)
   (add-to-invisibility-spec (cons 'ellipsis t))
@@ -631,6 +634,11 @@ 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 (and notmuch-search-limit
+				 (< 0 notmuch-search-limit)
+				 (< notmuch-search-limit
+				    (count-lines (point-min) (point-max))))
+			    (notmuch-search-setup-buttons))
 			(insert "\n")
 			(if (and atbob
 				 (not (string= notmuch-search-target-thread "found")))
@@ -906,7 +914,7 @@ PROMPT is the string to prompt with."
 			    'notmuch-query-history nil nil))))
-(defun notmuch-search (query &optional oldest-first target-thread target-line continuation)
+(defun notmuch-search (query &optional oldest-first limit target-thread target-line continuation)
   "Run \"notmuch search\" with the given query string and display results.
 The optional parameters are used as follows:
@@ -922,6 +930,7 @@ The optional parameters are used as follows:
     (set 'notmuch-search-query-string query)
     (set 'notmuch-search-oldest-first oldest-first)
+    (set 'notmuch-search-limit limit)
     (set 'notmuch-search-target-thread target-thread)
     (set 'notmuch-search-target-line target-line)
     (set 'notmuch-search-continuation continuation)
@@ -933,13 +942,19 @@ The optional parameters are used as follows:
       (goto-char (point-min))
-	(let ((proc (start-process
-		     "notmuch-search" buffer
-		     notmuch-command "search"
-		     (if oldest-first
-			 "--sort=oldest-first"
-		       "--sort=newest-first")
-		     query)))
+	(let* ((args (append
+		      (if oldest-first
+			  (list "--sort=oldest-first")
+			(list "--sort=newest-first"))
+		      (if (and limit (< 0 limit))
+			  (if oldest-first
+			      (list (format "--offset=-%d" limit))
+			    (list (format "--limit=%d" limit))))
+		      (list query)))
+	       (proc (apply 'start-process
+			    "notmuch-search" buffer
+			    notmuch-command "search"
+			    args)))
 	  (set-process-sentinel proc 'notmuch-search-process-sentinel)
 	  (set-process-filter proc 'notmuch-search-process-filter)
 	  (set-process-query-on-exit-flag proc nil))))
@@ -956,13 +971,38 @@ same relative position within the new buffer."
   (let ((target-line (line-number-at-pos))
 	(oldest-first notmuch-search-oldest-first)
+	(limit notmuch-search-limit)
 	(target-thread (notmuch-search-find-thread-id))
 	(query notmuch-search-query-string)
 	(continuation notmuch-search-continuation))
-    (notmuch-search query oldest-first target-thread target-line continuation)
+    (notmuch-search query oldest-first limit target-thread target-line continuation)
     (goto-char (point-min))))
+(defun notmuch-search-double-results (&rest ignore)
+  (if notmuch-search-limit
+      (setq notmuch-search-limit (* 2 notmuch-search-limit)))
+  (notmuch-search-refresh-view))
+(defun notmuch-search-unlimited-results (&rest ignore)
+  (setq notmuch-search-limit nil)
+  (notmuch-search-refresh-view))
+(defun notmuch-search-setup-buttons ()
+  (widget-insert "    ")
+  (widget-create 'push-button
+		 :keymap widget-keymap
+		 :notify 'notmuch-search-double-results
+		 :help-echo "Double the number of results shown"
+		 "Show 2X results")
+  (widget-insert "    ")
+  (widget-create 'push-button
+		 :keymap widget-keymap
+		 :notify 'notmuch-search-unlimited-results
+		 :help-echo "Show all search results"
+		 "Show unlimited results")
+  (widget-setup))
 (defcustom notmuch-poll-script ""
   "An external script to incorporate new mail into the notmuch database.
@@ -1021,7 +1061,7 @@ current search results AND the additional query string provided."
     (notmuch-search (if (string= notmuch-search-query-string "*")
-		      (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-limit)))
 (defun notmuch-search-filter-by-tag (tag)
   "Filter the current search results based on a single tag.
@@ -1030,7 +1070,7 @@ Runs a new search matching only messages that match both the
 current search results AND that are tagged with the given tag."
    (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-limit))
 (defun notmuch ()

More information about the notmuch mailing list