Re: [PATCH] Fix typo in Message.maildir_flags_to_tags
[notmuch-archives.git] / 85 / cc3f0f42b6209c843edeccb46484b58aa94bc4
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 D2D8C6DE0928\r
6  for <notmuch@notmuchmail.org>; Wed, 25 May 2016 03:51:55 -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.012\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.012 tagged_above=-999 required=5\r
12  tests=[AWL=-0.001, 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 iQ9ts41iEvXL for <notmuch@notmuchmail.org>;\r
17  Wed, 25 May 2016 03:51:46 -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 002676DE01BE\r
20  for <notmuch@notmuchmail.org>; Wed, 25 May 2016 03:51:36 -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 1b5WPP-0004Tz-IV; Wed, 25 May 2016 06:51:27 -0400\r
24 Received: (nullmailer pid 23150 invoked by uid 1000);\r
25  Wed, 25 May 2016 10:51:31 -0000\r
26 From: David Bremner <david@tethera.net>\r
27 To: notmuch@notmuchmail.org\r
28 Subject: [Patch v6 7/7] lib: add support for named queries\r
29 Date: Wed, 25 May 2016 07:51:26 -0300\r
30 Message-Id: <1464173486-23032-8-git-send-email-david@tethera.net>\r
31 X-Mailer: git-send-email 2.8.1\r
32 In-Reply-To: <1464173486-23032-1-git-send-email-david@tethera.net>\r
33 References: <1464173486-23032-1-git-send-email-david@tethera.net>\r
34 MIME-Version: 1.0\r
35 Content-Type: text/plain; charset=UTF-8\r
36 Content-Transfer-Encoding: 8bit\r
37 X-BeenThere: notmuch@notmuchmail.org\r
38 X-Mailman-Version: 2.1.20\r
39 Precedence: list\r
40 List-Id: "Use and development of the notmuch mail system."\r
41  <notmuch.notmuchmail.org>\r
42 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
43  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
44 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
45 List-Post: <mailto:notmuch@notmuchmail.org>\r
46 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
47 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
48  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
49 X-List-Received-Date: Wed, 25 May 2016 10:51:55 -0000\r
50 \r
51 This relies on the optional presense of xapian field processors, and the\r
52 library config API.\r
53 ---\r
54  doc/man7/notmuch-search-terms.rst |  8 ++++++++\r
55  lib/Makefile.local                |  1 +\r
56  lib/database-private.h            |  1 +\r
57  lib/database.cc                   |  3 +++\r
58  lib/query-fp.cc                   | 43 +++++++++++++++++++++++++++++++++++++++\r
59  lib/query-fp.h                    | 42 ++++++++++++++++++++++++++++++++++++++\r
60  test/T600-named-queries.sh        | 17 ++++++++++++++++\r
61  7 files changed, 115 insertions(+)\r
62  create mode 100644 lib/query-fp.cc\r
63  create mode 100644 lib/query-fp.h\r
64 \r
65 diff --git a/doc/man7/notmuch-search-terms.rst b/doc/man7/notmuch-search-terms.rst\r
66 index adedf5a..223031b 100644\r
67 --- a/doc/man7/notmuch-search-terms.rst\r
68 +++ b/doc/man7/notmuch-search-terms.rst\r
69 @@ -56,6 +56,8 @@ indicate user-supplied values):\r
70  \r
71  -  lastmod:<initial-revision>..<final-revision>\r
72  \r
73 +-  query:<name>\r
74 +\r
75  The **from:** prefix is used to match the name or address of the sender\r
76  of an email message.\r
77  \r
78 @@ -132,6 +134,11 @@ were added/removed or filenames changed).  This is usually used in\r
79  conjunction with the **--uuid** argument to **notmuch search**\r
80  to find messages that have changed since an earlier query.\r
81  \r
82 +The **query:** prefix allows queries to refer to previously saved\r
83 +queries added with **notmuch-config(1)**. Named queries are only\r
84 +available if notmuch is built with **Xapian Field Processors** (see\r
85 +below).\r
86 +\r
87  Operators\r
88  ---------\r
89  \r
90 @@ -385,6 +392,7 @@ notmuch was built against a sufficiently recent version of Xapian by running\r
91  Currently the following features require field processor support:\r
92  \r
93  - non-range date queries, e.g. "date:today"\r
94 +- named queries e.g. "query:my_special_query"\r
95  \r
96  SEE ALSO\r
97  ========\r
98 diff --git a/lib/Makefile.local b/lib/Makefile.local\r
99 index 76b57cb..beb9635 100644\r
100 --- a/lib/Makefile.local\r
101 +++ b/lib/Makefile.local\r
102 @@ -49,6 +49,7 @@ libnotmuch_cxx_srcs =         \\r
103         $(dir)/index.cc         \\r
104         $(dir)/message.cc       \\r
105         $(dir)/query.cc         \\r
106 +       $(dir)/query-fp.cc      \\r
107         $(dir)/config.cc        \\r
108         $(dir)/thread.cc\r
109  \r
110 diff --git a/lib/database-private.h b/lib/database-private.h\r
111 index d2990b6..1a78b60 100644\r
112 --- a/lib/database-private.h\r
113 +++ b/lib/database-private.h\r
114 @@ -185,6 +185,7 @@ struct _notmuch_database {\r
115      Xapian::ValueRangeProcessor *date_range_processor;\r
116  #if HAVE_XAPIAN_FIELD_PROCESSOR\r
117      Xapian::FieldProcessor *date_field_processor;\r
118 +    Xapian::FieldProcessor *query_field_processor;\r
119  #endif\r
120      Xapian::ValueRangeProcessor *last_mod_range_processor;\r
121  };\r
122 diff --git a/lib/database.cc b/lib/database.cc\r
123 index ebe019f..9630000 100644\r
124 --- a/lib/database.cc\r
125 +++ b/lib/database.cc\r
126 @@ -20,6 +20,7 @@\r
127  \r
128  #include "database-private.h"\r
129  #include "parse-time-vrp.h"\r
130 +#include "query-fp.h"\r
131  #include "string-util.h"\r
132  \r
133  #include <iostream>\r
134 @@ -1005,6 +1006,8 @@ notmuch_database_open_verbose (const char *path,\r
135          * with a .. to the range processor */\r
136         notmuch->date_field_processor = new DateFieldProcessor();\r
137         notmuch->query_parser->add_boolean_prefix("date", notmuch->date_field_processor);\r
138 +       notmuch->query_field_processor = new QueryFieldProcessor (*notmuch->query_parser, notmuch);\r
139 +       notmuch->query_parser->add_boolean_prefix("query", notmuch->query_field_processor);\r
140  #endif\r
141         notmuch->last_mod_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_LAST_MOD, "lastmod:");\r
142  \r
143 diff --git a/lib/query-fp.cc b/lib/query-fp.cc\r
144 new file mode 100644\r
145 index 0000000..4ffcb1e\r
146 --- /dev/null\r
147 +++ b/lib/query-fp.cc\r
148 @@ -0,0 +1,43 @@\r
149 +/* query-fp.cc - "query:" field processor glue\r
150 + *\r
151 + * This file is part of notmuch.\r
152 + *\r
153 + * Copyright © 2016 David Bremner\r
154 + *\r
155 + * This program is free software: you can redistribute it and/or modify\r
156 + * it under the terms of the GNU General Public License as published by\r
157 + * the Free Software Foundation, either version 3 of the License, or\r
158 + * (at your option) any later version.\r
159 + *\r
160 + * This program is distributed in the hope that it will be useful,\r
161 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
162 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
163 + * GNU General Public License for more details.\r
164 + *\r
165 + * You should have received a copy of the GNU General Public License\r
166 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
167 + *\r
168 + * Author: David Bremner <david@tethera.net>\r
169 + */\r
170 +\r
171 +#include "database-private.h"\r
172 +#include "query-fp.h"\r
173 +#include <iostream>\r
174 +\r
175 +#if HAVE_XAPIAN_FIELD_PROCESSOR\r
176 +\r
177 +Xapian::Query\r
178 +QueryFieldProcessor::operator() (const std::string & name)\r
179 +{\r
180 +    std::string key = "query." + name;\r
181 +    char *expansion;\r
182 +    notmuch_status_t status;\r
183 +\r
184 +    status = notmuch_database_get_config (notmuch, key.c_str (), &expansion);\r
185 +    if (status) {\r
186 +       throw Xapian::QueryParserError ("error looking up key" + name);\r
187 +    }\r
188 +\r
189 +    return parser.parse_query (expansion, NOTMUCH_QUERY_PARSER_FLAGS);\r
190 +}\r
191 +#endif\r
192 diff --git a/lib/query-fp.h b/lib/query-fp.h\r
193 new file mode 100644\r
194 index 0000000..67f8705\r
195 --- /dev/null\r
196 +++ b/lib/query-fp.h\r
197 @@ -0,0 +1,42 @@\r
198 +/* query-fp.h - query field processor glue\r
199 + *\r
200 + * This file is part of notmuch.\r
201 + *\r
202 + * Copyright © 2016 David Bremner\r
203 + *\r
204 + * This program is free software: you can redistribute it and/or modify\r
205 + * it under the terms of the GNU General Public License as published by\r
206 + * the Free Software Foundation, either version 3 of the License, or\r
207 + * (at your option) any later version.\r
208 + *\r
209 + * This program is distributed in the hope that it will be useful,\r
210 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
211 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
212 + * GNU General Public License for more details.\r
213 + *\r
214 + * You should have received a copy of the GNU General Public License\r
215 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
216 + *\r
217 + * Author: David Bremner <david@tethera.net>\r
218 + */\r
219 +\r
220 +#ifndef NOTMUCH_QUERY_FP_H\r
221 +#define NOTMUCH_QUERY_FP_H\r
222 +\r
223 +#include <xapian.h>\r
224 +#include "notmuch.h"\r
225 +\r
226 +#if HAVE_XAPIAN_FIELD_PROCESSOR\r
227 +class QueryFieldProcessor : public Xapian::FieldProcessor {\r
228 + protected:\r
229 +    Xapian::QueryParser &parser;\r
230 +    notmuch_database_t *notmuch;\r
231 +\r
232 + public:\r
233 +    QueryFieldProcessor (Xapian::QueryParser &parser_, notmuch_database_t *notmuch_)\r
234 +       : parser(parser_), notmuch(notmuch_) { };\r
235 +\r
236 +    Xapian::Query operator()(const std::string & str);\r
237 +};\r
238 +#endif\r
239 +#endif /* NOTMUCH_QUERY_FP_H */\r
240 diff --git a/test/T600-named-queries.sh b/test/T600-named-queries.sh\r
241 index 0922620..f0ae24f 100755\r
242 --- a/test/T600-named-queries.sh\r
243 +++ b/test/T600-named-queries.sh\r
244 @@ -50,4 +50,21 @@ notmuch restore < BEFORE\r
245  notmuch dump | grep '^#@' > OUTPUT\r
246  test_expect_equal_file QUERIES.BEFORE OUTPUT\r
247  \r
248 +if [ $NOTMUCH_HAVE_XAPIAN_FIELD_PROCESSOR -eq 1 ]; then\r
249 +    test_begin_subtest "search named query"\r
250 +    notmuch search query:test > OUTPUT\r
251 +    notmuch search $QUERYSTR > EXPECTED\r
252 +    test_expect_equal_file EXPECTED OUTPUT\r
253 +\r
254 +    test_begin_subtest "search named query with other terms"\r
255 +    notmuch search query:test and subject:Maildir > OUTPUT\r
256 +    notmuch search $QUERYSTR and subject:Maildir > EXPECTED\r
257 +    test_expect_equal_file EXPECTED OUTPUT\r
258 +\r
259 +    test_begin_subtest "search nested named query"\r
260 +    notmuch search query:test2 > OUTPUT\r
261 +    notmuch search $QUERYSTR2 > EXPECTED\r
262 +    test_expect_equal_file EXPECTED OUTPUT\r
263 +fi\r
264 +\r
265  test_done\r
266 -- \r
267 2.8.1\r
268 \r