Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / c5 / dfb0dfce93b93d1392b11c058df1157c7b0094
1 Return-Path: <m.walters@qmul.ac.uk>\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 0A6AA431FBC\r
6         for <notmuch@notmuchmail.org>; Sat,  1 Dec 2012 08:14:54 -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: -1.098\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5\r
12         tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
13         NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id r9MEK1+-76UV for <notmuch@notmuchmail.org>;\r
17         Sat,  1 Dec 2012 08:14:50 -0800 (PST)\r
18 Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
19         (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id C13BA431FAF\r
22         for <notmuch@notmuchmail.org>; Sat,  1 Dec 2012 08:14:49 -0800 (PST)\r
23 Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
24         by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
25         (envelope-from <m.walters@qmul.ac.uk>)\r
26         id 1TepiI-0003yf-19; Sat, 01 Dec 2012 16:14:48 +0000\r
27 Received: from 93-97-24-31.zone5.bethere.co.uk ([93.97.24.31] helo=localhost)\r
28         by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69)\r
29         (envelope-from <m.walters@qmul.ac.uk>)\r
30         id 1TepiH-00049c-EH; Sat, 01 Dec 2012 16:14:45 +0000\r
31 From: Mark Walters <markwalters1009@gmail.com>\r
32 To: Peter Feigl <craven@gmx.net>, notmuch@notmuchmail.org\r
33 Subject: Re: [PATCH 1/3] Adding an S-expression structured output printer.\r
34 In-Reply-To: <1354264143-30173-1-git-send-email-craven@gmx.net>\r
35 References: <1354264143-30173-1-git-send-email-craven@gmx.net>\r
36 User-Agent: Notmuch/0.14+81~g9730584 (http://notmuchmail.org) Emacs/23.4.1\r
37         (x86_64-pc-linux-gnu)\r
38 Date: Sat, 01 Dec 2012 16:14:44 +0000\r
39 Message-ID: <87sj7p3g23.fsf@qmul.ac.uk>\r
40 MIME-Version: 1.0\r
41 Content-Type: text/plain; charset=us-ascii\r
42 X-Sender-Host-Address: 93.97.24.31\r
43 X-QM-SPAM-Info: Sender has good ham record.  :)\r
44 X-QM-Body-MD5: 4787153bdec301a1bb606d636b5488a5 (of first 20000 bytes)\r
45 X-SpamAssassin-Score: -1.7\r
46 X-SpamAssassin-SpamBar: -\r
47 X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
48         determine if it is\r
49         spam. We require at least 5.0 points to mark a message as spam.\r
50         This message scored -1.7 points.\r
51         Summary of the scoring: \r
52         * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/,\r
53         *      medium trust\r
54         *      [138.37.6.40 listed in list.dnswl.org]\r
55         * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
56         provider *      (markwalters1009[at]gmail.com)\r
57         *  0.6 AWL AWL: From: address is in the auto white-list\r
58 X-QM-Scan-Virus: ClamAV says the message is clean\r
59 X-BeenThere: notmuch@notmuchmail.org\r
60 X-Mailman-Version: 2.1.13\r
61 Precedence: list\r
62 List-Id: "Use and development of the notmuch mail system."\r
63         <notmuch.notmuchmail.org>\r
64 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
65         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
66 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
67 List-Post: <mailto:notmuch@notmuchmail.org>\r
68 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
69 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
70         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
71 X-List-Received-Date: Sat, 01 Dec 2012 16:14:54 -0000\r
72 \r
73 \r
74 One small niggle with this patch: it seems to have lots of spaces rather\r
75 than tabs (unless I am misreading it)\r
76 \r
77 Best wishes\r
78 \r
79 Mark\r
80 \r
81 On Fri, 30 Nov 2012, Peter Feigl <craven@gmx.net> wrote:\r
82 > This commit adds an sprinter for Lisp S-Expressions. Later commits will\r
83 > use this printer.\r
84 >\r
85 > The structure is the same as json, but:\r
86 > - arrays are written as lists: ("foo" "bar" "baaz" 1 2 3)\r
87 > - maps are written as a-lists: ((key "value") (other-key "other-value"))\r
88 > - true is written as t\r
89 > - false is written as nil\r
90 > - null is written as nil\r
91 > ---\r
92 >  Makefile.local  |   1 +\r
93 >  sprinter-sexp.c | 235 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
94 >  2 files changed, 236 insertions(+)\r
95 >  create mode 100644 sprinter-sexp.c\r
96 >\r
97 > diff --git a/Makefile.local b/Makefile.local\r
98 > index 2b91946..0db1713 100644\r
99 > --- a/Makefile.local\r
100 > +++ b/Makefile.local\r
101 > @@ -270,6 +270,7 @@ notmuch_client_srcs =             \\r
102 >       notmuch-tag.c           \\r
103 >       notmuch-time.c          \\r
104 >       sprinter-json.c         \\r
105 > +     sprinter-sexp.c         \\r
106 >       sprinter-text.c         \\r
107 >       query-string.c          \\r
108 >       mime-node.c             \\r
109 > diff --git a/sprinter-sexp.c b/sprinter-sexp.c\r
110 > new file mode 100644\r
111 > index 0000000..8401c52\r
112 > --- /dev/null\r
113 > +++ b/sprinter-sexp.c\r
114 > @@ -0,0 +1,235 @@\r
115 > +#include <stdbool.h>\r
116 > +#include <stdio.h>\r
117 > +#include <talloc.h>\r
118 > +#include "sprinter.h"\r
119 > +\r
120 > +struct sprinter_sexp {\r
121 > +    struct sprinter vtable;\r
122 > +    FILE *stream;\r
123 > +    /* Top of the state stack, or NULL if the printer is not currently\r
124 > +     * inside any aggregate types. */\r
125 > +    struct sexp_state *state;\r
126 > +\r
127 > +    /* A flag to signify that a separator should be inserted in the\r
128 > +     * output as soon as possible.\r
129 > +     */\r
130 > +    notmuch_bool_t insert_separator;\r
131 > +};\r
132 > +\r
133 > +struct sexp_state {\r
134 > +    struct sexp_state *parent;\r
135 > +\r
136 > +    /* True if nothing has been printed in this aggregate yet.\r
137 > +     * Suppresses the space before a value. */\r
138 > +    notmuch_bool_t first;\r
139 > +\r
140 > +    /* True if the state is a map state.\r
141 > +       Used to add a space between key/value pairs. */\r
142 > +    notmuch_bool_t in_map;\r
143 > +\r
144 > +    /* The character that closes the current aggregate. */\r
145 > +    char close;\r
146 > +};\r
147 > +\r
148 > +/* Helper function to set up the stream to print a value.  If this\r
149 > + * value follows another value, prints a space. */\r
150 > +static struct sprinter_sexp *\r
151 > +sexp_begin_value (struct sprinter *sp)\r
152 > +{\r
153 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
154 > +\r
155 > +    if (sps->state) {\r
156 > +        if (! sps->state->first) {\r
157 > +            if (sps->insert_separator) {\r
158 > +                fputc ('\n', sps->stream);\r
159 > +                sps->insert_separator = FALSE;\r
160 > +            } else {\r
161 > +                if( ! sps->state->in_map)\r
162 > +                    fputc (' ', sps->stream);\r
163 > +            }\r
164 > +        } else {\r
165 > +            sps->state->first = FALSE;\r
166 > +        }\r
167 > +    }\r
168 > +    return sps;\r
169 > +}\r
170 > +\r
171 > +/* Helper function to begin an aggregate type.  Prints the open\r
172 > + * character and pushes a new state frame. */\r
173 > +static void\r
174 > +sexp_begin_aggregate (struct sprinter *sp, char open, char close)\r
175 > +{\r
176 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
177 > +    struct sexp_state *state = talloc (sps, struct sexp_state);\r
178 > +    fputc (open, sps->stream);\r
179 > +    state->parent = sps->state;\r
180 > +    state->first = TRUE;\r
181 > +    state->in_map = FALSE;\r
182 > +    state->close = close;\r
183 > +    sps->state = state;\r
184 > +}\r
185 > +\r
186 > +static void\r
187 > +sexp_begin_map (struct sprinter *sp)\r
188 > +{\r
189 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
190 > +    sexp_begin_aggregate (sp, '(', ')');\r
191 > +    sps->state->in_map = TRUE;\r
192 > +}\r
193 > +\r
194 > +static void\r
195 > +sexp_begin_list (struct sprinter *sp)\r
196 > +{\r
197 > +    sexp_begin_aggregate (sp, '(', ')');\r
198 > +}\r
199 > +\r
200 > +static void\r
201 > +sexp_end (struct sprinter *sp)\r
202 > +{\r
203 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
204 > +    struct sexp_state *state = sps->state;\r
205 > +\r
206 > +    if (sps->state->in_map)\r
207 > +        fputc (')', sps->stream);\r
208 > +    fputc (sps->state->close, sps->stream);\r
209 > +    sps->state = state->parent;\r
210 > +    talloc_free (state);\r
211 > +    if (sps->state == NULL)\r
212 > +        fputc ('\n', sps->stream);\r
213 > +}\r
214 > +\r
215 > +/* This implementation supports embedded NULs as allowed by the JSON\r
216 > + * specification and Unicode.  Support for *parsing* embedded NULs\r
217 > + * varies, but is generally not a problem outside of C-based parsers\r
218 > + * (Python's json module and Emacs' json.el take embedded NULs in\r
219 > + * stride). */\r
220 > +static void\r
221 > +sexp_string_len_internal (struct sprinter *sp, const char *val, size_t len, notmuch_bool_t quote)\r
222 > +{\r
223 > +    static const char *const escapes[] = {\r
224 > +        ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",\r
225 > +        ['\f'] = "\\f",  ['\n'] = "\\n",  ['\t'] = "\\t"\r
226 > +    };\r
227 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
228 > +\r
229 > +    if(quote)\r
230 > +        fputc ('"', sps->stream);\r
231 > +    for (; len; ++val, --len) {\r
232 > +        unsigned char ch = *val;\r
233 > +        if (ch < ARRAY_SIZE (escapes) && escapes[ch])\r
234 > +            fputs (escapes[ch], sps->stream);\r
235 > +        else if (ch >= 32)\r
236 > +            fputc (ch, sps->stream);\r
237 > +        else\r
238 > +            fprintf (sps->stream, "\\u%04x", ch);\r
239 > +    }\r
240 > +    if(quote)\r
241 > +        fputc ('"', sps->stream);\r
242 > +}\r
243 > +\r
244 > +static void\r
245 > +sexp_string_len (struct sprinter *sp, const char *val, size_t len)\r
246 > +{\r
247 > +    sexp_string_len_internal (sp, val, len, TRUE); /* print quoted */\r
248 > +}\r
249 > +\r
250 > +static void\r
251 > +sexp_symbol_len (struct sprinter *sp, const char *val, size_t len)\r
252 > +{\r
253 > +    sexp_string_len_internal (sp, val, len, FALSE); /* print unquoted */\r
254 > +}\r
255 > +\r
256 > +static void\r
257 > +sexp_string (struct sprinter *sp, const char *val)\r
258 > +{\r
259 > +    if (val == NULL)\r
260 > +        val = "";\r
261 > +    sexp_string_len (sp, val, strlen (val));\r
262 > +}\r
263 > +\r
264 > +static void\r
265 > +sexp_symbol (struct sprinter *sp, const char *val)\r
266 > +{\r
267 > +    if (val == NULL)\r
268 > +        val = "";\r
269 > +    sexp_symbol_len (sp, val, strlen (val));\r
270 > +}\r
271 > +\r
272 > +static void\r
273 > +sexp_integer (struct sprinter *sp, int val)\r
274 > +{\r
275 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
276 > +\r
277 > +    fprintf (sps->stream, "%d", val);\r
278 > +}\r
279 > +\r
280 > +static void\r
281 > +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)\r
282 > +{\r
283 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
284 > +\r
285 > +    fputs (val ? "t" : "nil", sps->stream);\r
286 > +}\r
287 > +\r
288 > +static void\r
289 > +sexp_null (struct sprinter *sp)\r
290 > +{\r
291 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
292 > +\r
293 > +    fputs ("nil", sps->stream);\r
294 > +}\r
295 > +\r
296 > +static void\r
297 > +sexp_map_key (struct sprinter *sp, const char *key)\r
298 > +{\r
299 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
300 > +\r
301 > +    if( sps->state->in_map && ! sps->state->first)\r
302 > +        fputs (") ", sps->stream);\r
303 > +    fputc ('(', sps->stream);\r
304 > +    sexp_symbol (sp, key);\r
305 > +    fputc (' ', sps->stream);\r
306 > +}\r
307 > +\r
308 > +static void\r
309 > +sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))\r
310 > +{\r
311 > +}\r
312 > +\r
313 > +static void\r
314 > +sexp_separator (struct sprinter *sp)\r
315 > +{\r
316 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
317 > +\r
318 > +    sps->insert_separator = TRUE;\r
319 > +}\r
320 > +\r
321 > +struct sprinter *\r
322 > +sprinter_sexp_create (const void *ctx, FILE *stream)\r
323 > +{\r
324 > +    static const struct sprinter_sexp template = {\r
325 > +        .vtable = {\r
326 > +            .begin_map = sexp_begin_map,\r
327 > +            .begin_list = sexp_begin_list,\r
328 > +            .end = sexp_end,\r
329 > +            .string = sexp_string,\r
330 > +            .string_len = sexp_string_len,\r
331 > +            .integer = sexp_integer,\r
332 > +            .boolean = sexp_boolean,\r
333 > +            .null = sexp_null,\r
334 > +            .map_key = sexp_map_key,\r
335 > +            .separator = sexp_separator,\r
336 > +            .set_prefix = sexp_set_prefix,\r
337 > +            .is_text_printer = FALSE,\r
338 > +        }\r
339 > +    };\r
340 > +    struct sprinter_sexp *res;\r
341 > +\r
342 > +    res = talloc (ctx, struct sprinter_sexp);\r
343 > +    if (! res)\r
344 > +        return NULL;\r
345 > +\r
346 > +    *res = template;\r
347 > +    res->stream = stream;\r
348 > +    return &res->vtable;\r
349 > +}\r
350 > -- \r
351 > 1.8.0\r
352 >\r
353 > _______________________________________________\r
354 > notmuch mailing list\r
355 > notmuch@notmuchmail.org\r
356 > http://notmuchmail.org/mailman/listinfo/notmuch\r