--- /dev/null
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 39D2C431FAF\r
+ for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:28 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id LTwm1bsS6x2K for <notmuch@notmuchmail.org>;\r
+ Mon, 26 Mar 2012 14:04:26 -0700 (PDT)\r
+Received: from mail-lpp01m010-f53.google.com (mail-lpp01m010-f53.google.com\r
+ [209.85.215.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 72B28431FBC\r
+ for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:23 -0700 (PDT)\r
+Received: by mail-lpp01m010-f53.google.com with SMTP id c1so4613102lah.26\r
+ for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:23 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=google.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
+ :in-reply-to:references:x-gm-message-state;\r
+ bh=JqaxQTOi2ByoMcyg6nVONkuJ3Xs/LSZejHUB41EedAA=;\r
+ b=e6e0HxQQVQ47/n7McM9oKUK1r/3q/C8Yl/HGtO3jkLnQx18ucei/Ljbj9VftukmZhQ\r
+ 5ShQl/NM/j4b9ch8scJnWrss5IxqmcAL7W8b2DpBDuP0IAJoLcg809t2X2prH08/7ohz\r
+ D5OWzbz8ucCl8N0MrZ4a6yUbIgQOpfqs+2L/Ll6QBqeX30wNcE5fF6D29N/mlbitJcrC\r
+ 78j5FPC8aR/JznT0ysS6Drp3TtBC0FCLtBbI/h/034uV5FcqORKRsqcN25/SiBOv3HgP\r
+ ibTiIDy9Xw/As9gvfhGs/KBxJMIfBFyeclEYWpwEKfmNZe3mplh4jaX6qcbBN5VErEQv\r
+ q//A==\r
+Received: by 10.112.41.169 with SMTP id g9mr8328809lbl.59.1332795862941;\r
+ Mon, 26 Mar 2012 14:04:22 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi.\r
+ [84.248.80.253])\r
+ by mx.google.com with ESMTPS id je9sm18172312lab.9.2012.03.26.14.04.20\r
+ (version=SSLv3 cipher=OTHER); Mon, 26 Mar 2012 14:04:21 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 2/4] cli: refactor "notmuch tag" data structures for\r
+ tagging operations\r
+Date: Tue, 27 Mar 2012 00:04:09 +0300\r
+Message-Id:\r
+ <dc6a35c80e3d56fabebf3f577470f0f015377204.1332795415.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <cover.1332795415.git.jani@nikula.org>\r
+References: <cover.1332795415.git.jani@nikula.org>\r
+In-Reply-To: <cover.1332795415.git.jani@nikula.org>\r
+References: <cover.1332795415.git.jani@nikula.org>\r
+X-Gm-Message-State:\r
+ ALoCoQkagyunwmzOlMjbHpsMaloVwSw9Cprvk76Cj5gNGzKKILyoFZIqfmeags1EX0n1OvrrMJHM\r
+Cc: tomi.ollila@iki.fi\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 26 Mar 2012 21:04:28 -0000\r
+\r
+To simplify code, keep all tagging operations in a single array\r
+instead of separate add and remove arrays. Apply tag changes in the\r
+order specified on the command line, instead of first removing and\r
+then adding the tags.\r
+\r
+This results in a minor functional change: If a tag is both added and\r
+removed, the last specified operation is now used. Previously the tag\r
+was always added. Change the relevant test to reflect the new\r
+behaviour.\r
+\r
+Signed-off-by: Jani Nikula <jani@nikula.org>\r
+---\r
+ notmuch-tag.c | 83 ++++++++++++++++++++++++++------------------------------\r
+ test/tagging | 2 +-\r
+ 2 files changed, 40 insertions(+), 45 deletions(-)\r
+\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index 36b9b09..0a6b140 100644\r
+--- a/notmuch-tag.c\r
++++ b/notmuch-tag.c\r
+@@ -53,10 +53,14 @@ _escape_tag (char *buf, const char *tag)\r
+ return buf;\r
+ }\r
+ \r
++typedef struct {\r
++ const char *tag;\r
++ notmuch_bool_t remove;\r
++} tag_operation_t;\r
++\r
+ static char *\r
+-_optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
+- int *add_tags, int add_tags_count,\r
+- int *remove_tags, int remove_tags_count)\r
++_optimize_tag_query (void *ctx, const char *orig_query_string,\r
++ const tag_operation_t *tag_ops)\r
+ {\r
+ /* This is subtler than it looks. Xapian ignores the '-' operator\r
+ * at the beginning both queries and parenthesized groups and,\r
+@@ -71,15 +75,16 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
+ int i;\r
+ unsigned int max_tag_len = 0;\r
+ \r
++ /* Don't optimize if there are no tag changes. */\r
++ if (tag_ops[0].tag == NULL)\r
++ return talloc_strdup (ctx, orig_query_string);\r
++\r
+ /* Allocate a buffer for escaping tags. This is large enough to\r
+ * hold a fully escaped tag with every character doubled plus\r
+ * enclosing quotes and a NUL. */\r
+- for (i = 0; i < add_tags_count; i++)\r
+- if (strlen (argv[add_tags[i]] + 1) > max_tag_len)\r
+- max_tag_len = strlen (argv[add_tags[i]] + 1);\r
+- for (i = 0; i < remove_tags_count; i++)\r
+- if (strlen (argv[remove_tags[i]] + 1) > max_tag_len)\r
+- max_tag_len = strlen (argv[remove_tags[i]] + 1);\r
++ for (i = 0; tag_ops[i].tag; i++)\r
++ if (strlen (tag_ops[i].tag) > max_tag_len)\r
++ max_tag_len = strlen (tag_ops[i].tag);\r
+ escaped = talloc_array(ctx, char, max_tag_len * 2 + 3);\r
+ if (!escaped)\r
+ return NULL;\r
+@@ -90,16 +95,11 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
+ else\r
+ query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);\r
+ \r
+- for (i = 0; i < add_tags_count && query_string; i++) {\r
++ for (i = 0; tag_ops[i].tag && query_string; i++) {\r
+ query_string = talloc_asprintf_append_buffer (\r
+- query_string, "%snot tag:%s", join,\r
+- _escape_tag (escaped, argv[add_tags[i]] + 1));\r
+- join = " or ";\r
+- }\r
+- for (i = 0; i < remove_tags_count && query_string; i++) {\r
+- query_string = talloc_asprintf_append_buffer (\r
+- query_string, "%stag:%s", join,\r
+- _escape_tag (escaped, argv[remove_tags[i]] + 1));\r
++ query_string, "%s%stag:%s", join,\r
++ tag_ops[i].remove ? "" : "not ",\r
++ _escape_tag (escaped, tag_ops[i].tag));\r
+ join = " or ";\r
+ }\r
+ \r
+@@ -113,9 +113,8 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
+ int\r
+ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ {\r
+- int *add_tags, *remove_tags;\r
+- int add_tags_count = 0;\r
+- int remove_tags_count = 0;\r
++ tag_operation_t *tag_ops;\r
++ int tag_ops_count = 0;\r
+ char *query_string;\r
+ notmuch_config_t *config;\r
+ notmuch_database_t *notmuch;\r
+@@ -133,35 +132,33 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ action.sa_flags = SA_RESTART;\r
+ sigaction (SIGINT, &action, NULL);\r
+ \r
+- add_tags = talloc_size (ctx, argc * sizeof (int));\r
+- if (add_tags == NULL) {\r
+- fprintf (stderr, "Out of memory.\n");\r
+- return 1;\r
+- }\r
++ argc--; argv++; /* skip subcommand argument */\r
+ \r
+- remove_tags = talloc_size (ctx, argc * sizeof (int));\r
+- if (remove_tags == NULL) {\r
++ /* Array of tagging operations (add or remove), terminated with an\r
++ * empty element. */\r
++ tag_ops = talloc_array (ctx, tag_operation_t, argc + 1);\r
++ if (tag_ops == NULL) {\r
+ fprintf (stderr, "Out of memory.\n");\r
+ return 1;\r
+ }\r
+ \r
+- argc--; argv++; /* skip subcommand argument */\r
+-\r
+ for (i = 0; i < argc; i++) {\r
+ if (strcmp (argv[i], "--") == 0) {\r
+ i++;\r
+ break;\r
+ }\r
+- if (argv[i][0] == '+') {\r
+- add_tags[add_tags_count++] = i;\r
+- } else if (argv[i][0] == '-') {\r
+- remove_tags[remove_tags_count++] = i;\r
++ if (argv[i][0] == '+' || argv[i][0] == '-') {\r
++ tag_ops[tag_ops_count].tag = argv[i] + 1;\r
++ tag_ops[tag_ops_count].remove = (argv[i][0] == '-');\r
++ tag_ops_count++;\r
+ } else {\r
+ break;\r
+ }\r
+ }\r
+ \r
+- if (add_tags_count == 0 && remove_tags_count == 0) {\r
++ tag_ops[tag_ops_count].tag = NULL;\r
++\r
++ if (tag_ops_count == 0) {\r
+ fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");\r
+ return 1;\r
+ }\r
+@@ -175,9 +172,7 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ \r
+ /* Optimize the query so it excludes messages that already have\r
+ * the specified set of tags. */\r
+- query_string = _optimize_tag_query (ctx, query_string, argv,\r
+- add_tags, add_tags_count,\r
+- remove_tags, remove_tags_count);\r
++ query_string = _optimize_tag_query (ctx, query_string, tag_ops);\r
+ if (query_string == NULL) {\r
+ fprintf (stderr, "Out of memory.\n");\r
+ return 1;\r
+@@ -211,12 +206,12 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ \r
+ notmuch_message_freeze (message);\r
+ \r
+- for (i = 0; i < remove_tags_count; i++)\r
+- notmuch_message_remove_tag (message,\r
+- argv[remove_tags[i]] + 1);\r
+-\r
+- for (i = 0; i < add_tags_count; i++)\r
+- notmuch_message_add_tag (message, argv[add_tags[i]] + 1);\r
++ for (i = 0; tag_ops[i].tag; i++) {\r
++ if (tag_ops[i].remove)\r
++ notmuch_message_remove_tag (message, tag_ops[i].tag);\r
++ else\r
++ notmuch_message_add_tag (message, tag_ops[i].tag);\r
++ }\r
+ \r
+ notmuch_message_thaw (message);\r
+ \r
+diff --git a/test/tagging b/test/tagging\r
+index 3acf1bc..e4782ed 100755\r
+--- a/test/tagging\r
++++ b/test/tagging\r
+@@ -43,7 +43,7 @@ notmuch tag +tag4 -tag4 One\r
+ notmuch tag -tag4 +tag4 Two\r
+ output=$(notmuch search \* | notmuch_search_sanitize)\r
+ test_expect_equal "$output" "\\r
+-thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; One (:\" inbox tag1 tag4 unread)\r
++thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; One (:\" inbox tag1 unread)\r
+ thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag1 tag4 unread)"\r
+ \r
+ test_done\r
+-- \r
+1.7.5.4\r
+\r