commit a1ecbb9cf0a6d7c32170cfef53d0f93bf4f78dc1
parent 62d20d78b83589833ee41922cde6121b646557a8
Author: Martin Ashby <martin@ashbysoft.com>
Date: Sun, 28 Jan 2024 20:01:23 +0000
Step 107, it is now an editor!
Diffstat:
2 files changed, 40 insertions(+), 4 deletions(-)
diff --git a/src/main.zig b/src/main.zig
@@ -1,9 +1,14 @@
const std = @import("std");
const version = @import("options").version;
-const quit: editorKey = .{ .char = 17 }; // ctrl+q
+const quit: editorKey = .{ .char = CTRL_KEY('q') };
const tabstop = 8;
+//#define CTRL_KEY(k) ((k) & 0x1f)
+fn CTRL_KEY(k: u8) u8 {
+ return k & 0x1f;
+}
+
var E: ?*EditorState = null; // ONLY use this for the log function...
pub const std_options = struct {
pub fn logFn(
@@ -168,6 +173,7 @@ const editorKey = union(enum) {
HOME,
END,
DEL,
+ BACKSPACE,
},
};
@@ -178,7 +184,9 @@ fn editorReadKey() !editorKey {
error.EndOfStream => continue,
else => return e,
};
- if (ch == '\x1b') {
+ if (ch == 127) {
+ return .{.virt = .BACKSPACE};
+ } else if (ch == '\x1b') {
const ch1 = rdr.readByte() catch |e| switch (e) {
error.EndOfStream => return .{ .char = '\x1b' },
else => return e,
@@ -336,6 +344,27 @@ fn editorOpen(es: *EditorState, filename: []const u8) !void {
es.filename = filename;
}
+fn editorSave(es: *EditorState) !void {
+ if (es.filename) |fname| {
+ const str = try editorRowsToString(es);
+ defer es.a.free(str);
+ const f = try std.fs.cwd().openFile(fname, .{.mode = .write_only});
+ defer f.close();
+ try f.setEndPos(str.len);
+ try f.writeAll(str);
+ }
+}
+
+fn editorRowsToString(es: *EditorState) ![]const u8 {
+ var str = CharAL{};
+ defer str.deinit(es.a);
+ for (es.erow.items) |erow| {
+ try str.appendSlice(es.a, erow.chars.items);
+ try str.append(es.a, '\n');
+ }
+ return try str.toOwnedSlice(es.a);
+}
+
//// Output
fn clearScreen() void {
@@ -471,13 +500,20 @@ fn editorProcessKeyPress(es: *EditorState) !void {
.char => |ch| {
switch (ch) {
quit.char => return error.Quit,
+ CTRL_KEY('s') => try editorSave(es),
+ '\r' => {
+ // TODO
+ },
else => try editorInsertChar(es, ch),
}
},
.virt => |v| {
switch (v) {
.ARROW_UP, .ARROW_DOWN, .ARROW_LEFT, .ARROW_RIGHT, .PAGE_UP, .PAGE_DOWN, .HOME, .END => editorMoveCursor(key, es),
- else => {},
+ .DEL, .BACKSPACE => {
+ // TODO
+ },
+ //else => {},
}
},
}
diff --git a/tabs.txt b/tabs.txt
@@ -1,3 +1,3 @@
- hi there I have a tab
+ho there hi there I have a tab
I don't
I have a tab after some space