[PATCH 4/5] cli: Add configurable address deduplication for --output=addresses

2014-09-22 Thread Michal Sojka
The code here is an extended version of a path from Jani Nikula.
---
 completion/notmuch-completion.bash |   6 ++-
 completion/notmuch-completion.zsh  |   3 +-
 doc/man1/notmuch-search.rst|  33 
 notmuch-search.c   | 101 ++---
 4 files changed, 135 insertions(+), 8 deletions(-)

diff --git a/completion/notmuch-completion.bash 
b/completion/notmuch-completion.bash
index c37ddf5..8bc7874 100644
--- a/completion/notmuch-completion.bash
+++ b/completion/notmuch-completion.bash
@@ -305,12 +305,16 @@ _notmuch_search()
COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
return
;;
+   --unique)
+   COMPREPLY=( $( compgen -W "none addr addrfold name" -- "${cur}" ) )
+   return
+   ;;
 esac

 ! $split &&
 case "${cur}" in
-*)
-   local options="--format= --output= --sort= --offset= --limit= 
--exclude= --duplicate="
+   local options="--format= --output= --sort= --offset= --limit= 
--exclude= --duplicate= --unique="
compopt -o nospace
COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
;;
diff --git a/completion/notmuch-completion.zsh 
b/completion/notmuch-completion.zsh
index bff8fd5..cf4968c 100644
--- a/completion/notmuch-completion.zsh
+++ b/completion/notmuch-completion.zsh
@@ -53,7 +53,8 @@ _notmuch_search()
 '--max-threads=[display only the first x threads from the search 
results]:number of threads to show: ' \
 '--first=[omit the first x threads from the search results]:number of 
threads to omit: ' \
 '--sort=[sort results]:sorting:((newest-first\:"reverse chronological 
order" oldest-first\:"chronological order"))' \
-'--output=[select what to output]:output:((summary threads messages files 
tags sender recipients addresses))'
+'--output=[select what to output]:output:((summary threads messages files 
tags sender recipients addresses))' \
+'--unique=[address deduplication]:unique:((none\:"no deduplication" 
addr\:"deduplicate by address" addrfold\:"deduplicate by case-insensitive 
address" name\:"deduplicate by name"))'
 }

 _notmuch()
diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst
index 6094906..a92779a 100644
--- a/doc/man1/notmuch-search.rst
+++ b/doc/man1/notmuch-search.rst
@@ -85,6 +85,9 @@ Supported options for **search** include
 (--format=text0), as a JSON array (--format=json), or as
 an S-Expression list (--format=sexp).

+Handling of duplicate addresses and/or names can be
+controlled with the --unique option.
+
Note: Searching for **sender** should much be faster than
searching for **recipients** or **addresses**, because
sender addresses are cached directly in the database
@@ -151,6 +154,36 @@ Supported options for **search** include
 prefix. The prefix matches messages based on filenames. This
 option filters filenames of the matching messages.

+``--unique=``\ (**none**\ \|\ **addr**\ \|\ **addrfold**\ \|\ **name**)[,\ 
...]
+
+Can be used with ``--output=addresses``, ``--output=sender``
+or ``--output=recipients`` to control the address
+deduplication algorithm.
+
+   **none** means that no deduplication is performed. The same
+   address can appear multiple times in the output.
+
+   **addr** means that case-sensitive deduplication is performed
+   on the address part. For example, given the addresses "John
+   Doe " and "Dr. John Doe ",
+   only one will be printed.
+
+   **addrfold** means that case-insensitive deduplication is
+   performed on the address part. For example, given the
+   addresses "John Doe " and "John Doe
+   ", only one will be printed. This is the
+   default.
+
+   **name** means that case-sensitive deduplication is performed
+   on the name part. For example, given the addresses "John Doe
+   " and "John Doe ", only one
+   will be printed.
+
+   It is possible to combine the above flags (except **none**) by
+   separating them with comma. For example,
+   ``--unique=name,addr`` will print unique case-sensitive
+   combinations of name and address.
+
 EXIT STATUS
 ===

diff --git a/notmuch-search.c b/notmuch-search.c
index 0614f10..00d6771 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -33,6 +33,15 @@ typedef enum {
 OUTPUT_ADDRESSES   = OUTPUT_SENDER | OUTPUT_RECIPIENTS,
 } output_t;

+typedef enum {
+UNIQUE_NONE  = 1 << 0,
+UNIQUE_ADDR  = 1 << 1,
+UNIQUE_NAME  = 1 << 2,
+UNIQUE_ADDR_CASEFOLD  = 1 << 3,
+
+UNIQUE_BOTH = UNIQUE_NAME | UNIQUE_ADDR,
+} unique_t;
+
 typedef struct {
 sprinter_t *format;
 notmuch_query_t *query;
@@ -41,6 +50,7 @@ typedef struct {
 int offset;
 int limit;
 int dupe;
+unique_t unique;
 } 

[PATCH 4/5] cli: Add configurable address deduplication for --output=addresses

2014-09-22 Thread Michal Sojka
The code here is an extended version of a path from Jani Nikula.
---
 completion/notmuch-completion.bash |   6 ++-
 completion/notmuch-completion.zsh  |   3 +-
 doc/man1/notmuch-search.rst|  33 
 notmuch-search.c   | 101 ++---
 4 files changed, 135 insertions(+), 8 deletions(-)

diff --git a/completion/notmuch-completion.bash 
b/completion/notmuch-completion.bash
index c37ddf5..8bc7874 100644
--- a/completion/notmuch-completion.bash
+++ b/completion/notmuch-completion.bash
@@ -305,12 +305,16 @@ _notmuch_search()
COMPREPLY=( $( compgen -W true false flag all -- ${cur} ) )
return
;;
+   --unique)
+   COMPREPLY=( $( compgen -W none addr addrfold name -- ${cur} ) )
+   return
+   ;;
 esac
 
 ! $split 
 case ${cur} in
-*)
-   local options=--format= --output= --sort= --offset= --limit= 
--exclude= --duplicate=
+   local options=--format= --output= --sort= --offset= --limit= 
--exclude= --duplicate= --unique=
compopt -o nospace
COMPREPLY=( $(compgen -W $options -- ${cur}) )
;;
diff --git a/completion/notmuch-completion.zsh 
b/completion/notmuch-completion.zsh
index bff8fd5..cf4968c 100644
--- a/completion/notmuch-completion.zsh
+++ b/completion/notmuch-completion.zsh
@@ -53,7 +53,8 @@ _notmuch_search()
 '--max-threads=[display only the first x threads from the search 
results]:number of threads to show: ' \
 '--first=[omit the first x threads from the search results]:number of 
threads to omit: ' \
 '--sort=[sort results]:sorting:((newest-first\:reverse chronological 
order oldest-first\:chronological order))' \
-'--output=[select what to output]:output:((summary threads messages files 
tags sender recipients addresses))'
+'--output=[select what to output]:output:((summary threads messages files 
tags sender recipients addresses))' \
+'--unique=[address deduplication]:unique:((none\:no deduplication 
addr\:deduplicate by address addrfold\:deduplicate by case-insensitive 
address name\:deduplicate by name))'
 }
 
 _notmuch()
diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst
index 6094906..a92779a 100644
--- a/doc/man1/notmuch-search.rst
+++ b/doc/man1/notmuch-search.rst
@@ -85,6 +85,9 @@ Supported options for **search** include
 (--format=text0), as a JSON array (--format=json), or as
 an S-Expression list (--format=sexp).
 
+Handling of duplicate addresses and/or names can be
+controlled with the --unique option.
+
Note: Searching for **sender** should much be faster than
searching for **recipients** or **addresses**, because
sender addresses are cached directly in the database
@@ -151,6 +154,36 @@ Supported options for **search** include
 prefix. The prefix matches messages based on filenames. This
 option filters filenames of the matching messages.
 
+``--unique=``\ (**none**\ \|\ **addr**\ \|\ **addrfold**\ \|\ **name**)[,\ 
...]
+
+Can be used with ``--output=addresses``, ``--output=sender``
+or ``--output=recipients`` to control the address
+deduplication algorithm.
+
+   **none** means that no deduplication is performed. The same
+   address can appear multiple times in the output.
+
+   **addr** means that case-sensitive deduplication is performed
+   on the address part. For example, given the addresses John
+   Doe j...@example.com and Dr. John Doe j...@example.com,
+   only one will be printed.
+
+   **addrfold** means that case-insensitive deduplication is
+   performed on the address part. For example, given the
+   addresses John Doe j...@example.com and John Doe
+   j...@example.com, only one will be printed. This is the
+   default.
+
+   **name** means that case-sensitive deduplication is performed
+   on the name part. For example, given the addresses John Doe
+   j...@example.com and John Doe j...@doe.name, only one
+   will be printed.
+
+   It is possible to combine the above flags (except **none**) by
+   separating them with comma. For example,
+   ``--unique=name,addr`` will print unique case-sensitive
+   combinations of name and address.
+
 EXIT STATUS
 ===
 
diff --git a/notmuch-search.c b/notmuch-search.c
index 0614f10..00d6771 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -33,6 +33,15 @@ typedef enum {
 OUTPUT_ADDRESSES   = OUTPUT_SENDER | OUTPUT_RECIPIENTS,
 } output_t;
 
+typedef enum {
+UNIQUE_NONE  = 1  0,
+UNIQUE_ADDR  = 1  1,
+UNIQUE_NAME  = 1  2,
+UNIQUE_ADDR_CASEFOLD  = 1  3,
+
+UNIQUE_BOTH = UNIQUE_NAME | UNIQUE_ADDR,
+} unique_t;
+
 typedef struct {
 sprinter_t *format;
 notmuch_query_t *query;
@@ -41,6 +50,7 @@ typedef struct {
 int offset;
 int