--- /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 1EBCC431E82\r
+ for <notmuch@notmuchmail.org>; Fri, 3 Oct 2014 07:19:40 -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 4gEjBGQh7oQK for <notmuch@notmuchmail.org>;\r
+ Fri, 3 Oct 2014 07:19:32 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-8.mit.edu (dmz-mailsec-scanner-8.mit.edu\r
+ [18.7.68.37])\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 8F6D5431FC9\r
+ for <notmuch@notmuchmail.org>; Fri, 3 Oct 2014 07:19:25 -0700 (PDT)\r
+X-AuditID: 12074425-f79e46d000002583-2c-542eb06c3630\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-8.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id DF.18.09603.C60BE245; Fri, 3 Oct 2014 10:19:24 -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 s93EJN7I020195; \r
+ Fri, 3 Oct 2014 10:19:23 -0400\r
+Received: from drake.dyndns.org\r
+ (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
+ [216.15.114.40]) (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 s93EJL1w023316\r
+ (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
+ Fri, 3 Oct 2014 10:19:22 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.84)\r
+ (envelope-from <aclements@csail.mit.edu>)\r
+ id 1Xa3hZ-0002Jx-6n; Fri, 03 Oct 2014 10:19:21 -0400\r
+From: Austin Clements <aclements@csail.mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 06/11] lib: Internal support for querying and creating ghost\r
+ messages\r
+Date: Fri, 3 Oct 2014 10:19:13 -0400\r
+Message-Id: <1412345958-8278-7-git-send-email-aclements@csail.mit.edu>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <1412345958-8278-1-git-send-email-aclements@csail.mit.edu>\r
+References: <1412345958-8278-1-git-send-email-aclements@csail.mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrIIsWRmVeSWpSXmKPExsUixG6nrpuzQS/E4Np2M4vrN2cyOzB6PFt1\r
+ izmAMYrLJiU1J7MstUjfLoErY17jNsaCa/oVX39tZmlgnKfWxcjBISFgIrF1uW4XIyeQKSZx\r
+ 4d56ti5GLg4hgdlMEjNer2WEcDYwSsy5N4UdpEpI4DaTxLNj4hCJJYwSXw6vYANJsAnoS6xY\r
+ O4kVxBYRkJbYeXc2K8gGZgE1iT9dKiBhYYFQidnrW5hAbBYBVYn9N+6zgNi8Am4SEybuZIK4\r
+ Qk5iw+7/jCA2p4C7xIW7T9kg9rpJrDrWzTaBkX8BI8MqRtmU3Crd3MTMnOLUZN3i5MS8vNQi\r
+ XQu93MwSvdSU0k2M4JBxUd3BOOGQ0iFGAQ5GJR7ejzd0Q4RYE8uKK3MPMUpyMCmJ8lYu1gsR\r
+ 4kvKT6nMSCzOiC8qzUktPsQowcGsJMK7YCVQjjclsbIqtSgfJiXNwaIkzrvpB1+IkEB6Yklq\r
+ dmpqQWoRTFaGg0NJgpcLGBtCgkWp6akVaZk5JQhpJg5OkOE8QMPb14MMLy5IzC3OTIfIn2JU\r
+ lBLnFQVpFgBJZJTmwfXCYvoVozjQK8K8TCBVPMB0ANf9CmgwE9Dgd/a6IINLEhFSUg2M1ptm\r
+ vU+PPv17dvO764rvWwqeNf7Tq5E+7yO3/qvvw+R7k8tWbrZ+1bW0TZ7742l75fkzwhmfn322\r
+ 4UCuZcqnN23W7IVm0+QlWnRvZT3oLb7/7e/Bjz/ee/d0FfP8lPkm0tQ0T1LgDdvs6UVy02RZ\r
+ HE9uD21+37Ls2+roI97clTLb5+3RszDqUmIpzkg01GIuKk4EAMvTMAvEAgAA\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: Fri, 03 Oct 2014 14:19:40 -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 | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--\r
+ lib/notmuch-private.h | 4 ++++\r
+ lib/notmuch.h | 9 ++++++++-\r
+ 3 files changed, 60 insertions(+), 3 deletions(-)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 38bc929..ad832cf 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,23 @@ _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 (! (message->lazy_flags & (1 << NOTMUCH_MESSAGE_FLAG_GHOST))) {\r
++ i.skip_to (type_prefix);\r
++ if (*i == "Tmail")\r
++ message->flags &= ~(1 << NOTMUCH_MESSAGE_FLAG_GHOST);\r
++ else if (*i == "Tghost")\r
++ message->flags |= (1 << NOTMUCH_MESSAGE_FLAG_GHOST);\r
++ else\r
++ INTERNAL_ERROR ("Message without type term");\r
++ message->lazy_flags |= (1 << 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 +389,11 @@ _notmuch_message_invalidate_metadata (notmuch_message_t *message,\r
+ message->tag_list = NULL;\r
+ }\r
+ \r
++ if (strcmp ("type", prefix_name) == 0) {\r
++ message->flags &= ~(1 << NOTMUCH_MESSAGE_FLAG_GHOST);\r
++ message->lazy_flags &= ~(1 << 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 +892,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
++ ! (message->lazy_flags & (1 << flag)))\r
++ _notmuch_message_ensure_metadata (message);\r
++\r
+ return message->flags & (1 << flag);\r
+ }\r
+ \r
+@@ -880,6 +907,7 @@ notmuch_message_set_flag (notmuch_message_t *message,\r
+ message->flags |= (1 << flag);\r
+ else\r
+ message->flags &= ~(1 << flag);\r
++ message->lazy_flags |= (1 << flag);\r
+ }\r
+ \r
+ time_t\r
+@@ -989,6 +1017,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 36cc12b..2fbd38e 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -297,6 +297,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