--- /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 olra.theworths.org (Postfix) with ESMTP id 962CA431FB6\r
+ for <notmuch@notmuchmail.org>; Tue, 29 Jul 2014 09:58:27 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id fVGPxr6qEdKe for <notmuch@notmuchmail.org>;\r
+ Tue, 29 Jul 2014 09:58:19 -0700 (PDT)\r
+Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
+ by olra.theworths.org (Postfix) with ESMTP id 4231A431FAE\r
+ for <notmuch@notmuchmail.org>; Tue, 29 Jul 2014 09:58:19 -0700 (PDT)\r
+Received: from localhost (unknown [192.168.200.7])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id 99CCD3CFEBF;\r
+ Tue, 29 Jul 2014 18:58:18 +0200 (CEST)\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,\r
+ port 10044)\r
+ with ESMTP id CVB3oBt-YdlU; Tue, 29 Jul 2014 18:58:14 +0200 (CEST)\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 D1F9A3CFE9F;\r
+ Tue, 29 Jul 2014 18:58:14 +0200 (CEST)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.82_1-5b7a7c0-XX)\r
+ (envelope-from <sojkam1@fel.cvut.cz>)\r
+ id 1XCAj8-0003R5-LU; Tue, 29 Jul 2014 18:58:14 +0200\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH RFC] Emacs: Add address completion mechanism implemented in\r
+ elisp\r
+Date: Tue, 29 Jul 2014 18:57:50 +0200\r
+Message-Id: <1406653070-13174-1-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.0.1\r
+In-Reply-To: <87bns8f6hi.fsf@steelpick.2x.cz>\r
+References: <87bns8f6hi.fsf@steelpick.2x.cz>\r
+Cc: tjim@mac.com\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://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: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Tue, 29 Jul 2014 16:58:27 -0000\r
+\r
+Notmuch currently has an address completion mechanism that requires\r
+external script to provide completion candidates. This patch adds a\r
+completion mechanism found in https://github.com/tjim/nevermore, which\r
+is implemented in elisp only.\r
+\r
+notmuch-lib.el is extended with function notmuch-async-harvest that\r
+collects the completion candidates from notmuch database and stores\r
+them in notmuch-completion-addresses.\r
+\r
+notmuch-company.el hooks itself into message-mode and uses\r
+company-mode to offer the completion to the user. The file is put into\r
+the contrib directory which means that the use has to install it\r
+himself. This is because company-mode is not a part of Emacs and\r
+bytecompiling notmuch-company.el fails due to used --quick option that\r
+causes user installed packages to be ignored. Moreover, Debian\r
+bytecompiles elisp files during installation which would require\r
+having company-mode packaged for Debian. This would be possible but\r
+company-mode requires emacs24 which would complicate notmuch Debian\r
+maintainer scripts.\r
+\r
+It would probably make sense to implement another completion frontend\r
+based only on Emacs built-in functionality and integrate it with\r
+notmuch-addresses.el.\r
+\r
+The original nevermore code was modified in the following ways:\r
+1) Prefix was changes from nm- to notmuch-.\r
+2) A few docstrings and comments were added.\r
+3) notmuch-flatten-* functions were renamed to match match\r
+ devel/schemata.\r
+---\r
+ debian/notmuch-emacs.examples | 1 +\r
+ emacs/contrib/notmuch-company.el | 62 ++++++++++++++++++++++++++++\r
+ emacs/notmuch-lib.el | 87 ++++++++++++++++++++++++++++++++++++++++\r
+ 3 files changed, 150 insertions(+)\r
+ create mode 100644 debian/notmuch-emacs.examples\r
+ create mode 100644 emacs/contrib/notmuch-company.el\r
+\r
+diff --git a/debian/notmuch-emacs.examples b/debian/notmuch-emacs.examples\r
+new file mode 100644\r
+index 0000000..4a42a47\r
+--- /dev/null\r
++++ b/debian/notmuch-emacs.examples\r
+@@ -0,0 +1 @@\r
++emacs/contrib/notmuch-company.el\r
+diff --git a/emacs/contrib/notmuch-company.el b/emacs/contrib/notmuch-company.el\r
+new file mode 100644\r
+index 0000000..228de94\r
+--- /dev/null\r
++++ b/emacs/contrib/notmuch-company.el\r
+@@ -0,0 +1,62 @@\r
++;; notmuch-company.el --- Mail address completion for notmuch via company-mode\r
++\r
++;; Author: Trevor Jim <tjim@mac.com>\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
++;; and add\r
++;;\r
++;; (require 'notmuch-company)\r
++;;\r
++;; to your .emacs.\r
++;;\r
++;; NB company-minimum-prefix-length defaults to 3 so you don't get\r
++;; completion unless you type 3 characters\r
++\r
++\r
++;;; Code:\r
++\r
++(require 'company)\r
++(require 'message)\r
++(require 'notmuch-lib)\r
++\r
++(defvar-local notmuch-company-last-prefix nil)\r
++;;;###autoload\r
++(defun notmuch-company (command &optional arg &rest ignore)\r
++ "`company-mode' completion back-end for `nevermore (nm)'."\r
++ (interactive (list 'interactive))\r
++ (let ((case-fold-search t))\r
++ (pcase command\r
++ (`interactive (company-begin-backend 'notmuch-company))\r
++ (`prefix (and (eq major-mode 'message-mode)\r
++ (looking-back "^\\(To\\|Cc\\|Bcc\\):.*"\r
++ (line-beginning-position))\r
++ (setq notmuch-company-last-prefix (company-grab-symbol))))\r
++ (`candidates (let ((results (completion-substring--all-completions arg notmuch-completion-addresses nil 0)))\r
++ (when results (car results))))\r
++ (`match (if (string-match notmuch-company-last-prefix arg)\r
++ (match-end 0)\r
++ 0))\r
++ (`no-cache t))))\r
++\r
++(add-hook 'message-mode-hook '(lambda ()\r
++ (company-mode)\r
++ (make-local-variable 'company-backends)\r
++ (setq company-backends '(notmuch-company))\r
++ (when (not notmuch-completion-addresses) (notmuch-async-harvest))))\r
++(provide 'notmuch-company)\r
+diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
+index 2941da3..c0f4ba0 100644\r
+--- a/emacs/notmuch-lib.el\r
++++ b/emacs/notmuch-lib.el\r
+@@ -216,6 +216,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
+@@ -845,6 +848,90 @@ status."\r
+ (defvar notmuch-show-process-crypto nil)\r
+ (make-variable-buffer-local 'notmuch-show-process-crypto)\r
+ \r
++(defun notmuch-flatten-thread-set (thread-set)\r
++ "Convert the result of 'notmuch show' to the plain list of messages."\r
++;; (display-message-or-buffer (format "Before: %S" thread-set))\r
++ (let ((result\r
++ (apply 'append\r
++ (mapcar 'notmuch-flatten-thread thread-set))))\r
++;; (display-message-or-buffer (format "After: %S" result))\r
++ result))\r
++\r
++(defun notmuch-flatten-thread (thread)\r
++ (apply 'append\r
++ (mapcar 'notmuch-flatten-thread-node thread)))\r
++\r
++(defun notmuch-flatten-thread-node (thread-node)\r
++ (let ((msg (car thread-node))\r
++ (replies (cadr thread-node)))\r
++ (if msg\r
++ (cons msg (notmuch-flatten-thread replies))\r
++ (notmuch-flatten-thread replies))))\r
++\r
++;;; async address harvesting\r
++(defvar notmuch-completion-addresses nil\r
++ "Hash of email addresses for completion during email composition.\r
++ This variable is set by `notmuch-async-harvest'.")\r
++\r
++(defvar notmuch-async-harvest-pending-proc nil) ; the process of a harvest underway\r
++(defvar notmuch-async-harvest-pending-output nil) ; holds the not-yet-processed part of the output of the harvest process\r
++(defun notmuch-async-harvest ()\r
++ "Collect possible addresses for completion. It queries the\r
++notmuch database for all emails sent by the user and collects the\r
++destination addresses from them in\r
++`notmuch-completion-addresses'. This takes some time so the\r
++address collection runs asynchronously."\r
++ (when notmuch-async-harvest-pending-proc\r
++ (ignore-errors (kill-process notmuch-async-harvest-pending-proc))\r
++ ; kill-process sends signal, actual process death is asynchronous, so indicate that we want the process dead\r
++ (setq notmuch-async-harvest-pending-proc nil))\r
++ (setq notmuch-completion-addresses (make-hash-table :test 'equal))\r
++ (setq notmuch-async-harvest-pending-output nil) ; indicate that we have not gotten any output yet\r
++ (setq notmuch-async-harvest-pending-proc\r
++ (notmuch-start-notmuch\r
++ "notmuch-async-harvest" ; process name\r
++ nil ; process buffer\r
++ nil ; process sentinel\r
++ "show" ; notmuch command\r
++ "--format=sexp"\r
++ "--format-version=2"\r
++ "--body=false"\r
++ "--entire-thread=false"\r
++ (mapconcat (lambda (x) (concat "from:" x)) (notmuch-user-emails) " or ")\r
++ ))\r
++ (set-process-filter\r
++ notmuch-async-harvest-pending-proc\r
++ (lambda (proc string)\r
++ (when (and notmuch-async-harvest-pending-proc (equal (process-id proc) (process-id notmuch-async-harvest-pending-proc)))\r
++ (if notmuch-async-harvest-pending-output\r
++ ; This is not the first time we have seen output, add it to anything remaining from last time\r
++ (setq notmuch-async-harvest-pending-output (concat notmuch-async-harvest-pending-output string))\r
++ ; This is the first time we have seen output. Skip the initial open paren\r
++ (setq notmuch-async-harvest-pending-output (substring string 1)))\r
++ (while\r
++ (let ((result (ignore-errors (read-from-string notmuch-async-harvest-pending-output))))\r
++ (and result\r
++ (let ((obj (car result))\r
++ (offset (cdr result)))\r
++ (setq notmuch-async-harvest-pending-output (substring notmuch-async-harvest-pending-output offset))\r
++ (let ((msgs (notmuch-flatten-thread-set (list obj))))\r
++ (mapc\r
++ (lambda (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-completion-addresses)))\r
++ (append to cc bcc))))\r
++ msgs)\r
++ t))))))))\r
++ ; return value\r
++ nil)\r
++\r
+ (provide 'notmuch-lib)\r
+ \r
+ ;; Local Variables:\r
+-- \r
+2.0.1\r
+\r