From 758a0611d098876ca93ae49179148f5ef5cf1b06 Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Fri, 31 Dec 2021 21:02:23 +0200 Subject: Now can do soft tabs yay --- src/Buffer.zig | 15 +++++++++++++++ src/Config.zig | 16 +++++++++++++++- src/key_state.zig | 2 ++ 3 files changed, 32 insertions(+), 1 deletion(-) (limited to 'src') 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 { self.dirty = true; } +pub fn insertNChars(self: *Buffer, char: u8, count: usize) !void { + var remaining = count; + while (remaining > 0) : (remaining -= 1) { + try self.insertChar(char); + } +} + pub fn insertNewline(self: *Buffer) !void { self.dirty = true; @@ -331,6 +338,14 @@ pub fn insertRow(self: *Buffer, at: usize, data: []const u8) !void { self.dirty = true; } +pub fn insertTab(self: *Buffer) !void { + if (self.config.hard_tabs) { + return self.insertChar('\t'); + } else { + return self.insertNChars(' ', self.config.tab_stop - self.cx % self.config.tab_stop); + } +} + pub fn killLine(self: *Buffer) !void { return self.deleteRow(self.cy); } 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(); const config_path = "arkta/es/es.ini"; +hard_tabs: bool = conf.default_hard_tabs, line_limit: usize = conf.default_line_limit, page_overlap: usize = conf.default_page_overlap, tab_stop: usize = conf.default_tab_stop, @@ -42,6 +43,17 @@ pub fn readConfig(allocator: Allocator) !Config { return config; } +fn parseBool(str: []const u8) !bool { + if (std.mem.eql(u8, str, "true")) { + return true; + } else if (std.mem.eql(u8, str, "false")) { + return false; + } else { + std.log.err("How to interpret '{s}' as a bool?", .{str}); + return error.MalformedConfig; + } +} + fn readConfigInBaseDir(allocator: Allocator, config: *Config, base_dir: []const u8) !void { const filename = try std.fs.path.join(allocator, &.{ base_dir, config_path }); defer allocator.free(filename); @@ -67,7 +79,9 @@ fn readConfigInBaseDir(allocator: Allocator, config: *Config, base_dir: []const const key = std.mem.trimRight(u8, line[0..split], &std.ascii.spaces); const value = std.mem.trimLeft(u8, line[(split + 1)..], &std.ascii.spaces); - if (std.mem.eql(u8, key, "line-limit")) { + if (std.mem.eql(u8, key, "hard-tabs")) { + config.hard_tabs = try parseBool(value); + } else if (std.mem.eql(u8, key, "line-limit")) { config.line_limit = try std.fmt.parseUnsigned(usize, value, 0); } else if (std.mem.eql(u8, key, "page-overlap")) { 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 { // TODO: C-h help + // tab + Key.ctrl('i') => try buf.insertTab(), // line feed Key.ctrl('j') => try buf.insertNewline(), Key.ctrl('k') => try buf.killLine(), -- cgit v1.2.3