Re: [PATCH] notmuch: Add "maildir:" search option
authorPeter Zijlstra <peterz@infradead.org>
Wed, 13 Nov 2013 20:08:52 +0000 (21:08 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:58:17 +0000 (09:58 -0800)
26/29ce253bd24b814ae4cacc841a8703ff007641 [new file with mode: 0644]

diff --git a/26/29ce253bd24b814ae4cacc841a8703ff007641 b/26/29ce253bd24b814ae4cacc841a8703ff007641
new file mode 100644 (file)
index 0000000..7edbe7b
--- /dev/null
@@ -0,0 +1,282 @@
+Return-Path: <peterz@infradead.org>\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 3C0FD431E82\r
+       for <notmuch@notmuchmail.org>; Wed, 13 Nov 2013 12:09:02 -0800 (PST)\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 svZTLYEtPkGu for <notmuch@notmuchmail.org>;\r
+       Wed, 13 Nov 2013 12:08:56 -0800 (PST)\r
+Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134])\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 71331431FD0\r
+       for <notmuch@notmuchmail.org>; Wed, 13 Nov 2013 12:08:56 -0800 (PST)\r
+Received: from dhcp-077-248-225-117.chello.nl ([77.248.225.117] helo=laptop)\r
+       by merlin.infradead.org with esmtpsa (Exim 4.80.1 #2 (Red Hat Linux))\r
+       id 1VggkA-0004CF-5F; Wed, 13 Nov 2013 20:08:54 +0000\r
+Received: by laptop (Postfix, from userid 1000)\r
+       id 5CD05103BA2D3; Wed, 13 Nov 2013 21:08:52 +0100 (CET)\r
+Date: Wed, 13 Nov 2013 21:08:52 +0100\r
+From: Peter Zijlstra <peterz@infradead.org>\r
+To: Austin Clements <aclements@csail.mit.edu>\r
+Subject: Re: [PATCH] notmuch: Add "maildir:" search option\r
+Message-ID: <20131113200852.GG16796@laptop.programming.kicks-ass.net>\r
+References: <20131112155637.GA16796@laptop.programming.kicks-ass.net>\r
+       <87mwl94dte.fsf@awakening.csail.mit.edu>\r
+       <87k3gd4dfb.fsf@awakening.csail.mit.edu>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+In-Reply-To: <87k3gd4dfb.fsf@awakening.csail.mit.edu>\r
+User-Agent: Mutt/1.5.21 (2012-12-30)\r
+Cc: notmuch@notmuchmail.org\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, 13 Nov 2013 20:09:02 -0000\r
+\r
+On Tue, Nov 12, 2013 at 02:39:52PM -0500, Austin Clements wrote:\r
+> On Tue, 12 Nov 2013, Austin Clements <aclements@csail.mit.edu> wrote:\r
+> > I think this is a great idea.  Personally I think this is how folder:\r
+> > should work.  I find the semantics of folder: to be useless except where\r
+> > they happen to coincide with the boolean semantics used here.\r
+> > Unfortunately, changing folder: would require versioning the database,\r
+> > which we have only primordial support for right now.\r
+> >\r
+> > Various comments below, though nothing major.  Of course, we'd also need\r
+> > some tests and man page updates for this.\r
+> \r
+> Sorry, one important thing I missed: this doesn't correctly handle when\r
+> file names are removed from a message\r
+> (_notmuch_message_remove_filename).  Probably the simplest thing would\r
+> be to follow the template for how folder: works by first removing *all*\r
+> folder terms and then adding back the still-valid ones.  (Unfortunately,\r
+> just removing the term for the removed filename's directory won't work\r
+> because the message could have other filenames in the same directory,\r
+> though maybe you could just scan for that possibility?)\r
+\r
+Oh, right you are. A little something like the below? Its compile tested\r
+only and I've not yet had time to look at how the test infrastructure\r
+works.\r
+\r
+\r
+---\r
+ lib/database.cc |   3 +-\r
+ lib/message.cc  | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++-----\r
+ 2 files changed, 104 insertions(+), 10 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index a021bf17253c..e43e17dffcd0 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -208,7 +208,8 @@ static prefix_t BOOLEAN_PREFIX_EXTERNAL[] = {\r
+     { "thread",                       "G" },\r
+     { "tag",                  "K" },\r
+     { "is",                   "K" },\r
+-    { "id",                   "Q" }\r
++    { "id",                   "Q" },\r
++    { "maildir",              "XMAILDIR:" },\r
+ };\r
\r
+ static prefix_t PROBABILISTIC_PREFIX[]= {\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 1b4637950f8e..73d3bb65ab67 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -22,6 +22,7 @@\r
+ #include "database-private.h"\r
\r
+ #include <stdint.h>\r
++#include <string.h>\r
\r
+ #include <gmime/gmime.h>\r
\r
+@@ -473,6 +474,71 @@ notmuch_message_get_replies (notmuch_message_t *message)\r
+     return _notmuch_messages_create (message->replies);\r
+ }\r
\r
++/* Construct a proper 'maildir' from 'directory'\r
++ *\r
++ * Takes the relative directory component inside the maildir pathname and\r
++ * construct a maildir path from it.\r
++ *\r
++ * For filesystem layout Maildir we use the regular filesystem path except the\r
++ * trailing "cur"/"new" component.\r
++ *\r
++ * For Maildir++ we strip the leading '.' and replace subsequent '.'s with '/'s\r
++ */\r
++static char *\r
++_notmuch_message_maildir (void *ctx, const char *directory)\r
++{\r
++    char *maildir;\r
++    int i;\r
++\r
++    maildir = talloc_strdup (ctx, directory);\r
++    i = strlen (maildir);\r
++\r
++    /* Strip trailing '/' */\r
++    while (i && maildir[i - 1] == '/') {\r
++      maildir[i - 1] = '\0';\r
++      i--;\r
++    }\r
++\r
++    /* Strip leading '/' */\r
++    while (maildir[0] == '/') {\r
++      maildir++;\r
++      i--;\r
++    }\r
++\r
++    if (i >= 3) {\r
++      /* Consume trailing maildir directory entries */\r
++      if (STRNCMP_LITERAL (maildir, "cur") == 0 ||\r
++          STRNCMP_LITERAL (maildir, "new") == 0)\r
++      {\r
++          maildir[i - 3] = '\0';\r
++          i -= 3;\r
++      }\r
++\r
++      /* Strip trailing '/' */\r
++      while (i && maildir[i - 1] == '/') {\r
++          maildir[i-1] = '\0';\r
++          i--;\r
++      }\r
++    }\r
++\r
++    /* Maildir++ */\r
++    if (maildir[0] == '.') {\r
++      maildir++;\r
++\r
++      /* Replace all remaining '.' with '/' */\r
++      for (i = 0; maildir[i]; i++) {\r
++          if (maildir[i] == '.')\r
++              maildir[i] = '/';\r
++      }\r
++    }\r
++\r
++    /* If there's no string left, we're the "INBOX" */\r
++    if (maildir[0] == '\0')\r
++          maildir = talloc_strdup (ctx, "INBOX");\r
++\r
++    return maildir;\r
++}\r
++\r
+ /* Add an additional 'filename' for 'message'.\r
+  *\r
+  * This change will not be reflected in the database until the next\r
+@@ -485,6 +551,7 @@ _notmuch_message_add_filename (notmuch_message_t *message,\r
+     notmuch_status_t status;\r
+     void *local = talloc_new (message);\r
+     char *direntry;\r
++    char *maildir;\r
\r
+     if (filename == NULL)\r
+       INTERNAL_ERROR ("Message filename cannot be NULL.");\r
+@@ -507,6 +574,10 @@ _notmuch_message_add_filename (notmuch_message_t *message,\r
+     /* New terms allow user to search with folder: specification. */\r
+     _notmuch_message_gen_terms (message, "folder", directory);\r
\r
++    /* New terms allow user to serarch with maildir: specification. */\r
++    maildir = _notmuch_message_maildir (local, directory);\r
++    _notmuch_message_add_term (message, "maildir", maildir);\r
++\r
+     talloc_free (local);\r
\r
+     return NOTMUCH_STATUS_SUCCESS;\r
+@@ -535,11 +606,18 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+     void *local = talloc_new (message);\r
+     char *zfolder_prefix = talloc_asprintf(local, "Z%s", folder_prefix);\r
+     int zfolder_prefix_len = strlen (zfolder_prefix);\r
+-    char *direntry;\r
++    const char *relative, *directory;\r
++    char *direntry, *maildir;\r
+     notmuch_private_status_t private_status;\r
+     notmuch_status_t status;\r
+     Xapian::TermIterator i, last;\r
\r
++    relative = _notmuch_database_relative_path (message->notmuch, filename);\r
++\r
++    status = _notmuch_database_split_path (local, relative, &directory, NULL);\r
++    if (status)\r
++      return status;\r
++\r
+     status = _notmuch_database_filename_to_direntry (\r
+       local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);\r
+     if (status || !direntry)\r
+@@ -553,12 +631,21 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+     if (status)\r
+       return status;\r
\r
+-    /* Re-synchronize "folder:" terms for this message. This requires:\r
+-     *  1. removing all "folder:" terms\r
+-     *  2. removing all "folder:" stemmed terms\r
+-     *  3. adding back terms for all remaining filenames of the message. */\r
+-\r
+-    /* 1. removing all "folder:" terms */\r
++    /* Re-synchronize "folder:" and "maildir:" terms for this message. This\r
++     * requires:\r
++     *  1. removing "maildir:" for this filename\r
++     *  2. removing all "folder:" terms\r
++     *  3. removing all "folder:" stemmed terms\r
++     *\r
++     * For all remaining filenames of the message:\r
++     *  4. adding back "folder:" terms\r
++     *  5. adding back "maildir:" */\r
++\r
++    /* 1. remove "maildir:" for this message */\r
++    maildir = _notmuch_message_maildir (local, directory);\r
++    _notmuch_message_remove_term (message, "maildir", maildir);\r
++\r
++    /* 2. removing all "folder:" terms */\r
+     while (1) {\r
+       i = message->doc.termlist_begin ();\r
+       i.skip_to (folder_prefix);\r
+@@ -577,7 +664,7 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+       }\r
+     }\r
\r
+-    /* 2. removing all "folder:" stemmed terms */\r
++    /* 3. removing all "folder:" stemmed terms */\r
+     while (1) {\r
+       i = message->doc.termlist_begin ();\r
+       i.skip_to (zfolder_prefix);\r
+@@ -596,7 +683,7 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+       }\r
+     }\r
\r
+-    /* 3. adding back terms for all remaining filenames of the message. */\r
++    /* for all remaining filenames of the message */\r
+     i = message->doc.termlist_begin ();\r
+     i.skip_to (direntry_prefix);\r
\r
+@@ -623,8 +710,14 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+       directory = _notmuch_database_get_directory_path (local,\r
+                                                         message->notmuch,\r
+                                                         directory_id);\r
++\r
++      /* 4. adding back "folder:" terms */\r
+       if (strlen (directory))\r
+           _notmuch_message_gen_terms (message, "folder", directory);\r
++\r
++      /* 5. adding back "maildir:" */\r
++      maildir = _notmuch_message_maildir (local, directory);\r
++      _notmuch_message_add_term (message, "maildir", maildir);\r
+     }\r
\r
+     talloc_free (local);\r