--- /dev/null
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id A0A4B6DE396E\r
+ for <notmuch@notmuchmail.org>; Sat, 13 Aug 2016 04:39:20 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.573\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.573 tagged_above=-999 required=5 tests=[AWL=0.147,\r
+ DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_LOW=-0.7,\r
+ RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id gmMDVdnOM9nr for <notmuch@notmuchmail.org>;\r
+ Sat, 13 Aug 2016 04:39:12 -0700 (PDT)\r
+Received: from mail-wm0-f68.google.com (mail-wm0-f68.google.com\r
+ [74.125.82.68]) by arlo.cworth.org (Postfix) with ESMTPS id 8A1076DE38C3 for\r
+ <notmuch@notmuchmail.org>; Sat, 13 Aug 2016 04:39:06 -0700 (PDT)\r
+Received: by mail-wm0-f68.google.com with SMTP id i138so2003901wmf.3\r
+ for <notmuch@notmuchmail.org>; Sat, 13 Aug 2016 04:39:06 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=nikula-org.20150623.gappssmtp.com; s=20150623;\r
+ h=from:to:cc:subject:date:message-id:in-reply-to:references\r
+ :in-reply-to:references;\r
+ bh=0gTT1Cs2x5flbIL5lOKo8lgQoRdqG9bi3NSycpTh6/I=;\r
+ b=cG01yENNqs2IXTeaO6+h661JOurOsGj+j0sGc5a9TKmNyvc0kS/8hMMItDl0BEb0bU\r
+ tBXZQy/ZI2MFj6G50KLcoIbyVC+vd5srbqkMa7dI6nBtJ+BKCDPkrjCs6sR+1BjgQXQd\r
+ AndhrTnMXVLnFaXJ7zV2F7RzO1NWx2ysX+HeliR4DUMnc6rW7PGHMAPmtaKbgicOOUjY\r
+ w9FK0P6x3rioVsTdEupH+ZIbfTGWEgFTb+5e/EiILjBCjFdY8azr6lnTCABqb7UCSXxT\r
+ 5ZEw+b5QT6rqrxZTLyJJakacq3+oliw4QXkiIlKWg03UQshKctDHfC0QGiWktRy48UxQ\r
+ 53jg==\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=1e100.net; s=20130820;\r
+ h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
+ :references:in-reply-to:references;\r
+ bh=0gTT1Cs2x5flbIL5lOKo8lgQoRdqG9bi3NSycpTh6/I=;\r
+ b=fQQi2gsnLDYi9SNsLYlxDjHlcwkec9mHlbJmtPSipAkYOQXE39Us5cgcQRaVQRhB1J\r
+ sRHfeVUB5kZQHjyotBFwJreJAPM0m3bF341W7UfgZBL9ns4MKe4XGnpf3NO3gq7td91X\r
+ behuh2AGhl6gxsY4bNcNcpjiHRUX22KuoWpPvYyeIll/GhRer4lS0Xzfuw+GahC/npYu\r
+ rI6hijHxsPB5VSWh3i+RAcl7h+mT08ojQU9djzt8pc0nAGxpD4NZHmoqj3NoHXB8DwyU\r
+ 0JE6sBFoGUTOks89w8ZMcHC+pMY/O24qUqS+pnPBVJYPUIyoqFDufFrh62ixX3+8JJX/\r
+ 2Tng==\r
+X-Gm-Message-State:\r
+ AEkoouuvpXgZBNZCpj2fWvO83qomQkvT6imaJyZOc5D9tCLZfUu8l30wy87qvqiZWngEug==\r
+X-Received: by 10.194.41.194 with SMTP id h2mr23191319wjl.2.1471088345119;\r
+ Sat, 13 Aug 2016 04:39:05 -0700 (PDT)\r
+Received: from localhost (mobile-access-bcee63-250.dhcp.inet.fi.\r
+ [188.238.99.250])\r
+ by smtp.gmail.com with ESMTPSA id ly9sm11749251wjb.44.2016.08.13.04.39.04\r
+ (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+ Sat, 13 Aug 2016 04:39:04 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Cc: Daniel Kahn Gillmor <dkg@fifthhorseman.net>,\r
+ jani@nikula.org\r
+Subject: [PATCH v2 02/14] cli/reply: push notmuch reply format abstraction\r
+ lower in the stack\r
+Date: Sat, 13 Aug 2016 14:37:26 +0300\r
+Message-Id:\r
+ <3760ff3cc6083d5f24c280a97121beccce8e0c5e.1471088022.git.jani@nikula.org>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <cover.1471088022.git.jani@nikula.org>\r
+References: <cover.1471088022.git.jani@nikula.org>\r
+In-Reply-To: <cover.1471088022.git.jani@nikula.org>\r
+References: <cover.1471088022.git.jani@nikula.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://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: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 13 Aug 2016 11:39:20 -0000\r
+\r
+There's quite a bit of duplication, and some consequent deviation,\r
+between the various notmuch reply format code paths. Perform the query\r
+and message iteration in common code, and make the format specific\r
+functions operate on single messages.\r
+\r
+There should be no functional changes.\r
+---\r
+ notmuch-reply.c | 216 ++++++++++++++++++++++++++------------------------------\r
+ 1 file changed, 101 insertions(+), 115 deletions(-)\r
+\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index 49513732e620..847e306f94d2 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -598,82 +598,42 @@ create_reply_message(void *ctx,\r
+ static int\r
+ notmuch_reply_format_default(void *ctx,\r
+ notmuch_config_t *config,\r
+- notmuch_query_t *query,\r
++ notmuch_message_t *message,\r
+ notmuch_show_params_t *params,\r
+ notmuch_bool_t reply_all,\r
+ unused (sprinter_t *sp))\r
+ {\r
+ GMimeMessage *reply;\r
+- notmuch_messages_t *messages;\r
+- notmuch_message_t *message;\r
+ mime_node_t *root;\r
+- notmuch_status_t status;\r
+ \r
+- status = notmuch_query_search_messages_st (query, &messages);\r
+- if (print_status_query ("notmuch reply", query, status))\r
++ reply = create_reply_message (ctx, config, message, reply_all);\r
++ if (!reply)\r
+ return 1;\r
+ \r
+- for (;\r
+- notmuch_messages_valid (messages);\r
+- notmuch_messages_move_to_next (messages))\r
+- {\r
+- message = notmuch_messages_get (messages);\r
++ show_reply_headers (reply);\r
+ \r
+- reply = create_reply_message (ctx, config, message, reply_all);\r
+-\r
+- /* If reply creation failed, we're out of memory, so don't\r
+- * bother trying any more messages.\r
+- */\r
+- if (!reply) {\r
+- notmuch_message_destroy (message);\r
+- return 1;\r
+- }\r
+-\r
+- show_reply_headers (reply);\r
+-\r
+- g_object_unref (G_OBJECT (reply));\r
+- reply = NULL;\r
+-\r
+- if (mime_node_open (ctx, message, &(params->crypto), &root) == NOTMUCH_STATUS_SUCCESS) {\r
+- format_part_reply (root);\r
+- talloc_free (root);\r
+- }\r
++ g_object_unref (G_OBJECT (reply));\r
+ \r
+- notmuch_message_destroy (message);\r
++ if (mime_node_open (ctx, message, ¶ms->crypto, &root) == NOTMUCH_STATUS_SUCCESS) {\r
++ format_part_reply (root);\r
++ talloc_free (root);\r
+ }\r
++\r
+ return 0;\r
+ }\r
+ \r
+ static int\r
+ notmuch_reply_format_sprinter(void *ctx,\r
+ notmuch_config_t *config,\r
+- notmuch_query_t *query,\r
++ notmuch_message_t *message,\r
+ notmuch_show_params_t *params,\r
+ notmuch_bool_t reply_all,\r
+ sprinter_t *sp)\r
+ {\r
+ GMimeMessage *reply;\r
+- notmuch_messages_t *messages;\r
+- notmuch_message_t *message;\r
+ mime_node_t *node;\r
+- unsigned count;\r
+- notmuch_status_t status;\r
+-\r
+- status = notmuch_query_count_messages_st (query, &count);\r
+- if (print_status_query ("notmuch reply", query, status))\r
+- return 1;\r
+-\r
+- if (count != 1) {\r
+- fprintf (stderr, "Error: search term did not match precisely one message (matched %d messages).\n", count);\r
+- return 1;\r
+- }\r
+ \r
+- status = notmuch_query_search_messages_st (query, &messages);\r
+- if (print_status_query ("notmuch reply", query, status))\r
+- return 1;\r
+-\r
+- message = notmuch_messages_get (messages);\r
+- if (mime_node_open (ctx, message, &(params->crypto), &node) != NOTMUCH_STATUS_SUCCESS)\r
++ if (mime_node_open (ctx, message, ¶ms->crypto, &node) != NOTMUCH_STATUS_SUCCESS)\r
+ return 1;\r
+ \r
+ reply = create_reply_message (ctx, config, message, reply_all);\r
+@@ -686,7 +646,6 @@ notmuch_reply_format_sprinter(void *ctx,\r
+ sp->map_key (sp, "reply-headers");\r
+ format_headers_sprinter (sp, reply, TRUE);\r
+ g_object_unref (G_OBJECT (reply));\r
+- reply = NULL;\r
+ \r
+ /* Start the original */\r
+ sp->map_key (sp, "original");\r
+@@ -694,7 +653,6 @@ notmuch_reply_format_sprinter(void *ctx,\r
+ \r
+ /* End */\r
+ sp->end (sp);\r
+- notmuch_message_destroy (message);\r
+ \r
+ return 0;\r
+ }\r
+@@ -703,65 +661,48 @@ notmuch_reply_format_sprinter(void *ctx,\r
+ static int\r
+ notmuch_reply_format_headers_only(void *ctx,\r
+ notmuch_config_t *config,\r
+- notmuch_query_t *query,\r
++ notmuch_message_t *message,\r
+ unused (notmuch_show_params_t *params),\r
+ notmuch_bool_t reply_all,\r
+ unused (sprinter_t *sp))\r
+ {\r
+ GMimeMessage *reply;\r
+- notmuch_messages_t *messages;\r
+- notmuch_message_t *message;\r
+ const char *in_reply_to, *orig_references, *references;\r
+ char *reply_headers;\r
+- notmuch_status_t status;\r
+ \r
+- status = notmuch_query_search_messages_st (query, &messages);\r
+- if (print_status_query ("notmuch reply", query, status))\r
++ /* The 0 means we do not want headers in a "pretty" order. */\r
++ reply = g_mime_message_new (0);\r
++ if (reply == NULL) {\r
++ fprintf (stderr, "Out of memory\n");\r
+ return 1;\r
++ }\r
+ \r
+- for (;\r
+- notmuch_messages_valid (messages);\r
+- notmuch_messages_move_to_next (messages))\r
+- {\r
+- message = notmuch_messages_get (messages);\r
+-\r
+- /* The 0 means we do not want headers in a "pretty" order. */\r
+- reply = g_mime_message_new (0);\r
+- if (reply == NULL) {\r
+- fprintf (stderr, "Out of memory\n");\r
+- return 1;\r
+- }\r
+-\r
+- in_reply_to = talloc_asprintf (ctx, "<%s>",\r
+- notmuch_message_get_message_id (message));\r
+-\r
+- g_mime_object_set_header (GMIME_OBJECT (reply),\r
+- "In-Reply-To", in_reply_to);\r
++ in_reply_to = talloc_asprintf (ctx, "<%s>",\r
++ notmuch_message_get_message_id (message));\r
+ \r
++ g_mime_object_set_header (GMIME_OBJECT (reply), "In-Reply-To", in_reply_to);\r
+ \r
+- orig_references = notmuch_message_get_header (message, "references");\r
++ orig_references = notmuch_message_get_header (message, "references");\r
+ \r
+- /* We print In-Reply-To followed by References because git format-patch treats them\r
+- * specially. Git does not interpret the other headers specially\r
+- */\r
+- references = talloc_asprintf (ctx, "%s%s%s",\r
+- orig_references ? orig_references : "",\r
+- orig_references ? " " : "",\r
+- in_reply_to);\r
+- g_mime_object_set_header (GMIME_OBJECT (reply),\r
+- "References", references);\r
++ /*\r
++ * We print In-Reply-To followed by References because git\r
++ * format-patch treats them specially. Git does not interpret the\r
++ * other headers specially.\r
++ */\r
++ references = talloc_asprintf (ctx, "%s%s%s",\r
++ orig_references ? orig_references : "",\r
++ orig_references ? " " : "",\r
++ in_reply_to);\r
++ g_mime_object_set_header (GMIME_OBJECT (reply), "References", references);\r
+ \r
+- (void)add_recipients_from_message (reply, config, message, reply_all);\r
++ (void)add_recipients_from_message (reply, config, message, reply_all);\r
+ \r
+- reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));\r
+- printf ("%s", reply_headers);\r
+- free (reply_headers);\r
++ reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));\r
++ printf ("%s", reply_headers);\r
++ free (reply_headers);\r
+ \r
+- g_object_unref (G_OBJECT (reply));\r
+- reply = NULL;\r
++ g_object_unref (G_OBJECT (reply));\r
+ \r
+- notmuch_message_destroy (message);\r
+- }\r
+ return 0;\r
+ }\r
+ \r
+@@ -772,6 +713,70 @@ enum {\r
+ FORMAT_HEADERS_ONLY,\r
+ };\r
+ \r
++static int do_reply(notmuch_config_t *config,\r
++ notmuch_query_t *query,\r
++ notmuch_show_params_t *params,\r
++ int format,\r
++ notmuch_bool_t reply_all)\r
++{\r
++ notmuch_messages_t *messages;\r
++ notmuch_message_t *message;\r
++ notmuch_status_t status;\r
++ struct sprinter *sp = NULL;\r
++ int ret = 0;\r
++ int (*reply_format_func) (void *ctx,\r
++ notmuch_config_t *config,\r
++ notmuch_message_t *message,\r
++ notmuch_show_params_t *params,\r
++ notmuch_bool_t reply_all,\r
++ struct sprinter *sp);\r
++\r
++ if (format == FORMAT_JSON || format == FORMAT_SEXP) {\r
++ unsigned count;\r
++\r
++ status = notmuch_query_count_messages_st (query, &count);\r
++ if (print_status_query ("notmuch reply", query, status))\r
++ return 1;\r
++\r
++ if (count != 1) {\r
++ fprintf (stderr, "Error: search term did not match precisely one message (matched %d messages).\n", count);\r
++ return 1;\r
++ }\r
++ }\r
++\r
++ if (format == FORMAT_HEADERS_ONLY) {\r
++ reply_format_func = notmuch_reply_format_headers_only;\r
++ } else if (format == FORMAT_JSON) {\r
++ reply_format_func = notmuch_reply_format_sprinter;\r
++ sp = sprinter_json_create (config, stdout);\r
++ } else if (format == FORMAT_SEXP) {\r
++ reply_format_func = notmuch_reply_format_sprinter;\r
++ sp = sprinter_sexp_create (config, stdout);\r
++ } else {\r
++ reply_format_func = notmuch_reply_format_default;\r
++ }\r
++\r
++ status = notmuch_query_search_messages_st (query, &messages);\r
++ if (print_status_query ("notmuch reply", query, status))\r
++ return 1;\r
++\r
++ for (;\r
++ notmuch_messages_valid (messages);\r
++ notmuch_messages_move_to_next (messages))\r
++ {\r
++ message = notmuch_messages_get (messages);\r
++\r
++ ret = reply_format_func(config, config, message, params, reply_all, sp);\r
++\r
++ notmuch_message_destroy (message);\r
++\r
++ if (ret)\r
++ break;\r
++ }\r
++\r
++ return ret;\r
++}\r
++\r
+ int\r
+ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+ {\r
+@@ -779,12 +784,6 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+ notmuch_query_t *query;\r
+ char *query_string;\r
+ int opt_index;\r
+- int (*reply_format_func) (void *ctx,\r
+- notmuch_config_t *config,\r
+- notmuch_query_t *query,\r
+- notmuch_show_params_t *params,\r
+- notmuch_bool_t reply_all,\r
+- struct sprinter *sp);\r
+ notmuch_show_params_t params = {\r
+ .part = -1,\r
+ .crypto = {\r
+@@ -795,7 +794,6 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+ };\r
+ int format = FORMAT_DEFAULT;\r
+ int reply_all = TRUE;\r
+- struct sprinter *sp = NULL;\r
+ \r
+ notmuch_opt_desc_t options[] = {\r
+ { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',\r
+@@ -820,18 +818,6 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+ \r
+ notmuch_process_shared_options (argv[0]);\r
+ \r
+- if (format == FORMAT_HEADERS_ONLY) {\r
+- reply_format_func = notmuch_reply_format_headers_only;\r
+- } else if (format == FORMAT_JSON) {\r
+- reply_format_func = notmuch_reply_format_sprinter;\r
+- sp = sprinter_json_create (config, stdout);\r
+- } else if (format == FORMAT_SEXP) {\r
+- reply_format_func = notmuch_reply_format_sprinter;\r
+- sp = sprinter_sexp_create (config, stdout);\r
+- } else {\r
+- reply_format_func = notmuch_reply_format_default;\r
+- }\r
+-\r
+ notmuch_exit_if_unsupported_format ();\r
+ \r
+ query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);\r
+@@ -859,7 +845,7 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+ return EXIT_FAILURE;\r
+ }\r
+ \r
+- if (reply_format_func (config, config, query, ¶ms, reply_all, sp) != 0)\r
++ if (do_reply (config, query, ¶ms, format, reply_all) != 0)\r
+ return EXIT_FAILURE;\r
+ \r
+ notmuch_crypto_cleanup (¶ms.crypto);\r
+-- \r
+2.1.4\r
+\r