Re: [PATCH 9/9] add has: query prefix to search for specific properties
[notmuch-archives.git] / 13 / 20e1ff2c0a543910791c42b7b5bf98dd03551a
1 Return-Path: <tomi.ollila@iki.fi>\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 3CA5F431FAF\r
6         for <notmuch@notmuchmail.org>; Sun, 25 Mar 2012 13:45:42 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         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 e-2IhMkHqoPm for <notmuch@notmuchmail.org>;\r
16         Sun, 25 Mar 2012 13:45:41 -0700 (PDT)\r
17 Received: from guru.guru-group.fi (guru-group.fi [87.108.86.66])\r
18         by olra.theworths.org (Postfix) with ESMTP id 317EB431FAE\r
19         for <notmuch@notmuchmail.org>; Sun, 25 Mar 2012 13:45:41 -0700 (PDT)\r
20 Received: by guru.guru-group.fi (Postfix, from userid 501)\r
21         id CD4E668055; Sun, 25 Mar 2012 23:45:39 +0300 (EEST)\r
22 From: Tomi Ollila <tomi.ollila@iki.fi>\r
23 To: Jani Nikula <jani@nikula.org>, notmuch@notmuchmail.org\r
24 Subject: Re: [PATCH v2 2/3] cli: refactor "notmuch tag" query tagging into a\r
25         separate function\r
26 In-Reply-To:\r
27  <584067da00dd9f04a7f3982b76fd4b4918e4fe61.1332702915.git.jani@nikula.org>\r
28 References: <cover.1332702915.git.jani@nikula.org>\r
29         <584067da00dd9f04a7f3982b76fd4b4918e4fe61.1332702915.git.jani@nikula.org>User-Agent:    Notmuch/0.12+71~gdacf76b (http://notmuchmail.org) Emacs/23.3.1\r
30         (x86_64-unknown-linux-gnu)\r
31 X-Face: HhBM'cA~<r"^Xv\KRN0P{vn'Y"Kd;zg_y3S[4)KSN~s?O\"QPoL\r
32         $[Xv_BD:i/F$WiEWax}R(MPS`^UaptOGD`*/=@\1lKoVa9tnrg0TW?"r7aRtgk[F\r
33         !)g;OY^,BjTbr)Np:%c_o'jj,Z\r
34 Date: Sun, 25 Mar 2012 23:45:39 +0300\r
35 Message-ID: <m262dse7f0.fsf@guru.guru-group.fi>\r
36 MIME-Version: 1.0\r
37 Content-Type: text/plain; charset=us-ascii\r
38 X-BeenThere: notmuch@notmuchmail.org\r
39 X-Mailman-Version: 2.1.13\r
40 Precedence: list\r
41 List-Id: "Use and development of the notmuch mail system."\r
42         <notmuch.notmuchmail.org>\r
43 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
44         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
45 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
46 List-Post: <mailto:notmuch@notmuchmail.org>\r
47 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
48 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
50 X-List-Received-Date: Sun, 25 Mar 2012 20:45:42 -0000\r
51 \r
52 Jani Nikula <jani@nikula.org> writes:\r
53 \r
54 > Refactor to make tagging code easier to reuse in the future. No\r
55 > functional changes.\r
56 >\r
57 > Signed-off-by: Jani Nikula <jani@nikula.org>\r
58 > ---\r
59 \r
60 tag_query() is pretty generic name for a function which (also) does\r
61 tagging operations (adds and removes tags based on tag_ops).\r
62 \r
63 If, however, tag_opts != NULL (as is needs to be) but tag_opts[0] == NULL\r
64 (now tag_query() would not do any tagging operations, but\r
65 optimize_tag_query would mangle query string ( '*' to '()' and \r
66 'any_other' to '( any_other ) and ()' )\r
67 \r
68 I.e. IMO the function name should be more spesific & the fact that this\r
69 function needs tag changing data in tag_ops array should be documented.\r
70 \r
71 ---\r
72 \r
73 Minor thing in patch #1:\r
74 \r
75  +          tag_ops[tag_ops_count].remove = argv[i][0] == '-';\r
76 \r
77 would be more clearer as:\r
78 \r
79  +          tag_ops[tag_ops_count].remove = (argv[i][0] == '-');\r
80 \r
81 \r
82 \r
83 Everything else LGTM.\r
84 \r
85 Tomi\r
86 \r
87 \r
88 >  notmuch-tag.c |  101 ++++++++++++++++++++++++++++++++-------------------------\r
89 >  1 files changed, 57 insertions(+), 44 deletions(-)\r
90 >\r
91 > diff --git a/notmuch-tag.c b/notmuch-tag.c\r
92 > index c39b235..edbfdec 100644\r
93 > --- a/notmuch-tag.c\r
94 > +++ b/notmuch-tag.c\r
95 > @@ -106,6 +106,60 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
96 >      return query_string;\r
97 >  }\r
98 >  \r
99 > +static int\r
100 > +tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,\r
101 > +        tag_operation_t *tag_ops, notmuch_bool_t synchronize_flags)\r
102 > +{\r
103 > +    notmuch_query_t *query;\r
104 > +    notmuch_messages_t *messages;\r
105 > +    notmuch_message_t *message;\r
106 > +    int i;\r
107 > +\r
108 > +    /* Optimize the query so it excludes messages that already have\r
109 > +     * the specified set of tags. */\r
110 > +    query_string = _optimize_tag_query (ctx, query_string, tag_ops);\r
111 > +    if (query_string == NULL) {\r
112 > +     fprintf (stderr, "Out of memory.\n");\r
113 > +     return 1;\r
114 > +    }\r
115 > +\r
116 > +    query = notmuch_query_create (notmuch, query_string);\r
117 > +    if (query == NULL) {\r
118 > +     fprintf (stderr, "Out of memory.\n");\r
119 > +     return 1;\r
120 > +    }\r
121 > +\r
122 > +    /* tagging is not interested in any special sort order */\r
123 > +    notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);\r
124 > +\r
125 > +    for (messages = notmuch_query_search_messages (query);\r
126 > +      notmuch_messages_valid (messages) && !interrupted;\r
127 > +      notmuch_messages_move_to_next (messages))\r
128 > +    {\r
129 > +     message = notmuch_messages_get (messages);\r
130 > +\r
131 > +     notmuch_message_freeze (message);\r
132 > +\r
133 > +     for (i = 0; tag_ops[i].tag; i++) {\r
134 > +         if (tag_ops[i].remove)\r
135 > +             notmuch_message_remove_tag (message, tag_ops[i].tag);\r
136 > +         else\r
137 > +             notmuch_message_add_tag (message, tag_ops[i].tag);\r
138 > +     }\r
139 > +\r
140 > +     notmuch_message_thaw (message);\r
141 > +\r
142 > +     if (synchronize_flags)\r
143 > +         notmuch_message_tags_to_maildir_flags (message);\r
144 > +\r
145 > +     notmuch_message_destroy (message);\r
146 > +    }\r
147 > +\r
148 > +    notmuch_query_destroy (query);\r
149 > +\r
150 > +    return interrupted;\r
151 > +}\r
152 > +\r
153 >  int\r
154 >  notmuch_tag_command (void *ctx, int argc, char *argv[])\r
155 >  {\r
156 > @@ -114,12 +168,10 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
157 >      char *query_string;\r
158 >      notmuch_config_t *config;\r
159 >      notmuch_database_t *notmuch;\r
160 > -    notmuch_query_t *query;\r
161 > -    notmuch_messages_t *messages;\r
162 > -    notmuch_message_t *message;\r
163 >      struct sigaction action;\r
164 >      notmuch_bool_t synchronize_flags;\r
165 >      int i;\r
166 > +    int ret;\r
167 >  \r
168 >      /* Setup our handler for SIGINT */\r
169 >      memset (&action, 0, sizeof (struct sigaction));\r
170 > @@ -166,14 +218,6 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
171 >       return 1;\r
172 >      }\r
173 >  \r
174 > -    /* Optimize the query so it excludes messages that already have\r
175 > -     * the specified set of tags. */\r
176 > -    query_string = _optimize_tag_query (ctx, query_string, tag_ops);\r
177 > -    if (query_string == NULL) {\r
178 > -     fprintf (stderr, "Out of memory.\n");\r
179 > -     return 1;\r
180 > -    }\r
181 > -\r
182 >      config = notmuch_config_open (ctx, NULL, NULL);\r
183 >      if (config == NULL)\r
184 >       return 1;\r
185 > @@ -185,40 +229,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
186 >  \r
187 >      synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
188 >  \r
189 > -    query = notmuch_query_create (notmuch, query_string);\r
190 > -    if (query == NULL) {\r
191 > -     fprintf (stderr, "Out of memory.\n");\r
192 > -     return 1;\r
193 > -    }\r
194 > -\r
195 > -    /* tagging is not interested in any special sort order */\r
196 > -    notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);\r
197 > +    ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags);\r
198 >  \r
199 > -    for (messages = notmuch_query_search_messages (query);\r
200 > -      notmuch_messages_valid (messages) && !interrupted;\r
201 > -      notmuch_messages_move_to_next (messages))\r
202 > -    {\r
203 > -     message = notmuch_messages_get (messages);\r
204 > -\r
205 > -     notmuch_message_freeze (message);\r
206 > -\r
207 > -     for (i = 0; tag_ops[i].tag; i++) {\r
208 > -         if (tag_ops[i].remove)\r
209 > -             notmuch_message_remove_tag (message, tag_ops[i].tag);\r
210 > -         else\r
211 > -             notmuch_message_add_tag (message, tag_ops[i].tag);\r
212 > -     }\r
213 > -\r
214 > -     notmuch_message_thaw (message);\r
215 > -\r
216 > -     if (synchronize_flags)\r
217 > -         notmuch_message_tags_to_maildir_flags (message);\r
218 > -\r
219 > -     notmuch_message_destroy (message);\r
220 > -    }\r
221 > -\r
222 > -    notmuch_query_destroy (query);\r
223 >      notmuch_database_close (notmuch);\r
224 >  \r
225 > -    return interrupted;\r
226 > +    return ret;\r
227 >  }\r
228 > -- \r
229 > 1.7.5.4\r
230 >\r
231 > _______________________________________________\r
232 > notmuch mailing list\r
233 > notmuch@notmuchmail.org\r
234 > http://notmuchmail.org/mailman/listinfo/notmuch\r