From 179af7f436d8c21bad90e7eb88fc17c56f83c26c Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Mon, 14 Dec 2009 14:16:20 -0500
Subject: [PATCH] Preseve folder information when indexing.

---
 lib/database.cc |   14 +++++++++-----
 lib/index.cc    |    2 ++
 lib/notmuch.h   |    1 +
 notmuch-new.c   |   35 ++++++++++++++++++++++++++++++++++-
 4 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index b6c4d07..e6b4272 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -70,10 +70,9 @@ typedef struct {
  *
  *	MESSAGE_ID:	The unique ID of the mail mess (see "id" above)
  *
- * In addition, terms from the content of the message are added with
- * "from", "to", "attachment", and "subject" prefixes for use by the
- * user in searching. But the database doesn't really care itself
- * about any of these.
+ * In addition, terms from the content of the message are added with "from",
+ * "to", "attachment", "subject", and "folder" prefixes for use by the user in
+ * searching. But the database doesn't really care itself about any of these.
  *
  * Timestamp document
  * ------------------
@@ -124,7 +123,8 @@ prefix_t PROBABILISTIC_PREFIX[]= {
     { "from", "XFROM" },
     { "to", "XTO" },
     { "attachment", "XATTACHMENT" },
-    { "subject", "XSUBJECT"}
+    { "subject", "XSUBJECT"},
+    { "folder", "XFOLDER"}
 };
 
 int
@@ -889,6 +889,7 @@ _notmuch_database_link_message (notmuch_database_t *notmuch,
 notmuch_status_t
 notmuch_database_add_message (notmuch_database_t *notmuch,
 			      const char *filename,
+			      const char *folder_name,
 			      notmuch_message_t **message_ret)
 {
     notmuch_message_file_t *message_file;
@@ -1006,6 +1007,9 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
 
 	_notmuch_message_index_file (message, filename);
 
+        if (folder_name != NULL)
+            _notmuch_message_gen_terms (message, "folder", folder_name);
+
 	_notmuch_message_sync (message);
     } catch (const Xapian::Error &error) {
 	fprintf (stderr, "A Xapian exception occurred adding message: %s.\n",
diff --git a/lib/index.cc b/lib/index.cc
index 125fa6c..55f3fbc 100644
--- a/lib/index.cc
+++ b/lib/index.cc
@@ -116,6 +116,8 @@ skip_re_in_subject (const char *subject)
 	    s++;
 	if (strncasecmp (s, "re:", 3) == 0)
 	    s += 3;
+        else if (strncasecmp (s, "aw:", 3) == 0)
+	    s += 3;
 	else
 	    break;
     }
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 60834fb..5d3b3c6 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -265,6 +265,7 @@ notmuch_database_get_timestamp (notmuch_database_t *database,
 notmuch_status_t
 notmuch_database_add_message (notmuch_database_t *database,
 			      const char *filename,
+			      const char *folder_name,
 			      notmuch_message_t **message);
 
 /* Find a message with the given message_id.
diff --git a/notmuch-new.c b/notmuch-new.c
index 9d20616..bc8adc8 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -21,6 +21,7 @@
 #include "notmuch-client.h"
 
 #include <unistd.h>
+#include <glib.h>
 
 static volatile sig_atomic_t do_add_files_print_progress = 0;
 
@@ -144,6 +145,34 @@ add_files_recursive (notmuch_database_t *notmuch,
     struct dirent **namelist = NULL;
     int num_entries;
 
+    gchar *full_folder_name = NULL;
+    gchar *folder_base_name = NULL;
+
+    /* Find name of "folder" containing the email. */
+    full_folder_name = g_strdup(path);
+    while (1)
+    {
+        folder_base_name = g_path_get_basename(full_folder_name);
+
+        if (strcmp(folder_base_name, "cur") == 0
+                || strcmp(folder_base_name, "new") == 0)
+        {
+            gchar *parent_name = g_path_get_dirname(full_folder_name);
+            g_free(full_folder_name);
+            full_folder_name = parent_name;
+        }
+        else
+            break;
+    }
+
+    g_free(full_folder_name);
+
+    if (strcmp(folder_base_name, ".") == 0)
+    {
+        g_free(folder_base_name);
+        folder_base_name = NULL;
+    }
+
     /* If we're told to, we bail out on encountering a read-only
      * directory, (with this being a clear clue from the user to
      * Notmuch that new mail won't be arriving there and we need not
@@ -235,7 +264,9 @@ add_files_recursive (notmuch_database_t *notmuch,
 		    fflush (stdout);
 		}
 
-		status = notmuch_database_add_message (notmuch, next, &message);
+		status = notmuch_database_add_message (notmuch, next, 
+                                                       folder_base_name, 
+                                                       &message);
 		switch (status) {
 		    /* success */
 		    case NOTMUCH_STATUS_SUCCESS:
@@ -301,6 +332,8 @@ add_files_recursive (notmuch_database_t *notmuch,
 	closedir (dir);
     if (namelist)
 	free (namelist);
+    if (folder_base_name)
+        g_free(folder_base_name);
 
     return ret;
 }
-- 
1.6.5

