refactor: move move_to_char mine mode keybinds

This commit is contained in:
CJ van den Berg 2024-10-26 21:11:55 +02:00
parent f67bfab5b7
commit 9724decc4a
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
4 changed files with 98 additions and 23 deletions

View file

@ -11,9 +11,7 @@ const fmt = @import("std").fmt;
const Mode = @import("root.zig").Mode;
pub fn create(_: Allocator) error{OutOfMemory}!Mode {
return .{
.handler = EventHandler.static(@This()),
};
return .{ .handler = EventHandler.static(@This()) };
}
pub fn receive(_: tp.pid_ref, m: tp.message) error{Exit}!bool {

View file

@ -0,0 +1,56 @@
const tp = @import("thespian");
const key = @import("renderer").input.key;
const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const command = @import("command");
const EventHandler = @import("EventHandler");
const Allocator = @import("std").mem.Allocator;
const Mode = @import("root.zig").Mode;
pub fn create(_: Allocator) !Mode {
return .{ .handler = EventHandler.static(@This()) };
}
pub fn receive(_: tp.pid_ref, m: tp.message) error{Exit}!bool {
var evtype: u32 = undefined;
var keypress: u32 = undefined;
var modifiers: u32 = undefined;
var egc: u32 = undefined;
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) }))
try mapEvent(evtype, keypress, egc, modifiers);
return false;
}
fn mapEvent(evtype: u32, keypress: u32, egc: u32, modifiers: u32) tp.result {
switch (evtype) {
event_type.PRESS => try mapPress(keypress, egc, modifiers),
else => {},
}
}
fn mapPress(keypress: u32, egc: u32, modifiers: u32) tp.result {
switch (keypress) {
key.LSUPER, key.RSUPER => return,
key.LSHIFT, key.RSHIFT => return,
key.LCTRL, key.RCTRL => return,
key.LALT, key.RALT => return,
else => {},
}
return switch (modifiers) {
mod.SHIFT => if (!key.synthesized_p(keypress))
command.executeName("mini_mode_insert_code_point", command.fmt(.{egc}))
else
command.executeName("mini_mode_cancel", .{}),
0 => switch (keypress) {
key.ESC => command.executeName("mini_mode_cancel", .{}),
key.ENTER => command.executeName("mini_mode_cancel", .{}),
else => if (!key.synthesized_p(keypress))
command.executeName("mini_mode_insert_code_point", command.fmt(.{egc}))
else
command.executeName("mini_mode_cancel", .{}),
},
else => command.executeName("mini_mode_cancel", .{}),
};
}

View file

@ -1,6 +1,7 @@
pub const mode = struct {
pub const mini = struct {
pub const goto = @import("goto.zig");
pub const move_to_char = @import("move_to_char.zig");
};
};