Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / d7 / ee5dd11497808f91d8d9f5e06386811853cf81
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 F38DC6DE1410\r
6  for <notmuch@notmuchmail.org>; Sat, 24 Oct 2015 10:44:01 -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.098\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0.098 tagged_above=-999 required=5 tests=[AWL=0.098]\r
12  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 PGF7NYmgAgfN for <notmuch@notmuchmail.org>;\r
16  Sat, 24 Oct 2015 10:43:58 -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 7CDA76DE1403\r
19  for <notmuch@notmuchmail.org>; Sat, 24 Oct 2015 10:43:58 -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 1Zq2pl-0001Hr-FX; Sat, 24 Oct 2015 17:42:25 +0000\r
23 Received: (nullmailer pid 32265 invoked by uid 1000); Sat, 24 Oct 2015\r
24  17:41:28 -0000\r
25 From: David Bremner <david@tethera.net>\r
26 To: Mark Walters <markwalters1009@gmail.com>, notmuch@notmuchmail.org\r
27 Subject: [PATCH 3/3] Emacs: Add address completion based on company-mode\r
28 Date: Sat, 24 Oct 2015 14:41:24 -0300\r
29 Message-Id: <1445708484-32189-4-git-send-email-david@tethera.net>\r
30 X-Mailer: git-send-email 2.6.1\r
31 In-Reply-To: <1445708484-32189-1-git-send-email-david@tethera.net>\r
32 References: <1445702019-10638-1-git-send-email-markwalters1009@gmail.com>\r
33  <1445708484-32189-1-git-send-email-david@tethera.net>\r
34 X-BeenThere: notmuch@notmuchmail.org\r
35 X-Mailman-Version: 2.1.20\r
36 Precedence: list\r
37 List-Id: "Use and development of the notmuch mail system."\r
38  <notmuch.notmuchmail.org>\r
39 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
40  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
41 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
42 List-Post: <mailto:notmuch@notmuchmail.org>\r
43 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
44 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
45  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
46 X-List-Received-Date: Sat, 24 Oct 2015 17:44:02 -0000\r
47 \r
48 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
49 \r
50 With this patch, address completion candidates are shown automatically\r
51 after short typing delay in a nice popup box. This requires company-mode\r
52 to be installed and it works only on Emacs >= 24. The completion is\r
53 based entirely on the asynchronous address harvesting from\r
54 notmuch-address.el so the GUI is theoretically not blocked for long\r
55 time.\r
56 \r
57 The completion works similarly as the TAB-initiated completion from\r
58 notmuch-address.el, i.e. quick harvest based on user input is executed\r
59 first and only after full harvesting is finished, in-memory cached data\r
60 is used.\r
61 ---\r
62  emacs/Makefile.local     |  1 +\r
63  emacs/notmuch-company.el | 73 ++++++++++++++++++++++++++++++++++++++++++++++++\r
64  emacs/notmuch-mua.el     | 13 ++++++++-\r
65  3 files changed, 86 insertions(+), 1 deletion(-)\r
66  create mode 100644 emacs/notmuch-company.el\r
67 \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
77  \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-company.el b/emacs/notmuch-company.el\r
81 new file mode 100644\r
82 index 0000000..03c492f\r
83 --- /dev/null\r
84 +++ b/emacs/notmuch-company.el\r
85 @@ -0,0 +1,73 @@\r
86 +;; notmuch-company.el --- Mail address completion for notmuch via company-mode  -*- lexical-binding: t -*-\r
87 +\r
88 +\r
89 +;; Authors: Trevor Jim <tjim@mac.com>\r
90 +;;         Michal Sojka <sojkam1@fel.cvut.cz>\r
91 +;;\r
92 +;; Keywords: mail, completion\r
93 +\r
94 +;; This program is free software; you can redistribute it and/or modify\r
95 +;; it under the terms of the GNU General Public License as published by\r
96 +;; the Free Software Foundation, either version 3 of the License, or\r
97 +;; (at your option) any later version.\r
98 +\r
99 +;; This program is distributed in the hope that it will be useful,\r
100 +;; but WITHOUT ANY WARRANTY; without even the implied warranty of\r
101 +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
102 +;; GNU General Public License for more details.\r
103 +\r
104 +;; You should have received a copy of the GNU General Public License\r
105 +;; along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
106 +\r
107 +;;; Commentary:\r
108 +\r
109 +;; To enable this, install company mode (https://company-mode.github.io/)\r
110 +;; and customize notmuch-message-use-company\r
111 +;;\r
112 +;; NB company-minimum-prefix-length defaults to 3 so you don't get\r
113 +;; completion unless you type 3 characters\r
114 +\r
115 +;;; Code:\r
116 +\r
117 +(require 'notmuch-address)\r
118 +(require 'cl-lib)\r
119 +\r
120 +(defvar-local notmuch-company-last-prefix nil)\r
121 +(declare-function company-begin-backend "company")\r
122 +(declare-function company-grab "company")\r
123 +\r
124 +;;;###autoload\r
125 +(defun notmuch-company (command &optional arg &rest _ignore)\r
126 +  "`company-mode' completion back-end for `notmuch'."\r
127 +  (interactive (list 'interactive))\r
128 +  (require 'company)\r
129 +  (let ((case-fold-search t)\r
130 +       (completion-ignore-case t))\r
131 +    (cl-case command\r
132 +      (interactive (company-begin-backend 'notmuch-company))\r
133 +      (prefix (and (derived-mode-p 'message-mode)\r
134 +                  (looking-back "^\\(To\\|Cc\\|Bcc\\):.*"\r
135 +                                (line-beginning-position))\r
136 +                  (setq notmuch-company-last-prefix (company-grab "[:,][ \t]*\\(.*\\)" 1 (point-at-bol)))))\r
137 +      (candidates (cond\r
138 +                  (notmuch-address-full-harvest-finished\r
139 +                   ;; Update harvested addressed from time to time\r
140 +                   (notmuch-address-harvest-trigger)\r
141 +                   (notmuch-address-matching arg))\r
142 +                  (t\r
143 +                   (cons :async\r
144 +                         (lambda (callback)\r
145 +                           ;; First run quick asynchronous harvest based on what the user entered so far\r
146 +                           (notmuch-address-harvest\r
147 +                            (format "to:%s*" arg) nil\r
148 +                            (lambda (_proc _event)\r
149 +                              (funcall callback (notmuch-address-matching arg))\r
150 +                              ;; Then (re)start potentially long-running full asynchronous harvesting\r
151 +                              (notmuch-address-harvest-trigger))))))))\r
152 +      (match (if (string-match notmuch-company-last-prefix arg)\r
153 +                (match-end 0)\r
154 +              0))\r
155 +      (no-cache t))))\r
156 +\r
157 +\r
158 +(provide 'notmuch-company)\r
159 diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el\r
160 index 6cc9656..c90381d 100644\r
161 --- a/emacs/notmuch-mua.el\r
162 +++ b/emacs/notmuch-mua.el\r
163 @@ -25,6 +25,7 @@\r
164  \r
165  (require 'notmuch-lib)\r
166  (require 'notmuch-address)\r
167 +(require 'notmuch-company)\r
168  \r
169  (eval-when-compile (require 'cl))\r
170  \r
171 @@ -268,12 +269,22 @@ Note that these functions use `mail-citation-hook' if that is non-nil."\r
172    (message-goto-body)\r
173    (set-buffer-modified-p nil))\r
174  \r
175 +(defcustom notmuch-message-use-company t\r
176 +  "If available, use company mode for completion in notmuch-message-mode"\r
177 +  :type 'boolean\r
178 +  :group 'notmuch-send)\r
179 +\r
180  (define-derived-mode notmuch-message-mode message-mode "Message[Notmuch]"\r
181    "Notmuch message composition mode. Mostly like `message-mode'"\r
182    (when notmuch-address-command\r
183      (unless (memq notmuch-address-message-alist-member message-completion-alist)\r
184        (setq message-completion-alist\r
185 -           (push notmuch-address-message-alist-member message-completion-alist)))))\r
186 +           (push notmuch-address-message-alist-member message-completion-alist))))\r
187 +  (when (and notmuch-message-use-company\r
188 +            (require 'company nil t))\r
189 +    (company-mode)\r
190 +    (make-local-variable 'company-backends)\r
191 +    (setq company-backends '(notmuch-company))))\r
192  \r
193  (define-key notmuch-message-mode-map (kbd "C-c C-c") #'notmuch-mua-send-and-exit)\r
194  (define-key notmuch-message-mode-map (kbd "C-c C-s") #'notmuch-mua-send)\r
195 -- \r
196 2.6.1\r
197 \r