+
+ if (first_thread)
+ fputs (format->results_null, stdout);
+ else
+ fputs (format->results_end, stdout);
+
+ return 0;
+}
+
+static int
+do_search_messages (const search_format_t *format,
+ notmuch_query_t *query,
+ output_t output,
+ int offset,
+ int limit)
+{
+ notmuch_message_t *message;
+ notmuch_messages_t *messages;
+ notmuch_filenames_t *filenames;
+ int first_message = 1;
+ int i;
+
+ notmuch_query_set_omit_excluded_messages (query, TRUE);
+
+ if (offset < 0) {
+ offset += notmuch_query_count_messages (query);
+ if (offset < 0)
+ offset = 0;
+ }
+
+ messages = notmuch_query_search_messages (query);
+ if (messages == NULL)
+ return 1;
+
+ fputs (format->results_start, stdout);
+
+ for (i = 0;
+ notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);
+ notmuch_messages_move_to_next (messages), i++)
+ {
+ if (i < offset)
+ continue;
+
+ message = notmuch_messages_get (messages);
+
+ if (output == OUTPUT_FILES) {
+ filenames = notmuch_message_get_filenames (message);
+
+ for (;
+ notmuch_filenames_valid (filenames);
+ notmuch_filenames_move_to_next (filenames))
+ {
+ if (! first_message)
+ fputs (format->item_sep, stdout);
+
+ format->item_id (message, "",
+ notmuch_filenames_get (filenames));
+
+ first_message = 0;
+ }
+
+ notmuch_filenames_destroy( filenames );
+
+ } else { /* output == OUTPUT_MESSAGES */
+ if (! first_message)
+ fputs (format->item_sep, stdout);
+
+ format->item_id (message, "id:",
+ notmuch_message_get_message_id (message));
+ first_message = 0;
+ }
+
+ notmuch_message_destroy (message);
+ }
+
+ notmuch_messages_destroy (messages);
+
+ if (first_message)
+ fputs (format->results_null, stdout);
+ else
+ fputs (format->results_end, stdout);
+
+ return 0;
+}
+
+static int
+do_search_tags (notmuch_database_t *notmuch,
+ const search_format_t *format,
+ notmuch_query_t *query)
+{
+ notmuch_messages_t *messages = NULL;
+ notmuch_tags_t *tags;
+ const char *tag;
+ int first_tag = 1;
+
+ notmuch_query_set_omit_excluded_messages (query, TRUE);
+ /* should the following only special case if no excluded terms
+ * specified? */
+
+ /* Special-case query of "*" for better performance. */
+ if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
+ tags = notmuch_database_get_all_tags (notmuch);
+ } else {
+ messages = notmuch_query_search_messages (query);
+ if (messages == NULL)
+ return 1;
+
+ tags = notmuch_messages_collect_tags (messages);
+ }
+ if (tags == NULL)
+ return 1;
+
+ fputs (format->results_start, stdout);
+
+ for (;
+ notmuch_tags_valid (tags);
+ notmuch_tags_move_to_next (tags))
+ {
+ tag = notmuch_tags_get (tags);
+
+ if (! first_tag)
+ fputs (format->item_sep, stdout);
+
+ format->item_id (tags, "", tag);
+
+ first_tag = 0;
+ }
+
+ notmuch_tags_destroy (tags);
+
+ if (messages)
+ notmuch_messages_destroy (messages);
+
+ if (first_tag)
+ fputs (format->results_null, stdout);
+ else
+ fputs (format->results_end, stdout);
+
+ return 0;