From 1551c2757878872cea7eb04edc6eb28a3e1468a5 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 2 Jun 2024 14:36:08 -0700 Subject: [PATCH] input: Kitty alternate should be reported even if no shifted text (#1817) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reproduction: HU layout Ctrl+ő On release, we were previously not sending an alt text (we were sending it properly on press). Kitty sends it also on release and the spec makes it clear we should send it on release. This was just due to some faulty logic; added a test and fixed that. --- src/input/KeyEncoder.zig | 60 ++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/src/input/KeyEncoder.zig b/src/input/KeyEncoder.zig index d6d9a2b4c..b690b6902 100644 --- a/src/input/KeyEncoder.zig +++ b/src/input/KeyEncoder.zig @@ -178,23 +178,30 @@ fn kitty( const view = try std.unicode.Utf8View.init(self.event.utf8); var it = view.iterator(); - // We break early if there are codepoints...there are no alt keys. - const cp1 = it.nextCodepoint() orelse break :alternates; + // If we have a codepoint in our UTF-8 sequence, then we can + // report the shifted version. + if (it.nextCodepoint()) |cp1| { + // Set the first alternate (shifted version) + if (cp1 != seq.key and seq.mods.shift) seq.alternates[0] = cp1; - // We want to know if there are additional codepoints because - // our logic below depends on the utf8 being a single codepoint. - const has_cp2 = it.nextCodepoint() != null; + // We want to know if there are additional codepoints because + // our logic below depends on the utf8 being a single codepoint. + const has_cp2 = it.nextCodepoint() != null; - // Set the first alternate (shifted version) - if (cp1 != seq.key and seq.mods.shift) seq.alternates[0] = cp1; - - // Set the base layout key. We only report this if this codepoint - // differs from our pressed key. - if (self.event.key.codepoint()) |base| { - if (base != seq.key and - (cp1 != base and !has_cp2)) - { - seq.alternates[1] = base; + // Set the base layout key. We only report this if this codepoint + // differs from our pressed key. + if (self.event.key.codepoint()) |base| { + if (base != seq.key and + (cp1 != base and !has_cp2)) + { + seq.alternates[1] = base; + } + } + } else { + // No UTF-8 so we can't report a shifted key but we can still + // report a base layout key. + if (self.event.key.codepoint()) |base| { + if (base != seq.key) seq.alternates[1] = base; } } } @@ -1387,6 +1394,29 @@ test "kitty: report alternates with ru layout caps lock" { try testing.expectEqualStrings("\x1b[1095::59;65;1063u", actual); } +test "kitty: report alternates with hu layout release" { + var buf: [128]u8 = undefined; + var enc: KeyEncoder = .{ + .event = .{ + .action = .release, + .key = .left_bracket, + .mods = .{ .ctrl = true }, + .utf8 = "", + .unshifted_codepoint = 337, + }, + .kitty_flags = .{ + .disambiguate = true, + .report_all = true, + .report_alternates = true, + .report_associated = true, + .report_events = true, + }, + }; + + const actual = try enc.kitty(&buf); + try testing.expectEqualStrings("[337::91;5:3u", actual[1..]); +} + // macOS generates utf8 text for arrow keys. test "kitty: up arrow with utf8" { var buf: [128]u8 = undefined;