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

Jani Nikula jani at nikula.org
Fri Jan 27 11:35:00 PST 2012


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
results.

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

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

---

Rebased since id:"1321560458-32010-1-git-send-email-jani at nikula.org",
and added option to limit results to window height. Limiting does not
work through the 's' key binding in search view, and some of the
issues reported by Austin in id:"20111104205415.GJ22935 at mit.edu" and
Aneesh in id:"87hb1gu643.fsf at linux.vnet.ibm.com" are unfortunately
still there.

If anyone wants to pick this up from here, I'd be delighted. I use
this, but don't have the time to polish. This is just to share the
rebased patch as Aneesh was asking for it on IRC.
---
 emacs/notmuch-hello.el |   21 +++++++++++++--
 emacs/notmuch.el       |   64 +++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 71 insertions(+), 14 deletions(-)

diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el
index d17a30f..9ed68e2 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" ())
 
 (defcustom notmuch-hello-recent-searches-max 10
@@ -34,6 +34,21 @@
   :type 'integer
   :group 'notmuch-hello)
 
+(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. Set to 0 to limit to window
+height. The search results will contain push buttons to double
+the number (can be repeated) or show unlimited number of results.
+
+If set to nil, the number of results is not limited."
+  :type '(choice (const :tag "Unlimited" nil)
+		 (const :tag "Window height" 0)
+		 (integer :tag "Limit"))
+  :group 'notmuch-search
+  :group 'notmuch)
+
 (defcustom notmuch-show-empty-saved-searches nil
   "Should saved searches with no messages be listed?"
   :type 'boolean
@@ -178,7 +193,7 @@ International Bureau of Weights and Measures."
     (setq search (notmuch-hello-trim search))
     (let ((history-delete-duplicates t))
       (add-to-history 'notmuch-search-history search)))
-  (notmuch-search search notmuch-search-oldest-first nil nil
+  (notmuch-search search notmuch-search-oldest-first notmuch-search-limit nil nil
 		  #'notmuch-hello-search-continuation))
 
 (defun notmuch-hello-add-saved-search (widget)
@@ -228,7 +243,7 @@ diagonal."
 (defun notmuch-hello-widget-search (widget &rest ignore)
   (notmuch-search (widget-get widget
 			      :notmuch-search-terms)
-		  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 72f78ed..b3fef88 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -231,6 +231,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)
@@ -277,6 +278,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]\\>")
 
@@ -418,6 +420,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))
@@ -680,6 +683,11 @@ This function advances the next thread when finished."
 			(insert "End of search results.")
 			(unless (= 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")))
@@ -957,7 +965,7 @@ PROMPT is the string to prompt with."
 			      'notmuch-search-history nil nil)))))
 
 ;;;###autoload
-(defun notmuch-search (&optional query oldest-first target-thread target-line continuation)
+(defun notmuch-search (&optional query oldest-first limit target-thread target-line continuation)
   "Run \"notmuch search\" with the given `query' and display results.
 
 If `query' is nil, it is read interactively from the minibuffer.
@@ -978,6 +986,9 @@ Other optional parameters are used as follows:
     (set 'buffer-undo-list t)
     (set 'notmuch-search-query-string query)
     (set 'notmuch-search-oldest-first oldest-first)
+    (if (and limit (= limit 0))
+	(setq limit (- (window-body-height) 1)))
+    (set 'notmuch-search-limit limit)
     (set 'notmuch-search-target-thread target-thread)
     (set 'notmuch-search-target-line target-line)
     (set 'notmuch-search-continuation continuation)
@@ -989,13 +1000,19 @@ Other optional parameters are used as follows:
       (erase-buffer)
       (goto-char (point-min))
       (save-excursion
-	(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))))
@@ -1012,13 +1029,38 @@ same relative position within the new buffer."
   (interactive)
   (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-kill-this-buffer)
-    (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 nil
   "An external script to incorporate new mail into the notmuch database.
 
@@ -1092,7 +1134,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-limit)))
 
 (defun notmuch-search-filter-by-tag (tag)
   "Filter the current search results based on a single tag.
@@ -1101,7 +1143,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-limit))
 
 ;;;###autoload
 (defun notmuch ()
-- 
1.7.5.4



More information about the notmuch mailing list