refactor: move find mini mode keybindings to keybinds module

This commit is contained in:
CJ van den Berg 2024-10-27 17:21:25 +01:00
parent 3340ff5ef9
commit 46e33d9d1e
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 167 additions and 94 deletions

View file

@ -0,0 +1,90 @@
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 egc: u32 = undefined;
var modifiers: u32 = undefined;
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) })) {
mapEvent(evtype, keypress, egc, modifiers) catch |e| return tp.exit_error(e, @errorReturnTrace());
}
return false;
}
fn mapEvent(evtype: u32, keypress: u32, egc: u32, modifiers: u32) !void {
switch (evtype) {
event_type.PRESS => try mapPress(keypress, egc, modifiers),
event_type.REPEAT => try mapPress(keypress, egc, modifiers),
else => {},
}
}
fn mapPress(keypress: u32, egc: u32, modifiers: u32) !void {
const keynormal = if ('a' <= keypress and keypress <= 'z') keypress - ('a' - 'A') else keypress;
return switch (modifiers) {
mod.CTRL => switch (keynormal) {
'Q' => command.executeName("quit", .{}),
'V' => command.executeName("system_paste", .{}),
'U' => command.executeName("mini_mode_reset", .{}),
'G' => command.executeName("mini_mode_cancel", .{}),
'C' => command.executeName("mini_mode_cancel", .{}),
'L' => command.executeName("scroll_view_center", .{}),
'F' => command.executeName("goto_next_match", .{}),
'N' => command.executeName("goto_next_match", .{}),
'P' => command.executeName("goto_prev_match", .{}),
'I' => command.executeName("mini_mode_insert_bytes", command.fmt(.{"\t"})),
key.SPACE => command.executeName("mini_mode_cancel", .{}),
key.ENTER => command.executeName("mini_mode_insert_bytes", command.fmt(.{"\n"})),
key.BACKSPACE => command.executeName("mini_mode_reset", .{}),
else => {},
},
mod.ALT => switch (keynormal) {
'V' => command.executeName("system_paste", .{}),
'N' => command.executeName("goto_next_match", .{}),
'P' => command.executeName("goto_prev_match", .{}),
else => {},
},
mod.ALT | mod.SHIFT => switch (keynormal) {
'V' => command.executeName("system_paste", .{}),
else => {},
},
mod.SHIFT => switch (keypress) {
key.ENTER => command.executeName("goto_prev_match", .{}),
key.F03 => command.executeName("goto_prev_match", .{}),
else => if (!key.synthesized_p(keypress))
command.executeName("mini_mode_insert_code_point", command.fmt(.{egc}))
else {},
},
0 => switch (keypress) {
key.UP => command.executeName("mini_mode_history_prev", .{}),
key.DOWN => command.executeName("mini_mode_history_next", .{}),
key.F03 => command.executeName("goto_next_match", .{}),
key.F15 => command.executeName("goto_prev_match", .{}),
key.F09 => command.executeName("theme_prev", .{}),
key.F10 => command.executeName("theme_next", .{}),
key.ESC => command.executeName("mini_mode_cancel", .{}),
key.ENTER => command.executeName("mini_mode_select", .{}),
key.BACKSPACE => command.executeName("mini_mode_delete_backwards", .{}),
key.LCTRL, key.RCTRL => command.executeName("enable_fast_scroll", .{}),
key.LALT, key.RALT => command.executeName("enable_fast_scroll", .{}),
else => if (!key.synthesized_p(keypress))
command.executeName("mini_mode_insert_code_point", command.fmt(.{egc}))
else {},
},
else => {},
};
}

View file

@ -4,6 +4,7 @@ pub const mode = struct {
pub const move_to_char = @import("move_to_char.zig");
pub const file_browser = @import("file_browser.zig");
pub const find_in_files = @import("find_in_files.zig");
pub const find = @import("find.zig");
};
};

View file

@ -3,6 +3,7 @@ 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 keybind = @import("keybind");
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const command = @import("command");
const EventHandler = @import("EventHandler");
@ -18,6 +19,8 @@ const ArrayList = @import("std").ArrayList;
const Self = @This();
const name = "󱎸 find";
const Commands = command.Collection(cmds);
allocator: Allocator,
input: ArrayList(u8),
last_input: ArrayList(u8),
@ -25,6 +28,7 @@ start_view: ed.View,
start_cursor: ed.Cursor,
editor: *ed.Editor,
history_pos: ?usize = null,
commands: Commands = undefined,
pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } {
if (tui.current().mainview.dynamic_cast(mainview)) |mv_| if (mv_.get_editor()) |editor| {
@ -37,16 +41,16 @@ pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.
.start_cursor = editor.get_primary().cursor,
.editor = editor,
};
try self.commands.init(self);
if (editor.get_primary().selection) |sel| ret: {
const text = editor.get_selection(sel, self.allocator) catch break :ret;
defer self.allocator.free(text);
try self.input.appendSlice(text);
}
return .{
try keybind.mode.mini.find.create(allocator),
.{
.handler = EventHandler.to_owned(self),
},
.{
.event_handler = EventHandler.to_owned(self),
.name = name,
},
};
@ -55,28 +59,18 @@ pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.
}
pub fn deinit(self: *Self) void {
self.commands.deinit();
self.input.deinit();
self.last_input.deinit();
self.allocator.destroy(self);
}
pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
var evtype: u32 = undefined;
var keypress: u32 = undefined;
var egc: u32 = undefined;
var modifiers: u32 = undefined;
var text: []const u8 = undefined;
defer {
if (tui.current().mini_mode) |*mini_mode| {
mini_mode.text = self.input.items;
mini_mode.cursor = self.input.items.len;
}
}
defer self.update_mini_mode_text();
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) })) {
self.mapEvent(evtype, keypress, egc, modifiers) catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{"F"})) {
if (try m.match(.{"F"})) {
self.flush_input() catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{ "system_clipboard", tp.extract(&text) })) {
self.insert_bytes(text) catch |e| return tp.exit_error(e, @errorReturnTrace());
@ -84,79 +78,6 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
return false;
}
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) !void {
switch (evtype) {
event_type.PRESS => try self.mapPress(keypress, egc, modifiers),
event_type.REPEAT => try self.mapPress(keypress, egc, modifiers),
event_type.RELEASE => try self.mapRelease(keypress, egc, modifiers),
else => {},
}
}
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void {
const keynormal = if ('a' <= keypress and keypress <= 'z') keypress - ('a' - 'A') else keypress;
return switch (modifiers) {
mod.CTRL => switch (keynormal) {
'Q' => self.cmd("quit", .{}),
'V' => self.cmd("system_paste", .{}),
'U' => self.input.clearRetainingCapacity(),
'G' => self.cancel(),
'C' => self.cancel(),
'L' => self.cmd("scroll_view_center", .{}),
'F' => self.cmd("goto_next_match", .{}),
'N' => self.cmd("goto_next_match", .{}),
'P' => self.cmd("goto_prev_match", .{}),
'I' => self.insert_bytes("\t"),
key.SPACE => self.cancel(),
key.ENTER => self.insert_bytes("\n"),
key.BACKSPACE => self.input.clearRetainingCapacity(),
else => {},
},
mod.ALT => switch (keynormal) {
'V' => self.cmd("system_paste", .{}),
'N' => self.cmd("goto_next_match", .{}),
'P' => self.cmd("goto_prev_match", .{}),
else => {},
},
mod.ALT | mod.SHIFT => switch (keynormal) {
'V' => self.cmd("system_paste", .{}),
else => {},
},
mod.SHIFT => switch (keypress) {
key.ENTER => self.cmd("goto_prev_match", .{}),
key.F03 => self.cmd("goto_prev_match", .{}),
else => if (!key.synthesized_p(keypress))
self.insert_code_point(egc)
else {},
},
0 => switch (keypress) {
key.UP => self.find_history_prev(),
key.DOWN => self.find_history_next(),
key.F03 => self.cmd("goto_next_match", .{}),
key.F15 => self.cmd("goto_prev_match", .{}),
key.F09 => self.cmd("theme_prev", .{}),
key.F10 => self.cmd("theme_next", .{}),
key.ESC => self.cancel(),
key.ENTER => self.confirm(),
key.BACKSPACE => _ = self.input.popOrNull(),
key.LCTRL, key.RCTRL => self.cmd("enable_fast_scroll", .{}),
key.LALT, key.RALT => self.cmd("enable_fast_scroll", .{}),
else => if (!key.synthesized_p(keypress))
self.insert_code_point(egc)
else {},
},
else => {},
};
}
fn mapRelease(self: *Self, keypress: u32, _: u32, _: u32) !void {
return switch (keypress) {
key.LCTRL, key.RCTRL => self.cmd("disable_fast_scroll", .{}),
key.LALT, key.RALT => self.cmd("disable_fast_scroll", .{}),
else => {},
};
}
fn insert_code_point(self: *Self, c: u32) !void {
var buf: [16]u8 = undefined;
const bytes = ucs32_to_utf8(&[_]u32{c}, &buf) catch |e| return tp.exit_error(e, @errorReturnTrace());
@ -189,11 +110,6 @@ fn cmd(self: *Self, name_: []const u8, ctx: command.Context) tp.result {
return command.executeName(name_, ctx);
}
fn confirm(self: *Self) void {
self.editor.push_find_history(self.input.items);
self.cmd("exit_mini_mode", .{}) catch {};
}
fn cancel(self: *Self) void {
self.editor.get_primary().cursor = self.start_cursor;
self.editor.scroll_to(self.start_view.row);
@ -230,3 +146,69 @@ fn load_history(self: *Self, pos: usize) void {
self.input.appendSlice(history.items[pos]) catch {};
}
}
fn update_mini_mode_text(self: *Self) void {
if (tui.current().mini_mode) |*mini_mode| {
mini_mode.text = self.input.items;
mini_mode.cursor = self.input.items.len;
}
}
const cmds = struct {
pub const Target = Self;
const Ctx = command.Context;
const Result = command.Result;
pub fn mini_mode_reset(self: *Self, _: Ctx) Result {
self.input.clearRetainingCapacity();
self.update_mini_mode_text();
}
pub const mini_mode_reset_meta = .{ .description = "Clear input" };
pub fn mini_mode_cancel(self: *Self, _: Ctx) Result {
self.cancel();
}
pub const mini_mode_cancel_meta = .{ .description = "Cancel input" };
pub fn mini_mode_select(self: *Self, _: Ctx) Result {
self.editor.push_find_history(self.input.items);
self.cmd("exit_mini_mode", .{}) catch {};
}
pub const mini_mode_select_meta = .{ .description = "Select" };
pub fn mini_mode_insert_code_point(self: *Self, ctx: Ctx) Result {
var egc: u32 = 0;
if (!try ctx.args.match(.{tp.extract(&egc)}))
return error.InvalidArgument;
self.insert_code_point(egc) catch |e| return tp.exit_error(e, @errorReturnTrace());
self.update_mini_mode_text();
}
pub const mini_mode_insert_code_point_meta = .{ .interactive = false };
pub fn mini_mode_insert_bytes(self: *Self, ctx: Ctx) Result {
var bytes: []const u8 = undefined;
if (!try ctx.args.match(.{tp.extract(&bytes)}))
return error.InvalidArgument;
self.insert_bytes(bytes) catch |e| return tp.exit_error(e, @errorReturnTrace());
self.update_mini_mode_text();
}
pub const mini_mode_insert_bytes_meta = .{ .interactive = false };
pub fn mini_mode_delete_backwards(self: *Self, _: Ctx) Result {
_ = self.input.popOrNull();
self.update_mini_mode_text();
}
pub const mini_mode_delete_backwards_meta = .{ .description = "Delete backwards" };
pub fn mini_mode_history_prev(self: *Self, _: Ctx) Result {
self.find_history_prev();
self.update_mini_mode_text();
}
pub const mini_mode_history_prev_meta = .{ .description = "History previous" };
pub fn mini_mode_history_next(self: *Self, _: Ctx) Result {
self.find_history_next();
self.update_mini_mode_text();
}
pub const mini_mode_history_next_meta = .{ .description = "History next" };
};