Re: [PATCH v3] Emacs: Add address completion mechanism implemented in elisp
[notmuch-archives.git] / 90 / d9ffef0f3b31235a64d56c6bf9cd6e22d582e6
1 Return-Path: <m.walters@qmul.ac.uk>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 10EA0431FBC\r
6         for <notmuch@notmuchmail.org>; Mon,  8 Sep 2014 02:10:45 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0.502\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0.502 tagged_above=-999 required=5\r
12         tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
13         NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id YY6fcwDUPKOo for <notmuch@notmuchmail.org>;\r
17         Mon,  8 Sep 2014 02:10:37 -0700 (PDT)\r
18 Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
19         (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 40441431FB6\r
22         for <notmuch@notmuchmail.org>; Mon,  8 Sep 2014 02:10:37 -0700 (PDT)\r
23 Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
24         by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
25         (envelope-from <m.walters@qmul.ac.uk>)\r
26         id 1XQuxh-0003eG-3E; Mon, 08 Sep 2014 10:10:33 +0100\r
27 Received: from 5751dfa2.skybroadband.com ([87.81.223.162] helo=localhost)\r
28         by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.71)\r
29         (envelope-from <m.walters@qmul.ac.uk>)\r
30         id 1XQuxf-00012g-Sw; Mon, 08 Sep 2014 10:10:12 +0100\r
31 From: Mark Walters <markwalters1009@gmail.com>\r
32 To: David Edmondson <dme@dme.org>, Michal Sojka <sojkam1@fel.cvut.cz>,\r
33         notmuch@notmuchmail.org\r
34 Subject: Re: [PATCH v3] Emacs: Add address completion mechanism\r
35         implemented     in      elisp\r
36 In-Reply-To: <m2lhpuzlbv.fsf@fenchurch.hh.sledj.net>\r
37 References: <1407771091-12651-1-git-send-email-sojkam1@fel.cvut.cz>\r
38         <m2lhpuzlbv.fsf@fenchurch.hh.sledj.net>\r
39 User-Agent: Notmuch/0.18.1+86~gef5e66a (http://notmuchmail.org) Emacs/24.3.1\r
40         (x86_64-pc-linux-gnu)\r
41 Date: Mon, 08 Sep 2014 10:10:11 +0100\r
42 Message-ID: <87d2b6mqa4.fsf@qmul.ac.uk>\r
43 MIME-Version: 1.0\r
44 Content-Type: text/plain\r
45 X-Sender-Host-Address: 87.81.223.162\r
46 X-QM-Geographic: According to ripencc,\r
47         this message was delivered by a machine in Britain (UK) (GB).\r
48 X-QM-SPAM-Info: Sender has good ham record.  :)\r
49 X-QM-Body-MD5: 9e5e999702657ed921aff8e7b0d90a5c (of first 20000 bytes)\r
50 X-SpamAssassin-Score: -0.1\r
51 X-SpamAssassin-SpamBar: /\r
52 X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
53         determine if it is\r
54         spam. We require at least 5.0 points to mark a message as spam.\r
55         This message scored -0.1 points.\r
56         Summary of the scoring: \r
57         * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
58         provider *      (markwalters1009[at]gmail.com)\r
59         *  0.0 T_FILL_THIS_FORM_SHORT Fill in a short form with personal\r
60         *      information\r
61         * -0.1 AWL AWL: From: address is in the auto white-list\r
62 X-QM-Scan-Virus: ClamAV says the message is clean\r
63 X-BeenThere: notmuch@notmuchmail.org\r
64 X-Mailman-Version: 2.1.13\r
65 Precedence: list\r
66 List-Id: "Use and development of the notmuch mail system."\r
67         <notmuch.notmuchmail.org>\r
68 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
69         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
70 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
71 List-Post: <mailto:notmuch@notmuchmail.org>\r
72 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
73 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
74         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
75 X-List-Received-Date: Mon, 08 Sep 2014 09:10:45 -0000\r
76 \r
77 On Mon, 08 Sep 2014, David Edmondson <dme@dme.org> wrote:\r
78 > On Mon, Aug 11 2014, Michal Sojka wrote:\r
79 >> Currently, notmuch has an address completion mechanism that requires\r
80 >> external command to provide completion candidates. This patch adds a\r
81 >> completion mechanism inspired by https://github.com/tjim/nevermore,\r
82 >> which is implemented in Emacs lisp only.\r
83 >>\r
84 >> The core of the new mechanism is the function notmuch-address-harvest\r
85 >> that collects the completion candidates from the notmuch database and\r
86 >> stores them in notmuch-address-completions variable.\r
87 >> notmuch-address-harvest is called on the first entry to message-mode\r
88 >> and runs asychnornously so that the user doesn't have to wait for it\r
89 >> to complete while composing the message. The\r
90 >> notmuch-address-completions variable is used in message-mode as a\r
91 >> source of completion candidates. Currently, there are two ways how the\r
92 >> notmuch-address-completions variable is used.\r
93 >>\r
94 >> First, preexisting address completion mechanism is extended to use\r
95 >> notmuch-address-completions in addition to the external command. This\r
96 >> new behavior is configured by setting notmuch-address-command to nil,\r
97 >> which is the new default. Note that this may *BREAK EXISTING SETUPS*\r
98 >> when the user used external command named "notmuch-addresses", i.e.\r
99 >> the previous default. The result will be that the user will use the\r
100 >> new mechanism instead of the his command. I believe that many users\r
101 >> may not even recognize this because the new mechanism works the same\r
102 >> as http://commonmeasure.org/~jkr/git/notmuch_addresses.git and perhaps\r
103 >> also as other commands suggested at\r
104 >> http://notmuchmail.org/emacstips/#address_completion.\r
105 >>\r
106 >> Second way of using notmuch-address-completions is notmuch-company.el.\r
107 >> This presents the possible completions in a nice popup box after a\r
108 >> short typing delay but requires company-mode to be installed.\r
109 >\r
110 > This looks great, thanks for doing it. It seems like a better approach\r
111 > than id:1409921969-65129-1-git-send-email-dme@dme.org. Some comments:\r
112 >\r
113 > - Adding the address collection to `message-mode-hook' means that it\r
114 >   runs every time I start to compose a message. If the address\r
115 >   collection is disk intensive, this might be bad for battery life. The\r
116 >   set of potential recipients doesn't change _that_ much over time for a\r
117 >   typical person, I'd wager. Maybe the hook should only run once a day?\r
118 >   (Tunable, of course.)\r
119 \r
120 Just a meta comment really: Austin is very close to posting a patch\r
121 adding a timestamp to messages in the database (I think roughly last\r
122 modify time). Once that is in an address-harvest query of the form all\r
123 messages with any changes since the last harvest should be simple and\r
124 fast.\r
125 \r
126 A second more general comment: if something like Jani's patch\r
127 id:1410021689-15901-1-git-send-email-jani@nikula.org goes in the\r
128 harvesting from addresses is hugely faster (circa 20 times, see\r
129 id:8761h0fxow.fsf@qmul.ac.uk ) than harvesting to,cc addresses so some\r
130 people might want to tweak/customize that.\r
131 \r
132 Best wishes\r
133 \r
134 Mark\r
135 \r
136 >\r
137 > - The addition of company mode support (which I haven't tried) should be\r
138 >   a separate patch in the series.\r
139 >\r
140 >> ---\r
141 >> Changes from v1:\r
142 >> - Use of notmuch-parser.el instead of the custom parser in the\r
143 >>   original code. The notmuch parser is slightly faster.\r
144 >> - Use of functions in notmuch-query.el instead of functions in the\r
145 >>   original code with almost the same functionality.\r
146 >> - Integrated with existing completion mechanism in notmuch.\r
147 >> - notmuch-company.el was moved from emacs/contrib to emacs and\r
148 >>   no-byte-compile directive was added to it.\r
149 >> - Aligned with notmuch naming conventions.\r
150 >> - Documented bugs found in notmuch-company.el\r
151 >>\r
152 >> Changes from v2:\r
153 >> - Updated Makefile.local to not conflict with current master\r
154 >> ---\r
155 >>  emacs/Makefile.local     |  6 ++-\r
156 >>  emacs/notmuch-address.el | 95 +++++++++++++++++++++++++++++++++++++++++++-----\r
157 >>  emacs/notmuch-company.el | 69 +++++++++++++++++++++++++++++++++++\r
158 >>  emacs/notmuch-lib.el     |  3 ++\r
159 >>  4 files changed, 163 insertions(+), 10 deletions(-)\r
160 >>  create mode 100644 emacs/notmuch-company.el\r
161 >>\r
162 >> diff --git a/emacs/Makefile.local b/emacs/Makefile.local\r
163 >> index 1109cfa..6c93e73 100644\r
164 >> --- a/emacs/Makefile.local\r
165 >> +++ b/emacs/Makefile.local\r
166 >> @@ -20,6 +20,7 @@ emacs_sources := \\r
167 >>      $(dir)/notmuch-print.el \\r
168 >>      $(dir)/notmuch-version.el \\r
169 >>      $(dir)/notmuch-jump.el \\r
170 >> +    $(dir)/notmuch-company.el\r
171 >>  \r
172 >>  $(dir)/notmuch-version.el: $(dir)/Makefile.local version.stamp\r
173 >>  $(dir)/notmuch-version.el: $(srcdir)/$(dir)/notmuch-version.el.tmpl\r
174 >> @@ -30,7 +31,10 @@ $(dir)/notmuch-version.el: $(srcdir)/$(dir)/notmuch-version.el.tmpl\r
175 >>  emacs_images := \\r
176 >>      $(srcdir)/$(dir)/notmuch-logo.png\r
177 >>  \r
178 >> -emacs_bytecode = $(emacs_sources:.el=.elc)\r
179 >> +# Do not try to install files that are not byte-compiled.\r
180 >> +emacs_no_byte_compile := $(dir)/notmuch-company.el\r
181 >> +\r
182 >> +emacs_bytecode = $(patsubst %.el,%.elc,$(filter-out $(emacs_no_byte_compile),$(emacs_sources)))\r
183 >>  \r
184 >>  # Because of defmacro's and defsubst's, we have to account for load\r
185 >>  # dependencies between Elisp files when byte compiling.  Otherwise,\r
186 >> diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el\r
187 >> index fa65cd5..a50f4f4 100644\r
188 >> --- a/emacs/notmuch-address.el\r
189 >> +++ b/emacs/notmuch-address.el\r
190 >> @@ -20,14 +20,18 @@\r
191 >>  ;; Authors: David Edmondson <dme@dme.org>\r
192 >>  \r
193 >>  (require 'message)\r
194 >> +(require 'notmuch-query)\r
195 >> +(require 'notmuch-parser)\r
196 >>  \r
197 >>  ;;\r
198 >>  \r
199 >> -(defcustom notmuch-address-command "notmuch-addresses"\r
200 >> -  "The command which generates possible addresses. It must take a\r
201 >> -single argument and output a list of possible matches, one per\r
202 >> -line."\r
203 >> -  :type 'string\r
204 >> +(defcustom notmuch-address-command nil\r
205 >> +  "The command which generates possible addresses for completion.\r
206 >> +It must take a single argument and output a list of possible\r
207 >> +matches, one per line. If set to nil, addresses are generated by\r
208 >> +a built-in completion mechanism."\r
209 >> +  :type '(radio (const :tag "No command: Use built-in completion" nil)\r
210 >> +             (string :tag "Custom command" :value "notmuch-addresses"))\r
211 >>    :group 'notmuch-send\r
212 >>    :group 'notmuch-external)\r
213 >>  \r
214 >> @@ -42,6 +46,10 @@ to know how address selection is made by default."\r
215 >>    :group 'notmuch-send\r
216 >>    :group 'notmuch-external)\r
217 >>  \r
218 >> +(defvar notmuch-address-completions nil\r
219 >> +  "Hash of email addresses for completion during email composition.\r
220 >> +  This variable is set by calling `notmuch-address-harvest'.")\r
221 >> +\r
222 >>  (defun notmuch-address-selection-function (prompt collection initial-input)\r
223 >>    "Call (`completing-read'\r
224 >>        PROMPT COLLECTION nil nil INITIAL-INPUT 'notmuch-address-history)"\r
225 >> @@ -60,7 +68,16 @@ to know how address selection is made by default."\r
226 >>        (push notmuch-address-message-alist-member message-completion-alist))))\r
227 >>  \r
228 >>  (defun notmuch-address-options (original)\r
229 >> -  (process-lines notmuch-address-command original))\r
230 >> +  (cond\r
231 >> +   ((eq notmuch-address-command nil)\r
232 >> +    (let ((candidates))\r
233 >> +      (maphash (lambda (key val)\r
234 >> +             (let ((re (concat "\\<" (regexp-quote original))))\r
235 >> +               (when (string-match re key)\r
236 >> +                 (push key candidates))))\r
237 >> +           notmuch-address-completions)\r
238 >> +      candidates))\r
239 >> +   (t (process-lines notmuch-address-command original))))\r
240 >>  \r
241 >>  (defun notmuch-address-expand-name ()\r
242 >>    (let* ((end (point))\r
243 >> @@ -108,11 +125,71 @@ to know how address selection is made by default."\r
244 >>                         (not (file-directory-p bin))))\r
245 >>            (throw 'found-command bin))))))))\r
246 >>  \r
247 >> -;; If we can find the program specified by `notmuch-address-command',\r
248 >> -;; insinuate ourselves into `message-mode'.\r
249 >> -(when (notmuch-address-locate-command notmuch-address-command)\r
250 >> +(defun notmuch-address-harvest-msg (msg)\r
251 >> +  (let* ((headers (plist-get msg :headers))\r
252 >> +     (to (ignore-errors (mail-extract-address-components (plist-get headers :To) t)))\r
253 >> +     (cc (ignore-errors (mail-extract-address-components (plist-get headers :Cc) t)))\r
254 >> +     (bcc (ignore-errors (mail-extract-address-components (plist-get headers :Bcc) t))))\r
255 >> +    (mapc (lambda (parts)\r
256 >> +        (let* ((name (car parts))\r
257 >> +               (email (cadr parts))\r
258 >> +               (entry (if name (format "%s <%s>" name email) email)))\r
259 >> +          (puthash entry t notmuch-address-completions)))\r
260 >> +      (append to cc bcc))\r
261 >> +    nil))\r
262 >> +\r
263 >> +(defun notmuch-address-harvest-handle-result (obj)\r
264 >> +  (notmuch-query-map-threads 'notmuch-address-harvest-msg (list obj)))\r
265 >> +\r
266 >> +(defun notmuch-address-harvest-filter (proc string)\r
267 >> +  (when (buffer-live-p (process-buffer proc))\r
268 >> +    (with-current-buffer (process-buffer proc)\r
269 >> +      (save-excursion\r
270 >> +    (goto-char (point-max))\r
271 >> +    (insert string))\r
272 >> +      (notmuch-sexp-parse-partial-list\r
273 >> +       'notmuch-address-harvest-handle-result (process-buffer proc)))))\r
274 >> +\r
275 >> +(defvar notmuch-address-harvest-proc nil)   ; the process of a harvest underway\r
276 >> +\r
277 >> +(defun notmuch-address-harvest ()\r
278 >> +  "Collect possible addresses for completion. It queries the\r
279 >> +notmuch database for all emails sent by the user and collects the\r
280 >> +destination addresses from them in\r
281 >> +`notmuch-address-completions'. This takes some time so the\r
282 >> +address collection runs asynchronously."\r
283 >> +  (when notmuch-address-harvest-proc\r
284 >> +    (kill-buffer (process-buffer notmuch-address-harvest-proc)) ; this also kills the process\r
285 >> +    (setq notmuch-address-harvest-proc nil))\r
286 >> +  (setq notmuch-address-completions (make-hash-table :test 'equal))\r
287 >> +  (setq notmuch-address-harvest-proc\r
288 >> +        (notmuch-start-notmuch\r
289 >> +         "notmuch-address-harvest"  ; process name\r
290 >> +         " *notmuch-address-harvest*"       ; process buffer\r
291 >> +         nil                                ; process sentinel\r
292 >> +         "show"                             ; notmuch command\r
293 >> +         "--format=sexp"\r
294 >> +         "--format-version=2"\r
295 >> +         "--body=false"\r
296 >> +         "--entire-thread=false"\r
297 >> +     (mapconcat (lambda (x) (concat "from:" x)) (notmuch-user-emails) " or ")))\r
298 >> +  (set-process-filter notmuch-address-harvest-proc 'notmuch-address-harvest-filter)\r
299 >> +  (set-process-query-on-exit-flag notmuch-address-harvest-proc nil)\r
300 >> +  ;; return value\r
301 >> +  nil)\r
302 >> +\r
303 >> +;; If we can find the program specified by `notmuch-address-command'\r
304 >> +;; or if it is nil, insinuate ourselves into `message-mode'.\r
305 >> +(when (or (eq notmuch-address-command nil)\r
306 >> +      (notmuch-address-locate-command notmuch-address-command))\r
307 >>    (notmuch-address-message-insinuate))\r
308 >>  \r
309 >> +(defun notmuch-address-harvest-start ()\r
310 >> +  (when (not notmuch-address-completions) (notmuch-address-harvest)))\r
311 >> +\r
312 >> +(when (eq notmuch-address-command nil)\r
313 >> +  (add-hook 'message-mode-hook 'notmuch-address-harvest-start))\r
314 >> +\r
315 >>  ;;\r
316 >>  \r
317 >>  (provide 'notmuch-address)\r
318 >> diff --git a/emacs/notmuch-company.el b/emacs/notmuch-company.el\r
319 >> new file mode 100644\r
320 >> index 0000000..748fcee\r
321 >> --- /dev/null\r
322 >> +++ b/emacs/notmuch-company.el\r
323 >> @@ -0,0 +1,69 @@\r
324 >> +;; -*-no-byte-compile: t; -*-\r
325 >> +\r
326 >> +;; notmuch-company.el --- Mail address completion for notmuch via company-mode\r
327 >> +\r
328 >> +;; Author: Trevor Jim <tjim@mac.com>\r
329 >> +;; Keywords: mail, completion\r
330 >> +\r
331 >> +;; This program is free software; you can redistribute it and/or modify\r
332 >> +;; it under the terms of the GNU General Public License as published by\r
333 >> +;; the Free Software Foundation, either version 3 of the License, or\r
334 >> +;; (at your option) any later version.\r
335 >> +\r
336 >> +;; This program is distributed in the hope that it will be useful,\r
337 >> +;; but WITHOUT ANY WARRANTY; without even the implied warranty of\r
338 >> +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
339 >> +;; GNU General Public License for more details.\r
340 >> +\r
341 >> +;; You should have received a copy of the GNU General Public License\r
342 >> +;; along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
343 >> +\r
344 >> +;;; Commentary:\r
345 >> +\r
346 >> +;; To enable this, install company mode (https://company-mode.github.io/)\r
347 >> +;; and add\r
348 >> +;;\r
349 >> +;;     (require 'notmuch-company)\r
350 >> +;;\r
351 >> +;; to your .emacs.\r
352 >> +;;\r
353 >> +;; NB company-minimum-prefix-length defaults to 3 so you don't get\r
354 >> +;; completion unless you type 3 characters\r
355 >> +\r
356 >> +;;; Bugs:\r
357 >> +;;\r
358 >> +;; - matching is case sensitive\r
359 >> +;; - matching of non-ascii characters doesn't work well\r
360 >> +\r
361 >> +;;; Code:\r
362 >> +\r
363 >> +(require 'company)\r
364 >> +(require 'message)\r
365 >> +(require 'notmuch-address)\r
366 >> +\r
367 >> +(defvar-local notmuch-company-last-prefix nil)\r
368 >> +;;;###autoload\r
369 >> +(defun notmuch-company (command &optional arg &rest ignore)\r
370 >> +  "`company-mode' completion back-end for `notmuch'."\r
371 >> +  (interactive (list 'interactive))\r
372 >> +  (let ((case-fold-search t))\r
373 >> +    (pcase command\r
374 >> +      (`interactive (company-begin-backend 'notmuch-company))\r
375 >> +      (`prefix (and (eq major-mode 'message-mode)\r
376 >> +                    (looking-back "^\\(To\\|Cc\\|Bcc\\):.*"\r
377 >> +                                  (line-beginning-position))\r
378 >> +                    (setq notmuch-company-last-prefix (company-grab-symbol))))\r
379 >> +      (`candidates (let ((results (completion-substring--all-completions arg notmuch-address-completions nil 0)))\r
380 >> +                     (when results (car results))))\r
381 >> +      (`match (if (string-match notmuch-company-last-prefix arg)\r
382 >> +                  (match-end 0)\r
383 >> +                0))\r
384 >> +      (`no-cache t))))\r
385 >> +\r
386 >> +(add-hook 'message-mode-hook '(lambda ()\r
387 >> +                                (company-mode)\r
388 >> +                                (make-local-variable 'company-backends)\r
389 >> +                                (setq company-backends '(notmuch-company))\r
390 >> +                            (notmuch-address-harvest-start)))\r
391 >> +\r
392 >> +(provide 'notmuch-company)\r
393 >> diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
394 >> index 19269e3..00e8554 100644\r
395 >> --- a/emacs/notmuch-lib.el\r
396 >> +++ b/emacs/notmuch-lib.el\r
397 >> @@ -228,6 +228,9 @@ on the command line, and then retry your notmuch command")))\r
398 >>    "Return the user.other_email value (as a list) from the notmuch configuration."\r
399 >>    (split-string (notmuch-config-get "user.other_email") "\n" t))\r
400 >>  \r
401 >> +(defun notmuch-user-emails ()\r
402 >> +  (cons (notmuch-user-primary-email) (notmuch-user-other-email)))\r
403 >> +\r
404 >>  (defun notmuch-poll ()\r
405 >>    "Run \"notmuch new\" or an external script to import mail.\r
406 >>  \r
407 >> -- \r
408 >> 2.0.1\r
409 > _______________________________________________\r
410 > notmuch mailing list\r
411 > notmuch@notmuchmail.org\r
412 > http://notmuchmail.org/mailman/listinfo/notmuch\r