+ author_copy = talloc_strdup (thread, author);
+
+ g_hash_table_insert (thread->authors_hash, author_copy, NULL);
+
+ g_ptr_array_add (thread->authors_array, author_copy);
+}
+
+/* Add each matched author of the thread to the thread's
+ * matched_authors_hash and to the thread's matched_authors_array. */
+static void
+_thread_add_matched_author (notmuch_thread_t *thread,
+ const char *author)
+{
+ char *author_copy;
+
+ if (author == NULL)
+ return;
+
+ if (g_hash_table_lookup_extended (thread->matched_authors_hash,
+ author, NULL, NULL))
+ return;
+
+ author_copy = talloc_strdup (thread, author);
+
+ g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
+
+ g_ptr_array_add (thread->matched_authors_array, author_copy);
+}
+
+/* Construct an authors string from matched_authors_array and
+ * authors_array. The string contains matched authors first, then
+ * non-matched authors (with the two groups separated by '|' or the custom
+ * separator defined in the configuration). Within each group, authors are
+ * listed in date order and separated by ',' or the custom separator defined in
+ * the configuration. */
+static void
+_resolve_thread_authors_string (notmuch_thread_t *thread)
+{
+ unsigned int i;
+ char *author;
+ int first_non_matched_author = 1;
+ const char *authors_sep = notmuch_config_get (thread->notmuch,
+ NOTMUCH_CONFIG_AUTHORS_SEPARATOR);
+ const char *authors_matched_sep = notmuch_config_get (thread->notmuch,
+ NOTMUCH_CONFIG_AUTHORS_MATCHED_SEPARATOR);
+
+ /* First, list all matched authors in date order. */
+ for (i = 0; i < thread->matched_authors_array->len; i++) {
+ author = (char *) g_ptr_array_index (thread->matched_authors_array, i);
+ if (thread->authors)
+ thread->authors = talloc_asprintf (thread, "%s%s%s",
+ thread->authors,
+ authors_sep,
+ author);
+ else
+ thread->authors = author;
+ }
+
+ /* Next, append any non-matched authors that haven't already appeared. */
+ for (i = 0; i < thread->authors_array->len; i++) {
+ author = (char *) g_ptr_array_index (thread->authors_array, i);
+ if (g_hash_table_lookup_extended (thread->matched_authors_hash,
+ author, NULL, NULL))
+ continue;
+ if (first_non_matched_author) {
+ thread->authors = talloc_asprintf (thread, "%s%s%s",
+ thread->authors,
+ authors_matched_sep,
+ author);
+ } else {
+ thread->authors = talloc_asprintf (thread, "%s%s%s",
+ thread->authors,
+ authors_sep,
+ author);
+ }
+
+ first_non_matched_author = 0;
+ }
+
+ g_ptr_array_free (thread->authors_array, true);
+ thread->authors_array = NULL;
+ g_ptr_array_free (thread->matched_authors_array, true);
+ thread->matched_authors_array = NULL;