Re: [PATCH v4 01/16] add util/search-path.{c, h} to test for executables in $PATH
[notmuch-archives.git] / 7d / 57180177d9f4b20a673914029fc733263cca5f
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 8241B6DE093F\r
6  for <notmuch@notmuchmail.org>; Fri, 13 May 2016 03:38:54 -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.013\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.013 tagged_above=-999 required=5\r
12  tests=[AWL=-0.002, 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 W1Lier8cjkMo for <notmuch@notmuchmail.org>;\r
17  Fri, 13 May 2016 03:38: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 7F6D56DE02B0\r
20  for <notmuch@notmuchmail.org>; Fri, 13 May 2016 03:38:26 -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 1b1AU7-0003P1-FS; Fri, 13 May 2016 06:38:19 -0400\r
24 Received: (nullmailer pid 7566 invoked by uid 1000);\r
25  Fri, 13 May 2016 10:38:17 -0000\r
26 From: David Bremner <david@tethera.net>\r
27 To: notmuch@notmuchmail.org\r
28 Subject: [Patch v5 11/11] lib: add support for named queries\r
29 Date: Fri, 13 May 2016 07:38:13 -0300\r
30 Message-Id: <1463135893-7471-12-git-send-email-david@tethera.net>\r
31 X-Mailer: git-send-email 2.8.1\r
32 In-Reply-To: <1463135893-7471-1-git-send-email-david@tethera.net>\r
33 References: <1463135893-7471-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: Fri, 13 May 2016 10:38:54 -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                   | 44 +++++++++++++++++++++++++++++++++++++++\r
59  lib/query-fp.h                    | 42 +++++++++++++++++++++++++++++++++++++\r
60  test/T600-named-queries.sh        | 17 +++++++++++++++\r
61  7 files changed, 116 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..73790cd\r
146 --- /dev/null\r
147 +++ b/lib/query-fp.cc\r
148 @@ -0,0 +1,44 @@\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 +\r
190 +    return parser.parse_query (expansion, NOTMUCH_QUERY_PARSER_FLAGS);\r
191 +}\r
192 +#endif\r
193 diff --git a/lib/query-fp.h b/lib/query-fp.h\r
194 new file mode 100644\r
195 index 0000000..67f8705\r
196 --- /dev/null\r
197 +++ b/lib/query-fp.h\r
198 @@ -0,0 +1,42 @@\r
199 +/* query-fp.h - query field processor glue\r
200 + *\r
201 + * This file is part of notmuch.\r
202 + *\r
203 + * Copyright © 2016 David Bremner\r
204 + *\r
205 + * This program is free software: you can redistribute it and/or modify\r
206 + * it under the terms of the GNU General Public License as published by\r
207 + * the Free Software Foundation, either version 3 of the License, or\r
208 + * (at your option) any later version.\r
209 + *\r
210 + * This program is distributed in the hope that it will be useful,\r
211 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
212 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
213 + * GNU General Public License for more details.\r
214 + *\r
215 + * You should have received a copy of the GNU General Public License\r
216 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
217 + *\r
218 + * Author: David Bremner <david@tethera.net>\r
219 + */\r
220 +\r
221 +#ifndef NOTMUCH_QUERY_FP_H\r
222 +#define NOTMUCH_QUERY_FP_H\r
223 +\r
224 +#include <xapian.h>\r
225 +#include "notmuch.h"\r
226 +\r
227 +#if HAVE_XAPIAN_FIELD_PROCESSOR\r
228 +class QueryFieldProcessor : public Xapian::FieldProcessor {\r
229 + protected:\r
230 +    Xapian::QueryParser &parser;\r
231 +    notmuch_database_t *notmuch;\r
232 +\r
233 + public:\r
234 +    QueryFieldProcessor (Xapian::QueryParser &parser_, notmuch_database_t *notmuch_)\r
235 +       : parser(parser_), notmuch(notmuch_) { };\r
236 +\r
237 +    Xapian::Query operator()(const std::string & str);\r
238 +};\r
239 +#endif\r
240 +#endif /* NOTMUCH_QUERY_FP_H */\r
241 diff --git a/test/T600-named-queries.sh b/test/T600-named-queries.sh\r
242 index 0922620..f0ae24f 100755\r
243 --- a/test/T600-named-queries.sh\r
244 +++ b/test/T600-named-queries.sh\r
245 @@ -50,4 +50,21 @@ notmuch restore < BEFORE\r
246  notmuch dump | grep '^#@' > OUTPUT\r
247  test_expect_equal_file QUERIES.BEFORE OUTPUT\r
248  \r
249 +if [ $NOTMUCH_HAVE_XAPIAN_FIELD_PROCESSOR -eq 1 ]; then\r
250 +    test_begin_subtest "search named query"\r
251 +    notmuch search query:test > OUTPUT\r
252 +    notmuch search $QUERYSTR > EXPECTED\r
253 +    test_expect_equal_file EXPECTED OUTPUT\r
254 +\r
255 +    test_begin_subtest "search named query with other terms"\r
256 +    notmuch search query:test and subject:Maildir > OUTPUT\r
257 +    notmuch search $QUERYSTR and subject:Maildir > EXPECTED\r
258 +    test_expect_equal_file EXPECTED OUTPUT\r
259 +\r
260 +    test_begin_subtest "search nested named query"\r
261 +    notmuch search query:test2 > OUTPUT\r
262 +    notmuch search $QUERYSTR2 > EXPECTED\r
263 +    test_expect_equal_file EXPECTED OUTPUT\r
264 +fi\r
265 +\r
266  test_done\r
267 -- \r
268 2.8.1\r
269 \r