--- /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 1EE5E431FC2\r
+ for <notmuch@notmuchmail.org>; Sun, 5 Oct 2014 13:52:41 -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 21k0tCkrMC-G for <notmuch@notmuchmail.org>;\r
+ Sun, 5 Oct 2014 13:52:37 -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 E1BD1431FAE\r
+ for <notmuch@notmuchmail.org>; Sun, 5 Oct 2014 13:52:36 -0700 (PDT)\r
+Received: from localhost (unknown [192.168.200.7])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id 1718E5CCEF9\r
+ for <notmuch@notmuchmail.org>; Sun, 5 Oct 2014 22:52:36 +0200 (CEST)\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 PrbitlJ4Rqvc for <notmuch@notmuchmail.org>;\r
+ Sun, 5 Oct 2014 22:52:32 +0200 (CEST)\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 5AFEC3CFEA1\r
+ for <notmuch@notmuchmail.org>; Sun, 5 Oct 2014 22:52:31 +0200 (CEST)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
+ (envelope-from <sojkam1@fel.cvut.cz>)\r
+ id 1Xasn2-0005EC-TO; Sun, 05 Oct 2014 22:52:24 +0200\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 4/4] cli: Add configurable address deduplication for\r
+ --output=addresses\r
+Date: Sun, 5 Oct 2014 22:51:59 +0200\r
+Message-Id: <1412542319-20017-5-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.1.1\r
+In-Reply-To: <1412542319-20017-1-git-send-email-sojkam1@fel.cvut.cz>\r
+References: <874mvqxrnp.fsf@nikula.org>\r
+ <1412542319-20017-1-git-send-email-sojkam1@fel.cvut.cz>\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: Sun, 05 Oct 2014 20:52:41 -0000\r
+\r
+The code here is an extended version of a patch from Jani Nikula.\r
+---\r
+ completion/notmuch-completion.bash | 6 ++-\r
+ completion/notmuch-completion.zsh | 3 +-\r
+ doc/man1/notmuch-search.rst | 32 ++++++++++++\r
+ notmuch-search.c | 101 ++++++++++++++++++++++++++++++++++---\r
+ test/T090-search-output.sh | 6 +--\r
+ test/T095-search-unique.sh | 63 +++++++++++++++++++++++\r
+ 6 files changed, 200 insertions(+), 11 deletions(-)\r
+ create mode 100755 test/T095-search-unique.sh\r
+\r
+diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash\r
+index c37ddf5..8bc7874 100644\r
+--- a/completion/notmuch-completion.bash\r
++++ b/completion/notmuch-completion.bash\r
+@@ -305,12 +305,16 @@ _notmuch_search()\r
+ COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )\r
+ return\r
+ ;;\r
++ --unique)\r
++ COMPREPLY=( $( compgen -W "none addr addrfold name" -- "${cur}" ) )\r
++ return\r
++ ;;\r
+ esac\r
+ \r
+ ! $split &&\r
+ case "${cur}" in\r
+ -*)\r
+- local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate="\r
++ local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate= --unique="\r
+ compopt -o nospace\r
+ COMPREPLY=( $(compgen -W "$options" -- ${cur}) )\r
+ ;;\r
+diff --git a/completion/notmuch-completion.zsh b/completion/notmuch-completion.zsh\r
+index bff8fd5..cf4968c 100644\r
+--- a/completion/notmuch-completion.zsh\r
++++ b/completion/notmuch-completion.zsh\r
+@@ -53,7 +53,8 @@ _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 addresses))'\r
++ '--output=[select what to output]:output:((summary threads messages files tags sender recipients addresses))' \\r
++ '--unique=[address deduplication]:unique:((none\:"no deduplication" addr\:"deduplicate by address" addrfold\:"deduplicate by case-insensitive address" name\:"deduplicate by name"))'\r
+ }\r
+ \r
+ _notmuch()\r
+diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst\r
+index 3447820..9a9d9c3 100644\r
+--- a/doc/man1/notmuch-search.rst\r
++++ b/doc/man1/notmuch-search.rst\r
+@@ -85,6 +85,9 @@ 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
++ Handling of duplicate addresses and/or names can be\r
++ controlled with the --unique option.\r
++\r
+ Note: Searching for **sender** should be much faster than\r
+ searching for **recipients** or **addresses**, because\r
+ sender addresses are cached directly in the database\r
+@@ -151,6 +154,35 @@ Supported options for **search** include\r
+ prefix. The prefix matches messages based on filenames. This\r
+ option filters filenames of the matching messages.\r
+ \r
++ ``--unique=``\ (**none**\ \|\ **addr**\ \|\ **addrfold**\ \|\ **name**)\r
++\r
++ Can be used with ``--output=addresses``, ``--output=sender``\r
++ or ``--output=recipients`` to control the address\r
++ deduplication algorithm.\r
++\r
++ **none** means that no deduplication is performed. The same\r
++ address can appear multiple times in the output.\r
++\r
++ **addr** means that case-sensitive deduplication is performed\r
++ on the address part. For example, given the addresses "John\r
++ Doe <john@example.com>" and "Dr. John Doe <john@example.com>",\r
++ only one will be printed.\r
++\r
++ **addrfold** is the same as **addr** but with case folding\r
++ applied. For example, given the addresses "John Doe\r
++ <john@example.com>" and "John Doe <JOHN@EXAMPLE.COM>", only\r
++ one will be printed. This is the default.\r
++\r
++ **name** means that case-sensitive deduplication is performed\r
++ on the name part. For example, given the addresses "John Doe\r
++ <john@example.com>" and "John Doe <john@doe.name>", only one\r
++ will be printed.\r
++\r
++ This option can be given multiple times to output unique\r
++ combinations of names and addresses. For example,\r
++ ``--unique=name --unique=addr`` will print unique\r
++ case-sensitive combinations of name and address.\r
++\r
+ EXIT STATUS\r
+ ===========\r
+ \r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 0614f10..94d400e 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -33,6 +33,15 @@ typedef enum {\r
+ OUTPUT_ADDRESSES = OUTPUT_SENDER | OUTPUT_RECIPIENTS,\r
+ } output_t;\r
+ \r
++typedef enum {\r
++ UNIQUE_NONE = 1 << 0,\r
++ UNIQUE_ADDR = 1 << 1,\r
++ UNIQUE_NAME = 1 << 2,\r
++ UNIQUE_ADDR_CASEFOLD = 1 << 3,\r
++\r
++ UNIQUE_BOTH = UNIQUE_NAME | UNIQUE_ADDR,\r
++} unique_t;\r
++\r
+ typedef struct {\r
+ sprinter_t *format;\r
+ notmuch_query_t *query;\r
+@@ -41,6 +50,7 @@ typedef struct {\r
+ int offset;\r
+ int limit;\r
+ int dupe;\r
++ unique_t unique;\r
+ } search_options_t;\r
+ \r
+ /* Return two stable query strings that identify exactly the matched\r
+@@ -223,8 +233,51 @@ do_search_threads (search_options_t *o)\r
+ return 0;\r
+ }\r
+ \r
++/* Returns TRUE iff name and/or addr is considered unique. */\r
++static notmuch_bool_t\r
++check_unique (const search_options_t *o, GHashTable *addrs, const char *name, const char *addr)\r
++{\r
++ notmuch_bool_t unique;\r
++ char *key;\r
++\r
++ if (o->unique == UNIQUE_NONE)\r
++ return TRUE;\r
++\r
++ if (o->unique & UNIQUE_ADDR_CASEFOLD) {\r
++ gchar *folded = g_utf8_casefold (addr, -1);\r
++ addr = talloc_strdup (o->format, folded);\r
++ g_free (folded);\r
++ }\r
++ switch (o->unique & UNIQUE_BOTH) {\r
++ case UNIQUE_NAME:\r
++ key = talloc_strdup (o->format, name); /* !name results in !key */\r
++ break;\r
++ case UNIQUE_ADDR:\r
++ key = talloc_strdup (o->format, addr);\r
++ break;\r
++ case UNIQUE_BOTH:\r
++ key = talloc_asprintf (o->format, "%s <%s>", name, addr);\r
++ break;\r
++ default:\r
++ INTERNAL_ERROR("invalid --unique flags");\r
++ }\r
++\r
++ if (! key)\r
++ return FALSE;\r
++\r
++ unique = !g_hash_table_lookup_extended (addrs, key, NULL, NULL);\r
++\r
++ if (unique)\r
++ g_hash_table_insert (addrs, key, NULL);\r
++ else\r
++ talloc_free (key);\r
++\r
++ return unique;\r
++}\r
++\r
+ static void\r
+-print_address_list (const search_options_t *o, InternetAddressList *list)\r
++print_address_list (const search_options_t *o, GHashTable *addrs,\r
++ InternetAddressList *list)\r
+ {\r
+ InternetAddress *address;\r
+ int i;\r
+@@ -240,7 +293,7 @@ print_address_list (const search_options_t *o, InternetAddressList *list)\r
+ if (group_list == NULL)\r
+ continue;\r
+ \r
+- print_address_list (o, group_list);\r
++ print_address_list (o, addrs, group_list);\r
+ } else {\r
+ InternetAddressMailbox *mailbox;\r
+ const char *name;\r
+@@ -252,6 +305,9 @@ print_address_list (const search_options_t *o, InternetAddressList *list)\r
+ name = internet_address_get_name (address);\r
+ addr = internet_address_mailbox_get_addr (mailbox);\r
+ \r
++ if (!check_unique (o, addrs, name, addr))\r
++ continue;\r
++\r
+ if (name && *name)\r
+ full_address = talloc_asprintf (o->format, "%s <%s>", name, addr);\r
+ else\r
+@@ -270,7 +326,7 @@ print_address_list (const search_options_t *o, InternetAddressList *list)\r
+ }\r
+ \r
+ static void\r
+-print_address_string (const search_options_t *o, const char *recipients)\r
++print_address_string (const search_options_t *o, GHashTable *addrs, const char *recipients)\r
+ {\r
+ InternetAddressList *list;\r
+ \r
+@@ -281,7 +337,13 @@ print_address_string (const search_options_t *o, const char *recipients)\r
+ if (list == NULL)\r
+ return;\r
+ \r
+- print_address_list (o, list);\r
++ print_address_list (o, 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
+@@ -291,8 +353,13 @@ do_search_messages (search_options_t *o)\r
+ notmuch_messages_t *messages;\r
+ notmuch_filenames_t *filenames;\r
+ sprinter_t *format = o->format;\r
++ GHashTable *addresses = NULL;\r
+ int i;\r
+ \r
++ if (o->output & OUTPUT_ADDRESSES)\r
++ addresses = g_hash_table_new_full (g_str_hash, g_str_equal,\r
++ _my_talloc_free_for_g_hash, NULL);\r
++\r
+ if (o->offset < 0) {\r
+ o->offset += notmuch_query_count_messages (o->query);\r
+ if (o->offset < 0)\r
+@@ -340,7 +407,7 @@ do_search_messages (search_options_t *o)\r
+ const char *addrs;\r
+ \r
+ addrs = notmuch_message_get_header (message, "from");\r
+- print_address_string (o, addrs);\r
++ print_address_string (o, addresses, addrs);\r
+ }\r
+ \r
+ if (o->output & OUTPUT_RECIPIENTS) {\r
+@@ -350,7 +417,7 @@ do_search_messages (search_options_t *o)\r
+ \r
+ for (j = 0; j < ARRAY_SIZE (hdrs); j++) {\r
+ addrs = notmuch_message_get_header (message, hdrs[j]);\r
+- print_address_string (o, addrs);\r
++ print_address_string (o, addresses, addrs);\r
+ }\r
+ }\r
+ }\r
+@@ -358,6 +425,9 @@ do_search_messages (search_options_t *o)\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
+@@ -423,6 +493,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ .offset = 0,\r
+ .limit = -1, /* unlimited */\r
+ .dupe = -1,\r
++ .unique = 0,\r
+ };\r
+ char *query_str;\r
+ int opt_index, ret;\r
+@@ -467,6 +538,12 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ { NOTMUCH_OPT_INT, &o.offset, "offset", 'O', 0 },\r
+ { NOTMUCH_OPT_INT, &o.limit, "limit", 'L', 0 },\r
+ { NOTMUCH_OPT_INT, &o.dupe, "duplicate", 'D', 0 },\r
++ { NOTMUCH_OPT_KEYWORD_FLAGS, &o.unique, "unique", 'u',\r
++ (notmuch_keyword_t []){ { "none", UNIQUE_NONE },\r
++ { "name", UNIQUE_NAME },\r
++ { "addr", UNIQUE_ADDR },\r
++ { "addrfold", UNIQUE_ADDR | UNIQUE_ADDR_CASEFOLD },\r
++ { 0, 0 } } },\r
+ { 0, 0, 0, 0, 0 }\r
+ };\r
+ \r
+@@ -474,6 +551,18 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ if (opt_index < 0)\r
+ return EXIT_FAILURE;\r
+ \r
++ if (o.unique && (o.output & ~OUTPUT_ADDRESSES)) {\r
++ fprintf (stderr, "Error: --unique can only be used with address output.\n");\r
++ return EXIT_FAILURE;\r
++ }\r
++ if ((o.unique & UNIQUE_NONE) &&\r
++ (o.unique & ~UNIQUE_NONE)) {\r
++ fprintf (stderr, "Error: --unique=none cannot be combined with other flags.\n");\r
++ return EXIT_FAILURE;\r
++ }\r
++ if (! o.unique)\r
++ o.unique = UNIQUE_ADDR | UNIQUE_ADDR_CASEFOLD;\r
++\r
+ switch (format_sel) {\r
+ case NOTMUCH_FORMAT_TEXT:\r
+ o.format = sprinter_text_create (config, stdout);\r
+diff --git a/test/T090-search-output.sh b/test/T090-search-output.sh\r
+index 5458de1..e11cb52 100755\r
+--- a/test/T090-search-output.sh\r
++++ b/test/T090-search-output.sh\r
+@@ -388,7 +388,7 @@ EOF\r
+ test_expect_equal_file OUTPUT EXPECTED\r
+ \r
+ test_begin_subtest "--output=sender"\r
+-notmuch search --output=sender '*' | sort | uniq --count >OUTPUT\r
++notmuch search --output=sender --unique=none '*' | sort | uniq --count >OUTPUT\r
+ cat <<EOF >EXPECTED\r
+ 1 Adrian Perez de Castro <aperez@igalia.com>\r
+ 2 Alex Botero-Lowry <alex.boterolowry@gmail.com>\r
+@@ -411,7 +411,7 @@ EOF\r
+ test_expect_equal_file OUTPUT EXPECTED\r
+ \r
+ test_begin_subtest "--output=recipients"\r
+-notmuch search --output=recipients '*' | sort | uniq --count >OUTPUT\r
++notmuch search --output=recipients --unique=none '*' | sort | uniq --count >OUTPUT\r
+ cat <<EOF >EXPECTED\r
+ 1 Allan McRae <allan@archlinux.org>\r
+ 1 Discussion about the Arch User Repository (AUR) <aur-general@archlinux.org>\r
+@@ -424,7 +424,7 @@ EOF\r
+ test_expect_equal_file OUTPUT EXPECTED\r
+ \r
+ test_begin_subtest "--output=addresses"\r
+-notmuch search --output=addresses '*' | sort | uniq --count >OUTPUT\r
++notmuch search --output=addresses --unique=none '*' | sort | uniq --count >OUTPUT\r
+ cat <<EOF >EXPECTED\r
+ 1 Adrian Perez de Castro <aperez@igalia.com>\r
+ 2 Alex Botero-Lowry <alex.boterolowry@gmail.com>\r
+diff --git a/test/T095-search-unique.sh b/test/T095-search-unique.sh\r
+new file mode 100755\r
+index 0000000..1d8afac\r
+--- /dev/null\r
++++ b/test/T095-search-unique.sh\r
+@@ -0,0 +1,63 @@\r
++#!/usr/bin/env bash\r
++test_description='address deduplication in "notmuch search --output=addresses"'\r
++. ./test-lib.sh\r
++\r
++add_message '[to]="Real Name <foo@example.com>, Real Name <bar@example.com>"'\r
++add_message '[to]="Nickname <foo@example.com>"' '[cc]="Real Name <Bar@Example.COM>"'\r
++add_message '[to]="Nickname <foo@example.com>"' '[bcc]="Real Name <Bar@Example.COM>"'\r
++\r
++test_begin_subtest "--output=recipients"\r
++notmuch search --output=recipients "*" >OUTPUT\r
++cat <<EOF >EXPECTED\r
++Real Name <foo@example.com>\r
++Real Name <bar@example.com>\r
++EOF\r
++test_expect_equal_file OUTPUT EXPECTED\r
++\r
++test_begin_subtest "--output=recipients --unique=none"\r
++notmuch search --output=recipients --unique=none "*" >OUTPUT\r
++cat <<EOF >EXPECTED\r
++Real Name <foo@example.com>\r
++Real Name <bar@example.com>\r
++Nickname <foo@example.com>\r
++Real Name <Bar@Example.COM>\r
++Nickname <foo@example.com>\r
++Real Name <Bar@Example.COM>\r
++EOF\r
++test_expect_equal_file OUTPUT EXPECTED\r
++\r
++test_begin_subtest "--output=recipients --unique=addr"\r
++notmuch search --output=recipients --unique=addr "*" >OUTPUT\r
++cat <<EOF >EXPECTED\r
++Real Name <foo@example.com>\r
++Real Name <bar@example.com>\r
++Real Name <Bar@Example.COM>\r
++EOF\r
++test_expect_equal_file OUTPUT EXPECTED\r
++\r
++test_begin_subtest "--output=recipients --unique=addrfold"\r
++notmuch search --output=recipients --unique=addrfold "*" >OUTPUT\r
++cat <<EOF >EXPECTED\r
++Real Name <foo@example.com>\r
++Real Name <bar@example.com>\r
++EOF\r
++test_expect_equal_file OUTPUT EXPECTED\r
++\r
++test_begin_subtest "--output=recipients --unique=name"\r
++notmuch search --output=recipients --unique=name "*" >OUTPUT\r
++cat <<EOF >EXPECTED\r
++Real Name <foo@example.com>\r
++Nickname <foo@example.com>\r
++EOF\r
++test_expect_equal_file OUTPUT EXPECTED\r
++\r
++test_begin_subtest "--output=recipients --unique=name --unique=addrfold"\r
++notmuch search --output=recipients --unique=name --unique=addrfold "*" >OUTPUT\r
++cat <<EOF >EXPECTED\r
++Real Name <foo@example.com>\r
++Real Name <bar@example.com>\r
++Nickname <foo@example.com>\r
++EOF\r
++test_expect_equal_file OUTPUT EXPECTED\r
++\r
++test_done\r
+-- \r
+2.1.1\r
+\r