refactor: rework error handling to simplifiy command implementation and provide better back traces

This commit is contained in:
CJ van den Berg 2024-06-26 23:29:39 +02:00
parent 21b604f4d6
commit 032982c1e8
26 changed files with 821 additions and 785 deletions

View file

@ -50,18 +50,18 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
var text: []const u8 = undefined;
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) })) {
try self.mapEvent(evtype, keypress, egc, modifiers);
self.mapEvent(evtype, keypress, egc, modifiers) catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{"F"})) {
try self.flush_input();
self.flush_input() catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{ "system_clipboard", tp.extract(&text) })) {
try self.paste_bytes(text);
self.paste_bytes(text) catch |e| return tp.exit_error(e, @errorReturnTrace());
}
return false;
}
pub fn add_keybind() void {}
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) tp.result {
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) !void {
return switch (evtype) {
event_type.PRESS => self.mapPress(keypress, egc, modifiers),
event_type.REPEAT => self.mapPress(keypress, egc, modifiers),
@ -70,7 +70,7 @@ fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) t
};
}
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void {
const keynormal = if ('a' <= keypress and keypress <= 'z') keypress - ('a' - 'A') else keypress;
if (self.leader) |_| return self.mapFollower(keynormal, egc, modifiers);
switch (keypress) {
@ -225,7 +225,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
};
}
fn mapFollower(self: *Self, keypress: u32, _: u32, modifiers: u32) tp.result {
fn mapFollower(self: *Self, keypress: u32, _: u32, modifiers: u32) !void {
defer self.leader = null;
const ldr = if (self.leader) |leader| leader else return;
return switch (ldr.modifiers) {
@ -245,7 +245,7 @@ fn mapFollower(self: *Self, keypress: u32, _: u32, modifiers: u32) tp.result {
};
}
fn mapRelease(self: *Self, keypress: u32, _: u32, _: u32) tp.result {
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_jump_mode", .{}),
@ -253,32 +253,32 @@ fn mapRelease(self: *Self, keypress: u32, _: u32, _: u32) tp.result {
};
}
fn insert_code_point(self: *Self, c: u32) tp.result {
fn insert_code_point(self: *Self, c: u32) !void {
if (self.input.items.len + 4 > input_buffer_size)
try self.flush_input();
var buf: [6]u8 = undefined;
const bytes = ucs32_to_utf8(&[_]u32{c}, &buf) catch |e| return tp.exit_error(e);
self.input.appendSlice(buf[0..bytes]) catch |e| return tp.exit_error(e);
const bytes = try ucs32_to_utf8(&[_]u32{c}, &buf);
try self.input.appendSlice(buf[0..bytes]);
}
fn insert_bytes(self: *Self, bytes: []const u8) tp.result {
fn insert_bytes(self: *Self, bytes: []const u8) !void {
if (self.input.items.len + 4 > input_buffer_size)
try self.flush_input();
self.input.appendSlice(bytes) catch |e| return tp.exit_error(e);
try self.input.appendSlice(bytes);
}
fn paste_bytes(self: *Self, bytes: []const u8) tp.result {
fn paste_bytes(self: *Self, bytes: []const u8) !void {
try self.flush_input();
try command.executeName("paste", command.fmt(.{bytes}));
}
var insert_chars_id: ?command.ID = null;
fn flush_input(self: *Self) tp.result {
fn flush_input(self: *Self) !void {
if (self.input.items.len > 0) {
defer self.input.clearRetainingCapacity();
const id = insert_chars_id orelse command.get_id_cache("insert_chars", &insert_chars_id) orelse {
return tp.exit_error(error.InputTargetNotFound);
return tp.exit_error(error.InputTargetNotFound, null);
};
try command.execute(id, command.fmt(.{self.input.items}));
self.last_cmd = "insert_chars";

View file

@ -53,20 +53,20 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
var text: []const u8 = undefined;
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) })) {
try self.mapEvent(evtype, keypress, egc, modifiers);
self.mapEvent(evtype, keypress, egc, modifiers) catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{"F"})) {
try self.flush_input();
self.flush_input() catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{ "system_clipboard", tp.extract(&text) })) {
try self.flush_input();
try self.insert_bytes(text);
try self.flush_input();
self.flush_input() catch |e| return tp.exit_error(e, @errorReturnTrace());
self.insert_bytes(text) catch |e| return tp.exit_error(e, @errorReturnTrace());
self.flush_input() catch |e| return tp.exit_error(e, @errorReturnTrace());
}
return false;
}
pub fn add_keybind() void {}
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) tp.result {
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) !void {
return switch (evtype) {
event_type.PRESS => self.mapPress(keypress, egc, modifiers),
event_type.REPEAT => self.mapPress(keypress, egc, modifiers),
@ -75,7 +75,7 @@ fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) t
};
}
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void {
const keynormal = if ('a' <= keypress and keypress <= 'z') keypress - ('a' - 'A') else keypress;
if (self.leader) |_| return self.mapFollower(keynormal, egc, modifiers);
switch (keypress) {
@ -227,7 +227,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
};
}
fn mapFollower(self: *Self, keypress: u32, _: u32, modifiers: u32) tp.result {
fn mapFollower(self: *Self, keypress: u32, _: u32, modifiers: u32) !void {
defer self.leader = null;
const ldr = if (self.leader) |leader| leader else return;
return switch (ldr.modifiers) {
@ -247,7 +247,7 @@ fn mapFollower(self: *Self, keypress: u32, _: u32, modifiers: u32) tp.result {
};
}
fn mapRelease(self: *Self, keypress: u32, _: u32, _: u32) tp.result {
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_jump_mode", .{}),
@ -255,27 +255,27 @@ fn mapRelease(self: *Self, keypress: u32, _: u32, _: u32) tp.result {
};
}
fn insert_code_point(self: *Self, c: u32) tp.result {
fn insert_code_point(self: *Self, c: u32) !void {
if (self.input.items.len + 4 > input_buffer_size)
try self.flush_input();
var buf: [6]u8 = undefined;
const bytes = ucs32_to_utf8(&[_]u32{c}, &buf) catch |e| return tp.exit_error(e);
self.input.appendSlice(buf[0..bytes]) catch |e| return tp.exit_error(e);
const bytes = try ucs32_to_utf8(&[_]u32{c}, &buf);
try self.input.appendSlice(buf[0..bytes]);
}
fn insert_bytes(self: *Self, bytes: []const u8) tp.result {
fn insert_bytes(self: *Self, bytes: []const u8) !void {
if (self.input.items.len + 4 > input_buffer_size)
try self.flush_input();
self.input.appendSlice(bytes) catch |e| return tp.exit_error(e);
try self.input.appendSlice(bytes);
}
var insert_chars_id: ?command.ID = null;
fn flush_input(self: *Self) tp.result {
fn flush_input(self: *Self) !void {
if (self.input.items.len > 0) {
defer self.input.clearRetainingCapacity();
const id = insert_chars_id orelse command.get_id_cache("insert_chars", &insert_chars_id) orelse {
return tp.exit_error(error.InputTargetNotFound);
return tp.exit_error(error.InputTargetNotFound, null);
};
try command.execute(id, command.fmt(.{self.input.items}));
self.last_cmd = "insert_chars";
@ -306,25 +306,26 @@ const Commands = command.Collection(cmds_);
const cmds_ = struct {
pub const Target = Self;
const Ctx = command.Context;
const Result = command.Result;
pub fn @"w"(self: *Self, _: Ctx) tp.result {
pub fn w(self: *Self, _: Ctx) Result {
try self.cmd("save_file", .{});
}
pub fn @"q"(self: *Self, _: Ctx) tp.result {
pub fn q(self: *Self, _: Ctx) Result {
try self.cmd("quit", .{});
}
pub fn @"q!"(self: *Self, _: Ctx) tp.result {
pub fn @"q!"(self: *Self, _: Ctx) Result {
try self.cmd("quit_without_saving", .{});
}
pub fn @"wq"(self: *Self, _: Ctx) tp.result {
pub fn wq(self: *Self, _: Ctx) Result {
try self.cmd("save_file", .{});
try self.cmd("quit", .{});
}
pub fn @"wq!"(self: *Self, _: Ctx) tp.result {
pub fn @"wq!"(self: *Self, _: Ctx) Result {
self.cmd("save_file", .{}) catch {};
try self.cmd("quit_without_saving", .{});
}

View file

@ -55,20 +55,20 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
var text: []const u8 = undefined;
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) })) {
try self.mapEvent(evtype, keypress, egc, modifiers);
self.mapEvent(evtype, keypress, egc, modifiers) catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{"F"})) {
try self.flush_input();
self.flush_input() catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{ "system_clipboard", tp.extract(&text) })) {
try self.flush_input();
try self.insert_bytes(text);
try self.flush_input();
self.flush_input() catch |e| return tp.exit_error(e, @errorReturnTrace());
self.insert_bytes(text) catch |e| return tp.exit_error(e, @errorReturnTrace());
self.flush_input() catch |e| return tp.exit_error(e, @errorReturnTrace());
}
return false;
}
pub fn add_keybind() void {}
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) tp.result {
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) !void {
return switch (evtype) {
event_type.PRESS => self.mapPress(keypress, egc, modifiers),
event_type.REPEAT => self.mapPress(keypress, egc, modifiers),
@ -77,7 +77,7 @@ fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) t
};
}
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void {
if (self.count > 0 and modifiers == 0 and '0' <= keypress and keypress <= '9') return self.add_count(keypress - '0');
const keynormal = if ('a' <= keypress and keypress <= 'z') keypress - ('a' - 'A') else keypress;
if (self.leader) |_| return self.mapFollower(keynormal, egc, modifiers);
@ -290,7 +290,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
};
}
fn mapFollower(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
fn mapFollower(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void {
if (keypress == key.LCTRL or
keypress == key.RCTRL or
keypress == key.LALT or
@ -403,7 +403,7 @@ fn mapFollower(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
};
}
fn mapRelease(self: *Self, keypress: u32, _: u32, _: u32) tp.result {
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_jump_mode", .{}),
@ -416,27 +416,27 @@ fn add_count(self: *Self, value: usize) void {
self.count += value;
}
fn insert_code_point(self: *Self, c: u32) tp.result {
fn insert_code_point(self: *Self, c: u32) !void {
if (self.input.items.len + 4 > input_buffer_size)
try self.flush_input();
var buf: [6]u8 = undefined;
const bytes = ucs32_to_utf8(&[_]u32{c}, &buf) catch |e| return tp.exit_error(e);
self.input.appendSlice(buf[0..bytes]) catch |e| return tp.exit_error(e);
const bytes = try ucs32_to_utf8(&[_]u32{c}, &buf);
try self.input.appendSlice(buf[0..bytes]);
}
fn insert_bytes(self: *Self, bytes: []const u8) tp.result {
fn insert_bytes(self: *Self, bytes: []const u8) !void {
if (self.input.items.len + 4 > input_buffer_size)
try self.flush_input();
self.input.appendSlice(bytes) catch |e| return tp.exit_error(e);
try self.input.appendSlice(bytes);
}
var insert_chars_id: ?command.ID = null;
fn flush_input(self: *Self) tp.result {
fn flush_input(self: *Self) !void {
if (self.input.items.len > 0) {
defer self.input.clearRetainingCapacity();
const id = insert_chars_id orelse command.get_id_cache("insert_chars", &insert_chars_id) orelse {
return tp.exit_error(error.InputTargetNotFound);
return tp.exit_error(error.InputTargetNotFound, null);
};
try command.execute(id, command.fmt(.{self.input.items}));
self.last_cmd = "insert_chars";
@ -610,25 +610,26 @@ const Commands = command.Collection(cmds_);
const cmds_ = struct {
pub const Target = Self;
const Ctx = command.Context;
const Result = command.Result;
pub fn @"w"(self: *Self, _: Ctx) tp.result {
pub fn w(self: *Self, _: Ctx) Result {
try self.cmd("save_file", .{});
}
pub fn @"q"(self: *Self, _: Ctx) tp.result {
pub fn q(self: *Self, _: Ctx) Result {
try self.cmd("quit", .{});
}
pub fn @"q!"(self: *Self, _: Ctx) tp.result {
pub fn @"q!"(self: *Self, _: Ctx) Result {
try self.cmd("quit_without_saving", .{});
}
pub fn @"wq"(self: *Self, _: Ctx) tp.result {
pub fn wq(self: *Self, _: Ctx) Result {
try self.cmd("save_file", .{});
try self.cmd("quit", .{});
}
pub fn @"wq!"(self: *Self, _: Ctx) tp.result {
pub fn @"wq!"(self: *Self, _: Ctx) Result {
self.cmd("save_file", .{}) catch {};
try self.cmd("quit_without_saving", .{});
}

View file

@ -55,20 +55,20 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
var text: []const u8 = undefined;
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) })) {
try self.mapEvent(evtype, keypress, egc, modifiers);
self.mapEvent(evtype, keypress, egc, modifiers) catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{"F"})) {
try self.flush_input();
self.flush_input() catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{ "system_clipboard", tp.extract(&text) })) {
try self.flush_input();
try self.insert_bytes(text);
try self.flush_input();
self.flush_input() catch |e| return tp.exit_error(e, @errorReturnTrace());
self.insert_bytes(text) catch |e| return tp.exit_error(e, @errorReturnTrace());
self.flush_input() catch |e| return tp.exit_error(e, @errorReturnTrace());
}
return false;
}
pub fn add_keybind() void {}
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) tp.result {
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) !void {
return switch (evtype) {
event_type.PRESS => self.mapPress(keypress, egc, modifiers),
event_type.REPEAT => self.mapPress(keypress, egc, modifiers),
@ -77,7 +77,7 @@ fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) t
};
}
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void {
if (self.count > 0 and modifiers == 0 and '0' <= keypress and keypress <= '9') return self.add_count(keypress - '0');
const keynormal = if ('a' <= keypress and keypress <= 'z') keypress - ('a' - 'A') else keypress;
if (self.leader) |_| return self.mapFollower(keynormal, egc, modifiers);
@ -288,7 +288,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
};
}
fn mapFollower(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
fn mapFollower(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void {
if (keypress == key.LCTRL or
keypress == key.RCTRL or
keypress == key.LALT or
@ -363,7 +363,7 @@ fn mapFollower(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
};
}
fn mapRelease(self: *Self, keypress: u32, _: u32, _: u32) tp.result {
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_jump_mode", .{}),
@ -376,27 +376,27 @@ fn add_count(self: *Self, value: usize) void {
self.count += value;
}
fn insert_code_point(self: *Self, c: u32) tp.result {
fn insert_code_point(self: *Self, c: u32) !void {
if (self.input.items.len + 4 > input_buffer_size)
try self.flush_input();
var buf: [6]u8 = undefined;
const bytes = ucs32_to_utf8(&[_]u32{c}, &buf) catch |e| return tp.exit_error(e);
self.input.appendSlice(buf[0..bytes]) catch |e| return tp.exit_error(e);
const bytes = try ucs32_to_utf8(&[_]u32{c}, &buf);
try self.input.appendSlice(buf[0..bytes]);
}
fn insert_bytes(self: *Self, bytes: []const u8) tp.result {
fn insert_bytes(self: *Self, bytes: []const u8) !void {
if (self.input.items.len + 4 > input_buffer_size)
try self.flush_input();
self.input.appendSlice(bytes) catch |e| return tp.exit_error(e);
try self.input.appendSlice(bytes);
}
var insert_chars_id: ?command.ID = null;
fn flush_input(self: *Self) tp.result {
fn flush_input(self: *Self) !void {
if (self.input.items.len > 0) {
defer self.input.clearRetainingCapacity();
const id = insert_chars_id orelse command.get_id_cache("insert_chars", &insert_chars_id) orelse {
return tp.exit_error(error.InputTargetNotFound);
return tp.exit_error(error.InputTargetNotFound, null);
};
try command.execute(id, command.fmt(.{self.input.items}));
self.last_cmd = "insert_chars";
@ -570,25 +570,26 @@ const Commands = command.Collection(cmds_);
const cmds_ = struct {
pub const Target = Self;
const Ctx = command.Context;
const Result = command.Result;
pub fn @"w"(self: *Self, _: Ctx) tp.result {
pub fn @"w"(self: *Self, _: Ctx) Result {
try self.cmd("save_file", .{});
}
pub fn @"q"(self: *Self, _: Ctx) tp.result {
pub fn @"q"(self: *Self, _: Ctx) Result {
try self.cmd("quit", .{});
}
pub fn @"q!"(self: *Self, _: Ctx) tp.result {
pub fn @"q!"(self: *Self, _: Ctx) Result {
try self.cmd("quit_without_saving", .{});
}
pub fn @"wq"(self: *Self, _: Ctx) tp.result {
pub fn @"wq"(self: *Self, _: Ctx) Result {
try self.cmd("save_file", .{});
try self.cmd("quit", .{});
}
pub fn @"wq!"(self: *Self, _: Ctx) tp.result {
pub fn @"wq!"(self: *Self, _: Ctx) Result {
self.cmd("save_file", .{}) catch {};
try self.cmd("quit_without_saving", .{});
}

View file

@ -74,16 +74,16 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
}
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) })) {
try self.mapEvent(evtype, keypress, egc, modifiers);
self.mapEvent(evtype, keypress, egc, modifiers) catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{"F"})) {
self.flush_input() catch |e| return e;
self.flush_input() catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{ "system_clipboard", tp.extract(&text) })) {
try self.insert_bytes(text);
self.insert_bytes(text) catch |e| return tp.exit_error(e, @errorReturnTrace());
}
return false;
}
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) tp.result {
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),
@ -92,7 +92,7 @@ fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) t
}
}
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
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) {
@ -150,7 +150,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
};
}
fn mapRelease(self: *Self, keypress: u32, _: u32, _: u32) tp.result {
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", .{}),
@ -158,14 +158,14 @@ fn mapRelease(self: *Self, keypress: u32, _: u32, _: u32) tp.result {
};
}
fn insert_code_point(self: *Self, c: u32) tp.result {
fn insert_code_point(self: *Self, c: u32) !void {
if (self.input.len + 16 > self.buf.len)
try self.flush_input();
const bytes = ucs32_to_utf8(&[_]u32{c}, self.buf[self.input.len..]) catch |e| return tp.exit_error(e);
const bytes = ucs32_to_utf8(&[_]u32{c}, self.buf[self.input.len..]) catch |e| return tp.exit_error(e, @errorReturnTrace());
self.input = self.buf[0 .. self.input.len + bytes];
}
fn insert_bytes(self: *Self, bytes: []const u8) tp.result {
fn insert_bytes(self: *Self, bytes: []const u8) !void {
if (self.input.len + 16 > self.buf.len)
try self.flush_input();
const newlen = self.input.len + bytes.len;
@ -175,7 +175,7 @@ fn insert_bytes(self: *Self, bytes: []const u8) tp.result {
var find_cmd_id: ?command.ID = null;
fn flush_input(self: *Self) tp.result {
fn flush_input(self: *Self) !void {
if (self.input.len > 0) {
if (eql(u8, self.input, self.last_input))
return;

View file

@ -73,16 +73,16 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
}
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) })) {
try self.mapEvent(evtype, keypress, egc, modifiers);
self.mapEvent(evtype, keypress, egc, modifiers) catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{"F"})) {
self.flush_input() catch |e| return e;
self.flush_input() catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{ "system_clipboard", tp.extract(&text) })) {
try self.insert_bytes(text);
self.insert_bytes(text) catch |e| return tp.exit_error(e, @errorReturnTrace());
}
return false;
}
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) tp.result {
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),
@ -91,7 +91,7 @@ fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) t
}
}
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
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) {
@ -147,7 +147,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
};
}
fn mapRelease(self: *Self, keypress: u32, _: u32, _: u32) tp.result {
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", .{}),
@ -155,14 +155,14 @@ fn mapRelease(self: *Self, keypress: u32, _: u32, _: u32) tp.result {
};
}
fn insert_code_point(self: *Self, c: u32) tp.result {
fn insert_code_point(self: *Self, c: u32) !void {
if (self.input.len + 16 > self.buf.len)
try self.flush_input();
const bytes = ucs32_to_utf8(&[_]u32{c}, self.buf[self.input.len..]) catch |e| return tp.exit_error(e);
const bytes = try ucs32_to_utf8(&[_]u32{c}, self.buf[self.input.len..]);
self.input = self.buf[0 .. self.input.len + bytes];
}
fn insert_bytes(self: *Self, bytes: []const u8) tp.result {
fn insert_bytes(self: *Self, bytes: []const u8) !void {
if (self.input.len + 16 > self.buf.len)
try self.flush_input();
const newlen = self.input.len + bytes.len;
@ -172,7 +172,7 @@ fn insert_bytes(self: *Self, bytes: []const u8) tp.result {
var find_cmd_id: ?command.ID = null;
fn flush_input(self: *Self) tp.result {
fn flush_input(self: *Self) !void {
if (self.input.len > 0) {
if (eql(u8, self.input, self.last_input))
return;

View file

@ -65,12 +65,12 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
}
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) })) {
try self.mapEvent(evtype, keypress, egc, modifiers);
self.mapEvent(evtype, keypress, egc, modifiers) catch |e| return tp.exit_error(e, @errorReturnTrace());
}
return false;
}
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) tp.result {
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),
@ -79,7 +79,7 @@ fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) t
}
}
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
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) {
@ -121,16 +121,16 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
};
}
fn mapRelease(_: *Self, _: u32, _: u32, _: u32) tp.result {}
fn mapRelease(_: *Self, _: u32, _: u32, _: u32) !void {}
fn insert_code_point(self: *Self, c: u32) tp.result {
fn insert_code_point(self: *Self, c: u32) !void {
var buf: [32]u8 = undefined;
const bytes = ucs32_to_utf8(&[_]u32{c}, &buf) catch |e| return tp.exit_error(e);
self.file_path.appendSlice(buf[0..bytes]) catch |e| return tp.exit_error(e);
const bytes = try ucs32_to_utf8(&[_]u32{c}, &buf);
try self.file_path.appendSlice(buf[0..bytes]);
}
fn insert_bytes(self: *Self, bytes: []const u8) tp.result {
self.file_path.appendSlice(bytes) catch |e| return tp.exit_error(e);
fn insert_bytes(self: *Self, bytes: []const u8) !void {
try self.file_path.appendSlice(bytes);
}
fn cmd(_: *Self, name_: []const u8, ctx: command.Context) tp.result {

View file

@ -170,7 +170,7 @@ fn menu_action_execute_command(menu: **Menu.State(*Self), button: *Button.State(
fn on_scroll(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!void {
if (try m.match(.{ "scroll_to", tp.extract(&self.view_pos) })) {
try self.start_query();
self.start_query() catch |e| return tp.exit_error(e, @errorReturnTrace());
}
}
@ -186,14 +186,14 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
var text: []const u8 = undefined;
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) })) {
try self.mapEvent(evtype, keypress, egc, modifiers);
self.mapEvent(evtype, keypress, egc, modifiers) catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{ "system_clipboard", tp.extract(&text) })) {
try self.insert_bytes(text);
self.insert_bytes(text) catch |e| return tp.exit_error(e, @errorReturnTrace());
}
return false;
}
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) tp.result {
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) !void {
return switch (evtype) {
event_type.PRESS => self.mapPress(keypress, egc, modifiers),
event_type.REPEAT => self.mapPress(keypress, egc, modifiers),
@ -202,7 +202,7 @@ fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) t
};
}
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
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) {
@ -267,14 +267,14 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
};
}
fn mapRelease(self: *Self, keypress: u32, _: u32) tp.result {
fn mapRelease(self: *Self, keypress: u32, _: u32) !void {
return switch (keypress) {
key.LCTRL, key.RCTRL => if (self.menu.selected orelse 0 > 0) return self.cmd("command_palette_menu_activate", .{}),
else => {},
};
}
fn start_query(self: *Self) tp.result {
fn start_query(self: *Self) !void {
self.items = 0;
self.menu.reset_items();
self.menu.selected = null;
@ -289,10 +289,10 @@ fn start_query(self: *Self) tp.result {
defer pos += 1;
if (pos < self.view_pos) continue;
if (self.items < self.view_rows)
self.add_item(cmd_.name, cmd_.id, null) catch |e| return tp.exit_error(e);
try self.add_item(cmd_.name, cmd_.id, null);
}
} else {
_ = self.query_commands(self.inputbox.text.items) catch |e| return tp.exit_error(e);
_ = try self.query_commands(self.inputbox.text.items);
}
self.menu.select_down();
self.do_resize();
@ -360,7 +360,7 @@ fn add_item(self: *Self, name: []const u8, id: command.ID, matches: ?[]const usi
self.items += 1;
}
fn delete_word(self: *Self) tp.result {
fn delete_word(self: *Self) !void {
if (std.mem.lastIndexOfAny(u8, self.inputbox.text.items, "/\\. -_")) |pos| {
self.inputbox.text.shrinkRetainingCapacity(pos);
} else {
@ -371,7 +371,7 @@ fn delete_word(self: *Self) tp.result {
return self.start_query();
}
fn delete_code_point(self: *Self) tp.result {
fn delete_code_point(self: *Self) !void {
if (self.inputbox.text.items.len > 0) {
self.inputbox.text.shrinkRetainingCapacity(self.inputbox.text.items.len - 1);
self.inputbox.cursor = self.inputbox.text.items.len;
@ -380,17 +380,17 @@ fn delete_code_point(self: *Self) tp.result {
return self.start_query();
}
fn insert_code_point(self: *Self, c: u32) tp.result {
fn insert_code_point(self: *Self, c: u32) !void {
var buf: [6]u8 = undefined;
const bytes = ucs32_to_utf8(&[_]u32{c}, &buf) catch |e| return tp.exit_error(e);
self.inputbox.text.appendSlice(buf[0..bytes]) catch |e| return tp.exit_error(e);
const bytes = try ucs32_to_utf8(&[_]u32{c}, &buf);
try self.inputbox.text.appendSlice(buf[0..bytes]);
self.inputbox.cursor = self.inputbox.text.items.len;
self.view_pos = 0;
return self.start_query();
}
fn insert_bytes(self: *Self, bytes: []const u8) tp.result {
self.inputbox.text.appendSlice(bytes) catch |e| return tp.exit_error(e);
fn insert_bytes(self: *Self, bytes: []const u8) !void {
try self.inputbox.text.appendSlice(bytes);
self.inputbox.cursor = self.inputbox.text.items.len;
self.view_pos = 0;
return self.start_query();
@ -476,8 +476,9 @@ fn restore_state(self: *Self) !void {
const cmds = struct {
pub const Target = Self;
const Ctx = command.Context;
const Result = command.Result;
pub fn command_palette_menu_down(self: *Self, _: Ctx) tp.result {
pub fn command_palette_menu_down(self: *Self, _: Ctx) Result {
if (self.menu.selected) |selected| {
if (selected == self.view_rows - 1) {
self.view_pos += 1;
@ -489,7 +490,7 @@ const cmds = struct {
self.menu.select_down();
}
pub fn command_palette_menu_up(self: *Self, _: Ctx) tp.result {
pub fn command_palette_menu_up(self: *Self, _: Ctx) Result {
if (self.menu.selected) |selected| {
if (selected == 0 and self.view_pos > 0) {
self.view_pos -= 1;
@ -501,7 +502,7 @@ const cmds = struct {
self.menu.select_up();
}
pub fn command_palette_menu_pagedown(self: *Self, _: Ctx) tp.result {
pub fn command_palette_menu_pagedown(self: *Self, _: Ctx) Result {
if (self.total_items > self.view_rows) {
self.view_pos += self.view_rows;
if (self.view_pos > self.total_items - self.view_rows)
@ -511,14 +512,14 @@ const cmds = struct {
self.menu.select_last();
}
pub fn command_palette_menu_pageup(self: *Self, _: Ctx) tp.result {
pub fn command_palette_menu_pageup(self: *Self, _: Ctx) Result {
if (self.view_pos > self.view_rows)
self.view_pos -= self.view_rows;
try self.start_query();
self.menu.select_first();
}
pub fn command_palette_menu_activate(self: *Self, _: Ctx) tp.result {
pub fn command_palette_menu_activate(self: *Self, _: Ctx) Result {
self.menu.activate_selected();
}
};

View file

@ -150,20 +150,20 @@ fn add_item(self: *Self, file_name: []const u8, matches: ?[]const u8) !void {
fn receive_project_manager(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
if (try m.match(.{ "PRJ", tp.more })) {
try self.process_project_manager(m);
self.process_project_manager(m) catch |e| return tp.exit_error(e, @errorReturnTrace());
return true;
}
return false;
}
fn process_project_manager(self: *Self, m: tp.message) tp.result {
fn process_project_manager(self: *Self, m: tp.message) !void {
var file_name: []const u8 = undefined;
var matches: []const u8 = undefined;
var query: []const u8 = undefined;
if (try m.match(.{ "PRJ", "recent", tp.extract(&file_name), tp.extract_cbor(&matches) })) {
if (self.need_reset) self.reset_results();
self.longest = @max(self.longest, file_name.len);
self.add_item(file_name, matches) catch |e| return tp.exit_error(e);
try self.add_item(file_name, matches);
self.menu.resize(.{ .y = 0, .x = 25, .w = @min(self.longest, max_menu_width) + 2 });
if (self.need_select_first) {
self.menu.select_down();
@ -173,7 +173,7 @@ fn process_project_manager(self: *Self, m: tp.message) tp.result {
} else if (try m.match(.{ "PRJ", "recent", tp.extract(&file_name) })) {
if (self.need_reset) self.reset_results();
self.longest = @max(self.longest, file_name.len);
self.add_item(file_name, null) catch |e| return tp.exit_error(e);
try self.add_item(file_name, null);
self.menu.resize(.{ .y = 0, .x = 25, .w = @min(self.longest, max_menu_width) + 2 });
if (self.need_select_first) {
self.menu.select_down();
@ -198,14 +198,14 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
var text: []const u8 = undefined;
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) })) {
try self.mapEvent(evtype, keypress, egc, modifiers);
self.mapEvent(evtype, keypress, egc, modifiers) catch |e| return tp.exit_error(e, @errorReturnTrace());
} else if (try m.match(.{ "system_clipboard", tp.extract(&text) })) {
try self.insert_bytes(text);
self.insert_bytes(text) catch |e| return tp.exit_error(e, @errorReturnTrace());
}
return false;
}
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) tp.result {
fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) !void {
return switch (evtype) {
event_type.PRESS => self.mapPress(keypress, egc, modifiers),
event_type.REPEAT => self.mapPress(keypress, egc, modifiers),
@ -214,7 +214,7 @@ fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) t
};
}
fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
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) {
@ -271,7 +271,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
};
}
fn mapRelease(self: *Self, keypress: u32, _: u32) tp.result {
fn mapRelease(self: *Self, keypress: u32, _: u32) !void {
return switch (keypress) {
key.LCTRL, key.RCTRL => if (self.menu.selected orelse 0 > 0) return self.cmd("open_recent_menu_activate", .{}),
else => {},
@ -285,13 +285,13 @@ fn reset_results(self: *Self) void {
self.need_select_first = true;
}
fn start_query(self: *Self) tp.result {
fn start_query(self: *Self) !void {
if (self.query_pending) return;
self.query_pending = true;
try project_manager.query_recent_files(max_recent_files, self.inputbox.text.items);
}
fn delete_word(self: *Self) tp.result {
fn delete_word(self: *Self) !void {
if (std.mem.lastIndexOfAny(u8, self.inputbox.text.items, "/\\. -_")) |pos| {
self.inputbox.text.shrinkRetainingCapacity(pos);
} else {
@ -301,7 +301,7 @@ fn delete_word(self: *Self) tp.result {
return self.start_query();
}
fn delete_code_point(self: *Self) tp.result {
fn delete_code_point(self: *Self) !void {
if (self.inputbox.text.items.len > 0) {
self.inputbox.text.shrinkRetainingCapacity(self.inputbox.text.items.len - 1);
self.inputbox.cursor = self.inputbox.text.items.len;
@ -309,16 +309,16 @@ fn delete_code_point(self: *Self) tp.result {
return self.start_query();
}
fn insert_code_point(self: *Self, c: u32) tp.result {
fn insert_code_point(self: *Self, c: u32) !void {
var buf: [6]u8 = undefined;
const bytes = ucs32_to_utf8(&[_]u32{c}, &buf) catch |e| return tp.exit_error(e);
self.inputbox.text.appendSlice(buf[0..bytes]) catch |e| return tp.exit_error(e);
const bytes = try ucs32_to_utf8(&[_]u32{c}, &buf);
try self.inputbox.text.appendSlice(buf[0..bytes]);
self.inputbox.cursor = self.inputbox.text.items.len;
return self.start_query();
}
fn insert_bytes(self: *Self, bytes: []const u8) tp.result {
self.inputbox.text.appendSlice(bytes) catch |e| return tp.exit_error(e);
fn insert_bytes(self: *Self, bytes: []const u8) !void {
try self.inputbox.text.appendSlice(bytes);
self.inputbox.cursor = self.inputbox.text.items.len;
return self.start_query();
}
@ -339,16 +339,17 @@ const Commands = command.Collection(cmds);
const cmds = struct {
pub const Target = Self;
const Ctx = command.Context;
const Result = command.Result;
pub fn open_recent_menu_down(self: *Self, _: Ctx) tp.result {
pub fn open_recent_menu_down(self: *Self, _: Ctx) Result {
self.menu.select_down();
}
pub fn open_recent_menu_up(self: *Self, _: Ctx) tp.result {
pub fn open_recent_menu_up(self: *Self, _: Ctx) Result {
self.menu.select_up();
}
pub fn open_recent_menu_activate(self: *Self, _: Ctx) tp.result {
pub fn open_recent_menu_activate(self: *Self, _: Ctx) Result {
self.menu.activate_selected();
}
};