be

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

commit b1b1dcb1ec7935433ea374551d4bceda2ef34d3b
parent 6fcf59ea96fde0092c1fd5d886a256c6d738a4d4
Author: kocotian <kocotian@kocotian.pl>
Date:   Mon, 19 Apr 2021 13:23:26 +0200

Global submode, submode push/pop, submodes on status

Diffstat:
Mbe.c | 67++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Mconfig/bindings.h | 16+++++++++++++++-
Mlang/en_US.h | 7+++++--
Mlang/pl_PL.h | 7+++++--
4 files changed, 81 insertions(+), 16 deletions(-)

diff --git a/be.c b/be.c @@ -43,6 +43,7 @@ #define CURBUF(EDITOR) ((EDITOR).bufs.data[(EDITOR).curbuf]) #define REPLACE (void *)(-1) +#define SUBMODES_MAX 32 #ifdef UNLIMITED #define PATH_MAX 1024 @@ -55,7 +56,9 @@ typedef enum Mod { typedef enum Mode { ModeNormal, ModeEdit, - SubModeBuffer + SubModeGlobal, + SubModeMovement, + SubModeBuffer, } Mode; typedef union Arg { @@ -79,7 +82,7 @@ typedef struct Buffer { int anonymous, dirty; ssize_t x, y; Mode mode; - Mode submodes[32]; + Mode submodes[SUBMODES_MAX]; size_t submodeslen; } Buffer; @@ -113,6 +116,8 @@ static void freeBuffer(Buffer *buf); static int writeBuffer(Buffer *buf, char *filename); static int minibufferPrint(const char *s); static int minibufferError(const char *s); +static inline int submodePush(Buffer *b, Mode m); +static inline int submodePop(Buffer *b); /*********/ static void setup(char *filename); static void finish(void); @@ -123,6 +128,7 @@ static void echoe(const Arg *arg); static void normalmode(const Arg *arg); static void insertmode(const Arg *arg); static void appendmode(const Arg *arg); +static void globalsubmode(const Arg *arg); static void buffersubmode(const Arg *arg); static void cursormove(const Arg *arg); static void beginning(const Arg *arg); @@ -248,7 +254,7 @@ appendStatus(String *ab) abAppend(ab, "\r", 1); /* status drawing */ { - abPrintf(ab, cp, 256, "%c:%c %s L%ld of %ld C%ld of %ld (%s) %ld buffer(s)", + abPrintf(ab, cp, 256, "%c:%c %s L%ld of %ld C%ld of %ld (%s", CURBUF(editor).anonymous ? 'U' : '-', CURBUF(editor).dirty ? '*' : '-', CURBUF(editor).anonymous ? @@ -257,7 +263,14 @@ appendStatus(String *ab) CURBUF(editor).rows.len, CURBUF(editor).x + 1, CURBUF(editor).rows.data[CURBUF(editor).y].len, - lang_modes[CURBUF(editor).mode], + lang_modes[CURBUF(editor).mode] + ); + for (i = 0; i < (signed)CURBUF(editor).submodeslen; ++i) { + abPrintf(ab, cp, 256, "/%s", + lang_modes[CURBUF(editor).submodes[i]] + ); + } + abPrintf(ab, cp, 256, ") %ld buffer(s)", editor.bufs.len - 1 ); } @@ -297,17 +310,20 @@ editorGetKey(void) static void editorParseKey(unsigned char key) { + Binding *binds; size_t i; Arg arg; - for (i = 0; i < bindings[CURBUF(editor).mode].len; ++i) - if (key == ((bindings[CURBUF(editor).mode]).keys[i].key - & (bindings[CURBUF(editor).mode]).keys[i].mod) - || i == bindings[CURBUF(editor).mode].len - 1) { - ((bindings[CURBUF(editor).mode]).keys[i].func) + binds = &bindings[CURBUF(editor).submodeslen ? + CURBUF(editor).submodes[CURBUF(editor).submodeslen - 1] : CURBUF(editor).mode]; + for (i = 0; i < binds->len; ++i) + if (key == (binds->keys[i].key + & binds->keys[i].mod) + || i == binds->len - 1) { + (binds->keys[i].func) ( - (bindings[CURBUF(editor).mode]).keys[i].arg.v == REPLACE ? + binds->keys[i].arg.v == REPLACE ? arg.c = (char)(key), &arg : - &((bindings[CURBUF(editor).mode]).keys[i].arg) + &(binds->keys[i].arg) ); return; } @@ -471,6 +487,24 @@ minibufferError(const char *s) return 1; } +static inline int +submodePush(Buffer *b, Mode m) +{ + if (b->submodeslen >= SUBMODES_MAX) + return 1; + b->submodes[(b->submodeslen)++] = m; + return 0; +} + +static inline int +submodePop(Buffer *b) +{ + if (!b->submodeslen) + return 1; + --(b->submodeslen); + return 0; +} + /* other */ static void setup(char *filename) @@ -544,6 +578,17 @@ appendmode(const Arg *arg) } static void +globalsubmode(const Arg *arg) +{ + (void)arg; + submodePush(&CURBUF(editor), SubModeGlobal); + submodePush(&CURBUF(editor), SubModeMovement); + editorParseKey(editorGetKey()); + submodePop(&CURBUF(editor)); /* SubModeMovement */ + submodePop(&CURBUF(editor)); /* SubModeGlobal */ +} + +static void buffersubmode(const Arg *arg) { (void)arg; diff --git a/config/bindings.h b/config/bindings.h @@ -24,8 +24,11 @@ normalbindings[] = { { ModNone, 'O', openline, {1} }, { ModNone, '\r', openline, {2} }, - /* other */ + /* Submodes */ + { ModNone, 'g', globalsubmode, {0} }, { ModShift, 'z', buffersubmode, {0} }, + + /* other */ { ModNone, 0, echoe, {.v = "Key is not bound"} }, }, @@ -38,6 +41,15 @@ editbindings[] = { }, /* submodes */ +s_globalbindings[] = { + /* modifier key function argument */ + { ModNone, 0, echoe, {.v = "Key is not bound"} }, +}, + +s_movementbindings[] = { + { ModNone, 0, echoe, {.v = "Key is not bound"} }, +}, + s_bufferbindings[] = { /* modifier key function argument */ { ModShift, 'z', bufwriteclose, {0} }, @@ -53,5 +65,7 @@ static Binding bindings[] = { /* mode keys */ [ModeEdit] = BIND(editbindings), [ModeNormal] = BIND(normalbindings), + [SubModeGlobal] = BIND(s_globalbindings), + [SubModeMovement] = BIND(s_movementbindings), [SubModeBuffer] = BIND(s_bufferbindings), }; diff --git a/lang/en_US.h b/lang/en_US.h @@ -5,8 +5,11 @@ static char [2] = "welcome to be, version " VERSION, }, *lang_modes[] = { - [ModeNormal] = "Normal", - [ModeEdit] = "Edit", + [ModeNormal] = "Normal", + [ModeEdit] = "Edit", + [SubModeGlobal] = "Global", + [SubModeMovement] = "Movement", + [SubModeBuffer] = "Buffer", }, *lang_err[] = { [0] = "usage", diff --git a/lang/pl_PL.h b/lang/pl_PL.h @@ -5,8 +5,11 @@ static char [2] = "witamy w be, wersja " VERSION, }, *lang_modes[] = { - [ModeNormal] = "Normalny", - [ModeEdit] = "Edycja", + [ModeNormal] = "Normalny", + [ModeEdit] = "Edycja", + [SubModeGlobal] = "Globalny", + [SubModeMovement] = "Ruch", + [SubModeBuffer] = "Bufor", }, *lang_err[] = { [0] = "u┼╝ycie",