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 --- build.zig | 4 +++- es.ini.in | 1 + src/Buffer.zig | 15 +++++++++++++++ src/Config.zig | 16 +++++++++++++++- src/key_state.zig | 2 ++ 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"; const config = struct { template: []const u8 = @embedFile("es.ini.in"), default: struct { + hard_tabs: bool = false, line_limit: usize = 100, page_overlap: usize = 2, - tab_stop: usize = 8, + tab_stop: usize = 4, } = .{}, }{}; @@ -38,6 +39,7 @@ pub fn build(b: *Builder) void { "es_version", SemanticVersion.parse(version) catch unreachable, ); + options.addOption(bool, "default_hard_tabs", config.default.hard_tabs); options.addOption(usize, "default_line_limit", config.default.line_limit); options.addOption(usize, "default_page_overlap", config.default.page_overlap); 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 @@ # # Copy this config to ~/.config/arkta/es/es.ini to modify it. +hard-tabs = {[hard_tabs]} line-limit = {[line_limit]} page-overlap = {[page_overlap]} tab-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 { 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