--- /dev/null
+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 18337431FBF\r
+ for <notmuch@notmuchmail.org>; Tue, 12 Nov 2013 07:56:52 -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 hei5nDsg-TOx for <notmuch@notmuchmail.org>;\r
+ Tue, 12 Nov 2013 07:56:40 -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 8FF09431FAF\r
+ for <notmuch@notmuchmail.org>; Tue, 12 Nov 2013 07:56:40 -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 1VgGKV-0002kK-CS\r
+ for notmuch@notmuchmail.org; Tue, 12 Nov 2013 15:56:39 +0000\r
+Received: by laptop (Postfix, from userid 1000)\r
+ id 85663103E57BE; Tue, 12 Nov 2013 16:56:37 +0100 (CET)\r
+Date: Tue, 12 Nov 2013 16:56:37 +0100\r
+From: Peter Zijlstra <peterz@infradead.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH] notmuch: Add "maildir:" search option\r
+Message-ID: <20131112155637.GA16796@laptop.programming.kicks-ass.net>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+User-Agent: Mutt/1.5.21 (2012-12-30)\r
+X-Mailman-Approved-At: Tue, 12 Nov 2013 08:02:58 -0800\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: Tue, 12 Nov 2013 15:56:52 -0000\r
+\r
+Subject: notmuch: Add "maildir:" search option\r
+\r
+The current "folder:" search terms are near useless when you have\r
+recursive folders, introduce a boolean "maildir:" search term to\r
+exactly match the maildir folder.\r
+\r
+Given a Maildir++ layout like:\r
+\r
+ ~/Maildir/\r
+ ~/Maildir/cur\r
+ ~/Maildir/new\r
+ ~/Maildir/tmp\r
+ ~/Maildir/.Sent\r
+ ~/Maildir/.Sent/cur\r
+ ~/Maildir/.Sent/new\r
+ ~/Maildir/.Sent/tmp\r
+ ~/Maildir/.INBOX.LKML\r
+ ~/Maildir/.INBOX.LKML/cur\r
+ ~/Maildir/.INBOX.LKML/new\r
+ ~/Maildir/.INBOX.LKML/tmp\r
+ ~/Maildir/.INBOX.LKML.netdev\r
+ ~/Maildir/.INBOX.LKML.netdev/cur\r
+ ~/Maildir/.INBOX.LKML.netdev/new\r
+ ~/Maildir/.INBOX.LKML.netdev/tmp\r
+ ~/Maildir/.INBOX.LKML.arch\r
+ ~/Maildir/.INBOX.LKML.arch/cur\r
+ ~/Maildir/.INBOX.LKML.arch/new\r
+ ~/Maildir/.INBOX.LKML.arch/tmp\r
+\r
+This patch generates the following search index:\r
+\r
+ $ delve -a Maildir/.notmuch/xapian/ | ~/s XMAILDIR\r
+ XMAILDIR:INBOX\r
+ XMAILDIR:INBOX/LKML\r
+ XMAILDIR:INBOX/LKML/arch\r
+ XMAILDIR:INBOX/LKML/netdev\r
+ XMAILDIR:Sent\r
+\r
+Which allows one (me!!1) to pose queries like:\r
+\r
+ maildir:INBOX and not tag:list\r
+\r
+to more easily find offlist mail (from people like my family who don't\r
+actually send their stuff over LKML :-).\r
+\r
+Signed-off-by: Peter Zijlstra <peterz@infradead.org>\r
+---\r
+\r
+XXX: now I need to go figure out how to do searches like:\r
+\r
+ subject:PATCH/0\r
+\r
+which would mandate that PATCH is the first word occurring in the\r
+subject. I think the position index holds enough information but I\r
+need to look into that and obviously the query parser needs work for\r
+this.\r
+\r
+\r
+ lib/database.cc | 7 ++++---\r
+ lib/message.cc | 40 ++++++++++++++++++++++++++++++++++++++++\r
+ 2 files changed, 44 insertions(+), 3 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index a021bf17253c..53aeaa68954d 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -208,15 +208,16 @@ 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
+ { "from", "XFROM" },\r
+ { "to", "XTO" },\r
+ { "attachment", "XATTACHMENT" },\r
+- { "subject", "XSUBJECT"},\r
+- { "folder", "XFOLDER"}\r
++ { "subject", "XSUBJECT" },\r
++ { "folder", "XFOLDER" },\r
+ };\r
+ \r
+ const char *\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 1b4637950f8e..45a727a6208f 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
+@@ -485,6 +486,8 @@ _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
++ int i;\r
+ \r
+ if (filename == NULL)\r
+ INTERNAL_ERROR ("Message filename cannot be NULL.");\r
+@@ -507,6 +510,43 @@ _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
++ /* Convert the directory into a maildir path */\r
++ maildir = talloc_strdup(local, directory);\r
++\r
++ /* Strip the maildir "cur", "new" directory entries. */\r
++ i = strlen(maildir);\r
++ if (strncmp(maildir + i - 3, "cur", 3) == 0 ||\r
++ strncmp(maildir + i - 3, "new", 3) == 0) {\r
++ maildir[i - 3] = '\0';\r
++ i -= 3;\r
++ }\r
++\r
++ /* Strip trailing '/' */\r
++ while (maildir[i-1] == '/') {\r
++ maildir[i-1] = '\0';\r
++ i--;\r
++ }\r
++\r
++ /* Strip leading '/' */\r
++ while (maildir[0] == '/')\r
++ maildir++;\r
++\r
++ /* Strip leading '.' */\r
++ while (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
++ /* If there's no string left, we're the "INBOX" */\r
++ if (maildir[0] == '\0')\r
++ maildir = talloc_strdup(local, "INBOX");\r
++\r
++ _notmuch_message_add_term (message, "maildir", maildir);\r
++\r
+ talloc_free (local);\r
+ \r
+ return NOTMUCH_STATUS_SUCCESS;\r