--- /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 E138F431FC3\r
+ for <notmuch@notmuchmail.org>; Tue, 4 Nov 2014 16:26:52 -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 WDPNgrM8VZtC for <notmuch@notmuchmail.org>;\r
+ Tue, 4 Nov 2014 16:26:47 -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 0D058431FC7\r
+ for <notmuch@notmuchmail.org>; Tue, 4 Nov 2014 16:26:23 -0800 (PST)\r
+Received: from localhost (unknown [192.168.200.7])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id 735785CD1A6\r
+ for <notmuch@notmuchmail.org>; Wed, 5 Nov 2014 01:26:22 +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 YLdHnPTDF5bR for <notmuch@notmuchmail.org>;\r
+ Wed, 5 Nov 2014 01:26:17 +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 27E315CD1AC\r
+ for <notmuch@notmuchmail.org>; Wed, 5 Nov 2014 01:26:14 +0100 (CET)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
+ (envelope-from <sojkam1@fel.cvut.cz>)\r
+ id 1XloQI-0005Cu-R5; Wed, 05 Nov 2014 01:26:06 +0100\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 04/10] cli: search: Split notmuch_search_command to smaller\r
+ functions\r
+Date: Wed, 5 Nov 2014 01:25:53 +0100\r
+Message-Id: <1415147159-19946-5-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.1.1\r
+In-Reply-To: <1415147159-19946-1-git-send-email-sojkam1@fel.cvut.cz>\r
+References: <1415147159-19946-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: Wed, 05 Nov 2014 00:26:53 -0000\r
+\r
+In a subsequent 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