diff --git a/xource.c b/xource.c index 18fad23..c736e95 100644 --- a/xource.c +++ b/xource.c @@ -10,54 +10,18 @@ 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 +}; + static int cursor = 0; static int cursor_x = 0; static int cursor_y = 0; -static void render_status_bar (int colour) { - char status_bar [] = " Xource : C = Xolatile's text editor"; +static int mode_active = MODE_INSERT; - int offset; - - curses_render_string (status_bar, colour, EFFECT_REVERSE, 0, 0); - - for (offset = string_length (status_bar); offset != curses_screen_width; ++offset) { - curses_render_string (" ", colour, EFFECT_REVERSE, offset, 0); - } - - for (offset = 1; offset != curses_screen_height; ++offset) { - curses_render_string (string_realign (number_to_string (offset), 4, ' '), colour, 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 (" ~", COLOUR_WHITE, EFFECT_REVERSE, 0, offset); - } - } -} - -static void append_character (char character) { +static void append_character (void) { int offset = 0; ++file_list_size [file_list_active]; @@ -68,7 +32,7 @@ static void append_character (char character) { file_list_data [file_list_active] [offset] = file_list_data [file_list_active] [offset - 1]; } - file_list_data [file_list_active] [cursor] = character; + file_list_data [file_list_active] [cursor] = curses_character; ++cursor; } @@ -99,6 +63,83 @@ static void cursor_limits (void) { } } +static void mode_none (void) { + return; +} + +static void mode_insert (void) { + int signal; + + for (signal = SIGNAL_A; signal != SIGNAL_9; ++signal) { + curses_bind (signal, append_character); + curses_bind (signal | SIGNAL_SHIFT, append_character); + } +} + +static void mode_command (void) { + return; +} + +static void mode_switch (int mode) { + curses_action = deallocate (curses_action); + curses_activator = deallocate (curses_activator); + + curses_action_count = 0; + + switch (mode) { + case MODE_INSERT: mode_insert (); break; + case MODE_COMMAND: mode_command (); break; + default: mode_none (); break; + } + + curses_bind (SIGNAL_ESCAPE, curses_exit); + + mode_active = mode; +} + +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 (" ~", COLOUR_WHITE, EFFECT_REVERSE, 0, offset); + } + } +} + int main (int argc, char * * argv) { char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n"; @@ -131,13 +172,12 @@ int main (int argc, char * * argv) { curses_configure (); - curses_bind (SIGNAL_Q, curses_exit); - curses_bind (SIGNAL_W | SIGNAL_SHIFT, curses_exit); + mode_switch (MODE_INSERT); do { curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL); - render_status_bar (COLOUR_WHITE); + render_status_bar (); render_source_code (); cursor_x = 8; @@ -150,7 +190,7 @@ int main (int argc, char * * argv) { if (curses_signal == ('Q' & 0X1F)) { curses_active = 0; } else if (curses_signal == '\r') { - append_character ('\n'); + append_character (); } else if (curses_signal == ('S' & 0X1F)) { file_list_export (file_list_name [file_list_active]); } else if (curses_signal == (char) 127) { @@ -185,7 +225,7 @@ int main (int argc, char * * argv) { cursor_limits (); } } else if ((curses_signal >= ' ') && (curses_signal <= '~')) { - append_character (curses_signal); + append_character (); cursor_limits (); } else { continue;