+ format->begin_list (format);
+
+ 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) {
+ int j;
+ filenames = notmuch_message_get_filenames (message);
+
+ for (j = 1;
+ notmuch_filenames_valid (filenames);
+ notmuch_filenames_move_to_next (filenames), j++)
+ {
+ if (dupe < 0 || dupe == j) {
+ format->string (format, notmuch_filenames_get (filenames));
+ format->separator (format);
+ }
+ }
+
+ notmuch_filenames_destroy( filenames );
+
+ } else { /* output == OUTPUT_MESSAGES */
+ format->set_prefix (format, "id");
+ format->string (format,
+ notmuch_message_get_message_id (message));
+ format->separator (format);
+ }
+
+ notmuch_message_destroy (message);
+ }
+
+ notmuch_messages_destroy (messages);
+
+ format->end (format);
+
+ return 0;
+}
+
+static int
+do_search_tags (notmuch_database_t *notmuch,
+ sprinter_t *format,
+ notmuch_query_t *query)
+{
+ notmuch_messages_t *messages = NULL;
+ notmuch_tags_t *tags;
+ const char *tag;
+
+ /* 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;
+
+ format->begin_list (format);
+
+ for (;
+ notmuch_tags_valid (tags);
+ notmuch_tags_move_to_next (tags))
+ {
+ tag = notmuch_tags_get (tags);
+
+ format->string (format, tag);
+ format->separator (format);
+
+ }
+
+ notmuch_tags_destroy (tags);
+
+ if (messages)
+ notmuch_messages_destroy (messages);
+
+ format->end (format);
+
+ return 0;
+}
+
+int
+notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
+{
+ notmuch_database_t *notmuch;
+ notmuch_query_t *query;
+ char *query_str;
+ notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
+ sprinter_t *format = NULL;
+ int opt_index, ret;
+ output_t output = OUTPUT_SUMMARY;
+ int offset = 0;
+ int limit = -1; /* unlimited */
+ notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;
+ int dupe = -1;
+ unsigned int i;
+
+ enum {
+ NOTMUCH_FORMAT_JSON,
+ NOTMUCH_FORMAT_TEXT,
+ NOTMUCH_FORMAT_TEXT0,
+ NOTMUCH_FORMAT_SEXP
+ } format_sel = NOTMUCH_FORMAT_TEXT;
+
+ notmuch_opt_desc_t options[] = {
+ { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',
+ (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
+ { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
+ { 0, 0 } } },
+ { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
+ (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
+ { "sexp", NOTMUCH_FORMAT_SEXP },
+ { "text", NOTMUCH_FORMAT_TEXT },
+ { "text0", NOTMUCH_FORMAT_TEXT0 },
+ { 0, 0 } } },
+ { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
+ { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
+ (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
+ { "threads", OUTPUT_THREADS },
+ { "messages", OUTPUT_MESSAGES },
+ { "files", OUTPUT_FILES },
+ { "tags", OUTPUT_TAGS },
+ { 0, 0 } } },
+ { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
+ (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
+ { "false", NOTMUCH_EXCLUDE_FALSE },
+ { "flag", NOTMUCH_EXCLUDE_FLAG },
+ { "all", NOTMUCH_EXCLUDE_ALL },
+ { 0, 0 } } },
+ { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },
+ { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0 },
+ { NOTMUCH_OPT_INT, &dupe, "duplicate", 'D', 0 },
+ { 0, 0, 0, 0, 0 }
+ };
+
+ opt_index = parse_arguments (argc, argv, options, 1);
+
+ if (opt_index < 0) {
+ return 1;
+ }
+
+ switch (format_sel) {
+ case NOTMUCH_FORMAT_TEXT:
+ format = sprinter_text_create (config, stdout);
+ break;
+ case NOTMUCH_FORMAT_TEXT0:
+ if (output == OUTPUT_SUMMARY) {
+ fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
+ return 1;
+ }
+ format = sprinter_text0_create (config, stdout);
+ break;
+ case NOTMUCH_FORMAT_JSON:
+ format = sprinter_json_create (config, stdout);
+ break;
+ case NOTMUCH_FORMAT_SEXP:
+ format = sprinter_sexp_create (config, stdout);
+ break;
+ default:
+ /* this should never happen */
+ INTERNAL_ERROR("no output format selected");
+ }
+
+ notmuch_exit_if_unsupported_format ();
+
+ if (notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))