about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2022-03-12 19:56:56 -0500
committerJune McEnroe <june@causal.agency>2022-03-12 19:56:56 -0500
commit2123804abef4cc95cf587327380abbfbcc9c6fe0 (patch)
tree227e7ab7b655c5b6b7283a00bd8c97223ed36c7c
parentImplement r with count (diff)
downloadcatgirl-2123804abef4cc95cf587327380abbfbcc9c6fe0.tar.gz
catgirl-2123804abef4cc95cf587327380abbfbcc9c6fe0.zip
Use a bool for vi.mode
enums use 4 bytes even if they only have 2 values and we have 256
of these structs...
-rw-r--r--edit.c28
-rw-r--r--edit.h7
2 files changed, 19 insertions, 16 deletions
diff --git a/edit.c b/edit.c
index 370b9ee..67c35b9 100644
--- a/edit.c
+++ b/edit.c
@@ -198,6 +198,11 @@ int editInsert(struct Edit *e, wchar_t ch) {
 }
 
 enum {
+	Insert = false,
+	Command = true,
+};
+
+enum {
 	Esc = L'\33',
 	Erase = L'\177',
 	Kill = L'@' ^ L'U',
@@ -206,7 +211,7 @@ enum {
 };
 
 static void viEscape(struct Edit *e) {
-	e->vi.mode = EditViCommand;
+	e->vi.mode = Command;
 	e->vi.count = 0;
 	e->vi.verb = '\0';
 }
@@ -254,17 +259,18 @@ static int viCommand(struct Edit *e, wchar_t ch) {
 	switch (ch) {
 		break; case Esc: viEscape(e);
 		break; case L'0': e->pos = 0; viEscape(e);
-		break; case L'R': e->vi.verb = 'R'; e->vi.mode = EditViInsert;
-		break; case L'i': e->vi.verb = 'i'; e->vi.mode = EditViInsert;
-		break; case L'r': e->vi.verb = 'r'; e->vi.mode = EditViInsert;
+		break; case L'R': e->vi.verb = 'R'; e->vi.mode = Insert;
+		break; case L'i': e->vi.verb = 'i'; e->vi.mode = Insert;
+		break; case L'r': e->vi.verb = 'r'; e->vi.mode = Insert;
 	}
 	return 0;
 }
 
 int editVi(struct Edit *e, wchar_t ch) {
-	switch (e->vi.mode) {
-		break; case EditViInsert: return viInsert(e, ch);
-		break; case EditViCommand: return viCommand(e, ch);
+	if (e->vi.mode == Command) {
+		return viCommand(e, ch);
+	} else {
+		return viInsert(e, ch);
 	}
 }
 
@@ -274,7 +280,7 @@ int editVi(struct Edit *e, wchar_t ch) {
 #include <string.h>
 
 static void fix(struct Edit *e, const char *str) {
-	e->vi.mode = EditViInsert;
+	e->vi.mode = Insert;
 	assert(0 == editFn(e, EditClear));
 	for (const char *ch = str; *ch; ++ch) {
 		assert(0 == editInsert(e, (wchar_t)*ch));
@@ -376,12 +382,12 @@ int main(void) {
 	assert(eq(&e, "foo bar\0"));
 
 	fix(&e, "foo");
-	assert(e.vi.mode == EditViInsert);
+	assert(e.vi.mode == Insert);
 	editVi(&e, Esc);
-	assert(e.vi.mode == EditViCommand);
+	assert(e.vi.mode == Command);
 	assert(eq(&e, "fo\0o"));
 	editVi(&e, L'i');
-	assert(e.vi.mode == EditViInsert);
+	assert(e.vi.mode == Insert);
 	editVi(&e, L'b');
 	assert(eq(&e, "fob\0o"));
 	editVi(&e, Esc);
diff --git a/edit.h b/edit.h
index 18e9cd2..d94a028 100644
--- a/edit.h
+++ b/edit.h
@@ -35,13 +35,10 @@ struct Edit {
 	size_t cap;
 	struct Edit *cut;
 	struct {
-		enum EditViMode {
-			EditViInsert,
-			EditViCommand,
-		} mode;
-		unsigned count;
+		bool mode;
 		char verb;
 		bool lnext;
+		unsigned count;
 	} vi;
 };