From 58d38d02b215f2e4829ccdc399df8d59bb6cb140 Mon Sep 17 00:00:00 2001 From: cflip Date: Mon, 16 Jan 2023 13:36:25 -0700 Subject: Ignore first keypress after entering insert mode This fixes the problem where the key used to enter insert mode ('i' for example) would also be typed into the document. --- editor.h | 6 ++++++ input.c | 3 +++ window.c | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/editor.h b/editor.h index f38a7d4..0fe8bab 100644 --- a/editor.h +++ b/editor.h @@ -25,6 +25,12 @@ struct editor_state { time_t status_message_time; struct editor_syntax* syntax; int mode; + /* + * Keep track of whether a key that toggles insert mode has been pressed, so + * we can ignore it during the text input event. Otherwise, an 'i' or other + * letter will be inserted when entering insert mode. + */ + int pressed_insert_key; }; void init_editor(struct editor_state* editor); diff --git a/input.c b/input.c index 37f4c2e..74222bb 100644 --- a/input.c +++ b/input.c @@ -61,6 +61,7 @@ void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) break; case SDLK_i: editor->mode = EDITOR_MODE_INSERT; + editor->pressed_insert_key = 1; break; case SDLK_a: if (keysym->mod & KMOD_SHIFT) @@ -68,6 +69,7 @@ void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) else editor_move_right(editor); editor->mode = EDITOR_MODE_INSERT; + editor->pressed_insert_key = 1; break; case SDLK_o: if (keysym->mod & KMOD_SHIFT) @@ -75,6 +77,7 @@ void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) else editor_add_line_below(editor); editor->mode = EDITOR_MODE_INSERT; + editor->pressed_insert_key = 1; break; case SDLK_k: editor_move_up(editor); diff --git a/window.c b/window.c index 3ffd748..ee97ed9 100644 --- a/window.c +++ b/window.c @@ -49,6 +49,14 @@ int window_handle_event(struct editor_state *editor) case SDL_TEXTINPUT: if (editor->mode == EDITOR_MODE_NORMAL) break; + /* + * Ignore the first letter after entering insert mode, because it + * usually is just 'i'. + */ + if (editor->pressed_insert_key) { + editor->pressed_insert_key = 0; + break; + } editor_insert_char(editor, *e.text.text); break; } -- cgit v1.2.3