[notmuch-emacs] Paginate user's saved search results - proposed patch

classic Classic list List threaded Threaded
1 message Options
Antoine R. Dumont Antoine R. Dumont
Reply | Threaded
Open this post in threaded view

[notmuch-emacs] Paginate user's saved search results - proposed patch


diff --git a/emacs/notmuch-jump.el b/emacs/notmuch-jump.el
index 3e20b8c7..5cc6e42e 100644
--- a/emacs/notmuch-jump.el
+++ b/emacs/notmuch-jump.el
@@ -54,11 +54,12 @@ fast way to jump to a saved search from anywhere in Notmuch."
  (case (plist-get saved-search :sort-order)
    (newest-first nil)
    (oldest-first t)
-   (otherwise (default-value 'notmuch-search-oldest-first)))))
+   (otherwise (default-value 'notmuch-search-oldest-first))))
+ (limit (plist-get saved-search :limit)))
     (push (list key name
  (if (eq (plist-get saved-search :search-type) 'tree)
     `(lambda () (notmuch-tree ',query))
-  `(lambda () (notmuch-search ',query ',oldest-first))))
+  `(lambda () (notmuch-search ',query ',oldest-first nil nil nil ',limit))))
     (setq action-map (nreverse action-map))
diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 1f453357..078eae45 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -189,6 +189,8 @@ there will be called at other points of notmuch execution."
     (define-key map "+" 'notmuch-search-add-tag)
     (define-key map (kbd "RET") 'notmuch-search-show-thread)
     (define-key map "Z" 'notmuch-tree-from-search-current-query)
+    (define-key map (kbd "M-n") 'notmuch-search-query-result-next)
+    (define-key map (kbd "M-p") 'notmuch-search-query-result-previous)
   "Keymap for \"notmuch search\" buffers.")
 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
@@ -383,6 +385,8 @@ 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-query-limit)
+  (make-local-variable 'notmuch-search-query-offset)
   (setq notmuch-buffer-refresh-function #'notmuch-search-refresh-view)
   (set (make-local-variable 'scroll-preserve-screen-position) t)
   (add-to-invisibility-spec (cons 'ellipsis t))
@@ -959,7 +963,7 @@ PROMPT is the string to prompt with."
 (put 'notmuch-search 'notmuch-doc "Search for messages.")
-(defun notmuch-search (&optional query oldest-first target-thread target-line no-display)
+(defun notmuch-search (&optional query oldest-first target-thread target-line no-display limit offset)
   "Display threads matching QUERY in a notmuch-search buffer.
 If QUERY is nil, it is read interactively from the minibuffer.
@@ -973,6 +977,8 @@ Other optional parameters are used as follows:
   NO-DISPLAY: Do not try to foreground the search results buffer. If it is
               already foregrounded i.e. displayed in a window, this has no
               effect, meaning the buffer will remain visible.
+  LIMIT: Try to limit the search to a given size. Defaults to no limit.
+  OFFSET: Start from offset. Defaults to nil
 When called interactively, this will prompt for a query and use
 the configured default sort order."
@@ -994,6 +1000,8 @@ the configured default sort order."
     (set 'buffer-undo-list t)
     (set 'notmuch-search-query-string query)
     (set 'notmuch-search-oldest-first oldest-first)
+    (set 'notmuch-search-query-limit limit)
+    (set 'notmuch-search-query-offset (if offset offset 0))
     (set 'notmuch-search-target-thread target-thread)
     (set 'notmuch-search-target-line target-line)
@@ -1011,6 +1019,12 @@ the configured default sort order."
      (if oldest-first
+                     (if limit
+                         (format "--limit=%s" limit)
+                       "")
+                     (if offset
+                         (format "--offset=%s" offset)
+                       "")
       ;; Use a scratch buffer to accumulate partial output.
       ;; This buffer will be killed by the sentinel, which
@@ -1021,6 +1035,22 @@ the configured default sort order."
   (set-process-query-on-exit-flag proc nil))))
     (run-hooks 'notmuch-search-hook)))
+(defun notmuch-search-query-result-next ()
+  "Trigger the next results page for the current query."
+  (interactive)
+  (when notmuch-search-query-offset
+    (setq notmuch-search-query-offset
+          (+ notmuch-search-query-offset notmuch-search-query-limit)))
+  (notmuch-search-refresh-view))
+(defun notmuch-search-query-result-previous ()
+  "Trigger the previous results page for the current query."
+  (interactive)
+  (when (and notmuch-search-query-offset (> notmuch-search-query-offset 0))
+    (setq notmuch-search-query-offset
+          (max 0 (- notmuch-search-query-offset notmuch-search-query-limit))))
+  (notmuch-search-refresh-view))
 (defun notmuch-search-refresh-view ()
   "Refresh the current view.
@@ -1033,9 +1063,11 @@ same relative position within the new buffer."
   (let ((target-line (line-number-at-pos))
  (oldest-first notmuch-search-oldest-first)
  (target-thread (notmuch-search-find-thread-id 'bare))
- (query notmuch-search-query-string))
+ (query notmuch-search-query-string)
+        (limit notmuch-search-query-limit)
+        (offset notmuch-search-query-offset))
     ;; notmuch-search erases the current buffer.
-    (notmuch-search query oldest-first target-thread target-line t)
+    (notmuch-search query oldest-first target-thread target-line t limit offset)
     (goto-char (point-min))))
 (defun notmuch-search-toggle-order ()

Here is a patch (against current master - 7f726c6e) to allow user
saved-search query pagination.  This is an opt-in option to be defined
on per saved-search basis.

To opt-in, define a :limit option on a saved-search.
If not defined, the current behavior (no limit) is kept.

# example

      '((:name "inbox"
         :query "tag:inbox"
         :count-query "tag:inbox"
         :sort-order 'newest-first
         :limit 30
         :key "i")
        (:name "unread"
         :query "tag:inbox and tag:unread"
         :count-query "tag:inbox and tag:unread"
         :sort-order 'newest-first
         :key "u"))))

Here the 'unread' saved-search will work as usual.
The 'inbox' one will paginate results by page of 30.
The user can then use M-n to move forward or M-p to move backward in

What this patch does not do:
- no new buttons in the search page view.
- no documentation patch about this option on saved-search

- Relevant threads:
  - id:[hidden email] (from 7 years
  - The state part was unclear, j4ni's old diff on that matter unstuck
  me http://paste.debian.net/1050094/
  - https://notmuchmail.org/pipermail/notmuch/2017/024495.html
  - https://notmuchmail.org/pipermail/notmuch/2017/024496.html
  - https://notmuchmail.org/pipermail/notmuch/2017/024497.html
- notmuchmail.org is down (some part at least, so i cannot download the
  archive, convert and reply to the right thread, sorry)

Thanks to bremner and j4ni for their help on irc

tony / @ardumont

gpg fingerprint BF00 203D 741A C9D5 46A8 BE07 52E2 E984 0D10 C3B8

notmuch mailing list
[hidden email]

signature.asc (847 bytes) Download Attachment