From 757efb85045408e2601da0f53547941a40d2e960 Mon Sep 17 00:00:00 2001 From: cflip Date: Tue, 31 Jan 2023 10:46:21 -0700 Subject: Start implementing editor commands This first step allows you to press the semicolon key to enter command mode, where you can type into a command line. Currently, pressing enter when in command mode will clear the command line but since there are no commands implemented it doesn't matter what you type in there. --- input.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'input.c') diff --git a/input.c b/input.c index 73889bd..36670e2 100644 --- a/input.c +++ b/input.c @@ -4,10 +4,25 @@ #include "file.h" #include "line.h" +void input_process_textinput(struct editor_state *editor, const char *text) +{ + /* Ignore the first letter after entering insert mode. */ + if (editor->pressed_insert_key) { + editor->pressed_insert_key = 0; + return; + } + + if (editor->mode == EDITOR_MODE_INSERT) { + editor_insert_char(editor, *text); + } else if (editor->mode == EDITOR_MODE_COMMAND) { + textbuf_append(&editor->cmdline, text, 1); + } +} + void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) { /* Handle keypresses for typing modes separately. */ - if (editor->mode != EDITOR_MODE_NORMAL) { + if (editor->mode == EDITOR_MODE_INSERT) { if (keysym->sym == SDLK_BACKSPACE) editor_delete_char(editor); @@ -22,6 +37,18 @@ void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) return; } + if (editor->mode == EDITOR_MODE_COMMAND) { + if (keysym->sym == SDLK_BACKSPACE) + textbuf_delete(&editor->cmdline); + + if (keysym->sym == SDLK_RETURN) + editor_run_command(editor); + + if (keysym->sym == SDLK_ESCAPE) + editor->mode = EDITOR_MODE_NORMAL; + return; + } + switch (keysym->sym) { /* TODO: Reimplement page up/down on Shift+W/S. */ case SDLK_w: @@ -80,5 +107,8 @@ void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) case SDLK_SLASH: editor_find(editor); break; + case SDLK_SEMICOLON: + editor->mode = EDITOR_MODE_COMMAND; + break; } } -- cgit v1.2.3