--- /dev/null
+Return-Path: <prvs=372b3579b=jrosenthal@jhu.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 1A9DA431FC2\r
+ for <notmuch@notmuchmail.org>; Wed, 29 Oct 2014 13:52:20 -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 oO6o5cdHs9XU for <notmuch@notmuchmail.org>;\r
+ Wed, 29 Oct 2014 13:52:12 -0700 (PDT)\r
+Received: from smtpauth.johnshopkins.edu (smtpauth.johnshopkins.edu\r
+ [162.129.8.200]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 5242D431FB6\r
+ for <notmuch@notmuchmail.org>; Wed, 29 Oct 2014 13:52:12 -0700 (PDT)\r
+X-IronPort-AV: E=Sophos;i="5.07,279,1413259200"; d="scan'208";a="437698602"\r
+Received: from guppy.hwcampus.jhu.edu (HELO localhost) ([10.161.32.234])\r
+ by ipex1.johnshopkins.edu with ESMTP/TLS/AES128-SHA;\r
+ 29 Oct 2014 16:52:10 -0400\r
+From: Jesse Rosenthal <jrosenthal@jhu.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [v2 1/3] thread.cc: Avoid empty thread names if possible.\r
+Date: Wed, 29 Oct 2014 16:51:43 -0400\r
+Message-Id: <1414615905-22554-2-git-send-email-jrosenthal@jhu.edu>\r
+X-Mailer: git-send-email 2.1.2\r
+In-Reply-To: <1414615905-22554-1-git-send-email-jrosenthal@jhu.edu>\r
+References: <1414615905-22554-1-git-send-email-jrosenthal@jhu.edu>\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: Wed, 29 Oct 2014 20:52:20 -0000\r
+\r
+Currently the thread is named based on either the oldest or newest\r
+matching message (depending on the search order). If this message has\r
+an empty subject, though, the thread will show up with an empty\r
+subject in the search results. (See the thread starting with\r
+`id:1412371140-21051-1-git-send-email-david@tethera.net` for an\r
+example.)\r
+\r
+This changes the behavior so it will use a non-empty name for the\r
+thread if possible. We name threads based on (a) non-empty matches for\r
+the query, and (b) the search order. If the search order is\r
+oldest-first (as in the default inbox) it chooses the oldest matching\r
+non-empty message as the subject. If the search order is newest-first\r
+it chooses the newest one.\r
+---\r
+ lib/thread.cc | 16 +++++++++++-----\r
+ 1 file changed, 11 insertions(+), 5 deletions(-)\r
+\r
+diff --git a/lib/thread.cc b/lib/thread.cc\r
+index 8922403..267f351 100644\r
+--- a/lib/thread.cc\r
++++ b/lib/thread.cc\r
+@@ -24,6 +24,8 @@\r
+ #include <gmime/gmime.h>\r
+ #include <glib.h> /* GHashTable */\r
+ \r
++#define EMPTY_STRING(s) ((s)[0] == '\0') \r
++\r
+ struct visible _notmuch_thread {\r
+ notmuch_database_t *notmuch;\r
+ char *thread_id;\r
+@@ -330,11 +332,13 @@ _thread_set_subject_from_message (notmuch_thread_t *thread,\r
+ } else {\r
+ cleaned_subject = talloc_strdup (thread, subject);\r
+ }\r
++ \r
++ if (! EMPTY_STRING(cleaned_subject)) {\r
++ if (thread->subject)\r
++ talloc_free (thread->subject);\r
+ \r
+- if (thread->subject)\r
+- talloc_free (thread->subject);\r
+-\r
+- thread->subject = talloc_strdup (thread, cleaned_subject);\r
++ thread->subject = talloc_strdup (thread, cleaned_subject);\r
++ }\r
+ }\r
+ \r
+ /* Add a message to this thread which is known to match the original\r
+@@ -348,8 +352,10 @@ _thread_add_matched_message (notmuch_thread_t *thread,\r
+ {\r
+ time_t date;\r
+ notmuch_message_t *hashed_message;\r
++ const char *cur_subject;\r
+ \r
+ date = notmuch_message_get_date (message);\r
++ cur_subject = notmuch_thread_get_subject(thread);\r
+ \r
+ if (date < thread->oldest || ! thread->matched_messages) {\r
+ thread->oldest = date;\r
+@@ -359,7 +365,7 @@ _thread_add_matched_message (notmuch_thread_t *thread,\r
+ \r
+ if (date > thread->newest || ! thread->matched_messages) {\r
+ thread->newest = date;\r
+- if (sort != NOTMUCH_SORT_OLDEST_FIRST)\r
++ if (sort != NOTMUCH_SORT_OLDEST_FIRST || EMPTY_STRING(cur_subject))\r
+ _thread_set_subject_from_message (thread, message);\r
+ }\r
+ \r
+-- \r
+2.1.2\r
+\r