be

based editor
git clone git://git.kocotian.pl/be.git
Log | Files | Refs | README | LICENSE

commit e64f1b8c7ebd1ff5dac160e481bca42dd56b6403
parent 62989ce05f1d5520fdb0f62cc81a5a056fc35dc0
Author: kocotian <kocotian@kocotian.pl>
Date:   Sat, 17 Apr 2021 20:35:45 +0200

Bindings in config.h

Diffstat:
Mbe.c | 42++++++++++++++++++++++++++++++++++++++----
Aconfig.h | 6++++++
Mutil.h | 2++
3 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/be.c b/be.c @@ -28,6 +28,26 @@ /* macros */ #define CTRL_KEY(KEY) ((KEY) & 0x1f) +/* types */ +typedef enum Mod { + ModNone = 0, ModControl = 0x1f, ModShift = 0xdf, +} Mod; + +typedef union Arg { + int i; + unsigned int ui; + char c; + float f; + const void *v; +} Arg; + +typedef struct Key { + Mod mod; + unsigned char key; + void (*func)(const Arg *); + const Arg arg; +} Key; + /* prototypes */ static void rawRestore(void); static void rawOn(void); @@ -37,8 +57,11 @@ static void edit(void); static void editorParseKey(unsigned char key); /*********/ static void finish(void); +/*********/ +static void quit(Arg *arg); /* global variables */ +#include "config.h" static struct termios origtermios; /* terminal */ @@ -83,10 +106,13 @@ editorGetKey(int fd) static void editorParseKey(unsigned char key) { - switch (key) { - case 'Q': - finish(); break; - } + size_t i; + for (i = 0; i < LEN(bindings); ++i) + if (key == (bindings[i].key & bindings[i].mod)) { + (bindings[i].func)(&(bindings[i].arg)); + return; + } + /* TODO: information that key is not bound */ } static void @@ -105,6 +131,14 @@ finish(void) exit(0); } +/* editor functions */ +static void +quit(Arg *arg) +{ + (void)arg; + finish(); +} + int main(int argc, char *argv[]) { diff --git a/config.h b/config.h @@ -0,0 +1,6 @@ +/* See COPYRIGHT file for copyright and license details */ + +static Key bindings[] = { + /* modifier key function argument */ + { ModShift, 'q', quit, {0} }, +}; diff --git a/util.h b/util.h @@ -3,6 +3,8 @@ #ifndef _UTIL_H #define _UTIL_H +#define LEN(X) (sizeof (X) / sizeof *(X)) + #define MAX(A, B) ((signed)(A) > (signed)(B) ? (signed)(A) : (signed)(B)) #define MIN(A, B) ((signed)(A) < (signed)(B) ? (signed)(A) : (signed)(B)) #define BETWEEN(X, A, B) ((signed)(A) <= (signed)(X) && (signed)(X) <= (signed)(B))