Re: [PATCH v4 13/16] add indexopts to notmuch python bindings.
[notmuch-archives.git] / f8 / 5bcb7acf8c2f2d4588fdbbdc595e01af5161b7
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 BF02F6DE01D0\r
6  for <notmuch@notmuchmail.org>; Wed, 18 May 2016 01:07: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.314\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.314 tagged_above=-999 required=5 tests=[AWL=0.256,\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 w-WIRrvSxEKO for <notmuch@notmuchmail.org>;\r
19  Wed, 18 May 2016 01:06:59 -0700 (PDT)\r
20 Received: from mail-wm0-f54.google.com (mail-wm0-f54.google.com\r
21  [74.125.82.54]) by arlo.cworth.org (Postfix) with ESMTPS id 676066DE00C9 for\r
22  <notmuch@notmuchmail.org>; Wed, 18 May 2016 01:06:59 -0700 (PDT)\r
23 Received: by mail-wm0-f54.google.com with SMTP id r12so21948814wme.0\r
24  for <notmuch@notmuchmail.org>; Wed, 18 May 2016 01:06:59 -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=Dc0/0u2ETijKb31HDOxG+HfsXsWk157hozviNy5XbfM=;\r
28  b=QtRV4BlVEH5B1u8DLiV15u41yLt7kSy/0/mmNJZp2p4UDn/MNq6ERZo8jVbzryGSPN\r
29  YgCZL8SptJfVCieuxkFgvYj/phazJScYYPEBf4ATMHI56qeSfgNzLkp3Mcb12n8E21Xg\r
30  dMq6inbKcytg1uwyR9TN76+MY5HvqkOMXUyO1gw4mP4SLXt5vHnQQXf/QzkoRUIIvNzb\r
31  NA1Yr0MultBusOWEnIZIDxw/dlNOQ5mxm7u8kdovipUXrqFSF/qymqqxFFNxb3KUPgK0\r
32  mw27ZIG0ICPd9Z4sakFUubMDVlxs2D1NxSkK38j3JhvtpbhcMUM1aLi15cdH1cuEUHIB\r
33  uYdA==\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=Dc0/0u2ETijKb31HDOxG+HfsXsWk157hozviNy5XbfM=;\r
39  b=iJO7EMLipSBjtG/Gs+ISANB+KoplyvJOauBJ5467o5o+XaFlX7qY0UTkqE1PT0k+rR\r
40  M3+jaMdlKmxwFa7lLMUakiXvwydILbHC+kYfTPRLMJdFdCQjr5yJ5Hcvea5DQ654L/zX\r
41  gEodm+tVROtc1+SNm3nsexfJtJ/CSGSpgi+eKt9WRlOLuHk322nS6BAr+B22DA0FaLak\r
42  whnCGevY52ZbziC2rbjvmFi0GEkedhfHa+sSnTD9syZCyYji7MMD/oKUoHqcycG+Mmkr\r
43  mQ3NeudNAHCxKRS98HBLLfSwOaqmK+xZfjAvlg7NhNTUR2Q6juT2UOthCk7MyLVQBK/D\r
44  lUaw==\r
45 X-Gm-Message-State:\r
46  AOPr4FX61uQRC2RwiLYLTDKrMOFzpXn/ijxqXh79KG0vGW2F/eK2mzJVP18BztIxHsYNwQ==\r
47 X-Received: by 10.194.59.108 with SMTP id y12mr5506253wjq.63.1463558817379;\r
48  Wed, 18 May 2016 01:06:57 -0700 (PDT)\r
49 Received: from localhost (5751dfa2.skybroadband.com. [87.81.223.162])\r
50  by smtp.gmail.com with ESMTPSA id w10sm6400427wjk.18.2016.05.18.01.06.56\r
51  (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
52  Wed, 18 May 2016 01:06:56 -0700 (PDT)\r
53 From: Mark Walters <markwalters1009@gmail.com>\r
54 To: notmuch@notmuchmail.org\r
55 Subject: [PATCH] emacs: address completion, allow sender/recipient and filters\r
56 Date: Wed, 18 May 2016 09:06:53 +0100\r
57 Message-Id: <1463558813-23603-1-git-send-email-markwalters1009@gmail.com>\r
58 X-Mailer: git-send-email 2.1.4\r
59 In-Reply-To: <8737pg99zz.fsf@steelpick.2x.cz>\r
60 References: <8737pg99zz.fsf@steelpick.2x.cz>\r
61 X-BeenThere: notmuch@notmuchmail.org\r
62 X-Mailman-Version: 2.1.20\r
63 Precedence: list\r
64 List-Id: "Use and development of the notmuch mail system."\r
65  <notmuch.notmuchmail.org>\r
66 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
67  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
68 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
69 List-Post: <mailto:notmuch@notmuchmail.org>\r
70 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
71 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
72  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
73 X-List-Received-Date: Wed, 18 May 2016 08:07:07 -0000\r
74 \r
75 This commit lets the user customize the address completion. It makes\r
76 two changes.\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 tweaks the commit message and the two docstrings as Michal\r
90 suggested, but is otherwise unchanged.\r
91 \r
92 Best wishes\r
93 \r
94 Mark\r
95 \r
96 emacs/notmuch-address.el | 119 ++++++++++++++++++++++++++++++++---------------\r
97  emacs/notmuch-company.el |   2 +-\r
98  2 files changed, 82 insertions(+), 39 deletions(-)\r
99 \r
100 diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el\r
101 index aafbe5f..62df8ba 100644\r
102 --- a/emacs/notmuch-address.el\r
103 +++ b/emacs/notmuch-address.el\r
104 @@ -28,15 +28,51 @@\r
105  ;;\r
106  (declare-function company-manual-begin "company")\r
107  \r
108 -(defcustom notmuch-address-command 'internal\r
109 -  "The command which generates possible addresses. It must take a\r
110 -single argument and output a list of possible matches, one per\r
111 -line. The default value of `internal' uses built-in address\r
112 -completion."\r
113 +(defvar notmuch-address-last-harvest 0\r
114 +  "Time of last address harvest")\r
115 +\r
116 +(defvar notmuch-address-completions (make-hash-table :test 'equal)\r
117 +  "Hash of email addresses for completion during email composition.\r
118 +  This variable is set by calling `notmuch-address-harvest'.")\r
119 +\r
120 +(defvar notmuch-address-full-harvest-finished nil\r
121 +  "t indicates that full completion address harvesting has been\r
122 +finished")\r
123 +\r
124 +(defcustom notmuch-address-command '(sent nil)\r
125 +  "Determines how to generate address completion candidates.\r
126 +\r
127 +If it is a string then that string should be an external program\r
128 +which must take a single argument (searched string) and output a\r
129 +list of completion candidates, one per line.\r
130 +\r
131 +Alternatively, it can be a (non-nil) list, in which case internal\r
132 +completion is used; in this case the list should have form\r
133 +'(DIRECTION FILTER), where DIRECTION is either sent or received\r
134 +and specifies whether the candidates are searched in messages\r
135 +sent by the user or received by the user (note received by is\r
136 +much faster), and FILTER is either nil or a filter-string, such\r
137 +as \"date:1y..\" to append to the query.\r
138 +\r
139 +If this variable is nil then address completion is disabled."\r
140    :type '(radio\r
141 -         (const :tag "Use internal address completion" internal)\r
142 +         (list :tag "Use internal address completion"\r
143 +               (radio\r
144 +                :tag "Base completion on messages you have"\r
145 +                :value sent\r
146 +                (const :tag "sent (more accurate)" sent)\r
147 +                (const :tag "received (faster)" received))\r
148 +               (radio :tag "Filter messages used for completion"\r
149 +                      (const :tag "Use all messages" nil)\r
150 +                      (string :tag "Filter query")))\r
151           (const :tag "Disable address completion" nil)\r
152 -         (string :tag "Use external completion command" "notmuch-addresses"))\r
153 +         (string :tag "Use external completion command"))\r
154 +  ;; We override set so that we can clear the cache when this changes\r
155 +  :set (lambda (symbol value)\r
156 +        (set-default symbol value)\r
157 +        (setq notmuch-address-last-harvest 0)\r
158 +        (setq notmuch-address-completions (clrhash notmuch-address-completions))\r
159 +        (setq notmuch-address-full-harvest-finished nil))\r
160    :group 'notmuch-send\r
161    :group 'notmuch-external)\r
162  \r
163 @@ -51,17 +87,6 @@ to know how address selection is made by default."\r
164    :group 'notmuch-send\r
165    :group 'notmuch-external)\r
166  \r
167 -(defvar notmuch-address-last-harvest 0\r
168 -  "Time of last address harvest")\r
169 -\r
170 -(defvar notmuch-address-completions (make-hash-table :test 'equal)\r
171 -  "Hash of email addresses for completion during email composition.\r
172 -  This variable is set by calling `notmuch-address-harvest'.")\r
173 -\r
174 -(defvar notmuch-address-full-harvest-finished nil\r
175 -  "t indicates that full completion address harvesting has been\r
176 -finished")\r
177 -\r
178  (defun notmuch-address-selection-function (prompt collection initial-input)\r
179    "Call (`completing-read'\r
180        PROMPT COLLECTION nil nil INITIAL-INPUT 'notmuch-address-history)"\r
181 @@ -83,7 +108,8 @@ finished")\r
182  \r
183  (defun notmuch-address-setup ()\r
184    (let* ((use-company (and notmuch-address-use-company\r
185 -                          (eq notmuch-address-command 'internal)\r
186 +                          notmuch-address-command\r
187 +                          (listp notmuch-address-command)\r
188                            (require 'company nil t)))\r
189          (pair (cons notmuch-address-completion-headers-regexp\r
190                      (if use-company\r
191 @@ -111,11 +137,11 @@ The candidates are taken from `notmuch-address-completions'."\r
192  elisp-based implementation or older implementation requiring\r
193  external commands."\r
194    (cond\r
195 -   ((eq notmuch-address-command 'internal)\r
196 +   ((and notmuch-address-command (listp notmuch-address-command))\r
197      (when (not notmuch-address-full-harvest-finished)\r
198        ;; First, run quick synchronous harvest based on what the user\r
199        ;; entered so far\r
200 -      (notmuch-address-harvest (format "to:%s*" original) t))\r
201 +      (notmuch-address-harvest original t))\r
202      (prog1 (notmuch-address-matching original)\r
203        ;; Then start the (potentially long-running) full asynchronous harvest if necessary\r
204        (notmuch-address-harvest-trigger)))\r
205 @@ -191,32 +217,49 @@ external commands."\r
206  \r
207  The car is a partial harvest, and the cdr is a full harvest")\r
208  \r
209 -(defun notmuch-address-harvest (&optional filter-query synchronous callback)\r
210 -  "Collect addresses completion candidates. It queries the\r
211 -notmuch database for all messages sent by the user optionally\r
212 -matching FILTER-QUERY (if not nil). It collects the destination\r
213 -addresses from those messages and stores them in\r
214 -`notmuch-address-completions'. Address harvesting may take some\r
215 -time so the address collection runs asynchronously unless\r
216 -SYNCHRONOUS is t. In case of asynchronous execution, CALLBACK is\r
217 -called when harvesting finishes."\r
218 -  (let* ((from-me-query (mapconcat (lambda (x) (concat "from:" x)) (notmuch-user-emails) " or "))\r
219 -        (query (if filter-query\r
220 -                   (format "(%s) and (%s)" from-me-query filter-query)\r
221 -                 from-me-query))\r
222 +(defun notmuch-address-harvest (&optional addr-prefix synchronous callback)\r
223 +  "Collect addresses completion candidates.\r
224 +\r
225 +It queries the notmuch database for messages sent/received (as\r
226 +configured with `notmuch-address-command`) by the user, collects\r
227 +destination/source addresses from those messages and stores them\r
228 +in `notmuch-address-completions'.\r
229 +\r
230 +If ADDR-PREFIX is not nil, only messages with to/from addresses\r
231 +matching ADDR-PREFIX*' are queried.\r
232 +\r
233 +Address harvesting may take some time so the address collection runs\r
234 +asynchronously unless SYNCHRONOUS is t. In case of asynchronous\r
235 +execution, CALLBACK is called when harvesting finishes."\r
236 +\r
237 +  (let* ((sent (eq (car notmuch-address-command) 'sent))\r
238 +        (config-query (cadr notmuch-address-command))\r
239 +        (prefix-query (when addr-prefix\r
240 +                        (format "%s:%s*" (if sent "to" "from") addr-prefix)))\r
241 +        (from-or-to-me-query\r
242 +         (mapconcat (lambda (x)\r
243 +                      (concat (if sent "from:" "to:") x))\r
244 +                    (notmuch-user-emails) " or "))\r
245 +        (query (if (or prefix-query config-query)\r
246 +                   (concat (format "(%s)" from-or-to-me-query)\r
247 +                           (when prefix-query\r
248 +                             (format " and (%s)" prefix-query))\r
249 +                           (when config-query\r
250 +                             (format " and (%s)" config-query)))\r
251 +                 from-or-to-me-query))\r
252          (args `("address" "--format=sexp" "--format-version=2"\r
253 -                "--output=recipients"\r
254 +                ,(if sent "--output=recipients" "--output=sender")\r
255                  "--deduplicate=address"\r
256                  ,query)))\r
257      (if synchronous\r
258         (mapc #'notmuch-address-harvest-addr\r
259                                    (apply 'notmuch-call-notmuch-sexp args))\r
260        ;; Asynchronous\r
261 -      (let* ((current-proc (if filter-query\r
262 +      (let* ((current-proc (if addr-prefix\r
263                                (car notmuch-address-harvest-procs)\r
264                              (cdr notmuch-address-harvest-procs)))\r
265              (proc-name (format "notmuch-address-%s-harvest"\r
266 -                               (if filter-query "partial" "full")))\r
267 +                               (if addr-prefix "partial" "full")))\r
268              (proc-buf (concat " *" proc-name "*")))\r
269         ;; Kill any existing process\r
270         (when current-proc\r
271 @@ -228,7 +271,7 @@ called when harvesting finishes."\r
272                      args))\r
273         (set-process-filter current-proc 'notmuch-address-harvest-filter)\r
274         (set-process-query-on-exit-flag current-proc nil)\r
275 -       (if filter-query\r
276 +       (if addr-prefix\r
277             (setcar notmuch-address-harvest-procs current-proc)\r
278           (setcdr notmuch-address-harvest-procs current-proc)))))\r
279    ;; return value\r
280 diff --git a/emacs/notmuch-company.el b/emacs/notmuch-company.el\r
281 index b881d6d..dcb59cd 100644\r
282 --- a/emacs/notmuch-company.el\r
283 +++ b/emacs/notmuch-company.el\r
284 @@ -72,7 +72,7 @@\r
285                           (lambda (callback)\r
286                             ;; First run quick asynchronous harvest based on what the user entered so far\r
287                             (notmuch-address-harvest\r
288 -                            (format "to:%s*" arg) nil\r
289 +                            arg nil\r
290                              (lambda (_proc _event)\r
291                                (funcall callback (notmuch-address-matching arg))\r
292                                ;; Then start the (potentially long-running) full asynchronous harvest if necessary\r
293 -- \r
294 2.1.4\r
295 \r