--- /dev/null
+Return-Path: <dkg@fifthhorseman.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 805076DE0350\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Apr 2016 07:16:03 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.439\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.439 tagged_above=-999 required=5\r
+ tests=[AWL=-0.439] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id GzGQCae5TLHf for <notmuch@notmuchmail.org>;\r
+ Sat, 2 Apr 2016 07:15:55 -0700 (PDT)\r
+Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])\r
+ by arlo.cworth.org (Postfix) with ESMTP id B25CC6DE00DF\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Apr 2016 07:15:47 -0700 (PDT)\r
+Received: from fifthhorseman.net (dhcp-a244.meeting.ietf.org [31.133.162.68])\r
+ by che.mayfirst.org (Postfix) with ESMTPSA id 54B77F997\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Apr 2016 10:15:45 -0400 (EDT)\r
+Received: by fifthhorseman.net (Postfix, from userid 1000)\r
+ id B360220A0B; Sat, 2 Apr 2016 11:15:41 -0300 (BRT)\r
+From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
+To: Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: [PATCH v2 5/7] Introduce _notmuch_message_has_term()\r
+Date: Sat, 2 Apr 2016 11:15:39 -0300\r
+Message-Id: <1459606541-23889-5-git-send-email-dkg@fifthhorseman.net>\r
+X-Mailer: git-send-email 2.8.0.rc3\r
+In-Reply-To: <1459606541-23889-1-git-send-email-dkg@fifthhorseman.net>\r
+References: <1459445693-3900-1-git-send-email-dkg@fifthhorseman.net>\r
+ <1459606541-23889-1-git-send-email-dkg@fifthhorseman.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://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: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 02 Apr 2016 14:16:03 -0000\r
+\r
+It can be useful to easily tell if a given message has a given term\r
+associated with it.\r
+---\r
+ lib/message.cc | 49 +++++++++++++++++++++++++++++++++++++++++++++++++\r
+ lib/notmuch-private.h | 13 +++++++++++++\r
+ 2 files changed, 62 insertions(+)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index e414e9c..fab70fd 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -1216,6 +1216,55 @@ _notmuch_message_remove_term (notmuch_message_t *message,\r
+ return NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
+ }\r
+ \r
++notmuch_bool_t\r
++_notmuch_message_has_term (notmuch_message_t *message,\r
++ const char *prefix_name,\r
++ const char *value)\r
++{\r
++ notmuch_bool_t out;\r
++ notmuch_private_status_t st =\r
++ _notmuch_message_has_term_st (message, prefix_name, value, &out);\r
++ if (st)\r
++ return FALSE;\r
++ return out;\r
++}\r
++\r
++\r
++notmuch_private_status_t\r
++_notmuch_message_has_term_st (notmuch_message_t *message,\r
++ const char *prefix_name,\r
++ const char *value,\r
++ notmuch_bool_t *result)\r
++{\r
++ char *term;\r
++ notmuch_bool_t out = FALSE;\r
++ notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
++\r
++ if (value == NULL)\r
++ return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;\r
++\r
++ term = talloc_asprintf (message, "%s%s",\r
++ _find_prefix (prefix_name), value);\r
++\r
++ if (strlen (term) > NOTMUCH_TERM_MAX)\r
++ return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;\r
++\r
++ try {\r
++ /* Look for the exact term */\r
++ Xapian::TermIterator i = message->doc.termlist_begin ();\r
++ i.skip_to (term);\r
++ if (i != message->doc.termlist_end () &&\r
++ !strcmp ((*i).c_str (), term))\r
++ out = TRUE;\r
++ } catch (Xapian::Error &error) {\r
++ status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;\r
++ }\r
++ talloc_free (term);\r
++\r
++ *result = out;\r
++ return status;\r
++}\r
++\r
+ notmuch_status_t\r
+ notmuch_message_add_tag (notmuch_message_t *message, const char *tag)\r
+ {\r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index cbfc144..00391a9 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -279,6 +279,19 @@ _notmuch_message_remove_term (notmuch_message_t *message,\r
+ const char *prefix_name,\r
+ const char *value);\r
+ \r
++/* _notmuch_message_has_term designed to be simple: if there is an\r
++ * error, it will return false */\r
++notmuch_bool_t\r
++_notmuch_message_has_term (notmuch_message_t *message,\r
++ const char *prefix_name,\r
++ const char *value);\r
++\r
++notmuch_private_status_t\r
++_notmuch_message_has_term_st (notmuch_message_t *message,\r
++ const char *prefix_name,\r
++ const char *value,\r
++ notmuch_bool_t *result);\r
++\r
+ notmuch_private_status_t\r
+ _notmuch_message_gen_terms (notmuch_message_t *message,\r
+ const char *prefix_name,\r
+-- \r
+2.8.0.rc3\r
+\r