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