[PATCH v1 3/3] emacs: Improve the acquisition of text parts.
[notmuch-archives.git] / 90 / 4133bc736ba08335952f56480cb4c4131519f9
1 Return-Path: <amthrax@awakening.csail.mit.edu>\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 871B1431FB6\r
6         for <notmuch@notmuchmail.org>; Thu,  9 Dec 2010 13:00:30 -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\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         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 qs2mtyM5j5os for <notmuch@notmuchmail.org>;\r
16         Thu,  9 Dec 2010 13:00:28 -0800 (PST)\r
17 Received: from dmz-mailsec-scanner-5.mit.edu (DMZ-MAILSEC-SCANNER-5.MIT.EDU\r
18         [18.7.68.34])\r
19         by olra.theworths.org (Postfix) with ESMTP id 917D3431FB5\r
20         for <notmuch@notmuchmail.org>; Thu,  9 Dec 2010 13:00:28 -0800 (PST)\r
21 X-AuditID: 12074422-b7c3eae000000a70-ea-4d01436cb68a\r
22 Received: from mailhub-auth-1.mit.edu ( [18.9.21.35])\r
23         by dmz-mailsec-scanner-5.mit.edu (Symantec Brightmail Gateway) with\r
24         SMTP id BC.D7.02672.C63410D4; Thu,  9 Dec 2010 16:00:28 -0500 (EST)\r
25 Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
26         by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id oB9L0Sij011352; \r
27         Thu, 9 Dec 2010 16:00:28 -0500\r
28 Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
29         (authenticated bits=0)\r
30         (User authenticated as amdragon@ATHENA.MIT.EDU)\r
31         by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id oB9L0QXa010074\r
32         (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
33         Thu, 9 Dec 2010 16:00:27 -0500 (EST)\r
34 Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.72)\r
35         (envelope-from <amthrax@awakening.csail.mit.edu>)\r
36         id 1PQnbG-0007Hp-SX; Thu, 09 Dec 2010 16:00:26 -0500\r
37 From: Austin Clements <amdragon@MIT.EDU>\r
38 To: notmuch@notmuchmail.org\r
39 Subject: [PATCH 2/5] Implement an internal generic string list and use it.\r
40 Date: Thu,  9 Dec 2010 15:59:53 -0500\r
41 Message-Id: <1291928396-27937-3-git-send-email-amdragon@mit.edu>\r
42 X-Mailer: git-send-email 1.7.2.3\r
43 In-Reply-To: <1291928396-27937-1-git-send-email-amdragon@mit.edu>\r
44 References: <1291928396-27937-1-git-send-email-amdragon@mit.edu>\r
45 MIME-Version: 1.0\r
46 Content-Type: text/plain; charset=UTF-8\r
47 Content-Transfer-Encoding: 8bit\r
48 X-Brightmail-Tracker: AAAAAhbjYfcW42MD\r
49 Cc: Austin Clements <amdragon@mit.edu>\r
50 X-BeenThere: notmuch@notmuchmail.org\r
51 X-Mailman-Version: 2.1.13\r
52 Precedence: list\r
53 List-Id: "Use and development of the notmuch mail system."\r
54         <notmuch.notmuchmail.org>\r
55 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
56         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
57 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
58 List-Post: <mailto:notmuch@notmuchmail.org>\r
59 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
60 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
61         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
62 X-List-Received-Date: Thu, 09 Dec 2010 21:00:30 -0000\r
63 \r
64 This replaces the guts of the filename list and tag list, making those\r
65 interfaces simple iterators over the generic string list.  The\r
66 directory, message filename, and tags-related code now build generic\r
67 string lists and then wraps them in specific iterators.  The real wins\r
68 come in later patches, when we use these for even more generic\r
69 functionality.\r
70 \r
71 As a nice side-effect, this also eliminates the annoying dependency on\r
72 GList in the tag list.\r
73 ---\r
74  lib/Makefile.local    |    1 +\r
75  lib/database.cc       |   12 +++---\r
76  lib/directory.cc      |    7 ++--\r
77  lib/filenames.c       |   52 +++------------------------\r
78  lib/message.cc        |   15 ++++----\r
79  lib/messages.c        |   11 +++---\r
80  lib/notmuch-private.h |   68 +++++++++++++++++++++--------------\r
81  lib/strings.c         |   94 +++++++++++++++++++++++++++++++++++++++++++++++++\r
82  lib/tags.c            |   63 ++++++--------------------------\r
83  lib/thread.cc         |   10 +++---\r
84  10 files changed, 179 insertions(+), 154 deletions(-)\r
85  create mode 100644 lib/strings.c\r
86 \r
87 diff --git a/lib/Makefile.local b/lib/Makefile.local\r
88 index 5233ea6..37d1c0d 100644\r
89 --- a/lib/Makefile.local\r
90 +++ b/lib/Makefile.local\r
91 @@ -50,6 +50,7 @@ extra_cflags += -I$(dir) -fPIC\r
92  libnotmuch_c_srcs =            \\r
93         $(notmuch_compat_srcs)  \\r
94         $(dir)/filenames.c      \\r
95 +       $(dir)/strings.c        \\r
96         $(dir)/libsha1.c        \\r
97         $(dir)/message-file.c   \\r
98         $(dir)/messages.c       \\r
99 diff --git a/lib/database.cc b/lib/database.cc\r
100 index 7a00917..45613bd 100644\r
101 --- a/lib/database.cc\r
102 +++ b/lib/database.cc\r
103 @@ -1752,15 +1752,15 @@ _notmuch_convert_tags (void *ctx, Xapian::TermIterator &i,\r
104                        Xapian::TermIterator &end)\r
105  {\r
106      const char *prefix = _find_prefix ("tag");\r
107 -    notmuch_tags_t *tags;\r
108 +    notmuch_string_list_t *list;\r
109      std::string tag;\r
110  \r
111      /* Currently this iteration is written with the assumption that\r
112       * "tag" has a single-character prefix. */\r
113      assert (strlen (prefix) == 1);\r
114  \r
115 -    tags = _notmuch_tags_create (ctx);\r
116 -    if (unlikely (tags == NULL))\r
117 +    list = _notmuch_string_list_create (ctx);\r
118 +    if (unlikely (list == NULL))\r
119         return NULL;\r
120  \r
121      i.skip_to (prefix);\r
122 @@ -1771,14 +1771,14 @@ _notmuch_convert_tags (void *ctx, Xapian::TermIterator &i,\r
123         if (tag.empty () || tag[0] != *prefix)\r
124             break;\r
125  \r
126 -       _notmuch_tags_add_tag (tags, tag.c_str () + 1);\r
127 +       _notmuch_string_list_append (list, tag.c_str () + 1);\r
128  \r
129         i++;\r
130      }\r
131  \r
132 -    _notmuch_tags_prepare_iterator (tags);\r
133 +    _notmuch_string_list_sort (list);\r
134  \r
135 -    return tags;\r
136 +    return _notmuch_tags_create (ctx, list, TRUE);\r
137  }\r
138  \r
139  notmuch_tags_t *\r
140 diff --git a/lib/directory.cc b/lib/directory.cc\r
141 index 946be4f..aeee9ca 100644\r
142 --- a/lib/directory.cc\r
143 +++ b/lib/directory.cc\r
144 @@ -33,11 +33,11 @@ _create_filenames_for_terms_with_prefix (void *ctx,\r
145                                          notmuch_database_t *notmuch,\r
146                                          const char *prefix)\r
147  {\r
148 -    notmuch_filename_list_t *filename_list;\r
149 +    notmuch_string_list_t *filename_list;\r
150      Xapian::TermIterator i, end;\r
151      int prefix_len = strlen (prefix);\r
152  \r
153 -    filename_list = _notmuch_filename_list_create (ctx);\r
154 +    filename_list = _notmuch_string_list_create (ctx);\r
155      if (unlikely (filename_list == NULL))\r
156         return NULL;\r
157  \r
158 @@ -47,8 +47,7 @@ _create_filenames_for_terms_with_prefix (void *ctx,\r
159      {\r
160         std::string term = *i;\r
161  \r
162 -       _notmuch_filename_list_add_filename (filename_list, term.c_str () +\r
163 -                                            prefix_len);\r
164 +       _notmuch_string_list_append (filename_list, term.c_str () + prefix_len);\r
165      }\r
166  \r
167      return _notmuch_filenames_create (ctx, filename_list);\r
168 diff --git a/lib/filenames.c b/lib/filenames.c\r
169 index f078c95..f1ea243 100644\r
170 --- a/lib/filenames.c\r
171 +++ b/lib/filenames.c\r
172 @@ -21,56 +21,14 @@\r
173  #include "notmuch-private.h"\r
174  \r
175  struct _notmuch_filenames {\r
176 -    notmuch_filename_node_t *iterator;\r
177 +    notmuch_string_node_t *iterator;\r
178  };\r
179  \r
180 -/* Create a new notmuch_filename_list_t object, with 'ctx' as its\r
181 - * talloc owner.\r
182 - *\r
183 - * This function can return NULL in case of out-of-memory.\r
184 - */\r
185 -notmuch_filename_list_t *\r
186 -_notmuch_filename_list_create (const void *ctx)\r
187 -{\r
188 -    notmuch_filename_list_t *list;\r
189 -\r
190 -    list = talloc (ctx, notmuch_filename_list_t);\r
191 -    if (unlikely (list == NULL))\r
192 -       return NULL;\r
193 -\r
194 -    list->head = NULL;\r
195 -    list->tail = &list->head;\r
196 -\r
197 -    return list;\r
198 -}\r
199 -\r
200 -void\r
201 -_notmuch_filename_list_add_filename (notmuch_filename_list_t *list,\r
202 -                                    const char *filename)\r
203 -{\r
204 -    /* Create and initialize new node. */\r
205 -    notmuch_filename_node_t *node = talloc (list,\r
206 -                                           notmuch_filename_node_t);\r
207 -\r
208 -    node->filename = talloc_strdup (node, filename);\r
209 -    node->next = NULL;\r
210 -\r
211 -    /* Append the node to the list. */\r
212 -    *(list->tail) = node;\r
213 -    list->tail = &node->next;\r
214 -}\r
215 -\r
216 -void\r
217 -_notmuch_filename_list_destroy (notmuch_filename_list_t *list)\r
218 -{\r
219 -    talloc_free (list);\r
220 -}\r
221 -\r
222 -/* The notmuch_filenames_t is an iterator object for a\r
223 - * notmuch_filename_list_t */\r
224 +/* The notmuch_filenames_t iterates over a notmuch_string_list_t of\r
225 + * file names */\r
226  notmuch_filenames_t *\r
227  _notmuch_filenames_create (const void *ctx,\r
228 -                          notmuch_filename_list_t *list)\r
229 +                          notmuch_string_list_t *list)\r
230  {\r
231      notmuch_filenames_t *filenames;\r
232  \r
233 @@ -99,7 +57,7 @@ notmuch_filenames_get (notmuch_filenames_t *filenames)\r
234      if (filenames->iterator == NULL)\r
235         return NULL;\r
236  \r
237 -    return filenames->iterator->filename;\r
238 +    return filenames->iterator->string;\r
239  }\r
240  \r
241  void\r
242 diff --git a/lib/message.cc b/lib/message.cc\r
243 index d6ab636..031eda5 100644\r
244 --- a/lib/message.cc\r
245 +++ b/lib/message.cc\r
246 @@ -32,7 +32,7 @@ struct _notmuch_message {\r
247      char *message_id;\r
248      char *thread_id;\r
249      char *in_reply_to;\r
250 -    notmuch_filename_list_t *filename_list;\r
251 +    notmuch_string_list_t *filename_list;\r
252      char *author;\r
253      notmuch_message_file_t *message_file;\r
254      notmuch_message_list_t *replies;\r
255 @@ -434,7 +434,7 @@ _notmuch_message_add_filename (notmuch_message_t *message,\r
256      char *direntry;\r
257  \r
258      if (message->filename_list) {\r
259 -       _notmuch_filename_list_destroy (message->filename_list);\r
260 +       talloc_free (message->filename_list);\r
261         message->filename_list = NULL;\r
262      }\r
263  \r
264 @@ -511,7 +511,7 @@ _notmuch_message_ensure_filename_list (notmuch_message_t *message)\r
265      if (message->filename_list)\r
266         return;\r
267  \r
268 -    message->filename_list = _notmuch_filename_list_create (message);\r
269 +    message->filename_list = _notmuch_string_list_create (message);\r
270  \r
271      i = message->doc.termlist_begin ();\r
272      i.skip_to (prefix);\r
273 @@ -532,7 +532,7 @@ _notmuch_message_ensure_filename_list (notmuch_message_t *message)\r
274         if (data == NULL)\r
275             INTERNAL_ERROR ("message with no filename");\r
276  \r
277 -       _notmuch_filename_list_add_filename (message->filename_list, data);\r
278 +       _notmuch_string_list_append (message->filename_list, data);\r
279  \r
280         return;\r
281      }\r
282 @@ -573,8 +573,7 @@ _notmuch_message_ensure_filename_list (notmuch_message_t *message)\r
283             filename = talloc_asprintf (message, "%s/%s",\r
284                                         db_path, basename);\r
285  \r
286 -       _notmuch_filename_list_add_filename (message->filename_list,\r
287 -                                            filename);\r
288 +       _notmuch_string_list_append (message->filename_list, filename);\r
289  \r
290         talloc_free (local);\r
291      }\r
292 @@ -589,12 +588,12 @@ notmuch_message_get_filename (notmuch_message_t *message)\r
293         return NULL;\r
294  \r
295      if (message->filename_list->head == NULL ||\r
296 -       message->filename_list->head->filename == NULL)\r
297 +       message->filename_list->head->string == NULL)\r
298      {\r
299         INTERNAL_ERROR ("message with no filename");\r
300      }\r
301  \r
302 -    return message->filename_list->head->filename;\r
303 +    return message->filename_list->head->string;\r
304  }\r
305  \r
306  notmuch_filenames_t *\r
307 diff --git a/lib/messages.c b/lib/messages.c\r
308 index 120a48a..404e8b3 100644\r
309 --- a/lib/messages.c\r
310 +++ b/lib/messages.c\r
311 @@ -146,13 +146,14 @@ notmuch_messages_destroy (notmuch_messages_t *messages)\r
312  notmuch_tags_t *\r
313  notmuch_messages_collect_tags (notmuch_messages_t *messages)\r
314  {\r
315 -    notmuch_tags_t *tags, *msg_tags;\r
316 +    notmuch_string_list_t *tags;\r
317 +    notmuch_tags_t *msg_tags;\r
318      notmuch_message_t *msg;\r
319      GHashTable *htable;\r
320      GList *keys, *l;\r
321      const char *tag;\r
322  \r
323 -    tags = _notmuch_tags_create (messages);\r
324 +    tags = _notmuch_string_list_create (messages);\r
325      if (tags == NULL) return NULL;\r
326  \r
327      htable = g_hash_table_new_full (g_str_hash, g_str_equal, free, NULL);\r
328 @@ -170,12 +171,12 @@ notmuch_messages_collect_tags (notmuch_messages_t *messages)\r
329  \r
330      keys = g_hash_table_get_keys (htable);\r
331      for (l = keys; l; l = l->next) {\r
332 -       _notmuch_tags_add_tag (tags, (char *)l->data);\r
333 +       _notmuch_string_list_append (tags, (char *)l->data);\r
334      }\r
335  \r
336      g_list_free (keys);\r
337      g_hash_table_destroy (htable);\r
338  \r
339 -    _notmuch_tags_prepare_iterator (tags);\r
340 -    return tags;\r
341 +    _notmuch_string_list_sort (tags);\r
342 +    return _notmuch_tags_create (messages, tags, TRUE);\r
343  }\r
344 diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
345 index 303aeb3..b6f1095 100644\r
346 --- a/lib/notmuch-private.h\r
347 +++ b/lib/notmuch-private.h\r
348 @@ -453,48 +453,60 @@ notmuch_sha1_of_string (const char *str);\r
349  char *\r
350  notmuch_sha1_of_file (const char *filename);\r
351  \r
352 -/* tags.c */\r
353 +/* strings.c */\r
354  \r
355 -notmuch_tags_t *\r
356 -_notmuch_tags_create (void *ctx);\r
357 +typedef struct _notmuch_string_node {\r
358 +    char *string;\r
359 +    struct _notmuch_string_node *next;\r
360 +} notmuch_string_node_t;\r
361 +\r
362 +typedef struct _notmuch_string_list {\r
363 +    int length;\r
364 +    notmuch_string_node_t *head;\r
365 +    notmuch_string_node_t **tail;\r
366 +} notmuch_string_list_t;\r
367  \r
368 +notmuch_string_list_t *\r
369 +_notmuch_string_list_create (const void *ctx);\r
370 +\r
371 +/* Add 'string' to 'list'.\r
372 + *\r
373 + * The list will create its own talloced copy of 'string'.\r
374 + */\r
375  void\r
376 -_notmuch_tags_add_tag (notmuch_tags_t *tags, const char *tag);\r
377 +_notmuch_string_list_append (notmuch_string_list_t *list,\r
378 +                            const char *string);\r
379  \r
380  void\r
381 -_notmuch_tags_prepare_iterator (notmuch_tags_t *tags);\r
382 +_notmuch_string_list_sort (notmuch_string_list_t *list);\r
383  \r
384 -/* filenames.c */\r
385 +/* tags.c */\r
386 +\r
387 +notmuch_tags_t *\r
388 +_notmuch_tags_create (const void *ctx, notmuch_string_list_t *list,\r
389 +                     notmuch_bool_t steal);\r
390  \r
391 -typedef struct _notmuch_filename_node {\r
392 -    char *filename;\r
393 -    struct _notmuch_filename_node *next;\r
394 -} notmuch_filename_node_t;\r
395 +/* filenames.c */\r
396  \r
397 -typedef struct _notmuch_filename_list {\r
398 -    notmuch_filename_node_t *head;\r
399 -    notmuch_filename_node_t **tail;\r
400 -} notmuch_filename_list_t;\r
401 +/* The notmuch_filenames_t iterates over a notmuch_string_list_t of\r
402 + * file names */\r
403 +notmuch_filenames_t *\r
404 +_notmuch_filenames_create (const void *ctx,\r
405 +                          notmuch_string_list_t *list);\r
406  \r
407 -notmuch_filename_list_t *\r
408 -_notmuch_filename_list_create (const void *ctx);\r
409 +/* tags.c */\r
410  \r
411 -/* Add 'filename' to 'list'.\r
412 - *\r
413 - * The list will create its own talloced copy of 'filename'.\r
414 - */\r
415 -void\r
416 -_notmuch_filename_list_add_filename (notmuch_filename_list_t *list,\r
417 -                                    const char *filename);\r
418 +notmuch_tags_t *\r
419 +_notmuch_tags_create (const void *ctx, notmuch_string_list_t *list,\r
420 +                     notmuch_bool_t steal);\r
421  \r
422 -void\r
423 -_notmuch_filename_list_destroy (notmuch_filename_list_t *list);\r
424 +/* filenames.c */\r
425  \r
426 -/* The notmuch_filenames_t is an iterator object for a\r
427 - * notmuch_filename_list_t */\r
428 +/* The notmuch_filenames_t iterates over a notmuch_string_list_t of\r
429 + * file names */\r
430  notmuch_filenames_t *\r
431  _notmuch_filenames_create (const void *ctx,\r
432 -                          notmuch_filename_list_t *list);\r
433 +                          notmuch_string_list_t *list);\r
434  \r
435  #pragma GCC visibility pop\r
436  \r
437 diff --git a/lib/strings.c b/lib/strings.c\r
438 new file mode 100644\r
439 index 0000000..d92a0ba\r
440 --- /dev/null\r
441 +++ b/lib/strings.c\r
442 @@ -0,0 +1,94 @@\r
443 +/* strings.c - Iterator for a list of strings\r
444 + *\r
445 + * Copyright Â© 2010 Intel Corporation\r
446 + *\r
447 + * This program is free software: you can redistribute it and/or modify\r
448 + * it under the terms of the GNU General Public License as published by\r
449 + * the Free Software Foundation, either version 3 of the License, or\r
450 + * (at your option) any later version.\r
451 + *\r
452 + * This program is distributed in the hope that it will be useful,\r
453 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
454 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
455 + * GNU General Public License for more details.\r
456 + *\r
457 + * You should have received a copy of the GNU General Public License\r
458 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
459 + *\r
460 + * Author: Carl Worth <cworth@cworth.org>\r
461 + */\r
462 +\r
463 +#include "notmuch-private.h"\r
464 +\r
465 +/* Create a new notmuch_string_list_t object, with 'ctx' as its\r
466 + * talloc owner.\r
467 + *\r
468 + * This function can return NULL in case of out-of-memory.\r
469 + */\r
470 +notmuch_string_list_t *\r
471 +_notmuch_string_list_create (const void *ctx)\r
472 +{\r
473 +    notmuch_string_list_t *list;\r
474 +\r
475 +    list = talloc (ctx, notmuch_string_list_t);\r
476 +    if (unlikely (list == NULL))\r
477 +       return NULL;\r
478 +\r
479 +    list->length = 0;\r
480 +    list->head = NULL;\r
481 +    list->tail = &list->head;\r
482 +\r
483 +    return list;\r
484 +}\r
485 +\r
486 +void\r
487 +_notmuch_string_list_append (notmuch_string_list_t *list,\r
488 +                            const char *string)\r
489 +{\r
490 +    /* Create and initialize new node. */\r
491 +    notmuch_string_node_t *node = talloc (list, notmuch_string_node_t);\r
492 +\r
493 +    node->string = talloc_strdup (node, string);\r
494 +    node->next = NULL;\r
495 +\r
496 +    /* Append the node to the list. */\r
497 +    *(list->tail) = node;\r
498 +    list->tail = &node->next;\r
499 +    list->length++;\r
500 +}\r
501 +\r
502 +static int\r
503 +cmpnode (const void *pa, const void *pb)\r
504 +{\r
505 +    notmuch_string_node_t *a = *(notmuch_string_node_t * const *)pa;\r
506 +    notmuch_string_node_t *b = *(notmuch_string_node_t * const *)pb;\r
507 +\r
508 +    return strcmp (a->string, b->string);\r
509 +}\r
510 +\r
511 +void\r
512 +_notmuch_string_list_sort (notmuch_string_list_t *list)\r
513 +{\r
514 +    notmuch_string_node_t **nodes, *node;\r
515 +    int i;\r
516 +\r
517 +    if (list->length == 0)\r
518 +       return;\r
519 +\r
520 +    nodes = talloc_array (list, notmuch_string_node_t *, list->length);\r
521 +    if (unlikely (nodes == NULL))\r
522 +       INTERNAL_ERROR ("Could not allocate memory for list sort");\r
523 +\r
524 +    for (i = 0, node = list->head; node; i++, node = node->next)\r
525 +       nodes[i] = node;\r
526 +\r
527 +    qsort (nodes, list->length, sizeof (*nodes), cmpnode);\r
528 +\r
529 +    for (i = 0; i < list->length - 1; ++i)\r
530 +       nodes[i]->next = nodes[i+1];\r
531 +    nodes[i]->next = NULL;\r
532 +    list->head = nodes[0];\r
533 +    list->tail = &nodes[i]->next;\r
534 +\r
535 +    talloc_free (nodes);\r
536 +}\r
537 diff --git a/lib/tags.c b/lib/tags.c\r
538 index 8fe4a3f..8392ef6 100644\r
539 --- a/lib/tags.c\r
540 +++ b/lib/tags.c\r
541 @@ -20,30 +20,20 @@\r
542  \r
543  #include "notmuch-private.h"\r
544  \r
545 -#include <glib.h> /* GList */\r
546 -\r
547  struct _notmuch_tags {\r
548 -    int sorted;\r
549 -    GList *tags;\r
550 -    GList *iterator;\r
551 +    notmuch_string_node_t *iterator;\r
552  };\r
553  \r
554 -/* XXX: Should write some talloc-friendly list to avoid the need for\r
555 - * this. */\r
556 -static int\r
557 -_notmuch_tags_destructor (notmuch_tags_t *tags)\r
558 -{\r
559 -    g_list_free (tags->tags);\r
560 -\r
561 -    return 0;\r
562 -}\r
563 -\r
564  /* Create a new notmuch_tags_t object, with 'ctx' as its talloc owner.\r
565 + * If steal is true, then the iterator will steal the reference to the\r
566 + * list (useful for one-shot iterators); otherwise it will add a\r
567 + * reference.\r
568   *\r
569   * This function can return NULL in case of out-of-memory.\r
570   */\r
571  notmuch_tags_t *\r
572 -_notmuch_tags_create (void *ctx)\r
573 +_notmuch_tags_create (const void *ctx, notmuch_string_list_t *list,\r
574 +                     notmuch_bool_t steal)\r
575  {\r
576      notmuch_tags_t *tags;\r
577  \r
578 @@ -51,44 +41,15 @@ _notmuch_tags_create (void *ctx)\r
579      if (unlikely (tags == NULL))\r
580         return NULL;\r
581  \r
582 -    talloc_set_destructor (tags, _notmuch_tags_destructor);\r
583 -\r
584 -    tags->sorted = 1;\r
585 -    tags->tags = NULL;\r
586 -    tags->iterator = NULL;\r
587 +    tags->iterator = list->head;\r
588 +    if (steal)\r
589 +       talloc_steal (tags, list);\r
590 +    else\r
591 +       (void) talloc_reference (tags, list);\r
592  \r
593      return tags;\r
594  }\r
595  \r
596 -/* Add a new tag to 'tags'. The tags object will create its own copy\r
597 - * of the string.\r
598 - *\r
599 - * Note: The tags object will not do anything to prevent duplicate\r
600 - * tags being stored, so the caller really shouldn't pass\r
601 - * duplicates. */\r
602 -void\r
603 -_notmuch_tags_add_tag (notmuch_tags_t *tags, const char *tag)\r
604 -{\r
605 -    tags->tags = g_list_prepend (tags->tags, talloc_strdup (tags, tag));\r
606 -    tags->sorted = 0;\r
607 -}\r
608 -\r
609 -/* Prepare 'tag' for iteration.\r
610 - *\r
611 - * The internal creator of 'tags' should call this function before\r
612 - * returning 'tags' to the user to call the public functions such as\r
613 - * notmuch_tags_valid, notmuch_tags_get, and\r
614 - * notmuch_tags_move_to_next. */\r
615 -void\r
616 -_notmuch_tags_prepare_iterator (notmuch_tags_t *tags)\r
617 -{\r
618 -    if (! tags->sorted)\r
619 -       tags->tags = g_list_sort (tags->tags, (GCompareFunc) strcmp);\r
620 -    tags->sorted = 1;\r
621 -\r
622 -    tags->iterator = tags->tags;\r
623 -}\r
624 -\r
625  notmuch_bool_t\r
626  notmuch_tags_valid (notmuch_tags_t *tags)\r
627  {\r
628 @@ -101,7 +62,7 @@ notmuch_tags_get (notmuch_tags_t *tags)\r
629      if (tags->iterator == NULL)\r
630         return NULL;\r
631  \r
632 -    return (char *) tags->iterator->data;\r
633 +    return (char *) tags->iterator->string;\r
634  }\r
635  \r
636  void\r
637 diff --git a/lib/thread.cc b/lib/thread.cc\r
638 index b29f2c9..5b024fd 100644\r
639 --- a/lib/thread.cc\r
640 +++ b/lib/thread.cc\r
641 @@ -535,23 +535,23 @@ notmuch_thread_get_newest_date (notmuch_thread_t *thread)\r
642  notmuch_tags_t *\r
643  notmuch_thread_get_tags (notmuch_thread_t *thread)\r
644  {\r
645 -    notmuch_tags_t *tags;\r
646 +    notmuch_string_list_t *tags;\r
647      GList *keys, *l;\r
648  \r
649 -    tags = _notmuch_tags_create (thread);\r
650 +    tags = _notmuch_string_list_create (thread);\r
651      if (unlikely (tags == NULL))\r
652         return NULL;\r
653  \r
654      keys = g_hash_table_get_keys (thread->tags);\r
655  \r
656      for (l = keys; l; l = l->next)\r
657 -       _notmuch_tags_add_tag (tags, (char *) l->data);\r
658 +       _notmuch_string_list_append (tags, (char *) l->data);\r
659  \r
660      g_list_free (keys);\r
661  \r
662 -    _notmuch_tags_prepare_iterator (tags);\r
663 +    _notmuch_string_list_sort (tags);\r
664  \r
665 -    return tags;\r
666 +    return _notmuch_tags_create (thread, tags, true);\r
667  }\r
668  \r
669  void\r
670 -- \r
671 1.7.2.3\r
672 \r