--- /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 B3963431FB6\r
+ for <notmuch@notmuchmail.org>; Mon, 25 Feb 2013 11:34:13 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\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 FZoKcaEOAPJO for <notmuch@notmuchmail.org>;\r
+ Mon, 25 Feb 2013 11:34:13 -0800 (PST)\r
+Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
+ [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id BF51B431FAF\r
+ for <notmuch@notmuchmail.org>; Mon, 25 Feb 2013 11:34:12 -0800 (PST)\r
+Received: by mail-lb0-f181.google.com with SMTP id gm6so2470862lbb.26\r
+ for <notmuch@notmuchmail.org>; Mon, 25 Feb 2013 11:34:09 -0800 (PST)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=google.com; s=20120113;\r
+ h=x-received:from:to:cc:subject:in-reply-to:references:user-agent\r
+ :date:message-id:mime-version:content-type:x-gm-message-state;\r
+ bh=qvNatpg9st7SuGZhVn1AJvtE8b7tNr2xnTJxYFOG6z0=;\r
+ b=PFPzzRC549cEbOPBU6QUBLX0p7GkU9NReXVQJ1jstuxo4TcS3offemVEVYI8Z0Ry6H\r
+ k1ui5ZZQ6iyg/0HU9ibQfGcxjRhkLJ9txMMw1ryNy0KlClz0jt7oy6Qmt4pSedcVY7K5\r
+ PFV5dvOtO5mq3AW+MGg1K5+xQxSnNsjkuT/CbNvjXIo6ny9TZbccxAsrLDbd2R1veyH7\r
+ RwBcc6XcKDz2ddBRdwJc5J0M5HbYwyNYK5nks1siKBZXm/g2JSkQllbziwPqq+pdHLpc\r
+ 5hs/E4TjBsGuNxsNhw+9F7wOe2PDmBtjLLRDjO6bNzKCeyTXxFQX0xfWh4fPcK2JGmrm\r
+ 5UKw==\r
+X-Received: by 10.152.105.17 with SMTP id gi17mr10804325lab.46.1361820848548; \r
+ Mon, 25 Feb 2013 11:34:08 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi.\r
+ [80.223.81.27])\r
+ by mx.google.com with ESMTPS id fm8sm4483305lbb.17.2013.02.25.11.34.06\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Mon, 25 Feb 2013 11:34:07 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 2/4] cli: add --remove-all option to "notmuch tag"\r
+In-Reply-To:\r
+ <e9dd76463809998cdb68bb5078a8d679e98560c2.1358876448.git.jani@nikula.org>\r
+References:\r
+ <1a2c09adc1c963f1aa209c09143f85dca7634e11.1358876448.git.jani@nikula.org>\r
+ <e9dd76463809998cdb68bb5078a8d679e98560c2.1358876448.git.jani@nikula.org>\r
+User-Agent: Notmuch/0.14+255~gff3cc55 (http://notmuchmail.org) Emacs/24.2.1\r
+ (x86_64-pc-linux-gnu)\r
+Date: Mon, 25 Feb 2013 21:34:09 +0200\r
+Message-ID: <87obf8ur0u.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain\r
+X-Gm-Message-State:\r
+ ALoCoQlTOpbpCKsly8q57nCHKKXCgu+Ulv85GGVLLIqc34ELLjHMK/jaQiWxg3vfbkNiIX+gscQR\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, 25 Feb 2013 19:34:13 -0000\r
+\r
+On Tue, 22 Jan 2013, Jani Nikula <jani@nikula.org> wrote:\r
+> Add --remove-all option to "notmuch tag" to remove all tags from the\r
+> messages matching query before applying the tag changes. This allows\r
+> removal of all tags and unconditional setting of the tags of a\r
+> message:\r
+>\r
+> $ notmuch tag --remove-all id:foo@example.com\r
+> $ notmuch tag --remove-all +foo +bar id:foo@example.com\r
+>\r
+> without having to resort to the complicated (and still quoting\r
+> broken):\r
+>\r
+> $ notmuch tag $(notmuch search --output=tags '*' | sed 's/^/-/') \\r
+> id:foo@example.com\r
+> $ notmuch tag $(notmuch search --output=tags '*' | sed 's/^/-/') \\r
+> +foo +bar id:foo@example.com\r
+\r
+Just today there was another question on IRC about removing all tags,\r
+and the reply from amdragon was along the lines of the above. This\r
+series would make things so much easier... reviews, anyone? ;)\r
+\r
+BR,\r
+Jani.\r
+\r
+\r
+> ---\r
+> notmuch-tag.c | 28 ++++++++++++++++++++--------\r
+> 1 file changed, 20 insertions(+), 8 deletions(-)\r
+>\r
+> diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+> index b2b22f7..e674215 100644\r
+> --- a/notmuch-tag.c\r
+> +++ b/notmuch-tag.c\r
+> @@ -99,12 +99,15 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,\r
+> notmuch_message_t *message;\r
+> int ret = NOTMUCH_STATUS_SUCCESS;\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, tag_ops);\r
+> - if (query_string == NULL) {\r
+> - fprintf (stderr, "Out of memory.\n");\r
+> - return 1;\r
+> + if (! (flags & TAG_FLAG_REMOVE_ALL)) {\r
+> + /* Optimize the query so it excludes messages that already\r
+> + * have the specified set of tags. */\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
+> + }\r
+> + flags |= TAG_FLAG_PRE_OPTIMIZED;\r
+> }\r
+> \r
+> query = notmuch_query_create (notmuch, query_string);\r
+> @@ -120,7 +123,7 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,\r
+> notmuch_messages_valid (messages) && ! interrupted;\r
+> notmuch_messages_move_to_next (messages)) {\r
+> message = notmuch_messages_get (messages);\r
+> - ret = tag_op_list_apply (message, tag_ops, flags | TAG_FLAG_PRE_OPTIMIZED);\r
+> + ret = tag_op_list_apply (message, tag_ops, flags);\r
+> notmuch_message_destroy (message);\r
+> if (ret != NOTMUCH_STATUS_SUCCESS)\r
+> break;\r
+> @@ -187,6 +190,7 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+> struct sigaction action;\r
+> tag_op_flag_t tag_flags = TAG_FLAG_NONE;\r
+> notmuch_bool_t batch = FALSE;\r
+> + notmuch_bool_t remove_all = FALSE;\r
+> FILE *input = stdin;\r
+> char *input_file_name = NULL;\r
+> int opt_index;\r
+> @@ -202,6 +206,7 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+> notmuch_opt_desc_t options[] = {\r
+> { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },\r
+> { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
+> + { NOTMUCH_OPT_BOOLEAN, &remove_all, "remove-all", 0, 0 },\r
+> { 0, 0, 0, 0, 0 }\r
+> };\r
+> \r
+> @@ -224,6 +229,10 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+> fprintf (stderr, "Can't specify both cmdline and stdin!\n");\r
+> return 1;\r
+> }\r
+> + if (remove_all) {\r
+> + fprintf (stderr, "Can't specify both --remove-all and --batch\n");\r
+> + return 1;\r
+> + }\r
+> } else {\r
+> tag_ops = tag_op_list_create (ctx);\r
+> if (tag_ops == NULL) {\r
+> @@ -235,7 +244,7 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+> &query_string, tag_ops))\r
+> return 1;\r
+> \r
+> - if (tag_op_list_size (tag_ops) == 0) {\r
+> + if (tag_op_list_size (tag_ops) == 0 && ! remove_all) {\r
+> fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");\r
+> return 1;\r
+> }\r
+> @@ -252,6 +261,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+> if (notmuch_config_get_maildir_synchronize_flags (config))\r
+> tag_flags |= TAG_FLAG_MAILDIR_SYNC;\r
+> \r
+> + if (remove_all)\r
+> + tag_flags |= TAG_FLAG_REMOVE_ALL;\r
+> +\r
+> if (batch)\r
+> ret = tag_file (ctx, notmuch, tag_flags, input);\r
+> else\r
+> -- \r
+> 1.7.10.4\r