--- /dev/null
+Return-Path: <sojkam1@fel.cvut.cz>\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 230546DE17D7\r
+ for <notmuch@notmuchmail.org>; Mon, 26 Oct 2015 16:30:12 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.612\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.612 tagged_above=-999 required=5 tests=[AWL=2.238,\r
+ RCVD_IN_DNSWL_MED=-2.3, RP_MATCHES_RCVD=-0.55] 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 K91wbfQuo-P7 for <notmuch@notmuchmail.org>;\r
+ Mon, 26 Oct 2015 16:30:09 -0700 (PDT)\r
+X-Greylist: delayed 421 seconds by postgrey-1.35 at arlo;\r
+ Mon, 26 Oct 2015 16:30:08 PDT\r
+Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
+ by arlo.cworth.org (Postfix) with ESMTP id C2C356DE0B4B\r
+ for <notmuch@notmuchmail.org>; Mon, 26 Oct 2015 16:30:08 -0700 (PDT)\r
+Received: from localhost (unknown [192.168.200.7])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id 5DAFF19F499E;\r
+ Tue, 27 Oct 2015 00:23:05 +0100 (CET)\r
+X-Virus-Scanned: IMAP STYX AMAVIS\r
+Received: from max.feld.cvut.cz ([192.168.200.1])\r
+ by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new, port 10044)\r
+ with ESMTP id HeYVG38Z3t1B; Tue, 27 Oct 2015 00:23:03 +0100 (CET)\r
+Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id AFE6019F49C3;\r
+ Tue, 27 Oct 2015 00:23:02 +0100 (CET)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.86)\r
+ (envelope-from <sojkam1@fel.cvut.cz>)\r
+ id 1Zqr6S-0007al-LC; Tue, 27 Oct 2015 00:23:00 +0100\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v8 3/3] Emacs: Add address completion based on company-mode\r
+Date: Tue, 27 Oct 2015 00:22:49 +0100\r
+Message-Id: <1445901769-29134-4-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.5.3\r
+In-Reply-To: <1445901769-29134-1-git-send-email-sojkam1@fel.cvut.cz>\r
+References: <1445901769-29134-1-git-send-email-sojkam1@fel.cvut.cz>\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: Mon, 26 Oct 2015 23:30:12 -0000\r
+\r
+When company-mode is available (Emacs >= 24), address completion\r
+candidates are shown in a nice popup box. This is triggered either by\r
+pressing TAB or by waiting a while during typing an address. The\r
+completion is based entirely on the asynchronous address harvesting\r
+from notmuch-address.el so the GUI is theoretically not blocked for\r
+long time.\r
+\r
+The completion works similarly as the TAB-initiated completion from\r
+notmuch-address.el, i.e. quick harvest based on user input is executed\r
+first and only after full harvesting is finished, in-memory cached data\r
+is used.\r
+\r
+[Improved by David Bremner]\r
+---\r
+ emacs/Makefile.local | 1 +\r
+ emacs/notmuch-address.el | 18 ++++++++--\r
+ emacs/notmuch-company.el | 86 ++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 3 files changed, 103 insertions(+), 2 deletions(-)\r
+ create mode 100644 emacs/notmuch-company.el\r
+\r
+diff --git a/emacs/Makefile.local b/emacs/Makefile.local\r
+index 1109cfa..4c06c52 100644\r
+--- a/emacs/Makefile.local\r
++++ b/emacs/Makefile.local\r
+@@ -20,6 +20,7 @@ emacs_sources := \\r
+ $(dir)/notmuch-print.el \\r
+ $(dir)/notmuch-version.el \\r
+ $(dir)/notmuch-jump.el \\r
++ $(dir)/notmuch-company.el\r
+ \r
+ $(dir)/notmuch-version.el: $(dir)/Makefile.local version.stamp\r
+ $(dir)/notmuch-version.el: $(srcdir)/$(dir)/notmuch-version.el.tmpl\r
+diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el\r
+index 498ef8a..49e2402 100644\r
+--- a/emacs/notmuch-address.el\r
++++ b/emacs/notmuch-address.el\r
+@@ -22,7 +22,9 @@\r
+ (require 'message)\r
+ (require 'notmuch-parser)\r
+ (require 'notmuch-lib)\r
++(require 'notmuch-company)\r
+ ;;\r
++(declare-function company-manual-begin "company")\r
+ \r
+ (defcustom notmuch-address-command 'internal\r
+ "The command which generates possible addresses. It must take a\r
+@@ -72,9 +74,21 @@ finished")\r
+ (defun notmuch-address-message-insinuate ()\r
+ (message "calling notmuch-address-message-insinuate is no longer needed"))\r
+ \r
++(defcustom notmuch-address-use-company t\r
++ "If available, use company mode for address completion"\r
++ :type 'boolean\r
++ :group 'notmuch-send)\r
++\r
+ (defun notmuch-address-setup ()\r
+- (let ((pair (cons notmuch-address-completion-headers-regexp\r
+- #'notmuch-address-expand-name)))\r
++ (let* ((use-company (and notmuch-address-use-company\r
++ (eq notmuch-address-command 'internal)\r
++ (require 'company nil t)))\r
++ (pair (cons notmuch-address-completion-headers-regexp\r
++ (if use-company\r
++ #'company-manual-begin\r
++ #'notmuch-address-expand-name))))\r
++ (when use-company\r
++ (notmuch-company-setup))\r
+ (unless (memq pair message-completion-alist)\r
+ (setq message-completion-alist\r
+ (push pair message-completion-alist)))))\r
+diff --git a/emacs/notmuch-company.el b/emacs/notmuch-company.el\r
+new file mode 100644\r
+index 0000000..add3161\r
+--- /dev/null\r
++++ b/emacs/notmuch-company.el\r
+@@ -0,0 +1,86 @@\r
++;; notmuch-company.el --- Mail address completion for notmuch via company-mode -*- lexical-binding: t -*-\r
++\r
++;; Authors: Trevor Jim <tjim@mac.com>\r
++;; Michal Sojka <sojkam1@fel.cvut.cz>\r
++;;\r
++;; Keywords: mail, completion\r
++\r
++;; This program is free software; you can redistribute it and/or modify\r
++;; it under the terms of the GNU General Public License as published by\r
++;; the Free Software Foundation, either version 3 of the License, or\r
++;; (at your option) any later version.\r
++\r
++;; This program is distributed in the hope that it will be useful,\r
++;; but WITHOUT ANY WARRANTY; without even the implied warranty of\r
++;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
++;; GNU General Public License for more details.\r
++\r
++;; You should have received a copy of the GNU General Public License\r
++;; along with this program. If not, see <http://www.gnu.org/licenses/>.\r
++\r
++;;; Commentary:\r
++\r
++;; To enable this, install company mode (https://company-mode.github.io/)\r
++;;\r
++;; NB company-minimum-prefix-length defaults to 3 so you don't get\r
++;; completion unless you type 3 characters\r
++\r
++;;; Code:\r
++\r
++(eval-when-compile (require 'cl))\r
++\r
++(defvar notmuch-company-last-prefix nil)\r
++(make-variable-buffer-local 'notmuch-company-last-prefix)\r
++(declare-function company-begin-backend "company")\r
++(declare-function company-grab "company")\r
++(declare-function company-mode "company")\r
++(declare-function company-manual-begin "company")\r
++(defvar company-backends)\r
++\r
++(declare-function notmuch-address-harvest "notmuch-address")\r
++(declare-function notmuch-address-harvest-trigger "notmuch-address")\r
++(declare-function notmuch-address-matching "notmuch-address")\r
++(defvar notmuch-address-full-harvest-finished)\r
++(defvar notmuch-address-completion-headers-regexp)\r
++\r
++;;;###autoload\r
++(defun notmuch-company-setup ()\r
++ (company-mode)\r
++ (make-local-variable 'company-backends)\r
++ (setq company-backends '(notmuch-company)))\r
++\r
++;;;###autoload\r
++(defun notmuch-company (command &optional arg &rest _ignore)\r
++ "`company-mode' completion back-end for `notmuch'."\r
++ (interactive (list 'interactive))\r
++ (require 'company)\r
++ (let ((case-fold-search t)\r
++ (completion-ignore-case t))\r
++ (case command\r
++ (interactive (company-begin-backend 'notmuch-company))\r
++ (prefix (and (derived-mode-p 'message-mode)\r
++ (looking-back (concat notmuch-address-completion-headers-regexp ".*")\r
++ (line-beginning-position))\r
++ (setq notmuch-company-last-prefix (company-grab "[:,][ \t]*\\(.*\\)" 1 (point-at-bol)))))\r
++ (candidates (cond\r
++ (notmuch-address-full-harvest-finished\r
++ ;; Update harvested addressed from time to time\r
++ (notmuch-address-harvest-trigger)\r
++ (notmuch-address-matching arg))\r
++ (t\r
++ (cons :async\r
++ (lambda (callback)\r
++ ;; First run quick asynchronous harvest based on what the user entered so far\r
++ (notmuch-address-harvest\r
++ (format "to:%s*" arg) nil\r
++ (lambda (_proc _event)\r
++ (funcall callback (notmuch-address-matching arg))\r
++ ;; Then start the (potentially long-running) full asynchronous harvest if necessary\r
++ (notmuch-address-harvest-trigger))))))))\r
++ (match (if (string-match notmuch-company-last-prefix arg)\r
++ (match-end 0)\r
++ 0))\r
++ (no-cache t))))\r
++\r
++\r
++(provide 'notmuch-company)\r
+-- \r
+2.5.3\r
+\r