xource/xource.c

152 lines
5.7 KiB
C

/*
Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
Xource is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version.
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 <xolatile/xtandard.c>
#include <xolatile/xyntax.c>
#include <xolatile/xurses.c>
#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)
int main (int argc, char * * argv) {
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"
};
int cursor = 0;
int cursor_x = 0;
int cursor_y = 0;
if (argc == 2) {
file_list_import (argv [1]);
} else {
out ("ARGUMENTS\n", 10);
return (-1);
}
syntax_define_separators (".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n");
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);
syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_YELLOW, EFFECT_NORMAL);
syntax_define_words (keywords, sizeof (keywords) / sizeof (keywords [0]), COLOUR_YELLOW, EFFECT_BOLD);
syntax_define_default (1, COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
curses_configure ();
do {
int offset, select, length;
curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL);
length = 0;
curses_render_string (file_list_name [file_list_active], COLOUR_WHITE, EFFECT_REVERSE, length, 0);
length += string_length (file_list_name [file_list_active]);
curses_render_string (" -- ", COLOUR_WHITE, EFFECT_REVERSE, length, 0);
length += 4;
curses_render_string (number_to_string (file_list_size [file_list_active]), COLOUR_WHITE, EFFECT_REVERSE, length, 0);
length += string_length (number_to_string (file_list_size [file_list_active]));
for (offset = length; offset != curses_screen_width; ++offset) {
curses_render_string (" ", COLOUR_WHITE, EFFECT_REVERSE, offset, 0);
}
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", " 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");
*/
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') {
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 == 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 == 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 == ARROW_RIGHT) {
++cursor;
} else if (curses_character == ARROW_LEFT) {
--cursor;
} else if (character_is_visible (curses_character)) {
file_list_insert_character (curses_character, cursor);
++cursor;
} else {
continue;
}
limit (& cursor, 0, file_list_size [file_list_active] - 1);
} while (curses_active != 0);
file_list_delete ();
syntax_delete ();
return (EXIT_SUCCESS);
}