--- /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 AA4DF431FC4\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:39:32 -0700 (PDT)\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 pn6wV4mUY5rd for <notmuch@notmuchmail.org>;\r
+ Mon, 22 Sep 2014 02:39:28 -0700 (PDT)\r
+Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
+ by olra.theworths.org (Postfix) with ESMTP id E8188431FB6\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:39:27 -0700 (PDT)\r
+Received: from localhost (unknown [192.168.200.7])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id 65A6E5CCE60;\r
+ Mon, 22 Sep 2014 11:39:27 +0200 (CEST)\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)\r
+ with ESMTP id 4HFBvm9VwXgd; Mon, 22 Sep 2014 11:39:22 +0200 (CEST)\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 6321D5CCE61;\r
+ Mon, 22 Sep 2014 11:39:22 +0200 (CEST)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
+ (envelope-from <sojkam1@fel.cvut.cz>)\r
+ id 1XW05U-0001w8-KN; Mon, 22 Sep 2014 11:39:16 +0200\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 1/5] cli: Refactor option passing in the search command\r
+Date: Mon, 22 Sep 2014 11:37:55 +0200\r
+Message-Id: <1411378679-7307-2-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <1411378679-7307-1-git-send-email-sojkam1@fel.cvut.cz>\r
+References: <1411378679-7307-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, 22 Sep 2014 09:39:32 -0000\r
+\r
+Many functions that implement the search command need to access command\r
+line options. Instead of passing each option in a separate variable, put\r
+them in a structure and pass only this structure.\r
+\r
+This will become handy in the following patches.\r
+---\r
+ notmuch-search.c | 122 ++++++++++++++++++++++++++++---------------------------\r
+ 1 file changed, 62 insertions(+), 60 deletions(-)\r
+\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index bc9be45..5ac2a26 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -30,6 +30,16 @@ typedef enum {\r
+ OUTPUT_TAGS\r
+ } output_t;\r
+ \r
++typedef struct {\r
++ sprinter_t *format;\r
++ notmuch_query_t *query;\r
++ notmuch_sort_t sort;\r
++ output_t output;\r
++ int offset;\r
++ int limit;\r
++ int dupe;\r
++} search_options_t;\r
++\r
+ /* Return two stable query strings that identify exactly the matched\r
+ * and unmatched messages currently in thread. If there are no\r
+ * matched or unmatched messages, the returned buffers will be\r
+@@ -70,46 +80,42 @@ get_thread_query (notmuch_thread_t *thread,\r
+ }\r
+ \r
+ static int\r
+-do_search_threads (sprinter_t *format,\r
+- notmuch_query_t *query,\r
+- notmuch_sort_t sort,\r
+- output_t output,\r
+- int offset,\r
+- int limit)\r
++do_search_threads (search_options_t *o)\r
+ {\r
+ notmuch_thread_t *thread;\r
+ notmuch_threads_t *threads;\r
+ notmuch_tags_t *tags;\r
++ sprinter_t *format = o->format;\r
+ time_t date;\r
+ int i;\r
+ \r
+- if (offset < 0) {\r
+- offset += notmuch_query_count_threads (query);\r
+- if (offset < 0)\r
+- offset = 0;\r
++ if (o->offset < 0) {\r
++ o->offset += notmuch_query_count_threads (o->query);\r
++ if (o->offset < 0)\r
++ o->offset = 0;\r
+ }\r
+ \r
+- threads = notmuch_query_search_threads (query);\r
++ threads = notmuch_query_search_threads (o->query);\r
+ if (threads == NULL)\r
+ return 1;\r
+ \r
+ format->begin_list (format);\r
+ \r
+ for (i = 0;\r
+- notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);\r
++ notmuch_threads_valid (threads) && (o->limit < 0 || i < o->offset + o->limit);\r
+ notmuch_threads_move_to_next (threads), i++)\r
+ {\r
+ thread = notmuch_threads_get (threads);\r
+ \r
+- if (i < offset) {\r
++ if (i < o->offset) {\r
+ notmuch_thread_destroy (thread);\r
+ continue;\r
+ }\r
+ \r
+- if (output == OUTPUT_THREADS) {\r
++ if (o->output == OUTPUT_THREADS) {\r
+ format->set_prefix (format, "thread");\r
+ format->string (format,\r
+- notmuch_thread_get_thread_id (thread));\r
++ notmuch_thread_get_thread_id (thread));\r
+ format->separator (format);\r
+ } else { /* output == OUTPUT_SUMMARY */\r
+ void *ctx_quote = talloc_new (thread);\r
+@@ -123,7 +129,7 @@ do_search_threads (sprinter_t *format,\r
+ \r
+ format->begin_map (format);\r
+ \r
+- if (sort == NOTMUCH_SORT_OLDEST_FIRST)\r
++ if (o->sort == NOTMUCH_SORT_OLDEST_FIRST)\r
+ date = notmuch_thread_get_oldest_date (thread);\r
+ else\r
+ date = notmuch_thread_get_newest_date (thread);\r
+@@ -215,40 +221,36 @@ do_search_threads (sprinter_t *format,\r
+ }\r
+ \r
+ static int\r
+-do_search_messages (sprinter_t *format,\r
+- notmuch_query_t *query,\r
+- output_t output,\r
+- int offset,\r
+- int limit,\r
+- int dupe)\r
++do_search_messages (search_options_t *o)\r
+ {\r
+ notmuch_message_t *message;\r
+ notmuch_messages_t *messages;\r
+ notmuch_filenames_t *filenames;\r
++ sprinter_t *format = o->format;\r
+ int i;\r
+ \r
+- if (offset < 0) {\r
+- offset += notmuch_query_count_messages (query);\r
+- if (offset < 0)\r
+- offset = 0;\r
++ if (o->offset < 0) {\r
++ o->offset += notmuch_query_count_messages (o->query);\r
++ if (o->offset < 0)\r
++ o->offset = 0;\r
+ }\r
+ \r
+- messages = notmuch_query_search_messages (query);\r
++ messages = notmuch_query_search_messages (o->query);\r
+ if (messages == NULL)\r
+ return 1;\r
+ \r
+ format->begin_list (format);\r
+ \r
+ for (i = 0;\r
+- notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);\r
++ notmuch_messages_valid (messages) && (o->limit < 0 || i < o->offset + o->limit);\r
+ notmuch_messages_move_to_next (messages), i++)\r
+ {\r
+- if (i < offset)\r
++ if (i < o->offset)\r
+ continue;\r
+ \r
+ message = notmuch_messages_get (messages);\r
+ \r
+- if (output == OUTPUT_FILES) {\r
++ if (o->output == OUTPUT_FILES) {\r
+ int j;\r
+ filenames = notmuch_message_get_filenames (message);\r
+ \r
+@@ -256,7 +258,7 @@ do_search_messages (sprinter_t *format,\r
+ notmuch_filenames_valid (filenames);\r
+ notmuch_filenames_move_to_next (filenames), j++)\r
+ {\r
+- if (dupe < 0 || dupe == j) {\r
++ if (o->dupe < 0 || o->dupe == j) {\r
+ format->string (format, notmuch_filenames_get (filenames));\r
+ format->separator (format);\r
+ }\r
+@@ -333,16 +335,16 @@ int\r
+ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ {\r
+ notmuch_database_t *notmuch;\r
+- notmuch_query_t *query;\r
++ search_options_t o = {\r
++ .sort = NOTMUCH_SORT_NEWEST_FIRST,\r
++ .output = OUTPUT_SUMMARY,\r
++ .offset = 0,\r
++ .limit = -1, /* unlimited */\r
++ .dupe = -1,\r
++ };\r
+ char *query_str;\r
+- notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;\r
+- sprinter_t *format = NULL;\r
+ int opt_index, ret;\r
+- output_t output = OUTPUT_SUMMARY;\r
+- int offset = 0;\r
+- int limit = -1; /* unlimited */\r
+ notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;\r
+- int dupe = -1;\r
+ unsigned int i;\r
+ \r
+ enum {\r
+@@ -353,7 +355,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ } format_sel = NOTMUCH_FORMAT_TEXT;\r
+ \r
+ notmuch_opt_desc_t options[] = {\r
+- { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',\r
++ { NOTMUCH_OPT_KEYWORD, &o.sort, "sort", 's',\r
+ (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },\r
+ { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },\r
+ { 0, 0 } } },\r
+@@ -364,7 +366,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ { "text0", NOTMUCH_FORMAT_TEXT0 },\r
+ { 0, 0 } } },\r
+ { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },\r
+- { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',\r
++ { NOTMUCH_OPT_KEYWORD, &o.output, "output", 'o',\r
+ (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },\r
+ { "threads", OUTPUT_THREADS },\r
+ { "messages", OUTPUT_MESSAGES },\r
+@@ -377,9 +379,9 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ { "flag", NOTMUCH_EXCLUDE_FLAG },\r
+ { "all", NOTMUCH_EXCLUDE_ALL },\r
+ { 0, 0 } } },\r
+- { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },\r
+- { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0 },\r
+- { NOTMUCH_OPT_INT, &dupe, "duplicate", 'D', 0 },\r
++ { NOTMUCH_OPT_INT, &o.offset, "offset", 'O', 0 },\r
++ { NOTMUCH_OPT_INT, &o.limit, "limit", 'L', 0 },\r
++ { NOTMUCH_OPT_INT, &o.dupe, "duplicate", 'D', 0 },\r
+ { 0, 0, 0, 0, 0 }\r
+ };\r
+ \r
+@@ -389,20 +391,20 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ \r
+ switch (format_sel) {\r
+ case NOTMUCH_FORMAT_TEXT:\r
+- format = sprinter_text_create (config, stdout);\r
++ o.format = sprinter_text_create (config, stdout);\r
+ break;\r
+ case NOTMUCH_FORMAT_TEXT0:\r
+- if (output == OUTPUT_SUMMARY) {\r
++ if (o.output == OUTPUT_SUMMARY) {\r
+ fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");\r
+ return EXIT_FAILURE;\r
+ }\r
+- format = sprinter_text0_create (config, stdout);\r
++ o.format = sprinter_text0_create (config, stdout);\r
+ break;\r
+ case NOTMUCH_FORMAT_JSON:\r
+- format = sprinter_json_create (config, stdout);\r
++ o.format = sprinter_json_create (config, stdout);\r
+ break;\r
+ case NOTMUCH_FORMAT_SEXP:\r
+- format = sprinter_sexp_create (config, stdout);\r
++ o.format = sprinter_sexp_create (config, stdout);\r
+ break;\r
+ default:\r
+ /* this should never happen */\r
+@@ -425,15 +427,15 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ return EXIT_FAILURE;\r
+ }\r
+ \r
+- query = notmuch_query_create (notmuch, query_str);\r
+- if (query == NULL) {\r
++ o.query = notmuch_query_create (notmuch, query_str);\r
++ if (o.query == NULL) {\r
+ fprintf (stderr, "Out of memory\n");\r
+ return EXIT_FAILURE;\r
+ }\r
+ \r
+- notmuch_query_set_sort (query, sort);\r
++ notmuch_query_set_sort (o.query, o.sort);\r
+ \r
+- if (exclude == NOTMUCH_EXCLUDE_FLAG && output != OUTPUT_SUMMARY) {\r
++ if (exclude == NOTMUCH_EXCLUDE_FLAG && o.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
+@@ -448,29 +450,29 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\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 (query, search_exclude_tags[i]);\r
+- notmuch_query_set_omit_excluded (query, exclude);\r
++ notmuch_query_add_tag_exclude (o.query, search_exclude_tags[i]);\r
++ notmuch_query_set_omit_excluded (o.query, exclude);\r
+ }\r
+ \r
+- switch (output) {\r
++ switch (o.output) {\r
+ default:\r
+ case OUTPUT_SUMMARY:\r
+ case OUTPUT_THREADS:\r
+- ret = do_search_threads (format, query, sort, output, offset, limit);\r
++ ret = do_search_threads (&o);\r
+ break;\r
+ case OUTPUT_MESSAGES:\r
+ case OUTPUT_FILES:\r
+- ret = do_search_messages (format, query, output, offset, limit, dupe);\r
++ ret = do_search_messages (&o);\r
+ break;\r
+ case OUTPUT_TAGS:\r
+- ret = do_search_tags (notmuch, format, query);\r
++ ret = do_search_tags (notmuch, o.format, o.query);\r
+ break;\r
+ }\r
+ \r
+- notmuch_query_destroy (query);\r
++ notmuch_query_destroy (o.query);\r
+ notmuch_database_destroy (notmuch);\r
+ \r
+- talloc_free (format);\r
++ talloc_free (o.format);\r
+ \r
+ return ret ? EXIT_FAILURE : EXIT_SUCCESS;\r
+ }\r
+-- \r
+2.1.0\r
+\r