--- /dev/null
+Return-Path: <peter.feigl@gmx.at>\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 21294431FB6\r
+ for <notmuch@notmuchmail.org>; Fri, 13 Jul 2012 01:18:07 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.001\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.001 tagged_above=-999 required=5\r
+ tests=[FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001]\r
+ 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 Ssh-EgvKis61 for <notmuch@notmuchmail.org>;\r
+ Fri, 13 Jul 2012 01:18:06 -0700 (PDT)\r
+Received: from mailout-de.gmx.net (mailout-de.gmx.net [213.165.64.23])\r
+ by olra.theworths.org (Postfix) with SMTP id 8E5B3431FAE\r
+ for <notmuch@notmuchmail.org>; Fri, 13 Jul 2012 01:18:05 -0700 (PDT)\r
+Received: (qmail invoked by alias); 13 Jul 2012 08:17:59 -0000\r
+Received: from www.nexoid.at (EHLO mail.nexoid.at) [178.79.130.240]\r
+ by mail.gmx.net (mp033) with SMTP; 13 Jul 2012 10:17:59 +0200\r
+X-Authenticated: #4563876\r
+X-Provags-ID: V01U2FsdGVkX18mSfJsQnom6dBKHkvNoucS8v0zN2qRATa2X2Iehy\r
+ tsWGQNsjSenVzp\r
+Received: from nexoid (localhost [127.0.0.1])\r
+ by mail.nexoid.at (Postfix) with ESMTP id 42168E00C\r
+ for <notmuch@notmuchmail.org>; Fri, 13 Jul 2012 10:17:56 +0200 (CEST)\r
+From: <craven@gmx.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: Re: Proof of concept: S-Expression format\r
+In-Reply-To: <1342167097-25012-1-git-send-email-craven@gmx.net>\r
+References: <20120710191331.GE7332@mit.edu>\r
+ <1342167097-25012-1-git-send-email-craven@gmx.net>\r
+User-Agent: Notmuch/0.11+77~gad6d0d5 (http://notmuchmail.org) Emacs/24.1.50.2\r
+ (i686-pc-linux-gnu)\r
+Date: Fri, 13 Jul 2012 10:17:56 +0200\r
+Message-ID: <877gu8t6q3.fsf@nexoid.at>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain\r
+X-Y-GMX-Trusted: 0\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: Fri, 13 Jul 2012 08:18:07 -0000\r
+\r
+This patch shows how to add a new output format to notmuch-search.c.\r
+\r
+As an example, it adds S-Expressions. The concrete formatting can\r
+easily be changed, this is meant as a proof of concept that the\r
+changes to core notmuch code are very few and all formatting state is\r
+kept inside sprinter-sexp.c.\r
+---\r
+ Makefile.local | 1 +\r
+ notmuch-search.c | 6 +-\r
+ sprinter-sexp.c | 185 +++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ sprinter.h | 4 ++\r
+ 4 files changed, 195 insertions(+), 1 deletion(-)\r
+ create mode 100644 sprinter-sexp.c\r
+\r
+diff --git a/Makefile.local b/Makefile.local\r
+index b6c7e0c..cc1d58a 100644\r
+--- a/Makefile.local\r
++++ b/Makefile.local\r
+@@ -292,6 +292,7 @@ notmuch_client_srcs = \\r
+ notmuch-time.c \\r
+ sprinter-json.c \\r
+ sprinter-text-search.c \\r
++ sprinter-sexp.c \\r
+ query-string.c \\r
+ mime-node.c \\r
+ crypto.c \\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 99fddac..2db58a5 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -256,7 +256,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+ int exclude = EXCLUDE_TRUE;\r
+ unsigned int i;\r
+ \r
+- enum { NOTMUCH_FORMAT_JSON, NOTMUCH_FORMAT_TEXT }\r
++ enum { NOTMUCH_FORMAT_JSON, NOTMUCH_FORMAT_TEXT, NOTMUCH_FORMAT_SEXP }\r
+ format_sel = NOTMUCH_FORMAT_TEXT;\r
+ \r
+ notmuch_opt_desc_t options[] = {\r
+@@ -267,6 +267,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+ { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',\r
+ (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },\r
+ { "text", NOTMUCH_FORMAT_TEXT },\r
++ { "sexp", NOTMUCH_FORMAT_SEXP },\r
+ { 0, 0 } } },\r
+ { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',\r
+ (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },\r
+@@ -298,6 +299,9 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+ case NOTMUCH_FORMAT_JSON:\r
+ format = sprinter_json_create (ctx, stdout);\r
+ break;\r
++ case NOTMUCH_FORMAT_SEXP:\r
++ format = sprinter_sexp_create (ctx, stdout);\r
++ break;\r
+ }\r
+ \r
+ config = notmuch_config_open (ctx, NULL, NULL);\r
+diff --git a/sprinter-sexp.c b/sprinter-sexp.c\r
+new file mode 100644\r
+index 0000000..68a5db5\r
+--- /dev/null\r
++++ b/sprinter-sexp.c\r
+@@ -0,0 +1,185 @@\r
++#include <stdbool.h>\r
++#include <stdio.h>\r
++#include <talloc.h>\r
++#include "sprinter.h"\r
++\r
++typedef enum { MAP, LIST } aggregate_t;\r
++\r
++struct sprinter_sexp {\r
++ struct sprinter vtable;\r
++ FILE *stream;\r
++ /* Top of the state stack, or NULL if the printer is not currently\r
++ * inside any aggregate types. */\r
++ struct sexp_state *state;\r
++};\r
++\r
++struct sexp_state {\r
++ struct sexp_state *parent;\r
++ /* True if nothing has been printed in this aggregate yet.\r
++ * Suppresses the comma before a value. */\r
++ notmuch_bool_t first;\r
++ /* The character that closes the current aggregate. */\r
++ aggregate_t type;\r
++};\r
++\r
++static struct sprinter_sexp *\r
++sexp_begin_value (struct sprinter *sp)\r
++{\r
++ struct sprinter_sexp *spsx = (struct sprinter_sexp *) sp;\r
++\r
++ if (spsx->state) {\r
++ if (! spsx->state->first)\r
++ fputc (' ', spsx->stream);\r
++ else\r
++ spsx->state->first = FALSE;\r
++ }\r
++ return spsx;\r
++}\r
++\r
++static void\r
++sexp_begin_aggregate (struct sprinter *sp, aggregate_t type)\r
++{\r
++ struct sprinter_sexp *spsx = (struct sprinter_sexp *) sp;\r
++ struct sexp_state *state = talloc (spsx, struct sexp_state);\r
++\r
++ fputc ('(', spsx->stream);\r
++ state->parent = spsx->state;\r
++ state->first = TRUE;\r
++ state->type = type;\r
++\r
++ spsx->state = state;\r
++}\r
++\r
++static void\r
++sexp_begin_map (struct sprinter *sp)\r
++{\r
++ sexp_begin_aggregate (sp, MAP);\r
++}\r
++\r
++static void\r
++sexp_begin_list (struct sprinter *sp)\r
++{\r
++ sexp_begin_aggregate (sp, LIST);\r
++}\r
++\r
++static void\r
++sexp_end (struct sprinter *sp)\r
++{\r
++ struct sprinter_sexp *spsx = (struct sprinter_sexp *) sp;\r
++ struct sexp_state *state = spsx->state;\r
++\r
++ fputc (')', spsx->stream);\r
++ spsx->state = state->parent;\r
++ talloc_free (state);\r
++ if (spsx->state == NULL)\r
++ fputc ('\n', spsx->stream);\r
++ else\r
++ if (spsx->state->type == MAP)\r
++ fputc (')', spsx->stream);\r
++}\r
++\r
++static void\r
++sexp_string (struct sprinter *sp, const char *val)\r
++{\r
++ static const char *const escapes[] = {\r
++ ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",\r
++ ['\f'] = "\\f", ['\n'] = "\\n", ['\t'] = "\\t"\r
++ };\r
++ struct sprinter_sexp *spsx = sexp_begin_value (sp);\r
++\r
++ fputc ('"', spsx->stream);\r
++ for (; *val; ++val) {\r
++ unsigned char ch = *val;\r
++ if (ch < ARRAY_SIZE (escapes) && escapes[ch])\r
++ fputs (escapes[ch], spsx->stream);\r
++ else if (ch >= 32)\r
++ fputc (ch, spsx->stream);\r
++ else\r
++ fprintf (spsx->stream, "\\u%04x", ch);\r
++ }\r
++ fputc ('"', spsx->stream);\r
++ if (spsx->state != NULL && spsx->state->type == MAP)\r
++ fputc (')', spsx->stream);\r
++ spsx->state->first = FALSE;\r
++}\r
++\r
++static void\r
++sexp_integer (struct sprinter *sp, int val)\r
++{\r
++ struct sprinter_sexp *spsx = sexp_begin_value (sp);\r
++\r
++ fprintf (spsx->stream, "%d", val);\r
++ if (spsx->state != NULL && spsx->state->type == MAP)\r
++ fputc (')', spsx->stream);\r
++}\r
++\r
++static void\r
++sexp_boolean (struct sprinter *sp, notmuch_bool_t val)\r
++{\r
++ struct sprinter_sexp *spsx = sexp_begin_value (sp);\r
++\r
++ fputs (val ? "#t" : "#f", spsx->stream);\r
++ if (spsx->state != NULL && spsx->state->type == MAP)\r
++ fputc (')', spsx->stream);\r
++}\r
++\r
++static void\r
++sexp_null (struct sprinter *sp)\r
++{\r
++ struct sprinter_sexp *spsx = sexp_begin_value (sp);\r
++\r
++ fputs ("'()", spsx->stream);\r
++ spsx->state->first = FALSE;\r
++}\r
++\r
++static void\r
++sexp_map_key (struct sprinter *sp, const char *key)\r
++{\r
++ struct sprinter_sexp *spsx = sexp_begin_value (sp);\r
++\r
++ fputc ('(', spsx->stream);\r
++ fputs (key, spsx->stream);\r
++ fputs (" . ", spsx->stream);\r
++ spsx->state->first = TRUE;\r
++}\r
++\r
++static void\r
++sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))\r
++{\r
++}\r
++\r
++static void\r
++sexp_separator (struct sprinter *sp)\r
++{\r
++ struct sprinter_sexp *spsx = (struct sprinter_sexp *) sp;\r
++\r
++ fputc ('\n', spsx->stream);\r
++}\r
++\r
++struct sprinter *\r
++sprinter_sexp_create (const void *ctx, FILE *stream)\r
++{\r
++ static const struct sprinter_sexp template = {\r
++ .vtable = {\r
++ .begin_map = sexp_begin_map,\r
++ .begin_list = sexp_begin_list,\r
++ .end = sexp_end,\r
++ .string = sexp_string,\r
++ .integer = sexp_integer,\r
++ .boolean = sexp_boolean,\r
++ .null = sexp_null,\r
++ .map_key = sexp_map_key,\r
++ .separator = sexp_separator,\r
++ .set_prefix = sexp_set_prefix,\r
++ }\r
++ };\r
++ struct sprinter_sexp *res;\r
++\r
++ res = talloc (ctx, struct sprinter_sexp);\r
++ if (! res)\r
++ return NULL;\r
++\r
++ *res = template;\r
++ res->stream = stream;\r
++ return &res->vtable;\r
++}\r
+diff --git a/sprinter.h b/sprinter.h\r
+index 4241d65..c0146f6 100644\r
+--- a/sprinter.h\r
++++ b/sprinter.h\r
+@@ -55,4 +55,8 @@ sprinter_text_search_create (const void *ctx, FILE *stream);\r
+ struct sprinter *\r
+ sprinter_json_create (const void *ctx, FILE *stream);\r
+ \r
++/* Create a new structure printer that emits S-Expressions. */\r
++struct sprinter *\r
++sprinter_sexp_create (const void *ctx, FILE *stream);\r
++\r
+ #endif // NOTMUCH_SPRINTER_H\r
+-- \r
+1.7.11.1\r
+\r