Re: [PATCH v3 0/6] Make Emacs search use sexp format
[notmuch-archives.git] / 68 / 66a6db9dbab15366add94c75c20fb01b099ded
1 Return-Path: <jani@nikula.org>\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 39D2C431FAF\r
6         for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:28 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id LTwm1bsS6x2K for <notmuch@notmuchmail.org>;\r
17         Mon, 26 Mar 2012 14:04:26 -0700 (PDT)\r
18 Received: from mail-lpp01m010-f53.google.com (mail-lpp01m010-f53.google.com\r
19         [209.85.215.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 72B28431FBC\r
22         for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:23 -0700 (PDT)\r
23 Received: by mail-lpp01m010-f53.google.com with SMTP id c1so4613102lah.26\r
24         for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:23 -0700 (PDT)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=google.com; s=20120113;\r
27         h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
28         :in-reply-to:references:x-gm-message-state;\r
29         bh=JqaxQTOi2ByoMcyg6nVONkuJ3Xs/LSZejHUB41EedAA=;\r
30         b=e6e0HxQQVQ47/n7McM9oKUK1r/3q/C8Yl/HGtO3jkLnQx18ucei/Ljbj9VftukmZhQ\r
31         5ShQl/NM/j4b9ch8scJnWrss5IxqmcAL7W8b2DpBDuP0IAJoLcg809t2X2prH08/7ohz\r
32         D5OWzbz8ucCl8N0MrZ4a6yUbIgQOpfqs+2L/Ll6QBqeX30wNcE5fF6D29N/mlbitJcrC\r
33         78j5FPC8aR/JznT0ysS6Drp3TtBC0FCLtBbI/h/034uV5FcqORKRsqcN25/SiBOv3HgP\r
34         ibTiIDy9Xw/As9gvfhGs/KBxJMIfBFyeclEYWpwEKfmNZe3mplh4jaX6qcbBN5VErEQv\r
35         q//A==\r
36 Received: by 10.112.41.169 with SMTP id g9mr8328809lbl.59.1332795862941;\r
37         Mon, 26 Mar 2012 14:04:22 -0700 (PDT)\r
38 Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi.\r
39         [84.248.80.253])\r
40         by mx.google.com with ESMTPS id je9sm18172312lab.9.2012.03.26.14.04.20\r
41         (version=SSLv3 cipher=OTHER); Mon, 26 Mar 2012 14:04:21 -0700 (PDT)\r
42 From: Jani Nikula <jani@nikula.org>\r
43 To: notmuch@notmuchmail.org\r
44 Subject: [PATCH v3 2/4] cli: refactor "notmuch tag" data structures for\r
45         tagging operations\r
46 Date: Tue, 27 Mar 2012 00:04:09 +0300\r
47 Message-Id:\r
48  <dc6a35c80e3d56fabebf3f577470f0f015377204.1332795415.git.jani@nikula.org>\r
49 X-Mailer: git-send-email 1.7.5.4\r
50 In-Reply-To: <cover.1332795415.git.jani@nikula.org>\r
51 References: <cover.1332795415.git.jani@nikula.org>\r
52 In-Reply-To: <cover.1332795415.git.jani@nikula.org>\r
53 References: <cover.1332795415.git.jani@nikula.org>\r
54 X-Gm-Message-State:\r
55  ALoCoQkagyunwmzOlMjbHpsMaloVwSw9Cprvk76Cj5gNGzKKILyoFZIqfmeags1EX0n1OvrrMJHM\r
56 Cc: tomi.ollila@iki.fi\r
57 X-BeenThere: notmuch@notmuchmail.org\r
58 X-Mailman-Version: 2.1.13\r
59 Precedence: list\r
60 List-Id: "Use and development of the notmuch mail system."\r
61         <notmuch.notmuchmail.org>\r
62 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
63         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
64 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
65 List-Post: <mailto:notmuch@notmuchmail.org>\r
66 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
67 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
68         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
69 X-List-Received-Date: Mon, 26 Mar 2012 21:04:28 -0000\r
70 \r
71 To simplify code, keep all tagging operations in a single array\r
72 instead of separate add and remove arrays. Apply tag changes in the\r
73 order specified on the command line, instead of first removing and\r
74 then adding the tags.\r
75 \r
76 This results in a minor functional change: If a tag is both added and\r
77 removed, the last specified operation is now used. Previously the tag\r
78 was always added. Change the relevant test to reflect the new\r
79 behaviour.\r
80 \r
81 Signed-off-by: Jani Nikula <jani@nikula.org>\r
82 ---\r
83  notmuch-tag.c |   83 ++++++++++++++++++++++++++------------------------------\r
84  test/tagging  |    2 +-\r
85  2 files changed, 40 insertions(+), 45 deletions(-)\r
86 \r
87 diff --git a/notmuch-tag.c b/notmuch-tag.c\r
88 index 36b9b09..0a6b140 100644\r
89 --- a/notmuch-tag.c\r
90 +++ b/notmuch-tag.c\r
91 @@ -53,10 +53,14 @@ _escape_tag (char *buf, const char *tag)\r
92      return buf;\r
93  }\r
94  \r
95 +typedef struct {\r
96 +    const char *tag;\r
97 +    notmuch_bool_t remove;\r
98 +} tag_operation_t;\r
99 +\r
100  static char *\r
101 -_optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
102 -                    int *add_tags, int add_tags_count,\r
103 -                    int *remove_tags, int remove_tags_count)\r
104 +_optimize_tag_query (void *ctx, const char *orig_query_string,\r
105 +                    const tag_operation_t *tag_ops)\r
106  {\r
107      /* This is subtler than it looks.  Xapian ignores the '-' operator\r
108       * at the beginning both queries and parenthesized groups and,\r
109 @@ -71,15 +75,16 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
110      int i;\r
111      unsigned int max_tag_len = 0;\r
112  \r
113 +    /* Don't optimize if there are no tag changes. */\r
114 +    if (tag_ops[0].tag == NULL)\r
115 +       return talloc_strdup (ctx, orig_query_string);\r
116 +\r
117      /* Allocate a buffer for escaping tags.  This is large enough to\r
118       * hold a fully escaped tag with every character doubled plus\r
119       * enclosing quotes and a NUL. */\r
120 -    for (i = 0; i < add_tags_count; i++)\r
121 -       if (strlen (argv[add_tags[i]] + 1) > max_tag_len)\r
122 -           max_tag_len = strlen (argv[add_tags[i]] + 1);\r
123 -    for (i = 0; i < remove_tags_count; i++)\r
124 -       if (strlen (argv[remove_tags[i]] + 1) > max_tag_len)\r
125 -           max_tag_len = strlen (argv[remove_tags[i]] + 1);\r
126 +    for (i = 0; tag_ops[i].tag; i++)\r
127 +       if (strlen (tag_ops[i].tag) > max_tag_len)\r
128 +           max_tag_len = strlen (tag_ops[i].tag);\r
129      escaped = talloc_array(ctx, char, max_tag_len * 2 + 3);\r
130      if (!escaped)\r
131         return NULL;\r
132 @@ -90,16 +95,11 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
133      else\r
134         query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);\r
135  \r
136 -    for (i = 0; i < add_tags_count && query_string; i++) {\r
137 +    for (i = 0; tag_ops[i].tag && query_string; i++) {\r
138         query_string = talloc_asprintf_append_buffer (\r
139 -           query_string, "%snot tag:%s", join,\r
140 -           _escape_tag (escaped, argv[add_tags[i]] + 1));\r
141 -       join = " or ";\r
142 -    }\r
143 -    for (i = 0; i < remove_tags_count && query_string; i++) {\r
144 -       query_string = talloc_asprintf_append_buffer (\r
145 -           query_string, "%stag:%s", join,\r
146 -           _escape_tag (escaped, argv[remove_tags[i]] + 1));\r
147 +           query_string, "%s%stag:%s", join,\r
148 +           tag_ops[i].remove ? "" : "not ",\r
149 +           _escape_tag (escaped, tag_ops[i].tag));\r
150         join = " or ";\r
151      }\r
152  \r
153 @@ -113,9 +113,8 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
154  int\r
155  notmuch_tag_command (void *ctx, int argc, char *argv[])\r
156  {\r
157 -    int *add_tags, *remove_tags;\r
158 -    int add_tags_count = 0;\r
159 -    int remove_tags_count = 0;\r
160 +    tag_operation_t *tag_ops;\r
161 +    int tag_ops_count = 0;\r
162      char *query_string;\r
163      notmuch_config_t *config;\r
164      notmuch_database_t *notmuch;\r
165 @@ -133,35 +132,33 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
166      action.sa_flags = SA_RESTART;\r
167      sigaction (SIGINT, &action, NULL);\r
168  \r
169 -    add_tags = talloc_size (ctx, argc * sizeof (int));\r
170 -    if (add_tags == NULL) {\r
171 -       fprintf (stderr, "Out of memory.\n");\r
172 -       return 1;\r
173 -    }\r
174 +    argc--; argv++; /* skip subcommand argument */\r
175  \r
176 -    remove_tags = talloc_size (ctx, argc * sizeof (int));\r
177 -    if (remove_tags == NULL) {\r
178 +    /* Array of tagging operations (add or remove), terminated with an\r
179 +     * empty element. */\r
180 +    tag_ops = talloc_array (ctx, tag_operation_t, argc + 1);\r
181 +    if (tag_ops == NULL) {\r
182         fprintf (stderr, "Out of memory.\n");\r
183         return 1;\r
184      }\r
185  \r
186 -    argc--; argv++; /* skip subcommand argument */\r
187 -\r
188      for (i = 0; i < argc; i++) {\r
189         if (strcmp (argv[i], "--") == 0) {\r
190             i++;\r
191             break;\r
192         }\r
193 -       if (argv[i][0] == '+') {\r
194 -           add_tags[add_tags_count++] = i;\r
195 -       } else if (argv[i][0] == '-') {\r
196 -           remove_tags[remove_tags_count++] = i;\r
197 +       if (argv[i][0] == '+' || argv[i][0] == '-') {\r
198 +           tag_ops[tag_ops_count].tag = argv[i] + 1;\r
199 +           tag_ops[tag_ops_count].remove = (argv[i][0] == '-');\r
200 +           tag_ops_count++;\r
201         } else {\r
202             break;\r
203         }\r
204      }\r
205  \r
206 -    if (add_tags_count == 0 && remove_tags_count == 0) {\r
207 +    tag_ops[tag_ops_count].tag = NULL;\r
208 +\r
209 +    if (tag_ops_count == 0) {\r
210         fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");\r
211         return 1;\r
212      }\r
213 @@ -175,9 +172,7 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
214  \r
215      /* Optimize the query so it excludes messages that already have\r
216       * the specified set of tags. */\r
217 -    query_string = _optimize_tag_query (ctx, query_string, argv,\r
218 -                                       add_tags, add_tags_count,\r
219 -                                       remove_tags, remove_tags_count);\r
220 +    query_string = _optimize_tag_query (ctx, query_string, tag_ops);\r
221      if (query_string == NULL) {\r
222         fprintf (stderr, "Out of memory.\n");\r
223         return 1;\r
224 @@ -211,12 +206,12 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
225  \r
226         notmuch_message_freeze (message);\r
227  \r
228 -       for (i = 0; i < remove_tags_count; i++)\r
229 -           notmuch_message_remove_tag (message,\r
230 -                                       argv[remove_tags[i]] + 1);\r
231 -\r
232 -       for (i = 0; i < add_tags_count; i++)\r
233 -           notmuch_message_add_tag (message, argv[add_tags[i]] + 1);\r
234 +       for (i = 0; tag_ops[i].tag; i++) {\r
235 +           if (tag_ops[i].remove)\r
236 +               notmuch_message_remove_tag (message, tag_ops[i].tag);\r
237 +           else\r
238 +               notmuch_message_add_tag (message, tag_ops[i].tag);\r
239 +       }\r
240  \r
241         notmuch_message_thaw (message);\r
242  \r
243 diff --git a/test/tagging b/test/tagging\r
244 index 3acf1bc..e4782ed 100755\r
245 --- a/test/tagging\r
246 +++ b/test/tagging\r
247 @@ -43,7 +43,7 @@ notmuch tag +tag4 -tag4 One\r
248  notmuch tag -tag4 +tag4 Two\r
249  output=$(notmuch search \* | notmuch_search_sanitize)\r
250  test_expect_equal "$output" "\\r
251 -thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (:\"  inbox tag1 tag4 unread)\r
252 +thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (:\"  inbox tag1 unread)\r
253  thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag1 tag4 unread)"\r
254  \r
255  test_done\r
256 -- \r
257 1.7.5.4\r
258 \r