+ *matched_out = *unmatched_out = NULL;
+
+ for (messages = notmuch_thread_get_messages (thread);
+ notmuch_messages_valid (messages);
+ notmuch_messages_move_to_next (messages))
+ {
+ notmuch_message_t *message = notmuch_messages_get (messages);
+ const char *mid = notmuch_message_get_message_id (message);
+ /* Determine which query buffer to extend */
+ char **buf = notmuch_message_get_flag (
+ message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
+ /* Add this message's id: query. Since "id" is an exclusive
+ * prefix, it is implicitly 'or'd together, so we only need to
+ * join queries with a space. */
+ if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
+ return -1;
+ if (*buf)
+ *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
+ else
+ *buf = talloc_strdup (thread, escaped);
+ if (!*buf)
+ return -1;
+ }
+ talloc_free (escaped);
+ return 0;
+}
+
+static int
+do_search_threads (search_context_t *ctx)
+{
+ notmuch_thread_t *thread;
+ notmuch_threads_t *threads;
+ notmuch_tags_t *tags;
+ sprinter_t *format = ctx->format;
+ time_t date;
+ int i;
+
+ if (ctx->offset < 0) {
+ ctx->offset += notmuch_query_count_threads (ctx->query);
+ if (ctx->offset < 0)
+ ctx->offset = 0;
+ }
+
+ threads = notmuch_query_search_threads (ctx->query);
+ if (threads == NULL)
+ return 1;
+
+ format->begin_list (format);
+
+ for (i = 0;
+ notmuch_threads_valid (threads) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
+ notmuch_threads_move_to_next (threads), i++)
+ {
+ thread = notmuch_threads_get (threads);
+
+ if (i < ctx->offset) {
+ notmuch_thread_destroy (thread);
+ continue;
+ }
+
+ if (ctx->output == OUTPUT_THREADS) {
+ format->set_prefix (format, "thread");
+ format->string (format,
+ notmuch_thread_get_thread_id (thread));
+ format->separator (format);
+ } else { /* output == OUTPUT_SUMMARY */
+ void *ctx_quote = talloc_new (thread);
+ const char *authors = notmuch_thread_get_authors (thread);
+ const char *subject = notmuch_thread_get_subject (thread);
+ const char *thread_id = notmuch_thread_get_thread_id (thread);
+ int matched = notmuch_thread_get_matched_messages (thread);
+ int total = notmuch_thread_get_total_messages (thread);
+ const char *relative_date = NULL;
+ notmuch_bool_t first_tag = TRUE;
+
+ format->begin_map (format);
+
+ if (ctx->sort == NOTMUCH_SORT_OLDEST_FIRST)
+ date = notmuch_thread_get_oldest_date (thread);
+ else
+ date = notmuch_thread_get_newest_date (thread);
+
+ relative_date = notmuch_time_relative_date (ctx_quote, date);
+
+ if (format->is_text_printer) {
+ /* Special case for the text formatter */
+ printf ("thread:%s %12s [%d/%d] %s; %s (",
+ thread_id,
+ relative_date,
+ matched,
+ total,
+ sanitize_string (ctx_quote, authors),
+ sanitize_string (ctx_quote, subject));
+ } else { /* Structured Output */
+ format->map_key (format, "thread");
+ format->string (format, thread_id);
+ format->map_key (format, "timestamp");
+ format->integer (format, date);
+ format->map_key (format, "date_relative");
+ format->string (format, relative_date);
+ format->map_key (format, "matched");
+ format->integer (format, matched);
+ format->map_key (format, "total");
+ format->integer (format, total);
+ format->map_key (format, "authors");
+ format->string (format, authors);
+ format->map_key (format, "subject");
+ format->string (format, subject);
+ if (notmuch_format_version >= 2) {
+ char *matched_query, *unmatched_query;
+ if (get_thread_query (thread, &matched_query,
+ &unmatched_query) < 0) {
+ fprintf (stderr, "Out of memory\n");
+ return 1;
+ }
+ format->map_key (format, "query");
+ format->begin_list (format);
+ if (matched_query)
+ format->string (format, matched_query);
+ else
+ format->null (format);
+ if (unmatched_query)
+ format->string (format, unmatched_query);
+ else
+ format->null (format);
+ format->end (format);
+ }
+ }
+
+ talloc_free (ctx_quote);
+
+ format->map_key (format, "tags");
+ format->begin_list (format);
+
+ for (tags = notmuch_thread_get_tags (thread);
+ notmuch_tags_valid (tags);
+ notmuch_tags_move_to_next (tags))
+ {
+ const char *tag = notmuch_tags_get (tags);
+
+ if (format->is_text_printer) {
+ /* Special case for the text formatter */
+ if (first_tag)
+ first_tag = FALSE;
+ else
+ fputc (' ', stdout);
+ fputs (tag, stdout);
+ } else { /* Structured Output */
+ format->string (format, tag);
+ }
+ }
+
+ if (format->is_text_printer)
+ printf (")");
+
+ format->end (format);
+ format->end (format);
+ format->separator (format);
+ }
+
+ notmuch_thread_destroy (thread);
+ }
+
+ format->end (format);
+
+ return 0;
+}
+
+/* Returns TRUE iff name and addr is duplicate. If not, stores the
+ * name/addr pair in order to detect subsequent duplicates. */
+static notmuch_bool_t
+is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
+{
+ notmuch_bool_t duplicate;
+ char *key;
+ mailbox_t *mailbox;
+
+ key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);
+ if (! key)
+ return FALSE;
+
+ duplicate = g_hash_table_lookup_extended (ctx->addresses, key, NULL, (gpointer)&mailbox);
+
+ if (! duplicate) {
+ mailbox = talloc (ctx->format, mailbox_t);
+ mailbox->name = talloc_strdup (mailbox, name);
+ mailbox->addr = talloc_strdup (mailbox, addr);
+ mailbox->count = 1;
+ g_hash_table_insert (ctx->addresses, key, mailbox);
+ } else {
+ mailbox->count++;
+ talloc_free (key);
+ }
+
+ return duplicate;
+}