[WIP PATCH] Allow user to sort address completion by count

Mark Walters markwalters1009 at gmail.com
Tue May 17 14:01:05 PDT 2016


---

This is a wip patch which allows the user to sort the address
completions by count. It seems to work but is not heavily
tested. However, I won't have time to work on this for a bit so, since
there was some discussion on irc about this, I thought it worth
posting.

Best wishes

Mark



 emacs/notmuch-address.el | 33 ++++++++++++++++++++++++++++-----
 emacs/notmuch-company.el |  3 ++-
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el
index aafbe5f..0df8a48 100644
--- a/emacs/notmuch-address.el
+++ b/emacs/notmuch-address.el
@@ -51,6 +51,12 @@ to know how address selection is made by default."
   :group 'notmuch-send
   :group 'notmuch-external)
 
+(defcustom notmuch-address-sort 't
+  "Sort to use for returned address completions"
+  :type 'sexp
+  :group 'notmuch-send
+  :group 'notmuch-external)
+
 (defvar notmuch-address-last-harvest 0
   "Time of last address harvest")
 
@@ -102,9 +108,23 @@ The candidates are taken from `notmuch-address-completions'."
 	(re (regexp-quote substring)))
     (maphash (lambda (key val)
 	       (when (string-match re key)
-		 (push key candidates)))
+		 (push (cons key val) candidates)))
 	     notmuch-address-completions)
-    candidates))
+
+    (let ((sorted-candidates
+	   (cond ((eq notmuch-address-sort 'alphabetical)
+		  (sort candidates (lambda (a b) (string< (car a) (car b)))))
+		 ((eq notmuch-address-sort 'count)
+		  (sort candidates (lambda (a b) (> (cdr a) (cdr b)))))
+		 ((null notmuch-address-sort) candidates)
+		 (t
+		  (sort candidates
+			(lambda (a b)
+			  (or (> (cdr a) (cdr b))
+			      (and (= (cdr a) (cdr b))
+				   (string< (car a) (car b))))))))))
+
+      (mapcar 'car sorted-candidates))))
 
 (defun notmuch-address-options (original)
   "Returns a list of completion candidates. Uses either
@@ -171,8 +191,10 @@ external commands."
 	      (throw 'found-command bin))))))))
 
 (defun notmuch-address-harvest-addr (result)
-  (let ((name-addr (plist-get result :name-addr)))
-    (puthash name-addr t notmuch-address-completions)))
+  (let ((name-addr (plist-get result :name-addr))
+	(count (plist-get result :count)))
+    (message "Putting name %s with count %s" name-addr count)
+    (puthash name-addr count notmuch-address-completions)))
 
 (defun notmuch-address-harvest-handle-result (obj)
   (notmuch-address-harvest-addr obj))
@@ -201,12 +223,13 @@ time so the address collection runs asynchronously unless
 SYNCHRONOUS is t. In case of asynchronous execution, CALLBACK is
 called when harvesting finishes."
   (let* ((from-me-query (mapconcat (lambda (x) (concat "from:" x)) (notmuch-user-emails) " or "))
-	 (query (if filter-query
+	 (query (if (stringp filter-query)
 		    (format "(%s) and (%s)" from-me-query filter-query)
 		  from-me-query))
 	 (args `("address" "--format=sexp" "--format-version=2"
 		 "--output=recipients"
 		 "--deduplicate=address"
+		 "--output=count"
 		 ,query)))
     (if synchronous
 	(mapc #'notmuch-address-harvest-addr
diff --git a/emacs/notmuch-company.el b/emacs/notmuch-company.el
index b881d6d..bf3c6d8 100644
--- a/emacs/notmuch-company.el
+++ b/emacs/notmuch-company.el
@@ -80,7 +80,8 @@
       (match (if (string-match notmuch-company-last-prefix arg)
 		 (match-end 0)
 	       0))
-      (no-cache t))))
+      (no-cache t)
+      (sorted t))))
 
 
 (provide 'notmuch-company)
-- 
2.1.4



More information about the notmuch mailing list