diff options
author | cflip <cflip@cflip.net> | 2023-01-12 12:19:08 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-01-12 12:21:03 -0700 |
commit | 4c150aab138f5733d29ad4548d66764fe19ffc44 (patch) | |
tree | 6559f4cfafb7abb9a10b4a12be0ec79ee5b93df7 /input.c | |
parent | ab00e1e288743496e75c5e54c0e6abdecf439642 (diff) |
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.
Diffstat (limited to 'input.c')
-rw-r--r-- | input.c | 92 |
1 files changed, 43 insertions, 49 deletions
@@ -1,82 +1,76 @@ #include "input.h" -#include <stdlib.h> -#include <errno.h> -#include <unistd.h> - -#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; |