]> git.notmuchmail.org Git - notmuch/blobdiff - lib/parse-sexp.cc
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / lib / parse-sexp.cc
index 356c32ea254675749e4883743ba75f6fdd648121..9cadbc1359d3fcb2d3813bab7eae5d0a91d6f096 100644 (file)
@@ -32,6 +32,8 @@ typedef enum {
     SEXP_FLAG_EXPAND   = 1 << 6,
     SEXP_FLAG_DO_EXPAND = 1 << 7,
     SEXP_FLAG_ORPHAN   = 1 << 8,
+    SEXP_FLAG_RANGE    = 1 << 9,
+    SEXP_FLAG_PATHNAME = 1 << 10,
 } _sexp_flag_t;
 
 /*
@@ -66,16 +68,21 @@ static _sexp_prefix_t prefixes[] =
       SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
     { "body",           Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
       SEXP_FLAG_FIELD },
+    { "date",           Xapian::Query::OP_INVALID,      Xapian::Query::MatchAll,
+      SEXP_FLAG_RANGE },
     { "from",           Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
       SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
     { "folder",         Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
-      SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
+      SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND |
+      SEXP_FLAG_PATHNAME },
     { "id",             Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
     { "infix",          Xapian::Query::OP_INVALID,      Xapian::Query::MatchAll,
       SEXP_FLAG_SINGLE | SEXP_FLAG_ORPHAN },
     { "is",             Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
+    { "lastmod",           Xapian::Query::OP_INVALID,      Xapian::Query::MatchAll,
+      SEXP_FLAG_RANGE },
     { "matching",       Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
       SEXP_FLAG_DO_EXPAND },
     { "mid",            Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
@@ -89,7 +96,8 @@ static _sexp_prefix_t prefixes[] =
     { "or",             Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
       SEXP_FLAG_NONE },
     { "path",           Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
-      SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
+      SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX |
+      SEXP_FLAG_PATHNAME },
     { "property",       Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
     { "query",          Xapian::Query::OP_INVALID,      Xapian::Query::MatchNothing,
@@ -179,6 +187,55 @@ _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query &
     return NOTMUCH_STATUS_SUCCESS;
 }
 
+static notmuch_status_t
+resolve_binding (notmuch_database_t *notmuch, const _sexp_binding_t *env, const char *name,
+                const _sexp_binding_t **out)
+{
+    for (; env; env = env->next) {
+       if (strcmp (name, env->name) == 0) {
+           *out = env;
+           return NOTMUCH_STATUS_SUCCESS;
+       }
+    }
+
+    _notmuch_database_log (notmuch, "undefined parameter '%s'\n", name);
+    return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
+}
+
+static notmuch_status_t
+_sexp_expand_term (notmuch_database_t *notmuch,
+                  const _sexp_prefix_t *prefix,
+                  const _sexp_binding_t *env,
+                  const sexp_t *sx,
+                  const char **out)
+{
+    notmuch_status_t status;
+
+    if (! out)
+       return NOTMUCH_STATUS_NULL_POINTER;
+
+    while (sx->ty == SEXP_VALUE && sx->aty == SEXP_BASIC && sx->val[0] == ',') {
+       const char *name = sx->val + 1;
+       const _sexp_binding_t *binding;
+
+       status = resolve_binding (notmuch, env, name, &binding);
+       if (status)
+           return status;
+
+       sx = binding->sx;
+       env = binding->context;
+    }
+
+    if (sx->ty != SEXP_VALUE) {
+       _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
+                              prefix->name);
+       return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
+    }
+
+    *out = sx->val;
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
 static notmuch_status_t
 _sexp_parse_wildcard (notmuch_database_t *notmuch,
                      const _sexp_prefix_t *parent,
@@ -219,8 +276,8 @@ _sexp_parse_one_term (notmuch_database_t *notmuch, std::string term_prefix, cons
 notmuch_status_t
 _sexp_parse_regex (notmuch_database_t *notmuch,
                   const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
-                  unused(const _sexp_binding_t *env),
-                  std::string val, Xapian::Query &output)
+                  const _sexp_binding_t *env,
+                  const sexp_t *term, Xapian::Query &output)
 {
     if (! parent) {
        _notmuch_database_log (notmuch, "illegal '%s' outside field\n",
@@ -235,9 +292,15 @@ _sexp_parse_regex (notmuch_database_t *notmuch,
     }
 
     std::string msg; /* ignored */
+    const char *str;
+    notmuch_status_t status;
+
+    status = _sexp_expand_term (notmuch, prefix, env, term, &str);
+    if (status)
+       return status;
 
     return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, parent->name,
-                                    val, output, msg);
+                                    str, output, msg);
 }
 
 
@@ -436,13 +499,80 @@ _sexp_expand_param (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
                    const _sexp_binding_t *env, const char *name,
                    Xapian::Query &output)
 {
-    for (; env; env = env->next) {
-       if (strcmp (name, env->name) == 0) {
-           return _sexp_to_xapian_query (notmuch, parent, env->context, env->sx,
-                                         output);
+    notmuch_status_t status;
+
+    const _sexp_binding_t *binding;
+
+    status = resolve_binding (notmuch, env, name, &binding);
+    if (status)
+       return status;
+
+    return _sexp_to_xapian_query (notmuch, parent, binding->context, binding->sx,
+                                 output);
+}
+
+static notmuch_status_t
+_sexp_parse_range (notmuch_database_t *notmuch,  const _sexp_prefix_t *prefix,
+                  const sexp_t *sx, Xapian::Query &output)
+{
+    const char *from, *to;
+    std::string msg;
+
+    /* empty range matches everything */
+    if (! sx) {
+       output = Xapian::Query::MatchAll;
+       return NOTMUCH_STATUS_SUCCESS;
+    }
+
+    if (sx->ty == SEXP_LIST) {
+       _notmuch_database_log (notmuch, "expected atom as first argument of '%s'\n", prefix->name);
+       return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
+    }
+
+    from = sx->val;
+    if (strcmp (from, "*") == 0)
+       from = "";
+
+    to = from;
+
+    if (sx->next) {
+       if (sx->next->ty == SEXP_LIST) {
+           _notmuch_database_log (notmuch, "expected atom as second argument of '%s'\n",
+                                  prefix->name);
+           return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
+       }
+
+       if (sx->next->next) {
+           _notmuch_database_log (notmuch, "'%s' expects maximum of two arguments\n", prefix->name);
+           return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
+       }
+
+       to = sx->next->val;
+       if (strcmp (to, "*") == 0)
+           to = "";
+    }
+
+    if (strcmp (prefix->name, "date") == 0) {
+       notmuch_status_t status;
+       status = _notmuch_date_strings_to_query (NOTMUCH_VALUE_TIMESTAMP, from, to, output, msg);
+       if (status) {
+           if (! msg.empty ())
+               _notmuch_database_log (notmuch, "%s\n", msg.c_str ());
+       }
+       return status;
+    }
+
+    if (strcmp (prefix->name, "lastmod") == 0) {
+       notmuch_status_t status;
+       status = _notmuch_lastmod_strings_to_query (notmuch, from, to, output, msg);
+       if (status) {
+           if (! msg.empty ())
+               _notmuch_database_log (notmuch, "%s\n", msg.c_str ());
        }
+       return status;
     }
-    _notmuch_database_log (notmuch, "undefined parameter %s\n", name);
+
+    _notmuch_database_log (notmuch, "unimplimented range prefix: '%s'\n", prefix->name);
     return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
 }
 
@@ -467,8 +597,13 @@ _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent
            return _sexp_parse_wildcard (notmuch, parent, env, "", output);
        }
 
+       char *atom = sx->val;
+
+       if (parent && parent->flags & SEXP_FLAG_PATHNAME)
+           strip_trailing (atom, '/');
+
        if (parent && (parent->flags & SEXP_FLAG_BOOLEAN)) {
-           output = Xapian::Query (term_prefix + sx->val);
+           output = Xapian::Query (term_prefix + atom);
            return NOTMUCH_STATUS_SUCCESS;
        }
 
@@ -519,7 +654,7 @@ _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent
 
     for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
        if (strcmp (prefix->name, sx->list->val) == 0) {
-           if (prefix->flags & SEXP_FLAG_FIELD) {
+           if (prefix->flags & (SEXP_FLAG_FIELD | SEXP_FLAG_RANGE)) {
                if (parent) {
                    _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
                                           prefix->name, parent->name);
@@ -541,6 +676,9 @@ _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent
                return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
            }
 
+           if (prefix->flags & SEXP_FLAG_RANGE)
+               return _sexp_parse_range (notmuch, prefix, sx->list->next, output);
+
            if (strcmp (prefix->name, "infix") == 0) {
                return _sexp_parse_infix (notmuch, sx->list->next, output);
            }
@@ -549,11 +687,17 @@ _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent
                return _notmuch_query_name_to_query (notmuch, sx->list->next->val, output);
            }
 
-           if (prefix->xapian_op == Xapian::Query::OP_WILDCARD)
-               return _sexp_parse_wildcard (notmuch, parent, env, sx->list->next->val, output);
+           if (prefix->xapian_op == Xapian::Query::OP_WILDCARD) {
+               const char *str;
+               status = _sexp_expand_term (notmuch, prefix, env, sx->list->next, &str);
+               if (status)
+                   return status;
+
+               return _sexp_parse_wildcard (notmuch, parent, env, str, output);
+           }
 
            if (prefix->flags & SEXP_FLAG_DO_REGEX) {
-               return _sexp_parse_regex (notmuch, prefix, parent, env, sx->list->next->val, output);
+               return _sexp_parse_regex (notmuch, prefix, parent, env, sx->list->next, output);
            }
 
            if (prefix->flags & SEXP_FLAG_DO_EXPAND) {