--- /dev/null
+Return-Path: <sojkam1@fel.cvut.cz>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id AB9A4429E47\r
+ for <notmuch@notmuchmail.org>; Mon, 3 Nov 2014 15:51:05 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id MEhAvMyixUJ5 for <notmuch@notmuchmail.org>;\r
+ Mon, 3 Nov 2014 15:51:00 -0800 (PST)\r
+Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
+ by olra.theworths.org (Postfix) with ESMTP id 88919429E30\r
+ for <notmuch@notmuchmail.org>; Mon, 3 Nov 2014 15:50:49 -0800 (PST)\r
+Received: from localhost (unknown [192.168.200.7])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id 399905CD012\r
+ for <notmuch@notmuchmail.org>; Tue, 4 Nov 2014 00:50:47 +0100 (CET)\r
+X-Virus-Scanned: IMAP STYX AMAVIS\r
+Received: from max.feld.cvut.cz ([192.168.200.1])\r
+ by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new,\r
+ port 10044) with ESMTP id OrxGelcQJpkw for <notmuch@notmuchmail.org>;\r
+ Tue, 4 Nov 2014 00:50:40 +0100 (CET)\r
+Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id C3EBD5CD007\r
+ for <notmuch@notmuchmail.org>; Tue, 4 Nov 2014 00:50:40 +0100 (CET)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
+ (envelope-from <sojkam1@fel.cvut.cz>)\r
+ id 1XlROM-0005WY-3x; Tue, 04 Nov 2014 00:50:34 +0100\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 04/10] cli: search: Split notmuch_search_command to smaller\r
+ functions\r
+Date: Tue, 4 Nov 2014 00:50:16 +0100\r
+Message-Id: <1415058622-21162-5-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.1.1\r
+In-Reply-To: <1415058622-21162-1-git-send-email-sojkam1@fel.cvut.cz>\r
+References: <1415058622-21162-1-git-send-email-sojkam1@fel.cvut.cz>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 03 Nov 2014 23:51:06 -0000\r
+\r
+In the next commit, these functions will be used to share some\r
+functionality between search and address commands.\r
+---\r
+ notmuch-search.c | 155 ++++++++++++++++++++++++++++++-------------------------\r
+ 1 file changed, 86 insertions(+), 69 deletions(-)\r
+\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 6765a16..f115359 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -471,6 +471,89 @@ do_search_tags (const search_context_t *ctx)\r
+ return 0;\r
+ }\r
+ \r
++static int\r
++_notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])\r
++{\r
++ char *query_str;\r
++ unsigned int i;\r
++\r
++ switch (ctx->format_sel) {\r
++ case NOTMUCH_FORMAT_TEXT:\r
++ ctx->format = sprinter_text_create (config, stdout);\r
++ break;\r
++ case NOTMUCH_FORMAT_TEXT0:\r
++ if (ctx->output == OUTPUT_SUMMARY) {\r
++ fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");\r
++ return EXIT_FAILURE;\r
++ }\r
++ ctx->format = sprinter_text0_create (config, stdout);\r
++ break;\r
++ case NOTMUCH_FORMAT_JSON:\r
++ ctx->format = sprinter_json_create (config, stdout);\r
++ break;\r
++ case NOTMUCH_FORMAT_SEXP:\r
++ ctx->format = sprinter_sexp_create (config, stdout);\r
++ break;\r
++ default:\r
++ /* this should never happen */\r
++ INTERNAL_ERROR("no output format selected");\r
++ }\r
++\r
++ notmuch_exit_if_unsupported_format ();\r
++\r
++ if (notmuch_database_open (notmuch_config_get_database_path (config),\r
++ NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))\r
++ return EXIT_FAILURE;\r
++\r
++ query_str = query_string_from_args (ctx->notmuch, argc, argv);\r
++ if (query_str == NULL) {\r
++ fprintf (stderr, "Out of memory.\n");\r
++ return EXIT_FAILURE;\r
++ }\r
++ if (*query_str == '\0') {\r
++ fprintf (stderr, "Error: notmuch search requires at least one search term.\n");\r
++ return EXIT_FAILURE;\r
++ }\r
++\r
++ ctx->query = notmuch_query_create (ctx->notmuch, query_str);\r
++ if (ctx->query == NULL) {\r
++ fprintf (stderr, "Out of memory\n");\r
++ return EXIT_FAILURE;\r
++ }\r
++\r
++ notmuch_query_set_sort (ctx->query, ctx->sort);\r
++\r
++ if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {\r
++ /* If we are not doing summary output there is nowhere to\r
++ * print the excluded flag so fall back on including the\r
++ * excluded messages. */\r
++ fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");\r
++ ctx->exclude = NOTMUCH_EXCLUDE_FALSE;\r
++ }\r
++\r
++ if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {\r
++ const char **search_exclude_tags;\r
++ size_t search_exclude_tags_length;\r
++\r
++ search_exclude_tags = notmuch_config_get_search_exclude_tags\r
++ (config, &search_exclude_tags_length);\r
++ for (i = 0; i < search_exclude_tags_length; i++)\r
++ notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);\r
++ notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);\r
++ }\r
++\r
++ return 0;\r
++}\r
++\r
++static void\r
++_notmuch_search_cleanup (search_context_t *ctx)\r
++{\r
++ notmuch_query_destroy (ctx->query);\r
++ notmuch_database_destroy (ctx->notmuch);\r
++\r
++ talloc_free (ctx->format);\r
++}\r
++\r
+ int\r
+ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ {\r
+@@ -484,9 +567,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ .dupe = -1,\r
+ };\r
+ search_context_t *ctx = &search_context;\r
+- char *query_str;\r
+ int opt_index, ret;\r
+- unsigned int i;\r
+ \r
+ notmuch_opt_desc_t options[] = {\r
+ { NOTMUCH_OPT_KEYWORD, &ctx->sort, "sort", 's',\r
+@@ -534,71 +615,10 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ return EXIT_FAILURE;\r
+ }\r
+ \r
+- switch (ctx->format_sel) {\r
+- case NOTMUCH_FORMAT_TEXT:\r
+- ctx->format = sprinter_text_create (config, stdout);\r
+- break;\r
+- case NOTMUCH_FORMAT_TEXT0:\r
+- if (ctx->output == OUTPUT_SUMMARY) {\r
+- fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");\r
+- return EXIT_FAILURE;\r
+- }\r
+- ctx->format = sprinter_text0_create (config, stdout);\r
+- break;\r
+- case NOTMUCH_FORMAT_JSON:\r
+- ctx->format = sprinter_json_create (config, stdout);\r
+- break;\r
+- case NOTMUCH_FORMAT_SEXP:\r
+- ctx->format = sprinter_sexp_create (config, stdout);\r
+- break;\r
+- default:\r
+- /* this should never happen */\r
+- INTERNAL_ERROR("no output format selected");\r
+- }\r
+-\r
+- notmuch_exit_if_unsupported_format ();\r
+-\r
+- if (notmuch_database_open (notmuch_config_get_database_path (config),\r
+- NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))\r
++ if (_notmuch_search_prepare (ctx, config,\r
++ argc - opt_index, argv + opt_index))\r
+ return EXIT_FAILURE;\r
+ \r
+- query_str = query_string_from_args (ctx->notmuch, argc-opt_index, argv+opt_index);\r
+- if (query_str == NULL) {\r
+- fprintf (stderr, "Out of memory.\n");\r
+- return EXIT_FAILURE;\r
+- }\r
+- if (*query_str == '\0') {\r
+- fprintf (stderr, "Error: notmuch search requires at least one search term.\n");\r
+- return EXIT_FAILURE;\r
+- }\r
+-\r
+- ctx->query = notmuch_query_create (ctx->notmuch, query_str);\r
+- if (ctx->query == NULL) {\r
+- fprintf (stderr, "Out of memory\n");\r
+- return EXIT_FAILURE;\r
+- }\r
+-\r
+- notmuch_query_set_sort (ctx->query, ctx->sort);\r
+-\r
+- if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {\r
+- /* If we are not doing summary output there is nowhere to\r
+- * print the excluded flag so fall back on including the\r
+- * excluded messages. */\r
+- fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");\r
+- ctx->exclude = NOTMUCH_EXCLUDE_FALSE;\r
+- }\r
+-\r
+- if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {\r
+- const char **search_exclude_tags;\r
+- size_t search_exclude_tags_length;\r
+-\r
+- search_exclude_tags = notmuch_config_get_search_exclude_tags\r
+- (config, &search_exclude_tags_length);\r
+- for (i = 0; i < search_exclude_tags_length; i++)\r
+- notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);\r
+- notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);\r
+- }\r
+-\r
+ if (ctx->output == OUTPUT_SUMMARY ||\r
+ ctx->output == OUTPUT_THREADS)\r
+ ret = do_search_threads (ctx);\r
+@@ -613,10 +633,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ ret = 1;\r
+ }\r
+ \r
+- notmuch_query_destroy (ctx->query);\r
+- notmuch_database_destroy (ctx->notmuch);\r
+-\r
+- talloc_free (ctx->format);\r
++ _notmuch_search_cleanup (ctx);\r
+ \r
+ return ret ? EXIT_FAILURE : EXIT_SUCCESS;\r
+ }\r
+-- \r
+2.1.1\r
+\r