summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2021-12-31 21:02:23 +0200
committerGravatar Uko Kokņevičs2021-12-31 21:02:23 +0200
commit758a0611d098876ca93ae49179148f5ef5cf1b06 (patch)
treef9bc54241f95b7e8647c5c5a1836d7fe86a5c14e
parentSome changes (diff)
downloades-758a0611d098876ca93ae49179148f5ef5cf1b06.tar.gz
es-758a0611d098876ca93ae49179148f5ef5cf1b06.tar.xz
es-758a0611d098876ca93ae49179148f5ef5cf1b06.zip
Now can do soft tabs yay
-rw-r--r--build.zig4
-rw-r--r--es.ini.in1
-rw-r--r--src/Buffer.zig15
-rw-r--r--src/Config.zig16
-rw-r--r--src/key_state.zig2
5 files changed, 36 insertions, 2 deletions
diff --git a/build.zig b/build.zig
index 6fac7d1..d65020a 100644
--- a/build.zig
+++ b/build.zig
@@ -10,9 +10,10 @@ const version = "0.3.0";
10const config = struct { 10const config = struct {
11 template: []const u8 = @embedFile("es.ini.in"), 11 template: []const u8 = @embedFile("es.ini.in"),
12 default: struct { 12 default: struct {
13 hard_tabs: bool = false,
13 line_limit: usize = 100, 14 line_limit: usize = 100,
14 page_overlap: usize = 2, 15 page_overlap: usize = 2,
15 tab_stop: usize = 8, 16 tab_stop: usize = 4,
16 } = .{}, 17 } = .{},
17}{}; 18}{};
18 19
@@ -38,6 +39,7 @@ pub fn build(b: *Builder) void {
38 "es_version", 39 "es_version",
39 SemanticVersion.parse(version) catch unreachable, 40 SemanticVersion.parse(version) catch unreachable,
40 ); 41 );
42 options.addOption(bool, "default_hard_tabs", config.default.hard_tabs);
41 options.addOption(usize, "default_line_limit", config.default.line_limit); 43 options.addOption(usize, "default_line_limit", config.default.line_limit);
42 options.addOption(usize, "default_page_overlap", config.default.page_overlap); 44 options.addOption(usize, "default_page_overlap", config.default.page_overlap);
43 options.addOption(usize, "default_tab_stop", config.default.tab_stop); 45 options.addOption(usize, "default_tab_stop", config.default.tab_stop);
diff --git a/es.ini.in b/es.ini.in
index 8b4e6c0..99a37d5 100644
--- a/es.ini.in
+++ b/es.ini.in
@@ -2,6 +2,7 @@
2# 2#
3# Copy this config to ~/.config/arkta/es/es.ini to modify it. 3# Copy this config to ~/.config/arkta/es/es.ini to modify it.
4 4
5hard-tabs = {[hard_tabs]}
5line-limit = {[line_limit]} 6line-limit = {[line_limit]}
6page-overlap = {[page_overlap]} 7page-overlap = {[page_overlap]}
7tab-stop = {[tab_stop]} 8tab-stop = {[tab_stop]}
diff --git a/src/Buffer.zig b/src/Buffer.zig
index cb89935..f105c55 100644
--- a/src/Buffer.zig
+++ b/src/Buffer.zig
@@ -291,6 +291,13 @@ pub fn insertChar(self: *Buffer, char: u8) !void {
291 self.dirty = true; 291 self.dirty = true;
292} 292}
293 293
294pub fn insertNChars(self: *Buffer, char: u8, count: usize) !void {
295 var remaining = count;
296 while (remaining > 0) : (remaining -= 1) {
297 try self.insertChar(char);
298 }
299}
300
294pub fn insertNewline(self: *Buffer) !void { 301pub fn insertNewline(self: *Buffer) !void {
295 self.dirty = true; 302 self.dirty = true;
296 303
@@ -331,6 +338,14 @@ pub fn insertRow(self: *Buffer, at: usize, data: []const u8) !void {
331 self.dirty = true; 338 self.dirty = true;
332} 339}
333 340
341pub fn insertTab(self: *Buffer) !void {
342 if (self.config.hard_tabs) {
343 return self.insertChar('\t');
344 } else {
345 return self.insertNChars(' ', self.config.tab_stop - self.cx % self.config.tab_stop);
346 }
347}
348
334pub fn killLine(self: *Buffer) !void { 349pub fn killLine(self: *Buffer) !void {
335 return self.deleteRow(self.cy); 350 return self.deleteRow(self.cy);
336} 351}
diff --git a/src/Config.zig b/src/Config.zig
index 16b4043..071c31b 100644
--- a/src/Config.zig
+++ b/src/Config.zig
@@ -9,6 +9,7 @@ const Config = @This();
9 9
10const config_path = "arkta/es/es.ini"; 10const config_path = "arkta/es/es.ini";
11 11
12hard_tabs: bool = conf.default_hard_tabs,
12line_limit: usize = conf.default_line_limit, 13line_limit: usize = conf.default_line_limit,
13page_overlap: usize = conf.default_page_overlap, 14page_overlap: usize = conf.default_page_overlap,
14tab_stop: usize = conf.default_tab_stop, 15tab_stop: usize = conf.default_tab_stop,
@@ -42,6 +43,17 @@ pub fn readConfig(allocator: Allocator) !Config {
42 return config; 43 return config;
43} 44}
44 45
46fn parseBool(str: []const u8) !bool {
47 if (std.mem.eql(u8, str, "true")) {
48 return true;
49 } else if (std.mem.eql(u8, str, "false")) {
50 return false;
51 } else {
52 std.log.err("How to interpret '{s}' as a bool?", .{str});
53 return error.MalformedConfig;
54 }
55}
56
45fn readConfigInBaseDir(allocator: Allocator, config: *Config, base_dir: []const u8) !void { 57fn readConfigInBaseDir(allocator: Allocator, config: *Config, base_dir: []const u8) !void {
46 const filename = try std.fs.path.join(allocator, &.{ base_dir, config_path }); 58 const filename = try std.fs.path.join(allocator, &.{ base_dir, config_path });
47 defer allocator.free(filename); 59 defer allocator.free(filename);
@@ -67,7 +79,9 @@ fn readConfigInBaseDir(allocator: Allocator, config: *Config, base_dir: []const
67 79
68 const key = std.mem.trimRight(u8, line[0..split], &std.ascii.spaces); 80 const key = std.mem.trimRight(u8, line[0..split], &std.ascii.spaces);
69 const value = std.mem.trimLeft(u8, line[(split + 1)..], &std.ascii.spaces); 81 const value = std.mem.trimLeft(u8, line[(split + 1)..], &std.ascii.spaces);
70 if (std.mem.eql(u8, key, "line-limit")) { 82 if (std.mem.eql(u8, key, "hard-tabs")) {
83 config.hard_tabs = try parseBool(value);
84 } else if (std.mem.eql(u8, key, "line-limit")) {
71 config.line_limit = try std.fmt.parseUnsigned(usize, value, 0); 85 config.line_limit = try std.fmt.parseUnsigned(usize, value, 0);
72 } else if (std.mem.eql(u8, key, "page-overlap")) { 86 } else if (std.mem.eql(u8, key, "page-overlap")) {
73 config.page_overlap = try std.fmt.parseUnsigned(usize, value, 0); 87 config.page_overlap = try std.fmt.parseUnsigned(usize, value, 0);
diff --git a/src/key_state.zig b/src/key_state.zig
index 0ea30cb..d477094 100644
--- a/src/key_state.zig
+++ b/src/key_state.zig
@@ -77,6 +77,8 @@ pub fn defaultState(editor: *Editor, buf: *Buffer, key: Key) Error!void {
77 77
78 // TODO: C-h help 78 // TODO: C-h help
79 79
80 // tab
81 Key.ctrl('i') => try buf.insertTab(),
80 // line feed 82 // line feed
81 Key.ctrl('j') => try buf.insertNewline(), 83 Key.ctrl('j') => try buf.insertNewline(),
82 Key.ctrl('k') => try buf.killLine(), 84 Key.ctrl('k') => try buf.killLine(),