X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch-search.c;h=7c973b3d6666ac46eca508b1364b4a9b4eb04304;hp=a96f07d53eab5fa073a539286c96f57213ebadbe;hb=20adfe39d7624150a5b337bb77a9647bb3def54b;hpb=0e035e1ccdbb989b27450d01bf3de90bbf9d0c33 diff --git a/notmuch-search.c b/notmuch-search.c index a96f07d5..7c973b3d 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -20,6 +20,7 @@ #include "notmuch-client.h" #include "sprinter.h" +#include "string-util.h" typedef enum { OUTPUT_SUMMARY, @@ -46,6 +47,45 @@ sanitize_string (const void *ctx, const char *str) return out; } +/* Return two stable query strings that identify exactly the matched + * and unmatched messages currently in thread. If there are no + * matched or unmatched messages, the returned buffers will be + * NULL. */ +static int +get_thread_query (notmuch_thread_t *thread, + char **matched_out, char **unmatched_out) +{ + notmuch_messages_t *messages; + char *escaped = NULL; + size_t escaped_len = 0; + + *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 (sprinter_t *format, notmuch_query_t *query, @@ -131,6 +171,25 @@ do_search_threads (sprinter_t *format, 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); @@ -177,7 +236,8 @@ do_search_messages (sprinter_t *format, notmuch_query_t *query, output_t output, int offset, - int limit) + int limit, + int dupe) { notmuch_message_t *message; notmuch_messages_t *messages; @@ -206,14 +266,17 @@ do_search_messages (sprinter_t *format, message = notmuch_messages_get (messages); if (output == OUTPUT_FILES) { + int j; filenames = notmuch_message_get_filenames (message); - for (; + for (j = 1; notmuch_filenames_valid (filenames); - notmuch_filenames_move_to_next (filenames)) + notmuch_filenames_move_to_next (filenames), j++) { - format->string (format, notmuch_filenames_get (filenames)); - format->separator (format); + if (dupe < 0 || dupe == j) { + format->string (format, notmuch_filenames_get (filenames)); + format->separator (format); + } } notmuch_filenames_destroy( filenames ); @@ -296,6 +359,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) int offset = 0; int limit = -1; /* unlimited */ notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE; + int dupe = -1; unsigned int i; enum { @@ -332,6 +396,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) { 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 } }; @@ -414,7 +479,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) break; case OUTPUT_MESSAGES: case OUTPUT_FILES: - ret = do_search_messages (format, query, output, offset, limit); + ret = do_search_messages (format, query, output, offset, limit, dupe); break; case OUTPUT_TAGS: ret = do_search_tags (notmuch, format, query);