--- /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 B8D06431FDF\r
+ for <notmuch@notmuchmail.org>; Thu, 30 Oct 2014 17:00:09 -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 gmDUghCcoGL7 for <notmuch@notmuchmail.org>;\r
+ Thu, 30 Oct 2014 17:00:01 -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 4B12A431E62\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 AAAD05CCFF5\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 8XfKai1M2e0x 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 C524D5CCFFA\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-0005bW-S3; 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 5/7] cli: search: Do not output duplicate addresses\r
+Date: Fri, 31 Oct 2014 00:59:31 +0100\r
+Message-Id: <1414713573-21461-6-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:10 -0000\r
+\r
+This filters out duplicate addresses from address outputs (sender,\r
+receivers).\r
+\r
+It also also adds tests for the new outputs.\r
+\r
+The code here is an extended version of a patch from Jani Nikula.\r
+---\r
+ doc/man1/notmuch-search.rst | 2 ++\r
+ notmuch-search.c | 51 ++++++++++++++++++++++----\r
+ test/T090-search-output.sh | 87 +++++++++++++++++++++++++++++++++++++++++++++\r
+ 3 files changed, 134 insertions(+), 6 deletions(-)\r
+\r
+diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst\r
+index b6607c9..42f17e4 100644\r
+--- a/doc/man1/notmuch-search.rst\r
++++ b/doc/man1/notmuch-search.rst\r
+@@ -85,6 +85,8 @@ Supported options for **search** include\r
+ (--format=text0), as a JSON array (--format=json), or as\r
+ an S-Expression list (--format=sexp).\r
+ \r
++ Duplicate addresses are filtered out.\r
++\r
+ Note: Searching for **sender** should be much faster than\r
+ searching for **recipients**, because sender addresses are\r
+ cached directly in the database whereas other addresses\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 31e4221..eae749a 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -229,6 +229,27 @@ do_search_threads (search_options_t *opt)\r
+ return 0;\r
+ }\r
+ \r
++/* Returns TRUE iff name and addr is duplicate. */\r
++static notmuch_bool_t\r
++is_duplicate (const search_options_t *opt, GHashTable *addrs, const char *name, const char *addr)\r
++{\r
++ notmuch_bool_t duplicate;\r
++ char *key;\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
++\r
++ if (! duplicate)\r
++ g_hash_table_insert (addrs, key, NULL);\r
++ else\r
++ talloc_free (key);\r
++\r
++ return duplicate;\r
++}\r
++\r
+ static void\r
+ print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)\r
+ {\r
+@@ -265,7 +286,8 @@ print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)\r
+ \r
+ /* Print addresses from InternetAddressList. */\r
+ static void\r
+-process_address_list (const search_options_t *opt, InternetAddressList *list)\r
++process_address_list (const search_options_t *opt, GHashTable *addrs,\r
++ InternetAddressList *list)\r
+ {\r
+ InternetAddress *address;\r
+ int i;\r
+@@ -281,7 +303,7 @@ process_address_list (const search_options_t *opt, InternetAddressList *list)\r
+ if (group_list == NULL)\r
+ continue;\r
+ \r
+- process_address_list (opt, group_list);\r
++ process_address_list (opt, addrs, group_list);\r
+ } else {\r
+ InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);\r
+ mailbox_t mbx = {\r
+@@ -289,6 +311,9 @@ process_address_list (const search_options_t *opt, InternetAddressList *list)\r
+ .addr = internet_address_mailbox_get_addr (mailbox),\r
+ };\r
+ \r
++ if (is_duplicate (opt, addrs, mbx.name, mbx.addr))\r
++ continue;\r
++\r
+ print_mailbox (opt, &mbx);\r
+ }\r
+ }\r
+@@ -296,7 +321,7 @@ process_address_list (const search_options_t *opt, InternetAddressList *list)\r
+ \r
+ /* Print addresses from a message header. */\r
+ static void\r
+-process_address_header (const search_options_t *opt, const char *value)\r
++process_address_header (const search_options_t *opt, GHashTable *addrs, const char *value)\r
+ {\r
+ InternetAddressList *list;\r
+ \r
+@@ -307,7 +332,13 @@ process_address_header (const search_options_t *opt, const char *value)\r
+ if (list == NULL)\r
+ return;\r
+ \r
+- process_address_list (opt, list);\r
++ process_address_list (opt, addrs, list);\r
++}\r
++\r
++static void\r
++_my_talloc_free_for_g_hash (void *ptr)\r
++{\r
++ talloc_free (ptr);\r
+ }\r
+ \r
+ static int\r
+@@ -317,8 +348,13 @@ do_search_messages (search_options_t *opt)\r
+ notmuch_messages_t *messages;\r
+ notmuch_filenames_t *filenames;\r
+ sprinter_t *format = opt->format;\r
++ GHashTable *addresses = NULL;\r
+ int i;\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
++\r
+ if (opt->offset < 0) {\r
+ opt->offset += notmuch_query_count_messages (opt->query);\r
+ if (opt->offset < 0)\r
+@@ -366,7 +402,7 @@ do_search_messages (search_options_t *opt)\r
+ const char *addrs;\r
+ \r
+ addrs = notmuch_message_get_header (message, "from");\r
+- process_address_header (opt, addrs);\r
++ process_address_header (opt, addresses, addrs);\r
+ }\r
+ \r
+ if (opt->output & OUTPUT_RECIPIENTS) {\r
+@@ -376,7 +412,7 @@ do_search_messages (search_options_t *opt)\r
+ \r
+ for (j = 0; j < ARRAY_SIZE (hdrs); j++) {\r
+ addrs = notmuch_message_get_header (message, hdrs[j]);\r
+- process_address_header (opt, addrs);\r
++ process_address_header (opt, addresses, addrs);\r
+ }\r
+ }\r
+ }\r
+@@ -384,6 +420,9 @@ do_search_messages (search_options_t *opt)\r
+ notmuch_message_destroy (message);\r
+ }\r
+ \r
++ if (addresses)\r
++ g_hash_table_unref (addresses);\r
++\r
+ notmuch_messages_destroy (messages);\r
+ \r
+ format->end (format);\r
+diff --git a/test/T090-search-output.sh b/test/T090-search-output.sh\r
+index 947d572..841a721 100755\r
+--- a/test/T090-search-output.sh\r
++++ b/test/T090-search-output.sh\r
+@@ -387,6 +387,93 @@ cat <<EOF >EXPECTED\r
+ EOF\r
+ test_expect_equal_file OUTPUT EXPECTED\r
+ \r
++test_begin_subtest "--output=sender"\r
++notmuch search --output=sender '*' >OUTPUT\r
++cat <<EOF >EXPECTED\r
++François Boulogne <boulogne.f@gmail.com>\r
++Olivier Berger <olivier.berger@it-sudparis.eu>\r
++Chris Wilson <chris@chris-wilson.co.uk>\r
++Carl Worth <cworth@cworth.org>\r
++Alexander Botero-Lowry <alex.boterolowry@gmail.com>\r
++Keith Packard <keithp@keithp.com>\r
++Jjgod Jiang <gzjjgod@gmail.com>\r
++Rolland Santimano <rollandsantimano@yahoo.com>\r
++Jan Janak <jan@ryngle.com>\r
++Stewart Smith <stewart@flamingspork.com>\r
++Lars Kellogg-Stedman <lars@seas.harvard.edu>\r
++Alex Botero-Lowry <alex.boterolowry@gmail.com>\r
++Ingmar Vanhassel <ingmar@exherbo.org>\r
++Aron Griffis <agriffis@n01se.net>\r
++Adrian Perez de Castro <aperez@igalia.com>\r
++Israel Herraiz <isra@herraiz.org>\r
++Mikhail Gusarov <dottedmag@dottedmag.net>\r
++EOF\r
++test_expect_equal_file OUTPUT EXPECTED\r
++\r
++test_begin_subtest "--output=sender --format=json"\r
++notmuch search --output=sender --format=json '*' >OUTPUT\r
++cat <<EOF >EXPECTED\r
++[{"name": "François Boulogne", "address": "boulogne.f@gmail.com"},\r
++{"name": "Olivier Berger", "address": "olivier.berger@it-sudparis.eu"},\r
++{"name": "Chris Wilson", "address": "chris@chris-wilson.co.uk"},\r
++{"name": "Carl Worth", "address": "cworth@cworth.org"},\r
++{"name": "Alexander Botero-Lowry", "address": "alex.boterolowry@gmail.com"},\r
++{"name": "Keith Packard", "address": "keithp@keithp.com"},\r
++{"name": "Jjgod Jiang", "address": "gzjjgod@gmail.com"},\r
++{"name": "Rolland Santimano", "address": "rollandsantimano@yahoo.com"},\r
++{"name": "Jan Janak", "address": "jan@ryngle.com"},\r
++{"name": "Stewart Smith", "address": "stewart@flamingspork.com"},\r
++{"name": "Lars Kellogg-Stedman", "address": "lars@seas.harvard.edu"},\r
++{"name": "Alex Botero-Lowry", "address": "alex.boterolowry@gmail.com"},\r
++{"name": "Ingmar Vanhassel", "address": "ingmar@exherbo.org"},\r
++{"name": "Aron Griffis", "address": "agriffis@n01se.net"},\r
++{"name": "Adrian Perez de Castro", "address": "aperez@igalia.com"},\r
++{"name": "Israel Herraiz", "address": "isra@herraiz.org"},\r
++{"name": "Mikhail Gusarov", "address": "dottedmag@dottedmag.net"}]\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
++Allan McRae <allan@archlinux.org>\r
++Discussion about the Arch User Repository (AUR) <aur-general@archlinux.org>\r
++olivier.berger@it-sudparis.eu\r
++notmuch@notmuchmail.org\r
++notmuch <notmuch@notmuchmail.org>\r
++Keith Packard <keithp@keithp.com>\r
++Mikhail Gusarov <dottedmag@dottedmag.net>\r
++EOF\r
++test_expect_equal_file OUTPUT EXPECTED\r
++\r
++test_begin_subtest "--output=sender --output=recipients"\r
++notmuch search --output=sender --output=recipients '*' >OUTPUT\r
++cat <<EOF >EXPECTED\r
++François Boulogne <boulogne.f@gmail.com>\r
++Allan McRae <allan@archlinux.org>\r
++Discussion about the Arch User Repository (AUR) <aur-general@archlinux.org>\r
++Olivier Berger <olivier.berger@it-sudparis.eu>\r
++olivier.berger@it-sudparis.eu\r
++Chris Wilson <chris@chris-wilson.co.uk>\r
++notmuch@notmuchmail.org\r
++Carl Worth <cworth@cworth.org>\r
++Alexander Botero-Lowry <alex.boterolowry@gmail.com>\r
++Keith Packard <keithp@keithp.com>\r
++Jjgod Jiang <gzjjgod@gmail.com>\r
++Rolland Santimano <rollandsantimano@yahoo.com>\r
++Jan Janak <jan@ryngle.com>\r
++Stewart Smith <stewart@flamingspork.com>\r
++Lars Kellogg-Stedman <lars@seas.harvard.edu>\r
++notmuch <notmuch@notmuchmail.org>\r
++Alex Botero-Lowry <alex.boterolowry@gmail.com>\r
++Ingmar Vanhassel <ingmar@exherbo.org>\r
++Aron Griffis <agriffis@n01se.net>\r
++Adrian Perez de Castro <aperez@igalia.com>\r
++Israel Herraiz <isra@herraiz.org>\r
++Mikhail Gusarov <dottedmag@dottedmag.net>\r
++EOF\r
++test_expect_equal_file OUTPUT EXPECTED\r
++\r
+ test_begin_subtest "sanitize output for quoted-printable line-breaks in author and subject"\r
+ add_message "[subject]='two =?ISO-8859-1?Q?line=0A_subject?=\r
+ headers'"\r
+-- \r
+2.1.1\r
+\r