diff --git a/doc/man7/notmuch-sexp-queries.rst b/doc/man7/notmuch-sexp-queries.rst index d28f40bb..422154c7 100644 --- a/doc/man7/notmuch-sexp-queries.rst +++ b/doc/man7/notmuch-sexp-queries.rst @@ -125,6 +125,9 @@ bounds. Either upper or lower bound may be specified as ``""`` or ``*`` to specify the lowest possible lower bound or highest possible upper bound. +``lastmod`` ranges support negative arguments, interpreted relative to +the most recent database revision (see :option:`count --lastmod`). + .. _field-table: .. table:: Fields with supported modifiers diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc index 0f14d8b7..e9ef4268 100644 --- a/lib/parse-sexp.cc +++ b/lib/parse-sexp.cc @@ -575,6 +575,9 @@ _sexp_parse_range (notmuch_database_t *notmuch, const _sexp_prefix_t *prefix, return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; } + if (from_idx < 0) + from_idx += notmuch_database_get_revision (notmuch, NULL); + try { if (EMPTY_STRING (to)) to_idx = LONG_MAX; @@ -585,6 +588,9 @@ _sexp_parse_range (notmuch_database_t *notmuch, const _sexp_prefix_t *prefix, return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; } + if (to_idx < 0) + to_idx += notmuch_database_get_revision (notmuch, NULL); + output = Xapian::Query (Xapian::Query::OP_VALUE_RANGE, NOTMUCH_VALUE_LAST_MOD, Xapian::sortable_serialise (from_idx), Xapian::sortable_serialise (to_idx)); diff --git a/test/T081-sexpr-search.sh b/test/T081-sexpr-search.sh index ce6b11b6..0c7db9c2 100755 --- a/test/T081-sexpr-search.sh +++ b/test/T081-sexpr-search.sh @@ -934,6 +934,14 @@ notmuch search lastmod:$revision..$revision | notmuch_search_sanitize > EXPECTED notmuch search --query=sexp "(and (lastmod $revision))" | notmuch_search_sanitize > OUTPUT test_expect_equal_file EXPECTED OUTPUT +test_begin_subtest "lastmod query, one argument (negative)" +notmuch tag +4EFC743A.3060609@april.org id:4EFC743A.3060609@april.org +revision=$(notmuch count --lastmod '*' | cut -f3) +revision1=$((revision - 1)) +notmuch search lastmod:$revision1..$revision1 | notmuch_search_sanitize > EXPECTED +notmuch search --query=sexp "(lastmod -1)" | notmuch_search_sanitize > OUTPUT +test_expect_equal_file_nonempty EXPECTED OUTPUT + test_begin_subtest "lastmod query, two arguments" notmuch tag +keithp from:keithp revision2=$(notmuch count --lastmod '*' | cut -f3) @@ -941,16 +949,38 @@ notmuch search lastmod:$revision..$revision2 | notmuch_search_sanitize > EXPECTE notmuch search --query=sexp "(and (lastmod $revision $revision2))" | notmuch_search_sanitize > OUTPUT test_expect_equal_file EXPECTED OUTPUT +test_begin_subtest "lastmod query, two arguments, first negative" +revdiff=$((revision2 - revision)) +notmuch search lastmod:$revision..$revision2 | notmuch_search_sanitize > EXPECTED +notmuch search --query=sexp "(lastmod -$revdiff $revision2)" | notmuch_search_sanitize > OUTPUT +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "lastmod query, two arguments, second negative" +revdiff=$((revision2 - revision)) +notmuch search lastmod:..$revision | notmuch_search_sanitize > EXPECTED +notmuch search --query=sexp "(lastmod 0 -$revdiff)" | notmuch_search_sanitize > OUTPUT +test_expect_equal_file EXPECTED OUTPUT + test_begin_subtest "lastmod query, lower bound only" notmuch search lastmod:$revision.. | notmuch_search_sanitize > EXPECTED notmuch search --query=sexp "(lastmod $revision \"\")" | notmuch_search_sanitize > OUTPUT test_expect_equal_file_nonempty EXPECTED OUTPUT +test_begin_subtest "lastmod query, lower bound only (negative)" +notmuch search lastmod:$revision.. | notmuch_search_sanitize > EXPECTED +notmuch search --query=sexp "(lastmod -$revdiff \"\")" | notmuch_search_sanitize > OUTPUT +test_expect_equal_file_nonempty EXPECTED OUTPUT + test_begin_subtest "lastmod query, upper bound only" notmuch search lastmod:..$revision2 | notmuch_search_sanitize > EXPECTED notmuch search --query=sexp "(lastmod \"\" $revision2)" | notmuch_search_sanitize > OUTPUT test_expect_equal_file_nonempty EXPECTED OUTPUT +test_begin_subtest "lastmod query, upper bound only (negative)" +notmuch search lastmod:..$revision | notmuch_search_sanitize > EXPECTED +notmuch search --query=sexp "(lastmod \"\" -$revdiff)" | notmuch_search_sanitize > OUTPUT +test_expect_equal_file_nonempty EXPECTED OUTPUT + test_begin_subtest "lastmod query, lower bound only, using *" notmuch search lastmod:$revision.. | notmuch_search_sanitize > EXPECTED notmuch search --query=sexp "(lastmod $revision *)" | notmuch_search_sanitize > OUTPUT diff --git a/test/T570-revision-tracking.sh b/test/T570-revision-tracking.sh index e1cc684d..aaa45468 100755 --- a/test/T570-revision-tracking.sh +++ b/test/T570-revision-tracking.sh @@ -95,4 +95,12 @@ subtotal=$(notmuch count lastmod:..$lastmod) result=$(($subtotal == $total-1)) test_expect_equal 1 "$result" +if [ $NOTMUCH_HAVE_SFSEXP -eq 1 ]; then + test_begin_subtest 'exclude one message using negative lastmod (sexp)' + total=$(notmuch count '*') + notmuch tag +${RANDOM} id:4EFC743A.3060609@april.org + count=$(notmuch count --query=sexp '(lastmod -1 *)') + test_expect_equal 1 "$count" +fi + test_done