Re: [feature request] emacs: use `notmuch insert` for FCC
[notmuch-archives.git] / 83 / 7f799c5cdaad7ecf47240eb55974b822da935b
1 Return-Path: <markwalters1009@gmail.com>\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 arlo.cworth.org (Postfix) with ESMTP id 4E2406DE17EF\r
6  for <notmuch@notmuchmail.org>; Sat, 24 Oct 2015 16:34:26 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0.276\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0.276 tagged_above=-999 required=5 tests=[AWL=0.102, \r
12  DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
13  FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001,\r
14  RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01,\r
15  RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001, URIBL_SBL=0.644,\r
16  URIBL_SBL_A=0.1] autolearn=disabled\r
17 Received: from arlo.cworth.org ([127.0.0.1])\r
18  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
19  with ESMTP id 2q5jFyltyGhn for <notmuch@notmuchmail.org>;\r
20  Sat, 24 Oct 2015 16:34:23 -0700 (PDT)\r
21 Received: from mail-wi0-f172.google.com (mail-wi0-f172.google.com\r
22  [209.85.212.172])\r
23  by arlo.cworth.org (Postfix) with ESMTPS id 26D466DE17A4\r
24  for <notmuch@notmuchmail.org>; Sat, 24 Oct 2015 16:34:23 -0700 (PDT)\r
25 Received: by wicfx6 with SMTP id fx6so71410167wic.1\r
26  for <notmuch@notmuchmail.org>; Sat, 24 Oct 2015 16:34:21 -0700 (PDT)\r
27 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
28  h=from:to:cc:subject:in-reply-to:references:user-agent:date\r
29  :message-id:mime-version:content-type;\r
30  bh=p0YTSE7RHIKMS8J/L6g7+MJBON+FTorskSWp4HNMYs0=;\r
31  b=XcFIdFKAFU/WYJgmPzp+Ty2e9FXNFgOLqMA7eFTO8Ls4ZjL+3bagkX6wYtRp0G/U8n\r
32  e5/KjVGm4Bi88f+jZju/iBQ4se72KEEwH20dwu1qDjERwdKs9Zmx3o5GFd3uGo4L775p\r
33  SzMEXsyyC7ybbYibnZR3vKAUcwhxQJ5htzfHi9/3N/g5fQ79FY3vdXDPXJZ2Q7SvvLSt\r
34  NNQR08unZfDwho6hYHg+MHwaIyIqmQ6muG+MkGYKU9mxmhSFnXK/D3+kBtOd9pE/xyJc\r
35  y+oTgoyS85d5fSN4nQneZDKGqxuB8Pfc1vrr+EpLbS0opnocn1DIyxvZPtx8ps05R21q\r
36  sZDQ==\r
37 X-Received: by 10.194.48.113 with SMTP id k17mr12132347wjn.62.1445729661356;\r
38  Sat, 24 Oct 2015 16:34:21 -0700 (PDT)\r
39 Received: from localhost (5751dfa2.skybroadband.com. [87.81.223.162])\r
40  by smtp.gmail.com with ESMTPSA id hk5sm30494962wjb.6.2015.10.24.16.34.19\r
41  (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
42  Sat, 24 Oct 2015 16:34:20 -0700 (PDT)\r
43 From: Mark Walters <markwalters1009@gmail.com>\r
44 To: David Bremner <david@tethera.net>, notmuch@notmuchmail.org\r
45 Subject: Re: [PATCH 3/3] Emacs: Add address completion based on company-mode\r
46 In-Reply-To: <1445708484-32189-4-git-send-email-david@tethera.net>\r
47 References: <1445702019-10638-1-git-send-email-markwalters1009@gmail.com>\r
48  <1445708484-32189-1-git-send-email-david@tethera.net>\r
49  <1445708484-32189-4-git-send-email-david@tethera.net>\r
50 User-Agent: Notmuch/0.21~rc1+29~g057f24d (http://notmuchmail.org) Emacs/23.4.1\r
51  (x86_64-pc-linux-gnu)\r
52 Date: Sun, 25 Oct 2015 00:34:17 +0100\r
53 Message-ID: <87lhardeo6.fsf@qmul.ac.uk>\r
54 MIME-Version: 1.0\r
55 Content-Type: text/plain; charset=us-ascii\r
56 X-BeenThere: notmuch@notmuchmail.org\r
57 X-Mailman-Version: 2.1.20\r
58 Precedence: list\r
59 List-Id: "Use and development of the notmuch mail system."\r
60  <notmuch.notmuchmail.org>\r
61 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
62  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
63 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
64 List-Post: <mailto:notmuch@notmuchmail.org>\r
65 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
66 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
67  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
68 X-List-Received-Date: Sat, 24 Oct 2015 23:34:26 -0000\r
69 \r
70 On Sat, 24 Oct 2015, David Bremner <david@tethera.net> wrote:\r
71 > From: Michal Sojka <sojkam1@fel.cvut.cz>\r
72 >\r
73 > With this patch, address completion candidates are shown automatically\r
74 > after short typing delay in a nice popup box. This requires company-mode\r
75 > to be installed and it works only on Emacs >= 24. The completion is\r
76 > based entirely on the asynchronous address harvesting from\r
77 > notmuch-address.el so the GUI is theoretically not blocked for long\r
78 > time.\r
79 >\r
80 > The completion works similarly as the TAB-initiated completion from\r
81 > notmuch-address.el, i.e. quick harvest based on user input is executed\r
82 > first and only after full harvesting is finished, in-memory cached data\r
83 > is used.\r
84 > ---\r
85 >  emacs/Makefile.local     |  1 +\r
86 >  emacs/notmuch-company.el | 73 ++++++++++++++++++++++++++++++++++++++++++++++++\r
87 >  emacs/notmuch-mua.el     | 13 ++++++++-\r
88 >  3 files changed, 86 insertions(+), 1 deletion(-)\r
89 >  create mode 100644 emacs/notmuch-company.el\r
90 >\r
91 > diff --git a/emacs/Makefile.local b/emacs/Makefile.local\r
92 > index 1109cfa..4c06c52 100644\r
93 > --- a/emacs/Makefile.local\r
94 > +++ b/emacs/Makefile.local\r
95 > @@ -20,6 +20,7 @@ emacs_sources := \\r
96 >       $(dir)/notmuch-print.el \\r
97 >       $(dir)/notmuch-version.el \\r
98 >       $(dir)/notmuch-jump.el \\r
99 > +     $(dir)/notmuch-company.el\r
100 >  \r
101 >  $(dir)/notmuch-version.el: $(dir)/Makefile.local version.stamp\r
102 >  $(dir)/notmuch-version.el: $(srcdir)/$(dir)/notmuch-version.el.tmpl\r
103 > diff --git a/emacs/notmuch-company.el b/emacs/notmuch-company.el\r
104 > new file mode 100644\r
105 > index 0000000..03c492f\r
106 > --- /dev/null\r
107 > +++ b/emacs/notmuch-company.el\r
108 > @@ -0,0 +1,73 @@\r
109 > +;; notmuch-company.el --- Mail address completion for notmuch via company-mode  -*- lexical-binding: t -*-\r
110 > +\r
111 > +\r
112 > +;; Authors: Trevor Jim <tjim@mac.com>\r
113 > +;;       Michal Sojka <sojkam1@fel.cvut.cz>\r
114 > +;;\r
115 > +;; Keywords: mail, completion\r
116 > +\r
117 > +;; This program is free software; you can redistribute it and/or modify\r
118 > +;; it under the terms of the GNU General Public License as published by\r
119 > +;; the Free Software Foundation, either version 3 of the License, or\r
120 > +;; (at your option) any later version.\r
121 > +\r
122 > +;; This program is distributed in the hope that it will be useful,\r
123 > +;; but WITHOUT ANY WARRANTY; without even the implied warranty of\r
124 > +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
125 > +;; GNU General Public License for more details.\r
126 > +\r
127 > +;; You should have received a copy of the GNU General Public License\r
128 > +;; along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
129 > +\r
130 > +;;; Commentary:\r
131 > +\r
132 > +;; To enable this, install company mode (https://company-mode.github.io/)\r
133 > +;; and customize notmuch-message-use-company\r
134 > +;;\r
135 > +;; NB company-minimum-prefix-length defaults to 3 so you don't get\r
136 > +;; completion unless you type 3 characters\r
137 > +\r
138 > +;;; Code:\r
139 > +\r
140 > +(require 'notmuch-address)\r
141 > +(require 'cl-lib)\r
142 > +\r
143 > +(defvar-local notmuch-company-last-prefix nil)\r
144 > +(declare-function company-begin-backend "company")\r
145 > +(declare-function company-grab "company")\r
146 > +\r
147 > +;;;###autoload\r
148 > +(defun notmuch-company (command &optional arg &rest _ignore)\r
149 > +  "`company-mode' completion back-end for `notmuch'."\r
150 > +  (interactive (list 'interactive))\r
151 > +  (require 'company)\r
152 > +  (let ((case-fold-search t)\r
153 > +     (completion-ignore-case t))\r
154 > +    (cl-case command\r
155 > +      (interactive (company-begin-backend 'notmuch-company))\r
156 > +      (prefix (and (derived-mode-p 'message-mode)\r
157 > +                (looking-back "^\\(To\\|Cc\\|Bcc\\):.*"\r
158 > +                              (line-beginning-position))\r
159 > +                (setq notmuch-company-last-prefix (company-grab "[:,][ \t]*\\(.*\\)" 1 (point-at-bol)))))\r
160 > +      (candidates (cond\r
161 > +                (notmuch-address-full-harvest-finished\r
162 > +                 ;; Update harvested addressed from time to time\r
163 > +                 (notmuch-address-harvest-trigger)\r
164 > +                 (notmuch-address-matching arg))\r
165 > +                (t\r
166 > +                 (cons :async\r
167 > +                       (lambda (callback)\r
168 > +                         ;; First run quick asynchronous harvest based on what the user entered so far\r
169 > +                         (notmuch-address-harvest\r
170 > +                          (format "to:%s*" arg) nil\r
171 > +                          (lambda (_proc _event)\r
172 > +                            (funcall callback (notmuch-address-matching arg))\r
173 > +                            ;; Then (re)start potentially long-running full asynchronous harvesting\r
174 > +                            (notmuch-address-harvest-trigger))))))))\r
175 \r
176 I have found a bug in this but I don't know the best way to fix it.\r
177 \r
178 If you start notmuch in emacs, start composing a message, type 3 letters\r
179 (say) in the to line and press tab then you get the "quick" address\r
180 completion, and it starts the full harvest. However if you don't select\r
181 one of the addresses and just type another character and pause then\r
182 notmuch-company starts and it sees that you haven't got a full harvest\r
183 so it asynchronously gets the quick completions. But this kills the main\r
184 harvest (the "; this also kills the process" line in\r
185 notmuch-address.el). The sentinel for the full harvest then marks the\r
186 full harvest complete.\r
187 \r
188 Since it is marked complete the full harvest won't ever get done (or\r
189 not until the completion timeouts in 24 hours). \r
190 \r
191 The first part of the fix is probably to only mark the harvest complete\r
192 if the sentinel returns normally. But it may need more, perhaps to run\r
193 the async quick case in a separate buffer from the full harvest.\r
194 \r
195 Best wishes\r
196 \r
197 Mark\r
198 \r
199 \r
200 > +      (match (if (string-match notmuch-company-last-prefix arg)\r
201 > +              (match-end 0)\r
202 > +            0))\r
203 > +      (no-cache t))))\r
204 > +\r
205 > +\r
206 > +(provide 'notmuch-company)\r
207 > diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el\r
208 > index 6cc9656..c90381d 100644\r
209 > --- a/emacs/notmuch-mua.el\r
210 > +++ b/emacs/notmuch-mua.el\r
211 > @@ -25,6 +25,7 @@\r
212 >  \r
213 >  (require 'notmuch-lib)\r
214 >  (require 'notmuch-address)\r
215 > +(require 'notmuch-company)\r
216 >  \r
217 >  (eval-when-compile (require 'cl))\r
218 >  \r
219 > @@ -268,12 +269,22 @@ Note that these functions use `mail-citation-hook' if that is non-nil."\r
220 >    (message-goto-body)\r
221 >    (set-buffer-modified-p nil))\r
222 >  \r
223 > +(defcustom notmuch-message-use-company t\r
224 > +  "If available, use company mode for completion in notmuch-message-mode"\r
225 > +  :type 'boolean\r
226 > +  :group 'notmuch-send)\r
227 > +\r
228 >  (define-derived-mode notmuch-message-mode message-mode "Message[Notmuch]"\r
229 >    "Notmuch message composition mode. Mostly like `message-mode'"\r
230 >    (when notmuch-address-command\r
231 >      (unless (memq notmuch-address-message-alist-member message-completion-alist)\r
232 >        (setq message-completion-alist\r
233 > -         (push notmuch-address-message-alist-member message-completion-alist)))))\r
234 > +         (push notmuch-address-message-alist-member message-completion-alist))))\r
235 > +  (when (and notmuch-message-use-company\r
236 > +          (require 'company nil t))\r
237 > +    (company-mode)\r
238 > +    (make-local-variable 'company-backends)\r
239 > +    (setq company-backends '(notmuch-company))))\r
240 >  \r
241 >  (define-key notmuch-message-mode-map (kbd "C-c C-c") #'notmuch-mua-send-and-exit)\r
242 >  (define-key notmuch-message-mode-map (kbd "C-c C-s") #'notmuch-mua-send)\r
243 > -- \r
244 > 2.6.1\r