[PATCH] emacs: replace oldest-first with sort-order

William Casarin jb55 at jb55.com
Sat Sep 9 17:17:10 PDT 2017


This generalizes notmuch-search-oldest-first to
notmuch-search-sort-order. notmuch-search-sort-order can now be one
of:

  * oldest-first
  * newest-first
  * subject-ascending
  * subject-descending
  * from-ascending
  * from-descending

Order toggling now simply inverts the corresponding option.
---
 emacs/notmuch-hello.el |  8 +++-----
 emacs/notmuch-jump.el  | 13 +++++--------
 emacs/notmuch-lib.el   | 10 ++++++++--
 emacs/notmuch-tree.el  |  2 +-
 emacs/notmuch.el       | 52 ++++++++++++++++++++++++++++----------------------
 5 files changed, 46 insertions(+), 39 deletions(-)

diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el
index c18d74fa..8f700f03 100644
--- a/emacs/notmuch-hello.el
+++ b/emacs/notmuch-hello.el
@@ -579,10 +579,8 @@ with `notmuch-hello-query-counts'."
 		  (widget-insert (make-string column-indent ? )))
 	      (let* ((name (plist-get elem :name))
 		     (query (plist-get elem :query))
-		     (oldest-first (case (plist-get elem :sort-order)
-				     (newest-first nil)
-				     (oldest-first t)
-				     (otherwise notmuch-search-oldest-first)))
+		     (order (plist-get elem :sort-order))
+		     (sort-order (if (not order) notmuch-search-sort-order order))
 		     (search-type (eq (plist-get elem :search-type) 'tree))
 		     (msg-count (plist-get elem :count)))
 		(widget-insert (format "%8s "
@@ -590,7 +588,7 @@ with `notmuch-hello-query-counts'."
 		(widget-create 'push-button
 			       :notify #'notmuch-hello-widget-search
 			       :notmuch-search-terms query
-			       :notmuch-search-oldest-first oldest-first
+			       :notmuch-search-sort-order sort-order
 			       :notmuch-search-type search-type
 			       name)
 		(setq column-indent
diff --git a/emacs/notmuch-jump.el b/emacs/notmuch-jump.el
index 3e20b8c7..5fd654fb 100644
--- a/emacs/notmuch-jump.el
+++ b/emacs/notmuch-jump.el
@@ -48,17 +48,14 @@ fast way to jump to a saved search from anywhere in Notmuch."
       (let* ((saved-search (notmuch-hello-saved-search-to-plist saved-search))
 	     (key (plist-get saved-search :key)))
 	(when key
-	  (let ((name (plist-get saved-search :name))
-		(query (plist-get saved-search :query))
-		(oldest-first
-		 (case (plist-get saved-search :sort-order)
-		   (newest-first nil)
-		   (oldest-first t)
-		   (otherwise (default-value 'notmuch-search-oldest-first)))))
+	  (let* ((name (plist-get saved-search :name))
+		 (query (plist-get saved-search :query))
+		 (order (plist-get saved-search :sort-order))
+		 (sort-order (if (not order) notmuch-search-sort-order order)))
 	    (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 ',sort-order))))
 		  action-map)))))
     (setq action-map (nreverse action-map))
 
diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
index 010be454..ee7b67d3 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -92,14 +92,20 @@ $PATH)."
   :type 'string
   :group 'notmuch-external)
 
-(defcustom notmuch-search-oldest-first t
+(defcustom notmuch-search-sort-order 'oldest-first
   "Show the oldest mail first when searching.
 
 This variable defines the default sort order for displaying
 search results. Note that any filtered searches created by
 `notmuch-search-filter' retain the search order of the parent
 search."
-  :type 'boolean
+  :type '(choice :tag " Sort Order"
+		 (const :tag "Oldest-first" oldest-first)
+		 (const :tag "Newest-first" newest-first)
+		 (const :tag "Subject-ascending" subject-ascending)
+		 (const :tag "Subject-descending" subject-descending)
+		 (const :tag "From-ascending" from-ascending)
+		 (const :tag "From-descending" from-descending))
   :group 'notmuch-search)
 
 (defcustom notmuch-poll-script nil
diff --git a/emacs/notmuch-tree.el b/emacs/notmuch-tree.el
index c00315e8..b95ff71f 100644
--- a/emacs/notmuch-tree.el
+++ b/emacs/notmuch-tree.el
@@ -33,7 +33,7 @@
 (require 'notmuch-parser)
 
 (eval-when-compile (require 'cl))
-(declare-function notmuch-search "notmuch" (&optional query oldest-first target-thread target-line))
+(declare-function notmuch-search "notmuch" (&optional query sort-order target-thread target-line))
 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
 (declare-function notmuch-read-query "notmuch" (prompt))
 (declare-function notmuch-search-find-thread-id "notmuch" (&optional bare))
diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 44402f8a..334458b7 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -380,7 +380,7 @@ Complete list of currently available key bindings:
 
 \\{notmuch-search-mode-map}"
   (make-local-variable 'notmuch-search-query-string)
-  (make-local-variable 'notmuch-search-oldest-first)
+  (make-local-variable 'notmuch-search-sort-order)
   (make-local-variable 'notmuch-search-target-thread)
   (make-local-variable 'notmuch-search-target-line)
   (setq notmuch-buffer-refresh-function #'notmuch-search-refresh-view)
@@ -959,7 +959,7 @@ PROMPT is the string to prompt with."
 
 (put 'notmuch-search 'notmuch-doc "Search for messages.")
 ;;;###autoload
-(defun notmuch-search (&optional query oldest-first target-thread target-line no-display)
+(defun notmuch-search (&optional query sort-order target-thread target-line no-display)
   "Display threads matching QUERY in a notmuch-search buffer.
 
 If QUERY is nil, it is read interactively from the minibuffer.
@@ -982,7 +982,7 @@ the configured default sort order."
     nil
     ;; Use the default search order (if we're doing a search from a
     ;; search buffer, ignore any buffer-local overrides)
-    (default-value 'notmuch-search-oldest-first)))
+    (default-value 'notmuch-search-sort-order)))
 
   (let* ((query (or query (notmuch-read-query "Notmuch search: ")))
 	 (buffer (get-buffer-create (notmuch-search-buffer-title query))))
@@ -993,29 +993,26 @@ the configured default sort order."
     ;; Don't track undo information for this buffer
     (set 'buffer-undo-list t)
     (set 'notmuch-search-query-string query)
-    (set 'notmuch-search-oldest-first oldest-first)
+    (set 'notmuch-search-sort-order sort-order)
     (set 'notmuch-search-target-thread target-thread)
     (set 'notmuch-search-target-line target-line)
     (notmuch-tag-clear-cache)
     (let ((proc (get-buffer-process (current-buffer)))
 	  (inhibit-read-only t))
       (if proc
-	  (error "notmuch search process already running for query `%s'" query)
-	)
+	  (error "notmuch search process already running for query `%s'" query))
       (erase-buffer)
       (goto-char (point-min))
       (save-excursion
-	(let ((proc (notmuch-start-notmuch
-		     "notmuch-search" buffer #'notmuch-search-process-sentinel
-		     "search" "--format=sexp" "--format-version=4"
-		     (if oldest-first
-			 "--sort=oldest-first"
-		       "--sort=newest-first")
-		     query))
-	      ;; Use a scratch buffer to accumulate partial output.
-	      ;; This buffer will be killed by the sentinel, which
-	      ;; should be called no matter how the process dies.
-	      (parse-buf (generate-new-buffer " *notmuch search parse*")))
+	(let* ((proc (notmuch-start-notmuch
+		      "notmuch-search" buffer #'notmuch-search-process-sentinel
+		      "search" "--format=sexp" "--format-version=4"
+		      (concat "--sort=" (symbol-name sort-order))
+		      query))
+	       ;; Use a scratch buffer to accumulate partial output.
+	       ;; This buffer will be killed by the sentinel, which
+	       ;; should be called no matter how the process dies.
+	       (parse-buf (generate-new-buffer " *notmuch search parse*")))
 	  (process-put proc 'parse-buf parse-buf)
 	  (set-process-filter proc 'notmuch-search-process-filter)
 	  (set-process-query-on-exit-flag proc nil))))
@@ -1031,20 +1028,29 @@ thread. Otherwise, point will be moved to attempt to be in the
 same relative position within the new buffer."
   (interactive)
   (let ((target-line (line-number-at-pos))
-	(oldest-first notmuch-search-oldest-first)
+	(sort-order notmuch-search-sort-order)
 	(target-thread (notmuch-search-find-thread-id 'bare))
 	(query notmuch-search-query-string))
     ;; notmuch-search erases the current buffer.
-    (notmuch-search query oldest-first target-thread target-line t)
+    (notmuch-search query sort-order target-thread target-line t)
     (goto-char (point-min))))
 
+(defun notmuch-toggle-order (order)
+  (case order
+    (newest-first 'oldest-first)
+    (oldest-first 'newest-first)
+    (from-ascending 'from-descending)
+    (from-descending 'from-ascending)
+    (subject-ascending 'subject-descending)
+    (subject-descending 'subject-ascending)))
+
 (defun notmuch-search-toggle-order ()
   "Toggle the current search order.
 
 This command toggles the sort order for the current search. The
-default sort order is defined by `notmuch-search-oldest-first'."
+default sort order is defined by `notmuch-search-sort-order'."
   (interactive)
-  (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
+  (set 'notmuch-search-sort-order (notmuch-toggle-order notmuch-search-sort-order))
   (notmuch-search-refresh-view))
 
 (defun notmuch-group-disjunctive-query-string (query-string)
@@ -1068,7 +1074,7 @@ current search results AND the additional query string provided."
     (notmuch-search (if (string= grouped-original-query "*")
 			grouped-query
 		      (concat grouped-original-query " and " grouped-query))
-		    notmuch-search-oldest-first)))
+		    notmuch-search-sort-order)))
 
 (defun notmuch-search-filter-by-tag (tag)
   "Filter the current search results based on a single tag.
@@ -1077,7 +1083,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-sort-order))
 
 ;;;###autoload
 (defun notmuch ()
-- 
2.13.2



More information about the notmuch mailing list