Re: [PATCH 9/9] add has: query prefix to search for specific properties
[notmuch-archives.git] / ce / 7e95cb9972cc217def1c6f5869948366806ef5
1 Return-Path: <polatel@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 olra.theworths.org (Postfix) with ESMTP id 74675431FBC\r
6         for <notmuch@notmuchmail.org>; Wed, 13 Jan 2010 05:17:46 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -1.099\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-1.099 tagged_above=-999 required=5 tests=[AWL=0.881,\r
12         BAYES_00=-2.599, RCVD_IN_SORBS_WEB=0.619] autolearn=no\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id 9UB43sZg3a4A for <notmuch@notmuchmail.org>;\r
16         Wed, 13 Jan 2010 05:17:45 -0800 (PST)\r
17 Received: from mail-fx0-f217.google.com (mail-fx0-f217.google.com\r
18         [209.85.220.217])\r
19         by olra.theworths.org (Postfix) with ESMTP id 4FD28431FAE\r
20         for <notmuch@notmuchmail.org>; Wed, 13 Jan 2010 05:17:45 -0800 (PST)\r
21 Received: by fxm9 with SMTP id 9so20804210fxm.30\r
22         for <notmuch@notmuchmail.org>; Wed, 13 Jan 2010 05:17:44 -0800 (PST)\r
23 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma;\r
24         h=domainkey-signature:received:received:sender:from:to:subject:date\r
25         :message-id:x-mailer:in-reply-to:references:organization;\r
26         bh=2HggMywjex1jI3NqygpcK8qh0Ptb9EqzPbD7dv/tv60=;\r
27         b=iGr3g4q+PK9j+9P9Ia+rdrvaYuWAB0oRuHeqZMTuuDjz8qAHpqQeYMoaAz6FXU6hdg\r
28         2FzByFA/lPX1ISky9XWdHZNQcKpc/huFgyaa78/c46jtxkpW3zNexGN86ukDnLCHU5h1\r
29         FhUT8oH6OYaFq4hwsLTVzEbJQiX9jgJLIC2mI=\r
30 DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma;\r
31         h=sender:from:to:subject:date:message-id:x-mailer:in-reply-to\r
32         :references:organization;\r
33         b=Yb9aNRjdvYbveLNqfdeC2/JjAmIovI2pG3l0W2IyfRqM/vSh8mnkM8+YXdFZ7gA/e4\r
34         QVpJkMoD73U7UZXHZg1r02zDaoKY8pXip61dAMafOKFnxH93RvmHVjpkkX3EzEP0+qt1\r
35         bKGN63YAfCtjHEoSk3afOi1Q579/RPcwfEYSE=\r
36 Received: by 10.86.198.9 with SMTP id v9mr5216790fgf.14.1263388664293;\r
37         Wed, 13 Jan 2010 05:17:44 -0800 (PST)\r
38 Received: from harikalardiyari ([78.179.54.193])\r
39         by mx.google.com with ESMTPS id 14sm10796277fxm.7.2010.01.13.05.17.42\r
40         (version=TLSv1/SSLv3 cipher=RC4-MD5);\r
41         Wed, 13 Jan 2010 05:17:43 -0800 (PST)\r
42 Sender: Ali Polatel <polatel@gmail.com>\r
43 From: Ali Polatel <alip@exherbo.org>\r
44 To: notmuch@notmuchmail.org\r
45 Date: Wed, 13 Jan 2010 15:17:41 +0200\r
46 Message-Id:\r
47  <a05f7197e759dba8e5f61951eaeeb83823d38a0d.1263388420.git.alip@exherbo.org>\r
48 X-Mailer: git-send-email 1.6.6\r
49 In-Reply-To:\r
50  <cd1ed7d2c2ea5a9f7da351b5916253708b65feb5.1263377759.git.alip@exherbo.org>\r
51 References:\r
52  <cd1ed7d2c2ea5a9f7da351b5916253708b65feb5.1263377759.git.alip@exherbo.org>\r
53 Organization: Pink Floyd\r
54 Subject: [notmuch] [RFC/PATCH v2] Add search-files command\r
55 X-BeenThere: notmuch@notmuchmail.org\r
56 X-Mailman-Version: 2.1.13\r
57 Precedence: list\r
58 List-Id: "Use and development of the notmuch mail system."\r
59         <notmuch.notmuchmail.org>\r
60 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
61         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
62 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
63 List-Post: <mailto:notmuch@notmuchmail.org>\r
64 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
65 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
66         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
67 X-List-Received-Date: Wed, 13 Jan 2010 13:17:46 -0000\r
68 \r
69 This command can be used to integrate notmuch with other MUAs as a\r
70 searching client. The idea is simple, a simple script could get\r
71 search-terms as argument and create a "virtual" maildir which has\r
72 symbolic links to files output by search-files command. This is similar\r
73 to nmzmail.\r
74 \r
75 Version 2:\r
76 - Update copyright\r
77 - Respect coding style about long lines\r
78 - Free config structure on exit\r
79 \r
80 ---\r
81  Makefile.local         |    1 +\r
82  notmuch-client.h       |    3 +\r
83  notmuch-search-files.c |  109 ++++++++++++++++++++++++++++++++++++++++++++++++\r
84  notmuch.c              |   13 ++++++\r
85  4 files changed, 126 insertions(+), 0 deletions(-)\r
86  create mode 100644 notmuch-search-files.c\r
87 \r
88 diff --git a/Makefile.local b/Makefile.local\r
89 index 933ff4c..78bc25d 100644\r
90 --- a/Makefile.local\r
91 +++ b/Makefile.local\r
92 @@ -12,6 +12,7 @@ notmuch_client_srcs =         \\r
93         notmuch-reply.c         \\r
94         notmuch-restore.c       \\r
95         notmuch-search.c        \\r
96 +       notmuch-search-files.c  \\r
97         notmuch-search-tags.c   \\r
98         notmuch-setup.c         \\r
99         notmuch-show.c          \\r
100 diff --git a/notmuch-client.h b/notmuch-client.h\r
101 index 77766de..d505d30 100644\r
102 --- a/notmuch-client.h\r
103 +++ b/notmuch-client.h\r
104 @@ -96,6 +96,9 @@ int\r
105  notmuch_search_command (void *ctx, int argc, char *argv[]);\r
106  \r
107  int\r
108 +notmuch_search_files_command (void *ctx, int argc, char *argv[]);\r
109 +\r
110 +int\r
111  notmuch_setup_command (void *ctx, int argc, char *argv[]);\r
112  \r
113  int\r
114 diff --git a/notmuch-search-files.c b/notmuch-search-files.c\r
115 new file mode 100644\r
116 index 0000000..7b6d14d\r
117 --- /dev/null\r
118 +++ b/notmuch-search-files.c\r
119 @@ -0,0 +1,109 @@\r
120 +/* notmuch - Not much of an email program, (just index and search)\r
121 + *\r
122 + * Copyright © 2010 Ali Polatel\r
123 + *\r
124 + * This program is free software: you can redistribute it and/or modify\r
125 + * it under the terms of the GNU General Public License as published by\r
126 + * the Free Software Foundation, either version 3 of the License, or\r
127 + * (at your option) any later version.\r
128 + *\r
129 + * This program is distributed in the hope that it will be useful,\r
130 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
131 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
132 + * GNU General Public License for more details.\r
133 + *\r
134 + * You should have received a copy of the GNU General Public License\r
135 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
136 + *\r
137 + * Author: Ali Polatel <alip@exherbo.org>\r
138 + */\r
139 +\r
140 +#include "notmuch-client.h"\r
141 +\r
142 +static void\r
143 +do_search_files (notmuch_query_t *query)\r
144 +{\r
145 +    notmuch_message_t *message;\r
146 +    notmuch_messages_t *messages;\r
147 +\r
148 +    for (messages = notmuch_query_search_messages (query);\r
149 +        notmuch_messages_has_more (messages);\r
150 +        notmuch_messages_advance (messages))\r
151 +    {\r
152 +       message = notmuch_messages_get (messages);\r
153 +       printf ("%s\n", notmuch_message_get_filename (message));\r
154 +       notmuch_message_destroy(message);\r
155 +    }\r
156 +}\r
157 +\r
158 +int\r
159 +notmuch_search_files_command (void *ctx, int argc, char *argv[])\r
160 +{\r
161 +    notmuch_config_t *config;\r
162 +    notmuch_database_t *notmuch;\r
163 +    notmuch_query_t *query;\r
164 +    char *query_str;\r
165 +    char *opt;\r
166 +    notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;\r
167 +    int i;\r
168 +\r
169 +    for (i = 0; i < argc && argv[i][0] == '-'; i++) {\r
170 +       if (strcmp (argv[i], "--") == 0) {\r
171 +           i++;\r
172 +           break;\r
173 +       }\r
174 +        if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {\r
175 +           opt = argv[i] + sizeof ("--sort=") - 1;\r
176 +           if (strcmp (opt, "oldest-first") == 0) {\r
177 +               sort = NOTMUCH_SORT_OLDEST_FIRST;\r
178 +           } else if (strcmp (opt, "newest-first") == 0) {\r
179 +               sort = NOTMUCH_SORT_NEWEST_FIRST;\r
180 +           } else {\r
181 +               fprintf (stderr, "Invalid value for --sort: %s\n", opt);\r
182 +               return 1;\r
183 +           }\r
184 +       } else {\r
185 +           fprintf (stderr, "Unrecognized option: %s\n", argv[i]);\r
186 +           return 1;\r
187 +       }\r
188 +    }\r
189 +\r
190 +    argc -= i;\r
191 +    argv += i;\r
192 +\r
193 +    config = notmuch_config_open (ctx, NULL, NULL);\r
194 +    if (config == NULL)\r
195 +       return 1;\r
196 +\r
197 +    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
198 +                                    NOTMUCH_DATABASE_MODE_READ_ONLY);\r
199 +    if (notmuch == NULL)\r
200 +       return 1;\r
201 +\r
202 +    query_str = query_string_from_args (ctx, argc, argv);\r
203 +    if (query_str == NULL) {\r
204 +       fprintf (stderr, "Out of memory.\n");\r
205 +       return 1;\r
206 +    }\r
207 +    if (*query_str == '\0') {\r
208 +       fprintf (stderr, "Error: notmuch search-files requires"\r
209 +                        " at least one search term.\n");\r
210 +       return 1;\r
211 +    }\r
212 +\r
213 +    query = notmuch_query_create (notmuch, query_str);\r
214 +    if (query == NULL) {\r
215 +       fprintf (stderr, "Out of memory\n");\r
216 +       return 1;\r
217 +    }\r
218 +\r
219 +    notmuch_query_set_sort (query, sort);\r
220 +\r
221 +    do_search_files (query);\r
222 +\r
223 +    notmuch_config_close (config);\r
224 +    notmuch_query_destroy (query);\r
225 +    notmuch_database_close (notmuch);\r
226 +\r
227 +    return 0;\r
228 +}\r
229 diff --git a/notmuch.c b/notmuch.c\r
230 index 87479f8..4907339 100644\r
231 --- a/notmuch.c\r
232 +++ b/notmuch.c\r
233 @@ -165,6 +165,19 @@ command_t commands[] = {\r
234        "\n"\r
235        "\t\tSee \"notmuch help search-terms\" for details of the search\n"\r
236        "\t\tterms syntax." },\r
237 +    { "search-files", notmuch_search_files_command,\r
238 +      "[options...] <search-terms> [...]",\r
239 +      "\t\tSearch for filenames matching the given search terms.",\r
240 +      "\t\tSupported options for search-files include:\n"\r
241 +      "\n"\r
242 +      "\t\t--sort=(newest-first|oldest-first)\n"\r
243 +      "\n"\r
244 +      "\t\t\tPresent results in either chronological order\n"\r
245 +      "\t\t\t(oldest-first) or reverse chronological order\n"\r
246 +      "\t\t\t(newest-first), which is the default.\n"\r
247 +      "\n"\r
248 +      "\t\tSee \"notmuch help search-terms\" for details of the search\n"\r
249 +      "\t\tterms syntax." },\r
250      { "show", notmuch_show_command,\r
251        "<search-terms> [...]",\r
252        "\t\tShow all messages matching the search terms.",\r
253 -- \r
254 1.6.6\r
255 \r