Re: [PATCH v4 13/16] add indexopts to notmuch python bindings.
[notmuch-archives.git] / 51 / 51c2bedd3b873cab01e8c70abb396e7e85d188
1 Return-Path: <sojkam1@fel.cvut.cz>\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 C6069431FDB\r
6         for <notmuch@notmuchmail.org>; Thu, 30 Oct 2014 16:59:57 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -2.3\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\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 sC9G+NoiBJS6 for <notmuch@notmuchmail.org>;\r
16         Thu, 30 Oct 2014 16:59:53 -0700 (PDT)\r
17 Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
18         by olra.theworths.org (Postfix) with ESMTP id C8EA1431FD0\r
19         for <notmuch@notmuchmail.org>; Thu, 30 Oct 2014 16:59:45 -0700 (PDT)\r
20 Received: from localhost (unknown [192.168.200.7])\r
21         by max.feld.cvut.cz (Postfix) with ESMTP id 360C15CCFF8\r
22         for <notmuch@notmuchmail.org>; Fri, 31 Oct 2014 00:59:45 +0100 (CET)\r
23 X-Virus-Scanned: IMAP STYX AMAVIS\r
24 Received: from max.feld.cvut.cz ([192.168.200.1])\r
25         by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new,\r
26         port 10044) with ESMTP id 1E_5ZuqRkyHl for <notmuch@notmuchmail.org>;\r
27         Fri, 31 Oct 2014 00:59:40 +0100 (CET)\r
28 Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
29         by max.feld.cvut.cz (Postfix) with ESMTP id 62A125CCFF5\r
30         for <notmuch@notmuchmail.org>; Fri, 31 Oct 2014 00:59:40 +0100 (CET)\r
31 Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
32         (envelope-from <sojkam1@fel.cvut.cz>)\r
33         id 1Xjzcs-0005bG-PX; Fri, 31 Oct 2014 00:59:34 +0100\r
34 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
35 To: notmuch@notmuchmail.org\r
36 Subject: [PATCH v5 1/7] cli: search: Refactor passing of command line options\r
37 Date: Fri, 31 Oct 2014 00:59:27 +0100\r
38 Message-Id: <1414713573-21461-2-git-send-email-sojkam1@fel.cvut.cz>\r
39 X-Mailer: git-send-email 2.1.1\r
40 In-Reply-To: <1414713573-21461-1-git-send-email-sojkam1@fel.cvut.cz>\r
41 References: <1414713573-21461-1-git-send-email-sojkam1@fel.cvut.cz>\r
42 X-BeenThere: notmuch@notmuchmail.org\r
43 X-Mailman-Version: 2.1.13\r
44 Precedence: list\r
45 List-Id: "Use and development of the notmuch mail system."\r
46         <notmuch.notmuchmail.org>\r
47 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
48         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
49 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
50 List-Post: <mailto:notmuch@notmuchmail.org>\r
51 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
52 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
53         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
54 X-List-Received-Date: Thu, 30 Oct 2014 23:59:58 -0000\r
55 \r
56 Many functions that implement the search command need to access command\r
57 line options. Instead of passing each option in a separate variable, put\r
58 them in a structure and pass only this structure.\r
59 \r
60 This will become handy in the following commits.\r
61 ---\r
62  notmuch-search.c | 125 ++++++++++++++++++++++++++++---------------------------\r
63  1 file changed, 64 insertions(+), 61 deletions(-)\r
64 \r
65 diff --git a/notmuch-search.c b/notmuch-search.c\r
66 index bc9be45..0c3e972 100644\r
67 --- a/notmuch-search.c\r
68 +++ b/notmuch-search.c\r
69 @@ -30,6 +30,16 @@ typedef enum {\r
70      OUTPUT_TAGS\r
71  } output_t;\r
72  \r
73 +typedef struct {\r
74 +    sprinter_t *format;\r
75 +    notmuch_query_t *query;\r
76 +    notmuch_sort_t sort;\r
77 +    output_t output;\r
78 +    int offset;\r
79 +    int limit;\r
80 +    int dupe;\r
81 +} search_options_t;\r
82 +\r
83  /* Return two stable query strings that identify exactly the matched\r
84   * and unmatched messages currently in thread.  If there are no\r
85   * matched or unmatched messages, the returned buffers will be\r
86 @@ -70,43 +80,39 @@ get_thread_query (notmuch_thread_t *thread,\r
87  }\r
88  \r
89  static int\r
90 -do_search_threads (sprinter_t *format,\r
91 -                  notmuch_query_t *query,\r
92 -                  notmuch_sort_t sort,\r
93 -                  output_t output,\r
94 -                  int offset,\r
95 -                  int limit)\r
96 +do_search_threads (search_options_t *opt)\r
97  {\r
98      notmuch_thread_t *thread;\r
99      notmuch_threads_t *threads;\r
100      notmuch_tags_t *tags;\r
101 +    sprinter_t *format = opt->format;\r
102      time_t date;\r
103      int i;\r
104  \r
105 -    if (offset < 0) {\r
106 -       offset += notmuch_query_count_threads (query);\r
107 -       if (offset < 0)\r
108 -           offset = 0;\r
109 +    if (opt->offset < 0) {\r
110 +       opt->offset += notmuch_query_count_threads (opt->query);\r
111 +       if (opt->offset < 0)\r
112 +           opt->offset = 0;\r
113      }\r
114  \r
115 -    threads = notmuch_query_search_threads (query);\r
116 +    threads = notmuch_query_search_threads (opt->query);\r
117      if (threads == NULL)\r
118         return 1;\r
119  \r
120      format->begin_list (format);\r
121  \r
122      for (i = 0;\r
123 -        notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);\r
124 +        notmuch_threads_valid (threads) && (opt->limit < 0 || i < opt->offset + opt->limit);\r
125          notmuch_threads_move_to_next (threads), i++)\r
126      {\r
127         thread = notmuch_threads_get (threads);\r
128  \r
129 -       if (i < offset) {\r
130 +       if (i < opt->offset) {\r
131             notmuch_thread_destroy (thread);\r
132             continue;\r
133         }\r
134  \r
135 -       if (output == OUTPUT_THREADS) {\r
136 +       if (opt->output == OUTPUT_THREADS) {\r
137             format->set_prefix (format, "thread");\r
138             format->string (format,\r
139                             notmuch_thread_get_thread_id (thread));\r
140 @@ -123,7 +129,7 @@ do_search_threads (sprinter_t *format,\r
141  \r
142             format->begin_map (format);\r
143  \r
144 -           if (sort == NOTMUCH_SORT_OLDEST_FIRST)\r
145 +           if (opt->sort == NOTMUCH_SORT_OLDEST_FIRST)\r
146                 date = notmuch_thread_get_oldest_date (thread);\r
147             else\r
148                 date = notmuch_thread_get_newest_date (thread);\r
149 @@ -215,40 +221,36 @@ do_search_threads (sprinter_t *format,\r
150  }\r
151  \r
152  static int\r
153 -do_search_messages (sprinter_t *format,\r
154 -                   notmuch_query_t *query,\r
155 -                   output_t output,\r
156 -                   int offset,\r
157 -                   int limit,\r
158 -                   int dupe)\r
159 +do_search_messages (search_options_t *opt)\r
160  {\r
161      notmuch_message_t *message;\r
162      notmuch_messages_t *messages;\r
163      notmuch_filenames_t *filenames;\r
164 +    sprinter_t *format = opt->format;\r
165      int i;\r
166  \r
167 -    if (offset < 0) {\r
168 -       offset += notmuch_query_count_messages (query);\r
169 -       if (offset < 0)\r
170 -           offset = 0;\r
171 +    if (opt->offset < 0) {\r
172 +       opt->offset += notmuch_query_count_messages (opt->query);\r
173 +       if (opt->offset < 0)\r
174 +           opt->offset = 0;\r
175      }\r
176  \r
177 -    messages = notmuch_query_search_messages (query);\r
178 +    messages = notmuch_query_search_messages (opt->query);\r
179      if (messages == NULL)\r
180         return 1;\r
181  \r
182      format->begin_list (format);\r
183  \r
184      for (i = 0;\r
185 -        notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);\r
186 +        notmuch_messages_valid (messages) && (opt->limit < 0 || i < opt->offset + opt->limit);\r
187          notmuch_messages_move_to_next (messages), i++)\r
188      {\r
189 -       if (i < offset)\r
190 +       if (i < opt->offset)\r
191             continue;\r
192  \r
193         message = notmuch_messages_get (messages);\r
194  \r
195 -       if (output == OUTPUT_FILES) {\r
196 +       if (opt->output == OUTPUT_FILES) {\r
197             int j;\r
198             filenames = notmuch_message_get_filenames (message);\r
199  \r
200 @@ -256,7 +258,7 @@ do_search_messages (sprinter_t *format,\r
201                  notmuch_filenames_valid (filenames);\r
202                  notmuch_filenames_move_to_next (filenames), j++)\r
203             {\r
204 -               if (dupe < 0 || dupe == j) {\r
205 +               if (opt->dupe < 0 || opt->dupe == j) {\r
206                     format->string (format, notmuch_filenames_get (filenames));\r
207                     format->separator (format);\r
208                 }\r
209 @@ -283,12 +285,13 @@ do_search_messages (sprinter_t *format,\r
210  \r
211  static int\r
212  do_search_tags (notmuch_database_t *notmuch,\r
213 -               sprinter_t *format,\r
214 -               notmuch_query_t *query)\r
215 +               const search_options_t *opt)\r
216  {\r
217      notmuch_messages_t *messages = NULL;\r
218      notmuch_tags_t *tags;\r
219      const char *tag;\r
220 +    sprinter_t *format = opt->format;\r
221 +    notmuch_query_t *query = opt->query;\r
222  \r
223      /* should the following only special case if no excluded terms\r
224       * specified? */\r
225 @@ -333,16 +336,16 @@ int\r
226  notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
227  {\r
228      notmuch_database_t *notmuch;\r
229 -    notmuch_query_t *query;\r
230 +    search_options_t opt = {\r
231 +       .sort = NOTMUCH_SORT_NEWEST_FIRST,\r
232 +       .output = OUTPUT_SUMMARY,\r
233 +       .offset = 0,\r
234 +       .limit = -1, /* unlimited */\r
235 +       .dupe = -1,\r
236 +    };\r
237      char *query_str;\r
238 -    notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;\r
239 -    sprinter_t *format = NULL;\r
240      int opt_index, ret;\r
241 -    output_t output = OUTPUT_SUMMARY;\r
242 -    int offset = 0;\r
243 -    int limit = -1; /* unlimited */\r
244      notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;\r
245 -    int dupe = -1;\r
246      unsigned int i;\r
247  \r
248      enum {\r
249 @@ -353,7 +356,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
250      } format_sel = NOTMUCH_FORMAT_TEXT;\r
251  \r
252      notmuch_opt_desc_t options[] = {\r
253 -       { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',\r
254 +       { NOTMUCH_OPT_KEYWORD, &opt.sort, "sort", 's',\r
255           (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },\r
256                                   { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },\r
257                                   { 0, 0 } } },\r
258 @@ -364,7 +367,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
259                                   { "text0", NOTMUCH_FORMAT_TEXT0 },\r
260                                   { 0, 0 } } },\r
261         { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },\r
262 -       { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',\r
263 +       { NOTMUCH_OPT_KEYWORD, &opt.output, "output", 'o',\r
264           (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },\r
265                                   { "threads", OUTPUT_THREADS },\r
266                                   { "messages", OUTPUT_MESSAGES },\r
267 @@ -377,9 +380,9 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
268                                    { "flag", NOTMUCH_EXCLUDE_FLAG },\r
269                                    { "all", NOTMUCH_EXCLUDE_ALL },\r
270                                    { 0, 0 } } },\r
271 -       { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },\r
272 -       { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0  },\r
273 -       { NOTMUCH_OPT_INT, &dupe, "duplicate", 'D', 0  },\r
274 +       { NOTMUCH_OPT_INT, &opt.offset, "offset", 'O', 0 },\r
275 +       { NOTMUCH_OPT_INT, &opt.limit, "limit", 'L', 0  },\r
276 +       { NOTMUCH_OPT_INT, &opt.dupe, "duplicate", 'D', 0  },\r
277         { 0, 0, 0, 0, 0 }\r
278      };\r
279  \r
280 @@ -389,20 +392,20 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
281  \r
282      switch (format_sel) {\r
283      case NOTMUCH_FORMAT_TEXT:\r
284 -       format = sprinter_text_create (config, stdout);\r
285 +       opt.format = sprinter_text_create (config, stdout);\r
286         break;\r
287      case NOTMUCH_FORMAT_TEXT0:\r
288 -       if (output == OUTPUT_SUMMARY) {\r
289 +       if (opt.output == OUTPUT_SUMMARY) {\r
290             fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");\r
291             return EXIT_FAILURE;\r
292         }\r
293 -       format = sprinter_text0_create (config, stdout);\r
294 +       opt.format = sprinter_text0_create (config, stdout);\r
295         break;\r
296      case NOTMUCH_FORMAT_JSON:\r
297 -       format = sprinter_json_create (config, stdout);\r
298 +       opt.format = sprinter_json_create (config, stdout);\r
299         break;\r
300      case NOTMUCH_FORMAT_SEXP:\r
301 -       format = sprinter_sexp_create (config, stdout);\r
302 +       opt.format = sprinter_sexp_create (config, stdout);\r
303         break;\r
304      default:\r
305         /* this should never happen */\r
306 @@ -425,15 +428,15 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
307         return EXIT_FAILURE;\r
308      }\r
309  \r
310 -    query = notmuch_query_create (notmuch, query_str);\r
311 -    if (query == NULL) {\r
312 +    opt.query = notmuch_query_create (notmuch, query_str);\r
313 +    if (opt.query == NULL) {\r
314         fprintf (stderr, "Out of memory\n");\r
315         return EXIT_FAILURE;\r
316      }\r
317  \r
318 -    notmuch_query_set_sort (query, sort);\r
319 +    notmuch_query_set_sort (opt.query, opt.sort);\r
320  \r
321 -    if (exclude == NOTMUCH_EXCLUDE_FLAG && output != OUTPUT_SUMMARY) {\r
322 +    if (exclude == NOTMUCH_EXCLUDE_FLAG && opt.output != OUTPUT_SUMMARY) {\r
323         /* If we are not doing summary output there is nowhere to\r
324          * print the excluded flag so fall back on including the\r
325          * excluded messages. */\r
326 @@ -448,29 +451,29 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
327         search_exclude_tags = notmuch_config_get_search_exclude_tags\r
328             (config, &search_exclude_tags_length);\r
329         for (i = 0; i < search_exclude_tags_length; i++)\r
330 -           notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);\r
331 -       notmuch_query_set_omit_excluded (query, exclude);\r
332 +           notmuch_query_add_tag_exclude (opt.query, search_exclude_tags[i]);\r
333 +       notmuch_query_set_omit_excluded (opt.query, exclude);\r
334      }\r
335  \r
336 -    switch (output) {\r
337 +    switch (opt.output) {\r
338      default:\r
339      case OUTPUT_SUMMARY:\r
340      case OUTPUT_THREADS:\r
341 -       ret = do_search_threads (format, query, sort, output, offset, limit);\r
342 +       ret = do_search_threads (&opt);\r
343         break;\r
344      case OUTPUT_MESSAGES:\r
345      case OUTPUT_FILES:\r
346 -       ret = do_search_messages (format, query, output, offset, limit, dupe);\r
347 +       ret = do_search_messages (&opt);\r
348         break;\r
349      case OUTPUT_TAGS:\r
350 -       ret = do_search_tags (notmuch, format, query);\r
351 +       ret = do_search_tags (notmuch, &opt);\r
352         break;\r
353      }\r
354  \r
355 -    notmuch_query_destroy (query);\r
356 +    notmuch_query_destroy (opt.query);\r
357      notmuch_database_destroy (notmuch);\r
358  \r
359 -    talloc_free (format);\r
360 +    talloc_free (opt.format);\r
361  \r
362      return ret ? EXIT_FAILURE : EXIT_SUCCESS;\r
363  }\r
364 -- \r
365 2.1.1\r
366 \r