refactor: move goto mini mode keybindings to keybind module
This commit is contained in:
parent
16c5471126
commit
49319d6207
5 changed files with 176 additions and 90 deletions
72
src/keybind/static/goto.zig
Normal file
72
src/keybind/static/goto.zig
Normal file
|
@ -0,0 +1,72 @@
|
|||
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 fmt = @import("std").fmt;
|
||||
|
||||
const Mode = @import("root.zig").Mode;
|
||||
|
||||
const name = "#goto";
|
||||
|
||||
pub fn create(_: Allocator) error{OutOfMemory}!Mode {
|
||||
return .{
|
||||
.handler = EventHandler.static(@This()),
|
||||
.name = name,
|
||||
.description = name,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn receive(_: tp.pid_ref, m: tp.message) error{Exit}!bool {
|
||||
var evtype: u32 = undefined;
|
||||
var keypress: u32 = undefined;
|
||||
var modifiers: u32 = undefined;
|
||||
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.any, tp.string, tp.extract(&modifiers) }))
|
||||
try mapEvent(evtype, keypress, modifiers);
|
||||
return false;
|
||||
}
|
||||
|
||||
fn mapEvent(evtype: u32, keypress: u32, modifiers: u32) tp.result {
|
||||
switch (evtype) {
|
||||
event_type.PRESS => try mapPress(keypress, modifiers),
|
||||
event_type.REPEAT => try mapPress(keypress, modifiers),
|
||||
event_type.RELEASE => try mapRelease(keypress, modifiers),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
fn mapPress(keypress: u32, modifiers: u32) tp.result {
|
||||
const keynormal = if ('a' <= keypress and keypress <= 'z') keypress - ('a' - 'A') else keypress;
|
||||
return switch (modifiers) {
|
||||
mod.CTRL => switch (keynormal) {
|
||||
'Q' => command.executeName("quit", .{}),
|
||||
'U' => command.executeName("mini_mode_reset", .{}),
|
||||
'G' => command.executeName("mini_mode_cancel", .{}),
|
||||
'C' => command.executeName("mini_mode_cancel", .{}),
|
||||
'L' => command.executeName("scroll_view_center", .{}),
|
||||
key.SPACE => command.executeName("mini_mode_cancel", .{}),
|
||||
else => {},
|
||||
},
|
||||
0 => switch (keypress) {
|
||||
key.LCTRL, key.RCTRL => command.executeName("enable_fast_scroll", .{}),
|
||||
key.LALT, key.RALT => command.executeName("enable_fast_scroll", .{}),
|
||||
key.ESC => command.executeName("mini_mode_cancel", .{}),
|
||||
key.ENTER => command.executeName("exit_mini_mode", .{}),
|
||||
key.BACKSPACE => command.executeName("mini_mode_delete_backwards", .{}),
|
||||
'0'...'9' => command.executeName("mini_mode_insert_code_point", command.fmt(.{keypress})),
|
||||
else => {},
|
||||
},
|
||||
else => {},
|
||||
};
|
||||
}
|
||||
|
||||
fn mapRelease(keypress: u32, _: u32) tp.result {
|
||||
return switch (keypress) {
|
||||
key.LCTRL, key.RCTRL => command.executeName("disable_fast_scroll", .{}),
|
||||
key.LALT, key.RALT => command.executeName("disable_fast_scroll", .{}),
|
||||
else => {},
|
||||
};
|
||||
}
|
24
src/keybind/static/root.zig
Normal file
24
src/keybind/static/root.zig
Normal file
|
@ -0,0 +1,24 @@
|
|||
pub const mode = struct {
|
||||
pub const mini = struct {
|
||||
pub const goto = @import("goto.zig");
|
||||
};
|
||||
};
|
||||
|
||||
pub const Mode = struct {
|
||||
handler: EventHandler,
|
||||
name: []const u8,
|
||||
description: []const u8,
|
||||
line_numbers: enum { absolute, relative } = .absolute,
|
||||
keybind_hints: ?*const KeybindHints = null,
|
||||
cursor_shape: renderer.CursorShape = .block,
|
||||
|
||||
pub fn deinit(self: *Mode) void {
|
||||
self.handler.deinit();
|
||||
}
|
||||
};
|
||||
|
||||
pub const KeybindHints = std.static_string_map.StaticStringMap([]const u8);
|
||||
|
||||
const renderer = @import("renderer");
|
||||
const EventHandler = @import("EventHandler");
|
||||
const std = @import("std");
|
Loading…
Add table
Add a link
Reference in a new issue