[PATCH 8/8] Support before: and after: date search with sane date syntax.

Austin Clements amdragon at MIT.EDU
Sun Jan 16 00:10:58 PST 2011


Gmail-compatible date search operators, where the date must be
specified as yyyy/mm/dd.  This is just a start; it would be great to
support fancier date syntax and, in particular, relative date
specifications.
---
 lib/database.cc |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index 20fd412..cb02220 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -633,6 +633,49 @@ transform_folder (_notmuch_token_t *root, void *opaque)
     return root;
 }
 
+static _notmuch_token_t *
+transform_date (_notmuch_token_t *root, void *opaque)
+{
+    if (!root)
+	return NULL;
+    if (root->type == TOK_PREFIX) {
+	int after = (strcmp (root->text, "after") == 0);
+	int before = (strcmp (root->text, "before") == 0);
+	if (after || before) {
+	    _notmuch_token_t *tok = root->left;
+	    struct tm t;
+	    time_t time_value;
+	    char *end, *serialzed;
+
+	    /* Parse the date */
+	    assert (tok && tok->type == TOK_LIT);
+	    memset(&t, 0, sizeof(t));
+	    end = strptime (tok->text, "%Y/%m/%d", &t);
+	    time_value = mktime(&t);
+	    if (!end || *end || time_value == -1) {
+		char *msg = talloc_asprintf (root, "Invalid date \"%s\"",
+					     tok->text);
+		return _notmuch_token_create_term (root, TOK_ERROR, msg);
+	    }
+
+	    /* Construct the date query */
+	    tok = _notmuch_token_create_term (root, TOK_RANGE, NULL);
+	    tok->valueno = NOTMUCH_VALUE_TIMESTAMP;
+	    serialzed = talloc_strdup
+		(tok, Xapian::sortable_serialise (time_value).c_str ());
+	    if (after)
+		tok->rangeBegin = serialzed;
+	    else
+		tok->rangeEnd = serialzed;
+	    return tok;
+	}
+    }
+
+    root->left = transform_date (root->left, opaque);
+    root->right = transform_date (root->right, opaque);
+    return root;
+}
+
 notmuch_database_t *
 notmuch_database_open (const char *path,
 		       notmuch_database_mode_t mode)
@@ -746,6 +789,13 @@ notmuch_database_open (const char *path,
 				     TRUE, TRUE);
 	_notmuch_qparser_add_transform (notmuch->query_parser,
 					transform_folder, notmuch);
+
+	_notmuch_qparser_add_prefix (notmuch->query_parser, "after",
+				     TRUE, TRUE);
+	_notmuch_qparser_add_prefix (notmuch->query_parser, "before",
+				     TRUE, TRUE);
+	_notmuch_qparser_add_transform (notmuch->query_parser,
+					transform_date, NULL);
     } catch (const Xapian::Error &error) {
 	fprintf (stderr, "A Xapian exception occurred opening database: %s\n",
 		 error.get_msg().c_str());
-- 
1.7.2.3



More information about the notmuch mailing list