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 7A90F431FCB
\r
6 for <notmuch@notmuchmail.org>; Sat, 24 Mar 2012 09:14:49 -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
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 3CJsC2RZQ6Ep for <notmuch@notmuchmail.org>;
\r
17 Sat, 24 Mar 2012 09:14:47 -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 4697B431FAF
\r
22 for <notmuch@notmuchmail.org>; Sat, 24 Mar 2012 09:14:47 -0700 (PDT)
\r
23 Received: by lahc1 with SMTP id c1so3315541lah.26
\r
24 for <notmuch@notmuchmail.org>; Sat, 24 Mar 2012 09:14:45 -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=LnlT8c0fIqQu8/YJQmbrdc7d0rlhF34c1krs3ax1FX8=;
\r
30 b=Gf3SVFEgT59h2GN4b7D2fNZQGYgMTAz3DNs4Z47Cii14eZQAN7GdVdrowF5ZVEIf6i
\r
31 4lG/7yGSL4i6QjvmfgL6h+W+KdY98ixhiS8F47AkJPC+1fDfhpL0PXu055H2bUQcnLCu
\r
32 cR5eyyJp/ec/GHuIGWR+RVkzOfbfE5u/3igSF5eLw/uaWSWhKp1gEyb2TeAUobytQaUH
\r
33 xLkaN6fMAeoVH1obfPT4yMMaLAZBy+6AMg2DBfIDDlZUaUcD4lokeEgVwC1tyWvokxDN
\r
34 yw+yuONVgeHi6EhgJOsjW53ChMWcjRhxzgYD2zTJmKfnB/ZqseGq9qXXSI11z93hP/VP
\r
36 Received: by 10.112.101.164 with SMTP id fh4mr5697827lbb.76.1332605685618;
\r
37 Sat, 24 Mar 2012 09:14:45 -0700 (PDT)
\r
38 Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi.
\r
40 by mx.google.com with ESMTPS id ph8sm11377376lab.15.2012.03.24.09.14.43
\r
41 (version=SSLv3 cipher=OTHER); Sat, 24 Mar 2012 09:14:44 -0700 (PDT)
\r
42 From: Jani Nikula <jani@nikula.org>
\r
43 To: notmuch@notmuchmail.org
\r
44 Subject: [PATCH 1/3] cli: refactor "notmuch tag" data structures for tagging
\r
46 Date: Sat, 24 Mar 2012 18:14:35 +0200
\r
48 <992ada5cb2fa5f89d4f38b10d62f1c178b3174e2.1332604895.git.jani@nikula.org>
\r
49 X-Mailer: git-send-email 1.7.5.4
\r
50 In-Reply-To: <cover.1332604895.git.jani@nikula.org>
\r
51 References: <cover.1332604895.git.jani@nikula.org>
\r
52 In-Reply-To: <cover.1332604895.git.jani@nikula.org>
\r
53 References: <cover.1332604895.git.jani@nikula.org>
\r
55 ALoCoQmpt7D/U6wwZn/eZP6pp6LrglNnCEajz2s2QvgLuc42Xzi3KIZviThzXTa5MrpGNSYG0LKI
\r
56 X-BeenThere: notmuch@notmuchmail.org
\r
57 X-Mailman-Version: 2.1.13
\r
59 List-Id: "Use and development of the notmuch mail system."
\r
60 <notmuch.notmuchmail.org>
\r
61 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
62 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
63 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
64 List-Post: <mailto:notmuch@notmuchmail.org>
\r
65 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
66 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
67 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
68 X-List-Received-Date: Sat, 24 Mar 2012 16:14:49 -0000
\r
70 To simplify code, keep all tagging operations in a single array
\r
71 instead of separate add and remove arrays. Apply tag changes in the
\r
72 order specified on the command line, instead of first removing and
\r
73 then adding the tags.
\r
75 This results in a minor functional change: If a tag is both added and
\r
76 removed, the last specified operation is now used. Previously the tag
\r
79 Signed-off-by: Jani Nikula <jani@nikula.org>
\r
81 notmuch-tag.c | 80 +++++++++++++++++++++++++-------------------------------
\r
82 1 files changed, 36 insertions(+), 44 deletions(-)
\r
84 diff --git a/notmuch-tag.c b/notmuch-tag.c
\r
85 index 36b9b09..98b2126 100644
\r
88 @@ -53,10 +53,14 @@ _escape_tag (char *buf, const char *tag)
\r
94 + notmuch_bool_t remove;
\r
98 -_optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],
\r
99 - int *add_tags, int add_tags_count,
\r
100 - int *remove_tags, int remove_tags_count)
\r
101 +_optimize_tag_query (void *ctx, const char *orig_query_string,
\r
102 + const tag_operation_t *tag_ops)
\r
104 /* This is subtler than it looks. Xapian ignores the '-' operator
\r
105 * at the beginning both queries and parenthesized groups and,
\r
106 @@ -74,12 +78,9 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],
\r
107 /* Allocate a buffer for escaping tags. This is large enough to
\r
108 * hold a fully escaped tag with every character doubled plus
\r
109 * enclosing quotes and a NUL. */
\r
110 - for (i = 0; i < add_tags_count; i++)
\r
111 - if (strlen (argv[add_tags[i]] + 1) > max_tag_len)
\r
112 - max_tag_len = strlen (argv[add_tags[i]] + 1);
\r
113 - for (i = 0; i < remove_tags_count; i++)
\r
114 - if (strlen (argv[remove_tags[i]] + 1) > max_tag_len)
\r
115 - max_tag_len = strlen (argv[remove_tags[i]] + 1);
\r
116 + for (i = 0; tag_ops[i].tag; i++)
\r
117 + if (strlen (tag_ops[i].tag) > max_tag_len)
\r
118 + max_tag_len = strlen (tag_ops[i].tag);
\r
119 escaped = talloc_array(ctx, char, max_tag_len * 2 + 3);
\r
122 @@ -90,16 +91,11 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],
\r
124 query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);
\r
126 - for (i = 0; i < add_tags_count && query_string; i++) {
\r
127 - query_string = talloc_asprintf_append_buffer (
\r
128 - query_string, "%snot tag:%s", join,
\r
129 - _escape_tag (escaped, argv[add_tags[i]] + 1));
\r
132 - for (i = 0; i < remove_tags_count && query_string; i++) {
\r
133 + for (i = 0; tag_ops[i].tag && query_string; i++) {
\r
134 query_string = talloc_asprintf_append_buffer (
\r
135 - query_string, "%stag:%s", join,
\r
136 - _escape_tag (escaped, argv[remove_tags[i]] + 1));
\r
137 + query_string, "%s%stag:%s", join,
\r
138 + tag_ops[i].remove ? "" : "not ",
\r
139 + _escape_tag (escaped, tag_ops[i].tag));
\r
143 @@ -113,9 +109,8 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],
\r
145 notmuch_tag_command (void *ctx, int argc, char *argv[])
\r
147 - int *add_tags, *remove_tags;
\r
148 - int add_tags_count = 0;
\r
149 - int remove_tags_count = 0;
\r
150 + tag_operation_t *tag_ops;
\r
151 + int tag_ops_count = 0;
\r
152 char *query_string;
\r
153 notmuch_config_t *config;
\r
154 notmuch_database_t *notmuch;
\r
155 @@ -133,35 +128,34 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
\r
156 action.sa_flags = SA_RESTART;
\r
157 sigaction (SIGINT, &action, NULL);
\r
159 - add_tags = talloc_size (ctx, argc * sizeof (int));
\r
160 - if (add_tags == NULL) {
\r
161 - fprintf (stderr, "Out of memory.\n");
\r
164 + argc--; argv++; /* skip subcommand argument */
\r
166 - remove_tags = talloc_size (ctx, argc * sizeof (int));
\r
167 - if (remove_tags == NULL) {
\r
168 + /* Array of tagging operations (add or remove), terminated with an
\r
169 + * empty element. */
\r
170 + tag_ops = talloc_array (ctx, tag_operation_t, argc + 1);
\r
171 + if (tag_ops == NULL) {
\r
172 fprintf (stderr, "Out of memory.\n");
\r
176 - argc--; argv++; /* skip subcommand argument */
\r
178 for (i = 0; i < argc; i++) {
\r
179 if (strcmp (argv[i], "--") == 0) {
\r
183 - if (argv[i][0] == '+') {
\r
184 - add_tags[add_tags_count++] = i;
\r
185 - } else if (argv[i][0] == '-') {
\r
186 - remove_tags[remove_tags_count++] = i;
\r
187 + if (argv[i][0] == '+' || argv[i][0] == '-') {
\r
188 + tag_ops[tag_ops_count++] = (tag_operation_t) {
\r
189 + .tag = argv[i] + 1,
\r
190 + .remove = argv[i][0] == '-',
\r
197 - if (add_tags_count == 0 && remove_tags_count == 0) {
\r
198 + tag_ops[tag_ops_count].tag = NULL;
\r
200 + if (tag_ops_count == 0) {
\r
201 fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
\r
204 @@ -175,9 +169,7 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
\r
206 /* Optimize the query so it excludes messages that already have
\r
207 * the specified set of tags. */
\r
208 - query_string = _optimize_tag_query (ctx, query_string, argv,
\r
209 - add_tags, add_tags_count,
\r
210 - remove_tags, remove_tags_count);
\r
211 + query_string = _optimize_tag_query (ctx, query_string, tag_ops);
\r
212 if (query_string == NULL) {
\r
213 fprintf (stderr, "Out of memory.\n");
\r
215 @@ -211,12 +203,12 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
\r
217 notmuch_message_freeze (message);
\r
219 - for (i = 0; i < remove_tags_count; i++)
\r
220 - notmuch_message_remove_tag (message,
\r
221 - argv[remove_tags[i]] + 1);
\r
223 - for (i = 0; i < add_tags_count; i++)
\r
224 - notmuch_message_add_tag (message, argv[add_tags[i]] + 1);
\r
225 + for (i = 0; tag_ops[i].tag; i++) {
\r
226 + if (tag_ops[i].remove)
\r
227 + notmuch_message_remove_tag (message, tag_ops[i].tag);
\r
229 + notmuch_message_add_tag (message, tag_ops[i].tag);
\r
232 notmuch_message_thaw (message);
\r