Rather than adding one prefix per user header, we create a single 'header' prefix that takes the configured name of the prefix as a parameter. --- lib/parse-sexp.cc | 50 +++++++++++++++++++++++ test/T081-sexpr-search.sh | 84 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc index 502b1be0..207403a5 100644 --- a/lib/parse-sexp.cc +++ b/lib/parse-sexp.cc @@ -55,6 +55,7 @@ static _sexp_field_t fields[] = { "date", Xapian::Query::OP_INVALID, SEXP_FLAG_NONE }, { "from", Xapian::Query::OP_PHRASE, SEXP_FLAG_REGEXP }, { "folder", Xapian::Query::OP_OR, SEXP_FLAG_REGEXP }, + { "header", Xapian::Query::OP_INVALID, SEXP_FLAG_WILDCARD }, { "id", Xapian::Query::OP_OR, SEXP_FLAG_REGEXP }, { "infix", Xapian::Query::OP_INVALID, SEXP_FLAG_NONE }, { "is", Xapian::Query::OP_AND, SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEXP }, @@ -265,6 +266,52 @@ _sexp_parse_infix (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query return NOTMUCH_STATUS_SUCCESS; } +static notmuch_status_t +_sexp_parse_header (notmuch_database_t *notmuch, _sexp_flag_t flags, const sexp_t *sx, + Xapian::Query &output) +{ + const char *term_prefix; + _sexp_flag_t new_flags; + const sexp_t *rest; + notmuch_status_t status; + + if (! sx) { + _notmuch_database_log (notmuch, "missing header name\n"); + return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; + } + + if (sx->ty != SEXP_VALUE) { + _notmuch_database_log (notmuch, "header name must be atom, not list\n"); + return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; + } + + term_prefix = _notmuch_string_map_get (notmuch->user_prefix, sx->val); + if (! term_prefix) { + _notmuch_database_log (notmuch, "unknown header name %s\n", sx->val); + return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; + } + + status = _sexp_parse_keywords (notmuch, "header", sx->next, SEXP_FLAG_WILDCARD, new_flags, rest); + if (status) + return status; + + if ((new_flags | flags) & SEXP_FLAG_WILDCARD) { + if (rest) { + _notmuch_database_log (notmuch, "extra term(s) after wildcard\n", sx->val); + return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; + } + output = Xapian::Query (Xapian::Query::OP_WILDCARD, term_prefix); + return NOTMUCH_STATUS_SUCCESS; + } + + if (! rest) { + _notmuch_database_log (notmuch, "missing header terms\n"); + return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; + } + + return _sexp_combine_field (term_prefix, Xapian::Query::OP_PHRASE, rest, output); +} + /* Here we expect the s-expression to be a proper list, with first * element defining and operation, or as a special case the empty * list */ @@ -317,6 +364,9 @@ _notmuch_sexp_to_xapian_query (notmuch_database_t *notmuch, const sexp_t *sx, Xa if (strcasecmp (field->name, "date") == 0) { return _sexp_parse_date (notmuch, sx, output); } + if (strcasecmp (field->name, "header") == 0) { + return _sexp_parse_header (notmuch, flags, rest, output); + } if (strcasecmp (field->name, "query") == 0) { if (! sx->list->next || ! sx->list->next->val) { _notmuch_database_log (notmuch, "missing query name\n"); diff --git a/test/T081-sexpr-search.sh b/test/T081-sexpr-search.sh index 45568ba5..ae76a6e6 100755 --- a/test/T081-sexpr-search.sh +++ b/test/T081-sexpr-search.sh @@ -506,4 +506,88 @@ notmuch search date:2009-11-18..2009-11-18 and tag:unread > EXPECTED notmuch search --query-syntax=sexp '(and (infix "date:2009-11-18..2009-11-18") (infix "tag:unread"))' > OUTPUT test_expect_equal_file EXPECTED OUTPUT +test_begin_subtest "user header (no name)" +notmuch search --query-syntax=sexp '(header)' >& OUTPUT +cat <<EOF > EXPECTED +notmuch search: Syntax error in query +missing header name +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "user header (illegal name)" +notmuch search --query-syntax=sexp '(header (what?))' >& OUTPUT +cat <<EOF > EXPECTED +notmuch search: Syntax error in query +header name must be atom, not list +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "user header (unknown header)" +notmuch search --query-syntax=sexp '(header FooBar)' >& OUTPUT +cat <<EOF > EXPECTED +notmuch search: Syntax error in query +unknown header name FooBar +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "adding user header" +test_expect_code 0 "notmuch config set index.header.List \"List-Id\"" + +test_begin_subtest "user header (no terms)" +notmuch search --query-syntax=sexp '(header List)' >& OUTPUT +cat <<EOF > EXPECTED +notmuch search: Syntax error in query +missing header terms +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "reindexing" +test_expect_code 0 'notmuch reindex "*"' + +test_begin_subtest "wildcard search for user header" +grep -Ril List-Id ${MAIL_DIR} | sort | notmuch_dir_sanitize > EXPECTED +notmuch search --output=files --query-syntax=sexp '(header :any List)' | sort | notmuch_dir_sanitize > OUTPUT +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "wildcard search for user header: extra tokens" +notmuch search --query-syntax=sexp '(header :any List trailing-garbage)' >& OUTPUT +cat <<EOF > EXPECTED +notmuch search: Syntax error in query +extra term(s) after wildcard +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "wildcard search for user header: extra tokens (2)" +notmuch search --query-syntax=sexp '(header List :any trailing-garbage)' >& OUTPUT +cat <<EOF > EXPECTED +notmuch search: Syntax error in query +extra term(s) after wildcard +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "wildcard search for user header (post header flag)" +grep -Ril List-Id ${MAIL_DIR} | sort | notmuch_dir_sanitize > EXPECTED +notmuch search --output=files --query-syntax=sexp '(header List :any)' | sort | notmuch_dir_sanitize > OUTPUT +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "search for user header" +notmuch search List:notmuch | notmuch_search_sanitize > EXPECTED +notmuch search --query-syntax=sexp '(header List notmuch)' | notmuch_search_sanitize > OUTPUT +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "search for user header (list token)" +notmuch search List:notmuch | notmuch_search_sanitize > EXPECTED +notmuch search --query-syntax=sexp '(header List notmuch.notmuchmail.org)' | notmuch_search_sanitize > OUTPUT +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "search for user header (quoted string)" +notmuch search 'List:"notmuch notmuchmail org"' | notmuch_search_sanitize > EXPECTED +notmuch search --query-syntax=sexp '(header List "notmuch notmuchmail org")' | notmuch_search_sanitize > OUTPUT +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "search for user header (atoms)" +notmuch search 'List:"notmuch notmuchmail org"' | notmuch_search_sanitize > EXPECTED +notmuch search --query-syntax=sexp '(header List notmuch notmuchmail org)' | notmuch_search_sanitize > OUTPUT +test_expect_equal_file EXPECTED OUTPUT + test_done -- 2.30.2 _______________________________________________ notmuch mailing list -- notmuch@notmuchmail.org To unsubscribe send an email to notmuch-leave@notmuchmail.org