--- /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 E6F8A429E2E\r
+ for <notmuch@notmuchmail.org>; Mon, 27 Oct 2014 07:51:27 -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 aIsqRaU+i1tm for <notmuch@notmuchmail.org>;\r
+ Mon, 27 Oct 2014 07:51:20 -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 201B0431FDD\r
+ for <notmuch@notmuchmail.org>; Mon, 27 Oct 2014 07:51:16 -0700 (PDT)\r
+Received: from localhost (unknown [192.168.200.7])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id 68D1C5CCF92\r
+ for <notmuch@notmuchmail.org>; Mon, 27 Oct 2014 15:51:15 +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 O_3Xs0EmMl-a for <notmuch@notmuchmail.org>;\r
+ Mon, 27 Oct 2014 15:51:12 +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 9E1545CCF99\r
+ for <notmuch@notmuchmail.org>; Mon, 27 Oct 2014 15:51:08 +0100 (CET)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
+ (envelope-from <sojkam1@fel.cvut.cz>)\r
+ id 1XildT-0000ox-KU; Mon, 27 Oct 2014 15:51:07 +0100\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v4 6/6] cli: search: Add --output=count\r
+Date: Mon, 27 Oct 2014 15:50:55 +0100\r
+Message-Id: <1414421455-3037-7-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.1.1\r
+In-Reply-To: <1414421455-3037-1-git-send-email-sojkam1@fel.cvut.cz>\r
+References: <1414421455-3037-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, 27 Oct 2014 14:51:28 -0000\r
+\r
+This output can be used with --output=recipients or --output=sender and\r
+in addition to the mailboxes, it prints how many times was each mailbox\r
+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 | 47 ++++++++++++++++++++++++++++++-----\r
+ test/T090-search-output.sh | 50 ++++++++++++++++++++++++++++++++++++++\r
+ 5 files changed, 100 insertions(+), 10 deletions(-)\r
+\r
+diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash\r
+index 6b6d43a..b625b02 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 3e535df..c1ccc32 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
+ '--filter-by=[filter out duplicate addresses]:filter-by:((nameaddr\:"both name and address part" name\:"name part" addr\:"address part" addrfold\:"case-insensitive address part" nameaddrfold\:"name and case-insensitive address part"))'\r
+ }\r
+ \r
+diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst\r
+index 84af2da..4b408f6 100644\r
+--- a/doc/man1/notmuch-search.rst\r
++++ b/doc/man1/notmuch-search.rst\r
+@@ -97,9 +97,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 47aa979..41f4107 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 enum {\r
+ FILTER_BY_NAMEADDR = 0,\r
+@@ -56,6 +57,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
+@@ -244,6 +246,7 @@ check_duplicite (const search_options_t *opt, GHashTable *addrs, const char *nam\r
+ {\r
+ notmuch_bool_t duplicite;\r
+ char *key;\r
++ mailbox_t *mailbox;\r
+ \r
+ if (opt->filter_by == FILTER_BY_ADDRFOLD ||\r
+ opt->filter_by == FILTER_BY_NAMEADDRFOLD) {\r
+@@ -274,12 +277,18 @@ check_duplicite (const search_options_t *opt, GHashTable *addrs, const char *nam\r
+ if (! key)\r
+ return FALSE;\r
+ \r
+- duplicite = g_hash_table_lookup_extended (addrs, key, NULL, NULL);\r
++ duplicite = g_hash_table_lookup_extended (addrs, key, NULL, (gpointer)&mailbox);\r
+ \r
+- if (! duplicite)\r
+- g_hash_table_insert (addrs, key, NULL);\r
+- else\r
++ if (! duplicite) {\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 duplicite;\r
+ }\r
+@@ -289,6 +298,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
+ \r
+ if (opt->format->is_text_printer) {\r
+ char *mailbox_str;\r
+@@ -302,6 +312,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
++ opt->format->integer (opt->format, count);\r
++ opt->format->string (opt->format, "\t");\r
++ }\r
+ opt->format->string (opt->format, mailbox_str);\r
+ opt->format->separator (opt->format);\r
+ \r
+@@ -312,6 +326,10 @@ print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)\r
+ opt->format->string (opt->format, name);\r
+ opt->format->map_key (opt->format, "address");\r
+ opt->format->string (opt->format, addr);\r
++ if (count > 0) {\r
++ opt->format->map_key (opt->format, "count");\r
++ opt->format->integer (opt->format, count);\r
++ }\r
+ opt->format->end (opt->format);\r
+ opt->format->separator (opt->format);\r
+ }\r
+@@ -341,11 +359,15 @@ 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 (check_duplicite (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
+@@ -372,6 +394,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
+@@ -384,7 +415,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
+@@ -451,6 +482,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
+@@ -554,6 +588,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