Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id A65BA429E2F for ; Sun, 16 Jan 2011 00:11:40 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_NONE=-0.0001] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PRQ56k3oj6TA for ; Sun, 16 Jan 2011 00:11:39 -0800 (PST) Received: from dmz-mailsec-scanner-7.mit.edu (DMZ-MAILSEC-SCANNER-7.MIT.EDU [18.7.68.36]) by olra.theworths.org (Postfix) with ESMTP id F0BF442D29F for ; Sun, 16 Jan 2011 00:11:26 -0800 (PST) X-AuditID: 12074424-b7b0bae000000a05-e1-4d32a82eb16a Received: from mailhub-auth-3.mit.edu ( [18.9.21.43]) by dmz-mailsec-scanner-7.mit.edu (Symantec Brightmail Gateway) with SMTP id CD.6D.02565.E28A23D4; Sun, 16 Jan 2011 03:11:26 -0500 (EST) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-3.mit.edu (8.13.8/8.9.2) with ESMTP id p0G8BP0S013772; Sun, 16 Jan 2011 03:11:25 -0500 Received: from drake.mit.edu (a074.catapulsion.net [70.36.81.74]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id p0G8BNVE010531 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Sun, 16 Jan 2011 03:11:25 -0500 (EST) Received: from amthrax by drake.mit.edu with local (Exim 4.72) (envelope-from ) id 1PeNhr-0002Xj-L4; Sun, 16 Jan 2011 03:11:23 -0500 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH 8/8] Support before: and after: date search with sane date syntax. Date: Sun, 16 Jan 2011 03:10:58 -0500 Message-Id: <1295165458-9573-9-git-send-email-amdragon@mit.edu> X-Mailer: git-send-email 1.7.2.3 In-Reply-To: <1295165458-9573-1-git-send-email-amdragon@mit.edu> References: <1295165458-9573-1-git-send-email-amdragon@mit.edu> X-Brightmail-Tracker: AAAAAA== Cc: amdragon@mit.edu X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 16 Jan 2011 08:11:41 -0000 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