notmuch.el: controlling what does and doesn't get expanded in searches
[notmuch-archives.git] / 1e / b415e5cb1156abc9375d8909985050ceadd7e1
1 Return-Path: <sojkam1@fel.cvut.cz>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 14932431FD9\r
6         for <notmuch@notmuchmail.org>; Mon,  3 Nov 2014 15:51:19 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -2.3\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id PoggeupQBCuM for <notmuch@notmuchmail.org>;\r
16         Mon,  3 Nov 2014 15:51:13 -0800 (PST)\r
17 Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
18         by olra.theworths.org (Postfix) with ESMTP id 93A04429E35\r
19         for <notmuch@notmuchmail.org>; Mon,  3 Nov 2014 15:50:52 -0800 (PST)\r
20 Received: from localhost (unknown [192.168.200.7])\r
21         by max.feld.cvut.cz (Postfix) with ESMTP id 11A915CD006\r
22         for <notmuch@notmuchmail.org>; Tue,  4 Nov 2014 00:50:52 +0100 (CET)\r
23 X-Virus-Scanned: IMAP STYX AMAVIS\r
24 Received: from max.feld.cvut.cz ([192.168.200.1])\r
25         by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new,\r
26         port 10044) with ESMTP id Ul0JGX8-YPnK for <notmuch@notmuchmail.org>;\r
27         Tue,  4 Nov 2014 00:50:47 +0100 (CET)\r
28 Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
29         by max.feld.cvut.cz (Postfix) with ESMTP id 1ADE95CD00F\r
30         for <notmuch@notmuchmail.org>; Tue,  4 Nov 2014 00:50:41 +0100 (CET)\r
31 Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
32         (envelope-from <sojkam1@fel.cvut.cz>)\r
33         id 1XlROM-0005X2-9e; Tue, 04 Nov 2014 00:50:34 +0100\r
34 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
35 To: notmuch@notmuchmail.org\r
36 Subject: [PATCH v2 10/10] cli: address: Add --filter-by option to configure\r
37         address filtering\r
38 Date: Tue,  4 Nov 2014 00:50:22 +0100\r
39 Message-Id: <1415058622-21162-11-git-send-email-sojkam1@fel.cvut.cz>\r
40 X-Mailer: git-send-email 2.1.1\r
41 In-Reply-To: <1415058622-21162-1-git-send-email-sojkam1@fel.cvut.cz>\r
42 References: <1415058622-21162-1-git-send-email-sojkam1@fel.cvut.cz>\r
43 X-BeenThere: notmuch@notmuchmail.org\r
44 X-Mailman-Version: 2.1.13\r
45 Precedence: list\r
46 List-Id: "Use and development of the notmuch mail system."\r
47         <notmuch.notmuchmail.org>\r
48 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
50 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
51 List-Post: <mailto:notmuch@notmuchmail.org>\r
52 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
53 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
54         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
55 X-List-Received-Date: Mon, 03 Nov 2014 23:51:19 -0000\r
56 \r
57 This option allows to configure the criterion for duplicate address\r
58 filtering. Without this option, all unique combinations of name and\r
59 address parts are printed. This option allows to filter the output\r
60 more, for example to only contain unique address parts.\r
61 ---\r
62  completion/notmuch-completion.bash |  6 +++-\r
63  completion/notmuch-completion.zsh  |  1 +\r
64  doc/man1/notmuch-address.rst       | 36 ++++++++++++++++++-\r
65  notmuch-search.c                   | 48 +++++++++++++++++++++++--\r
66  test/T097-address-filter-by.sh     | 73 ++++++++++++++++++++++++++++++++++++++\r
67  5 files changed, 160 insertions(+), 4 deletions(-)\r
68  create mode 100755 test/T097-address-filter-by.sh\r
69 \r
70 diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash\r
71 index db152f3..2cb1586 100644\r
72 --- a/completion/notmuch-completion.bash\r
73 +++ b/completion/notmuch-completion.bash\r
74 @@ -310,7 +310,7 @@ _notmuch_search()\r
75      ! $split &&\r
76      case "${cur}" in\r
77         -*)\r
78 -           local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate="\r
79 +           local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate= --filter-by="\r
80             compopt -o nospace\r
81             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )\r
82             ;;\r
83 @@ -343,6 +343,10 @@ _notmuch_address()\r
84             COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )\r
85             return\r
86             ;;\r
87 +       --filter-by)\r
88 +           COMPREPLY=( $( compgen -W "nameaddr name addr addrfold nameaddrfold" -- "${cur}" ) )\r
89 +           return\r
90 +           ;;\r
91      esac\r
92  \r
93      ! $split &&\r
94 diff --git a/completion/notmuch-completion.zsh b/completion/notmuch-completion.zsh\r
95 index 8968562..3758f1a 100644\r
96 --- a/completion/notmuch-completion.zsh\r
97 +++ b/completion/notmuch-completion.zsh\r
98 @@ -62,6 +62,7 @@ _notmuch_address()\r
99    _arguments -s : \\r
100      '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \\r
101      '--output=[select what to output]:output:((sender recipients count))'\r
102 +    '--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
103  }\r
104  \r
105  _notmuch()\r
106 diff --git a/doc/man1/notmuch-address.rst b/doc/man1/notmuch-address.rst\r
107 index 18473a7..524ab91 100644\r
108 --- a/doc/man1/notmuch-address.rst\r
109 +++ b/doc/man1/notmuch-address.rst\r
110 @@ -11,7 +11,8 @@ DESCRIPTION\r
111  ===========\r
112  \r
113  Search for messages matching the given search terms, and display the\r
114 -addresses from them. Duplicate addresses are filtered out.\r
115 +addresses from them. Duplicate addresses are filtered out. Filtering\r
116 +can be configured with the --filter-by option.\r
117  \r
118  See **notmuch-search-terms(7)** for details of the supported syntax for\r
119  <search-terms>.\r
120 @@ -85,6 +86,39 @@ Supported options for **address** include\r
121          is the number of matching non-excluded messages in the thread,\r
122          rather than the number of matching messages.\r
123  \r
124 +    ``--filter-by=``\ (**nameaddr**\ \|\ **name** \|\ **addr**\ \|\ **addrfold**\ \|\ **nameaddrfold**\)\r
125 +\r
126 +       Controls how to filter out duplicate addresses. The filtering\r
127 +       algorithm receives a sequence of email addresses and outputs\r
128 +       the same sequence without the addresses that are considered a\r
129 +       duplicate of a previously output address. What is considered a\r
130 +       duplicate depends on how the two addresses are compared:\r
131 +\r
132 +       **nameaddr** means that both name and address parts are\r
133 +       compared in case-sensitive manner. Therefore, all same looking\r
134 +       addresses strings are considered duplicate. This is the\r
135 +       default.\r
136 +\r
137 +       **name** means that only the name part is compared (in\r
138 +       case-sensitive manner). For example, the addresses "John Doe\r
139 +       <me@example.com>" and "John Doe <john@doe.name>" will be\r
140 +       considered duplicate.\r
141 +\r
142 +       **addr** means that only the address part is compared (in\r
143 +       case-sensitive manner). For example, the addresses "John Doe\r
144 +       <john@example.com>" and "Dr. John Doe <john@example.com>" will\r
145 +       be considered duplicate.\r
146 +\r
147 +       **addrfold** is like **addr**, but comparison is done in\r
148 +       canse-insensitive manner. For example, the addresses "John Doe\r
149 +       <john@example.com>" and "Dr. John Doe <JOHN@EXAMPLE.COM>" will\r
150 +       be considered duplicate.\r
151 +\r
152 +       **nameaddrfold** is like **nameaddr**, but address comparison\r
153 +       is done in canse-insensitive manner. For example, the\r
154 +       addresses "John Doe <john@example.com>" and "John Doe\r
155 +       <JOHN@EXAMPLE.COM>" will be considered duplicate.\r
156 +\r
157  EXIT STATUS\r
158  ===========\r
159  \r
160 diff --git a/notmuch-search.c b/notmuch-search.c\r
161 index d99e530..04e33c6 100644\r
162 --- a/notmuch-search.c\r
163 +++ b/notmuch-search.c\r
164 @@ -43,6 +43,14 @@ typedef enum {\r
165      NOTMUCH_FORMAT_SEXP\r
166  } format_sel_t;\r
167  \r
168 +typedef enum {\r
169 +    FILTER_BY_NAMEADDR = 0,\r
170 +    FILTER_BY_NAME,\r
171 +    FILTER_BY_ADDR,\r
172 +    FILTER_BY_ADDRFOLD,\r
173 +    FILTER_BY_NAMEADDRFOLD,\r
174 +} filter_by_t;\r
175 +\r
176  typedef struct {\r
177      notmuch_database_t *notmuch;\r
178      format_sel_t format_sel;\r
179 @@ -55,6 +63,7 @@ typedef struct {\r
180      int limit;\r
181      int dupe;\r
182      GHashTable *addresses;\r
183 +    filter_by_t filter_by;\r
184  } search_context_t;\r
185  \r
186  typedef struct {\r
187 @@ -243,15 +252,42 @@ do_search_threads (search_context_t *ctx)\r
188      return 0;\r
189  }\r
190  \r
191 -/* Returns TRUE iff name and addr is duplicate. */\r
192 +/* Returns TRUE iff name and/or addr is considered duplicate. */\r
193  static notmuch_bool_t\r
194  is_duplicate (const search_context_t *ctx, const char *name, const char *addr)\r
195  {\r
196      notmuch_bool_t duplicate;\r
197      char *key;\r
198 +    gchar *addrfold = NULL;\r
199      mailbox_t *mailbox;\r
200  \r
201 -    key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);\r
202 +    if (ctx->filter_by == FILTER_BY_ADDRFOLD ||\r
203 +       ctx->filter_by == FILTER_BY_NAMEADDRFOLD)\r
204 +       addrfold = g_utf8_casefold (addr, -1);\r
205 +\r
206 +    switch (ctx->filter_by) {\r
207 +    case FILTER_BY_NAMEADDR:\r
208 +       key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);\r
209 +       break;\r
210 +    case FILTER_BY_NAMEADDRFOLD:\r
211 +       key = talloc_asprintf (ctx->format, "%s <%s>", name, addrfold);\r
212 +       break;\r
213 +    case FILTER_BY_NAME:\r
214 +       key = talloc_strdup (ctx->format, name); /* !name results in !key */\r
215 +       break;\r
216 +    case FILTER_BY_ADDR:\r
217 +       key = talloc_strdup (ctx->format, addr);\r
218 +       break;\r
219 +    case FILTER_BY_ADDRFOLD:\r
220 +       key = talloc_strdup (ctx->format, addrfold);\r
221 +       break;\r
222 +    default:\r
223 +       INTERNAL_ERROR("invalid --filter-by flags");\r
224 +    }\r
225 +\r
226 +    if (addrfold)\r
227 +       g_free (addrfold);\r
228 +\r
229      if (! key)\r
230         return FALSE;\r
231  \r
232 @@ -721,10 +757,18 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])\r
233                                   { "recipients", OUTPUT_RECIPIENTS },\r
234                                   { "count", OUTPUT_COUNT },\r
235                                   { 0, 0 } } },\r
236 +       { NOTMUCH_OPT_KEYWORD, &ctx->filter_by, "filter-by", 'b',\r
237 +         (notmuch_keyword_t []){ { "nameaddr", FILTER_BY_NAMEADDR },\r
238 +                                 { "name", FILTER_BY_NAME },\r
239 +                                 { "addr", FILTER_BY_ADDR },\r
240 +                                 { "addrfold", FILTER_BY_ADDRFOLD },\r
241 +                                 { "nameaddrfold", FILTER_BY_NAMEADDRFOLD },\r
242 +                                 { 0, 0 } } },\r
243         { NOTMUCH_OPT_INHERIT, &common_options, NULL, 0, 0 },\r
244         { 0, 0, 0, 0, 0 }\r
245      };\r
246  \r
247 +    ctx->filter_by = FILTER_BY_NAMEADDR,\r
248      opt_index = parse_arguments (argc, argv, options, 1);\r
249      if (opt_index < 0)\r
250         return EXIT_FAILURE;\r
251 diff --git a/test/T097-address-filter-by.sh b/test/T097-address-filter-by.sh\r
252 new file mode 100755\r
253 index 0000000..544d8e8\r
254 --- /dev/null\r
255 +++ b/test/T097-address-filter-by.sh\r
256 @@ -0,0 +1,73 @@\r
257 +#!/usr/bin/env bash\r
258 +test_description='duplicite address filtering in "notmuch address"'\r
259 +. ./test-lib.sh\r
260 +\r
261 +add_message '[to]="John Doe <foo@example.com>, John Doe <bar@example.com>"'\r
262 +add_message '[to]="\"Doe, John\" <foo@example.com>"' '[cc]="John Doe <Bar@Example.COM>"'\r
263 +add_message '[to]="\"Doe, John\" <foo@example.com>"' '[bcc]="John Doe <Bar@Example.COM>"'\r
264 +\r
265 +test_begin_subtest "--output=recipients"\r
266 +notmuch address --output=recipients "*" >OUTPUT\r
267 +cat <<EOF >EXPECTED\r
268 +John Doe <foo@example.com>\r
269 +John Doe <bar@example.com>\r
270 +"Doe, John" <foo@example.com>\r
271 +John Doe <Bar@Example.COM>\r
272 +EOF\r
273 +test_expect_equal_file OUTPUT EXPECTED\r
274 +\r
275 +test_begin_subtest "--output=recipients --filter-by=nameaddr"\r
276 +notmuch address --output=recipients --filter-by=nameaddr "*" >OUTPUT\r
277 +# The same as above\r
278 +cat <<EOF >EXPECTED\r
279 +John Doe <foo@example.com>\r
280 +John Doe <bar@example.com>\r
281 +"Doe, John" <foo@example.com>\r
282 +John Doe <Bar@Example.COM>\r
283 +EOF\r
284 +test_expect_equal_file OUTPUT EXPECTED\r
285 +\r
286 +test_begin_subtest "--output=recipients --filter-by=name"\r
287 +notmuch address --output=recipients --filter-by=name "*" >OUTPUT\r
288 +cat <<EOF >EXPECTED\r
289 +John Doe <foo@example.com>\r
290 +"Doe, John" <foo@example.com>\r
291 +EOF\r
292 +test_expect_equal_file OUTPUT EXPECTED\r
293 +\r
294 +test_begin_subtest "--output=recipients --filter-by=addr"\r
295 +notmuch address --output=recipients --filter-by=addr "*" >OUTPUT\r
296 +cat <<EOF >EXPECTED\r
297 +John Doe <foo@example.com>\r
298 +John Doe <bar@example.com>\r
299 +John Doe <Bar@Example.COM>\r
300 +EOF\r
301 +test_expect_equal_file OUTPUT EXPECTED\r
302 +\r
303 +test_begin_subtest "--output=recipients --filter-by=addrfold"\r
304 +notmuch address --output=recipients --filter-by=addrfold "*" >OUTPUT\r
305 +cat <<EOF >EXPECTED\r
306 +John Doe <foo@example.com>\r
307 +John Doe <bar@example.com>\r
308 +EOF\r
309 +test_expect_equal_file OUTPUT EXPECTED\r
310 +\r
311 +test_begin_subtest "--output=recipients --filter-by=nameaddrfold"\r
312 +notmuch address --output=recipients --filter-by=nameaddrfold "*" >OUTPUT\r
313 +cat <<EOF >EXPECTED\r
314 +John Doe <foo@example.com>\r
315 +John Doe <bar@example.com>\r
316 +"Doe, John" <foo@example.com>\r
317 +EOF\r
318 +test_expect_equal_file OUTPUT EXPECTED\r
319 +\r
320 +test_begin_subtest "--output=recipients --filter-by=nameaddrfold --output=count"\r
321 +notmuch address --output=recipients --filter-by=nameaddrfold --output=count "*" | sort -n >OUTPUT\r
322 +cat <<EOF >EXPECTED\r
323 +1      John Doe <foo@example.com>\r
324 +2      "Doe, John" <foo@example.com>\r
325 +3      John Doe <bar@example.com>\r
326 +EOF\r
327 +test_expect_equal_file OUTPUT EXPECTED\r
328 +\r
329 +test_done\r
330 -- \r
331 2.1.1\r
332 \r