summaryrefslogtreecommitdiff
path: root/input.c
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-01-31 10:46:21 -0700
committercflip <cflip@cflip.net>2023-01-31 12:20:03 -0700
commit757efb85045408e2601da0f53547941a40d2e960 (patch)
tree0ab46da5222222971d3dd4bd7319262c6842be8a /input.c
parentc3da108c2d41853beb49a90a1f43a2a9a158c385 (diff)
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.
Diffstat (limited to 'input.c')
-rw-r--r--input.c32
1 files changed, 31 insertions, 1 deletions
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;
}
}