Commit 84b54791 authored by Alexander Lohnau's avatar Alexander Lohnau 💬

Allow syntax for DuckDuckGo bangs in Webshortcuts

FEATURE: 423003
FIXED-IN: 5.21
parent 68306ef1
......@@ -105,13 +105,23 @@ void WebshortcutRunner::configurePrivateBrowsingActions()
void WebshortcutRunner::match(Plasma::RunnerContext &context)
{
const QString term = context.query();
const int delimIndex = term.indexOf(m_delimiter);
if (delimIndex == -1 || delimIndex == term.length() - 1) {
return;
const static QRegularExpression bangRegex(QStringLiteral("!([^ ]+).*"));
const static QRegularExpression normalRegex(QStringLiteral("^([^ ]+)%1").arg(QRegularExpression::escape(m_delimiter)));
const auto bangMatch = bangRegex.match(term);
QString key;
QString rawQuery = term;
if (bangMatch.hasMatch()) {
key = bangMatch.captured(1);
rawQuery = rawQuery.remove(rawQuery.indexOf(key) - 1, key.size() + 1);
} else {
const auto normalMatch = normalRegex.match(term);
if (normalMatch.hasMatch()) {
key = normalMatch.captured(0);
rawQuery = rawQuery.mid(key.length());
}
}
const QString key = term.left(delimIndex);
if (key == m_lastFailedKey) {
if (key.isEmpty() || key == m_lastFailedKey) {
return; // we already know it's going to suck ;)
}
......@@ -120,7 +130,7 @@ void WebshortcutRunner::match(Plasma::RunnerContext &context)
// filtering
if (m_lastKey == key) {
m_filterBeforeRun = true;
m_match.setText(i18n("Search %1 for %2", m_lastProvider, term.mid(delimIndex + 1)));
m_match.setText(i18n("Search %1 for %2", m_lastProvider, rawQuery));
context.addMatch(m_match);
return;
}
......
......@@ -44,6 +44,7 @@ class WebshortcutRunner : public Plasma::AbstractRunner
QString m_lastFailedKey;
QString m_lastKey;
QString m_lastProvider;
QRegularExpression m_regex;
KServiceAction m_privateAction;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment