Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / 38 / 99bc46e96117f77332eb34046d3caf3f382803
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 B15936DE00C9\r
6  for <notmuch@notmuchmail.org>; Fri, 10 Jun 2016 18:50:14 -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.011\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.011 tagged_above=-999 required=5\r
12  tests=[AWL=-0.000, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01]\r
13  autolearn=disabled\r
14 Received: from arlo.cworth.org ([127.0.0.1])\r
15  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
16  with ESMTP id NrBZv-_2V-KX for <notmuch@notmuchmail.org>;\r
17  Fri, 10 Jun 2016 18:50:05 -0700 (PDT)\r
18 Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
19  by arlo.cworth.org (Postfix) with ESMTPS id 4CCD46DE0032\r
20  for <notmuch@notmuchmail.org>; Fri, 10 Jun 2016 18:50:05 -0700 (PDT)\r
21 Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
22  (envelope-from <bremner@tethera.net>)\r
23  id 1bBY3X-0003Hb-JH; Fri, 10 Jun 2016 21:49:47 -0400\r
24 Received: (nullmailer pid 13212 invoked by uid 1000);\r
25  Sat, 11 Jun 2016 01:49:59 -0000\r
26 From: David Bremner <david@tethera.net>\r
27 To: David Bremner <david@tethera.net>\r
28 Cc: notmuch <notmuch@notmuchmail.org>, jani@nikula.org\r
29 Subject: [PATCH] WIP: regexp matching in 'subject' and 'from'\r
30 Date: Fri, 10 Jun 2016 22:49:54 -0300\r
31 Message-Id: <1465609794-13159-1-git-send-email-david@tethera.net>\r
32 X-Mailer: git-send-email 2.8.1\r
33 In-Reply-To: <1465525688-30913-1-git-send-email-david@tethera.net>\r
34 References: <1465525688-30913-1-git-send-email-david@tethera.net>\r
35 MIME-Version: 1.0\r
36 Content-Type: text/plain; charset=UTF-8\r
37 Content-Transfer-Encoding: 8bit\r
38 X-BeenThere: notmuch@notmuchmail.org\r
39 X-Mailman-Version: 2.1.20\r
40 Precedence: list\r
41 List-Id: "Use and development of the notmuch mail system."\r
42  <notmuch.notmuchmail.org>\r
43 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
44  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
45 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
46 List-Post: <mailto:notmuch@notmuchmail.org>\r
47 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
48 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
49  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
50 X-List-Received-Date: Sat, 11 Jun 2016 01:50:14 -0000\r
51 \r
52 the idea is that you can run\r
53 \r
54 % notmuch search re:subject:<your-favourite-regexp>\r
55 % notmuch search re:from:<your-favourite-regexp>'\r
56 \r
57 or\r
58 \r
59 % notmuch search subject:"your usual phrase search"\r
60 % notmuch search from:"usual phrase search"\r
61 \r
62 This should also work with bindings, since it extends the query parser.\r
63 \r
64 This is trivial to extend for other value slots, but currently the only\r
65 value slots are date, message_id, from, subject, and last_mod. Date is\r
66 already searchable, and message_id is not obviously useful to regex\r
67 match.\r
68 ---\r
69 \r
70 After some discussion on IRC, here is a version that uses a single re:\r
71 prefix internally, and some examples/tests of how that syntax would\r
72 work in practice.\r
73 \r
74  lib/Makefile.local        |   1 +\r
75  lib/database-private.h    |   1 +\r
76  lib/database.cc           |   5 ++\r
77  lib/regexp-fields.cc      | 117 ++++++++++++++++++++++++++++++++++++++++++++++\r
78  lib/regexp-fields.h       |  77 ++++++++++++++++++++++++++++++\r
79  test/T630-regexp-query.sh |  77 ++++++++++++++++++++++++++++++\r
80  6 files changed, 278 insertions(+)\r
81  create mode 100644 lib/regexp-fields.cc\r
82  create mode 100644 lib/regexp-fields.h\r
83  create mode 100755 test/T630-regexp-query.sh\r
84 \r
85 diff --git a/lib/Makefile.local b/lib/Makefile.local\r
86 index beb9635..68771e6 100644\r
87 --- a/lib/Makefile.local\r
88 +++ b/lib/Makefile.local\r
89 @@ -51,6 +51,7 @@ libnotmuch_cxx_srcs =         \\r
90         $(dir)/query.cc         \\r
91         $(dir)/query-fp.cc      \\r
92         $(dir)/config.cc        \\r
93 +       $(dir)/regexp-fields.cc     \\r
94         $(dir)/thread.cc\r
95  \r
96  libnotmuch_modules := $(libnotmuch_c_srcs:.c=.o) $(libnotmuch_cxx_srcs:.cc=.o)\r
97 diff --git a/lib/database-private.h b/lib/database-private.h\r
98 index ca71a92..900a989 100644\r
99 --- a/lib/database-private.h\r
100 +++ b/lib/database-private.h\r
101 @@ -186,6 +186,7 @@ struct _notmuch_database {\r
102  #if HAVE_XAPIAN_FIELD_PROCESSOR\r
103      Xapian::FieldProcessor *date_field_processor;\r
104      Xapian::FieldProcessor *query_field_processor;\r
105 +    Xapian::FieldProcessor *re_field_processor;\r
106  #endif\r
107      Xapian::ValueRangeProcessor *last_mod_range_processor;\r
108  };\r
109 diff --git a/lib/database.cc b/lib/database.cc\r
110 index afafe88..b52b62d 100644\r
111 --- a/lib/database.cc\r
112 +++ b/lib/database.cc\r
113 @@ -21,6 +21,7 @@\r
114  #include "database-private.h"\r
115  #include "parse-time-vrp.h"\r
116  #include "query-fp.h"\r
117 +#include "regexp-fields.h"\r
118  #include "string-util.h"\r
119  \r
120  #include <iostream>\r
121 @@ -1016,6 +1017,8 @@ notmuch_database_open_verbose (const char *path,\r
122         notmuch->query_parser->add_boolean_prefix("date", notmuch->date_field_processor);\r
123         notmuch->query_field_processor = new QueryFieldProcessor (*notmuch->query_parser, notmuch);\r
124         notmuch->query_parser->add_boolean_prefix("query", notmuch->query_field_processor);\r
125 +       notmuch->re_field_processor = new RegexpFieldProcessor (*notmuch->query_parser, notmuch);\r
126 +       notmuch->query_parser->add_boolean_prefix("re", notmuch->re_field_processor);\r
127  #endif\r
128         notmuch->last_mod_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_LAST_MOD, "lastmod:");\r
129  \r
130 @@ -1112,6 +1115,8 @@ notmuch_database_close (notmuch_database_t *notmuch)\r
131      notmuch->date_field_processor = NULL;\r
132      delete notmuch->query_field_processor;\r
133      notmuch->query_field_processor = NULL;\r
134 +    delete notmuch->re_field_processor;\r
135 +    notmuch->re_field_processor = NULL;\r
136  #endif\r
137  \r
138      return status;\r
139 diff --git a/lib/regexp-fields.cc b/lib/regexp-fields.cc\r
140 new file mode 100644\r
141 index 0000000..d9d1625\r
142 --- /dev/null\r
143 +++ b/lib/regexp-fields.cc\r
144 @@ -0,0 +1,117 @@\r
145 +/* query-fp.cc - "query:" field processor glue\r
146 + *\r
147 + * This file is part of notmuch.\r
148 + *\r
149 + * Copyright © 2015 Austin Clements\r
150 + * Copyright © 2016 David Bremner\r
151 + *\r
152 + * This program is free software: you can redistribute it and/or modify\r
153 + * it under the terms of the GNU General Public License as published by\r
154 + * the Free Software Foundation, either version 3 of the License, or\r
155 + * (at your option) any later version.\r
156 + *\r
157 + * This program is distributed in the hope that it will be useful,\r
158 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
159 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
160 + * GNU General Public License for more details.\r
161 + *\r
162 + * You should have received a copy of the GNU General Public License\r
163 + * along with this program.  If not, see https://www.gnu.org/licenses/ .\r
164 + *\r
165 + * Author: Austin Clements <aclements@csail.mit.edu>\r
166 + *                David Bremner <david@tethera.net>\r
167 + */\r
168 +\r
169 +#include "regexp-fields.h"\r
170 +#include "notmuch-private.h"\r
171 +\r
172 +#if HAVE_XAPIAN_FIELD_PROCESSOR\r
173 +RegexpPostingSource::RegexpPostingSource (Xapian::valueno slot, const std::string &regexp)\r
174 +    : slot_ (slot)\r
175 +{\r
176 +    int r = regcomp (&regexp_, regexp.c_str (), REG_EXTENDED | REG_NOSUB);\r
177 +\r
178 +    if (r != 0)\r
179 +       /* XXX Report a query syntax error using regerror */\r
180 +       throw "regcomp failed";\r
181 +}\r
182 +\r
183 +RegexpPostingSource::~RegexpPostingSource ()\r
184 +{\r
185 +    regfree (&regexp_);\r
186 +}\r
187 +\r
188 +void\r
189 +RegexpPostingSource::init (const Xapian::Database &db)\r
190 +{\r
191 +    db_ = db;\r
192 +    it_ = db_.valuestream_begin (slot_);\r
193 +    end_ = db.valuestream_end (slot_);\r
194 +    started_ = false;\r
195 +}\r
196 +\r
197 +Xapian::doccount\r
198 +RegexpPostingSource::get_termfreq_min () const\r
199 +{\r
200 +    return 0;\r
201 +}\r
202 +\r
203 +Xapian::doccount\r
204 +RegexpPostingSource::get_termfreq_est () const\r
205 +{\r
206 +    return get_termfreq_max () / 2;\r
207 +}\r
208 +\r
209 +Xapian::doccount\r
210 +RegexpPostingSource::get_termfreq_max () const\r
211 +{\r
212 +    return db_.get_value_freq (slot_);\r
213 +}\r
214 +\r
215 +Xapian::docid\r
216 +RegexpPostingSource::get_docid () const\r
217 +{\r
218 +    return it_.get_docid ();\r
219 +}\r
220 +\r
221 +bool\r
222 +RegexpPostingSource::at_end () const\r
223 +{\r
224 +    return it_ == end_;\r
225 +}\r
226 +\r
227 +void\r
228 +RegexpPostingSource::next (unused (double min_wt))\r
229 +{\r
230 +    if (started_ && ! at_end ())\r
231 +       ++it_;\r
232 +    started_ = true;\r
233 +\r
234 +    for (; ! at_end (); ++it_) {\r
235 +       std::string value = *it_;\r
236 +       if (regexec (&regexp_, value.c_str (), 0, NULL, 0) == 0)\r
237 +           break;\r
238 +    }\r
239 +}\r
240 +\r
241 +static Xapian::valueno\r
242 +_find_slot(std::string prefix){\r
243 +    if (prefix == "from")\r
244 +       return NOTMUCH_VALUE_FROM;\r
245 +    else if (prefix == "subject")\r
246 +       return NOTMUCH_VALUE_SUBJECT;\r
247 +    else\r
248 +       throw Xapian::QueryParserError ("unsupported regexp field '" + prefix + "'");\r
249 +}\r
250 +\r
251 +Xapian::Query\r
252 +RegexpFieldProcessor::operator() (const std::string & str)\r
253 +{\r
254 +    size_t pos = str.find_first_of(':');\r
255 +    std::string prefix = str.substr(0,pos);\r
256 +    std::string regexp = str.substr(pos+1);\r
257 +\r
258 +    postings = new RegexpPostingSource (_find_slot (prefix), regexp);\r
259 +    return Xapian::Query (postings);\r
260 +}\r
261 +#endif\r
262 diff --git a/lib/regexp-fields.h b/lib/regexp-fields.h\r
263 new file mode 100644\r
264 index 0000000..2c9c2d7\r
265 --- /dev/null\r
266 +++ b/lib/regexp-fields.h\r
267 @@ -0,0 +1,77 @@\r
268 +/* regex-fields.h - xapian glue for semi-bruteforce regexp search\r
269 + *\r
270 + * This file is part of notmuch.\r
271 + *\r
272 + * Copyright © 2015 Austin Clements\r
273 + * Copyright © 2016 David Bremner\r
274 + *\r
275 + * This program is free software: you can redistribute it and/or modify\r
276 + * it under the terms of the GNU General Public License as published by\r
277 + * the Free Software Foundation, either version 3 of the License, or\r
278 + * (at your option) any later version.\r
279 + *\r
280 + * This program is distributed in the hope that it will be useful,\r
281 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
282 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
283 + * GNU General Public License for more details.\r
284 + *\r
285 + * You should have received a copy of the GNU General Public License\r
286 + * along with this program.  If not, see https://www.gnu.org/licenses/ .\r
287 + *\r
288 + * Author: Austin Clements <aclements@csail.mit.edu>\r
289 + *                David Bremner <david@tethera.net>\r
290 + */\r
291 +\r
292 +#ifndef NOTMUCH_REGEXP_FIELDS_H\r
293 +#define NOTMUCH_REGEXP_FIELDS_H\r
294 +#if HAVE_XAPIAN_FIELD_PROCESSOR\r
295 +#include <sys/types.h>\r
296 +#include <regex.h>\r
297 +#include <xapian.h>\r
298 +#include "notmuch-private.h"\r
299 +\r
300 +/* A posting source that returns documents where a value matches a\r
301 + * regexp.\r
302 + */\r
303 +class RegexpPostingSource : public Xapian::PostingSource\r
304 +{\r
305 + protected:\r
306 +    const Xapian::valueno slot_;\r
307 +    regex_t regexp_;\r
308 +    Xapian::Database db_;\r
309 +    bool started_;\r
310 +    Xapian::ValueIterator it_, end_;\r
311 +\r
312 +/* No copying */\r
313 +    RegexpPostingSource (const RegexpPostingSource &);\r
314 +    RegexpPostingSource &operator= (const RegexpPostingSource &);\r
315 +\r
316 + public:\r
317 +    RegexpPostingSource (Xapian::valueno slot, const std::string &regexp);\r
318 +    ~RegexpPostingSource ();\r
319 +    void init (const Xapian::Database &db);\r
320 +    Xapian::doccount get_termfreq_min () const;\r
321 +    Xapian::doccount get_termfreq_est () const;\r
322 +    Xapian::doccount get_termfreq_max () const;\r
323 +    Xapian::docid get_docid () const;\r
324 +    bool at_end () const;\r
325 +    void next (unused (double min_wt));\r
326 +};\r
327 +\r
328 +\r
329 +class RegexpFieldProcessor : public Xapian::FieldProcessor {\r
330 + protected:\r
331 +    Xapian::QueryParser &parser;\r
332 +    notmuch_database_t *notmuch;\r
333 +    RegexpPostingSource *postings = NULL;\r
334 +\r
335 + public:\r
336 +    RegexpFieldProcessor (Xapian::QueryParser &parser_, notmuch_database_t *notmuch_)\r
337 +       : parser(parser_), notmuch(notmuch_) { };\r
338 +\r
339 +    ~RegexpFieldProcessor () { delete postings; };\r
340 +\r
341 +    Xapian::Query operator()(const std::string & str);\r
342 +};\r
343 +#endif\r
344 +#endif /* NOTMUCH_REGEXP_FIELDS_H */\r
345 diff --git a/test/T630-regexp-query.sh b/test/T630-regexp-query.sh\r
346 new file mode 100755\r
347 index 0000000..09caed6\r
348 --- /dev/null\r
349 +++ b/test/T630-regexp-query.sh\r
350 @@ -0,0 +1,77 @@\r
351 +#!/usr/bin/env bash\r
352 +test_description='named queries'\r
353 +. ./test-lib.sh || exit 1\r
354 +\r
355 +QUERYSTR="date:2009-11-18..2009-11-18 and tag:unread"\r
356 +QUERYSTR2="query:test and subject:Maildir"\r
357 +\r
358 +add_email_corpus\r
359 +\r
360 +\r
361 +if [ $NOTMUCH_HAVE_XAPIAN_FIELD_PROCESSOR -eq 1 ]; then\r
362 +\r
363 +    notmuch search --output=messages from:cworth > cworth.msg-ids\r
364 +\r
365 +    test_begin_subtest "regexp from search, case sensitive"\r
366 +    notmuch search --output=messages re:from:carl > OUTPUT\r
367 +    test_expect_equal_file /dev/null OUTPUT\r
368 +\r
369 +    test_begin_subtest "empty regexp or query"\r
370 +    notmuch search --output=messages re:from:carl or from:cworth > OUTPUT\r
371 +    test_expect_equal_file cworth.msg-ids OUTPUT\r
372 +\r
373 +    test_begin_subtest "non-empty regexp and query"\r
374 +    notmuch search  re:from:cworth and subject:patch > OUTPUT\r
375 +    cat <<EOF > EXPECTED\r
376 +thread:0000000000000008   2009-11-18 [1/2] Carl Worth| Alex Botero-Lowry; [notmuch] [PATCH] Error out if no query is supplied to search instead of going into an infinite loop (attachment inbox unread)\r
377 +thread:0000000000000007   2009-11-18 [1/2] Carl Worth| Ingmar Vanhassel; [notmuch] [PATCH] Typsos (inbox unread)\r
378 +thread:0000000000000018   2009-11-18 [1/2] Carl Worth| Jan Janak; [notmuch] [PATCH] Older versions of install do not support -C. (inbox unread)\r
379 +thread:0000000000000017   2009-11-18 [1/2] Carl Worth| Keith Packard; [notmuch] [PATCH] Make notmuch-show 'X' (and 'x') commands remove inbox (and unread) tags (inbox unread)\r
380 +thread:0000000000000014   2009-11-18 [2/5] Carl Worth| Mikhail Gusarov, Keith Packard; [notmuch] [PATCH 1/2] Close message file after parsing message headers (inbox unread)\r
381 +thread:0000000000000001   2009-11-18 [1/1] Stewart Smith; [notmuch] [PATCH] Fix linking with gcc to use g++ to link in C++ libs. (inbox unread)\r
382 +EOF\r
383 +    test_expect_equal_file EXPECTED OUTPUT\r
384 +\r
385 +    test_begin_subtest "regexp from search, duplicate term search"\r
386 +    notmuch search --output=messages re:from:cworth > OUTPUT\r
387 +    test_expect_equal_file cworth.msg-ids OUTPUT\r
388 +\r
389 +    test_begin_subtest "long enough regexp matches only desired senders"\r
390 +    notmuch search --output=messages 're:"from:C.* Wo"' > OUTPUT\r
391 +    test_expect_equal_file cworth.msg-ids OUTPUT\r
392 +\r
393 +    test_begin_subtest "shorter regexp matches one more sender"\r
394 +    notmuch search --output=messages 're:"from:C.* W"' > OUTPUT\r
395 +    (echo id:1258544095-16616-1-git-send-email-chris@chris-wilson.co.uk ; cat cworth.msg-ids) > EXPECTED\r
396 +    test_expect_equal_file EXPECTED OUTPUT\r
397 +\r
398 +    test_begin_subtest "regexp subject search, non-ASCII"\r
399 +    notmuch search --output=messages re:subject:accentué > OUTPUT\r
400 +    echo id:877h1wv7mg.fsf@inf-8657.int-evry.fr > EXPECTED\r
401 +    test_expect_equal_file EXPECTED OUTPUT\r
402 +\r
403 +    test_begin_subtest "regexp subject search, punctuation"\r
404 +    notmuch search   re:subject:\'X\' > OUTPUT\r
405 +    cat <<EOF > EXPECTED\r
406 +thread:0000000000000017   2009-11-18 [2/2] Keith Packard, Carl Worth; [notmuch] [PATCH] Make notmuch-show 'X' (and 'x') commands remove inbox (and unread) tags (inbox unread)\r
407 +EOF\r
408 +    test_expect_equal_file EXPECTED OUTPUT\r
409 +\r
410 +    test_begin_subtest "regexp subject search, no punctuation"\r
411 +    notmuch search  re:subject:X > OUTPUT\r
412 +    cat <<EOF > EXPECTED\r
413 +thread:0000000000000017   2009-11-18 [2/2] Keith Packard, Carl Worth; [notmuch] [PATCH] Make notmuch-show 'X' (and 'x') commands remove inbox (and unread) tags (inbox unread)\r
414 +thread:000000000000000f   2009-11-18 [4/4] Jjgod Jiang, Alexander Botero-Lowry; [notmuch] Mac OS X/Darwin compatibility issues (inbox unread)\r
415 +EOF\r
416 +    test_expect_equal_file EXPECTED OUTPUT\r
417 +\r
418 +    test_begin_subtest "combine regexp from and subject"\r
419 +    notmuch search  re:subject:-C and re:from:.an.k > OUTPUT\r
420 +    cat <<EOF > EXPECTED\r
421 +thread:0000000000000018   2009-11-17 [1/2] Jan Janak| Carl Worth; [notmuch] [PATCH] Older versions of install do not support -C. (inbox unread)\r
422 +EOF\r
423 +    test_expect_equal_file EXPECTED OUTPUT\r
424 +\r
425 +fi\r
426 +\r
427 +test_done\r
428 -- \r
429 2.8.1\r
430 \r