Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / 6b / a47104a0c86286008425141e4703e1ad02e3c9
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 AA4DF431FC4\r
6         for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:39:32 -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 pn6wV4mUY5rd for <notmuch@notmuchmail.org>;\r
16         Mon, 22 Sep 2014 02:39:28 -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 E8188431FB6\r
19         for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:39:27 -0700 (PDT)\r
20 Received: from localhost (unknown [192.168.200.7])\r
21         by max.feld.cvut.cz (Postfix) with ESMTP id 65A6E5CCE60;\r
22         Mon, 22 Sep 2014 11:39:27 +0200 (CEST)\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)\r
27         with ESMTP id 4HFBvm9VwXgd; Mon, 22 Sep 2014 11:39:22 +0200 (CEST)\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 6321D5CCE61;\r
30         Mon, 22 Sep 2014 11:39:22 +0200 (CEST)\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 1XW05U-0001w8-KN; Mon, 22 Sep 2014 11:39:16 +0200\r
34 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
35 To: notmuch@notmuchmail.org\r
36 Subject: [PATCH 1/5] cli: Refactor option passing in the search command\r
37 Date: Mon, 22 Sep 2014 11:37:55 +0200\r
38 Message-Id: <1411378679-7307-2-git-send-email-sojkam1@fel.cvut.cz>\r
39 X-Mailer: git-send-email 2.1.0\r
40 In-Reply-To: <1411378679-7307-1-git-send-email-sojkam1@fel.cvut.cz>\r
41 References: <1411378679-7307-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: Mon, 22 Sep 2014 09:39:32 -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 patches.\r
61 ---\r
62  notmuch-search.c | 122 ++++++++++++++++++++++++++++---------------------------\r
63  1 file changed, 62 insertions(+), 60 deletions(-)\r
64 \r
65 diff --git a/notmuch-search.c b/notmuch-search.c\r
66 index bc9be45..5ac2a26 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,46 +80,42 @@ 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 *o)\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 = o->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 (o->offset < 0) {\r
110 +       o->offset += notmuch_query_count_threads (o->query);\r
111 +       if (o->offset < 0)\r
112 +           o->offset = 0;\r
113      }\r
114  \r
115 -    threads = notmuch_query_search_threads (query);\r
116 +    threads = notmuch_query_search_threads (o->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) && (o->limit < 0 || i < o->offset + o->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 < o->offset) {\r
131             notmuch_thread_destroy (thread);\r
132             continue;\r
133         }\r
134  \r
135 -       if (output == OUTPUT_THREADS) {\r
136 +       if (o->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 +                              notmuch_thread_get_thread_id (thread));\r
141             format->separator (format);\r
142         } else { /* output == OUTPUT_SUMMARY */\r
143             void *ctx_quote = talloc_new (thread);\r
144 @@ -123,7 +129,7 @@ do_search_threads (sprinter_t *format,\r
145  \r
146             format->begin_map (format);\r
147  \r
148 -           if (sort == NOTMUCH_SORT_OLDEST_FIRST)\r
149 +           if (o->sort == NOTMUCH_SORT_OLDEST_FIRST)\r
150                 date = notmuch_thread_get_oldest_date (thread);\r
151             else\r
152                 date = notmuch_thread_get_newest_date (thread);\r
153 @@ -215,40 +221,36 @@ do_search_threads (sprinter_t *format,\r
154  }\r
155  \r
156  static int\r
157 -do_search_messages (sprinter_t *format,\r
158 -                   notmuch_query_t *query,\r
159 -                   output_t output,\r
160 -                   int offset,\r
161 -                   int limit,\r
162 -                   int dupe)\r
163 +do_search_messages (search_options_t *o)\r
164  {\r
165      notmuch_message_t *message;\r
166      notmuch_messages_t *messages;\r
167      notmuch_filenames_t *filenames;\r
168 +    sprinter_t *format = o->format;\r
169      int i;\r
170  \r
171 -    if (offset < 0) {\r
172 -       offset += notmuch_query_count_messages (query);\r
173 -       if (offset < 0)\r
174 -           offset = 0;\r
175 +    if (o->offset < 0) {\r
176 +       o->offset += notmuch_query_count_messages (o->query);\r
177 +       if (o->offset < 0)\r
178 +           o->offset = 0;\r
179      }\r
180  \r
181 -    messages = notmuch_query_search_messages (query);\r
182 +    messages = notmuch_query_search_messages (o->query);\r
183      if (messages == NULL)\r
184         return 1;\r
185  \r
186      format->begin_list (format);\r
187  \r
188      for (i = 0;\r
189 -        notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);\r
190 +        notmuch_messages_valid (messages) && (o->limit < 0 || i < o->offset + o->limit);\r
191          notmuch_messages_move_to_next (messages), i++)\r
192      {\r
193 -       if (i < offset)\r
194 +       if (i < o->offset)\r
195             continue;\r
196  \r
197         message = notmuch_messages_get (messages);\r
198  \r
199 -       if (output == OUTPUT_FILES) {\r
200 +       if (o->output == OUTPUT_FILES) {\r
201             int j;\r
202             filenames = notmuch_message_get_filenames (message);\r
203  \r
204 @@ -256,7 +258,7 @@ do_search_messages (sprinter_t *format,\r
205                  notmuch_filenames_valid (filenames);\r
206                  notmuch_filenames_move_to_next (filenames), j++)\r
207             {\r
208 -               if (dupe < 0 || dupe == j) {\r
209 +               if (o->dupe < 0 || o->dupe == j) {\r
210                     format->string (format, notmuch_filenames_get (filenames));\r
211                     format->separator (format);\r
212                 }\r
213 @@ -333,16 +335,16 @@ int\r
214  notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
215  {\r
216      notmuch_database_t *notmuch;\r
217 -    notmuch_query_t *query;\r
218 +    search_options_t o = {\r
219 +       .sort = NOTMUCH_SORT_NEWEST_FIRST,\r
220 +       .output = OUTPUT_SUMMARY,\r
221 +       .offset = 0,\r
222 +       .limit = -1, /* unlimited */\r
223 +       .dupe = -1,\r
224 +    };\r
225      char *query_str;\r
226 -    notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;\r
227 -    sprinter_t *format = NULL;\r
228      int opt_index, ret;\r
229 -    output_t output = OUTPUT_SUMMARY;\r
230 -    int offset = 0;\r
231 -    int limit = -1; /* unlimited */\r
232      notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;\r
233 -    int dupe = -1;\r
234      unsigned int i;\r
235  \r
236      enum {\r
237 @@ -353,7 +355,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
238      } format_sel = NOTMUCH_FORMAT_TEXT;\r
239  \r
240      notmuch_opt_desc_t options[] = {\r
241 -       { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',\r
242 +       { NOTMUCH_OPT_KEYWORD, &o.sort, "sort", 's',\r
243           (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },\r
244                                   { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },\r
245                                   { 0, 0 } } },\r
246 @@ -364,7 +366,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
247                                   { "text0", NOTMUCH_FORMAT_TEXT0 },\r
248                                   { 0, 0 } } },\r
249         { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },\r
250 -       { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',\r
251 +       { NOTMUCH_OPT_KEYWORD, &o.output, "output", 'o',\r
252           (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },\r
253                                   { "threads", OUTPUT_THREADS },\r
254                                   { "messages", OUTPUT_MESSAGES },\r
255 @@ -377,9 +379,9 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
256                                    { "flag", NOTMUCH_EXCLUDE_FLAG },\r
257                                    { "all", NOTMUCH_EXCLUDE_ALL },\r
258                                    { 0, 0 } } },\r
259 -       { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },\r
260 -       { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0  },\r
261 -       { NOTMUCH_OPT_INT, &dupe, "duplicate", 'D', 0  },\r
262 +       { NOTMUCH_OPT_INT, &o.offset, "offset", 'O', 0 },\r
263 +       { NOTMUCH_OPT_INT, &o.limit, "limit", 'L', 0  },\r
264 +       { NOTMUCH_OPT_INT, &o.dupe, "duplicate", 'D', 0  },\r
265         { 0, 0, 0, 0, 0 }\r
266      };\r
267  \r
268 @@ -389,20 +391,20 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
269  \r
270      switch (format_sel) {\r
271      case NOTMUCH_FORMAT_TEXT:\r
272 -       format = sprinter_text_create (config, stdout);\r
273 +       o.format = sprinter_text_create (config, stdout);\r
274         break;\r
275      case NOTMUCH_FORMAT_TEXT0:\r
276 -       if (output == OUTPUT_SUMMARY) {\r
277 +       if (o.output == OUTPUT_SUMMARY) {\r
278             fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");\r
279             return EXIT_FAILURE;\r
280         }\r
281 -       format = sprinter_text0_create (config, stdout);\r
282 +       o.format = sprinter_text0_create (config, stdout);\r
283         break;\r
284      case NOTMUCH_FORMAT_JSON:\r
285 -       format = sprinter_json_create (config, stdout);\r
286 +       o.format = sprinter_json_create (config, stdout);\r
287         break;\r
288      case NOTMUCH_FORMAT_SEXP:\r
289 -       format = sprinter_sexp_create (config, stdout);\r
290 +       o.format = sprinter_sexp_create (config, stdout);\r
291         break;\r
292      default:\r
293         /* this should never happen */\r
294 @@ -425,15 +427,15 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
295         return EXIT_FAILURE;\r
296      }\r
297  \r
298 -    query = notmuch_query_create (notmuch, query_str);\r
299 -    if (query == NULL) {\r
300 +    o.query = notmuch_query_create (notmuch, query_str);\r
301 +    if (o.query == NULL) {\r
302         fprintf (stderr, "Out of memory\n");\r
303         return EXIT_FAILURE;\r
304      }\r
305  \r
306 -    notmuch_query_set_sort (query, sort);\r
307 +    notmuch_query_set_sort (o.query, o.sort);\r
308  \r
309 -    if (exclude == NOTMUCH_EXCLUDE_FLAG && output != OUTPUT_SUMMARY) {\r
310 +    if (exclude == NOTMUCH_EXCLUDE_FLAG && o.output != OUTPUT_SUMMARY) {\r
311         /* If we are not doing summary output there is nowhere to\r
312          * print the excluded flag so fall back on including the\r
313          * excluded messages. */\r
314 @@ -448,29 +450,29 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
315         search_exclude_tags = notmuch_config_get_search_exclude_tags\r
316             (config, &search_exclude_tags_length);\r
317         for (i = 0; i < search_exclude_tags_length; i++)\r
318 -           notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);\r
319 -       notmuch_query_set_omit_excluded (query, exclude);\r
320 +           notmuch_query_add_tag_exclude (o.query, search_exclude_tags[i]);\r
321 +       notmuch_query_set_omit_excluded (o.query, exclude);\r
322      }\r
323  \r
324 -    switch (output) {\r
325 +    switch (o.output) {\r
326      default:\r
327      case OUTPUT_SUMMARY:\r
328      case OUTPUT_THREADS:\r
329 -       ret = do_search_threads (format, query, sort, output, offset, limit);\r
330 +       ret = do_search_threads (&o);\r
331         break;\r
332      case OUTPUT_MESSAGES:\r
333      case OUTPUT_FILES:\r
334 -       ret = do_search_messages (format, query, output, offset, limit, dupe);\r
335 +       ret = do_search_messages (&o);\r
336         break;\r
337      case OUTPUT_TAGS:\r
338 -       ret = do_search_tags (notmuch, format, query);\r
339 +       ret = do_search_tags (notmuch, o.format, o.query);\r
340         break;\r
341      }\r
342  \r
343 -    notmuch_query_destroy (query);\r
344 +    notmuch_query_destroy (o.query);\r
345      notmuch_database_destroy (notmuch);\r
346  \r
347 -    talloc_free (format);\r
348 +    talloc_free (o.format);\r
349  \r
350      return ret ? EXIT_FAILURE : EXIT_SUCCESS;\r
351  }\r
352 -- \r
353 2.1.0\r
354 \r