--- /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 84A35429E39\r
+ for <notmuch@notmuchmail.org>; Thu, 2 Feb 2012 09:43:25 -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 cXguDjoPkW+K for <notmuch@notmuchmail.org>;\r
+ Thu, 2 Feb 2012 09:43:22 -0800 (PST)\r
+Received: from mail-ww0-f45.google.com (mail-ww0-f45.google.com\r
+ [74.125.82.45]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client\r
+ certificate requested) by olra.theworths.org (Postfix) with ESMTPS id\r
+ 2BA11429E3E for <notmuch@notmuchmail.org>; Thu, 2 Feb 2012 09:43:10 -0800\r
+ (PST)\r
+Received: by mail-ww0-f45.google.com with SMTP id dt12so2522817wgb.2\r
+ for <notmuch@notmuchmail.org>; Thu, 02 Feb 2012 09:43:09 -0800 (PST)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma;\r
+ h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;\r
+ bh=ft1IahEMhtmHYG7nZwKDqN9UBhvMMgnXonDQYddK1RU=;\r
+ b=imcmImRnnqaDP+RPiLQzNC7tt0t/L7bl2N+CLnyzWG59w4qaa1/dvcyO6L7/snIKtr\r
+ i3Qa40lJtFqAsgeQ98+LTahMr9CM971o+Fy94bIPsRTwoBuNCRCl7ntmwIFli8BmjNYZ\r
+ Ugll8FS+a1+GtA5CzzffbUbt8Ut5hYnwEMd3I=\r
+Received: by 10.180.106.33 with SMTP id gr1mr6195411wib.6.1328204589854;\r
+ Thu, 02 Feb 2012 09:43:09 -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
+ y1sm9134771wiw.6.2012.02.02.09.43.08 (version=TLSv1/SSLv3 cipher=OTHER); Thu,\r
+ 02 Feb 2012 09:43:09 -0800 (PST)\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: notmuch@notmuchmail.org,\r
+ amdragon@MIT.EDU\r
+Subject: [PATCH v4 07/11] lib: added interface\r
+ notmuch_thread_get_flag_messages\r
+Date: Thu, 2 Feb 2012 17:43:35 +0000\r
+Message-Id: <1328204619-25046-7-git-send-email-markwalters1009@gmail.com>\r
+X-Mailer: git-send-email 1.7.2.3\r
+In-Reply-To: <874nv9rv79.fsf@qmul.ac.uk>\r
+References: <874nv9rv79.fsf@qmul.ac.uk>\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, 02 Feb 2012 17:43:26 -0000\r
+\r
+The function is\r
+notmuch_thread_get_flag_messages\r
+(notmuch_thread_t *thread, unsigned int flag_mask, unsigned int flags)\r
+\r
+and returns the number of messages with the specified flags on flag_mask.\r
+\r
+This generalises the existing function\r
+notmuch_thread_get_total_messages and\r
+notmuch_thread_get_matched_messages which are retained to maintain\r
+compatibility.\r
+---\r
+ lib/message.cc | 6 +++---\r
+ lib/notmuch.h | 15 +++++++++++++--\r
+ lib/thread.cc | 39 ++++++++++++++++++++++++++-------------\r
+ 3 files changed, 42 insertions(+), 18 deletions(-)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 0075425..d60da83 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -746,7 +746,7 @@ notmuch_bool_t\r
+ notmuch_message_get_flag (notmuch_message_t *message,\r
+ notmuch_message_flag_t flag)\r
+ {\r
+- return message->flags & (1 << flag);\r
++ return message->flags & flag;\r
+ }\r
+ \r
+ void\r
+@@ -754,9 +754,9 @@ notmuch_message_set_flag (notmuch_message_t *message,\r
+ notmuch_message_flag_t flag, notmuch_bool_t enable)\r
+ {\r
+ if (enable)\r
+- message->flags |= (1 << flag);\r
++ message->flags |= flag;\r
+ else\r
+- message->flags &= ~(1 << flag);\r
++ message->flags &= ~flag;\r
+ }\r
+ \r
+ time_t\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index f75afae..c02e7f4 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -654,6 +654,16 @@ notmuch_thread_get_thread_id (notmuch_thread_t *thread);\r
+ int\r
+ notmuch_thread_get_total_messages (notmuch_thread_t *thread);\r
+ \r
++/* Get the number of messages in 'thread' which have the specified\r
++ * flags on flag_mask.\r
++ *\r
++ * This is a more general interface than\r
++ * notmuch_thread_get_total_messages or\r
++ * notmuch_thread_get_matched_messages\r
++ */\r
++int\r
++notmuch_thread_get_flag_messages (notmuch_thread_t *thread, unsigned int flag_mask, unsigned int flags);\r
++\r
+ /* Get a notmuch_messages_t iterator for the top-level messages in\r
+ * 'thread'.\r
+ *\r
+@@ -902,8 +912,9 @@ 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_EXCLUDED\r
++ NOTMUCH_MESSAGE_FLAG_MATCH = (1<<0),\r
++ NOTMUCH_MESSAGE_FLAG_EXCLUDED = (1<<1),\r
++ NOTMUCH_MESSAGE_FLAG_MAX = (1<<2)\r
+ } notmuch_message_flag_t;\r
+ \r
+ /* Get a value of a flag for the email corresponding to 'message'. */\r
+diff --git a/lib/thread.cc b/lib/thread.cc\r
+index e976d64..542f7f4 100644\r
+--- a/lib/thread.cc\r
++++ b/lib/thread.cc\r
+@@ -37,8 +37,7 @@ struct visible _notmuch_thread {\r
+ \r
+ notmuch_message_list_t *message_list;\r
+ GHashTable *message_hash;\r
+- int total_messages;\r
+- int matched_messages;\r
++ int flag_count_messages[NOTMUCH_MESSAGE_FLAG_MAX];\r
+ time_t oldest;\r
+ time_t newest;\r
+ };\r
+@@ -226,7 +225,6 @@ _thread_add_message (notmuch_thread_t *thread,\r
+ \r
+ _notmuch_message_list_add_message (thread->message_list,\r
+ talloc_steal (thread, message));\r
+- thread->total_messages++;\r
+ \r
+ g_hash_table_insert (thread->message_hash,\r
+ xstrdup (notmuch_message_get_message_id (message)),\r
+@@ -319,21 +317,18 @@ _thread_add_matched_message (notmuch_thread_t *thread,\r
+ \r
+ date = notmuch_message_get_date (message);\r
+ \r
+- if (date < thread->oldest || ! thread->matched_messages) {\r
++ if (date < thread->oldest || ! notmuch_thread_get_matched_messages (thread)) {\r
+ thread->oldest = date;\r
+ if (sort == NOTMUCH_SORT_OLDEST_FIRST)\r
+ _thread_set_subject_from_message (thread, message);\r
+ }\r
+ \r
+- if (date > thread->newest || ! thread->matched_messages) {\r
++ if (date > thread->newest || ! notmuch_thread_get_matched_messages (thread)) {\r
+ thread->newest = date;\r
+ if (sort != NOTMUCH_SORT_OLDEST_FIRST)\r
+ _thread_set_subject_from_message (thread, message);\r
+ }\r
+ \r
+- if (!notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED))\r
+- thread->matched_messages++;\r
+-\r
+ if (g_hash_table_lookup_extended (thread->message_hash,\r
+ notmuch_message_get_message_id (message), NULL,\r
+ (void **) &hashed_message)) {\r
+@@ -411,7 +406,7 @@ _notmuch_thread_create (void *ctx,\r
+ const char *thread_id;\r
+ char *thread_id_query_string;\r
+ notmuch_query_t *thread_id_query;\r
+-\r
++ int i;\r
+ notmuch_messages_t *messages;\r
+ notmuch_message_t *message;\r
+ \r
+@@ -457,8 +452,8 @@ _notmuch_thread_create (void *ctx,\r
+ thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,\r
+ free, NULL);\r
+ \r
+- thread->total_messages = 0;\r
+- thread->matched_messages = 0;\r
++ for (i = 0; i < NOTMUCH_MESSAGE_FLAG_MAX; i++)\r
++ thread->flag_count_messages[i] = 0;\r
+ thread->oldest = 0;\r
+ thread->newest = 0;\r
+ \r
+@@ -473,6 +468,7 @@ _notmuch_thread_create (void *ctx,\r
+ notmuch_messages_move_to_next (messages))\r
+ {\r
+ unsigned int doc_id;\r
++ unsigned int message_flags;\r
+ \r
+ message = notmuch_messages_get (messages);\r
+ doc_id = _notmuch_message_get_doc_id (message);\r
+@@ -485,6 +481,10 @@ _notmuch_thread_create (void *ctx,\r
+ _notmuch_doc_id_set_remove (match_set, doc_id);\r
+ _thread_add_matched_message (thread, message, sort);\r
+ }\r
++ message_flags =\r
++ notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) |\r
++ notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);\r
++ thread->flag_count_messages[message_flags]++;\r
+ \r
+ _notmuch_message_close (message);\r
+ }\r
+@@ -511,15 +511,28 @@ notmuch_thread_get_thread_id (notmuch_thread_t *thread)\r
+ }\r
+ \r
+ int\r
++notmuch_thread_get_flag_messages (notmuch_thread_t *thread, unsigned int flag_mask, unsigned int flags)\r
++{\r
++ unsigned int i;\r
++ int count = 0;\r
++ for (i = 0; i < NOTMUCH_MESSAGE_FLAG_MAX; i++)\r
++ if ((i & flag_mask) == (flags & flag_mask))\r
++ count += thread->flag_count_messages[i];\r
++ return count;\r
++}\r
++\r
++int\r
+ notmuch_thread_get_total_messages (notmuch_thread_t *thread)\r
+ {\r
+- return thread->total_messages;\r
++ return notmuch_thread_get_flag_messages (thread, 0, 0);\r
+ }\r
+ \r
+ int\r
+ notmuch_thread_get_matched_messages (notmuch_thread_t *thread)\r
+ {\r
+- return thread->matched_messages;\r
++ return notmuch_thread_get_flag_messages (thread,\r
++ NOTMUCH_MESSAGE_FLAG_MATCH | NOTMUCH_MESSAGE_FLAG_EXCLUDED,\r
++ NOTMUCH_MESSAGE_FLAG_MATCH);\r
+ }\r
+ \r
+ const char *\r
+-- \r
+1.7.2.3\r
+\r