[PATCH 07/25] lib/parse-sexp: parse 'subject'

Subject: [PATCH 07/25] lib/parse-sexp: parse 'subject'

Date: Sat, 17 Jul 2021 23:40:03 -0300

To: notmuch@notmuchmail.org

Cc: David Bremner

From: David Bremner


Initially this only works for lists of individual terms, which is a
bit inconvenient. This will be improved in a following commit.
---
 lib/parse-sexp.cc         | 37 +++++++++++++++++++++++++++++++++++++
 test/T081-sexpr-search.sh | 15 +++++++++++++++
 2 files changed, 52 insertions(+)

diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc
index dfbebf2b..898cfdd0 100644
--- a/lib/parse-sexp.cc
+++ b/lib/parse-sexp.cc
@@ -16,6 +16,17 @@ static _sexp_op_t operations[] =
     { }
 };
 
+typedef struct  {
+    const char *name;
+    Xapian::Query::op xapian_op;
+} _sexp_field_t;
+
+static _sexp_field_t fields[] =
+{
+    { "subject",        Xapian::Query::OP_PHRASE },
+    { }
+};
+
 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch, const sexp_t *sx,
 					       Xapian::Query &output);
 
@@ -61,6 +72,27 @@ _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *q
     return _sexp_to_xapian_query (notmuch, sx, output);
 }
 
+static notmuch_status_t
+_sexp_combine_field (const char *prefix,
+		     Xapian::Query::op operation,
+		     const sexp_t *sx,
+		     Xapian::Query &output)
+{
+    std::vector<std::string> terms;
+
+    for (const sexp_t *cur = sx; cur; cur = cur->next) {
+	std::string pref_str = prefix;
+	std::string word = cur->val;
+
+	if (operation == Xapian::Query::OP_PHRASE)
+	    word = Xapian::Unicode::tolower (word);
+
+	terms.push_back (pref_str + word);
+    }
+    output = Xapian::Query (operation, terms.begin (), terms.end ());
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
 /* 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 */
@@ -84,7 +116,12 @@ _sexp_to_xapian_query (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Qu
 	if (strcasecmp (op->name, hd_sexp (sx)->val) == 0) {
 	    return _sexp_combine_query (notmuch, op->xapian_op, op->initial, sx->list->next, output);
 	}
+    }
 
+    for (const _sexp_field_t *field = fields; field && field->name; field++) {
+	if (strcasecmp (field->name, hd_sexp (sx)->val) == 0)
+	    return _sexp_combine_field (_find_prefix (field->name), field->xapian_op, sx->list->next,
+					output);
     }
 
     _notmuch_database_log_append (notmuch, "unimplemented prefix %s\n", sx->list->val);
diff --git a/test/T081-sexpr-search.sh b/test/T081-sexpr-search.sh
index 41a82886..872f2603 100755
--- a/test/T081-sexpr-search.sh
+++ b/test/T081-sexpr-search.sh
@@ -19,6 +19,21 @@ for query in '(or)' '(not ())' '(not (not))' '(not (and))' \
     test_expect_equal_file /dev/null OUTPUT
 done
 
+test_begin_subtest "Search by subject"
+add_message [subject]=subjectsearchtest '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
+output=$(notmuch search --query-syntax=sexp '(subject subjectsearchtest)' | notmuch_search_sanitize)
+test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; subjectsearchtest (inbox unread)"
+
+test_begin_subtest "Search by subject (case insensitive)"
+notmuch search tag:inbox and subject:maildir | notmuch_search_sanitize > EXPECTED
+notmuch search --query-syntax=sexp '(subject Maildir)' | notmuch_search_sanitize > OUTPUT
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "Search by subject (utf-8):"
+add_message [subject]=utf8-sübjéct '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
+output=$(notmuch search --query-syntax=sexp '(subject utf8 sübjéct)' | notmuch_search_sanitize)
+test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; utf8-sübjéct (inbox unread)"
+
 test_begin_subtest "Unbalanced parens"
 # A code 1 indicates the error was handled (a crash will return e.g. 139).
 test_expect_code 1 "notmuch search --query-syntax=sexp '('"
-- 
2.30.2
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: