--- /dev/null
+Return-Path: <aclements@csail.mit.edu>\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 145E3431FC3\r
+ for <notmuch@notmuchmail.org>; Mon, 6 Oct 2014 16:17:31 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] 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 H5yCLonCmNsE for <notmuch@notmuchmail.org>;\r
+ Mon, 6 Oct 2014 16:17:27 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-6.mit.edu (dmz-mailsec-scanner-6.mit.edu\r
+ [18.7.68.35])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id DD05A431FAF\r
+ for <notmuch@notmuchmail.org>; Mon, 6 Oct 2014 16:17:26 -0700 (PDT)\r
+X-AuditID: 12074423-f799d6d00000337c-b9-543323062838\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+ (using TLS with cipher AES256-SHA (256/256 bits))\r
+ (Client did not present a certificate)\r
+ by dmz-mailsec-scanner-6.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 3C.5C.13180.60323345; Mon, 6 Oct 2014 19:17:26 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id s96NHPcd032090; \r
+ Mon, 6 Oct 2014 19:17:25 -0400\r
+Received: from drake.dyndns.org ([12.185.136.2]) (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s96NHMbk014469\r
+ (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
+ Mon, 6 Oct 2014 19:17:24 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.84)\r
+ (envelope-from <aclements@csail.mit.edu>)\r
+ id 1XbHWs-0001Jh-2O; Mon, 06 Oct 2014 17:17:22 -0600\r
+From: Austin Clements <aclements@csail.mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 07/12] lib: Internal support for querying and creating\r
+ ghost messages\r
+Date: Mon, 6 Oct 2014 17:17:13 -0600\r
+Message-Id: <1412637438-4821-8-git-send-email-aclements@csail.mit.edu>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <1412637438-4821-1-git-send-email-aclements@csail.mit.edu>\r
+References: <1412637438-4821-1-git-send-email-aclements@csail.mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrDIsWRmVeSWpSXmKPExsUixG6nrsumbBxiMKuLx+JGazejxfWbM5kd\r
+ mDyerbrF7LHl0HvmAKYoLpuU1JzMstQifbsEroyujZIF0wwr2p9dYWpgfKzexcjJISFgIrH4\r
+ 3mk2CFtM4sK99UA2F4eQwGwmialz97BAOBsYJW4snQRWJSSwlEni8UlXiMQSRonnV++ygCTY\r
+ BPQlVqydxApiiwhIS+y8OxvMZhawlLj6tZ0JxBYWiJDYduQQ2CAWAVWJ72cuMoPYvAJuEo0b\r
+ VrNAnCEnsWH3f0YQm1PAXWLNlcmsEIvdJJ7/fMoygZF/ASPDKkbZlNwq3dzEzJzi1GTd4uTE\r
+ vLzUIl0zvdzMEr3UlNJNjOBAclHewfjnoNIhRgEORiUe3ogdhiFCrIllxZW5hxglOZiURHnn\r
+ sBuHCPEl5adUZiQWZ8QXleakFh9ilOBgVhLh5fptFCLEm5JYWZValA+TkuZgURLn3fSDL0RI\r
+ ID2xJDU7NbUgtQgmK8PBoSTBa6wENFSwKDU9tSItM6cEIc3EwQkynAdo+D9FoBre4oLE3OLM\r
+ dIj8KUZFKXHeYJCEAEgiozQPrhcW6a8YxYFeEeYNAlnBA0wScN2vgAYzAQ02naMPMrgkESEl\r
+ 1cB4vPoai4TkY/8Lk4S4pPesjO1+WjXdYYroc30WbtNscbfHl5r8v21U+KdzzDN7wbu+JZPU\r
+ fLyCjl9dvvkQy98yuwV65tqO+zdfnrtUbUOph+f+OX4/X25u5Laf6VG+acksqWeRHOUBAa9X\r
+ Teg1TXrVJpiiJrRx3saL3k17DkxS3FJ5Rm7HhBW3lFiKMxINtZiLihMBXwDlGc8CAAA=\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, 06 Oct 2014 23:17:31 -0000\r
+\r
+From: Austin Clements <amdragon@mit.edu>\r
+\r
+This updates the message abstraction to support ghost messages: it\r
+adds a message flag that distinguishes regular messages from ghost\r
+messages, and an internal function for initializing a newly created\r
+(blank) message as a ghost message.\r
+---\r
+ lib/message.cc | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--\r
+ lib/notmuch-private.h | 4 ++++\r
+ lib/notmuch.h | 9 ++++++++-\r
+ 3 files changed, 62 insertions(+), 3 deletions(-)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 55d2ff6..a7a13cc 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -39,6 +39,9 @@ struct visible _notmuch_message {\r
+ notmuch_message_file_t *message_file;\r
+ notmuch_message_list_t *replies;\r
+ unsigned long flags;\r
++ /* For flags that are initialized on-demand, lazy_flags indicates\r
++ * if each flag has been initialized. */\r
++ unsigned long lazy_flags;\r
+ \r
+ Xapian::Document doc;\r
+ Xapian::termcount termpos;\r
+@@ -99,6 +102,7 @@ _notmuch_message_create_for_document (const void *talloc_owner,\r
+ \r
+ message->frozen = 0;\r
+ message->flags = 0;\r
++ message->lazy_flags = 0;\r
+ \r
+ /* Each of these will be lazily created as needed. */\r
+ message->message_id = NULL;\r
+@@ -192,7 +196,7 @@ _notmuch_message_create (const void *talloc_owner,\r
+ *\r
+ * There is already a document with message ID 'message_id' in the\r
+ * database. The returned message can be used to query/modify the\r
+- * document.\r
++ * document. The message may be a ghost message.\r
+ *\r
+ * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:\r
+ *\r
+@@ -305,6 +309,7 @@ _notmuch_message_ensure_metadata (notmuch_message_t *message)\r
+ const char *thread_prefix = _find_prefix ("thread"),\r
+ *tag_prefix = _find_prefix ("tag"),\r
+ *id_prefix = _find_prefix ("id"),\r
++ *type_prefix = _find_prefix ("type"),\r
+ *filename_prefix = _find_prefix ("file-direntry"),\r
+ *replyto_prefix = _find_prefix ("replyto");\r
+ \r
+@@ -337,10 +342,25 @@ _notmuch_message_ensure_metadata (notmuch_message_t *message)\r
+ message->message_id =\r
+ _notmuch_message_get_term (message, i, end, id_prefix);\r
+ \r
++ /* Get document type */\r
++ assert (strcmp (id_prefix, type_prefix) < 0);\r
++ if (! NOTMUCH_TEST_BIT (message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST)) {\r
++ i.skip_to (type_prefix);\r
++ /* "T" is the prefix "type" fields. See\r
++ * BOOLEAN_PREFIX_INTERNAL. */\r
++ if (*i == "Tmail")\r
++ NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);\r
++ else if (*i == "Tghost")\r
++ NOTMUCH_SET_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);\r
++ else\r
++ INTERNAL_ERROR ("Message without type term");\r
++ NOTMUCH_SET_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);\r
++ }\r
++\r
+ /* Get filename list. Here we get only the terms. We lazily\r
+ * expand them to full file names when needed in\r
+ * _notmuch_message_ensure_filename_list. */\r
+- assert (strcmp (id_prefix, filename_prefix) < 0);\r
++ assert (strcmp (type_prefix, filename_prefix) < 0);\r
+ if (!message->filename_term_list && !message->filename_list)\r
+ message->filename_term_list =\r
+ _notmuch_database_get_terms_with_prefix (message, i, end,\r
+@@ -371,6 +391,11 @@ _notmuch_message_invalidate_metadata (notmuch_message_t *message,\r
+ message->tag_list = NULL;\r
+ }\r
+ \r
++ if (strcmp ("type", prefix_name) == 0) {\r
++ NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);\r
++ NOTMUCH_CLEAR_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);\r
++ }\r
++\r
+ if (strcmp ("file-direntry", prefix_name) == 0) {\r
+ talloc_free (message->filename_term_list);\r
+ talloc_free (message->filename_list);\r
+@@ -869,6 +894,10 @@ notmuch_bool_t\r
+ notmuch_message_get_flag (notmuch_message_t *message,\r
+ notmuch_message_flag_t flag)\r
+ {\r
++ if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&\r
++ ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))\r
++ _notmuch_message_ensure_metadata (message);\r
++\r
+ return NOTMUCH_TEST_BIT (message->flags, flag);\r
+ }\r
+ \r
+@@ -880,6 +909,7 @@ notmuch_message_set_flag (notmuch_message_t *message,\r
+ NOTMUCH_SET_BIT (&message->flags, flag);\r
+ else\r
+ NOTMUCH_CLEAR_BIT (&message->flags, flag);\r
++ NOTMUCH_SET_BIT (&message->lazy_flags, flag);\r
+ }\r
+ \r
+ time_t\r
+@@ -989,6 +1019,24 @@ _notmuch_message_delete (notmuch_message_t *message)\r
+ return NOTMUCH_STATUS_SUCCESS;\r
+ }\r
+ \r
++/* Transform a blank message into a ghost message. The caller must\r
++ * _notmuch_message_sync the message. */\r
++notmuch_private_status_t\r
++_notmuch_message_initialize_ghost (notmuch_message_t *message,\r
++ const char *thread_id)\r
++{\r
++ notmuch_private_status_t status;\r
++\r
++ status = _notmuch_message_add_term (message, "type", "ghost");\r
++ if (status)\r
++ return status;\r
++ status = _notmuch_message_add_term (message, "thread", thread_id);\r
++ if (status)\r
++ return status;\r
++\r
++ return NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
++}\r
++\r
+ /* Ensure that 'message' is not holding any file object open. Future\r
+ * calls to various functions will still automatically open the\r
+ * message file as needed.\r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index 7250291..2f43c1d 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -308,6 +308,10 @@ _notmuch_message_sync (notmuch_message_t *message);\r
+ notmuch_status_t\r
+ _notmuch_message_delete (notmuch_message_t *message);\r
+ \r
++notmuch_private_status_t\r
++_notmuch_message_initialize_ghost (notmuch_message_t *message,\r
++ const char *thread_id);\r
++\r
+ void\r
+ _notmuch_message_close (notmuch_message_t *message);\r
+ \r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index dae0416..92594b9 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -1221,7 +1221,14 @@ notmuch_message_get_filenames (notmuch_message_t *message);\r
+ */\r
+ typedef enum _notmuch_message_flag {\r
+ NOTMUCH_MESSAGE_FLAG_MATCH,\r
+- NOTMUCH_MESSAGE_FLAG_EXCLUDED\r
++ NOTMUCH_MESSAGE_FLAG_EXCLUDED,\r
++\r
++ /* This message is a "ghost message", meaning it has no filenames\r
++ * or content, but we know it exists because it was referenced by\r
++ * some other message. A ghost message has only a message ID and\r
++ * thread ID.\r
++ */\r
++ NOTMUCH_MESSAGE_FLAG_GHOST,\r
+ } notmuch_message_flag_t;\r
+ \r
+ /**\r
+-- \r
+2.1.0\r
+\r