--- /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 2E97C429E42\r
+ for <notmuch@notmuchmail.org>; Thu, 30 Oct 2014 17:00:06 -0700 (PDT)\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 yVywUpvPJlzq for <notmuch@notmuchmail.org>;\r
+ Thu, 30 Oct 2014 16:59:57 -0700 (PDT)\r
+Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
+ by olra.theworths.org (Postfix) with ESMTP id 27AA8431FDF\r
+ for <notmuch@notmuchmail.org>; Thu, 30 Oct 2014 16:59:48 -0700 (PDT)\r
+Received: from localhost (unknown [192.168.200.7])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id 7F3335CCFF4\r
+ for <notmuch@notmuchmail.org>; Fri, 31 Oct 2014 00:59:47 +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 oIR5Spa-B4u7 for <notmuch@notmuchmail.org>;\r
+ Fri, 31 Oct 2014 00:59:44 +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 EE9D75CCFFB\r
+ for <notmuch@notmuchmail.org>; Fri, 31 Oct 2014 00:59: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 1Xjzcs-0005bb-Se; Fri, 31 Oct 2014 00:59:34 +0100\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v5 6/7] cli: search: Add --output=count\r
+Date: Fri, 31 Oct 2014 00:59:32 +0100\r
+Message-Id: <1414713573-21461-7-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.1.1\r
+In-Reply-To: <1414713573-21461-1-git-send-email-sojkam1@fel.cvut.cz>\r
+References: <1414713573-21461-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: Fri, 31 Oct 2014 00:00:06 -0000\r
+\r
+This output can be used with --output=recipients or --output=sender\r
+and in addition to the addresses, it prints how many times was each\r
+address encountered during search.\r
+---\r
+ completion/notmuch-completion.bash | 2 +-\r
+ completion/notmuch-completion.zsh | 2 +-\r
+ doc/man1/notmuch-search.rst | 9 +++++--\r
+ notmuch-search.c | 51 ++++++++++++++++++++++++++++++++------\r
+ test/T090-search-output.sh | 50 +++++++++++++++++++++++++++++++++++++\r
+ 5 files changed, 102 insertions(+), 12 deletions(-)\r
+\r
+diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash\r
+index cfbd389..39cd829 100644\r
+--- a/completion/notmuch-completion.bash\r
++++ b/completion/notmuch-completion.bash\r
+@@ -294,7 +294,7 @@ _notmuch_search()\r
+ return\r
+ ;;\r
+ --output)\r
+- COMPREPLY=( $( compgen -W "summary threads messages files tags sender recipients" -- "${cur}" ) )\r
++ COMPREPLY=( $( compgen -W "summary threads messages files tags sender recipients count" -- "${cur}" ) )\r
+ return\r
+ ;;\r
+ --sort)\r
+diff --git a/completion/notmuch-completion.zsh b/completion/notmuch-completion.zsh\r
+index 3e52a00..d7e5a5e 100644\r
+--- a/completion/notmuch-completion.zsh\r
++++ b/completion/notmuch-completion.zsh\r
+@@ -53,7 +53,7 @@ _notmuch_search()\r
+ '--max-threads=[display only the first x threads from the search results]:number of threads to show: ' \\r
+ '--first=[omit the first x threads from the search results]:number of threads to omit: ' \\r
+ '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \\r
+- '--output=[select what to output]:output:((summary threads messages files tags sender recipients))'\r
++ '--output=[select what to output]:output:((summary threads messages files tags sender recipients count))'\r
+ }\r
+ \r
+ _notmuch()\r
+diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst\r
+index 42f17e4..ec89200 100644\r
+--- a/doc/man1/notmuch-search.rst\r
++++ b/doc/man1/notmuch-search.rst\r
+@@ -96,9 +96,14 @@ Supported options for **search** include\r
+ Like **sender** but for addresses from *To*, *Cc* and\r
+ *Bcc* headers.\r
+ \r
++ **count**\r
++ Can be used in combination with **sender** or\r
++ **recipients** to print the count of how many times was\r
++ the address encountered during search.\r
++\r
+ This option can be given multiple times to combine different\r
+- outputs. Currently, this is only supported for **sender** and\r
+- **recipients** outputs.\r
++ outputs. Currently, this is only supported for **sender**,\r
++ **recipients** and **count** outputs.\r
+ \r
+ ``--sort=``\ (**newest-first**\ \|\ **oldest-first**)\r
+ This option can be used to present results in either\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index eae749a..15527c4 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -30,9 +30,10 @@ typedef enum {\r
+ OUTPUT_TAGS = 1 << 4,\r
+ OUTPUT_SENDER = 1 << 5,\r
+ OUTPUT_RECIPIENTS = 1 << 6,\r
++ OUTPUT_COUNT = 1 << 7,\r
+ } output_t;\r
+ \r
+-#define OUTPUT_ADDRESS_FLAGS (OUTPUT_SENDER | OUTPUT_RECIPIENTS)\r
++#define OUTPUT_ADDRESS_FLAGS (OUTPUT_SENDER | OUTPUT_RECIPIENTS | OUTPUT_COUNT)\r
+ \r
+ typedef struct {\r
+ sprinter_t *format;\r
+@@ -47,6 +48,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
+@@ -235,17 +237,24 @@ is_duplicate (const search_options_t *opt, GHashTable *addrs, const char *name,\r
+ {\r
+ notmuch_bool_t duplicate;\r
+ char *key;\r
++ mailbox_t *mailbox;\r
+ \r
+ key = talloc_asprintf (opt->format, "%s <%s>", name, addr);\r
+ if (! key)\r
+ return FALSE;\r
+ \r
+- duplicate = g_hash_table_lookup_extended (addrs, key, NULL, NULL);\r
++ duplicate = g_hash_table_lookup_extended (addrs, key, NULL, (gpointer)&mailbox);\r
+ \r
+- if (! duplicate)\r
+- g_hash_table_insert (addrs, key, NULL);\r
+- else\r
++ if (! duplicate) {\r
++ mailbox = talloc (opt->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 (addrs, key, mailbox);\r
++ } else {\r
++ mailbox->count++;\r
+ talloc_free (key);\r
++ }\r
+ \r
+ return duplicate;\r
+ }\r
+@@ -255,6 +264,7 @@ print_mailbox (const search_options_t *opt, 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 = opt->format;\r
+ \r
+ if (format->is_text_printer) {\r
+@@ -269,6 +279,10 @@ print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)\r
+ fprintf (stderr, "Error: out of memory\n");\r
+ return;\r
+ }\r
++ if (count > 0) {\r
++ format->integer (format, count);\r
++ format->string (format, "\t");\r
++ }\r
+ format->string (format, mailbox_str);\r
+ format->separator (format);\r
+ \r
+@@ -279,12 +293,16 @@ print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)\r
+ format->string (format, name);\r
+ format->map_key (format, "address");\r
+ format->string (format, 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
+ }\r
+ \r
+-/* Print addresses from InternetAddressList. */\r
++/* Print or prepare for printing addresses from InternetAddressList. */\r
+ static void\r
+ process_address_list (const search_options_t *opt, GHashTable *addrs,\r
+ InternetAddressList *list)\r
+@@ -309,17 +327,21 @@ process_address_list (const search_options_t *opt, GHashTable *addrs,\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 (opt, addrs, mbx.name, mbx.addr))\r
+ continue;\r
+ \r
++ if (opt->output & OUTPUT_COUNT)\r
++ continue;\r
++\r
+ print_mailbox (opt, &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_options_t *opt, GHashTable *addrs, const char *value)\r
+ {\r
+@@ -341,6 +363,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_options_t *opt = user_data;\r
++\r
++ print_mailbox (opt, mailbox);\r
++}\r
++\r
+ static int\r
+ do_search_messages (search_options_t *opt)\r
+ {\r
+@@ -353,7 +384,7 @@ do_search_messages (search_options_t *opt)\r
+ \r
+ if (opt->output & OUTPUT_ADDRESS_FLAGS)\r
+ 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
+ if (opt->offset < 0) {\r
+ opt->offset += notmuch_query_count_messages (opt->query);\r
+@@ -420,6 +451,9 @@ do_search_messages (search_options_t *opt)\r
+ notmuch_message_destroy (message);\r
+ }\r
+ \r
++ if (addresses && opt->output & OUTPUT_COUNT)\r
++ g_hash_table_foreach (addresses, print_hash_value, opt);\r
++\r
+ if (addresses)\r
+ g_hash_table_unref (addresses);\r
+ \r
+@@ -522,6 +556,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ { "recipients", OUTPUT_RECIPIENTS },\r
+ { "files", OUTPUT_FILES },\r
+ { "tags", OUTPUT_TAGS },\r
++ { "count", OUTPUT_COUNT },\r
+ { 0, 0 } } },\r
+ { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',\r
+ (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },\r
+diff --git a/test/T090-search-output.sh b/test/T090-search-output.sh\r
+index 841a721..5a9bbc9 100755\r
+--- a/test/T090-search-output.sh\r
++++ b/test/T090-search-output.sh\r
+@@ -433,6 +433,56 @@ cat <<EOF >EXPECTED\r
+ EOF\r
+ test_expect_equal_file OUTPUT EXPECTED\r
+ \r
++test_begin_subtest "--output=sender --output=count"\r
++notmuch search --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 search --output=sender --output=count --format=json '*' | \\r
++ sed -e 's/^\[//' -e 's/]$//' -e 's/,$//' | \\r
++ sort --field-separator=":" --key=4n --key=2 >OUTPUT\r
++cat <<EOF >EXPECTED\r
++{"name": "Adrian Perez de Castro", "address": "aperez@igalia.com", "count": 1}\r
++{"name": "Aron Griffis", "address": "agriffis@n01se.net", "count": 1}\r
++{"name": "Chris Wilson", "address": "chris@chris-wilson.co.uk", "count": 1}\r
++{"name": "François Boulogne", "address": "boulogne.f@gmail.com", "count": 1}\r
++{"name": "Ingmar Vanhassel", "address": "ingmar@exherbo.org", "count": 1}\r
++{"name": "Israel Herraiz", "address": "isra@herraiz.org", "count": 1}\r
++{"name": "Olivier Berger", "address": "olivier.berger@it-sudparis.eu", "count": 1}\r
++{"name": "Rolland Santimano", "address": "rollandsantimano@yahoo.com", "count": 1}\r
++{"name": "Alex Botero-Lowry", "address": "alex.boterolowry@gmail.com", "count": 2}\r
++{"name": "Jjgod Jiang", "address": "gzjjgod@gmail.com", "count": 2}\r
++{"name": "Stewart Smith", "address": "stewart@flamingspork.com", "count": 3}\r
++{"name": "Alexander Botero-Lowry", "address": "alex.boterolowry@gmail.com", "count": 4}\r
++{"name": "Jan Janak", "address": "jan@ryngle.com", "count": 4}\r
++{"name": "Lars Kellogg-Stedman", "address": "lars@seas.harvard.edu", "count": 5}\r
++{"name": "Mikhail Gusarov", "address": "dottedmag@dottedmag.net", "count": 5}\r
++{"name": "Keith Packard", "address": "keithp@keithp.com", "count": 7}\r
++{"name": "Carl Worth", "address": "cworth@cworth.org", "count": 12}\r
++EOF\r
++test_expect_equal_file OUTPUT EXPECTED\r
++\r
+ test_begin_subtest "--output=recipients"\r
+ notmuch search --output=recipients '*' >OUTPUT\r
+ cat <<EOF >EXPECTED\r
+-- \r
+2.1.1\r
+\r