From 4c150aab138f5733d29ad4548d66764fe19ffc44 Mon Sep 17 00:00:00 2001 From: cflip Date: Thu, 12 Jan 2023 12:19:08 -0700 Subject: Make the editor modal and refactor keyboard events This makes the editor behave more like vi, with a seperate insert mode. This also refactors the keyboard input code to pass the SDL_Keysym structure to input.c and to use the SDL_TEXTINPUT event for editing text. SDL's text input event already fixes typing capital letters with shift, and should make it possible to enter text using an IME, however most unicode characters aren't properly rendered. --- input.c | 92 ++++++++++++++++++++++++++++++----------------------------------- 1 file changed, 43 insertions(+), 49 deletions(-) (limited to 'input.c') diff --git a/input.c b/input.c index 50191e7..7a18441 100644 --- a/input.c +++ b/input.c @@ -1,82 +1,76 @@ #include "input.h" -#include -#include -#include - -#include "file.h" +#include "editor.h" #include "row.h" -void editor_process_keypress(struct editor_state* editor, int c) +void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) { static int quit_message = 1; - switch (c) { + /* Handle keypresses for typing modes separately. */ + if (editor->mode != EDITOR_MODE_NORMAL) { + if (keysym->sym == SDLK_BACKSPACE) + editor_delete_char(editor); + + if (keysym->sym == SDLK_RETURN) + editor_insert_newline(editor); + + if (keysym->sym == SDLK_ESCAPE) + editor->mode = EDITOR_MODE_NORMAL; + return; + } + + switch (keysym->sym) { case '\r': editor_insert_newline(editor); break; - case CTRL_KEY('q'): - if (editor->dirty && quit_message) { - editor_set_status_message(editor, "This file has unsaved changes. Press Ctrl+Q again to quit"); - quit_message = 0; - return; + case SDLK_q: + if (keysym->mod & KMOD_CTRL) { + if (editor->dirty && quit_message) { + editor_set_status_message(editor, "This file has unsaved changes. Press Ctrl+Q again to quit"); + quit_message = 0; + return; + } + exit(0); } - exit(0); break; - case CTRL_KEY('s'): - editor_save(editor); + case SDLK_s: + if (keysym->mod & KMOD_CTRL) + editor_save(editor); break; - case HOME_KEY: + case SDLK_0: editor->cursor_x = 0; break; - case END_KEY: - if (editor->cursor_y < editor->row_count) + case SDLK_4: + if (keysym->mod & KMOD_SHIFT && editor->cursor_y < editor->row_count) editor->cursor_x = editor->rows[editor->cursor_y].size; break; - case CTRL_KEY('f'): + case SDLK_SLASH: editor_find(editor); break; - case BACKSPACE: - case DELETE_KEY: - if (c == DELETE_KEY) - editor_move_right(editor); + case SDLK_x: + editor_move_right(editor); editor_delete_char(editor); break; - case PAGE_UP: - case PAGE_DOWN: - { - if (c == PAGE_UP) { - editor->cursor_y = editor->row_offset; - } else { - editor->cursor_y = editor->row_offset + editor->screen_rows -1; - if (editor->cursor_y > editor->row_count) - editor->cursor_y = editor->row_count; - } - /* - * TODO: Reimplement pageup/pagedown, this time by scrolling the - * screen without necessarily changing the position of the - * cursor relative to the screen - */ - } + case SDLK_PAGEUP: + case SDLK_PAGEDOWN: + /* TODO: Reimplement page up & page down. */ break; - case ARROW_UP: + case SDLK_i: + editor->mode = EDITOR_MODE_INSERT; + break; + case SDLK_k: editor_move_up(editor); break; - case ARROW_DOWN: + case SDLK_j: editor_move_down(editor); break; - case ARROW_LEFT: + case SDLK_h: editor_move_left(editor); break; - case ARROW_RIGHT: + case SDLK_l: editor_move_right(editor); break; - case CTRL_KEY('l'): - case '\x1b': - break; - default: - editor_insert_char(editor, c); - break; } quit_message = 1; -- cgit v1.2.3