Re: [PATCH v3] Emacs: Add address completion mechanism implemented in elisp
authorMark Walters <markwalters1009@gmail.com>
Mon, 8 Sep 2014 09:10:11 +0000 (10:10 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 18:04:30 +0000 (10:04 -0800)
90/d9ffef0f3b31235a64d56c6bf9cd6e22d582e6 [new file with mode: 0644]

diff --git a/90/d9ffef0f3b31235a64d56c6bf9cd6e22d582e6 b/90/d9ffef0f3b31235a64d56c6bf9cd6e22d582e6
new file mode 100644 (file)
index 0000000..13e028d
--- /dev/null
@@ -0,0 +1,412 @@
+Return-Path: <m.walters@qmul.ac.uk>\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 10EA0431FBC\r
+       for <notmuch@notmuchmail.org>; Mon,  8 Sep 2014 02:10:45 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.502\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.502 tagged_above=-999 required=5\r
+       tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
+       NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_LOW=-0.7] 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 YY6fcwDUPKOo for <notmuch@notmuchmail.org>;\r
+       Mon,  8 Sep 2014 02:10:37 -0700 (PDT)\r
+Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
+       (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 40441431FB6\r
+       for <notmuch@notmuchmail.org>; Mon,  8 Sep 2014 02:10:37 -0700 (PDT)\r
+Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
+       by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1XQuxh-0003eG-3E; Mon, 08 Sep 2014 10:10:33 +0100\r
+Received: from 5751dfa2.skybroadband.com ([87.81.223.162] helo=localhost)\r
+       by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.71)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1XQuxf-00012g-Sw; Mon, 08 Sep 2014 10:10:12 +0100\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: David Edmondson <dme@dme.org>, Michal Sojka <sojkam1@fel.cvut.cz>,\r
+       notmuch@notmuchmail.org\r
+Subject: Re: [PATCH v3] Emacs: Add address completion mechanism\r
+       implemented     in      elisp\r
+In-Reply-To: <m2lhpuzlbv.fsf@fenchurch.hh.sledj.net>\r
+References: <1407771091-12651-1-git-send-email-sojkam1@fel.cvut.cz>\r
+       <m2lhpuzlbv.fsf@fenchurch.hh.sledj.net>\r
+User-Agent: Notmuch/0.18.1+86~gef5e66a (http://notmuchmail.org) Emacs/24.3.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Mon, 08 Sep 2014 10:10:11 +0100\r
+Message-ID: <87d2b6mqa4.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain\r
+X-Sender-Host-Address: 87.81.223.162\r
+X-QM-Geographic: According to ripencc,\r
+       this message was delivered by a machine in Britain (UK) (GB).\r
+X-QM-SPAM-Info: Sender has good ham record.  :)\r
+X-QM-Body-MD5: 9e5e999702657ed921aff8e7b0d90a5c (of first 20000 bytes)\r
+X-SpamAssassin-Score: -0.1\r
+X-SpamAssassin-SpamBar: /\r
+X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
+       determine if it is\r
+       spam. We require at least 5.0 points to mark a message as spam.\r
+       This message scored -0.1 points.\r
+       Summary of the scoring: \r
+       * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+       provider *      (markwalters1009[at]gmail.com)\r
+       *  0.0 T_FILL_THIS_FORM_SHORT Fill in a short form with personal\r
+       *      information\r
+       * -0.1 AWL AWL: From: address is in the auto white-list\r
+X-QM-Scan-Virus: ClamAV says the message is clean\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: Mon, 08 Sep 2014 09:10:45 -0000\r
+\r
+On Mon, 08 Sep 2014, David Edmondson <dme@dme.org> wrote:\r
+> On Mon, Aug 11 2014, Michal Sojka wrote:\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 core of the new mechanism is the function notmuch-address-harvest\r
+>> that collects the completion candidates from the notmuch database and\r
+>> stores them in notmuch-address-completions variable.\r
+>> notmuch-address-harvest is called on the first entry to message-mode\r
+>> and runs asychnornously so that the user doesn't have to wait for it\r
+>> to complete while composing the message. The\r
+>> notmuch-address-completions variable is used in message-mode as a\r
+>> source of completion candidates. Currently, there are two ways how the\r
+>> notmuch-address-completions variable is used.\r
+>>\r
+>> First, preexisting address completion mechanism is extended to use\r
+>> notmuch-address-completions in addition to the external command. This\r
+>> new behavior is configured by setting notmuch-address-command to nil,\r
+>> which is the new default. Note that this may *BREAK EXISTING SETUPS*\r
+>> when the user used external command named "notmuch-addresses", i.e.\r
+>> the previous default. The result will be that the user will use the\r
+>> new mechanism instead of the his command. I believe that many users\r
+>> may not even recognize this because the new mechanism works the same\r
+>> as http://commonmeasure.org/~jkr/git/notmuch_addresses.git and perhaps\r
+>> also as other commands suggested at\r
+>> http://notmuchmail.org/emacstips/#address_completion.\r
+>>\r
+>> Second way of using notmuch-address-completions is notmuch-company.el.\r
+>> This presents the possible completions in a nice popup box after a\r
+>> short typing delay but requires company-mode to be installed.\r
+>\r
+> This looks great, thanks for doing it. It seems like a better approach\r
+> than id:1409921969-65129-1-git-send-email-dme@dme.org. Some comments:\r
+>\r
+> - Adding the address collection to `message-mode-hook' means that it\r
+>   runs every time I start to compose a message. If the address\r
+>   collection is disk intensive, this might be bad for battery life. The\r
+>   set of potential recipients doesn't change _that_ much over time for a\r
+>   typical person, I'd wager. Maybe the hook should only run once a day?\r
+>   (Tunable, of course.)\r
+\r
+Just a meta comment really: Austin is very close to posting a patch\r
+adding a timestamp to messages in the database (I think roughly last\r
+modify time). Once that is in an address-harvest query of the form all\r
+messages with any changes since the last harvest should be simple and\r
+fast.\r
+\r
+A second more general comment: if something like Jani's patch\r
+id:1410021689-15901-1-git-send-email-jani@nikula.org goes in the\r
+harvesting from addresses is hugely faster (circa 20 times, see\r
+id:8761h0fxow.fsf@qmul.ac.uk ) than harvesting to,cc addresses so some\r
+people might want to tweak/customize that.\r
+\r
+Best wishes\r
+\r
+Mark\r
+\r
+>\r
+> - The addition of company mode support (which I haven't tried) should be\r
+>   a separate patch in the series.\r
+>\r
+>> ---\r
+>> Changes from v1:\r
+>> - Use of notmuch-parser.el instead of the custom parser in the\r
+>>   original code. The notmuch parser is slightly faster.\r
+>> - Use of functions in notmuch-query.el instead of functions in the\r
+>>   original code with almost the same functionality.\r
+>> - Integrated with existing completion mechanism in notmuch.\r
+>> - notmuch-company.el was moved from emacs/contrib to emacs and\r
+>>   no-byte-compile directive was added to it.\r
+>> - Aligned with notmuch naming conventions.\r
+>> - Documented bugs found in notmuch-company.el\r
+>>\r
+>> Changes from v2:\r
+>> - Updated Makefile.local to not conflict with current master\r
+>> ---\r
+>>  emacs/Makefile.local     |  6 ++-\r
+>>  emacs/notmuch-address.el | 95 +++++++++++++++++++++++++++++++++++++++++++-----\r
+>>  emacs/notmuch-company.el | 69 +++++++++++++++++++++++++++++++++++\r
+>>  emacs/notmuch-lib.el     |  3 ++\r
+>>  4 files changed, 163 insertions(+), 10 deletions(-)\r
+>>  create mode 100644 emacs/notmuch-company.el\r
+>>\r
+>> diff --git a/emacs/Makefile.local b/emacs/Makefile.local\r
+>> index 1109cfa..6c93e73 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
+>> @@ -30,7 +31,10 @@ $(dir)/notmuch-version.el: $(srcdir)/$(dir)/notmuch-version.el.tmpl\r
+>>  emacs_images := \\r
+>>     $(srcdir)/$(dir)/notmuch-logo.png\r
+>>  \r
+>> -emacs_bytecode = $(emacs_sources:.el=.elc)\r
+>> +# Do not try to install files that are not byte-compiled.\r
+>> +emacs_no_byte_compile := $(dir)/notmuch-company.el\r
+>> +\r
+>> +emacs_bytecode = $(patsubst %.el,%.elc,$(filter-out $(emacs_no_byte_compile),$(emacs_sources)))\r
+>>  \r
+>>  # Because of defmacro's and defsubst's, we have to account for load\r
+>>  # dependencies between Elisp files when byte compiling.  Otherwise,\r
+>> diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el\r
+>> index fa65cd5..a50f4f4 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,10 @@ to know how address selection is made by default."\r
+>>    :group 'notmuch-send\r
+>>    :group 'notmuch-external)\r
+>>  \r
+>> +(defvar notmuch-address-completions nil\r
+>> +  "Hash of email addresses for completion during email composition.\r
+>> +  This variable is set by calling `notmuch-address-harvest'.")\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
+>> @@ -60,7 +68,16 @@ to know how address selection is made by default."\r
+>>       (push notmuch-address-message-alist-member message-completion-alist))))\r
+>>  \r
+>>  (defun notmuch-address-options (original)\r
+>> -  (process-lines notmuch-address-command original))\r
+>> +  (cond\r
+>> +   ((eq notmuch-address-command nil)\r
+>> +    (let ((candidates))\r
+>> +      (maphash (lambda (key val)\r
+>> +            (let ((re (concat "\\<" (regexp-quote original))))\r
+>> +              (when (string-match re key)\r
+>> +                (push key candidates))))\r
+>> +          notmuch-address-completions)\r
+>> +      candidates))\r
+>> +   (t (process-lines notmuch-address-command original))))\r
+>>  \r
+>>  (defun notmuch-address-expand-name ()\r
+>>    (let* ((end (point))\r
+>> @@ -108,11 +125,71 @@ to know how address selection is made by default."\r
+>>                        (not (file-directory-p bin))))\r
+>>           (throw 'found-command bin))))))))\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
+>> +(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 ()\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-address-completions'. This takes some time so the\r
+>> +address collection runs asynchronously."\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 nil))\r
+>> +  (setq notmuch-address-completions (make-hash-table :test 'equal))\r
+>> +  (setq notmuch-address-harvest-proc\r
+>> +        (notmuch-start-notmuch\r
+>> +         "notmuch-address-harvest" ; process name\r
+>> +         " *notmuch-address-harvest*"      ; 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
+>> +  (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
+>> +;; or if it is nil, insinuate ourselves into `message-mode'.\r
+>> +(when (or (eq notmuch-address-command nil)\r
+>> +     (notmuch-address-locate-command notmuch-address-command))\r
+>>    (notmuch-address-message-insinuate))\r
+>>  \r
+>> +(defun notmuch-address-harvest-start ()\r
+>> +  (when (not notmuch-address-completions) (notmuch-address-harvest)))\r
+>> +\r
+>> +(when (eq notmuch-address-command nil)\r
+>> +  (add-hook 'message-mode-hook 'notmuch-address-harvest-start))\r
+>> +\r
+>>  ;;\r
+>>  \r
+>>  (provide 'notmuch-address)\r
+>> diff --git a/emacs/notmuch-company.el b/emacs/notmuch-company.el\r
+>> new file mode 100644\r
+>> index 0000000..748fcee\r
+>> --- /dev/null\r
+>> +++ b/emacs/notmuch-company.el\r
+>> @@ -0,0 +1,69 @@\r
+>> +;; -*-no-byte-compile: t; -*-\r
+>> +\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
+>> +;;; Bugs:\r
+>> +;;\r
+>> +;; - matching is case sensitive\r
+>> +;; - matching of non-ascii characters doesn't work well\r
+>> +\r
+>> +;;; Code:\r
+>> +\r
+>> +(require 'company)\r
+>> +(require 'message)\r
+>> +(require 'notmuch-address)\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 `notmuch'."\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-address-completions 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
+>> +                           (notmuch-address-harvest-start)))\r
+>> +\r
+>> +(provide 'notmuch-company)\r
+>> diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
+>> index 19269e3..00e8554 100644\r
+>> --- a/emacs/notmuch-lib.el\r
+>> +++ b/emacs/notmuch-lib.el\r
+>> @@ -228,6 +228,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.0.1\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r