Re: notmuch and "mute" -- useful to anyone?
[notmuch-archives.git] / 29 / 6027c3dac6ebeec4065e6983672acff02b490e
1 Return-Path: <sojkam1@fel.cvut.cz>\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 230546DE17D7\r
6  for <notmuch@notmuchmail.org>; Mon, 26 Oct 2015 16:30:12 -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.612\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.612 tagged_above=-999 required=5 tests=[AWL=2.238,\r
12   RCVD_IN_DNSWL_MED=-2.3, RP_MATCHES_RCVD=-0.55] autolearn=disabled\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 K91wbfQuo-P7 for <notmuch@notmuchmail.org>;\r
16  Mon, 26 Oct 2015 16:30:09 -0700 (PDT)\r
17 X-Greylist: delayed 421 seconds by postgrey-1.35 at arlo;\r
18  Mon, 26 Oct 2015 16:30:08 PDT\r
19 Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
20  by arlo.cworth.org (Postfix) with ESMTP id C2C356DE0B4B\r
21  for <notmuch@notmuchmail.org>; Mon, 26 Oct 2015 16:30:08 -0700 (PDT)\r
22 Received: from localhost (unknown [192.168.200.7])\r
23  by max.feld.cvut.cz (Postfix) with ESMTP id 5DAFF19F499E;\r
24  Tue, 27 Oct 2015 00:23:05 +0100 (CET)\r
25 X-Virus-Scanned: IMAP STYX AMAVIS\r
26 Received: from max.feld.cvut.cz ([192.168.200.1])\r
27  by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new, port 10044)\r
28  with ESMTP id HeYVG38Z3t1B; Tue, 27 Oct 2015 00:23:03 +0100 (CET)\r
29 Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
30  by max.feld.cvut.cz (Postfix) with ESMTP id AFE6019F49C3;\r
31  Tue, 27 Oct 2015 00:23:02 +0100 (CET)\r
32 Received: from wsh by steelpick.2x.cz with local (Exim 4.86)\r
33  (envelope-from <sojkam1@fel.cvut.cz>)\r
34  id 1Zqr6S-0007al-LC; Tue, 27 Oct 2015 00:23:00 +0100\r
35 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
36 To: notmuch@notmuchmail.org\r
37 Subject: [PATCH v8 3/3] Emacs: Add address completion based on company-mode\r
38 Date: Tue, 27 Oct 2015 00:22:49 +0100\r
39 Message-Id: <1445901769-29134-4-git-send-email-sojkam1@fel.cvut.cz>\r
40 X-Mailer: git-send-email 2.5.3\r
41 In-Reply-To: <1445901769-29134-1-git-send-email-sojkam1@fel.cvut.cz>\r
42 References: <1445901769-29134-1-git-send-email-sojkam1@fel.cvut.cz>\r
43 X-BeenThere: notmuch@notmuchmail.org\r
44 X-Mailman-Version: 2.1.20\r
45 Precedence: list\r
46 List-Id: "Use and development of the notmuch mail system."\r
47  <notmuch.notmuchmail.org>\r
48 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
49  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
50 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
51 List-Post: <mailto:notmuch@notmuchmail.org>\r
52 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
53 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
54  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
55 X-List-Received-Date: Mon, 26 Oct 2015 23:30:12 -0000\r
56 \r
57 When company-mode is available (Emacs >= 24), address completion\r
58 candidates are shown in a nice popup box. This is triggered either by\r
59 pressing TAB or by waiting a while during typing an address. The\r
60 completion is based entirely on the asynchronous address harvesting\r
61 from notmuch-address.el so the GUI is theoretically not blocked for\r
62 long time.\r
63 \r
64 The completion works similarly as the TAB-initiated completion from\r
65 notmuch-address.el, i.e. quick harvest based on user input is executed\r
66 first and only after full harvesting is finished, in-memory cached data\r
67 is used.\r
68 \r
69 [Improved by David Bremner]\r
70 ---\r
71  emacs/Makefile.local     |  1 +\r
72  emacs/notmuch-address.el | 18 ++++++++--\r
73  emacs/notmuch-company.el | 86 ++++++++++++++++++++++++++++++++++++++++++++++++\r
74  3 files changed, 103 insertions(+), 2 deletions(-)\r
75  create mode 100644 emacs/notmuch-company.el\r
76 \r
77 diff --git a/emacs/Makefile.local b/emacs/Makefile.local\r
78 index 1109cfa..4c06c52 100644\r
79 --- a/emacs/Makefile.local\r
80 +++ b/emacs/Makefile.local\r
81 @@ -20,6 +20,7 @@ emacs_sources := \\r
82         $(dir)/notmuch-print.el \\r
83         $(dir)/notmuch-version.el \\r
84         $(dir)/notmuch-jump.el \\r
85 +       $(dir)/notmuch-company.el\r
86  \r
87  $(dir)/notmuch-version.el: $(dir)/Makefile.local version.stamp\r
88  $(dir)/notmuch-version.el: $(srcdir)/$(dir)/notmuch-version.el.tmpl\r
89 diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el\r
90 index 498ef8a..49e2402 100644\r
91 --- a/emacs/notmuch-address.el\r
92 +++ b/emacs/notmuch-address.el\r
93 @@ -22,7 +22,9 @@\r
94  (require 'message)\r
95  (require 'notmuch-parser)\r
96  (require 'notmuch-lib)\r
97 +(require 'notmuch-company)\r
98  ;;\r
99 +(declare-function company-manual-begin "company")\r
100  \r
101  (defcustom notmuch-address-command 'internal\r
102    "The command which generates possible addresses. It must take a\r
103 @@ -72,9 +74,21 @@ finished")\r
104  (defun notmuch-address-message-insinuate ()\r
105    (message "calling notmuch-address-message-insinuate is no longer needed"))\r
106  \r
107 +(defcustom notmuch-address-use-company t\r
108 +  "If available, use company mode for address completion"\r
109 +  :type 'boolean\r
110 +  :group 'notmuch-send)\r
111 +\r
112  (defun notmuch-address-setup ()\r
113 -  (let ((pair (cons notmuch-address-completion-headers-regexp\r
114 -                   #'notmuch-address-expand-name)))\r
115 +  (let* ((use-company (and notmuch-address-use-company\r
116 +                          (eq notmuch-address-command 'internal)\r
117 +                          (require 'company nil t)))\r
118 +        (pair (cons notmuch-address-completion-headers-regexp\r
119 +                    (if use-company\r
120 +                        #'company-manual-begin\r
121 +                      #'notmuch-address-expand-name))))\r
122 +      (when use-company\r
123 +       (notmuch-company-setup))\r
124        (unless (memq pair message-completion-alist)\r
125         (setq message-completion-alist\r
126               (push pair message-completion-alist)))))\r
127 diff --git a/emacs/notmuch-company.el b/emacs/notmuch-company.el\r
128 new file mode 100644\r
129 index 0000000..add3161\r
130 --- /dev/null\r
131 +++ b/emacs/notmuch-company.el\r
132 @@ -0,0 +1,86 @@\r
133 +;; notmuch-company.el --- Mail address completion for notmuch via company-mode  -*- lexical-binding: t -*-\r
134 +\r
135 +;; Authors: Trevor Jim <tjim@mac.com>\r
136 +;;         Michal Sojka <sojkam1@fel.cvut.cz>\r
137 +;;\r
138 +;; Keywords: mail, completion\r
139 +\r
140 +;; This program is free software; you can redistribute it and/or modify\r
141 +;; it under the terms of the GNU General Public License as published by\r
142 +;; the Free Software Foundation, either version 3 of the License, or\r
143 +;; (at your option) any later version.\r
144 +\r
145 +;; This program is distributed in the hope that it will be useful,\r
146 +;; but WITHOUT ANY WARRANTY; without even the implied warranty of\r
147 +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
148 +;; GNU General Public License for more details.\r
149 +\r
150 +;; You should have received a copy of the GNU General Public License\r
151 +;; along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
152 +\r
153 +;;; Commentary:\r
154 +\r
155 +;; To enable this, install company mode (https://company-mode.github.io/)\r
156 +;;\r
157 +;; NB company-minimum-prefix-length defaults to 3 so you don't get\r
158 +;; completion unless you type 3 characters\r
159 +\r
160 +;;; Code:\r
161 +\r
162 +(eval-when-compile (require 'cl))\r
163 +\r
164 +(defvar notmuch-company-last-prefix nil)\r
165 +(make-variable-buffer-local 'notmuch-company-last-prefix)\r
166 +(declare-function company-begin-backend "company")\r
167 +(declare-function company-grab "company")\r
168 +(declare-function company-mode "company")\r
169 +(declare-function company-manual-begin "company")\r
170 +(defvar company-backends)\r
171 +\r
172 +(declare-function notmuch-address-harvest "notmuch-address")\r
173 +(declare-function notmuch-address-harvest-trigger "notmuch-address")\r
174 +(declare-function notmuch-address-matching "notmuch-address")\r
175 +(defvar notmuch-address-full-harvest-finished)\r
176 +(defvar notmuch-address-completion-headers-regexp)\r
177 +\r
178 +;;;###autoload\r
179 +(defun notmuch-company-setup ()\r
180 +  (company-mode)\r
181 +  (make-local-variable 'company-backends)\r
182 +  (setq company-backends '(notmuch-company)))\r
183 +\r
184 +;;;###autoload\r
185 +(defun notmuch-company (command &optional arg &rest _ignore)\r
186 +  "`company-mode' completion back-end for `notmuch'."\r
187 +  (interactive (list 'interactive))\r
188 +  (require 'company)\r
189 +  (let ((case-fold-search t)\r
190 +       (completion-ignore-case t))\r
191 +    (case command\r
192 +      (interactive (company-begin-backend 'notmuch-company))\r
193 +      (prefix (and (derived-mode-p 'message-mode)\r
194 +                  (looking-back (concat notmuch-address-completion-headers-regexp ".*")\r
195 +                                (line-beginning-position))\r
196 +                  (setq notmuch-company-last-prefix (company-grab "[:,][ \t]*\\(.*\\)" 1 (point-at-bol)))))\r
197 +      (candidates (cond\r
198 +                  (notmuch-address-full-harvest-finished\r
199 +                   ;; Update harvested addressed from time to time\r
200 +                   (notmuch-address-harvest-trigger)\r
201 +                   (notmuch-address-matching arg))\r
202 +                  (t\r
203 +                   (cons :async\r
204 +                         (lambda (callback)\r
205 +                           ;; First run quick asynchronous harvest based on what the user entered so far\r
206 +                           (notmuch-address-harvest\r
207 +                            (format "to:%s*" arg) nil\r
208 +                            (lambda (_proc _event)\r
209 +                              (funcall callback (notmuch-address-matching arg))\r
210 +                              ;; Then start the (potentially long-running) full asynchronous harvest if necessary\r
211 +                              (notmuch-address-harvest-trigger))))))))\r
212 +      (match (if (string-match notmuch-company-last-prefix arg)\r
213 +                (match-end 0)\r
214 +              0))\r
215 +      (no-cache t))))\r
216 +\r
217 +\r
218 +(provide 'notmuch-company)\r
219 -- \r
220 2.5.3\r
221 \r