diff options
author | cflip <cflip@cflip.net> | 2023-01-31 12:30:50 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-01-31 12:30:50 -0700 |
commit | 16b949aa3147e3aaadb6888f574f6424d4c2d8f3 (patch) | |
tree | fa0dc9e332eb319e58186d334cfb843a2ee9ff2a /editor.c | |
parent | 757efb85045408e2601da0f53547941a40d2e960 (diff) |
Add a function to handle changing the editor mode
This way any extra stuff such as clearing the command line buffer or
setting the flag to ignore the extra first key can be automatically set.
Diffstat (limited to 'editor.c')
-rw-r--r-- | editor.c | 22 |
1 files changed, 20 insertions, 2 deletions
@@ -62,8 +62,7 @@ char* editor_prompt(struct editor_state* editor, char* prompt, void (*callback)( void editor_run_command(struct editor_state *editor) { /* TODO: Do something here */ - editor->mode = EDITOR_MODE_NORMAL; - textbuf_clear(&editor->cmdline); + editor_set_mode(editor, EDITOR_MODE_NORMAL); } void editor_try_quit(struct editor_state *editor) @@ -183,6 +182,25 @@ void editor_add_line_below(struct editor_state* editor) editor->cursor_x = 0; } +void editor_set_mode(struct editor_state *editor, enum editor_mode mode) +{ + enum editor_mode last_mode = editor->mode; + if (mode == last_mode) + return; + + /* Clear the command line if we are leaving command mode. */ + if (last_mode == EDITOR_MODE_COMMAND) { + textbuf_clear(&editor->cmdline); + } + + /* Ignore the extra first letter if we are entering a typing mode. */ + if (mode != EDITOR_MODE_NORMAL) { + editor->pressed_insert_key = 1; + } + + editor->mode = mode; +} + static void editor_find_callback(struct editor_state* editor, char* query, int key) { static int last_match = -1; |