diff --git a/src/Buffer.zig b/src/Buffer.zig index 15031bb..7a11322 100644 --- a/src/Buffer.zig +++ b/src/Buffer.zig @@ -996,6 +996,13 @@ pub fn load_from_string(self: *const Self, s: []const u8) !Root { return self.load(stream.reader(), s.len); } +pub fn load_from_string_and_update(self: *Self, file_path: []const u8, s: []const u8) !void { + self.root = try self.load_from_string(s); + self.file_path = try self.a.dupe(u8, file_path); + self.last_save = self.root; + self.file_exists = false; +} + pub fn load_from_file(self: *const Self, file_path: []const u8, file_exists: *bool) !Root { const file = cwd().openFile(file_path, .{ .mode = .read_only }) catch |e| switch (e) { error.FileNotFound => return self.new_file(file_exists), diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 7a1772c..5cee975 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -340,7 +340,8 @@ pub const Editor = struct { fn open_scratch(self: *Self, file_path: []const u8, content: []const u8) !void { var new_buf = try Buffer.create(self.a); errdefer new_buf.deinit(); - try new_buf.load_from_string(content); + try new_buf.load_from_string_and_update(file_path, content); + new_buf.file_exists = true; return self.open_buffer(file_path, new_buf); } @@ -2603,7 +2604,7 @@ pub const Editor = struct { var file_path: []const u8 = undefined; var content: []const u8 = undefined; if (ctx.args.match(.{ tp.extract(&file_path), tp.extract(&content) }) catch false) { - self.open(file_path) catch |e| return tp.exit_error(e); + self.open_scratch(file_path, content) catch |e| return tp.exit_error(e); self.clamp(); } else return tp.exit_error(error.InvalidArgument); }