Skip to content

Commit

Permalink
Fix passing captured texts to automated commands
Browse files Browse the repository at this point in the history
Fixes #2707
  • Loading branch information
hluk committed May 12, 2024
1 parent 7a23987 commit 09bd52f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
31 changes: 16 additions & 15 deletions src/scriptable/scriptable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() ) {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/scriptable/scriptable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")' },
])
)";
Expand Down
2 changes: 1 addition & 1 deletion src/ui/commandwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@
<property name="toolTip">
<string>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:

Expand Down

0 comments on commit 09bd52f

Please sign in to comment.