Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / a2 / 66dc8ef08a60e66fec8ff6709c3fd4b2040380
1 Return-Path: <nex@nexoid.at>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 17E8F431FC0\r
6         for <notmuch@notmuchmail.org>; Thu,  6 Dec 2012 13:12:36 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0.001\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0.001 tagged_above=-999 required=5\r
12         tests=[FREEMAIL_FROM=0.001] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id w17tk12DHJ6n for <notmuch@notmuchmail.org>;\r
16         Thu,  6 Dec 2012 13:12:33 -0800 (PST)\r
17 Received: from mail.nexoid.at (www.nexoid.at [178.79.130.240])\r
18         (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 75589431FB6\r
21         for <notmuch@notmuchmail.org>; Thu,  6 Dec 2012 13:12:29 -0800 (PST)\r
22 Received: by mail.nexoid.at (Postfix, from userid 1000)\r
23         id 44A4E11C110; Thu,  6 Dec 2012 22:12:23 +0100 (CET)\r
24 From: Peter Feigl <craven@gmx.net>\r
25 To: notmuch@notmuchmail.org\r
26 Subject: [PATCH v5 1/5] Adding an S-expression structured output printer.\r
27 Date: Thu,  6 Dec 2012 22:12:11 +0100\r
28 Message-Id: <1354828335-18151-2-git-send-email-craven@gmx.net>\r
29 X-Mailer: git-send-email 1.8.0\r
30 In-Reply-To: <1354828335-18151-1-git-send-email-craven@gmx.net>\r
31 References: <1354828335-18151-1-git-send-email-craven@gmx.net>\r
32 MIME-Version: 1.0\r
33 Content-Type: text/plain; charset=UTF-8\r
34 Content-Transfer-Encoding: 8bit\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.13\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39         <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Thu, 06 Dec 2012 21:12:36 -0000\r
48 \r
49 This commit adds a structured output printer for Lisp\r
50 S-Expressions. Later commits will use this printer in notmuch search,\r
51 show and reply.\r
52 \r
53 The structure is the same as json, but:\r
54 - arrays are written as lists: ("foo" "bar" "baaz" 1 2 3)\r
55 - maps are written as p-lists: (:key "value" :other-key "other-value")\r
56 - true is written as t\r
57 - false is written as nil\r
58 - null is written as nil\r
59 ---\r
60  Makefile.local  |   1 +\r
61  sprinter-sexp.c | 235 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
62  sprinter.h      |   4 +\r
63  3 files changed, 240 insertions(+)\r
64  create mode 100644 sprinter-sexp.c\r
65 \r
66 diff --git a/Makefile.local b/Makefile.local\r
67 index 2b91946..0db1713 100644\r
68 --- a/Makefile.local\r
69 +++ b/Makefile.local\r
70 @@ -270,6 +270,7 @@ notmuch_client_srcs =               \\r
71         notmuch-tag.c           \\r
72         notmuch-time.c          \\r
73         sprinter-json.c         \\r
74 +       sprinter-sexp.c         \\r
75         sprinter-text.c         \\r
76         query-string.c          \\r
77         mime-node.c             \\r
78 diff --git a/sprinter-sexp.c b/sprinter-sexp.c\r
79 new file mode 100644\r
80 index 0000000..06856f4\r
81 --- /dev/null\r
82 +++ b/sprinter-sexp.c\r
83 @@ -0,0 +1,235 @@\r
84 +/* notmuch - Not much of an email program, (just index and search)\r
85 + *\r
86 + * Copyright © 2012 Peter Feigl\r
87 + *\r
88 + * This program is free software: you can redistribute it and/or modify\r
89 + * it under the terms of the GNU General Public License as published by\r
90 + * the Free Software Foundation, either version 3 of the License, or\r
91 + * (at your option) any later version.\r
92 + *\r
93 + * This program is distributed in the hope that it will be useful,\r
94 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
95 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
96 + * GNU General Public License for more details.\r
97 + *\r
98 + * You should have received a copy of the GNU General Public License\r
99 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
100 + *\r
101 + * Author: Peter Feigl <peter.feigl@gmx.at>\r
102 + */\r
103 +\r
104 +#include <stdbool.h>\r
105 +#include <stdio.h>\r
106 +#include <talloc.h>\r
107 +#include "sprinter.h"\r
108 +#include <ctype.h>\r
109 +\r
110 +struct sprinter_sexp {\r
111 +    struct sprinter vtable;\r
112 +    FILE *stream;\r
113 +    /* Top of the state stack, or NULL if the printer is not currently\r
114 +     * inside any aggregate types. */\r
115 +    struct sexp_state *state;\r
116 +\r
117 +    /* A flag to signify that a separator should be inserted in the\r
118 +     * output as soon as possible. */\r
119 +    notmuch_bool_t insert_separator;\r
120 +};\r
121 +\r
122 +struct sexp_state {\r
123 +    struct sexp_state *parent;\r
124 +\r
125 +    /* True if nothing has been printed in this aggregate yet.\r
126 +     * Suppresses the space before a value. */\r
127 +    notmuch_bool_t first;\r
128 +};\r
129 +\r
130 +/* Helper function to set up the stream to print a value.  If this\r
131 + * value follows another value, prints a space. */\r
132 +static struct sprinter_sexp *\r
133 +sexp_begin_value (struct sprinter *sp)\r
134 +{\r
135 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
136 +\r
137 +    if (sps->state) {\r
138 +       if (! sps->state->first) {\r
139 +           if (sps->insert_separator) {\r
140 +               fputc ('\n', sps->stream);\r
141 +               sps->insert_separator = FALSE;\r
142 +           } else {\r
143 +               fputc (' ', sps->stream);\r
144 +           }\r
145 +       } else {\r
146 +           sps->state->first = FALSE;\r
147 +       }\r
148 +    }\r
149 +    return sps;\r
150 +}\r
151 +\r
152 +/* Helper function to begin an aggregate type.  Prints the open\r
153 + * character and pushes a new state frame. */\r
154 +static void\r
155 +sexp_begin_aggregate (struct sprinter *sp)\r
156 +{\r
157 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
158 +    struct sexp_state *state = talloc (sps, struct sexp_state);\r
159 +    fputc ('(', sps->stream);\r
160 +    state->parent = sps->state;\r
161 +    state->first = TRUE;\r
162 +    sps->state = state;\r
163 +}\r
164 +\r
165 +static void\r
166 +sexp_begin_map (struct sprinter *sp)\r
167 +{\r
168 +    sexp_begin_aggregate (sp);\r
169 +}\r
170 +\r
171 +static void\r
172 +sexp_begin_list (struct sprinter *sp)\r
173 +{\r
174 +    sexp_begin_aggregate (sp);\r
175 +}\r
176 +\r
177 +static void\r
178 +sexp_end (struct sprinter *sp)\r
179 +{\r
180 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
181 +    struct sexp_state *state = sps->state;\r
182 +\r
183 +    fputc (')', sps->stream);\r
184 +    sps->state = state->parent;\r
185 +    talloc_free (state);\r
186 +    if (sps->state == NULL)\r
187 +       fputc ('\n', sps->stream);\r
188 +}\r
189 +\r
190 +static void\r
191 +sexp_string_len (struct sprinter *sp, const char *val, size_t len)\r
192 +{\r
193 +    /* Some characters need escaping. " and \ work fine in all Lisps,\r
194 +     * \n is not supported in CL, but all others work fine.\r
195 +     * Characters below 32 are printed as \123o (three-digit\r
196 +     * octals), which work fine in most Schemes and Emacs. */\r
197 +    static const char *const escapes[] = {\r
198 +       ['\"'] = "\\\"", ['\\'] = "\\\\",  ['\n'] = "\\n"\r
199 +    };\r
200 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
201 +\r
202 +    fputc ('"', sps->stream);\r
203 +    for (; len; ++val, --len) {\r
204 +       unsigned char ch = *val;\r
205 +       if (ch < ARRAY_SIZE (escapes) && escapes[ch])\r
206 +           fputs (escapes[ch], sps->stream);\r
207 +       else if (ch >= 32)\r
208 +           fputc (ch, sps->stream);\r
209 +       else\r
210 +           fprintf (sps->stream, "\\%03o", ch);\r
211 +    }\r
212 +    fputc ('"', sps->stream);\r
213 +}\r
214 +\r
215 +static void\r
216 +sexp_string (struct sprinter *sp, const char *val)\r
217 +{\r
218 +    if (val == NULL)\r
219 +       val = "";\r
220 +    sexp_string_len (sp, val, strlen (val));\r
221 +}\r
222 +\r
223 +/* Prints a symbol, i.e. the name preceded by a colon. This should work\r
224 + * in all Lisps, at least as a symbol, if not as a proper keyword */\r
225 +static void\r
226 +sexp_keyword (struct sprinter *sp, const char *val)\r
227 +{\r
228 +    unsigned int i = 0;\r
229 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
230 +    char ch;\r
231 +\r
232 +    if (val == NULL)\r
233 +       INTERNAL_ERROR ("illegal symbol NULL");\r
234 +\r
235 +    for (i = 0; i < strlen (val); i++) {\r
236 +       ch = val[i];\r
237 +       if (! (isalnum(ch) || (ch == '-') || (ch == '_'))) {\r
238 +          INTERNAL_ERROR ("illegal character in symbol %s: %c", val, ch);\r
239 +       }\r
240 +    }\r
241 +    fputc (':', sps->stream);\r
242 +    fputs (val, sps->stream);\r
243 +}\r
244 +\r
245 +static void\r
246 +sexp_integer (struct sprinter *sp, int val)\r
247 +{\r
248 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
249 +\r
250 +    fprintf (sps->stream, "%d", val);\r
251 +}\r
252 +\r
253 +static void\r
254 +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)\r
255 +{\r
256 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
257 +\r
258 +    fputs (val ? "t" : "nil", sps->stream);\r
259 +}\r
260 +\r
261 +static void\r
262 +sexp_null (struct sprinter *sp)\r
263 +{\r
264 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
265 +\r
266 +    fputs ("nil", sps->stream);\r
267 +}\r
268 +\r
269 +static void\r
270 +sexp_map_key (struct sprinter *sp, const char *key)\r
271 +{\r
272 +    sexp_begin_value (sp);\r
273 +    \r
274 +    sexp_keyword (sp, key);\r
275 +}\r
276 +\r
277 +static void\r
278 +sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))\r
279 +{\r
280 +}\r
281 +\r
282 +static void\r
283 +sexp_separator (struct sprinter *sp)\r
284 +{\r
285 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
286 +\r
287 +    sps->insert_separator = TRUE;\r
288 +}\r
289 +\r
290 +struct sprinter *\r
291 +sprinter_sexp_create (const void *ctx, FILE *stream)\r
292 +{\r
293 +    static const struct sprinter_sexp template = {\r
294 +       .vtable = {\r
295 +           .begin_map = sexp_begin_map,\r
296 +           .begin_list = sexp_begin_list,\r
297 +           .end = sexp_end,\r
298 +           .string = sexp_string,\r
299 +           .string_len = sexp_string_len,\r
300 +           .integer = sexp_integer,\r
301 +           .boolean = sexp_boolean,\r
302 +           .null = sexp_null,\r
303 +           .map_key = sexp_map_key,\r
304 +           .separator = sexp_separator,\r
305 +           .set_prefix = sexp_set_prefix,\r
306 +           .is_text_printer = FALSE,\r
307 +       }\r
308 +    };\r
309 +    struct sprinter_sexp *res;\r
310 +\r
311 +    res = talloc (ctx, struct sprinter_sexp);\r
312 +    if (! res)\r
313 +       return NULL;\r
314 +\r
315 +    *res = template;\r
316 +    res->stream = stream;\r
317 +    return &res->vtable;\r
318 +}\r
319 diff --git a/sprinter.h b/sprinter.h\r
320 index 912a526..59776a9 100644\r
321 --- a/sprinter.h\r
322 +++ b/sprinter.h\r
323 @@ -70,4 +70,8 @@ sprinter_text_create (const void *ctx, FILE *stream);\r
324  struct sprinter *\r
325  sprinter_json_create (const void *ctx, FILE *stream);\r
326  \r
327 +/* Create a new structure printer that emits S-Expressions. */\r
328 +struct sprinter *\r
329 +sprinter_sexp_create (const void *ctx, FILE *stream);\r
330 +\r
331  #endif // NOTMUCH_SPRINTER_H\r
332 -- \r
333 1.8.0\r
334 \r