--- /dev/null
+Return-Path: <sojkam1@fel.cvut.cz>\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 483BB431FAF\r
+ for <notmuch@notmuchmail.org>; Mon, 3 Nov 2014 15:51:13 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] 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 hXR4mywG6ity for <notmuch@notmuchmail.org>;\r
+ Mon, 3 Nov 2014 15:51:05 -0800 (PST)\r
+Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
+ by olra.theworths.org (Postfix) with ESMTP id 45158429E31\r
+ for <notmuch@notmuchmail.org>; Mon, 3 Nov 2014 15:50:51 -0800 (PST)\r
+Received: from localhost (unknown [192.168.200.7])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id BDA6D5CD006\r
+ for <notmuch@notmuchmail.org>; Tue, 4 Nov 2014 00:50:50 +0100 (CET)\r
+X-Virus-Scanned: IMAP STYX AMAVIS\r
+Received: from max.feld.cvut.cz ([192.168.200.1])\r
+ by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new,\r
+ port 10044) with ESMTP id gpMrCRIUrYiv for <notmuch@notmuchmail.org>;\r
+ Tue, 4 Nov 2014 00:50:47 +0100 (CET)\r
+Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id EAF265CD00E\r
+ for <notmuch@notmuchmail.org>; Tue, 4 Nov 2014 00:50:40 +0100 (CET)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
+ (envelope-from <sojkam1@fel.cvut.cz>)\r
+ id 1XlROM-0005Wx-8Y; Tue, 04 Nov 2014 00:50:34 +0100\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 09/10] cli: address: Add --output=count\r
+Date: Tue, 4 Nov 2014 00:50:21 +0100\r
+Message-Id: <1415058622-21162-10-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.1.1\r
+In-Reply-To: <1415058622-21162-1-git-send-email-sojkam1@fel.cvut.cz>\r
+References: <1415058622-21162-1-git-send-email-sojkam1@fel.cvut.cz>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\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: Mon, 03 Nov 2014 23:51:13 -0000\r
+\r
+This output prints how many times was each address encountered during\r
+search.\r
+---\r
+ completion/notmuch-completion.bash | 2 +-\r
+ completion/notmuch-completion.zsh | 2 +-\r
+ doc/man1/notmuch-address.rst | 7 ++++++\r
+ notmuch-search.c | 49 ++++++++++++++++++++++++++++++++------\r
+ test/T095-address.sh | 48 +++++++++++++++++++++++++++++++++++++\r
+ 5 files changed, 99 insertions(+), 9 deletions(-)\r
+\r
+diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash\r
+index 94ea2d5..db152f3 100644\r
+--- a/completion/notmuch-completion.bash\r
++++ b/completion/notmuch-completion.bash\r
+@@ -332,7 +332,7 @@ _notmuch_address()\r
+ return\r
+ ;;\r
+ --output)\r
+- COMPREPLY=( $( compgen -W "sender recipients" -- "${cur}" ) )\r
++ COMPREPLY=( $( compgen -W "sender recipients count" -- "${cur}" ) )\r
+ return\r
+ ;;\r
+ --sort)\r
+diff --git a/completion/notmuch-completion.zsh b/completion/notmuch-completion.zsh\r
+index c606b75..8968562 100644\r
+--- a/completion/notmuch-completion.zsh\r
++++ b/completion/notmuch-completion.zsh\r
+@@ -61,7 +61,7 @@ _notmuch_address()\r
+ {\r
+ _arguments -s : \\r
+ '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \\r
+- '--output=[select what to output]:output:((sender recipients))'\r
++ '--output=[select what to output]:output:((sender recipients count))'\r
+ }\r
+ \r
+ _notmuch()\r
+diff --git a/doc/man1/notmuch-address.rst b/doc/man1/notmuch-address.rst\r
+index 96512b7..18473a7 100644\r
+--- a/doc/man1/notmuch-address.rst\r
++++ b/doc/man1/notmuch-address.rst\r
+@@ -48,6 +48,13 @@ Supported options for **address** include\r
+ Output all addresses from the *To*, *Cc* and *Bcc*\r
+ headers.\r
+ \r
++ **count**\r
++ Print the count of how many times was the address\r
++ encountered during search.\r
++\r
++ Note: With this option, addresses are printed only after\r
++ the whole search is finished. This may take long time.\r
++\r
+ ``--sort=``\ (**newest-first**\ \|\ **oldest-first**)\r
+ This option can be used to present results in either\r
+ chronological order (**oldest-first**) or reverse chronological\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 741702a..d99e530 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -33,6 +33,7 @@ typedef enum {\r
+ /* Address command */\r
+ OUTPUT_SENDER = 1 << 5,\r
+ OUTPUT_RECIPIENTS = 1 << 6,\r
++ OUTPUT_COUNT = 1 << 7,\r
+ } output_t;\r
+ \r
+ typedef enum {\r
+@@ -59,6 +60,7 @@ typedef struct {\r
+ typedef struct {\r
+ const char *name;\r
+ const char *addr;\r
++ int count;\r
+ } mailbox_t;\r
+ \r
+ /* Return two stable query strings that identify exactly the matched\r
+@@ -247,17 +249,24 @@ is_duplicate (const search_context_t *ctx, const char *name, const char *addr)\r
+ {\r
+ notmuch_bool_t duplicate;\r
+ char *key;\r
++ mailbox_t *mailbox;\r
+ \r
+ key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);\r
+ if (! key)\r
+ return FALSE;\r
+ \r
+- duplicate = g_hash_table_lookup_extended (ctx->addresses, key, NULL, NULL);\r
++ duplicate = g_hash_table_lookup_extended (ctx->addresses, key, NULL, (gpointer)&mailbox);\r
+ \r
+- if (! duplicate)\r
+- g_hash_table_insert (ctx->addresses, key, NULL);\r
+- else\r
++ if (! duplicate) {\r
++ mailbox = talloc (ctx->format, mailbox_t);\r
++ mailbox->name = talloc_strdup (mailbox, name);\r
++ mailbox->addr = talloc_strdup (mailbox, addr);\r
++ mailbox->count = 1;\r
++ g_hash_table_insert (ctx->addresses, key, mailbox);\r
++ } else {\r
++ mailbox->count++;\r
+ talloc_free (key);\r
++ }\r
+ \r
+ return duplicate;\r
+ }\r
+@@ -267,6 +276,7 @@ print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)\r
+ {\r
+ const char *name = mailbox->name;\r
+ const char *addr = mailbox->addr;\r
++ int count = mailbox->count;\r
+ sprinter_t *format = ctx->format;\r
+ InternetAddress *ia = internet_address_mailbox_new (name, addr);\r
+ char *name_addr;\r
+@@ -276,6 +286,10 @@ 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
++ format->integer (format, count);\r
++ format->string (format, "\t");\r
++ }\r
+ format->string (format, name_addr);\r
+ format->separator (format);\r
+ } else {\r
+@@ -286,6 +300,10 @@ 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
++ format->map_key (format, "count");\r
++ format->integer (format, count);\r
++ }\r
+ format->end (format);\r
+ format->separator (format);\r
+ }\r
+@@ -294,7 +312,7 @@ print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)\r
+ g_free (name_addr);\r
+ }\r
+ \r
+-/* Print addresses from InternetAddressList. */\r
++/* Print or prepare for printing addresses from InternetAddressList. */\r
+ static void\r
+ process_address_list (const search_context_t *ctx,\r
+ InternetAddressList *list)\r
+@@ -319,17 +337,21 @@ 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
+ if (is_duplicate (ctx, mbx.name, mbx.addr))\r
+ continue;\r
+ \r
++ if (ctx->output & OUTPUT_COUNT)\r
++ continue;\r
++\r
+ print_mailbox (ctx, &mbx);\r
+ }\r
+ }\r
+ }\r
+ \r
+-/* Print addresses from a message header. */\r
++/* Print or prepare for printing addresses from a message header. */\r
+ static void\r
+ process_address_header (const search_context_t *ctx, const char *value)\r
+ {\r
+@@ -353,6 +375,15 @@ _my_talloc_free_for_g_hash (void *ptr)\r
+ talloc_free (ptr);\r
+ }\r
+ \r
++static void\r
++print_hash_value (unused (gpointer key), gpointer value, gpointer user_data)\r
++{\r
++ const mailbox_t *mailbox = value;\r
++ search_context_t *ctx = user_data;\r
++\r
++ print_mailbox (ctx, mailbox);\r
++}\r
++\r
+ static int\r
+ _count_filenames (notmuch_message_t *message)\r
+ {\r
+@@ -448,6 +479,9 @@ do_search_messages (search_context_t *ctx)\r
+ notmuch_message_destroy (message);\r
+ }\r
+ \r
++ if (ctx->addresses && ctx->output & OUTPUT_COUNT)\r
++ g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);\r
++\r
+ notmuch_messages_destroy (messages);\r
+ \r
+ format->end (format);\r
+@@ -685,6 +719,7 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])\r
+ { NOTMUCH_OPT_KEYWORD_FLAGS, &ctx->output, "output", 'o',\r
+ (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },\r
+ { "recipients", OUTPUT_RECIPIENTS },\r
++ { "count", OUTPUT_COUNT },\r
+ { 0, 0 } } },\r
+ { NOTMUCH_OPT_INHERIT, &common_options, NULL, 0, 0 },\r
+ { 0, 0, 0, 0, 0 }\r
+@@ -702,7 +737,7 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])\r
+ return EXIT_FAILURE;\r
+ \r
+ ctx->addresses = g_hash_table_new_full (g_str_hash, g_str_equal,\r
+- _my_talloc_free_for_g_hash, NULL);\r
++ _my_talloc_free_for_g_hash, _my_talloc_free_for_g_hash);\r
+ \r
+ ret = do_search_messages (ctx);\r
+ \r
+diff --git a/test/T095-address.sh b/test/T095-address.sh\r
+index 8a256d2..92e17b0 100755\r
+--- a/test/T095-address.sh\r
++++ b/test/T095-address.sh\r
+@@ -96,5 +96,53 @@ notmuch address --output=sender --output=recipients '*' >OUTPUT\r
+ # Use EXPECTED from previous subtest\r
+ test_expect_equal_file OUTPUT EXPECTED\r
+ \r
++test_begin_subtest "--output=sender --output=count"\r
++notmuch address --output=sender --output=count '*' | sort -n >OUTPUT\r
++cat <<EOF >EXPECTED\r
++1 Adrian Perez de Castro <aperez@igalia.com>\r
++1 Aron Griffis <agriffis@n01se.net>\r
++1 Chris Wilson <chris@chris-wilson.co.uk>\r
++1 François Boulogne <boulogne.f@gmail.com>\r
++1 Ingmar Vanhassel <ingmar@exherbo.org>\r
++1 Israel Herraiz <isra@herraiz.org>\r
++1 Olivier Berger <olivier.berger@it-sudparis.eu>\r
++1 Rolland Santimano <rollandsantimano@yahoo.com>\r
++2 Alex Botero-Lowry <alex.boterolowry@gmail.com>\r
++2 Jjgod Jiang <gzjjgod@gmail.com>\r
++3 Stewart Smith <stewart@flamingspork.com>\r
++4 Alexander Botero-Lowry <alex.boterolowry@gmail.com>\r
++4 Jan Janak <jan@ryngle.com>\r
++5 Lars Kellogg-Stedman <lars@seas.harvard.edu>\r
++5 Mikhail Gusarov <dottedmag@dottedmag.net>\r
++7 Keith Packard <keithp@keithp.com>\r
++12 Carl Worth <cworth@cworth.org>\r
++EOF\r
++test_expect_equal_file OUTPUT EXPECTED\r
++\r
++test_begin_subtest "--output=sender --output=count --format=json"\r
++# Since the iteration order of GHashTable is not specified, we\r
++# preprocess and sort the results to keep the order stable here.\r
++notmuch address --output=sender --output=count --format=json '*' | \\r
++ sed -e 's/^\[//' -e 's/]$//' -e 's/,$//' | sort >OUTPUT\r
++cat <<EOF >EXPECTED\r
++{"name": "Adrian Perez de Castro", "address": "aperez@igalia.com", "name-addr": "Adrian Perez de Castro <aperez@igalia.com>", "count": 1}\r
++{"name": "Alex Botero-Lowry", "address": "alex.boterolowry@gmail.com", "name-addr": "Alex Botero-Lowry <alex.boterolowry@gmail.com>", "count": 2}\r
++{"name": "Alexander Botero-Lowry", "address": "alex.boterolowry@gmail.com", "name-addr": "Alexander Botero-Lowry <alex.boterolowry@gmail.com>", "count": 4}\r
++{"name": "Aron Griffis", "address": "agriffis@n01se.net", "name-addr": "Aron Griffis <agriffis@n01se.net>", "count": 1}\r
++{"name": "Carl Worth", "address": "cworth@cworth.org", "name-addr": "Carl Worth <cworth@cworth.org>", "count": 12}\r
++{"name": "Chris Wilson", "address": "chris@chris-wilson.co.uk", "name-addr": "Chris Wilson <chris@chris-wilson.co.uk>", "count": 1}\r
++{"name": "François Boulogne", "address": "boulogne.f@gmail.com", "name-addr": "François Boulogne <boulogne.f@gmail.com>", "count": 1}\r
++{"name": "Ingmar Vanhassel", "address": "ingmar@exherbo.org", "name-addr": "Ingmar Vanhassel <ingmar@exherbo.org>", "count": 1}\r
++{"name": "Israel Herraiz", "address": "isra@herraiz.org", "name-addr": "Israel Herraiz <isra@herraiz.org>", "count": 1}\r
++{"name": "Jan Janak", "address": "jan@ryngle.com", "name-addr": "Jan Janak <jan@ryngle.com>", "count": 4}\r
++{"name": "Jjgod Jiang", "address": "gzjjgod@gmail.com", "name-addr": "Jjgod Jiang <gzjjgod@gmail.com>", "count": 2}\r
++{"name": "Keith Packard", "address": "keithp@keithp.com", "name-addr": "Keith Packard <keithp@keithp.com>", "count": 7}\r
++{"name": "Lars Kellogg-Stedman", "address": "lars@seas.harvard.edu", "name-addr": "Lars Kellogg-Stedman <lars@seas.harvard.edu>", "count": 5}\r
++{"name": "Mikhail Gusarov", "address": "dottedmag@dottedmag.net", "name-addr": "Mikhail Gusarov <dottedmag@dottedmag.net>", "count": 5}\r
++{"name": "Olivier Berger", "address": "olivier.berger@it-sudparis.eu", "name-addr": "Olivier Berger <olivier.berger@it-sudparis.eu>", "count": 1}\r
++{"name": "Rolland Santimano", "address": "rollandsantimano@yahoo.com", "name-addr": "Rolland Santimano <rollandsantimano@yahoo.com>", "count": 1}\r
++{"name": "Stewart Smith", "address": "stewart@flamingspork.com", "name-addr": "Stewart Smith <stewart@flamingspork.com>", "count": 3}\r
++EOF\r
++test_expect_equal_file OUTPUT EXPECTED\r
+ \r
+ test_done\r
+-- \r
+2.1.1\r
+\r