aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-10-31 11:41:08 +0100
committerDanilo M. <danix@danix.xyz>2026-10-31 11:41:08 +0100
commit8804a75ae561a7e504fdce8aea58d3ddc8670b71 (patch)
tree85125d2ba1e1880f3439e7d3462247351c46ff1d
parent8078e8180920984de475db796a2725bb2f470dd8 (diff)
downloadqtmaildir-8804a75ae561a7e504fdce8aea58d3ddc8670b71.tar.gz
qtmaildir-8804a75ae561a7e504fdce8aea58d3ddc8670b71.zip
feat: build the query for what a tag change would move
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
-rw-r--r--src/searchterm.cpp21
-rw-r--r--src/searchterm.h14
-rw-r--r--tests/test_searchterm.cpp44
3 files changed, 79 insertions, 0 deletions
diff --git a/src/searchterm.cpp b/src/searchterm.cpp
index 16936df..06d03be 100644
--- a/src/searchterm.cpp
+++ b/src/searchterm.cpp
@@ -18,6 +18,8 @@
#include "searchterm.h"
+#include "config.h"
+
#include <algorithm>
namespace SearchTerm {
@@ -108,4 +110,23 @@ QString exclude(const QString &existing, const QString &addition)
return QStringLiteral("(%1) AND NOT (%2)").arg(left, right);
}
+QString changes(const QString &query, const QStringList &add,
+ const QStringList &remove)
+{
+ QStringList terms;
+ for (const QString &name : add) {
+ const QString term = tag(name);
+ if (!term.isEmpty())
+ terms.append(QStringLiteral("not ") + term);
+ }
+ for (const QString &name : remove) {
+ const QString term = tag(name);
+ if (!term.isEmpty())
+ terms.append(term);
+ }
+ if (query.trimmed().isEmpty() || terms.isEmpty())
+ return Config::matchNothingQuery();
+ return extend(query, terms.join(QStringLiteral(" or ")));
+}
+
} // namespace SearchTerm
diff --git a/src/searchterm.h b/src/searchterm.h
index 8ca67e4..dd43e99 100644
--- a/src/searchterm.h
+++ b/src/searchterm.h
@@ -111,6 +111,20 @@ QString extend(const QString &existing, const QString &addition);
/// An empty `addition` leaves `existing` untouched.
QString exclude(const QString &existing, const QString &addition);
+/// The messages among `query` that `+add -remove` would actually change:
+/// `(query) AND (not tag:a1 or ... or tag:r1 ...)`.
+///
+/// Used to count and to write a rule applied to existing mail (item 212), so
+/// the number in the confirmation is the number of messages touched, not the
+/// number matched. Built on extend(), so a disjunction in the rule cannot
+/// escape the scope.
+///
+/// **Never empty.** An empty query or an empty change yields
+/// Config::matchNothingQuery(), because notmuch reads an empty query as
+/// "match everything" and this string is written to.
+QString changes(const QString &query, const QStringList &add,
+ const QStringList &remove);
+
} // namespace SearchTerm
/// One entry a context menu can offer: a finished query and the text naming it.
diff --git a/tests/test_searchterm.cpp b/tests/test_searchterm.cpp
index 7a0dfd7..b704529 100644
--- a/tests/test_searchterm.cpp
+++ b/tests/test_searchterm.cpp
@@ -18,6 +18,7 @@
#include <QtTest>
+#include "config.h"
#include "searchterm.h"
/// The query grammar of the right-click search actions.
@@ -30,6 +31,9 @@ class TestSearchTerm : public QObject
{
Q_OBJECT
private slots:
+ void changesNarrowsToMessagesTheTagsWouldMove();
+ void changesKeepsADisjunctionInsideItsScope();
+ void changesWithNothingToDoMatchesNothing();
void quotesAPlainValue();
void escapesEmbeddedQuotes();
void collapsesWhitespaceAndNewlines();
@@ -185,5 +189,45 @@ void TestSearchTerm::excludeWithNothingToExcludeLeavesTheQuery()
QVERIFY(SearchTerm::exclude(QString(), QString()).isEmpty());
}
+void TestSearchTerm::changesNarrowsToMessagesTheTagsWouldMove()
+{
+ // Item 212. A message already carrying every added tag and none of the
+ // removed ones would not change, so it must not be counted or written.
+ QCOMPARE(SearchTerm::changes(QStringLiteral("from:a@example.org"),
+ { QStringLiteral("promo") },
+ { QStringLiteral("inbox") }),
+ QStringLiteral("(from:a@example.org) AND "
+ "(not tag:promo or tag:inbox)"));
+ QCOMPARE(SearchTerm::changes(QStringLiteral("from:a@example.org"),
+ { QStringLiteral("x"), QStringLiteral("y") },
+ {}),
+ QStringLiteral("(from:a@example.org) AND "
+ "(not tag:x or not tag:y)"));
+ QCOMPARE(SearchTerm::changes(QStringLiteral("from:a@example.org"), {},
+ { QStringLiteral("inbox") }),
+ QStringLiteral("(from:a@example.org) AND (tag:inbox)"));
+}
+
+void TestSearchTerm::changesKeepsADisjunctionInsideItsScope()
+{
+ // `a or b AND c` binds as `a or (b AND c)`, which would write to every
+ // message matching `a` whatever its tags. extend() parenthesises both.
+ QCOMPARE(SearchTerm::changes(QStringLiteral("from:a@example.org or "
+ "from:b@example.org"),
+ { QStringLiteral("promo") }, {}),
+ QStringLiteral("(from:a@example.org or from:b@example.org) AND "
+ "(not tag:promo)"));
+}
+
+void TestSearchTerm::changesWithNothingToDoMatchesNothing()
+{
+ // notmuch reads an EMPTY query as "match everything". A caller writing
+ // through this must never be handed one.
+ QCOMPARE(SearchTerm::changes(QString(), { QStringLiteral("x") }, {}),
+ Config::matchNothingQuery());
+ QCOMPARE(SearchTerm::changes(QStringLiteral("from:a@example.org"), {}, {}),
+ Config::matchNothingQuery());
+}
+
QTEST_MAIN(TestSearchTerm)
#include "test_searchterm.moc"