+static int do_reply(notmuch_config_t *config,
+ notmuch_query_t *query,
+ notmuch_show_params_t *params,
+ int format,
+ notmuch_bool_t reply_all)
+{
+ notmuch_messages_t *messages;
+ notmuch_message_t *message;
+ notmuch_status_t status;
+ struct sprinter *sp = NULL;
+ int ret = 0;
+ int (*reply_format_func) (void *ctx,
+ notmuch_config_t *config,
+ notmuch_message_t *message,
+ notmuch_show_params_t *params,
+ notmuch_bool_t reply_all,
+ struct sprinter *sp);
+
+ if (format == FORMAT_JSON || format == FORMAT_SEXP) {
+ unsigned count;
+
+ status = notmuch_query_count_messages_st (query, &count);
+ if (print_status_query ("notmuch reply", query, status))
+ return 1;
+
+ if (count != 1) {
+ fprintf (stderr, "Error: search term did not match precisely one message (matched %d messages).\n", count);
+ return 1;
+ }
+ }
+
+ if (format == FORMAT_HEADERS_ONLY) {
+ reply_format_func = notmuch_reply_format_headers_only;
+ } else if (format == FORMAT_JSON) {
+ reply_format_func = notmuch_reply_format_sprinter;
+ sp = sprinter_json_create (config, stdout);
+ } else if (format == FORMAT_SEXP) {
+ reply_format_func = notmuch_reply_format_sprinter;
+ sp = sprinter_sexp_create (config, stdout);
+ } else {
+ reply_format_func = notmuch_reply_format_default;
+ }
+
+ status = notmuch_query_search_messages_st (query, &messages);
+ if (print_status_query ("notmuch reply", query, status))
+ return 1;
+
+ for (;
+ notmuch_messages_valid (messages);
+ notmuch_messages_move_to_next (messages))
+ {
+ message = notmuch_messages_get (messages);
+
+ ret = reply_format_func(config, config, message, params, reply_all, sp);
+
+ notmuch_message_destroy (message);
+
+ if (ret)
+ break;
+ }
+
+ return ret;
+}
+