--- /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 1E64B431FCB\r
+ for <notmuch@notmuchmail.org>; Thu, 30 Oct 2014 14:45:00 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\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 W9e1c-guwEsW for <notmuch@notmuchmail.org>;\r
+ Thu, 30 Oct 2014 14:44:54 -0700 (PDT)\r
+Received: from mail-wi0-f169.google.com (mail-wi0-f169.google.com\r
+ [209.85.212.169]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 7117A431FC2\r
+ for <notmuch@notmuchmail.org>; Thu, 30 Oct 2014 14:44:54 -0700 (PDT)\r
+Received: by mail-wi0-f169.google.com with SMTP id n3so2272309wiv.0\r
+ for <notmuch@notmuchmail.org>; Thu, 30 Oct 2014 14:44:53 -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;\r
+ bh=rGi9ycrqvPf8RS14xxuXc12EDY2C/sfKEdCfLC2VhUA=;\r
+ b=BIV6cqz4Hyi+DtpacKOswgqV46vcn5q/yIL+UqxMDNuojKsDKsWniWkHN3kJ6szqsk\r
+ oxZYMh1MuxIbeLhmrVMrYn9EQiQDssVxoR3W9tJryFQ236d5F0gNzVw4fwW/SfH/TRtx\r
+ rYnJYp0rNZXZd8eGumLJGZe5CykPgE0FImDTztfrtPC1Aw0/sNz2x2qj/22vxTBEpjtl\r
+ Y3Vjf/OB3dJsVqX3rdqqZ7pjZJwI9sn6x0oyx8YY6TrTooJwwePjJEvNeUWgllS+t2aW\r
+ kDvq+gp8mItnhBBpt/XnUwLAHE7ee9TmUCKW15MRoVVgsMvyOSwBtT3ve/DgO4SUljXE\r
+ v3yg==\r
+X-Gm-Message-State:\r
+ ALoCoQnavuTXMKmOOKjzh+gAtdUm9jxFM7pdsKCXJ+3ldhGJXIwbEWt+OGmaF9lj+wgij3UUTIFW\r
+X-Received: by 10.180.207.72 with SMTP id lu8mr45292742wic.2.1414705492760;\r
+ Thu, 30 Oct 2014 14:44:52 -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 bf6sm6254375wjb.13.2014.10.30.14.44.51\r
+ for <multiple recipients>\r
+ (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+ Thu, 30 Oct 2014 14:44:52 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH] cli: add support for notmuch search --duplicate=N with\r
+ --output=messages\r
+Date: Thu, 30 Oct 2014 23:44:49 +0200\r
+Message-Id: <1414705489-30771-1-git-send-email-jani@nikula.org>\r
+X-Mailer: git-send-email 2.1.1\r
+In-Reply-To: <m28ujy6kba.fsf@dme.org>\r
+References: <m28ujy6kba.fsf@dme.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: Thu, 30 Oct 2014 21:45:00 -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 | 34 ++++++++++++++++++++++++++++++----\r
+ 2 files changed, 38 insertions(+), 8 deletions(-)\r
+\r
+diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst\r
+index 90160f21e23c..aeba4bf604f6 100644\r
+--- a/doc/man1/notmuch-search.rst\r
++++ b/doc/man1/notmuch-search.rst\r
+@@ -122,10 +122,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 bc9be4593ecc..2bf876fd5abf 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -215,6 +215,24 @@ do_search_threads (sprinter_t *format,\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 (sprinter_t *format,\r
+ notmuch_query_t *query,\r
+ output_t output,\r
+@@ -265,10 +283,13 @@ do_search_messages (sprinter_t *format,\r
+ notmuch_filenames_destroy( filenames );\r
+ \r
+ } else { /* 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 (dupe <= 1 || 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
+ }\r
+ \r
+ notmuch_message_destroy (message);\r
+@@ -387,6 +408,11 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ if (opt_index < 0)\r
+ return EXIT_FAILURE;\r
+ \r
++ if (output != OUTPUT_FILES && output != OUTPUT_MESSAGES && 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
+ format = sprinter_text_create (config, stdout);\r
+-- \r
+2.1.1\r
+\r