Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id D5E35431FDD for ; Sat, 1 Nov 2014 02:31:18 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id C62JcoodMM8V for ; Sat, 1 Nov 2014 02:31:14 -0700 (PDT) Received: from mail-wi0-f170.google.com (mail-wi0-f170.google.com [209.85.212.170]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 3AD8C431FD0 for ; Sat, 1 Nov 2014 02:31:14 -0700 (PDT) Received: by mail-wi0-f170.google.com with SMTP id q5so2848513wiv.1 for ; Sat, 01 Nov 2014 02:31:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=6OZMTSURYSdctYk2aJsQra6yGYrzzwbsP4NqbP8eI/k=; b=kNiNb96k1keA2D1MCXj++7hmIcCfDUbo4Z7CW+wn0AHgQ7OnhMeCRRpSwyt8G6vb54 pCnNY9Ri/SpYJqRU8gEaVwJMn1m4uurpGqpMOWdIxfPvDH3rXyjRMvqk6MNmCvi6T1pS uUNrRwlPOsK64IvmzeEpNubj4gqEOwr2ltSz2+MZUChm827mSOekrqJGyOAI5UzGYmC2 qCeSVHAoYllHmbZmml50F9uQ0rkAG2kl3YQUxjsduQIXghJfLDdnsP6hx5g08xpPYpDe bCnuahd0P3KSs+dZ45fgz4VC/0i/a1+j9cVOj2EF0mi8cgJe8FoFEHCJu8AAEPrtQD5r bxNw== X-Gm-Message-State: ALoCoQnKPyrbVX4dplugOxSlS3e0L9wSNEggFThPNxmJqSiSIwJOE3uUywKCvxfHUBCCJZYn9Wdl X-Received: by 10.180.19.234 with SMTP id i10mr2975057wie.28.1414834272829; Sat, 01 Nov 2014 02:31:12 -0700 (PDT) Received: from localhost (dsl-hkibrasgw2-58c36d-48.dhcp.inet.fi. [88.195.109.48]) by mx.google.com with ESMTPSA id n4sm1364692wiz.17.2014.11.01.02.31.11 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 01 Nov 2014 02:31:12 -0700 (PDT) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [PATCH v2 1/2] cli: add support for notmuch search --duplicate=N with --output=messages Date: Sat, 1 Nov 2014 11:31:06 +0200 Message-Id: <848dc1dd0e62dc85abd69f5cf675cbce89f5e127.1414834143.git.jani@nikula.org> X-Mailer: git-send-email 2.1.1 In-Reply-To: References: In-Reply-To: References: X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 01 Nov 2014 09:31:19 -0000 Print the message IDs of all messages matching the search terms that have at least N files associated with them. --- doc/man1/notmuch-search.rst | 12 ++++++++---- notmuch-search.c | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst index b6607c922cc0..8110086eff4f 100644 --- a/doc/man1/notmuch-search.rst +++ b/doc/man1/notmuch-search.rst @@ -142,10 +142,14 @@ Supported options for **search** include rather than the number of matching messages. ``--duplicate=N`` - Effective with ``--output=files``, output the Nth filename - associated with each message matching the query (N is 1-based). - If N is greater than the number of files associated with the - message, don't print anything. + For ``--output=files``, output the Nth filename associated + with each message matching the query (N is 1-based). If N is + greater than the number of files associated with the message, + don't print anything. + + For ``--output=messages``, only output message IDs of messages + matching the search terms that have at least N filenames + associated with them. Note that this option is orthogonal with the **folder:** search prefix. The prefix matches messages based on filenames. This diff --git a/notmuch-search.c b/notmuch-search.c index 671fe4139981..6345fb6280ac 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -311,6 +311,24 @@ process_address_header (const search_options_t *opt, const char *value) } static int +_count_filenames (notmuch_message_t *message) +{ + notmuch_filenames_t *filenames; + int i = 0; + + filenames = notmuch_message_get_filenames (message); + + while (notmuch_filenames_valid (filenames)) { + notmuch_filenames_move_to_next (filenames); + i++; + } + + notmuch_filenames_destroy (filenames); + + return i; +} + +static int do_search_messages (search_options_t *opt) { notmuch_message_t *message; @@ -357,10 +375,13 @@ do_search_messages (search_options_t *opt) notmuch_filenames_destroy( filenames ); } else if (opt->output == OUTPUT_MESSAGES) { - format->set_prefix (format, "id"); - format->string (format, - notmuch_message_get_message_id (message)); - format->separator (format); + /* special case 1 for speed */ + if (opt->dupe <= 1 || opt->dupe <= _count_filenames (message)) { + format->set_prefix (format, "id"); + format->string (format, + notmuch_message_get_message_id (message)); + format->separator (format); + } } else { if (opt->output & OUTPUT_SENDER) { const char *addrs; @@ -503,6 +524,12 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) if (! opt.output) opt.output = OUTPUT_SUMMARY; + if (opt.output != OUTPUT_FILES && opt.output != OUTPUT_MESSAGES && + opt.dupe != -1) { + fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n"); + return EXIT_FAILURE; + } + switch (format_sel) { case NOTMUCH_FORMAT_TEXT: opt.format = sprinter_text_create (config, stdout); -- 2.1.1