From 09bd52f18dd222e401729187c48608af9f84e39f Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Sun, 12 May 2024 13:36:13 +0200 Subject: [PATCH] Fix passing captured texts to automated commands Fixes #2707 --- src/scriptable/scriptable.cpp | 31 ++++++++++++++++--------------- src/scriptable/scriptable.h | 2 +- src/tests/tests.cpp | 2 +- src/ui/commandwidget.ui | 2 +- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/scriptable/scriptable.cpp b/src/scriptable/scriptable.cpp index 5ce3f797e5..dc56f232c1 100644 --- a/src/scriptable/scriptable.cpp +++ b/src/scriptable/scriptable.cpp @@ -212,15 +212,6 @@ QString parseCommandLineArgument(const QString &arg) return result; } -bool matchData(const QRegularExpression &re, const QVariantMap &data, const QString &format) -{ - if ( re.pattern().isEmpty() ) - return true; - - const QString text = getTextData(data, format); - return text.contains(re); -} - bool isInternalDataFormat(const QString &format) { return format == mimeWindowTitle @@ -3217,12 +3208,13 @@ bool Scriptable::runCommands(CommandType::CommandType type) if ( command.outputTab.isEmpty() ) command.outputTab = tabName; - if ( !canExecuteCommand(command) ) + QStringList arguments; + if ( !canExecuteCommand(command, &arguments) ) continue; if ( canContinue() && !command.cmd.isEmpty() ) { Action action; - action.setCommand( command.cmd, QStringList(getTextData(m_data)) ); + action.setCommand(command.cmd, arguments); action.setInputWithFormat(m_data, command.input); action.setName(command.name); action.setData(m_data); @@ -3261,7 +3253,7 @@ bool Scriptable::runCommands(CommandType::CommandType type) return true; } -bool Scriptable::canExecuteCommand(const Command &command) +bool Scriptable::canExecuteCommand(const Command &command, QStringList *arguments) { // Verify that data for given MIME is available. if ( !command.input.isEmpty() ) { @@ -3274,12 +3266,21 @@ bool Scriptable::canExecuteCommand(const Command &command) } } + const QString text = getTextData(m_data); + arguments->append(text); + // Verify that and text matches given regexp. - if ( !matchData(command.re, m_data, mimeText) ) - return false; + if ( !command.re.pattern().isEmpty() ) { + QRegularExpressionMatchIterator it = command.re.globalMatch(text); + if ( !it.isValid() || !it.hasNext() ) + return false; + + while (it.hasNext()) + arguments->append( it.next().capturedTexts().mid(1) ); + } // Verify that window title matches given regexp. - if ( !matchData(command.wndre, m_data, mimeWindowTitle) ) + if ( !command.wndre.pattern().isEmpty() && !text.contains(command.wndre) ) return false; return canExecuteCommandFilter(command.matchCmd); diff --git a/src/scriptable/scriptable.h b/src/scriptable/scriptable.h index 0ea6ed3bb3..d1d24d7707 100644 --- a/src/scriptable/scriptable.h +++ b/src/scriptable/scriptable.h @@ -434,7 +434,7 @@ public slots: QJSValue eval(const QString &script); bool runAction(Action *action); bool runCommands(CommandType::CommandType type); - bool canExecuteCommand(const Command &command); + bool canExecuteCommand(const Command &command, QStringList *arguments); bool canExecuteCommandFilter(const QString &matchCommand); bool verifyClipboardAccess(); void provideClipboard(ClipboardMode mode); diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index 4e416315bf..07c8b03494 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -3845,7 +3845,7 @@ void Tests::automaticCommandRegExp() { const auto script = R"( setCommands([ - { automatic: true, re: 'SHOULD BE CHANGED$', cmd: 'copyq: setData("text/plain", "CHANGED")' }, + { automatic: true, re: 'SHOULD BE (CHANGED)$', cmd: 'copyq: setData(mimeText, arguments[1])' }, { automatic: true, cmd: 'copyq: setData("DATA", "DONE")' }, ]) )"; diff --git a/src/ui/commandwidget.ui b/src/ui/commandwidget.ui index 1ea5f620bd..f5af741b09 100644 --- a/src/ui/commandwidget.ui +++ b/src/ui/commandwidget.ui @@ -355,7 +355,7 @@ Skips the command if the input text does not match this regular expression (leave empty to match everything). -%2 through %9 in Command and Filter will be replaced with the captured texts. +%2 through %9 (or argument[1] and up in script) in Command and Filter will be replaced with the captured texts. Examples: