--- /dev/null
+Return-Path: <markwalters1009@gmail.com>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id A78C46DE0217\r
+ for <notmuch@notmuchmail.org>; Tue, 17 May 2016 14:01:20 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.313\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.313 tagged_above=-999 required=5 tests=[AWL=0.257,\r
+ DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7,\r
+ RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id itGiU1RcJdSJ for <notmuch@notmuchmail.org>;\r
+ Tue, 17 May 2016 14:01:12 -0700 (PDT)\r
+Received: from mail-wm0-f52.google.com (mail-wm0-f52.google.com\r
+ [74.125.82.52]) by arlo.cworth.org (Postfix) with ESMTPS id 7BDCB6DE01D0 for\r
+ <notmuch@notmuchmail.org>; Tue, 17 May 2016 14:01:12 -0700 (PDT)\r
+Received: by mail-wm0-f52.google.com with SMTP id g17so50596952wme.1\r
+ for <notmuch@notmuchmail.org>; Tue, 17 May 2016 14:01:12 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id;\r
+ bh=4SHzLp1cGCQCZ8uhtjhfDOa5cFbJA8TTkwrUPqSO0I8=;\r
+ b=bKQg3Yi/PPHUs8HOZ7h26zHycA1LcbzFufsyu6mKJFGDEcmw2+2AcN2NbXRwwhvheX\r
+ cW2N+FXC1TUt6Vw4HGVFErLFZtFCuKQ1UQqnujqzp+KhOrP8Tur6YtqrHfymO/8a0XyJ\r
+ SZ0QQZx3qJrBVPdZp8Jn4ZmHN1o+zv+jsG9HP9V2m5O6sGtbWWTn/rwDbKRalAsdd1nx\r
+ h1QNmuaMW9wM+Nex0B+OfvlL1t5DiNCxeyPk8qiuaOZZkL4bGc49Zty38PdTPDvIT0KA\r
+ b99XQLCPqDETvmpVDSbTTXdlkWzqYk9AiUB9F969qz8Ilid6r9mG1sUE5wjUCANvgvfm\r
+ ZlKw==\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=1e100.net; s=20130820;\r
+ h=x-gm-message-state:from:to:cc:subject:date:message-id;\r
+ bh=4SHzLp1cGCQCZ8uhtjhfDOa5cFbJA8TTkwrUPqSO0I8=;\r
+ b=jIkIeZAoj6wcjDq9sTLM1zzKSs+oRZ/8XzkM39yqMSuHvgLyp0Eq9eKAn37PxxmUdj\r
+ 3brt0Or1U3EDZrEdcSAAbwcsBXEvYtlsf+H8NRKGAy4Ouv3POaBM3P8Sfbs+nQvlhVJ6\r
+ +r73gWo1HrtJQsNfcqsnS+s3Dn1Ubi3ZU6CDIr0tIIN6BH0IW23J6eNhloDWnTZ/H9pa\r
+ TMBTntL6IBK4qZdgBrpvTCP1TgSQNu78od/PXuCXEo3UPZo5td4avMYRC+Oifp9lbLqD\r
+ MPwruq7DtzQ7ave7yfy+MQC34BTN046eVovNwaeZAReqVCYgBPP8QePamdDdDAEH/pUT\r
+ 4MuA==\r
+X-Gm-Message-State:\r
+ AOPr4FXJ7av0F57mNu4R2L/QaeCK67IF6MXGW/ptZQtpdt5sJGTQAC9vbY3a8E1MyVE4aQ==\r
+X-Received: by 10.194.89.33 with SMTP id bl1mr1757936wjb.43.1463518870382;\r
+ Tue, 17 May 2016 14:01:10 -0700 (PDT)\r
+Received: from localhost (5751dfa2.skybroadband.com. [87.81.223.162])\r
+ by smtp.gmail.com with ESMTPSA id xt9sm5032582wjb.17.2016.05.17.14.01.09\r
+ (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+ Tue, 17 May 2016 14:01:09 -0700 (PDT)\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [WIP PATCH] Allow user to sort address completion by count\r
+Date: Tue, 17 May 2016 22:01:05 +0100\r
+Message-Id: <1463518865-23317-1-git-send-email-markwalters1009@gmail.com>\r
+X-Mailer: git-send-email 2.1.4\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Tue, 17 May 2016 21:01:20 -0000\r
+\r
+---\r
+\r
+This is a wip patch which allows the user to sort the address\r
+completions by count. It seems to work but is not heavily\r
+tested. However, I won't have time to work on this for a bit so, since\r
+there was some discussion on irc about this, I thought it worth\r
+posting.\r
+\r
+Best wishes\r
+\r
+Mark\r
+\r
+\r
+\r
+ emacs/notmuch-address.el | 33 ++++++++++++++++++++++++++++-----\r
+ emacs/notmuch-company.el | 3 ++-\r
+ 2 files changed, 30 insertions(+), 6 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el\r
+index aafbe5f..0df8a48 100644\r
+--- a/emacs/notmuch-address.el\r
++++ b/emacs/notmuch-address.el\r
+@@ -51,6 +51,12 @@ to know how address selection is made by default."\r
+ :group 'notmuch-send\r
+ :group 'notmuch-external)\r
+ \r
++(defcustom notmuch-address-sort 't\r
++ "Sort to use for returned address completions"\r
++ :type 'sexp\r
++ :group 'notmuch-send\r
++ :group 'notmuch-external)\r
++\r
+ (defvar notmuch-address-last-harvest 0\r
+ "Time of last address harvest")\r
+ \r
+@@ -102,9 +108,23 @@ The candidates are taken from `notmuch-address-completions'."\r
+ (re (regexp-quote substring)))\r
+ (maphash (lambda (key val)\r
+ (when (string-match re key)\r
+- (push key candidates)))\r
++ (push (cons key val) candidates)))\r
+ notmuch-address-completions)\r
+- candidates))\r
++\r
++ (let ((sorted-candidates\r
++ (cond ((eq notmuch-address-sort 'alphabetical)\r
++ (sort candidates (lambda (a b) (string< (car a) (car b)))))\r
++ ((eq notmuch-address-sort 'count)\r
++ (sort candidates (lambda (a b) (> (cdr a) (cdr b)))))\r
++ ((null notmuch-address-sort) candidates)\r
++ (t\r
++ (sort candidates\r
++ (lambda (a b)\r
++ (or (> (cdr a) (cdr b))\r
++ (and (= (cdr a) (cdr b))\r
++ (string< (car a) (car b))))))))))\r
++\r
++ (mapcar 'car sorted-candidates))))\r
+ \r
+ (defun notmuch-address-options (original)\r
+ "Returns a list of completion candidates. Uses either\r
+@@ -171,8 +191,10 @@ external commands."\r
+ (throw 'found-command bin))))))))\r
+ \r
+ (defun notmuch-address-harvest-addr (result)\r
+- (let ((name-addr (plist-get result :name-addr)))\r
+- (puthash name-addr t notmuch-address-completions)))\r
++ (let ((name-addr (plist-get result :name-addr))\r
++ (count (plist-get result :count)))\r
++ (message "Putting name %s with count %s" name-addr count)\r
++ (puthash name-addr count notmuch-address-completions)))\r
+ \r
+ (defun notmuch-address-harvest-handle-result (obj)\r
+ (notmuch-address-harvest-addr obj))\r
+@@ -201,12 +223,13 @@ time so the address collection runs asynchronously unless\r
+ SYNCHRONOUS is t. In case of asynchronous execution, CALLBACK is\r
+ called when harvesting finishes."\r
+ (let* ((from-me-query (mapconcat (lambda (x) (concat "from:" x)) (notmuch-user-emails) " or "))\r
+- (query (if filter-query\r
++ (query (if (stringp filter-query)\r
+ (format "(%s) and (%s)" from-me-query filter-query)\r
+ from-me-query))\r
+ (args `("address" "--format=sexp" "--format-version=2"\r
+ "--output=recipients"\r
+ "--deduplicate=address"\r
++ "--output=count"\r
+ ,query)))\r
+ (if synchronous\r
+ (mapc #'notmuch-address-harvest-addr\r
+diff --git a/emacs/notmuch-company.el b/emacs/notmuch-company.el\r
+index b881d6d..bf3c6d8 100644\r
+--- a/emacs/notmuch-company.el\r
++++ b/emacs/notmuch-company.el\r
+@@ -80,7 +80,8 @@\r
+ (match (if (string-match notmuch-company-last-prefix arg)\r
+ (match-end 0)\r
+ 0))\r
+- (no-cache t))))\r
++ (no-cache t)\r
++ (sorted t))))\r
+ \r
+ \r
+ (provide 'notmuch-company)\r
+-- \r
+2.1.4\r
+\r