Release v0.6.15: reliable snippets and Enter actions

This commit is contained in:
2026-10-30 13:15:50 +02:00
parent 39d3876fd2
commit 07e31228d7
13 changed files with 176 additions and 37 deletions
+59 -18
View File
@@ -12,6 +12,11 @@ namespace ks
App *App::current = nullptr;
namespace
{
ULONG_PTR snippetReplayMarker()
{
static const char marker{};
return reinterpret_cast<ULONG_PTR>(&marker);
}
std::vector<std::pair<int, std::wstring>> shortcuts(const Settings &s)
{
std::vector<std::pair<int, std::wstring>> out{{Toggle, s.toggle},
@@ -566,7 +571,8 @@ void App::keyboard(const KBDLLHOOKSTRUCT &k, UINT mods)
return;
}
run.record(ch, fg, layout, GetTickCount64());
if (settings.autoCorrect && mods == 0 && ch == L' ' && run.keys.size() >= (size_t)settings.autoMinLength + 1 &&
if (settings.autoCorrect && !snippetTyping && k.dwExtraInfo != snippetReplayMarker() &&
mods == 0 && ch == L' ' && run.keys.size() >= (size_t)settings.autoMinLength + 1 &&
run.keys.size() <= 41 && !busy && !run.overflow)
{
for (int source = 0; source < (int)layouts.size(); ++source)
@@ -619,7 +625,18 @@ void App::keyboard(const KBDLLHOOKSTRUCT &k, UINT mods)
for (auto &trigger : variants)
if (keys.size() >= trigger.size() && keys.ends_with(trigger))
{
PostMessageW(window, SnippetMessage, (WPARAM)i, 0);
// Keep subsequent typing out of the selection/paste transaction.
typingTarget = fg;
snippetPending = (int)i;
snippetTyping = true;
busy = true;
if (!PostMessageW(window, SnippetMessage, (WPARAM)i, (LPARAM)inputVersion.load()))
{
typingTarget = nullptr;
snippetPending = -1;
snippetTyping = false;
busy = false;
}
return;
}
}
@@ -693,12 +710,13 @@ bool App::handleKeyboard(const KBDLLHOOKSTRUCT &key, bool down, UINT mods)
return true;
return false;
}
bool App::beginTypingDeferral(HWND target, uint64_t version)
bool App::beginTypingDeferral(HWND target, uint64_t version, bool snippet)
{
std::lock_guard lock(stateMutex);
if (inputVersion != version || GetForegroundWindow() != target || !modifiersReleased())
return false;
typingTarget = target;
snippetTyping = snippet;
return true;
}
void App::replayTyping()
@@ -710,7 +728,12 @@ void App::replayTyping()
target = typingTarget;
typingTarget = nullptr;
autoPending = false;
snippetPending = -1;
inputs.swap(deferredTyping);
if (snippetTyping)
for (auto &input : inputs)
input.ki.dwExtraInfo = snippetReplayMarker();
snippetTyping = false;
}
busy = false;
if (inputs.empty())
@@ -826,14 +849,19 @@ void App::transform(int command, int snippet)
L" version=" + std::to_wstring(currentVersion) +
L" repeat=" + std::to_wstring(repeatAttempts));
}
const bool preDeferred = command == AutoCorrect && autoPending;
const bool preDeferred = (command == AutoCorrect && autoPending) ||
(snippet >= 0 && snippetPending == snippet);
if (busy.exchange(true) && !preDeferred)
{
if (manual)
diagnosticEvent(L"Transform outcome id=" + std::to_wstring(attempt) + L" reason=busy", true);
return;
}
if (preDeferred) autoPending = false;
if (preDeferred)
{
autoPending = false;
snippetPending = -1;
}
Run recent;
std::optional<LastTransform> previous;
Settings config;
@@ -904,7 +932,7 @@ void App::transform(int command, int snippet)
reason != L"paste-sent-unverified" && reason != L"layout-only" &&
reason != L"unchanged-expected");
}
} audit{*this, target, version, attempt, requestedAt, manual, reason, text, result, expected, hint, to};
} audit{*this, target, version, attempt, requestedAt, manual || snippet >= 0, reason, text, result, expected, hint, to};
try
{
auto valid = [&]
@@ -946,9 +974,9 @@ void App::transform(int command, int snippet)
return;
}
reason = L"clipboard-snapshot";
if (command == AutoCorrect)
if (command == AutoCorrect || snippet >= 0)
{
if (!beginTypingDeferral(target, version))
if (!beginTypingDeferral(target, version, snippet >= 0))
return;
reset.deferred = true;
}
@@ -1043,13 +1071,11 @@ void App::transform(int command, int snippet)
tail.keys = tail.keys.substr(tail.keys.size() - sn.trigger.size());
if (!tx.select(sn.trigger.size()))
return;
autoSelected = true;
reason = L"snippet-verification-mismatch";
text = tx.copy();
if (tail.match(text, available, physicalLayout()) < 0)
{
if (tx.valid())
chord(VK_RIGHT);
return;
}
result = parseTemplate(sn.text).text;
automatic = true;
}
@@ -1162,12 +1188,22 @@ void App::transform(int command, int snippet)
if (snippet >= 0 && sent && tx.valid())
{
auto t = parseTemplate(config.snippets[snippet].text);
if (!t.suffix.empty() && tx.select(caretSteps(t.suffix)))
if (!t.suffix.empty())
{
if (!tx.select(caretSteps(t.suffix))) return;
auto suffix = tx.copy();
if (tx.valid())
chord(newlines(suffix) == newlines(t.suffix) ? VK_LEFT : VK_RIGHT);
if (!tx.valid()) return;
if (newlines(suffix) != newlines(t.suffix))
{
chord(VK_RIGHT);
return;
}
if (!chord(VK_LEFT)) return;
}
// Action markers run after insertion and cursor placement,
// before any physical typing buffered during expansion.
for (int i = 0; i < t.enters && tx.valid(); ++i)
if (!chord(VK_RETURN)) break;
}
if (to >= 0 && config.switchLayout && tx.valid() && (sent || result == text))
{
@@ -1517,15 +1553,20 @@ LRESULT CALLBACK App::windowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
case AutoCorrectMessage:
if (app->autoPending)
{
if (!app->installingUpdate && app->settings.autoCorrect && app->inputVersion.load() == (uint64_t)wp)
if (!app->installingUpdate && !app->snippetTyping && app->settings.autoCorrect && app->inputVersion.load() == (uint64_t)wp)
app->transform(AutoCorrect);
else
app->replayTyping();
}
return 0;
case SnippetMessage:
if (app->installingUpdate) return 0;
app->transform(0, (int)wp);
if (app->snippetPending == (int)wp)
{
if (!app->installingUpdate && app->inputVersion.load() == (uint64_t)lp)
app->transform(0, (int)wp);
else
app->replayTyping();
}
return 0;
case HistoryMessage:
if (app->historyWindow && IsWindowVisible(app->historyWindow))
+3 -1
View File
@@ -109,8 +109,10 @@ class App
HotkeyHook hotkeyHook;
HWND typingTarget{};
bool autoPending{};
int snippetPending = -1;
bool snippetTyping{};
std::vector<INPUT> deferredTyping;
bool beginTypingDeferral(HWND, uint64_t);
bool beginTypingDeferral(HWND, uint64_t, bool snippet = false);
void replayTyping();
static LRESULT CALLBACK windowProc(HWND, UINT, WPARAM, LPARAM);
static LRESULT CALLBACK keyProc(int, WPARAM, LPARAM);
+11 -1
View File
@@ -261,7 +261,17 @@ Template parseTemplate(const std::wstring &text)
size_t cursor = std::wstring::npos;
for (size_t i = 0; i < text.size();)
{
if (text.compare(i, 10, L"{{cursor}}\0", 10) == 0)
if (text.compare(i, 9, L"{{enter}}") == 0)
{
t.text += L"{enter}";
i += 9;
}
else if (text.compare(i, 7, L"{enter}") == 0)
{
++t.enters;
i += 7;
}
else if (text.compare(i, 10, L"{{cursor}}\0", 10) == 0)
{
t.text += L"{cursor}";
i += 10;
+1
View File
@@ -34,6 +34,7 @@ struct Template
{
std::wstring text, suffix;
int cursors = 0;
int enters = 0;
};
Template parseTemplate(const std::wstring &);
struct Hotkey
+4 -3
View File
@@ -1448,9 +1448,9 @@ LRESULT CALLBACK snippetProc(HWND w, UINT msg, WPARAM wp, LPARAM lp)
}
return 0;
}
if (id == 312)
if (id == 312 || id == 313)
{
SendDlgItemMessageW(w, 303, EM_REPLACESEL, TRUE, (LPARAM)L"{cursor}");
SendDlgItemMessageW(w, 303, EM_REPLACESEL, TRUE, (LPARAM)(id == 312 ? L"{cursor}" : L"{enter}"));
return 0;
}
if (id == IDOK)
@@ -1900,8 +1900,9 @@ void showSnippets(App &app)
edit(w, 302, 236, 44, 488);
label(w, L"Replacement text", 236, 82, 480);
edit(w, 303, 236, 110, 488, 270, ES_MULTILINE | ES_WANTRETURN | ES_AUTOVSCROLL | WS_VSCROLL);
label(w, L"Use {cursor} once; {{cursor}} inserts the literal marker.", 236, 392, 488);
control(w, L"STATIC", L"{cursor}: caret; {enter}: press Enter after insertion.\nUse {{cursor}} / {{enter}} for literal markers.", 0, 0, 236, 392, 488, 36);
button(w, L"Insert cursor marker", 312, 236, 430, 190);
button(w, L"Insert Enter marker", 313, 436, 430, 190);
button(w, L"Save", IDOK, 510, 478, 100);
button(w, L"Cancel", IDCANCEL, 624, 478, 100);
fillSnippets(w, (SnippetView *)GetWindowLongPtrW(w, GWLP_USERDATA));