1 Return-Path: <bremner@tethera.net>
\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 D6A596DE13ED
\r
6 for <notmuch@notmuchmail.org>; Sun, 25 Oct 2015 08:25:21 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org
\r
11 X-Spam-Status: No, score=0.098 tagged_above=-999 required=5 tests=[AWL=0.098]
\r
13 Received: from arlo.cworth.org ([127.0.0.1])
\r
14 by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)
\r
15 with ESMTP id H71GHrMDFfYv for <notmuch@notmuchmail.org>;
\r
16 Sun, 25 Oct 2015 08:25:19 -0700 (PDT)
\r
17 Received: from gitolite.debian.net (gitolite.debian.net [87.98.215.224])
\r
18 by arlo.cworth.org (Postfix) with ESMTPS id B40786DE13AF
\r
19 for <notmuch@notmuchmail.org>; Sun, 25 Oct 2015 08:25:18 -0700 (PDT)
\r
20 Received: from remotemail by gitolite.debian.net with local (Exim 4.80)
\r
21 (envelope-from <bremner@tethera.net>)
\r
22 id 1ZqNA3-0004z9-QX; Sun, 25 Oct 2015 15:24:43 +0000
\r
23 Received: (nullmailer pid 13829 invoked by uid 1000); Sun, 25 Oct 2015
\r
25 From: David Bremner <david@tethera.net>
\r
26 To: notmuch@notmuchmail.org
\r
27 Subject: [Patch v7 3/3] Emacs: Add address completion based on company-mode
\r
28 Date: Sun, 25 Oct 2015 12:23:58 -0300
\r
29 Message-Id: <1445786638-13763-4-git-send-email-david@tethera.net>
\r
30 X-Mailer: git-send-email 2.6.1
\r
31 In-Reply-To: <1445786638-13763-1-git-send-email-david@tethera.net>
\r
32 References: <1445786638-13763-1-git-send-email-david@tethera.net>
\r
33 X-BeenThere: notmuch@notmuchmail.org
\r
34 X-Mailman-Version: 2.1.20
\r
36 List-Id: "Use and development of the notmuch mail system."
\r
37 <notmuch.notmuchmail.org>
\r
38 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,
\r
39 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
40 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>
\r
41 List-Post: <mailto:notmuch@notmuchmail.org>
\r
42 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
43 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,
\r
44 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
45 X-List-Received-Date: Sun, 25 Oct 2015 15:25:22 -0000
\r
47 From: Michal Sojka <sojkam1@fel.cvut.cz>
\r
49 With this patch, address completion candidates are shown automatically
\r
50 after short typing delay in a nice popup box. This requires company-mode
\r
51 to be installed and it works only on Emacs >= 24. The completion is
\r
52 based entirely on the asynchronous address harvesting from
\r
53 notmuch-address.el so the GUI is theoretically not blocked for long
\r
56 The completion works similarly as the TAB-initiated completion from
\r
57 notmuch-address.el, i.e. quick harvest based on user input is executed
\r
58 first and only after full harvesting is finished, in-memory cached data
\r
61 emacs/Makefile.local | 1 +
\r
62 emacs/notmuch-address.el | 18 ++++++++--
\r
63 emacs/notmuch-company.el | 86 ++++++++++++++++++++++++++++++++++++++++++++++++
\r
64 emacs/notmuch-mua.el | 2 +-
\r
65 4 files changed, 104 insertions(+), 3 deletions(-)
\r
66 create mode 100644 emacs/notmuch-company.el
\r
68 diff --git a/emacs/Makefile.local b/emacs/Makefile.local
\r
69 index 1109cfa..4c06c52 100644
\r
70 --- a/emacs/Makefile.local
\r
71 +++ b/emacs/Makefile.local
\r
72 @@ -20,6 +20,7 @@ emacs_sources := \
\r
73 $(dir)/notmuch-print.el \
\r
74 $(dir)/notmuch-version.el \
\r
75 $(dir)/notmuch-jump.el \
\r
76 + $(dir)/notmuch-company.el
\r
78 $(dir)/notmuch-version.el: $(dir)/Makefile.local version.stamp
\r
79 $(dir)/notmuch-version.el: $(srcdir)/$(dir)/notmuch-version.el.tmpl
\r
80 diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el
\r
81 index aa6228d..5456d5c 100644
\r
82 --- a/emacs/notmuch-address.el
\r
83 +++ b/emacs/notmuch-address.el
\r
86 (require 'notmuch-parser)
\r
87 (require 'notmuch-lib)
\r
88 +(require 'notmuch-company)
\r
90 +(declare-function company-manual-begin "company")
\r
92 (defcustom notmuch-address-command 'internal
\r
93 "The command which generates possible addresses. It must take a
\r
94 @@ -72,9 +74,21 @@ finished")
\r
95 (defun notmuch-address-message-insinuate ()
\r
96 (message "calling notmuch-address-message-insinuate is no longer needed"))
\r
98 +(defcustom notmuch-address-use-company t
\r
99 + "If available, use company mode for address completion"
\r
101 + :group 'notmuch-send)
\r
103 (defun notmuch-address-setup ()
\r
104 - (let ((pair (cons notmuch-address-completion-headers-regexp
\r
105 - #'notmuch-address-expand-name)))
\r
106 + (let* ((use-company (and notmuch-address-use-company
\r
107 + (eq notmuch-address-command 'internal)
\r
108 + (require 'company nil t)))
\r
109 + (pair (cons notmuch-address-completion-headers-regexp
\r
111 + #'company-manual-begin
\r
112 + #'notmuch-address-expand-name))))
\r
113 + (when use-company
\r
114 + (notmuch-company-setup))
\r
115 (unless (memq pair message-completion-alist)
\r
116 (setq message-completion-alist
\r
117 (push pair message-completion-alist)))))
\r
118 diff --git a/emacs/notmuch-company.el b/emacs/notmuch-company.el
\r
119 new file mode 100644
\r
120 index 0000000..49d1d81
\r
122 +++ b/emacs/notmuch-company.el
\r
124 +;; notmuch-company.el --- Mail address completion for notmuch via company-mode -*- lexical-binding: t -*-
\r
126 +;; Authors: Trevor Jim <tjim@mac.com>
\r
127 +;; Michal Sojka <sojkam1@fel.cvut.cz>
\r
129 +;; Keywords: mail, completion
\r
131 +;; This program is free software; you can redistribute it and/or modify
\r
132 +;; it under the terms of the GNU General Public License as published by
\r
133 +;; the Free Software Foundation, either version 3 of the License, or
\r
134 +;; (at your option) any later version.
\r
136 +;; This program is distributed in the hope that it will be useful,
\r
137 +;; but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
138 +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
139 +;; GNU General Public License for more details.
\r
141 +;; You should have received a copy of the GNU General Public License
\r
142 +;; along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
146 +;; To enable this, install company mode (https://company-mode.github.io/)
\r
148 +;; NB company-minimum-prefix-length defaults to 3 so you don't get
\r
149 +;; completion unless you type 3 characters
\r
153 +(eval-when-compile (require 'cl))
\r
155 +(defvar notmuch-company-last-prefix nil)
\r
156 +(make-variable-buffer-local 'notmuch-company-last-prefix)
\r
157 +(declare-function company-begin-backend "company")
\r
158 +(declare-function company-grab "company")
\r
159 +(declare-function company-mode "company")
\r
160 +(declare-function company-manual-begin "company")
\r
161 +(defvar company-backends)
\r
163 +(declare-function notmuch-address-harvest "notmuch-address")
\r
164 +(declare-function notmuch-address-harvest-trigger "notmuch-address")
\r
165 +(declare-function notmuch-address-matching "notmuch-address")
\r
166 +(defvar notmuch-address-full-harvest-finished)
\r
167 +(defvar notmuch-address-completion-headers-regexp)
\r
170 +(defun notmuch-company-setup ()
\r
172 + (make-local-variable 'company-backends)
\r
173 + (setq company-backends '(notmuch-company)))
\r
176 +(defun notmuch-company (command &optional arg &rest _ignore)
\r
177 + "`company-mode' completion back-end for `notmuch'."
\r
178 + (interactive (list 'interactive))
\r
179 + (require 'company)
\r
180 + (let ((case-fold-search t)
\r
181 + (completion-ignore-case t))
\r
183 + (interactive (company-begin-backend 'notmuch-company))
\r
184 + (prefix (and (derived-mode-p 'message-mode)
\r
185 + (looking-back (concat notmuch-address-completion-headers-regexp ".*")
\r
186 + (line-beginning-position))
\r
187 + (setq notmuch-company-last-prefix (company-grab "[:,][ \t]*\\(.*\\)" 1 (point-at-bol)))))
\r
188 + (candidates (cond
\r
189 + (notmuch-address-full-harvest-finished
\r
190 + ;; Update harvested addressed from time to time
\r
191 + (notmuch-address-harvest-trigger)
\r
192 + (notmuch-address-matching arg))
\r
195 + (lambda (callback)
\r
196 + ;; First run quick asynchronous harvest based on what the user entered so far
\r
197 + (notmuch-address-harvest
\r
198 + (format "to:%s*" arg) nil
\r
199 + (lambda (_proc _event)
\r
200 + (funcall callback (notmuch-address-matching arg))
\r
201 + ;; Then (re)start potentially long-running full asynchronous harvesting
\r
202 + (notmuch-address-harvest-trigger))))))))
\r
203 + (match (if (string-match notmuch-company-last-prefix arg)
\r
209 +(provide 'notmuch-company)
\r
210 diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el
\r
211 index fd98ea4..c12054c 100644
\r
212 --- a/emacs/notmuch-mua.el
\r
213 +++ b/emacs/notmuch-mua.el
\r
214 @@ -271,7 +271,7 @@ Note that these functions use `mail-citation-hook' if that is non-nil."
\r
215 (define-derived-mode notmuch-message-mode message-mode "Message[Notmuch]"
\r
216 "Notmuch message composition mode. Mostly like `message-mode'"
\r
217 (when notmuch-address-command
\r
218 - (notmuch-address-setup)))
\r
219 + (notmuch-address-setup)))
\r
221 (define-key notmuch-message-mode-map (kbd "C-c C-c") #'notmuch-mua-send-and-exit)
\r
222 (define-key notmuch-message-mode-map (kbd "C-c C-s") #'notmuch-mua-send)
\r