--- /dev/null
+Return-Path: <bremner@tethera.net>\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 462606DE1329\r
+ for <notmuch@notmuchmail.org>; Fri, 23 Oct 2015 17:22:17 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.104\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.104 tagged_above=-999 required=5 tests=[AWL=0.094, \r
+ T_FILL_THIS_FORM_SHORT=0.01] 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 mej0Oh08C7Gx for <notmuch@notmuchmail.org>;\r
+ Fri, 23 Oct 2015 17:22:12 -0700 (PDT)\r
+Received: from gitolite.debian.net (gitolite.debian.net [87.98.215.224])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 888BA6DE1403\r
+ for <notmuch@notmuchmail.org>; Fri, 23 Oct 2015 17:22:12 -0700 (PDT)\r
+Received: from remotemail by gitolite.debian.net with local (Exim 4.80)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1Zpmaa-0006nC-Je; Sat, 24 Oct 2015 00:21:40 +0000\r
+Received: (nullmailer pid 26051 invoked by uid 1000); Sat, 24 Oct 2015\r
+ 00:20:41 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v5 1/3] Emacs: Add address completion mechanism implemented\r
+ in elisp\r
+Date: Fri, 23 Oct 2015 21:20:35 -0300\r
+Message-Id: <1445646037-25994-2-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.6.1\r
+In-Reply-To: <1445646037-25994-1-git-send-email-david@tethera.net>\r
+References: <1445646037-25994-1-git-send-email-david@tethera.net>\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: Sat, 24 Oct 2015 00:22:17 -0000\r
+\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+\r
+Currently, notmuch has an address completion mechanism that requires\r
+external command to provide completion candidates. This patch adds a\r
+completion mechanism inspired by https://github.com/tjim/nevermore,\r
+which is implemented in Emacs lisp only.\r
+\r
+The preexisting address completion mechanism, activated by pressing TAB\r
+on To/Cc lines, is extended to use the new mechanism when no external\r
+command is configured, i.e. when notmuch-address-command to nil, which\r
+is the new default.\r
+\r
+The core of the new mechanism is the function notmuch-address-harvest,\r
+which collects the completion candidates from the notmuch database and\r
+stores them in notmuch-address-completions variable. The address\r
+harvesting can run either synchronously (same as with the previous\r
+mechanism) or asynchronously. When the user presses TAB for the first\r
+time, synchronous harvesting limited to user entered text is performed.\r
+If the entered text is reasonably long, this operation is relatively\r
+fast. Then, asynchronous harvesting over the full database is triggered.\r
+This operation may take long time (minutes on rotating disk). After it\r
+finishes, no harvesting is normally performed again and subsequent\r
+completion requests use the harvested data cached in memory. Completion\r
+cache is updated after 24 hours.\r
+\r
+Note that the change of the notmuch-address-command default value\r
+may *BREAK EXISTING SETUPS* when the user used external command named\r
+"notmuch-addresses", i.e. the previous default. The result will be that\r
+the user will use the new mechanism instead of the his command. I\r
+believe that many users may not even recognize this because the new\r
+mechanism works the same as\r
+http://commonmeasure.org/~jkr/git/notmuch_addresses.git and perhaps also\r
+as other commands suggested at\r
+http://notmuchmail.org/emacstips/#address_completion.\r
+---\r
+ emacs/notmuch-address.el | 123 ++++++++++++++++++++++++++++++++++++++++++++---\r
+ emacs/notmuch-lib.el | 3 ++\r
+ 2 files changed, 118 insertions(+), 8 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el\r
+index fde3c1b..9f6711b 100644\r
+--- a/emacs/notmuch-address.el\r
++++ b/emacs/notmuch-address.el\r
+@@ -20,14 +20,18 @@\r
+ ;; Authors: David Edmondson <dme@dme.org>\r
+ \r
+ (require 'message)\r
++(require 'notmuch-query)\r
++(require 'notmuch-parser)\r
+ \r
+ ;;\r
+ \r
+-(defcustom notmuch-address-command "notmuch-addresses"\r
+- "The command which generates possible addresses. It must take a\r
+-single argument and output a list of possible matches, one per\r
+-line."\r
+- :type 'string\r
++(defcustom notmuch-address-command nil\r
++ "The command which generates possible addresses for completion.\r
++It must take a single argument and output a list of possible\r
++matches, one per line. If set to nil, addresses are generated by\r
++a built-in completion mechanism."\r
++ :type '(radio (const :tag "No command: Use built-in completion" nil)\r
++ (string :tag "Custom command" :value "notmuch-addresses"))\r
+ :group 'notmuch-send\r
+ :group 'notmuch-external)\r
+ \r
+@@ -42,6 +46,17 @@ to know how address selection is made by default."\r
+ :group 'notmuch-send\r
+ :group 'notmuch-external)\r
+ \r
++(defvar notmuch-address-last-harvest 0\r
++ "Time of last address harvest")\r
++\r
++(defvar notmuch-address-completions (make-hash-table :test 'equal)\r
++ "Hash of email addresses for completion during email composition.\r
++ This variable is set by calling `notmuch-address-harvest'.")\r
++\r
++(defvar notmuch-address-full-harvest-finished nil\r
++ "t indicates that full completion address harvesting has been\r
++finished")\r
++\r
+ (defun notmuch-address-selection-function (prompt collection initial-input)\r
+ "Call (`completing-read'\r
+ PROMPT COLLECTION nil nil INITIAL-INPUT 'notmuch-address-history)"\r
+@@ -59,8 +74,32 @@ to know how address selection is made by default."\r
+ (setq message-completion-alist\r
+ (push notmuch-address-message-alist-member message-completion-alist))))\r
+ \r
++(defun notmuch-address-matching (substring)\r
++ "Returns a list of completion candidates matching SUBSTRING.\r
++The candidates are taked form `notmuch-address-completions'."\r
++ (let ((candidates)\r
++ (re (regexp-quote substring)))\r
++ (maphash (lambda (key val)\r
++ (when (string-match re key)\r
++ (push key candidates)))\r
++ notmuch-address-completions)\r
++ candidates))\r
++\r
+ (defun notmuch-address-options (original)\r
+- (process-lines notmuch-address-command original))\r
++ "Returns a list of completion candidates. Uses either\r
++elisp-based implementation or older implementation requiring\r
++external commands."\r
++ (cond\r
++ ((null notmuch-address-command)\r
++ (when (not notmuch-address-full-harvest-finished)\r
++ ;; First, run quick synchronous harvest based on what the user\r
++ ;; entered so far\r
++ (notmuch-address-harvest (format "to:%s*" original) t))\r
++ (prog1 (notmuch-address-matching original)\r
++ ;; Then (re)start potentially long-running full asynchronous harvesting\r
++ (notmuch-address-harvest-trigger)))\r
++ (t\r
++ (process-lines notmuch-address-command original))))\r
+ \r
+ (defun notmuch-address-expand-name ()\r
+ (let* ((end (point))\r
+@@ -109,11 +148,79 @@ to know how address selection is made by default."\r
+ (not (file-directory-p bin))))\r
+ (throw 'found-command bin))))))))\r
+ \r
++(defun notmuch-address-harvest-msg (msg)\r
++ (let* ((headers (plist-get msg :headers))\r
++ (to (ignore-errors (mail-extract-address-components (plist-get headers :To) t)))\r
++ (cc (ignore-errors (mail-extract-address-components (plist-get headers :Cc) t)))\r
++ (bcc (ignore-errors (mail-extract-address-components (plist-get headers :Bcc) t))))\r
++ (mapc (lambda (parts)\r
++ (let* ((name (car parts))\r
++ (email (cadr parts))\r
++ (entry (if name (format "%s <%s>" name email) email)))\r
++ (puthash entry t notmuch-address-completions)))\r
++ (append to cc bcc))\r
++ nil))\r
++\r
++(defun notmuch-address-harvest-handle-result (obj)\r
++ (notmuch-query-map-threads 'notmuch-address-harvest-msg (list obj)))\r
++\r
++(defun notmuch-address-harvest-filter (proc string)\r
++ (when (buffer-live-p (process-buffer proc))\r
++ (with-current-buffer (process-buffer proc)\r
++ (save-excursion\r
++ (goto-char (point-max))\r
++ (insert string))\r
++ (notmuch-sexp-parse-partial-list\r
++ 'notmuch-address-harvest-handle-result (process-buffer proc)))))\r
++\r
++(defvar notmuch-address-harvest-proc nil) ; the process of a harvest underway\r
++\r
++(defun notmuch-address-harvest (&optional filter-query synchronous callback)\r
++ "Collect addresses completion candidates. It queries the\r
++notmuch database for all messages sent by the user optionally\r
++matching FILTER-QUERY (if not nil). It collects the destination\r
++addresses from those messages and stores them in\r
++`notmuch-address-completions'. Address harvesting may take some\r
++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
++ (format "(%s) and (%s)" from-me-query filter-query)\r
++ from-me-query))\r
++ (args `("show" "--format=sexp" "--format-version=2"\r
++ "--body=false" "--entire-thread=false" ,query)))\r
++ (if synchronous\r
++ (notmuch-query-map-threads 'notmuch-address-harvest-msg\r
++ (apply 'notmuch-call-notmuch-sexp args))\r
++ ;; Asynchronous\r
++ (when notmuch-address-harvest-proc\r
++ (kill-buffer (process-buffer notmuch-address-harvest-proc))) ; this also kills the process\r
++ (setq notmuch-address-harvest-proc\r
++ (apply 'notmuch-start-notmuch\r
++ "notmuch-address-harvest" ; process name\r
++ " *notmuch-address-harvest*" ; process buffer\r
++ callback ; process sentinel\r
++ args))\r
++ (set-process-filter notmuch-address-harvest-proc 'notmuch-address-harvest-filter)\r
++ (set-process-query-on-exit-flag notmuch-address-harvest-proc nil)))\r
++ ;; return value\r
++ nil)\r
++\r
+ ;; If we can find the program specified by `notmuch-address-command',\r
+-;; insinuate ourselves into `message-mode'.\r
+-(when (notmuch-address-locate-command notmuch-address-command)\r
++;; or if it is nil, insinuate ourselves into `message-mode'.\r
++(when (or (null notmuch-address-command)\r
++ (notmuch-address-locate-command notmuch-address-command))\r
+ (notmuch-address-message-insinuate))\r
+ \r
++(defun notmuch-address-harvest-trigger ()\r
++ (let ((now (float-time)))\r
++ (when (> (- now notmuch-address-last-harvest) 86400)\r
++ (setq notmuch-address-last-harvest now)\r
++ (notmuch-address-harvest nil nil\r
++ (lambda (proc event)\r
++ (setq notmuch-address-full-harvest-finished t))))))\r
++\r
+ ;;\r
+ \r
+ (provide 'notmuch-address)\r
+diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
+index 201d7ec..1c3a9fe 100644\r
+--- a/emacs/notmuch-lib.el\r
++++ b/emacs/notmuch-lib.el\r
+@@ -232,6 +232,9 @@ on the command line, and then retry your notmuch command")))\r
+ "Return the user.other_email value (as a list) from the notmuch configuration."\r
+ (split-string (notmuch-config-get "user.other_email") "\n" t))\r
+ \r
++(defun notmuch-user-emails ()\r
++ (cons (notmuch-user-primary-email) (notmuch-user-other-email)))\r
++\r
+ (defun notmuch-poll ()\r
+ "Run \"notmuch new\" or an external script to import mail.\r
+ \r
+-- \r
+2.6.1\r
+\r