From 767d6efbfb09828248d03f8bb5e7c1905c2c202a Mon Sep 17 00:00:00 2001 From: xolatile Date: Sat, 28 Oct 2023 17:29:10 -0400 Subject: [PATCH] Unfinished, bumping to know where I left off... --- compile.sh | 2 +- xource.c | 262 ++++++++++++++++++++++--------------------------------------- 2 files changed, 96 insertions(+), 168 deletions(-) diff --git a/compile.sh b/compile.sh index 37dbe83..847ca15 100644 --- a/compile.sh +++ b/compile.sh @@ -2,6 +2,6 @@ set -xe -gcc -g -ansi -Wall -Wextra -Wpedantic -Werror -o xource xource.c +gcc -g -ansi -Wall -Wextra -Wpedantic -Werror -Ofast -o xource xource.c exit diff --git a/xource.c b/xource.c index 29220cc..922de2a 100644 --- a/xource.c +++ b/xource.c @@ -6,145 +6,118 @@ * It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3. */ -#include +#include #include -#include #include -extern int file_list_active; -extern int file_list_count; -extern int * file_list_mark; -extern int * file_list_size; -extern char * * file_list_name; -extern char * * file_list_data; +static int cursor = 0; -extern void file_list_import (char *); -extern void file_list_export (char *); +static void render_status_bar (int colour) { + char status_bar [] = "Xource : C = Xolatile's text editor"; -extern void append_character (char); -extern void remove_character (void); + int offset; -extern void cursor_limits (void); + curses_render_string_limit (status_bar, string_length (status_bar), COLOUR_WHITE, EFFECT_REVERSE, 0, 0); -int file_list_active = 0; -int file_list_count = 0; -int * file_list_mark = NULL; -int * file_list_size = NULL; -char * * file_list_name = NULL; -char * * file_list_data = NULL; - -void file_list_import (char * name) { - ++file_list_count; - - file_list_active = file_list_count - 1; - - file_list_mark = reallocate (file_list_mark, (int) sizeof (* file_list_mark) * file_list_count); - file_list_size = reallocate (file_list_size, (int) sizeof (* file_list_size) * file_list_count); - file_list_name = reallocate (file_list_name, (int) sizeof (* file_list_name) * file_list_count); - file_list_data = reallocate (file_list_data, (int) sizeof (* file_list_data) * file_list_count); - - file_list_mark [file_list_count - 1] = -1; - file_list_size [file_list_count - 1] = -1; - file_list_name [file_list_count - 1] = NULL; - file_list_data [file_list_count - 1] = NULL; - - file_list_name [file_list_count - 1] = allocate (string_length (name) + 1); - - (void) string_copy_limit (file_list_name [file_list_count - 1], name, string_length (name) + 1); - - file_list_mark [file_list_count - 1] = open (name, O_RDONLY); - - file_list_size [file_list_count - 1] = (int) lseek (file_list_mark [file_list_count - 1], 0, SEEK_END) + 1; - - (void) lseek (file_list_mark [file_list_count - 1], 0, SEEK_SET); - - file_list_data [file_list_count - 1] = allocate (file_list_size [file_list_count - 1]); - - (void) read (file_list_mark [file_list_count - 1], file_list_data [file_list_count - 1], (unsigned long int) (file_list_size [file_list_count - 1] - 1)); - - close (file_list_mark [file_list_count - 1]); - - file_list_data [file_list_count - 1] [file_list_size [file_list_count - 1] - 1] = '\0'; + for (offset = string_length (status_bar); offset != curses_screen_width; ++offset) { + curses_render_string_limit (" ", 1, colour, EFFECT_REVERSE, offset, 0); + } } -void file_list_export (char * name) { - file_list_mark [file_list_active] = open (name, O_WRONLY | O_CREAT | O_TRUNC); +static void render_source_code (void) { + int offset; - (void) write (file_list_mark [file_list_active], file_list_data [file_list_active], (unsigned long int) file_list_size [file_list_active]); + int index = 0; + int length = 0; + int line = 0; - close (file_list_mark [file_list_active]); + int cursor_x = 0; + int cursor_y = 1; + + for (offset = 0; file_list_data [file_list_active] [offset] != '\0'; offset += length) { + if (line >= curses_screen_height - 1) { + break; + } + + index = syntax_select (& file_list_data [file_list_active] [offset], & length); + + if (index >= syntax_count) { + curses_render_string_limit (& file_list_data [file_list_active] [offset], length, COLOUR_WHITE, EFFECT_NORMAL, cursor_x, cursor_y); + } else { + curses_render_string_limit (& file_list_data [file_list_active] [offset], length, syntax_colour [index], syntax_effect [index], cursor_x, cursor_y); + } + + if (file_list_data [file_list_active] [offset + length - 1] == '\n') { + cursor_x = 0; + ++cursor_y; + ++line; + } else if (file_list_data [file_list_active] [offset] == '\t') { + cursor_x += 8; + } else { + cursor_x += length; + } + } + + /*if (line <= curses_screen_height - 1) { + for (offset = 0; offset != curses_screen_height - 1 - line; ++offset) { + curses_render_character ('~', COLOUR_WHITE, EFFECT_REVERSE, 0, cursor_y); + ++cursor_y; + } + }*/ } -void append_character (char character) { +static void append_character (char character) { 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 != curses_cursor; --offset) { + 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] [curses_cursor] = character; + file_list_data [file_list_active] [cursor] = character; - ++curses_cursor; + ++cursor; } -void remove_character (void) { +static void remove_character (void) { int offset = 0; - if (curses_cursor == 0) { + if (cursor == 0) { return; } --file_list_size [file_list_active]; - for (offset = curses_cursor - 1; offset != file_list_size [file_list_active] - 1; ++offset) { + 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]; } - --curses_cursor; + --cursor; } -void cursor_limits (void) { - if (curses_cursor <= -1) { - curses_cursor = 0; +static void cursor_limits (void) { + if (cursor <= -1) { + cursor = 0; } - if (curses_cursor >= file_list_size [file_list_active]) { - curses_cursor = file_list_size [file_list_active] - 1; + if (cursor >= file_list_size [file_list_active]) { + cursor = file_list_size [file_list_active] - 1; } } int main (int argc, char * * argv) { - int word = 0; - int i = 0; + char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n"; - int preprocessor = 0; - int line_comment = 0; - int multiline_comment = 0; - int character = 0; - int string = 0; - int bracket = 0; - int operator = 0; - int keyword = 0; - int digit = 0; - int uppercase = 0; - int lowercase = 0; - int underscore = 0; - - char separator [29] = ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n"; - - char * c_keywords [32] = { - "register", "volatile", "auto", "const", "static", "extern", "if", "else", - "do", "while", "for", "continue", "switch", "case", "default", "break", - "enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof", - "char", "short", "int", "long", "signed", "unsigned", "float", "double" + char * keywords [] = { + "register", "volatile", "auto", "const", "static", "extern", "if", "else", + "do", "while", "for", "continue", "switch", "case", "default", "break", + "enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof", + "char", "short", "int", "long", "signed", "unsigned", "float", "double" }; - char status_bar [36] = "Xource : C = Xolatile's text editor"; - if (argc == 2) { file_list_import (argv [1]); } else { @@ -152,63 +125,28 @@ int main (int argc, char * * argv) { return (-1); } - curses_initialize (); + syntax_define_separators (separators); - syntax_define (& preprocessor, 0, 0, "#", "\n", '\\', COLOUR_RED, EFFECT_BOLD); - syntax_define (& line_comment, 0, 0, "//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD); - syntax_define (& multiline_comment, 0, 0, "/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD); - syntax_define (& character, 0, 0, "'", "'", '\\', COLOUR_PINK, EFFECT_BOLD); - syntax_define (& string, 0, 0, "\"", "\"", '\\', COLOUR_RED, EFFECT_BOLD); - syntax_define (& bracket, 1, 0, "()[]{}", "", '\0', COLOUR_GREEN, EFFECT_BOLD); - syntax_define (& operator, 1, 0, ".,:;<=>+-*/%!&~^?|", "", '\0', COLOUR_BLUE, EFFECT_BOLD); + syntax_define_range ("/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD); + syntax_define_range ("//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD); + syntax_define_range ("#", "\n", '\\', COLOUR_PINK, EFFECT_NORMAL); + syntax_define_range ("'", "'", '\\', COLOUR_PINK, EFFECT_BOLD); - for (word = 0; word != 32; ++word) { - syntax_define (& keyword, 0, 1, c_keywords [word], separator, '\0', COLOUR_YELLOW, EFFECT_BOLD); - } + syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_BLUE, EFFECT_BOLD); - syntax_define (& digit, 1, 1, "0123456789", separator, '\0', COLOUR_CYAN, EFFECT_BOLD); - syntax_define (& uppercase, 1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separator, '\0', COLOUR_PINK, EFFECT_ITALIC); - syntax_define (& lowercase, 1, 1, "abcdefghijklmnopqrstuvwxyz", separator, '\0', COLOUR_WHITE, EFFECT_ITALIC); - syntax_define (& underscore, 0, 1, "_", separator, '\0', COLOUR_YELLOW, EFFECT_ITALIC); + syntax_define_words (keywords, sizeof (keywords) / sizeof (keywords [0]), COLOUR_BLUE, EFFECT_NORMAL); + + syntax_define_default (1, COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD); + + curses_configure (); do { - int offset = 0; - int index = 0; - int length = 0; - int line = 0; + curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL); - curses_append_string (status_bar, EFFECT_REVERSE, COLOUR_WHITE, string_length (status_bar)); + render_status_bar (COLOUR_WHITE); + render_source_code (); - for (offset = 0; offset != curses_screen_width - string_length (status_bar); ++offset) { - curses_append_string (" ", EFFECT_REVERSE, COLOUR_WHITE, 1); - } - - curses_append_string ("\r\n", EFFECT_NORMAL, COLOUR_WHITE, 2); - - for (offset = 0; file_list_data [file_list_active] [offset] != '\0'; offset += length) { - if (line <= curses_screen_height - 1) { - break; - } - syntax_select (& file_list_data [file_list_active] [offset], & index, & length); - if (file_list_data [file_list_active] [offset] == '\n') { - curses_append_string ("\r\n", EFFECT_NORMAL, COLOUR_WHITE, 2); - ++line; - } else { - if (index >= syntax_count) { - curses_append_string (& file_list_data [file_list_active] [offset], EFFECT_NORMAL, COLOUR_WHITE, length); - } else { - curses_append_string (& file_list_data [file_list_active] [offset], syntax_effect [index], syntax_colour [index], length); - } - } - } - - if (line <= curses_screen_height - 2) { - for (offset = 0; offset != curses_screen_height - 2 - line; ++offset) { - curses_append_string ("~\r\n", EFFECT_REVERSE, COLOUR_WHITE, 3); - } - } - - curses_append_cursor (5, 3); + curses_render_cursor (5, 3); curses_synchronize (); @@ -226,26 +164,26 @@ int main (int argc, char * * argv) { in (& curses_signal, 1); if (curses_signal == 'A') { do { - --curses_cursor; + --cursor; } while ( - (curses_cursor >= 0) && - (file_list_data [file_list_active] [curses_cursor] != '\n') && - (file_list_data [file_list_active] [curses_cursor] != '\0') + (cursor >= 0) && + (file_list_data [file_list_active] [cursor] != '\n') && + (file_list_data [file_list_active] [cursor] != '\0') ); - --curses_cursor; + --cursor; } else if (curses_signal == 'B') { do { - ++curses_cursor; + ++cursor; } while ( - (curses_cursor <= file_list_size [file_list_active] - 1) && - (file_list_data [file_list_active] [curses_cursor] != '\n') && - (file_list_data [file_list_active] [curses_cursor] != '\0') + (cursor <= file_list_size [file_list_active] - 1) && + (file_list_data [file_list_active] [cursor] != '\n') && + (file_list_data [file_list_active] [cursor] != '\0') ); - ++curses_cursor; + ++cursor; } else if (curses_signal == 'C') { - ++curses_cursor; + ++cursor; } else if (curses_signal == 'D') { - --curses_cursor; + --cursor; } cursor_limits (); } @@ -257,19 +195,9 @@ int main (int argc, char * * argv) { } } while (curses_active != 0); + file_list_delete (); + syntax_delete (); - curses_deinitialize (); - - for (i = 0; i != file_list_count; ++i) { - file_list_name [i] = deallocate (file_list_name [i]); - file_list_data [i] = deallocate (file_list_data [i]); - } - - file_list_mark = deallocate (file_list_mark); - file_list_size = deallocate (file_list_size); - file_list_name = deallocate (file_list_name); - file_list_data = deallocate (file_list_data); - return (EXIT_SUCCESS); }