+ return 0;
+}
+
+static int
+do_search_messages (const void *ctx,
+ const search_format_t *format,
+ notmuch_query_t *query,
+ output_t output)
+{
+ notmuch_message_t *message;
+ notmuch_messages_t *messages;
+ int first_message = 1;
+
+ messages = notmuch_query_search_messages (query);
+ if (messages == NULL)
+ return 1;
+
+ for (;
+ notmuch_messages_valid (messages);
+ notmuch_messages_move_to_next (messages))
+ {
+ message = notmuch_messages_get (messages);
+
+ if (! first_message)
+ fputs (format->item_sep, stdout);
+
+ if (output == OUTPUT_FILES) {
+ format->item_id (ctx, "",
+ notmuch_message_get_filename (message));
+ } else { /* output == OUTPUT_MESSAGES */
+ format->item_id (ctx, "id:",
+ notmuch_message_get_message_id (message));
+ }
+
+ first_message = 0;
+
+ notmuch_message_destroy (message);
+ }
+
+ notmuch_messages_destroy (messages);
+
+ return 0;
+}
+
+static int
+do_search_tags (const void *ctx,
+ 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;
+
+ /* 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;
+
+ 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 (ctx, "", tag);
+
+ first_tag = 0;
+ }
+
+ notmuch_tags_destroy (tags);
+
+ if (messages)
+ notmuch_messages_destroy (messages);
+
+ return 0;