--- /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 221AA6DE091F\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Apr 2016 07:16:19 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.244\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.244 tagged_above=-999 required=5\r
+ tests=[AWL=-0.244] 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 wRtNIK7n5KyE for <notmuch@notmuchmail.org>;\r
+ Sat, 2 Apr 2016 07:16:11 -0700 (PDT)\r
+Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])\r
+ by arlo.cworth.org (Postfix) with ESMTP id C47E16DE02DB\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Apr 2016 07:15:55 -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 6847DF997\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Apr 2016 10:15:54 -0400 (EDT)\r
+Received: by fifthhorseman.net (Postfix, from userid 1000)\r
+ id B6F8920CD0; 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 6/7] On deletion,\r
+ replace with ghost when other active messages in thread\r
+Date: Sat, 2 Apr 2016 11:15:40 -0300\r
+Message-Id: <1459606541-23889-6-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:19 -0000\r
+\r
+There is no need to add a ghost message upon deletion if there are no\r
+other active messages in the thread.\r
+\r
+Also, if the message being deleted was a ghost already, we can just go\r
+ahead and delete it.\r
+---\r
+ lib/message.cc | 58 ++++++++++++++++++++++++++++++++++++++++++----------------\r
+ 1 file changed, 42 insertions(+), 16 deletions(-)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index fab70fd..f715e39 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -1044,11 +1044,14 @@ _notmuch_message_delete (notmuch_message_t *message)\r
+ {\r
+ notmuch_status_t status;\r
+ Xapian::WritableDatabase *db;\r
+- const char *mid, *tid;\r
++ const char *mid, *tid, *query_string;\r
+ notmuch_message_t *ghost;\r
+ notmuch_private_status_t private_status;\r
+ notmuch_database_t *notmuch;\r
+- \r
++ notmuch_query_t *query;\r
++ unsigned int count = 0;\r
++ notmuch_bool_t is_ghost;\r
++\r
+ mid = notmuch_message_get_message_id (message);\r
+ tid = notmuch_message_get_thread_id (message);\r
+ notmuch = message->notmuch;\r
+@@ -1059,21 +1062,44 @@ _notmuch_message_delete (notmuch_message_t *message)\r
+ \r
+ db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);\r
+ db->delete_document (message->doc_id);\r
+- \r
+- /* and reintroduce a ghost in its place */\r
+- ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);\r
+- if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {\r
+- /* this is deeply weird, and we should not have gotten into\r
+- this state. is there a better error message to return\r
+- here? */\r
+- return NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;\r
+- } else if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
+- private_status = _notmuch_message_initialize_ghost (ghost, tid);\r
+- if (! private_status)\r
+- _notmuch_message_sync (ghost);\r
++\r
++ /* if this was a ghost to begin with, we are done */\r
++ private_status = _notmuch_message_has_term_st (message, "type", "ghost", &is_ghost);\r
++ if (private_status)\r
++ return COERCE_STATUS (private_status,\r
++ "Error trying to determine whether message was a ghost");\r
++ if (is_ghost)\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++ \r
++ query_string = talloc_asprintf (message, "thread:%s", tid);\r
++ query = notmuch_query_create (notmuch, query_string);\r
++ if (query == NULL)\r
++ return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
++ status = notmuch_query_count_messages_st (query, &count);\r
++ if (status) {\r
++ notmuch_query_destroy (query);\r
++ return status;\r
+ }\r
+- notmuch_message_destroy (ghost);\r
+- return COERCE_STATUS (private_status, "Error converting to ghost message");\r
++\r
++ if (count > 0) {\r
++ /* reintroduce a ghost in its place because there are still\r
++ * other active messages in this thread: */\r
++ ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);\r
++ if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {\r
++ /* this is deeply weird, and we should not have gotten\r
++ into this state. is there a better error message to\r
++ return here? */\r
++ status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;\r
++ } else if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
++ private_status = _notmuch_message_initialize_ghost (ghost, tid);\r
++ if (! private_status)\r
++ _notmuch_message_sync (ghost);\r
++ }\r
++ notmuch_message_destroy (ghost);\r
++ status = COERCE_STATUS (private_status, "Error converting to ghost message");\r
++ }\r
++ notmuch_query_destroy (query);\r
++ return status;\r
+ }\r
+ \r
+ /* Transform a blank message into a ghost message. The caller must\r
+-- \r
+2.8.0.rc3\r
+\r