--- /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 arlo.cworth.org (Postfix) with ESMTP id DBE016DE15D4\r
+ for <notmuch@notmuchmail.org>; Thu, 3 Sep 2015 12:40:36 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.426\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.426 tagged_above=-999 required=5 tests=[AWL=0.294,\r
+ RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 0ixXvFvmogwZ for <notmuch@notmuchmail.org>;\r
+ Thu, 3 Sep 2015 12:40:35 -0700 (PDT)\r
+Received: from mail-wi0-f174.google.com (mail-wi0-f174.google.com\r
+ [209.85.212.174])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 7E6076DE1534\r
+ for <notmuch@notmuchmail.org>; Thu, 3 Sep 2015 12:40:28 -0700 (PDT)\r
+Received: by wiclk2 with SMTP id lk2so1207528wic.0\r
+ for <notmuch@notmuchmail.org>; Thu, 03 Sep 2015 12:40:27 -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=wj+tnZTwmrqNPh73iKZVy64DGNf+PYHfGPjHpeWz3Cs=;\r
+ b=RvqXDkwKlZg0JcuQOXwyfv2hJAFWgxfO69Z3gOTgorrEVwvoF8nDJrsgQzxixMI55v\r
+ v0avq/Tu/gFsq9E0avlwIeOp+iARZRmNFxmIsZ3Vc1TdYYi/hjv6niGpvqBEkKpdQHSU\r
+ sZaF9DWrPQV529LybAB79pp0r330Ft6mbRilgnoKCmEQrANzXePbmNWgf4YndYibvOz8\r
+ CifBrZzNQGUN23sdAx9h94jzkDxUz5N8pAVrb4tKajhTGX7yR4dM/9mHGQGcRDdv48vY\r
+ xjiRb9t4tDM+P5OArdJFEWnko9kaeoUVCeXaqZJ4BCibe7LEnU4hHMZtvU65qsh2FpGO\r
+ RPcg==\r
+X-Gm-Message-State:\r
+ ALoCoQmkdY8vuX2kaXvbnJVtNC/w/1HSklbfImyg9/RRvPnS3oT7FvNL4QjlTx295ce319ZqcEwf\r
+X-Received: by 10.180.74.52 with SMTP id q20mr18151766wiv.94.1441309226937;\r
+ Thu, 03 Sep 2015 12:40:26 -0700 (PDT)\r
+Received: from localhost (mobile-access-bcee4f-131.dhcp.inet.fi.\r
+ [188.238.79.131])\r
+ by smtp.gmail.com with ESMTPSA id i7sm625597wib.15.2015.09.03.12.40.26\r
+ (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+ Thu, 03 Sep 2015 12:40:26 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 7/9] cli: add support for deduplicating based on case\r
+ insensitive address\r
+Date: Thu, 3 Sep 2015 22:40:03 +0300\r
+Message-Id:\r
+ <e711ba0bd92624352e2366f4f866a406ff25981e.1441308761.git.jani@nikula.org>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <cover.1441308761.git.jani@nikula.org>\r
+References: <cover.1441308761.git.jani@nikula.org>\r
+In-Reply-To: <cover.1441308761.git.jani@nikula.org>\r
+References: <cover.1441308761.git.jani@nikula.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.18\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, 03 Sep 2015 19:40:36 -0000\r
+\r
+Consider all variants of an email address as one, and print the most\r
+common variant.\r
+---\r
+ notmuch-search.c | 48 ++++++++++++++++++++++++++++++++++++++++++------\r
+ 1 file changed, 42 insertions(+), 6 deletions(-)\r
+\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 7c51d5df6bd4..deb9e58a747c 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -39,6 +39,7 @@ typedef enum {\r
+ typedef enum {\r
+ DEDUP_NONE,\r
+ DEDUP_MAILBOX,\r
++ DEDUP_ADDRESS,\r
+ } dedup_t;\r
+ \r
+ typedef enum {\r
+@@ -352,7 +353,7 @@ print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)\r
+ name_addr = internet_address_to_string (ia, FALSE);\r
+ \r
+ if (format->is_text_printer) {\r
+- if (count > 0) {\r
++ if (ctx->output & OUTPUT_COUNT) {\r
+ format->integer (format, count);\r
+ format->string (format, "\t");\r
+ }\r
+@@ -366,7 +367,7 @@ print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)\r
+ format->string (format, addr);\r
+ format->map_key (format, "name-addr");\r
+ format->string (format, name_addr);\r
+- if (count > 0) {\r
++ if (ctx->output & OUTPUT_COUNT) {\r
+ format->map_key (format, "count");\r
+ format->integer (format, count);\r
+ }\r
+@@ -403,7 +404,6 @@ process_address_list (const search_context_t *ctx,\r
+ mailbox_t mbx = {\r
+ .name = internet_address_get_name (address),\r
+ .addr = internet_address_mailbox_get_addr (mailbox),\r
+- .count = 0,\r
+ };\r
+ \r
+ /* OUTPUT_COUNT only works with deduplication */\r
+@@ -411,7 +411,8 @@ process_address_list (const search_context_t *ctx,\r
+ is_duplicate (ctx, mbx.name, mbx.addr))\r
+ continue;\r
+ \r
+- if (ctx->output & OUTPUT_COUNT)\r
++ /* OUTPUT_COUNT and DEDUP_ADDRESS require a full pass. */\r
++ if (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS)\r
+ continue;\r
+ \r
+ print_mailbox (ctx, &mbx);\r
+@@ -450,6 +451,34 @@ _list_free_for_g_hash (void *ptr)\r
+ g_list_free_full (ptr, _talloc_free_for_g_hash);\r
+ }\r
+ \r
++/* Print the most common variant of a list of unique mailboxes, and\r
++ * conflate the counts. */\r
++static void\r
++print_popular (const search_context_t *ctx, GList *list)\r
++{\r
++ GList *l;\r
++ mailbox_t *mailbox = NULL, *m;\r
++ int max = 0;\r
++ int total = 0;\r
++\r
++ for (l = list; l; l = l->next) {\r
++ m = l->data;\r
++ total += m->count;\r
++ if (m->count > max) {\r
++ mailbox = m;\r
++ max = m->count;\r
++ }\r
++ }\r
++\r
++ if (! mailbox)\r
++ INTERNAL_ERROR("Empty list in address hash table\n");\r
++\r
++ /* The original count is no longer needed, so overwrite. */\r
++ mailbox->count = total;\r
++\r
++ print_mailbox (ctx, mailbox);\r
++}\r
++\r
+ static void\r
+ print_list_value (void *mailbox, void *context)\r
+ {\r
+@@ -459,7 +488,12 @@ print_list_value (void *mailbox, void *context)\r
+ static void\r
+ print_hash_value (unused (void *key), void *list, void *context)\r
+ {\r
+- g_list_foreach (list, print_list_value, context);\r
++ const search_context_t *ctx = context;\r
++\r
++ if (ctx->dedup == DEDUP_ADDRESS)\r
++ print_popular (ctx, list);\r
++ else\r
++ g_list_foreach (list, print_list_value, context);\r
+ }\r
+ \r
+ static int\r
+@@ -557,7 +591,8 @@ do_search_messages (search_context_t *ctx)\r
+ notmuch_message_destroy (message);\r
+ }\r
+ \r
+- if (ctx->addresses && ctx->output & OUTPUT_COUNT)\r
++ if (ctx->addresses &&\r
++ (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS))\r
+ g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);\r
+ \r
+ notmuch_messages_destroy (messages);\r
+@@ -821,6 +856,7 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])\r
+ { NOTMUCH_OPT_KEYWORD, &ctx->dedup, "deduplicate", 'D',\r
+ (notmuch_keyword_t []){ { "no", DEDUP_NONE },\r
+ { "mailbox", DEDUP_MAILBOX },\r
++ { "address", DEDUP_ADDRESS },\r
+ { 0, 0 } } },\r
+ { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },\r
+ { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },\r
+-- \r
+2.1.4\r
+\r