[PATCH v4 0/7] emacs: Improve the cited message included in replies
[notmuch-archives.git] / 9b / 2d7c5dd144006c537ee5b9a3420b6cdfbcab6d
1 Return-Path: <jani@nikula.org>\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 402E3431FBC\r
6         for <notmuch@notmuchmail.org>; Sat, 15 Dec 2012 14:20:51 -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.7\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 DpbKc0TzkIEn for <notmuch@notmuchmail.org>;\r
16         Sat, 15 Dec 2012 14:20:49 -0800 (PST)\r
17 Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
18         [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 53C8D431FB6\r
21         for <notmuch@notmuchmail.org>; Sat, 15 Dec 2012 14:20:49 -0800 (PST)\r
22 Received: by mail-lb0-f181.google.com with SMTP id ge1so3662168lbb.26\r
23         for <notmuch@notmuchmail.org>; Sat, 15 Dec 2012 14:20:47 -0800 (PST)\r
24 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
25         d=google.com; s=20120113;\r
26         h=from:to:cc:subject:in-reply-to:references:user-agent:date\r
27         :message-id:mime-version:content-type:x-gm-message-state;\r
28         bh=PLnt+C2UQKPR8q12fFktFhDO5bw/oqGtj4oRH1Ud4V8=;\r
29         b=bfmkqzd/TvyoYNFWuzqQ2aroZfKHKU/mlTwqi/UINi2WUcsgrUMRYGhlvrF/bNT2fe\r
30         In+F++xbmaIQw8djPbVSCZN7ooFjoYT6T9iAD36tmexGD3B3ItmoLUvj4VQTwyb33vDY\r
31         t0naeOaveAhxaGAPJlUonLcwxVJiSohySTH5/h56i4toMymHVGG12UR8+m6FsRYPLAui\r
32         XLflMss2HdFyKpgvxLFW/nE+66KTrGADyBCNwwAPCsjDnLn1GLmgTiBnDUELTCx4q7Uz\r
33         Qjx0K4RboOeOe3l5gx7eoSSLg7QNGGRHx7JGNCvUPloLm4x3sJxiXb3mGGBWXj9yTU1p\r
34         N6Cw==\r
35 Received: by 10.112.50.43 with SMTP id z11mr3984586lbn.36.1355610046340;\r
36         Sat, 15 Dec 2012 14:20:46 -0800 (PST)\r
37 Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi.\r
38         [80.223.81.27])\r
39         by mx.google.com with ESMTPS id fb1sm3247399lbb.15.2012.12.15.14.20.43\r
40         (version=SSLv3 cipher=OTHER); Sat, 15 Dec 2012 14:20:44 -0800 (PST)\r
41 From: Jani Nikula <jani@nikula.org>\r
42 To: david@tethera.net, notmuch@notmuchmail.org\r
43 Subject: Re: [Patch v7 04/14] notmuch-tag: factor out double quoting routine\r
44 In-Reply-To: <1355492062-7546-5-git-send-email-david@tethera.net>\r
45 References: <1355492062-7546-1-git-send-email-david@tethera.net>\r
46         <1355492062-7546-5-git-send-email-david@tethera.net>\r
47 User-Agent: Notmuch/0.14+138~g7041c56 (http://notmuchmail.org) Emacs/23.4.1\r
48         (i686-pc-linux-gnu)\r
49 Date: Sun, 16 Dec 2012 00:20:42 +0200\r
50 Message-ID: <87zk1fot39.fsf@nikula.org>\r
51 MIME-Version: 1.0\r
52 Content-Type: text/plain; charset=us-ascii\r
53 X-Gm-Message-State:\r
54  ALoCoQlvGOEE9k1xDPF+Wk/Oxa/6vrQm4o74sMNA/RWPvneQWBmnF5SgQng57vSMJs3RXjlvIdOD\r
55 Cc: David Bremner <bremner@debian.org>\r
56 X-BeenThere: notmuch@notmuchmail.org\r
57 X-Mailman-Version: 2.1.13\r
58 Precedence: list\r
59 List-Id: "Use and development of the notmuch mail system."\r
60         <notmuch.notmuchmail.org>\r
61 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
62         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
63 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
64 List-Post: <mailto:notmuch@notmuchmail.org>\r
65 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
66 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
67         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
68 X-List-Received-Date: Sat, 15 Dec 2012 22:20:51 -0000\r
69 \r
70 On Fri, 14 Dec 2012, david@tethera.net wrote:\r
71 > From: David Bremner <bremner@debian.org>\r
72 >\r
73 > This could live in tag-util as well, but it is really nothing specific\r
74 > to tags (although the conventions are arguable specific to Xapian).\r
75 >\r
76 > The API is changed from "caller-allocates" to "readline-like". The scan for\r
77 > max tag length is pushed down into the double quoting routine.\r
78 > ---\r
79 >  notmuch-tag.c      |   50 ++++++++++++++++----------------------------------\r
80 >  util/string-util.c |   34 ++++++++++++++++++++++++++++++++++\r
81 >  util/string-util.h |    8 ++++++++\r
82 >  3 files changed, 58 insertions(+), 34 deletions(-)\r
83 >\r
84 > diff --git a/notmuch-tag.c b/notmuch-tag.c\r
85 > index 0965ee7..13f2268 100644\r
86 > --- a/notmuch-tag.c\r
87 > +++ b/notmuch-tag.c\r
88 > @@ -20,6 +20,7 @@\r
89 >  \r
90 >  #include "notmuch-client.h"\r
91 >  #include "tag-util.h"\r
92 > +#include "string-util.h"\r
93 >  \r
94 >  static volatile sig_atomic_t interrupted;\r
95 >  \r
96 > @@ -37,25 +38,6 @@ handle_sigint (unused (int sig))\r
97 >  }\r
98 >  \r
99 >  static char *\r
100 > -_escape_tag (char *buf, const char *tag)\r
101 > -{\r
102 > -    const char *in = tag;\r
103 > -    char *out = buf;\r
104 > -\r
105 > -    /* Boolean terms surrounded by double quotes can contain any\r
106 > -     * character.  Double quotes are quoted by doubling them. */\r
107 > -    *out++ = '"';\r
108 > -    while (*in) {\r
109 > -     if (*in == '"')\r
110 > -         *out++ = '"';\r
111 > -     *out++ = *in++;\r
112 > -    }\r
113 > -    *out++ = '"';\r
114 > -    *out = 0;\r
115 > -    return buf;\r
116 > -}\r
117 > -\r
118 > -static char *\r
119 >  _optimize_tag_query (void *ctx, const char *orig_query_string,\r
120 >                    const tag_op_list_t *list)\r
121 >  {\r
122 > @@ -67,44 +49,44 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
123 >       * parenthesize and the exclusion part of the query must not use\r
124 >       * the '-' operator (though the NOT operator is fine). */\r
125 >  \r
126 > -    char *escaped, *query_string;\r
127 > +    char *escaped = NULL;\r
128 > +    size_t escaped_len = 0;\r
129 > +    char *query_string;\r
130 >      const char *join = "";\r
131 >      size_t i;\r
132 > -    unsigned int max_tag_len = 0;\r
133 >  \r
134 >      /* Don't optimize if there are no tag changes. */\r
135 >      if (tag_op_list_size (list) == 0)\r
136 >       return talloc_strdup (ctx, orig_query_string);\r
137 >  \r
138 > -    /* Allocate a buffer for escaping tags.  This is large enough to\r
139 > -     * hold a fully escaped tag with every character doubled plus\r
140 > -     * enclosing quotes and a NUL. */\r
141 > -    for (i = 0; i < tag_op_list_size (list); i++)\r
142 > -     if (strlen (tag_op_list_tag (list, i)) > max_tag_len)\r
143 > -         max_tag_len = strlen (tag_op_list_tag (list, i));\r
144 > -\r
145 > -    escaped = talloc_array (ctx, char, max_tag_len * 2 + 3);\r
146 > -    if (! escaped)\r
147 > -     return NULL;\r
148 > -\r
149 >      /* Build the new query string */\r
150 >      if (strcmp (orig_query_string, "*") == 0)\r
151 >       query_string = talloc_strdup (ctx, "(");\r
152 >      else\r
153 >       query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);\r
154 >  \r
155 > +\r
156 > +    /* Boolean terms surrounded by double quotes can contain any\r
157 > +     * character.  Double quotes are quoted by doubling them. */\r
158 > +\r
159 >      for (i = 0; i < tag_op_list_size (list) && query_string; i++) {\r
160 > +     double_quote_str (ctx,\r
161 > +                       tag_op_list_tag (list, i),\r
162 > +                       &escaped, &escaped_len);\r
163 \r
164 Check return value?\r
165 \r
166 > +\r
167 >       query_string = talloc_asprintf_append_buffer (\r
168 >           query_string, "%s%stag:%s", join,\r
169 >           tag_op_list_isremove (list, i) ? "" : "not ",\r
170 > -         _escape_tag (escaped, tag_op_list_tag (list, i)));\r
171 > +         escaped);\r
172 >       join = " or ";\r
173 >      }\r
174 >  \r
175 >      if (query_string)\r
176 >       query_string = talloc_strdup_append_buffer (query_string, ")");\r
177 >  \r
178 > -    talloc_free (escaped);\r
179 > +    if (escaped)\r
180 > +     talloc_free (escaped);\r
181 > +\r
182 >      return query_string;\r
183 >  }\r
184 >  \r
185 > diff --git a/util/string-util.c b/util/string-util.c\r
186 > index 44f8cd3..ea7c25b 100644\r
187 > --- a/util/string-util.c\r
188 > +++ b/util/string-util.c\r
189 > @@ -20,6 +20,7 @@\r
190 >  \r
191 >  \r
192 >  #include "string-util.h"\r
193 > +#include "talloc.h"\r
194 >  \r
195 >  char *\r
196 >  strtok_len (char *s, const char *delim, size_t *len)\r
197 > @@ -32,3 +33,36 @@ strtok_len (char *s, const char *delim, size_t *len)\r
198 >  \r
199 >      return *len ? s : NULL;\r
200 >  }\r
201 > +\r
202 > +\r
203 > +int\r
204 > +double_quote_str (void *ctx, const char *str,\r
205 > +               char **buf, size_t *len)\r
206 > +{\r
207 > +    const char *in;\r
208 > +    char *out;\r
209 > +    size_t needed = 3;\r
210 > +\r
211 > +    for (in = str; *in; in++)\r
212 > +     needed += (*in == '"') ? 2 : 1;\r
213 > +\r
214 > +    if (needed > *len)\r
215 > +     *buf = talloc_realloc (ctx, *buf, char, 2*needed);\r
216 \r
217 You fail to set *len to 2*needed, leading to doing realloc every time.\r
218 \r
219 Also, I think you should follow the getline pattern like you did in\r
220 hex_encode: if *buf == NULL, the input value of *len is ignored.\r
221 \r
222 BR,\r
223 Jani.\r
224 \r
225 > +\r
226 > +    if (! *buf)\r
227 > +     return 1;\r
228 > +\r
229 > +    out = *buf;\r
230 > +\r
231 > +    *out++ = '"';\r
232 > +    in = str;\r
233 > +    while (*in) {\r
234 > +     if (*in == '"')\r
235 > +         *out++ = '"';\r
236 > +     *out++ = *in++;\r
237 > +    }\r
238 > +    *out++ = '"';\r
239 > +    *out = 0;\r
240 > +\r
241 > +    return 0;\r
242 > +}\r
243 > diff --git a/util/string-util.h b/util/string-util.h\r
244 > index ac7676c..b593bc7 100644\r
245 > --- a/util/string-util.h\r
246 > +++ b/util/string-util.h\r
247 > @@ -19,4 +19,12 @@\r
248 >  \r
249 >  char *strtok_len (char *s, const char *delim, size_t *len);\r
250 >  \r
251 > +/* Copy str to dest, surrounding with double quotes.\r
252 > + * Any internal double-quotes are doubled, i.e. a"b -> "a""b"\r
253 > + *\r
254 > + * Output is into buf; it may be talloc_realloced\r
255 > + * return 0 on success, non-zero on failure.\r
256 > + */\r
257 > +int double_quote_str (void *talloc_ctx, const char *str,\r
258 > +                   char **buf, size_t *len);\r
259 >  #endif\r
260 > -- \r
261 > 1.7.10.4\r
262 >\r
263 > _______________________________________________\r
264 > notmuch mailing list\r
265 > notmuch@notmuchmail.org\r
266 > http://notmuchmail.org/mailman/listinfo/notmuch\r