Re: [PATCH] rewriting notmuch-search for structured output to make other output forma...
authorAustin Clements <amdragon@MIT.EDU>
Sat, 21 Jan 2012 22:04:07 +0000 (17:04 +1900)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:42:53 +0000 (09:42 -0800)
e0/6741afe31804f8fb0a98dd08ee61c3f0cd5644 [new file with mode: 0644]

diff --git a/e0/6741afe31804f8fb0a98dd08ee61c3f0cd5644 b/e0/6741afe31804f8fb0a98dd08ee61c3f0cd5644
new file mode 100644 (file)
index 0000000..9015fc2
--- /dev/null
@@ -0,0 +1,143 @@
+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 E8234431FAF\r
+       for <notmuch@notmuchmail.org>; Sat, 21 Jan 2012 14:04:34 -0800 (PST)\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 tRg74xEG--gV for <notmuch@notmuchmail.org>;\r
+       Sat, 21 Jan 2012 14:04:34 -0800 (PST)\r
+Received: from dmz-mailsec-scanner-5.mit.edu (DMZ-MAILSEC-SCANNER-5.MIT.EDU\r
+       [18.7.68.34])\r
+       by olra.theworths.org (Postfix) with ESMTP id 3C86A431FAE\r
+       for <notmuch@notmuchmail.org>; Sat, 21 Jan 2012 14:04:34 -0800 (PST)\r
+X-AuditID: 12074422-b7fd66d0000008f9-88-4f1b36712b86\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+       by dmz-mailsec-scanner-5.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id D9.F9.02297.1763B1F4; Sat, 21 Jan 2012 17:04:33 -0500 (EST)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id q0LM4WG0005438; \r
+       Sat, 21 Jan 2012 17:04:33 -0500\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 q0LM4Up6017078\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Sat, 21 Jan 2012 17:04:31 -0500 (EST)\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1Roj2d-0008Sa-4N; Sat, 21 Jan 2012 17:04:07 -0500\r
+Date: Sat, 21 Jan 2012 17:04:07 -0500\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: Peter Feigl <craven@gmx.net>\r
+Subject: Re: [PATCH] rewriting notmuch-search for structured output to make\r
+       other output formats easier\r
+Message-ID: <20120121220407.GK16740@mit.edu>\r
+References: <1327180568-30385-1-git-send-email-craven@gmx.net>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+In-Reply-To: <1327180568-30385-1-git-send-email-craven@gmx.net>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFuphleLIzCtJLcpLzFFi42IRYrdT1y00k/Y3eHRf3mJvQzujxfWbM5kd\r
+       mDwWb9rP5vFs1S3mAKYoLpuU1JzMstQifbsErozuiz9ZCs6KVdx5MJexgXG9UBcjJ4eEgInE\r
+       p9vTWCBsMYkL99azgdhCAvsYJZ4ui+9i5AKyNzBKrNq1kh3COckk8WPSXTYIZwmjREfTftYu\r
+       Rg4OFgFVie5/wiDdbAIaEtv2L2cEsUUEFCSerWsCs5kFpCW+/W5mAikXFsiUuNgeBGLyCuhI\r
+       LFtqDbHXTmLf3L3MIDavgKDEyZlPWCA6tSRu/HsJ1gkyZfk/DpAwp4C9xPovu9lBbFEBFYkp\r
+       J7exTWAUmoWkexaS7lkI3QsYmVcxyqbkVunmJmbmFKcm6xYnJ+blpRbpmurlZpbopaaUbmIE\r
+       B7SL0g7GnweVDjEKcDAq8fAm7JP0F2JNLCuuzD3EKMnBpCTKm2Io7S/El5SfUpmRWJwRX1Sa\r
+       k1p8iFGCg1lJhLesC6icNyWxsiq1KB8mJc3BoiTOq671zk9IID2xJDU7NbUgtQgmK8PBoSTB\r
+       +9cUaKhgUWp6akVaZk4JQpqJgxNkOA/Q8HsgNbzFBYm5xZnpEPlTjLocX363nWcUYsnLz0uV\r
+       EufdA1IkAFKUUZoHNweWiF4xigO9Jcz7FaSKB5jE4Ca9AlrCBLSEI08KZElJIkJKqoHRJ/zc\r
+       9zy7d8XCkVOuuEYxV77+8eroU6kdzx8J5hg82cX267k1/wLLv0IBIZc/XhQV/3bN2ITv+9Un\r
+       huc5BWc/5n1fvMu24dn8D9c7a8Tz1yd86V/OXbX8HsuSw5fn9P41Uz71WNEy7xnPOtVtWgWO\r
+       sldufRZq/9UYJvHdR2LtX2E3jQf+/RqzlFiKMxINtZiLihMBg0hRsx8DAAA=\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, 21 Jan 2012 22:04:35 -0000\r
+\r
+Quoth Peter Feigl on Jan 21 at 10:16 pm:\r
+> The output routines have been rewritten so that logical structure\r
+> (objects with key/value pairs, arrays, strings and numbers) are\r
+> written instead of ad-hoc printfs. This allows for easier adaptation\r
+> of other output formats, as only the routines that start/end an object\r
+> etc. have to be rewritten. The logic is the same for all formats.\r
+> The default text output is handled differently, special cases are\r
+> inserted at the proper places, as it differs too much from the\r
+> structured output.\r
+\r
+I think this is a great idea and I'm a fan of having an S-expression\r
+format, but I also think there's a much easier and more general way to\r
+structure this.\r
+\r
+In particular, I don't think you should hijack search_format, since\r
+you'll wind up repeating most of this work for anything else that\r
+needs to output structured data (notmuch show, at least).  Rather, I'd\r
+suggest creating a general "structure printer" struct that isn't tied\r
+to search.  You've essentially already done this, you just called it\r
+search_format.  Then, leave the existing format callbacks in place,\r
+just use this new API instead of lots of printf calls.\r
+\r
+What about all of those annoying {tag,item}_{start,sep,end} fields?  I\r
+think you can simultaneously get rid of those *and* simplify the\r
+structure printer API.  If the structure printer is allowed to keep a\r
+little state, it can automatically insert separators.  With a little\r
+nesting state, it could even insert terminators by just saying "pop me\r
+to this level".  This could probably be better, but I'm imagining\r
+something like\r
+\r
+struct sprinter *\r
+new_json_sprinter (const void *ctx, FILE *stream);\r
+struct sprinter *\r
+new_sexp_sprinter (const void *ctx, FILE *stream);\r
+\r
+/* Start a map (a JSON object or a S-expression alist/plist/whatever)\r
+ * and return the nesting level of the map. */\r
+int\r
+sprinter_map (struct sprinter *sp);\r
+/* Start a list (aka array) and return the nesting level of the list. */\r
+int\r
+sprinter_list (struct sprinter *sp);\r
+\r
+/* Close maps and lists until reaching level. */\r
+void\r
+sprinter_pop (struct sprinter *sp, int level);\r
+\r
+void\r
+sprinter_map_key (struct sprinter *sp, const char *key);\r
+void\r
+sprinter_number (struct sprinter *sp, int val);\r
+void\r
+sprinter_string (struct sprinter *sp, const char *val);\r
+void\r
+sprinter_bool (struct sprinter *sp, notmuch_bool_t val);\r
+\r
+and that's it.  This would also subsume your format_attribute_*\r
+helpers.\r
+\r
+Unfortunately, it's a pain to pass things like a structure printer\r
+object around formatters (too bad notmuch isn't written in C++, eh?).\r
+I think it's better to address this than to structure around it.\r
+Probably the simplest thing to do is to make a struct for formatter\r
+state and pass that in to the callbacks.  You could also more\r
+completely emulate classes, but that would probably be overkill for\r
+this.\r