From 6feb751f766631488bb908c000564c67e3552f0b Mon Sep 17 00:00:00 2001 From: Adriel Velazquez Date: Tue, 21 Jan 2025 23:32:56 -0800 Subject: [PATCH] Stop gap to allow opening in the editor --- src/Surface.zig | 28 ++++++++++++++++++++++++++++ src/config/Config.zig | 6 ++++++ src/input/Binding.zig | 1 + 3 files changed, 35 insertions(+) diff --git a/src/Surface.zig b/src/Surface.zig index d9a985aa7..be1c16b7d 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -16,6 +16,7 @@ pub const Mailbox = apprt.surface.Mailbox; pub const Message = apprt.surface.Message; const std = @import("std"); +const process = std.process; const builtin = @import("builtin"); const assert = std.debug.assert; const Allocator = std.mem.Allocator; @@ -4317,6 +4318,32 @@ fn closingAction(action: input.Binding.Action) bool { }; } +fn openScreenFile( + self: *Surface, + file_path: []const u8, +) !void { + const editor = std.posix.getenv("EDITOR") orelse { + std.log.err("EDITOR environment variable not set", .{}); + return error.EnvironmentVariableNotFound; + }; + const allocator = std.heap.page_allocator; + const command_buf = try allocator.alloc(u8, editor.len + file_path.len + 2); + errdefer allocator.free(command_buf); + defer allocator.free(command_buf); + // This is quite ugly sending the message to the mailbox and executing it on behalf of the user + // Spawing a process doesn't render the command in the main surface. + const command = try std.fmt.bufPrint( + command_buf, + "{s} {s}\n", + .{ editor, file_path }, + ); + + self.io.queueMessage(try termio.Message.writeReq( + allocator, + command, + ), .unlocked); +} + /// The portion of the screen to write for writeScreenFile. const WriteScreenLoc = enum { screen, // Full screen @@ -4413,6 +4440,7 @@ fn writeScreenFile( switch (write_action) { .open => try internal_os.open(self.alloc, .text, path), + .editor => try self.openScreenFile(path), .paste => self.io.queueMessage(try termio.Message.writeReq( self.alloc, path, diff --git a/src/config/Config.zig b/src/config/Config.zig index 0ed98bdea..e563ac401 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -2393,6 +2393,12 @@ pub fn default(alloc_gpa: Allocator) Allocator.Error!Config { .{ .write_screen_file = .paste }, ); + try result.keybind.set.put( + alloc, + .{ .key = .{ .translated = .f }, .mods = inputpkg.ctrlOrSuper(.{ .shift = true }) }, + .{ .write_screen_file = .editor }, + ); + try result.keybind.set.put( alloc, .{ .key = .{ .translated = .j }, .mods = inputpkg.ctrlOrSuper(.{ .shift = true, .alt = true }) }, diff --git a/src/input/Binding.zig b/src/input/Binding.zig index 19c103195..27e1fa816 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -592,6 +592,7 @@ pub const Action = union(enum) { pub const WriteScreenAction = enum { paste, open, + editor, }; // Extern because it is used in the embedded runtime ABI.