--- /dev/null
+Return-Path: <markwalters1009@gmail.com>\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 B5038431FDE\r
+ for <notmuch@notmuchmail.org>; Thu, 1 Mar 2012 14:29:45 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.201\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.201 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001,\r
+ 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 YXxIMO5o8-0m for <notmuch@notmuchmail.org>;\r
+ Thu, 1 Mar 2012 14:29:43 -0800 (PST)\r
+Received: from mail-wi0-f181.google.com (mail-wi0-f181.google.com\r
+ [209.85.212.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 05E52429E3D\r
+ for <notmuch@notmuchmail.org>; Thu, 1 Mar 2012 14:29:25 -0800 (PST)\r
+Received: by mail-wi0-f181.google.com with SMTP id o1so305757wic.26\r
+ for <notmuch@notmuchmail.org>; Thu, 01 Mar 2012 14:29:25 -0800 (PST)\r
+Received-SPF: pass (google.com: domain of markwalters1009@gmail.com designates\r
+ 10.180.84.170 as permitted sender) client-ip=10.180.84.170; \r
+Authentication-Results: mr.google.com; spf=pass (google.com: domain of\r
+ markwalters1009@gmail.com designates 10.180.84.170 as permitted\r
+ sender) smtp.mail=markwalters1009@gmail.com;\r
+ dkim=pass header.i=markwalters1009@gmail.com\r
+Received: from mr.google.com ([10.180.84.170])\r
+ by 10.180.84.170 with SMTP id a10mr6523375wiz.9.1330640965867 (num_hops\r
+ = 1); Thu, 01 Mar 2012 14:29:25 -0800 (PST)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;\r
+ bh=SoROdO1KnSo5ZQOPHxwkh6inm1EtWKujflP7hNz3yVM=;\r
+ b=gNvReNVnUuJQb2NsEblMA4/wRFDzEtZ5DZaNpTmiF4zScm67RHChTEu1/t9qkczSET\r
+ sr1QAZKXhN9U/W3HVdeXlqQo+B5FtJ7A61maDF8zyigT1Yc03DkWwbuHBvwJUE/+6wHV\r
+ 9GEm1Atsw24evh8Z5Mf+nJz4zf2dux5o/kocoy/3hNeNvdeLNFwhAjdhaujjZKRA14zR\r
+ XoZin4RERWzmnYPXsxR/X4tTy1yYNvtej7YU8QjJcvYtJp8uszM23dAZJakcTrI6NBb4\r
+ M3oKp6uXFY0IOyzWUZLRJWpu2WWQ66QXilW6xYz8P+dvIGH+ueiALfVi3N3Ynz81yaDg\r
+ GWNg==\r
+Received: by 10.180.84.170 with SMTP id a10mr5262781wiz.9.1330640965803;\r
+ Thu, 01 Mar 2012 14:29:25 -0800 (PST)\r
+Received: from localhost (94-192-233-223.zone6.bethere.co.uk.\r
+ [94.192.233.223]) by mx.google.com with ESMTPS id\r
+ 9sm16612907wid.2.2012.03.01.14.29.16 (version=TLSv1/SSLv3 cipher=OTHER); Thu,\r
+ 01 Mar 2012 14:29:17 -0800 (PST)\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v7 05/13] lib: Make notmuch_query_search_messages set the\r
+ exclude flag\r
+Date: Thu, 1 Mar 2012 22:30:37 +0000\r
+Message-Id: <1330641045-27416-6-git-send-email-markwalters1009@gmail.com>\r
+X-Mailer: git-send-email 1.7.2.3\r
+In-Reply-To: <1330641045-27416-1-git-send-email-markwalters1009@gmail.com>\r
+References: <1330641045-27416-1-git-send-email-markwalters1009@gmail.com>\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: Thu, 01 Mar 2012 22:29:46 -0000\r
+\r
+Add a flag NOTMUCH_MESSAGE_FLAG_EXCLUDED which is set by\r
+notmuch_query_search_messages for excluded messages. Also add an\r
+option omit_excluded_messages to the search that we do not want the\r
+excludes at all.\r
+\r
+This exclude flag will be added to notmuch_query_search threads in the\r
+next patch.\r
+---\r
+ lib/notmuch-private.h | 1 +\r
+ lib/notmuch.h | 10 +++++++-\r
+ lib/query.cc | 59 ++++++++++++++++++++++++++++++++++++++++++++-----\r
+ 3 files changed, 63 insertions(+), 7 deletions(-)\r
+\r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index 7bf153e..e791bb0 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -401,6 +401,7 @@ typedef struct _notmuch_message_list {\r
+ */\r
+ struct visible _notmuch_messages {\r
+ notmuch_bool_t is_of_list_type;\r
++ notmuch_doc_id_set_t *excluded_doc_ids;\r
+ notmuch_message_node_t *iterator;\r
+ };\r
+ \r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 7929fe7..f75afae 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -449,6 +449,13 @@ typedef enum {\r
+ const char *\r
+ notmuch_query_get_query_string (notmuch_query_t *query);\r
+ \r
++/* Specify whether to results should omit the excluded results rather\r
++ * than just marking them excluded. This is useful for passing a\r
++ * notmuch_messages_t not containing the excluded messages to other\r
++ * functions. */\r
++void\r
++notmuch_query_set_omit_excluded_messages (notmuch_query_t *query, notmuch_bool_t omit);\r
++\r
+ /* Specify the sorting desired for this query. */\r
+ void\r
+ notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);\r
+@@ -895,7 +902,8 @@ notmuch_message_get_filenames (notmuch_message_t *message);\r
+ \r
+ /* Message flags */\r
+ typedef enum _notmuch_message_flag {\r
+- NOTMUCH_MESSAGE_FLAG_MATCH\r
++ NOTMUCH_MESSAGE_FLAG_MATCH,\r
++ NOTMUCH_MESSAGE_FLAG_EXCLUDED\r
+ } notmuch_message_flag_t;\r
+ \r
+ /* Get a value of a flag for the email corresponding to 'message'. */\r
+diff --git a/lib/query.cc b/lib/query.cc\r
+index c25b301..ef2a11f 100644\r
+--- a/lib/query.cc\r
++++ b/lib/query.cc\r
+@@ -28,6 +28,7 @@ struct _notmuch_query {\r
+ const char *query_string;\r
+ notmuch_sort_t sort;\r
+ notmuch_string_list_t *exclude_terms;\r
++ notmuch_bool_t omit_excluded_messages;\r
+ };\r
+ \r
+ typedef struct _notmuch_mset_messages {\r
+@@ -57,6 +58,12 @@ struct visible _notmuch_threads {\r
+ notmuch_doc_id_set_t match_set;\r
+ };\r
+ \r
++/* We need this in the message functions so forward declare. */\r
++static notmuch_bool_t\r
++_notmuch_doc_id_set_init (void *ctx,\r
++ notmuch_doc_id_set_t *doc_ids,\r
++ GArray *arr);\r
++\r
+ notmuch_query_t *\r
+ notmuch_query_create (notmuch_database_t *notmuch,\r
+ const char *query_string)\r
+@@ -79,6 +86,8 @@ notmuch_query_create (notmuch_database_t *notmuch,\r
+ \r
+ query->exclude_terms = _notmuch_string_list_create (query);\r
+ \r
++ query->omit_excluded_messages = FALSE;\r
++\r
+ return query;\r
+ }\r
+ \r
+@@ -89,6 +98,12 @@ notmuch_query_get_query_string (notmuch_query_t *query)\r
+ }\r
+ \r
+ void\r
++notmuch_query_set_omit_excluded_messages (notmuch_query_t *query, notmuch_bool_t omit)\r
++{\r
++ query->omit_excluded_messages = omit;\r
++}\r
++\r
++void\r
+ notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)\r
+ {\r
+ query->sort = sort;\r
+@@ -124,8 +139,9 @@ _notmuch_messages_destructor (notmuch_mset_messages_t *messages)\r
+ \r
+ /* Return a query that matches messages with the excluded tags\r
+ * registered with query. Any tags that explicitly appear in xquery\r
+- * will not be excluded. The caller of this function has to combine\r
+- * the returned query appropriately.*/\r
++ * will not be excluded, and will be removed from the list of exclude\r
++ * tags. The caller of this function has to combine the returned\r
++ * query appropriately.*/\r
+ static Xapian::Query\r
+ _notmuch_exclude_tags (notmuch_query_t *query, Xapian::Query xquery)\r
+ {\r
+@@ -142,6 +158,8 @@ _notmuch_exclude_tags (notmuch_query_t *query, Xapian::Query xquery)\r
+ if (it == end)\r
+ exclude_query = Xapian::Query (Xapian::Query::OP_OR,\r
+ exclude_query, Xapian::Query (term->string));\r
++ else\r
++ term->string = talloc_strdup (query, "");\r
+ }\r
+ return exclude_query;\r
+ }\r
+@@ -173,6 +191,7 @@ notmuch_query_search_messages (notmuch_query_t *query)\r
+ "mail"));\r
+ Xapian::Query string_query, final_query, exclude_query;\r
+ Xapian::MSet mset;\r
++ Xapian::MSetIterator iterator;\r
+ unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |\r
+ Xapian::QueryParser::FLAG_PHRASE |\r
+ Xapian::QueryParser::FLAG_LOVEHATE |\r
+@@ -190,11 +209,35 @@ notmuch_query_search_messages (notmuch_query_t *query)\r
+ final_query = Xapian::Query (Xapian::Query::OP_AND,\r
+ mail_query, string_query);\r
+ }\r
++ messages->base.excluded_doc_ids = NULL;\r
++\r
++ if (query->exclude_terms) {\r
++ exclude_query = _notmuch_exclude_tags (query, final_query);\r
++ exclude_query = Xapian::Query (Xapian::Query::OP_AND,\r
++ exclude_query, final_query);\r
++\r
++ if (query->omit_excluded_messages)\r
++ final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,\r
++ final_query, exclude_query);\r
++ else {\r
++ enquire.set_weighting_scheme (Xapian::BoolWeight());\r
++ enquire.set_query (exclude_query);\r
++\r
++ mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());\r
++\r
++ GArray *excluded_doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));\r
++\r
++ for (iterator = mset.begin (); iterator != mset.end (); iterator++) {\r
++ unsigned int doc_id = *iterator;\r
++ g_array_append_val (excluded_doc_ids, doc_id);\r
++ }\r
++ messages->base.excluded_doc_ids = talloc (messages, _notmuch_doc_id_set);\r
++ _notmuch_doc_id_set_init (query, messages->base.excluded_doc_ids,\r
++ excluded_doc_ids);\r
++ g_array_unref (excluded_doc_ids);\r
++ }\r
++ }\r
+ \r
+- exclude_query = _notmuch_exclude_tags (query, final_query);\r
+-\r
+- final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,\r
+- final_query, exclude_query);\r
+ \r
+ enquire.set_weighting_scheme (Xapian::BoolWeight());\r
+ \r
+@@ -283,6 +326,10 @@ _notmuch_mset_messages_get (notmuch_messages_t *messages)\r
+ INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");\r
+ }\r
+ \r
++ if (messages->excluded_doc_ids &&\r
++ _notmuch_doc_id_set_contains (messages->excluded_doc_ids, doc_id))\r
++ notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, TRUE);\r
++\r
+ return message;\r
+ }\r
+ \r
+-- \r
+1.7.2.3\r
+\r