From a5de15292ad99490c124be8412905d36e5394c39 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 6 Oct 2024 19:11:58 -0400 Subject: [PATCH] Delete input on KEY_BACKSPACE --- src/v.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 16 deletions(-) diff --git a/src/v.c b/src/v.c index 6bbb16a..98e4130 100644 --- a/src/v.c +++ b/src/v.c @@ -1,4 +1,5 @@ -#include +#define NCURSES_WIDECHAR 1 +#include #include #include #include @@ -168,6 +169,7 @@ struct state }; const char *escaped_strings[] = { + "→", "to", "α", "alpha", "β", "beta", "Γ", "Gamma", @@ -267,7 +269,7 @@ const char *escaped_strings[] = { "𝟞", "66", "𝟟", "77", "𝟠", "88", - "𝟡", "99", + "𝟡", "99", }; GHashTable *escaped_table; @@ -303,7 +305,7 @@ struct rule * create_rule() { struct rule *rule; - + rule = malloc(sizeof(struct rule)); rule->name = NULL; rule->hypotheses = NULL; @@ -485,6 +487,7 @@ draw_judgment(WINDOW *w, int *y, int *x, bool virtual, bool workplace_mode, /* Γ */ set_ctp_and_pair(&ctp, &pair, caret, (*point)++); + prev_not_displayed = FALSE; if (judgment->generic_l) draw_wrap(w, y, x, v, TRUE, pair, judgment->generic_l); else if (workplace_mode) { @@ -635,7 +638,7 @@ draw_rule(WINDOW *w, int *y, int *x, bool virtual, bool workspace_mode, caret, &point, inputstr, rule->conclusion); set_ctp_and_pair(&ctp, &pair, caret, point++); draw_slot(w, y, &r, TRUE, FALSE, pair, ctp == ctp_on ? inputstr : rule->name); - + lw = c; if (!virtual) { *y = row + 1; *x = col; @@ -725,7 +728,7 @@ render(struct state *state) y = 0; x = 0; draw_rule(state->workspace_w, &y, &x, FALSE, TRUE, state->caret, state->input->str, state->rule); - + /* draw existing rules */ i = 0; g_hash_table_iter_init(&iter, state->rules); @@ -747,7 +750,7 @@ render(struct state *state) draw_rule(state->support_w, &y, &x, FALSE, FALSE, -1, NULL, rule); i = y + 2; l = l->next; - } + } /* draw echo area */ if (state->err) { @@ -779,7 +782,7 @@ render(struct state *state) move(2 * q + 1, 0); printw("support%*s", COLS - 7, ""); attroff(COLOR_PAIR(PAIR_WB)); - + refresh(); } @@ -788,7 +791,7 @@ handle_input(struct state *state, char key) { GSList *l; struct name *name; - + if (key == '\\') { state->escape = TRUE; state->status = 0; @@ -822,6 +825,43 @@ handle_input(struct state *state, char key) } } +void +handle_slot_backspace(struct state *state) +{ + switch (state->st) { + case st_a: + state->st = st_thesis; + state->judgment->kind = tk_unknown; + break; + case st_b: + break; + default: + } +} + +void +handle_input_backspace(struct state *state) +{ + if (*state->input->str == '\0') + handle_slot_backspace(state); + else + g_string_erase(state->input, 0, -1); +} + +void +handle_backspace(struct state *state) +{ + if (!state->escape) + handle_input_backspace(state); + else + { + if (*state->escaped_input->str == '\0') + state->escape = FALSE; + else + g_string_erase(state->escaped_input, 0, -1); + } +} + bool conclusion_p(struct rule *rule, struct judgment *judgment) { @@ -872,7 +912,7 @@ int validate_type_thesis(struct state *state) { struct name *name; - + if (*state->input->str == '\0') return 1; if (conclusion_p(state->rule, state->judgment)) { @@ -881,7 +921,7 @@ validate_type_thesis(struct state *state) if ((name = g_hash_table_lookup(state->vets, state->input->str))) { if (name->kind != nk_type) return 6; - + /* the name exists, is a type, and is local */ state->judgment->a = create_wrap(FALSE, name); return -1; /* TODO: check dependencies */ @@ -932,14 +972,15 @@ int main() { const char *k, *v; - char key, *str; + char *str; int i, j, q; struct state state; struct judgment *j_p; GSList *l; GHashTableIter iter; gpointer hkey, hvalue; - + wint_t key; + setlocale(LC_ALL, ""); initscr(); start_color(); @@ -965,7 +1006,7 @@ main() state.support_w = subwin(stdscr, q, COLS, q + 1, 0); state.echo_w = subwin(stdscr, 1, COLS, LINES - 1, 0); state.rules = g_hash_table_new_full(g_str_hash, g_str_equal, - free, (GDestroyNotify)free_rule); + NULL, (GDestroyNotify)free_rule); state.decls = g_hash_table_new(g_str_hash, g_str_equal); state.gctxs = g_hash_table_new(g_str_hash, g_str_equal); state.gtheses = g_hash_table_new(g_str_hash, g_str_equal); @@ -977,13 +1018,13 @@ main() state.escaped_input = g_string_new(NULL); state.escape = FALSE; state.err = 0; - + /* populate escaped strings table */ /* NOTE: `g_hash_table_insert' deletes keys and values if they already exist. This is not what we want for the value list. Thus, we will free the values manually before destroying the table. */ - escaped_table = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); + escaped_table = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); for (i = 0 ; i < sizeof(escaped_strings) / sizeof(char *) / 2 ; i++) { k = escaped_strings[2 * i + 1]; v = escaped_strings[2 * i + 0]; @@ -1002,7 +1043,7 @@ main() render(&state); } while(state.running) { - key = getch(); + get_wch(&key); if (key == KEY_ESC) { state.running = FALSE; break; @@ -1021,6 +1062,8 @@ main() g_slist_free(g_steal_pointer(&state.support)); } } + else if (key == KEY_BACKSPACE) + handle_backspace(&state); else handle_input(&state, key); break; @@ -1031,6 +1074,8 @@ main() state.st = st_ctx_dots; } } + else if (key == KEY_BACKSPACE) + handle_backspace(&state); else handle_input(&state, key); break; @@ -1054,6 +1099,8 @@ main() state.status = 0; } } + else if (key == KEY_BACKSPACE) + handle_backspace(&state); else handle_input(&state, key); break; @@ -1107,6 +1154,8 @@ main() state.err = -1; } } + else if (key == KEY_BACKSPACE) + handle_backspace(&state); else handle_input(&state, key); break; @@ -1120,6 +1169,8 @@ main() state.st = st_rule_name; } } + else if (key == KEY_BACKSPACE) + handle_backspace(&state); else handle_input(&state, key); break; @@ -1128,6 +1179,8 @@ main() state.caret += 1; state.st = st_rule_name; } + else if (key == KEY_BACKSPACE) + handle_backspace(&state); else handle_input(&state, key); break; -- 2.39.5