diff options
-rw-r--r-- | editor.c | 22 | ||||
-rw-r--r-- | editor.h | 2 | ||||
-rw-r--r-- | input.c | 16 |
3 files changed, 29 insertions, 11 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; @@ -55,6 +55,8 @@ void editor_delete_char(struct editor_state* editor); void editor_add_line_above(struct editor_state* editor); void editor_add_line_below(struct editor_state* editor); +void editor_set_mode(struct editor_state *editor, enum editor_mode mode); + void editor_find(struct editor_state* editor); void editor_scroll(struct editor_state* editor); void editor_update_screen_size(struct editor_state *); @@ -33,7 +33,7 @@ void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) editor_insert_char(editor, '\t'); if (keysym->sym == SDLK_ESCAPE) - editor->mode = EDITOR_MODE_NORMAL; + editor_set_mode(editor, EDITOR_MODE_NORMAL); return; } @@ -45,7 +45,7 @@ void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) editor_run_command(editor); if (keysym->sym == SDLK_ESCAPE) - editor->mode = EDITOR_MODE_NORMAL; + editor_set_mode(editor, EDITOR_MODE_NORMAL); return; } @@ -85,30 +85,28 @@ void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) editor_delete_char(editor); break; case SDLK_i: - editor->mode = EDITOR_MODE_INSERT; - editor->pressed_insert_key = 1; + editor_set_mode(editor, EDITOR_MODE_INSERT); break; case SDLK_l: if (keysym->mod & KMOD_SHIFT) editor_move_end(editor); else editor_move_right(editor); - editor->mode = EDITOR_MODE_INSERT; - editor->pressed_insert_key = 1; + editor_set_mode(editor, EDITOR_MODE_INSERT); break; case SDLK_o: if (keysym->mod & KMOD_SHIFT) editor_add_line_above(editor); else editor_add_line_below(editor); - editor->mode = EDITOR_MODE_INSERT; - editor->pressed_insert_key = 1; + editor_set_mode(editor, EDITOR_MODE_INSERT); break; case SDLK_SLASH: editor_find(editor); break; case SDLK_SEMICOLON: - editor->mode = EDITOR_MODE_COMMAND; + if (keysym->mod & KMOD_SHIFT) + editor_set_mode(editor, EDITOR_MODE_COMMAND); break; } } |