[PATCH v2 02/10] cli: search: Move more variables into search_context_t
[notmuch-archives.git] / a8 / 533b1c8a61891e52a986f6fb2e7a24a4a03baf
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 0580F429E3C\r
6         for <notmuch@notmuchmail.org>; Mon,  3 Nov 2014 15:50:59 -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: -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 OP4B65rPY69E for <notmuch@notmuchmail.org>;\r
16         Mon,  3 Nov 2014 15:50:54 -0800 (PST)\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 C8E74429E29\r
19         for <notmuch@notmuchmail.org>; Mon,  3 Nov 2014 15:50:47 -0800 (PST)\r
20 Received: from localhost (unknown [192.168.200.7])\r
21         by max.feld.cvut.cz (Postfix) with ESMTP id 41CB15CD014\r
22         for <notmuch@notmuchmail.org>; Tue,  4 Nov 2014 00:50:47 +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 J1nRsW_lV2gy for <notmuch@notmuchmail.org>;\r
27         Tue,  4 Nov 2014 00:50: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 C8FF45CD009\r
30         for <notmuch@notmuchmail.org>; Tue,  4 Nov 2014 00:50:39 +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 1XlROM-0005WQ-2H; Tue, 04 Nov 2014 00:50:34 +0100\r
34 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
35 To: notmuch@notmuchmail.org\r
36 Subject: [PATCH v2 02/10] cli: search: Move more variables into\r
37         search_context_t\r
38 Date: Tue,  4 Nov 2014 00:50:14 +0100\r
39 Message-Id: <1415058622-21162-3-git-send-email-sojkam1@fel.cvut.cz>\r
40 X-Mailer: git-send-email 2.1.1\r
41 In-Reply-To: <1415058622-21162-1-git-send-email-sojkam1@fel.cvut.cz>\r
42 References: <1415058622-21162-1-git-send-email-sojkam1@fel.cvut.cz>\r
43 X-BeenThere: notmuch@notmuchmail.org\r
44 X-Mailman-Version: 2.1.13\r
45 Precedence: list\r
46 List-Id: "Use and development of the notmuch mail system."\r
47         <notmuch.notmuchmail.org>\r
48 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
50 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
51 List-Post: <mailto:notmuch@notmuchmail.org>\r
52 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
53 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
54         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
55 X-List-Received-Date: Mon, 03 Nov 2014 23:50:59 -0000\r
56 \r
57 Just refactoring, no functional changes.\r
58 ---\r
59  notmuch-search.c | 49 ++++++++++++++++++++++++++-----------------------\r
60  1 file changed, 26 insertions(+), 23 deletions(-)\r
61 \r
62 diff --git a/notmuch-search.c b/notmuch-search.c\r
63 index 2c47b80..3d2012b 100644\r
64 --- a/notmuch-search.c\r
65 +++ b/notmuch-search.c\r
66 @@ -34,8 +34,18 @@ typedef enum {\r
67  \r
68  #define OUTPUT_ADDRESS_FLAGS (OUTPUT_SENDER | OUTPUT_RECIPIENTS)\r
69  \r
70 +typedef enum {\r
71 +    NOTMUCH_FORMAT_JSON,\r
72 +    NOTMUCH_FORMAT_TEXT,\r
73 +    NOTMUCH_FORMAT_TEXT0,\r
74 +    NOTMUCH_FORMAT_SEXP\r
75 +} format_sel_t;\r
76 +\r
77  typedef struct {\r
78 +    notmuch_database_t *notmuch;\r
79 +    format_sel_t format_sel;\r
80      sprinter_t *format;\r
81 +    notmuch_exclude_t exclude;\r
82      notmuch_query_t *query;\r
83      notmuch_sort_t sort;\r
84      output_t output;\r
85 @@ -413,14 +423,14 @@ do_search_messages (search_context_t *ctx)\r
86  }\r
87  \r
88  static int\r
89 -do_search_tags (notmuch_database_t *notmuch,\r
90 -               const search_context_t *ctx)\r
91 +do_search_tags (const search_context_t *ctx)\r
92  {\r
93      notmuch_messages_t *messages = NULL;\r
94      notmuch_tags_t *tags;\r
95      const char *tag;\r
96      sprinter_t *format = ctx->format;\r
97      notmuch_query_t *query = ctx->query;\r
98 +    notmuch_database_t *notmuch = ctx->notmuch;\r
99  \r
100      /* should the following only special case if no excluded terms\r
101       * specified? */\r
102 @@ -464,8 +474,9 @@ do_search_tags (notmuch_database_t *notmuch,\r
103  int\r
104  notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
105  {\r
106 -    notmuch_database_t *notmuch;\r
107      search_context_t ctx = {\r
108 +       .format_sel = NOTMUCH_FORMAT_TEXT,\r
109 +       .exclude = NOTMUCH_EXCLUDE_TRUE,\r
110         .sort = NOTMUCH_SORT_NEWEST_FIRST,\r
111         .output = 0,\r
112         .offset = 0,\r
113 @@ -474,22 +485,14 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
114      };\r
115      char *query_str;\r
116      int opt_index, ret;\r
117 -    notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;\r
118      unsigned int i;\r
119  \r
120 -    enum {\r
121 -       NOTMUCH_FORMAT_JSON,\r
122 -       NOTMUCH_FORMAT_TEXT,\r
123 -       NOTMUCH_FORMAT_TEXT0,\r
124 -       NOTMUCH_FORMAT_SEXP\r
125 -    } format_sel = NOTMUCH_FORMAT_TEXT;\r
126 -\r
127      notmuch_opt_desc_t options[] = {\r
128         { NOTMUCH_OPT_KEYWORD, &ctx.sort, "sort", 's',\r
129           (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },\r
130                                   { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },\r
131                                   { 0, 0 } } },\r
132 -       { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',\r
133 +       { NOTMUCH_OPT_KEYWORD, &ctx.format_sel, "format", 'f',\r
134           (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },\r
135                                   { "sexp", NOTMUCH_FORMAT_SEXP },\r
136                                   { "text", NOTMUCH_FORMAT_TEXT },\r
137 @@ -505,7 +508,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
138                                   { "files", OUTPUT_FILES },\r
139                                   { "tags", OUTPUT_TAGS },\r
140                                   { 0, 0 } } },\r
141 -        { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',\r
142 +        { NOTMUCH_OPT_KEYWORD, &ctx.exclude, "exclude", 'x',\r
143            (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },\r
144                                    { "false", NOTMUCH_EXCLUDE_FALSE },\r
145                                    { "flag", NOTMUCH_EXCLUDE_FLAG },\r
146 @@ -530,7 +533,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
147          return EXIT_FAILURE;\r
148      }\r
149  \r
150 -    switch (format_sel) {\r
151 +    switch (ctx.format_sel) {\r
152      case NOTMUCH_FORMAT_TEXT:\r
153         ctx.format = sprinter_text_create (config, stdout);\r
154         break;\r
155 @@ -555,10 +558,10 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
156      notmuch_exit_if_unsupported_format ();\r
157  \r
158      if (notmuch_database_open (notmuch_config_get_database_path (config),\r
159 -                              NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
160 +                              NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx.notmuch))\r
161         return EXIT_FAILURE;\r
162  \r
163 -    query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);\r
164 +    query_str = query_string_from_args (ctx.notmuch, argc-opt_index, argv+opt_index);\r
165      if (query_str == NULL) {\r
166         fprintf (stderr, "Out of memory.\n");\r
167         return EXIT_FAILURE;\r
168 @@ -568,7 +571,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
169         return EXIT_FAILURE;\r
170      }\r
171  \r
172 -    ctx.query = notmuch_query_create (notmuch, query_str);\r
173 +    ctx.query = notmuch_query_create (ctx.notmuch, query_str);\r
174      if (ctx.query == NULL) {\r
175         fprintf (stderr, "Out of memory\n");\r
176         return EXIT_FAILURE;\r
177 @@ -576,15 +579,15 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
178  \r
179      notmuch_query_set_sort (ctx.query, ctx.sort);\r
180  \r
181 -    if (exclude == NOTMUCH_EXCLUDE_FLAG && ctx.output != OUTPUT_SUMMARY) {\r
182 +    if (ctx.exclude == NOTMUCH_EXCLUDE_FLAG && ctx.output != OUTPUT_SUMMARY) {\r
183         /* If we are not doing summary output there is nowhere to\r
184          * print the excluded flag so fall back on including the\r
185          * excluded messages. */\r
186         fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");\r
187 -       exclude = NOTMUCH_EXCLUDE_FALSE;\r
188 +       ctx.exclude = NOTMUCH_EXCLUDE_FALSE;\r
189      }\r
190  \r
191 -    if (exclude != NOTMUCH_EXCLUDE_FALSE) {\r
192 +    if (ctx.exclude != NOTMUCH_EXCLUDE_FALSE) {\r
193         const char **search_exclude_tags;\r
194         size_t search_exclude_tags_length;\r
195  \r
196 @@ -592,7 +595,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
197             (config, &search_exclude_tags_length);\r
198         for (i = 0; i < search_exclude_tags_length; i++)\r
199             notmuch_query_add_tag_exclude (ctx.query, search_exclude_tags[i]);\r
200 -       notmuch_query_set_omit_excluded (ctx.query, exclude);\r
201 +       notmuch_query_set_omit_excluded (ctx.query, ctx.exclude);\r
202      }\r
203  \r
204      if (ctx.output == OUTPUT_SUMMARY ||\r
205 @@ -603,14 +606,14 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
206              (ctx.output & OUTPUT_ADDRESS_FLAGS && !(ctx.output & ~OUTPUT_ADDRESS_FLAGS)))\r
207         ret = do_search_messages (&ctx);\r
208      else if (ctx.output == OUTPUT_TAGS)\r
209 -       ret = do_search_tags (notmuch, &ctx);\r
210 +       ret = do_search_tags (&ctx);\r
211      else {\r
212         fprintf (stderr, "Error: the combination of outputs is not supported.\n");\r
213         ret = 1;\r
214      }\r
215  \r
216      notmuch_query_destroy (ctx.query);\r
217 -    notmuch_database_destroy (notmuch);\r
218 +    notmuch_database_destroy (ctx.notmuch);\r
219  \r
220      talloc_free (ctx.format);\r
221  \r
222 -- \r
223 2.1.1\r
224 \r