Match sed behavior for sed editing
Before s/a/b/ turns "aaa" into "bbb", while sed would turn it into "baa". This updates the sed editing syntax to match sed behavior. The /g flag is supported for doing global replacements when needed.
This commit is contained in:
committed by
Carl Schwan
parent
db1a9a0c4c
commit
383d2a6e71
@@ -68,7 +68,7 @@ void ActionsHandler::postEdit(const QString &text)
|
|||||||
const auto &evt = **it;
|
const auto &evt = **it;
|
||||||
if (const auto event = eventCast<const RoomMessageEvent>(&evt)) {
|
if (const auto event = eventCast<const RoomMessageEvent>(&evt)) {
|
||||||
if (event->senderId() == localId && event->hasTextContent()) {
|
if (event->senderId() == localId && event->hasTextContent()) {
|
||||||
static QRegularExpression re("^s/([^/]*)/([^/]*)");
|
static QRegularExpression re("^s/([^/]*)/([^/]*)(/g)?");
|
||||||
auto match = re.match(text);
|
auto match = re.match(text);
|
||||||
if (!match.hasMatch()) {
|
if (!match.hasMatch()) {
|
||||||
// should not happen but still make sure to send the message normally
|
// should not happen but still make sure to send the message normally
|
||||||
@@ -77,13 +77,22 @@ void ActionsHandler::postEdit(const QString &text)
|
|||||||
}
|
}
|
||||||
const QString regex = match.captured(1);
|
const QString regex = match.captured(1);
|
||||||
const QString replacement = match.captured(2);
|
const QString replacement = match.captured(2);
|
||||||
|
const QString flags = match.captured(3);
|
||||||
QString originalString;
|
QString originalString;
|
||||||
if (event->content()) {
|
if (event->content()) {
|
||||||
originalString = static_cast<const Quotient::EventContent::TextContent *>(event->content())->body;
|
originalString = static_cast<const Quotient::EventContent::TextContent *>(event->content())->body;
|
||||||
} else {
|
} else {
|
||||||
originalString = event->plainBody();
|
originalString = event->plainBody();
|
||||||
}
|
}
|
||||||
m_room->postHtmlMessage(text, originalString.replace(regex, replacement), event->msgtype(), "", event->id());
|
if (flags == "/g") {
|
||||||
|
m_room->postHtmlMessage(text, originalString.replace(regex, replacement), event->msgtype(), "", event->id());
|
||||||
|
} else {
|
||||||
|
m_room->postHtmlMessage(text,
|
||||||
|
originalString.replace(originalString.indexOf(regex), regex.size(), replacement),
|
||||||
|
event->msgtype(),
|
||||||
|
"",
|
||||||
|
event->id());
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user