1+ /*
2+ Input Actions - Input handler that executes user-defined actions
3+ Copyright (C) 2024-2026 Marcin Woźniak
4+
5+ This program is free software: you can redistribute it and/or modify
6+ it under the terms of the GNU General Public License as published by
7+ the Free Software Foundation, either version 3 of the License, or
8+ (at your option) any later version.
9+
10+ This program is distributed in the hope that it will be useful,
11+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ GNU General Public License for more details.
14+
15+ You should have received a copy of the GNU General Public License
16+ along with this program. If not, see <https://www.gnu.org/licenses/>.
17+ */
18+
19+ #include " TextInput.h"
20+ #include < libinputactions/helpers/QThread.h>
21+ #include < libinputactions/variables/VariableManager.h>
22+
23+ namespace InputActions
24+ {
25+
26+ bool TextInput::canReplaceSurroundingText (const QRegularExpression ®ex)
27+ {
28+ const auto text = surroundingText ();
29+ if (!text) {
30+ return false ;
31+ }
32+
33+ const auto match = regex.match (text.value ());
34+ if (!match.hasMatch ()) {
35+ return false ;
36+ }
37+
38+ const auto cursorPos = surroundingTextCursorPosition ();
39+ return !cursorPos || cursorPos == match.capturedEnd ();
40+ }
41+
42+ void TextInput::replaceSurroundingText (const QRegularExpression ®ex, const Value<QString> &newText)
43+ {
44+ uint32_t capturedLength{};
45+ QThreadHelpers::runOnThread (
46+ QThreadHelpers::mainThread (),
47+ [this , ®ex, &capturedLength]() {
48+ const auto match = regex.match (surroundingText ().value ());
49+ capturedLength = match.capturedLength ();
50+
51+ for (auto i = 0 ; i < REGEX_MATCH_VARIABLE_COUNT; i++) {
52+ auto *variable = g_variableManager->getVariable (QString (" match_%1" ).arg (i));
53+ if (i > match.lastCapturedIndex ()) {
54+ variable->set ({});
55+ continue ;
56+ }
57+
58+ variable->set (match.capturedTexts ()[i]);
59+ }
60+ },
61+ true );
62+
63+ const auto text = newText.get ();
64+ if (!text) {
65+ return ;
66+ }
67+
68+ QThreadHelpers::runOnThread (
69+ QThreadHelpers::mainThread (),
70+ [this , capturedLength, &text = text.value ()]() {
71+ deleteSurroundingText (capturedLength, 0 );
72+ writeText (text);
73+ },
74+ true );
75+ }
76+
77+ }
0 commit comments