Re: [PATCH v5 3/3] Use the structured formatters in notmuch-search.c.
authorAustin Clements <amdragon@MIT.EDU>
Sat, 14 Jul 2012 02:09:54 +0000 (22:09 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:48:17 +0000 (09:48 -0800)
5a/6269bacb11cc7e8f2506263ddb6aa87e518b83 [new file with mode: 0644]

diff --git a/5a/6269bacb11cc7e8f2506263ddb6aa87e518b83 b/5a/6269bacb11cc7e8f2506263ddb6aa87e518b83
new file mode 100644 (file)
index 0000000..b51db70
--- /dev/null
@@ -0,0 +1,591 @@
+Return-Path: <amdragon@mit.edu>\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 9D7D8431FB6\r
+       for <notmuch@notmuchmail.org>; Fri, 13 Jul 2012 19:09:59 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 uXg3NFNtSJQX for <notmuch@notmuchmail.org>;\r
+       Fri, 13 Jul 2012 19:09:57 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-7.mit.edu (DMZ-MAILSEC-SCANNER-7.MIT.EDU\r
+       [18.7.68.36])\r
+       by olra.theworths.org (Postfix) with ESMTP id A22E8431FAE\r
+       for <notmuch@notmuchmail.org>; Fri, 13 Jul 2012 19:09:57 -0700 (PDT)\r
+X-AuditID: 12074424-b7f2a6d0000008bf-16-5000d4f548e4\r
+Received: from mailhub-auth-1.mit.edu ( [18.9.21.35])\r
+       by dmz-mailsec-scanner-7.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id D6.1C.02239.5F4D0005; Fri, 13 Jul 2012 22:09:57 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id q6E29ukA022868; \r
+       Fri, 13 Jul 2012 22:09:56 -0400\r
+Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
+       (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q6E29tcc009849\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Fri, 13 Jul 2012 22:09:55 -0400 (EDT)\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1Sprnu-0000f4-Sr; Fri, 13 Jul 2012 22:09:54 -0400\r
+Date: Fri, 13 Jul 2012 22:09:54 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: Peter Feigl <craven@gmx.net>\r
+Subject: Re: [PATCH v5 3/3] Use the structured formatters in notmuch-search.c.\r
+Message-ID: <20120714020954.GD31670@mit.edu>\r
+References: <20120710191331.GE7332@mit.edu>\r
+       <1342167097-25012-1-git-send-email-craven@gmx.net>\r
+       <1342167097-25012-4-git-send-email-craven@gmx.net>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=iso-8859-1\r
+Content-Disposition: inline\r
+Content-Transfer-Encoding: 8bit\r
+In-Reply-To: <1342167097-25012-4-git-send-email-craven@gmx.net>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFprDKsWRmVeSWpSXmKPExsUixCmqrPv1CkOAwcqtHBZ7G9oZLa7fnMns\r
+       wOSxeNN+No9nq24xBzBFcdmkpOZklqUW6dslcGX8vXiSuWBKXcXnm2tZGxjnxXcxcnBICJhI\r
+       rP4j2MXICWSKSVy4t56ti5GLQ0hgH6PEp9bPUM4GRomVDyayQjgnmSRmbbgK5SxhlPjx/Asz\r
+       SD+LgKrEmpmbWUBsNgENiW37lzOC2CICChLP1jWB2cwC0hLffjczgdjCAn4SFxb1soHYvAI6\r
+       Ept372GEGDqVUWLeit+MEAlBiZMzn7BANOtI7Nx6hw3kbpBBy/9xQITlJZq3zga7gVPAXuLq\r
+       6s3sILaogIrElJPb2CYwCs9CMmkWkkmzECbNQjJpASPLKkbZlNwq3dzEzJzi1GTd4uTEvLzU\r
+       Il1zvdzMEr3UlNJNjKBIYHdR2cHYfEjpEKMAB6MSD2+SP0OAEGtiWXFl7iFGSQ4mJVHeWReB\r
+       QnxJ+SmVGYnFGfFFpTmpxYcYJTiYlUR4zduAcrwpiZVVqUX5MClpDhYlcd7rKTf9hQTSE0tS\r
+       s1NTC1KLYLIyHBxKEryFwIgXEixKTU+tSMvMKUFIM3FwggznARreAlLDW1yQmFucmQ6RP8Wo\r
+       KCXOGwKSEABJZJTmwfXCEtUrRnGgV4R540GqeIBJDq77FdBgJqDBs37+8wcaXJKIkJJqYDT5\r
+       zDhXLax3wr8rR6Mn7fv3RcXMPXfe9DunHMucw3dNL3T7tKZ9W+LNpfHNp80t98+awbA77zO/\r
+       v5IOf0BGftLFKx1zF0xe8KfqSK3bH3/vGfy/vJvub5i5ZottlV7GDE3pPmPJG86LlPa4PE2y\r
+       l5P+dSZa/rZ28PulWf9X7GFqfvS+pzRrSq0SS3FGoqEWc1FxIgDICgKQLwMAAA==\r
+Cc: notmuch@notmuchmail.org\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: Sat, 14 Jul 2012 02:09:59 -0000\r
+\r
+Just one comment below.  Otherwise this patch LGTM (though some of my\r
+comments on the previous patch will affect this one).  It's nice to\r
+see all of that old newline logic disappear.\r
+\r
+Quoth Peter Feigl on Jul 13 at 10:11 am:\r
+> From: <craven@gmx.net>\r
+> \r
+> This patch switches from the current ad-hoc printer to the structured\r
+> output formatter in sprinter.h, sprinter-text-search.c and\r
+> sprinter-json.c.\r
+> \r
+> The JSON tests are changed slightly in order to make them PASS for the\r
+> new structured output formatter.\r
+> \r
+> The Text tests pass without adaptation.\r
+> ---\r
+>  notmuch-search.c | 292 +++++++++++--------------------------------------------\r
+>  test/json        |  18 +---\r
+>  2 files changed, 58 insertions(+), 252 deletions(-)\r
+> \r
+> diff --git a/notmuch-search.c b/notmuch-search.c\r
+> index 3be296d..99fddac 100644\r
+> --- a/notmuch-search.c\r
+> +++ b/notmuch-search.c\r
+> @@ -19,6 +19,7 @@\r
+>   */\r
+>  \r
+>  #include "notmuch-client.h"\r
+> +#include "sprinter.h"\r
+>  \r
+>  typedef enum {\r
+>      OUTPUT_SUMMARY,\r
+> @@ -28,175 +29,8 @@ typedef enum {\r
+>      OUTPUT_TAGS\r
+>  } output_t;\r
+>  \r
+> -typedef struct search_format {\r
+> -    const char *results_start;\r
+> -    const char *item_start;\r
+> -    void (*item_id) (const void *ctx,\r
+> -                 const char *item_type,\r
+> -                 const char *item_id);\r
+> -    void (*thread_summary) (const void *ctx,\r
+> -                        const char *thread_id,\r
+> -                        const time_t date,\r
+> -                        const int matched,\r
+> -                        const int total,\r
+> -                        const char *authors,\r
+> -                        const char *subject);\r
+> -    const char *tag_start;\r
+> -    const char *tag;\r
+> -    const char *tag_sep;\r
+> -    const char *tag_end;\r
+> -    const char *item_sep;\r
+> -    const char *item_end;\r
+> -    const char *results_end;\r
+> -    const char *results_null;\r
+> -} search_format_t;\r
+> -\r
+> -static void\r
+> -format_item_id_text (const void *ctx,\r
+> -                 const char *item_type,\r
+> -                 const char *item_id);\r
+> -\r
+> -static void\r
+> -format_thread_text (const void *ctx,\r
+> -                const char *thread_id,\r
+> -                const time_t date,\r
+> -                const int matched,\r
+> -                const int total,\r
+> -                const char *authors,\r
+> -                const char *subject);\r
+> -static const search_format_t format_text = {\r
+> -    "",\r
+> -    "",\r
+> -        format_item_id_text,\r
+> -        format_thread_text,\r
+> -        " (",\r
+> -            "%s", " ",\r
+> -        ")", "\n",\r
+> -    "",\r
+> -    "\n",\r
+> -    "",\r
+> -};\r
+> -\r
+> -static void\r
+> -format_item_id_json (const void *ctx,\r
+> -                 const char *item_type,\r
+> -                 const char *item_id);\r
+> -\r
+> -static void\r
+> -format_thread_json (const void *ctx,\r
+> -                const char *thread_id,\r
+> -                const time_t date,\r
+> -                const int matched,\r
+> -                const int total,\r
+> -                const char *authors,\r
+> -                const char *subject);\r
+> -\r
+> -/* Any changes to the JSON format should be reflected in the file\r
+> - * devel/schemata. */\r
+> -static const search_format_t format_json = {\r
+> -    "[",\r
+> -    "{",\r
+> -        format_item_id_json,\r
+> -        format_thread_json,\r
+> -        "\"tags\": [",\r
+> -            "\"%s\"", ", ",\r
+> -        "]", ",\n",\r
+> -    "}",\r
+> -    "]\n",\r
+> -    "]\n",\r
+> -};\r
+> -\r
+> -static void\r
+> -format_item_id_text (unused (const void *ctx),\r
+> -                 const char *item_type,\r
+> -                 const char *item_id)\r
+> -{\r
+> -    printf ("%s%s", item_type, item_id);\r
+> -}\r
+> -\r
+> -static char *\r
+> -sanitize_string (const void *ctx, const char *str)\r
+> -{\r
+> -    char *out, *loop;\r
+> -\r
+> -    if (NULL == str)\r
+> -    return NULL;\r
+> -\r
+> -    loop = out = talloc_strdup (ctx, str);\r
+> -\r
+> -    for (; *loop; loop++) {\r
+> -    if ((unsigned char)(*loop) < 32)\r
+> -        *loop = '?';\r
+> -    }\r
+> -    return out;\r
+> -}\r
+> -\r
+> -static void\r
+> -format_thread_text (const void *ctx,\r
+> -                const char *thread_id,\r
+> -                const time_t date,\r
+> -                const int matched,\r
+> -                const int total,\r
+> -                const char *authors,\r
+> -                const char *subject)\r
+> -{\r
+> -    void *ctx_quote = talloc_new (ctx);\r
+> -\r
+> -    printf ("thread:%s %12s [%d/%d] %s; %s",\r
+> -        thread_id,\r
+> -        notmuch_time_relative_date (ctx, date),\r
+> -        matched,\r
+> -        total,\r
+> -        sanitize_string (ctx_quote, authors),\r
+> -        sanitize_string (ctx_quote, subject));\r
+> -\r
+> -    talloc_free (ctx_quote);\r
+> -}\r
+> -\r
+> -static void\r
+> -format_item_id_json (const void *ctx,\r
+> -                 unused (const char *item_type),\r
+> -                 const char *item_id)\r
+> -{\r
+> -    void *ctx_quote = talloc_new (ctx);\r
+> -\r
+> -    printf ("%s", json_quote_str (ctx_quote, item_id));\r
+> -\r
+> -    talloc_free (ctx_quote);\r
+> -    \r
+> -}\r
+> -\r
+> -static void\r
+> -format_thread_json (const void *ctx,\r
+> -                const char *thread_id,\r
+> -                const time_t date,\r
+> -                const int matched,\r
+> -                const int total,\r
+> -                const char *authors,\r
+> -                const char *subject)\r
+> -{\r
+> -    void *ctx_quote = talloc_new (ctx);\r
+> -\r
+> -    printf ("\"thread\": %s,\n"\r
+> -        "\"timestamp\": %ld,\n"\r
+> -        "\"date_relative\": \"%s\",\n"\r
+> -        "\"matched\": %d,\n"\r
+> -        "\"total\": %d,\n"\r
+> -        "\"authors\": %s,\n"\r
+> -        "\"subject\": %s,\n",\r
+> -        json_quote_str (ctx_quote, thread_id),\r
+> -        date,\r
+> -        notmuch_time_relative_date (ctx, date),\r
+> -        matched,\r
+> -        total,\r
+> -        json_quote_str (ctx_quote, authors),\r
+> -        json_quote_str (ctx_quote, subject));\r
+> -\r
+> -    talloc_free (ctx_quote);\r
+> -}\r
+> -\r
+>  static int\r
+> -do_search_threads (const search_format_t *format,\r
+> +do_search_threads (sprinter_t *format,\r
+>                 notmuch_query_t *query,\r
+>                 notmuch_sort_t sort,\r
+>                 output_t output,\r
+> @@ -207,7 +41,6 @@ do_search_threads (const search_format_t *format,\r
+>      notmuch_threads_t *threads;\r
+>      notmuch_tags_t *tags;\r
+>      time_t date;\r
+> -    int first_thread = 1;\r
+>      int i;\r
+>  \r
+>      if (offset < 0) {\r
+> @@ -220,14 +53,12 @@ do_search_threads (const search_format_t *format,\r
+>      if (threads == NULL)\r
+>      return 1;\r
+>  \r
+> -    fputs (format->results_start, stdout);\r
+> +    format->begin_list (format);\r
+>  \r
+>      for (i = 0;\r
+>       notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);\r
+>       notmuch_threads_move_to_next (threads), i++)\r
+>      {\r
+> -    int first_tag = 1;\r
+> -\r
+>      thread = notmuch_threads_get (threads);\r
+>  \r
+>      if (i < offset) {\r
+> @@ -235,60 +66,65 @@ do_search_threads (const search_format_t *format,\r
+>          continue;\r
+>      }\r
+>  \r
+> -    if (! first_thread)\r
+> -        fputs (format->item_sep, stdout);\r
+> -\r
+>      if (output == OUTPUT_THREADS) {\r
+> -        format->item_id (thread, "thread:",\r
+> -                         notmuch_thread_get_thread_id (thread));\r
+> +        format->set_prefix (format, "thread");\r
+> +        format->string (format,\r
+> +                        notmuch_thread_get_thread_id (thread));\r
+> +        format->separator (format);\r
+>      } else { /* output == OUTPUT_SUMMARY */\r
+> -        fputs (format->item_start, stdout);\r
+> +        void *ctx_quote = talloc_new (thread);\r
+> +\r
+> +        format->begin_map (format);\r
+>  \r
+>          if (sort == NOTMUCH_SORT_OLDEST_FIRST)\r
+>              date = notmuch_thread_get_oldest_date (thread);\r
+>          else\r
+>              date = notmuch_thread_get_newest_date (thread);\r
+>  \r
+> -        format->thread_summary (thread,\r
+> -                                notmuch_thread_get_thread_id (thread),\r
+> -                                date,\r
+> -                                notmuch_thread_get_matched_messages (thread),\r
+> -                                notmuch_thread_get_total_messages (thread),\r
+> -                                notmuch_thread_get_authors (thread),\r
+> -                                notmuch_thread_get_subject (thread));\r
+> -\r
+> -        fputs (format->tag_start, stdout);\r
+> +        format->map_key (format, "thread");\r
+> +        format->string (format, notmuch_thread_get_thread_id (thread));\r
+> +        format->map_key (format, "timestamp");\r
+> +        format->integer (format, date);\r
+> +        format->map_key (format, "date_relative");\r
+> +        format->string (format, notmuch_time_relative_date (ctx_quote, date));\r
+> +        format->map_key (format, "matched");\r
+> +        format->integer (format, notmuch_thread_get_matched_messages (thread));\r
+> +        format->map_key (format, "total");\r
+> +        format->integer (format, notmuch_thread_get_total_messages (thread));\r
+> +        format->map_key (format, "authors");\r
+> +        format->string (format, notmuch_thread_get_authors (thread));\r
+> +        format->map_key (format, "subject");\r
+> +        format->string (format, notmuch_thread_get_subject (thread));\r
+> +\r
+> +        talloc_free (ctx_quote);\r
+> +\r
+> +        format->map_key (format, "tags");\r
+> +        format->begin_list (format);\r
+>  \r
+>          for (tags = notmuch_thread_get_tags (thread);\r
+>               notmuch_tags_valid (tags);\r
+>               notmuch_tags_move_to_next (tags))\r
+>          {\r
+> -            if (! first_tag)\r
+> -                fputs (format->tag_sep, stdout);\r
+> -            printf (format->tag, notmuch_tags_get (tags));\r
+> -            first_tag = 0;\r
+> -        }\r
+> +            const char *tag = notmuch_tags_get (tags);\r
+>  \r
+> -        fputs (format->tag_end, stdout);\r
+> +            format->string (format, tag);\r
+> +        }\r
+>  \r
+> -        fputs (format->item_end, stdout);\r
+> +        format->end (format);\r
+> +        format->end (format);\r
+> +        format->separator (format);\r
+>      }\r
+>  \r
+> -    first_thread = 0;\r
+> -\r
+>      notmuch_thread_destroy (thread);\r
+>      }\r
+>  \r
+> -    if (first_thread)\r
+> -    fputs (format->results_null, stdout);\r
+> -    else\r
+> -    fputs (format->results_end, stdout);\r
+> +    format->end (format);\r
+>  \r
+>      return 0;\r
+>  }\r
+>  \r
+>  static int\r
+> -do_search_messages (const search_format_t *format,\r
+> +do_search_messages (sprinter_t *format,\r
+>                  notmuch_query_t *query,\r
+>                  output_t output,\r
+>                  int offset,\r
+> @@ -297,7 +133,6 @@ do_search_messages (const search_format_t *format,\r
+>      notmuch_message_t *message;\r
+>      notmuch_messages_t *messages;\r
+>      notmuch_filenames_t *filenames;\r
+> -    int first_message = 1;\r
+>      int i;\r
+>  \r
+>      if (offset < 0) {\r
+> @@ -310,7 +145,7 @@ do_search_messages (const search_format_t *format,\r
+>      if (messages == NULL)\r
+>      return 1;\r
+>  \r
+> -    fputs (format->results_start, stdout);\r
+> +    format->begin_list (format);\r
+>  \r
+>      for (i = 0;\r
+>       notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);\r
+> @@ -328,24 +163,17 @@ do_search_messages (const search_format_t *format,\r
+>               notmuch_filenames_valid (filenames);\r
+>               notmuch_filenames_move_to_next (filenames))\r
+>          {\r
+> -            if (! first_message)\r
+> -                fputs (format->item_sep, stdout);\r
+> -\r
+> -            format->item_id (message, "",\r
+> -                             notmuch_filenames_get (filenames));\r
+> -\r
+> -            first_message = 0;\r
+> +            format->string (format, notmuch_filenames_get (filenames));\r
+> +            format->separator (format);\r
+>          }\r
+> -        \r
+> +\r
+>          notmuch_filenames_destroy( filenames );\r
+>  \r
+>      } else { /* output == OUTPUT_MESSAGES */\r
+> -        if (! first_message)\r
+> -            fputs (format->item_sep, stdout);\r
+> -\r
+> -        format->item_id (message, "id:",\r
+> -                         notmuch_message_get_message_id (message));\r
+> -        first_message = 0;\r
+> +        format->set_prefix (format, "id");\r
+> +        format->string (format,\r
+> +                        notmuch_message_get_message_id (message));\r
+> +        format->separator (format);\r
+>      }\r
+>  \r
+>      notmuch_message_destroy (message);\r
+> @@ -353,23 +181,19 @@ do_search_messages (const search_format_t *format,\r
+>  \r
+>      notmuch_messages_destroy (messages);\r
+>  \r
+> -    if (first_message)\r
+> -    fputs (format->results_null, stdout);\r
+> -    else\r
+> -    fputs (format->results_end, stdout);\r
+> +    format->end (format);\r
+>  \r
+>      return 0;\r
+>  }\r
+>  \r
+>  static int\r
+>  do_search_tags (notmuch_database_t *notmuch,\r
+> -            const search_format_t *format,\r
+> +            sprinter_t *format,\r
+>              notmuch_query_t *query)\r
+>  {\r
+>      notmuch_messages_t *messages = NULL;\r
+>      notmuch_tags_t *tags;\r
+>      const char *tag;\r
+> -    int first_tag = 1;\r
+>  \r
+>      /* should the following only special case if no excluded terms\r
+>       * specified? */\r
+> @@ -387,7 +211,7 @@ do_search_tags (notmuch_database_t *notmuch,\r
+>      if (tags == NULL)\r
+>      return 1;\r
+>  \r
+> -    fputs (format->results_start, stdout);\r
+> +    format->begin_list (format);\r
+>  \r
+>      for (;\r
+>       notmuch_tags_valid (tags);\r
+> @@ -395,12 +219,9 @@ do_search_tags (notmuch_database_t *notmuch,\r
+>      {\r
+>      tag = notmuch_tags_get (tags);\r
+>  \r
+> -    if (! first_tag)\r
+> -        fputs (format->item_sep, stdout);\r
+> +    format->string (format, tag);\r
+> +    format->separator (format);\r
+>  \r
+> -    format->item_id (tags, "", tag);\r
+> -\r
+> -    first_tag = 0;\r
+>      }\r
+>  \r
+>      notmuch_tags_destroy (tags);\r
+> @@ -408,10 +229,7 @@ do_search_tags (notmuch_database_t *notmuch,\r
+>      if (messages)\r
+>      notmuch_messages_destroy (messages);\r
+>  \r
+> -    if (first_tag)\r
+> -    fputs (format->results_null, stdout);\r
+> -    else\r
+> -    fputs (format->results_end, stdout);\r
+> +    format->end (format);\r
+>  \r
+>      return 0;\r
+>  }\r
+> @@ -430,7 +248,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+>      notmuch_query_t *query;\r
+>      char *query_str;\r
+>      notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;\r
+> -    const search_format_t *format = &format_text;\r
+> +    sprinter_t *format = NULL;\r
+>      int opt_index, ret;\r
+>      output_t output = OUTPUT_SUMMARY;\r
+>      int offset = 0;\r
+> @@ -475,10 +293,10 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+>  \r
+>      switch (format_sel) {\r
+>      case NOTMUCH_FORMAT_TEXT:\r
+> -    format = &format_text;\r
+> +    format = sprinter_text_search_create (ctx, stdout);\r
+>      break;\r
+>      case NOTMUCH_FORMAT_JSON:\r
+> -    format = &format_json;\r
+> +    format = sprinter_json_create (ctx, stdout);\r
+>      break;\r
+>      }\r
+>  \r
+> @@ -546,5 +364,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+>      notmuch_query_destroy (query);\r
+>      notmuch_database_destroy (notmuch);\r
+>  \r
+> +    talloc_free (format);\r
+> +\r
+>      return ret;\r
+>  }\r
+> diff --git a/test/json b/test/json\r
+> index 6439788..f0ebf08 100755\r
+> --- a/test/json\r
+> +++ b/test/json\r
+> @@ -10,14 +10,7 @@ test_expect_equal "$output" "[[[{\"id\": \"${gen_msg_id}\", \"match\": true, \"e\r
+>  test_begin_subtest "Search message: json"\r
+>  add_message "[subject]=\"json-search-subject\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=\"json-search-message\""\r
+>  output=$(notmuch search --format=json "json-search-message" | notmuch_search_sanitize)\r
+\r
+If you pipe this through notmuch_json_show_sanitize, it'll make the\r
+test output much cleaner and closer to the original output (probably\r
+not exactly the same, unfortunately).\r
+\r
+> -test_expect_equal "$output" "[{\"thread\": \"XXX\",\r
+> -\"timestamp\": 946728000,\r
+> -\"date_relative\": \"2000-01-01\",\r
+> -\"matched\": 1,\r
+> -\"total\": 1,\r
+> -\"authors\": \"Notmuch Test Suite\",\r
+> -\"subject\": \"json-search-subject\",\r
+> -\"tags\": [\"inbox\", \"unread\"]}]"\r
+> +test_expect_equal "$output" "[{\"thread\": \"XXX\", \"timestamp\": 946728000, \"date_relative\": \"2000-01-01\", \"matched\": 1, \"total\": 1, \"authors\": \"Notmuch Test Suite\", \"subject\": \"json-search-subject\", \"tags\": [\"inbox\", \"unread\"]}]"\r
+>  \r
+>  test_begin_subtest "Show message: json, utf-8"\r
+>  add_message "[subject]=\"json-show-utf8-body-sübjéct\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=\"jsön-show-méssage\""\r
+> @@ -40,13 +33,6 @@ test_expect_equal "$output" "[[[{\"id\": \"$id\", \"match\": true, \"excluded\":\r
+>  test_begin_subtest "Search message: json, utf-8"\r
+>  add_message "[subject]=\"json-search-utf8-body-sübjéct\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=\"jsön-search-méssage\""\r
+>  output=$(notmuch search --format=json "jsön-search-méssage" | notmuch_search_sanitize)\r
+\r
+Same here.\r
+\r
+> -test_expect_equal "$output" "[{\"thread\": \"XXX\",\r
+> -\"timestamp\": 946728000,\r
+> -\"date_relative\": \"2000-01-01\",\r
+> -\"matched\": 1,\r
+> -\"total\": 1,\r
+> -\"authors\": \"Notmuch Test Suite\",\r
+> -\"subject\": \"json-search-utf8-body-sübjéct\",\r
+> -\"tags\": [\"inbox\", \"unread\"]}]"\r
+> +test_expect_equal "$output" "[{\"thread\": \"XXX\", \"timestamp\": 946728000, \"date_relative\": \"2000-01-01\", \"matched\": 1, \"total\": 1, \"authors\": \"Notmuch Test Suite\", \"subject\": \"json-search-utf8-body-sübjéct\", \"tags\": [\"inbox\", \"unread\"]}]"\r
+>  \r
+>  test_done\r