From 048dc14e6582945adffdbf530047a75b3396236d Mon Sep 17 00:00:00 2001 From: xolatile Date: Tue, 31 Oct 2023 07:08:09 -0400 Subject: [PATCH] A lot of simplifications... 150 SLOC. --- xource.c | 230 ++++++++++++++++++++------------------------------------------- 1 file changed, 71 insertions(+), 159 deletions(-) diff --git a/xource.c b/xource.c index 7aa3880..aeb2a46 100644 --- a/xource.c +++ b/xource.c @@ -10,148 +10,14 @@ It is distributed in the hope that it will be useful or harmful, it really depen #include #include -enum { - MODE_NONE, MODE_INSERT, MODE_COMMAND, - MODE_COUNT -}; +#define CONTROL (0X1F) +#define ESCAPE (0X1B) +#define ARROW_UP (0X415B1B) +#define ARROW_DOWN (0X425B1B) +#define ARROW_RIGHT (0X435B1B) +#define ARROW_LEFT (0X445B1B) +#define BACKSPACE (0X7F) -static int cursor = 0; -static int cursor_x = 0; -static int cursor_y = 0; - -static int mode_active = MODE_INSERT; - -static void append_character (void) { - int offset = 0; - - ++file_list_size [file_list_active]; - - file_list_data [file_list_active] = reallocate (file_list_data [file_list_active], file_list_size [file_list_active]); - - for (offset = file_list_size [file_list_active] - 1; offset != cursor; --offset) { - file_list_data [file_list_active] [offset] = file_list_data [file_list_active] [offset - 1]; - } - - file_list_data [file_list_active] [cursor] = curses_character; - - ++cursor; -} - -static void remove_character (void) { - int offset = 0; - - if (cursor == 0) { - return; - } - - --file_list_size [file_list_active]; - - for (offset = cursor - 1; offset != file_list_size [file_list_active] - 1; ++offset) { - file_list_data [file_list_active] [offset] = file_list_data [file_list_active] [offset + 1]; - } - - --cursor; -} - -static void cursor_limits (void) { - if (cursor <= -1) { - cursor = 0; - } - - if (cursor >= file_list_size [file_list_active]) { - cursor = file_list_size [file_list_active] - 1; - } -} - -static void mode_switch (void); - -static void mode_none (void) { - curses_bind (SIGNAL_ESCAPE, mode_switch); -} - -static void mode_insert (void) { - int signal; - - for (signal = SIGNAL_A; signal != SIGNAL_COUNT; ++signal) { - if ((signal == SIGNAL_ESCAPE) || (signal == SIGNAL_BACKSPACE) || (signal == SIGNAL_CAPS_LOCK)) { - continue; - } - - curses_bind (signal, append_character); - curses_bind (signal | SIGNAL_SHIFT, append_character); - } - - curses_bind (SIGNAL_ESCAPE, mode_switch); - curses_bind (SIGNAL_BACKSPACE, remove_character); -} - -static void mode_command (void) { - curses_bind (SIGNAL_ESCAPE, mode_switch); -} - -static void mode_switch (void) { - curses_action = deallocate (curses_action); - curses_activator = deallocate (curses_activator); - - curses_action_count = 0; - - mode_active += 1; - mode_active %= MODE_COUNT; - - switch (mode_active) { - case MODE_INSERT: mode_insert (); break; - case MODE_COMMAND: mode_command (); break; - default: mode_none (); break; - } - - curses_bind (SIGNAL_ESCAPE | SIGNAL_SHIFT, curses_exit); - - mode_active = mode_active; -} - -static void render_status_bar (void) { - char status_bar [] = " Xource : C = Xolatile's text editor"; - - int offset; - - curses_render_string (status_bar, mode_active, EFFECT_REVERSE, 0, 0); - - for (offset = string_length (status_bar); offset != curses_screen_width; ++offset) { - curses_render_string (" ", mode_active, EFFECT_REVERSE, offset, 0); - } - - for (offset = 1; offset != curses_screen_height; ++offset) { - curses_render_string (string_realign (number_to_string (offset), 4, ' '), mode_active, EFFECT_REVERSE, 0, offset); - } -} - -static void render_source_code (void) { - int offset, select, length; - - cursor_x = curses_realign_x = 5; - cursor_y = curses_realign_y = 1; - - for (offset = 0; file_list_data [file_list_active] [offset] != '\0'; offset += length) { - if (cursor_y > curses_screen_height - 2) { - break; - } - - select = syntax_select (& file_list_data [file_list_active] [offset], & length); - - if (select >= syntax_count) { - curses_render_string_point (& file_list_data [file_list_active] [offset], length, COLOUR_WHITE, EFFECT_NORMAL, & cursor_x, & cursor_y); - } else { - curses_render_string_point (& file_list_data [file_list_active] [offset], length, syntax_colour [select], syntax_effect [select], & cursor_x, & cursor_y); - } - } - - if (cursor_y < curses_screen_height) { - for (offset = ++cursor_y; offset != curses_screen_height; ++offset) { - curses_render_string (" ~", mode_active, EFFECT_REVERSE, 0, offset); - } - } -} -#include int main (int argc, char * * argv) { char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n"; @@ -162,6 +28,10 @@ int main (int argc, char * * argv) { "char", "short", "int", "long", "signed", "unsigned", "float", "double" }; + int cursor = 0; + int cursor_x = 0; + int cursor_y = 0; + if (argc == 2) { file_list_import (argv [1]); } else { @@ -184,49 +54,91 @@ int main (int argc, char * * argv) { curses_configure (); - /*mode_switch ();*/ - do { + char status_bar [] = " Xource : C = Xolatile's text editor"; + + int offset, select, length; + curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL); - render_status_bar (); - render_source_code (); + curses_render_string (status_bar, COLOUR_WHITE, EFFECT_REVERSE, 0, 0); - curses_render_cursor (cursor_x, cursor_y); + for (offset = string_length (status_bar); offset != curses_screen_width; ++offset) { + curses_render_string (" ", COLOUR_WHITE, EFFECT_REVERSE, offset, 0); + } - curses_synchronize (); + for (offset = 1; offset != curses_screen_height; ++offset) { + curses_render_string (string_realign (number_to_string (offset), 4, ' '), COLOUR_WHITE, EFFECT_REVERSE, 0, offset); + } + + cursor_x = curses_realign_x = 5; + cursor_y = curses_realign_y = 1; + + for (offset = 0; file_list_data [file_list_active] [offset] != '\0'; offset += length) { + if (cursor_y > curses_screen_height - 2) { + break; + } + + select = syntax_select (& file_list_data [file_list_active] [offset], & length); + + if (select >= syntax_count) { + curses_render_string_point (& file_list_data [file_list_active] [offset], length, COLOUR_WHITE, EFFECT_NORMAL, & cursor_x, & cursor_y); + } else { + curses_render_string_point (& file_list_data [file_list_active] [offset], length, syntax_colour [select], syntax_effect [select], & cursor_x, & cursor_y); + } + } + + if (cursor_y < curses_screen_height) { + for (offset = ++cursor_y; offset != curses_screen_height; ++offset) { + curses_render_string (" ~", COLOUR_WHITE, EFFECT_REVERSE, 0, offset); + } + } + + cursor_y = character_count (file_list_data [file_list_active], 0, cursor, '\n', '\0'); + cursor_x = character_count (file_list_data [file_list_active], cursor, 0, '\0', '\n'); /* - dump ("a.log", number_to_string (curses_character)); + dump ("a.log", " xx "); + dump ("a.log", number_to_string (cursor_x)); + dump ("a.log", " yy "); + dump ("a.log", number_to_string (cursor_y)); dump ("a.log", "\n"); */ - if (curses_character == ('Q' & 0X1F)) { + curses_render_cursor (cursor_x + curses_realign_x + 1, cursor_y + curses_realign_y + 1); + + curses_synchronize (); + + if (curses_character == ('Q' & CONTROL)) { curses_active = 0; } else if (curses_character == '\r') { - append_character (); - } else if (curses_character == ('S' & 0X1F)) { + file_list_insert_character ('\n', cursor); + ++cursor; + } else if (curses_character == ('S' & CONTROL)) { file_list_export (file_list_name [file_list_active]); - } else if (curses_character == (char) 127) { - remove_character (); - } else if (curses_character == 0X415B1B) { + } else if (curses_character == BACKSPACE) { + file_list_remove_character (cursor); + --cursor; + } else if (curses_character == ARROW_UP) { do { --cursor; } while ((cursor >= 0) && (file_list_data [file_list_active] [cursor] != '\n') && (file_list_data [file_list_active] [cursor] != '\0')); --cursor; - } else if (curses_character == 0X425B1B) { + } else if (curses_character == ARROW_DOWN) { do { ++cursor; } while ((cursor <= file_list_size [file_list_active] - 1) && (file_list_data [file_list_active] [cursor] != '\n') && (file_list_data [file_list_active] [cursor] != '\0')); ++cursor; - } else if (curses_character == 0X435B1B) { + } else if (curses_character == ARROW_RIGHT) { ++cursor; - } else if (curses_character == 0X445B1B) { + } else if (curses_character == ARROW_LEFT) { --cursor; } else if ((curses_character >= ' ') && (curses_character <= '~')) { - append_character (); + file_list_insert_character (curses_character, cursor); + ++cursor; } else { continue; } - cursor_limits (); + + limit (& cursor, 0, file_list_size [file_list_active] - 1); } while (curses_active != 0); file_list_delete ();