From 25335e94a639cb020c5e077692ffdc855253acef Mon Sep 17 00:00:00 2001 From: KikooDX Date: Thu, 25 Feb 2021 23:31:23 +0100 Subject: [PATCH] Allow user to configure keybindings. --- README.md | 8 ++++++ src/conf.zig | 21 ++++++++++++++++ src/main.zig | 71 +++++++++++++++++++++++++++++----------------------- 3 files changed, 69 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index d681c3c..d3f9c72 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,14 @@ $ kble sample.kble $ kble $HOME/projects/kble/sample.kble ``` +# Configuration. +In `src/conf.zig`, edit values from `BEGIN USER CONFIG`. +The configuration is applied at compile time. You have the possibility to: +* Disable mouse support. +* Swap mouse buttons. +* Change and add colors. +* Edit keybindings. + # Default keybindings Repetition/parameter: * `0` → `9`: behavior similar to Vi. diff --git a/src/conf.zig b/src/conf.zig index 96a1652..6fb12b3 100644 --- a/src/conf.zig +++ b/src/conf.zig @@ -41,4 +41,25 @@ pub fn cell_color(cell: cell_type) ray.Color { else => ray.PURPLE, // undefined }; } + +/// Used to set keybindings. +/// Only bind to default keybindings, use 0 to unbind a key. +pub fn bind_key(key: u8) u8 { + return switch (key) { + // // jkl; movement. + // 'j' => 'h', + // 'k' => 'j', + // 'l' => 'k', + // ';' => 'l', + // // Backward movement with HJKL. + // 'H' => 'l', + // 'J' => 'k', + // 'K' => 'j', + // 'L' => 'h', + // // Use x instead of return for normal mode. + // '\n' => 0, + // 'x' => '\n', + else => key + }; +} // END USER CONFIG diff --git a/src/main.zig b/src/main.zig index 1a8d39b..7701285 100644 --- a/src/main.zig +++ b/src/main.zig @@ -76,39 +76,48 @@ pub fn main() void { // Create binding "table". var bindings = [_]*const actions.Action{&actions.ActionsDef.none} ** char_range; - // Set default bindings. - // Parameters/repetition. - { - var i: u8 = 0; - while (i < 10) : (i += 1) { - bindings['0' + i] = &ActionsDef.parameter; + comptime { + // Set default bindings. + var default_bindings = [_]*const actions.Action{&actions.ActionsDef.none} ** char_range; + comptime { + var i: u8 = 0; + while (i < 10) : (i += 1) { + default_bindings['0' + i] = &ActionsDef.parameter; + } + } + // Movement. + default_bindings['h'] = &ActionsDef.move_left; + default_bindings['j'] = &ActionsDef.move_down; + default_bindings['k'] = &ActionsDef.move_up; + default_bindings['l'] = &ActionsDef.move_right; + default_bindings['y'] = &ActionsDef.move_up_left; + default_bindings['u'] = &ActionsDef.move_up_right; + default_bindings['b'] = &ActionsDef.move_down_left; + default_bindings['n'] = &ActionsDef.move_down_right; + // Verbs. + default_bindings[' '] = &ActionsDef.verb_clear_selection; + default_bindings['d'] = &ActionsDef.verb_delete; + default_bindings['r'] = &ActionsDef.verb_replace; + // Scale. + default_bindings['='] = &ActionsDef.scale_reset; + default_bindings['+'] = &ActionsDef.scale_up; + default_bindings['-'] = &ActionsDef.scale_down; + // File. + default_bindings['e'] = &ActionsDef.file_read; + default_bindings['w'] = &ActionsDef.file_write; + // Mode. + default_bindings['\n'] = &ActionsDef.mode_normal; + default_bindings['i'] = &ActionsDef.mode_select; + default_bindings['v'] = &ActionsDef.mode_rectangle; + default_bindings['c'] = &ActionsDef.mode_camera; + // Map user bindings. + comptime { + var i: u8 = 0; + while (i < char_range) : (i += 1) { + bindings[i] = default_bindings[conf.bind_key(i)]; + } } } - // Movement. - bindings['h'] = &ActionsDef.move_left; - bindings['j'] = &ActionsDef.move_down; - bindings['k'] = &ActionsDef.move_up; - bindings['l'] = &ActionsDef.move_right; - bindings['y'] = &ActionsDef.move_up_left; - bindings['u'] = &ActionsDef.move_up_right; - bindings['b'] = &ActionsDef.move_down_left; - bindings['n'] = &ActionsDef.move_down_right; - // Verbs. - bindings[' '] = &ActionsDef.verb_clear_selection; - bindings['d'] = &ActionsDef.verb_delete; - bindings['r'] = &ActionsDef.verb_replace; - // Scale. - bindings['='] = &ActionsDef.scale_reset; - bindings['+'] = &ActionsDef.scale_up; - bindings['-'] = &ActionsDef.scale_down; - // File. - bindings['e'] = &ActionsDef.file_read; - bindings['w'] = &ActionsDef.file_write; - // Mode. - bindings['\n'] = &ActionsDef.mode_normal; - bindings['i'] = &ActionsDef.mode_select; - bindings['v'] = &ActionsDef.mode_rectangle; - bindings['c'] = &ActionsDef.mode_camera; // Create input buffer. const input_buffer_len = 255;