Re: [PATCH v4 13/16] add indexopts to notmuch python bindings.
[notmuch-archives.git] / be / 0cf3276271bfb8a06353ff98524416338ff65b
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 DB45A6DE0222\r
6  for <notmuch@notmuchmail.org>; Mon, 16 May 2016 00:37:07 -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.311\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.311 tagged_above=-999 required=5 tests=[AWL=0.259,\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, RCVD_IN_DNSWL_LOW=-0.7,\r
14  RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001]\r
15  autolearn=disabled\r
16 Received: from arlo.cworth.org ([127.0.0.1])\r
17  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
18  with ESMTP id 6xuXdlzBDpkn for <notmuch@notmuchmail.org>;\r
19  Mon, 16 May 2016 00:36:59 -0700 (PDT)\r
20 Received: from mail-wm0-f51.google.com (mail-wm0-f51.google.com\r
21  [74.125.82.51]) by arlo.cworth.org (Postfix) with ESMTPS id D6FCD6DE01D0 for\r
22  <notmuch@notmuchmail.org>; Mon, 16 May 2016 00:36:58 -0700 (PDT)\r
23 Received: by mail-wm0-f51.google.com with SMTP id a17so121637055wme.0\r
24  for <notmuch@notmuchmail.org>; Mon, 16 May 2016 00:36:58 -0700 (PDT)\r
25 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
26  h=from:to:cc:subject:date:message-id:in-reply-to:references;\r
27  bh=MNzWU9q5T3VkW1ct7Ma7Q8aO356CL3GdW2ogBNbJDBk=;\r
28  b=tBJq3yTyddqToL45vgK5HIesf4KZta8L9CmGJ5DNqKv/5+qW5PR+vAOtmdcJG37Dbd\r
29  ydFHnDFJJxT/AD4RtReA2AB7Mtac5H8z9d4S4yG0cViuQqE3Le+p2zMghtO/Tk9lxqe2\r
30  BHosKGRjQSdoDCHIl/Q/CIkEWfGjcaepaknbPj0XkuF1xiL0A7Qwm6qPvmROHeoKX98d\r
31  /bnQeBE4jK9A1oTT4sJGokO+lwrwd0MWTGcoHjPUGY1rC+l52unL58haSz8e6bzFUnCD\r
32  uUY4I6uI6V/Ehu6EwQub/tb/XC7uT464z60sxTc6mBVFt+u8ynM3/T8Eiz7wB3mz3oNJ\r
33  HnZg==\r
34 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
35  d=1e100.net; s=20130820;\r
36  h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
37  :references;\r
38  bh=MNzWU9q5T3VkW1ct7Ma7Q8aO356CL3GdW2ogBNbJDBk=;\r
39  b=JCmSxgM6qvH3mAlqKdKLGpbqsim+CK72w6LnlQsmoWA0o9IoLvHCt+vDuB4fujlgjG\r
40  uMzaGpS+lLGFOkU0Cp9Wv28+o7Vcq+zxrIKUpXFL6hojhLa7VoxVz3qdsogTX6UimhZv\r
41  W8yYDwURAa4f3cFsyIaDOvtIwciLayCzklvJ1BjPA46cBcDAg7UYaCWXs94qd/N0T0So\r
42  nJH1tg/JQl27mGY4WpCyYsqnfXi5rpN6QcDdiaNPhyQFdEedrxQB5td4usJvKs7QLRnj\r
43  tNWv1xKRBFWW6dM4Rmzz0st6L6q2SgBxhMk5w3pnLP6Vi48VzKm3+h1ZyVlfZhdKlWLI\r
44  Mp/Q==\r
45 X-Gm-Message-State:\r
46  AOPr4FU9jVHxio2+3Cacu5FQoL4SOmfdxX8BD4TfSpun/95mg7rmwVrK1Ppxy3MG0zftKA==\r
47 X-Received: by 10.194.14.6 with SMTP id l6mr31907904wjc.48.1463384216658;\r
48  Mon, 16 May 2016 00:36:56 -0700 (PDT)\r
49 Received: from localhost (5751dfa2.skybroadband.com. [87.81.223.162])\r
50  by smtp.gmail.com with ESMTPSA id c85sm16379952wmd.0.2016.05.16.00.36.55\r
51  (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
52  Mon, 16 May 2016 00:36:56 -0700 (PDT)\r
53 From: Mark Walters <markwalters1009@gmail.com>\r
54 To: notmuch@notmuchmail.org\r
55 Subject: [PATCH v3] emacs: address completion,\r
56  allow sender/recipient and filters\r
57 Date: Mon, 16 May 2016 08:36:51 +0100\r
58 Message-Id: <1463384211-13086-1-git-send-email-markwalters1009@gmail.com>\r
59 X-Mailer: git-send-email 2.1.4\r
60 In-Reply-To: <87h9e11vq1.fsf@steelpick.2x.cz>\r
61 References: <87h9e11vq1.fsf@steelpick.2x.cz>\r
62 X-BeenThere: notmuch@notmuchmail.org\r
63 X-Mailman-Version: 2.1.20\r
64 Precedence: list\r
65 List-Id: "Use and development of the notmuch mail system."\r
66  <notmuch.notmuchmail.org>\r
67 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
68  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
69 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
70 List-Post: <mailto:notmuch@notmuchmail.org>\r
71 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
72 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
73  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
74 X-List-Received-Date: Mon, 16 May 2016 07:37:07 -0000\r
75 \r
76 This commit lets the user customize the address completion.\r
77 \r
78 The first change controls whether to build the address completion list\r
79 based on messages you have sent or you have received (the latter is\r
80 much faster).\r
81 \r
82 The second change add a possible filter query to limit the messages\r
83 used -- for example, setting this to date:1y..  would limit the\r
84 address completions to addresses used in the last year. This speeds up\r
85 the address harvest and may also make the search less cluttered as old\r
86 addresses may well no longer be valid.\r
87 ---\r
88 \r
89 This version uses the docstrings suggested my Michal (which are much\r
90 better than mine), and renames some variables, as he suggested, to\r
91 make the code clearer.\r
92 \r
93 I wondered about allowing the user to specify completion based on\r
94 message "sent or received", rather than either sent, or received, but\r
95 think that is adding too much mess. We could also allow completion\r
96 based on any messages in the database, which would the include\r
97 completion based on messages received via mailing lists or\r
98 distribution lists.\r
99 \r
100 I also note that if the user enters a bad query into the filter query\r
101 box (eg 6M.. rather than date:6M..) they may get an obscure error as\r
102 notmuch/xapian fails. I don't see a goo way round that so have left\r
103 that as a "don't do that" case.\r
104 \r
105 Best wishes\r
106 \r
107 Mark\r
108 \r
109 \r
110  emacs/notmuch-address.el | 119 ++++++++++++++++++++++++++++++++---------------\r
111  emacs/notmuch-company.el |   2 +-\r
112  2 files changed, 82 insertions(+), 39 deletions(-)\r
113 \r
114 diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el\r
115 index aafbe5f..3dc5da9 100644\r
116 --- a/emacs/notmuch-address.el\r
117 +++ b/emacs/notmuch-address.el\r
118 @@ -28,15 +28,51 @@\r
119  ;;\r
120  (declare-function company-manual-begin "company")\r
121  \r
122 -(defcustom notmuch-address-command 'internal\r
123 -  "The command which generates possible addresses. It must take a\r
124 -single argument and output a list of possible matches, one per\r
125 -line. The default value of `internal' uses built-in address\r
126 -completion."\r
127 +(defvar notmuch-address-last-harvest 0\r
128 +  "Time of last address harvest")\r
129 +\r
130 +(defvar notmuch-address-completions (make-hash-table :test 'equal)\r
131 +  "Hash of email addresses for completion during email composition.\r
132 +  This variable is set by calling `notmuch-address-harvest'.")\r
133 +\r
134 +(defvar notmuch-address-full-harvest-finished nil\r
135 +  "t indicates that full completion address harvesting has been\r
136 +finished")\r
137 +\r
138 +(defcustom notmuch-address-command '(sent nil)\r
139 +  "Determines how to generate address completion candidates.\r
140 +\r
141 +If it is a string then that string should be an external program\r
142 +which must take a single argument (searched string) and output a\r
143 +list of completion candidates, one per line.\r
144 +\r
145 +Alternatively, it can be a (non-nil) list, in which case internal\r
146 +completion is used; in this case the list should have form\r
147 +'(DIRECTION FILTER), where DIRECTION is either sent or received\r
148 +and specifies whether the candidates are searched in messages\r
149 +sent by the user or received by the user (note received by is\r
150 +much faster), and FILTER is either nil or a filter-string, such\r
151 +as \"date:1y..\" to append to the query.\r
152 +\r
153 +If this variable is nil then address completion is disabled."\r
154    :type '(radio\r
155 -         (const :tag "Use internal address completion" internal)\r
156 +         (list :tag "Use internal address completion"\r
157 +               (radio\r
158 +                :tag "Base completion on messages you have"\r
159 +                :value sent\r
160 +                (const :tag "sent" sent)\r
161 +                (const :tag "received" received))\r
162 +               (radio :tag "Filter messages used for completion"\r
163 +                      (const :tag "Use all messages" nil)\r
164 +                      (string :tag "Filter query")))\r
165           (const :tag "Disable address completion" nil)\r
166 -         (string :tag "Use external completion command" "notmuch-addresses"))\r
167 +         (string :tag "Use external completion command"))\r
168 +  ;; We override set so that we can clear the cache when this changes\r
169 +  :set (lambda (symbol value)\r
170 +        (set-default symbol value)\r
171 +        (setq notmuch-address-last-harvest 0)\r
172 +        (setq notmuch-address-completions (clrhash notmuch-address-completions))\r
173 +        (setq notmuch-address-full-harvest-finished nil))\r
174    :group 'notmuch-send\r
175    :group 'notmuch-external)\r
176  \r
177 @@ -51,17 +87,6 @@ to know how address selection is made by default."\r
178    :group 'notmuch-send\r
179    :group 'notmuch-external)\r
180  \r
181 -(defvar notmuch-address-last-harvest 0\r
182 -  "Time of last address harvest")\r
183 -\r
184 -(defvar notmuch-address-completions (make-hash-table :test 'equal)\r
185 -  "Hash of email addresses for completion during email composition.\r
186 -  This variable is set by calling `notmuch-address-harvest'.")\r
187 -\r
188 -(defvar notmuch-address-full-harvest-finished nil\r
189 -  "t indicates that full completion address harvesting has been\r
190 -finished")\r
191 -\r
192  (defun notmuch-address-selection-function (prompt collection initial-input)\r
193    "Call (`completing-read'\r
194        PROMPT COLLECTION nil nil INITIAL-INPUT 'notmuch-address-history)"\r
195 @@ -83,7 +108,8 @@ finished")\r
196  \r
197  (defun notmuch-address-setup ()\r
198    (let* ((use-company (and notmuch-address-use-company\r
199 -                          (eq notmuch-address-command 'internal)\r
200 +                          notmuch-address-command\r
201 +                          (listp notmuch-address-command)\r
202                            (require 'company nil t)))\r
203          (pair (cons notmuch-address-completion-headers-regexp\r
204                      (if use-company\r
205 @@ -111,11 +137,11 @@ The candidates are taken from `notmuch-address-completions'."\r
206  elisp-based implementation or older implementation requiring\r
207  external commands."\r
208    (cond\r
209 -   ((eq notmuch-address-command 'internal)\r
210 +   ((and notmuch-address-command (listp notmuch-address-command))\r
211      (when (not notmuch-address-full-harvest-finished)\r
212        ;; First, run quick synchronous harvest based on what the user\r
213        ;; entered so far\r
214 -      (notmuch-address-harvest (format "to:%s*" original) t))\r
215 +      (notmuch-address-harvest original t))\r
216      (prog1 (notmuch-address-matching original)\r
217        ;; Then start the (potentially long-running) full asynchronous harvest if necessary\r
218        (notmuch-address-harvest-trigger)))\r
219 @@ -191,32 +217,49 @@ external commands."\r
220  \r
221  The car is a partial harvest, and the cdr is a full harvest")\r
222  \r
223 -(defun notmuch-address-harvest (&optional filter-query synchronous callback)\r
224 -  "Collect addresses completion candidates. It queries the\r
225 -notmuch database for all messages sent by the user optionally\r
226 -matching FILTER-QUERY (if not nil). It collects the destination\r
227 -addresses from those messages and stores them in\r
228 -`notmuch-address-completions'. Address harvesting may take some\r
229 -time so the address collection runs asynchronously unless\r
230 -SYNCHRONOUS is t. In case of asynchronous execution, CALLBACK is\r
231 -called when harvesting finishes."\r
232 -  (let* ((from-me-query (mapconcat (lambda (x) (concat "from:" x)) (notmuch-user-emails) " or "))\r
233 -        (query (if filter-query\r
234 -                   (format "(%s) and (%s)" from-me-query filter-query)\r
235 -                 from-me-query))\r
236 +(defun notmuch-address-harvest (&optional addr-prefix synchronous callback)\r
237 +  "Collect addresses completion candidates.\r
238 +\r
239 +It queries the notmuch database for messages sent/received (as\r
240 +configured with `notmuch-address-command`) by the user, collects\r
241 +destination/source addresses from those messages and stores them\r
242 +in `notmuch-address-completions'.\r
243 +\r
244 +If ADDR-PREFIX is not nil, only messages with to/from addresses\r
245 +matching ADDR-PREFIX*' are queried.\r
246 +\r
247 +Address harvesting may take some time so the address collection runs\r
248 +asynchronously unless SYNCHRONOUS is t. In case of asynchronous\r
249 +execution, CALLBACK is called when harvesting finishes."\r
250 +\r
251 +  (let* ((sent (eq (car notmuch-address-command) 'sent))\r
252 +        (config-query (cadr notmuch-address-command))\r
253 +        (prefix-query (when addr-prefix\r
254 +                        (format "%s:%s*" (if sent "to" "from") addr-prefix)))\r
255 +        (from-or-to-me-query\r
256 +         (mapconcat (lambda (x)\r
257 +                      (concat (if sent "from:" "to:") x))\r
258 +                    (notmuch-user-emails) " or "))\r
259 +        (query (if (or prefix-query config-query)\r
260 +                   (concat (format "(%s)" from-or-to-me-query)\r
261 +                           (when prefix-query\r
262 +                             (format " and (%s)" prefix-query))\r
263 +                           (when config-query\r
264 +                             (format " and (%s)" config-query)))\r
265 +                 from-or-to-me-query))\r
266          (args `("address" "--format=sexp" "--format-version=2"\r
267 -                "--output=recipients"\r
268 +                ,(if sent "--output=recipients" "--output=sender")\r
269                  "--deduplicate=address"\r
270                  ,query)))\r
271      (if synchronous\r
272         (mapc #'notmuch-address-harvest-addr\r
273                                    (apply 'notmuch-call-notmuch-sexp args))\r
274        ;; Asynchronous\r
275 -      (let* ((current-proc (if filter-query\r
276 +      (let* ((current-proc (if addr-prefix\r
277                                (car notmuch-address-harvest-procs)\r
278                              (cdr notmuch-address-harvest-procs)))\r
279              (proc-name (format "notmuch-address-%s-harvest"\r
280 -                               (if filter-query "partial" "full")))\r
281 +                               (if addr-prefix "partial" "full")))\r
282              (proc-buf (concat " *" proc-name "*")))\r
283         ;; Kill any existing process\r
284         (when current-proc\r
285 @@ -228,7 +271,7 @@ called when harvesting finishes."\r
286                      args))\r
287         (set-process-filter current-proc 'notmuch-address-harvest-filter)\r
288         (set-process-query-on-exit-flag current-proc nil)\r
289 -       (if filter-query\r
290 +       (if addr-prefix\r
291             (setcar notmuch-address-harvest-procs current-proc)\r
292           (setcdr notmuch-address-harvest-procs current-proc)))))\r
293    ;; return value\r
294 diff --git a/emacs/notmuch-company.el b/emacs/notmuch-company.el\r
295 index b881d6d..dcb59cd 100644\r
296 --- a/emacs/notmuch-company.el\r
297 +++ b/emacs/notmuch-company.el\r
298 @@ -72,7 +72,7 @@\r
299                           (lambda (callback)\r
300                             ;; First run quick asynchronous harvest based on what the user entered so far\r
301                             (notmuch-address-harvest\r
302 -                            (format "to:%s*" arg) nil\r
303 +                            arg nil\r
304                              (lambda (_proc _event)\r
305                                (funcall callback (notmuch-address-matching arg))\r
306                                ;; Then start the (potentially long-running) full asynchronous harvest if necessary\r
307 -- \r
308 2.1.4\r
309 \r