From 185bf5b364efb4d7885ff5adcb69204e892ff5f7 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Wed, 30 Dec 2020 22:27:24 -0500 Subject: config working! --- command.c | 10 +++- command.h | 14 ++++-- config.c | 14 +++--- main.c | 152 +++++++++---------------------------------------------------- ted-base.c | 11 +++-- ted.cfg | 28 ++++++++++++ 6 files changed, 79 insertions(+), 150 deletions(-) diff --git a/command.c b/command.c index 12bd1c8..4f1bb89 100644 --- a/command.c +++ b/command.c @@ -94,15 +94,21 @@ void command_execute(Ted *ted, Command c, i64 argument) { case CMD_DELETE: buffer_delete_chars_at_cursor(buffer, argument); break; - case CMD_BACKSPACE_WORDS: + case CMD_BACKSPACE_WORD: buffer_backspace_words_at_cursor(buffer, argument); break; - case CMD_DELETE_WORDS: + case CMD_DELETE_WORD: buffer_delete_words_at_cursor(buffer, argument); break; case CMD_SAVE: buffer_save(buffer); break; + case CMD_UNDO: + buffer_undo(buffer, argument); + break; + case CMD_REDO: + buffer_redo(buffer, argument); + break; } } diff --git a/command.h b/command.h index d4df03a..df6bf74 100644 --- a/command.h +++ b/command.h @@ -26,10 +26,12 @@ ENUM_U16 { // deletion CMD_BACKSPACE, CMD_DELETE, - CMD_BACKSPACE_WORDS, - CMD_DELETE_WORDS, + CMD_BACKSPACE_WORD, + CMD_DELETE_WORD, CMD_SAVE, + CMD_UNDO, + CMD_REDO, CMD_COUNT } ENUM_U16_END(Command); @@ -64,8 +66,10 @@ static CommandName const command_names[CMD_COUNT] = { {"select-end-of-file", CMD_SELECT_END_OF_FILE}, {"backspace", CMD_BACKSPACE}, {"delete", CMD_DELETE}, - {"backspace-words", CMD_BACKSPACE}, - {"delete-words", CMD_DELETE}, - {"save", CMD_SAVE} + {"backspace-word", CMD_BACKSPACE_WORD}, + {"delete-word", CMD_DELETE_WORD}, + {"save", CMD_SAVE}, + {"undo", CMD_UNDO}, + {"redo", CMD_REDO} }; diff --git a/config.c b/config.c index 3e2c1a1..259c85e 100644 --- a/config.c +++ b/config.c @@ -14,12 +14,14 @@ typedef enum { // all worth it for the -Wformat warnings #define config_err(cfg, ...) snprintf((cfg)->ted->error, sizeof (cfg)->ted->error - 1, "%s:%u: ", (cfg)->filename, (cfg)->line_number), \ - snprintf((cfg)->ted->error + strlen((cfg)->ted->error), strlen((cfg)->ted->error) - sizeof (cfg)->ted->error - 1, __VA_ARGS__) + snprintf((cfg)->ted->error + strlen((cfg)->ted->error), strlen((cfg)->ted->error) - sizeof (cfg)->ted->error - 1, __VA_ARGS__), \ + (cfg)->error = true typedef struct { Ted *ted; char const *filename; u32 line_number; // currently processing this line number + bool error; } ConfigReader; // Returns the key combination described by str. @@ -140,7 +142,8 @@ void config_read(Ted *ted, char const *filename) { ConfigReader cfg_reader = { .ted = ted, .filename = filename, - .line_number = 1 + .line_number = 1, + .error = false }; ConfigReader *cfg = &cfg_reader; FILE *fp = fopen(filename, "rb"); @@ -155,8 +158,6 @@ void config_read(Ted *ted, char const *filename) { if (newline || feof(fp)) { if (newline) *newline = '\0'; - bool error = false; - // ok, we've now read a line. switch (line[0]) { case '#': // comment @@ -167,10 +168,8 @@ void config_read(Ted *ted, char const *filename) { char *closing = strchr(line, ']'); if (!closing) { config_err(cfg, "Unmatched [. " SECTION_HEADER_HELP); - error = true; } else if (closing[1] != '\0') { config_err(cfg, "Text after section. " SECTION_HEADER_HELP); - error = true; } else { *closing = '\0'; char *section_name = line + 1; @@ -178,7 +177,6 @@ void config_read(Ted *ted, char const *filename) { section = SECTION_KEYBOARD; } else { config_err(cfg, "Unrecognized section: [%s].", section_name); - error = true; } } } break; @@ -245,7 +243,7 @@ void config_read(Ted *ted, char const *filename) { } break; } - if (error) break; + if (cfg->error) break; ++cfg->line_number; } else { diff --git a/main.c b/main.c index d526710..c941ca9 100644 --- a/main.c +++ b/main.c @@ -1,3 +1,4 @@ +// @TODO: page up, page down (buffer_page_up, buffer_page_down) #include "base.h" no_warn_start #if _WIN32 @@ -90,6 +91,7 @@ int main(void) { TextBuffer text_buffer; TextBuffer *buffer = &text_buffer; buffer_create(buffer, font); + ted->active_buffer = buffer; if (!buffer_load_file(buffer, "buffer.c")) die("Error loading file: %s", buffer_geterr(buffer)); @@ -109,6 +111,8 @@ int main(void) { bool shift = keyboard_state[SDL_SCANCODE_LSHIFT] || keyboard_state[SDL_SCANCODE_RSHIFT]; bool alt = keyboard_state[SDL_SCANCODE_LALT] || keyboard_state[SDL_SCANCODE_RALT]; + (void)ctrl; (void)shift; (void)alt; + while (SDL_PollEvent(&event)) { // @TODO: make a function to handle text buffer events switch (event.type) { @@ -131,138 +135,24 @@ int main(void) { } break; case SDL_KEYDOWN: { - SDL_Keycode keycode = event.key.keysym.sym; - switch (keycode) { - case SDLK_PAGEUP: - buffer_scroll(buffer, 0, -buffer_display_lines(buffer)); - break; - case SDLK_PAGEDOWN: - buffer_scroll(buffer, 0, +buffer_display_lines(buffer)); - break; - case SDLK_RIGHT: - if (!alt) { - if (shift) { - if (ctrl) - buffer_select_right_words(buffer, 1); - else - buffer_select_right(buffer, 1); - } else { - if (ctrl) - buffer_cursor_move_right_words(buffer, 1); - else - buffer_cursor_move_right(buffer, 1); - } - } - break; - case SDLK_LEFT: - if (!alt) { - if (shift) { - if (ctrl) - buffer_select_left_words(buffer, 1); - else - buffer_select_left(buffer, 1); - } else { - if (ctrl) - buffer_cursor_move_left_words(buffer, 1); - else - buffer_cursor_move_left(buffer, 1); - } - } - break; - case SDLK_UP: - if (!alt) { - if (shift) { - if (ctrl) - buffer_select_up(buffer, 10); - else - buffer_select_up(buffer, 1); - } else { - if (ctrl) - buffer_cursor_move_up(buffer, 10); - else - buffer_cursor_move_up(buffer, 1); - } - } - break; - case SDLK_DOWN: - if (!alt) { - if (shift) { - if (ctrl) - buffer_select_down(buffer, 10); - else - buffer_select_down(buffer, 1); - } else { - if (ctrl) - buffer_cursor_move_down(buffer, 10); - else - buffer_cursor_move_down(buffer, 1); - } - } - break; - case SDLK_RETURN: - buffer_insert_char_at_cursor(buffer, U'\n'); - break; - case SDLK_TAB: - buffer_insert_char_at_cursor(buffer, U'\t'); - break; - case SDLK_DELETE: - if (ctrl) - buffer_delete_words_at_cursor(buffer, 1); - else - buffer_delete_chars_at_cursor(buffer, 1); - break; - case SDLK_BACKSPACE: - if (ctrl) - buffer_backspace_words_at_cursor(buffer, 1); - else - buffer_backspace_at_cursor(buffer, 1); - break; - case SDLK_s: - if (ctrl) { - if (!buffer_save(buffer)) { - printf("Error saving: %s.", buffer_geterr(buffer)); - } - } - break; - case SDLK_z: - if (ctrl) { - if (shift) { - buffer_redo(buffer, 1); - } else { - buffer_undo(buffer, 1); - } - } - break; - case SDLK_HOME: - if (ctrl) { - if (shift) { - buffer_select_to_start_of_file(buffer); - } else { - buffer_cursor_move_to_start_of_file(buffer); - } - } else { - if (shift) { - buffer_select_to_start_of_line(buffer); - } else { - buffer_cursor_move_to_start_of_line(buffer); - } - } - break; - case SDLK_END: - if (ctrl) { - if (shift) { - buffer_select_to_end_of_file(buffer); - } else { - buffer_cursor_move_to_end_of_file(buffer); - } - } else { - if (shift) { - buffer_select_to_end_of_line(buffer); - } else { - buffer_cursor_move_to_end_of_line(buffer); - } + SDL_Scancode scancode = event.key.keysym.scancode; + SDL_Keymod modifier = event.key.keysym.mod; + u32 key_combo = (u32)scancode << 3 | + (u32)((modifier & (KMOD_LCTRL|KMOD_RCTRL)) != 0) << KEY_MODIFIER_CTRL_BIT | + (u32)((modifier & (KMOD_LSHIFT|KMOD_RSHIFT)) != 0) << KEY_MODIFIER_SHIFT_BIT | + (u32)((modifier & (KMOD_LALT|KMOD_RALT)) != 0) << KEY_MODIFIER_ALT_BIT; + if (key_combo < KEY_COMBO_COUNT) { + KeyAction *action = &ted->key_actions[key_combo]; + if (action->command) { + command_execute(ted, action->command, action->argument); + } else switch (event.key.keysym.sym) { + case SDLK_RETURN: + buffer_insert_char_at_cursor(buffer, U'\n'); + break; + case SDLK_TAB: + buffer_insert_char_at_cursor(buffer, U'\t'); + break; } - break; } } break; case SDL_TEXTINPUT: { diff --git a/ted-base.c b/ted-base.c index 34f8a03..1024404 100644 --- a/ted-base.c +++ b/ted-base.c @@ -1,15 +1,18 @@ typedef struct { u32 line_number; // config line number where this was set - Command command; + Command command; // this will be 0 (COMMAND_UNKNOWN) if there's no action for the key i64 argument; } KeyAction; #define SCANCODE_COUNT 0x120 // SDL scancodes should be less than this value. // a "key combo" is some subset of {control, shift, alt} + some key. #define KEY_COMBO_COUNT (SCANCODE_COUNT << 3) -#define KEY_MODIFIER_CTRL 1 -#define KEY_MODIFIER_SHIFT 2 -#define KEY_MODIFIER_ALT 4 +#define KEY_MODIFIER_CTRL_BIT 0 +#define KEY_MODIFIER_SHIFT_BIT 1 +#define KEY_MODIFIER_ALT_BIT 2 +#define KEY_MODIFIER_CTRL (1<