xource/xource.c

172 lines
7.5 KiB
C
Raw Permalink Normal View History

2023-09-21 01:12:01 -04:00
/*
2023-10-29 11:21:16 -04:00
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.
*/
2023-09-21 01:12:01 -04:00
#include <xolatile/xyntax.c>
#include <xolatile/xurses.c>
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"
};
2023-09-21 01:12:01 -04:00
2023-10-31 07:08:09 -04:00
int cursor = 0;
int cursor_x = 0;
int cursor_y = 0;
2023-09-21 01:12:01 -04:00
if (argc == 2) {
file_list_import (argv [1]);
} else {
out ("ARGUMENTS\n", 10);
return (-1);
}
2023-10-31 09:40:50 -04:00
syntax_define_separators (".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n");
2023-09-21 01:12:01 -04:00
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);
2023-09-21 01:12:01 -04:00
syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_YELLOW, EFFECT_NORMAL);
2023-09-21 01:12:01 -04:00
2023-11-01 09:06:36 -04:00
syntax_define_words (keywords, (int) (sizeof (keywords) / sizeof (keywords [0])), COLOUR_YELLOW, EFFECT_BOLD);
2023-09-21 01:12:01 -04:00
syntax_define_default (1, COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
2023-09-21 01:12:01 -04:00
curses_configure ();
2023-09-21 01:12:01 -04:00
do {
2023-11-03 08:26:14 -04:00
int offset, select, length, cursor_on_new_line;
2023-10-31 07:08:09 -04:00
curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL);
2023-09-21 01:12:01 -04:00
2023-11-04 21:16:51 -04:00
/* Work in progress, this will be factored out or removed. */
2023-10-31 09:40:50 -04:00
length = 0;
2023-11-01 09:06:36 -04:00
curses_render_string (file_list_name [file_list_active], COLOUR_WHITE, EFFECT_REVERSE, length, 0);
2023-10-31 09:40:50 -04:00
length += string_length (file_list_name [file_list_active]);
2023-11-03 08:26:14 -04:00
/* */
2023-11-04 21:16:51 -04:00
curses_render_string (" $ ", COLOUR_RED, EFFECT_REVERSE, length, 0);
2023-11-01 09:06:36 -04:00
length += 3;
2023-10-31 09:40:50 -04:00
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]));
2023-11-03 08:26:14 -04:00
/* */
2023-11-04 21:16:51 -04:00
curses_render_string (" O ", COLOUR_GREEN, EFFECT_REVERSE, length, 0);
2023-11-03 08:26:14 -04:00
length += 3;
curses_render_string (number_to_string (cursor), COLOUR_WHITE, EFFECT_REVERSE, length, 0);
length += string_length (number_to_string (cursor));
/* */
2023-11-04 21:16:51 -04:00
curses_render_string (" X ", COLOUR_YELLOW, EFFECT_REVERSE, length, 0);
2023-11-01 09:06:36 -04:00
length += 3;
curses_render_string (number_to_string (cursor_x), COLOUR_WHITE, EFFECT_REVERSE, length, 0);
length += string_length (number_to_string (cursor_x));
2023-11-03 08:26:14 -04:00
/* */
2023-11-04 21:16:51 -04:00
curses_render_string (" Y ", COLOUR_BLUE, EFFECT_REVERSE, length, 0);
2023-11-01 09:06:36 -04:00
length += 3;
curses_render_string (number_to_string (cursor_y), COLOUR_WHITE, EFFECT_REVERSE, length, 0);
length += string_length (number_to_string (cursor_y));
2023-11-04 21:16:51 -04:00
/* */
offset = character_count (file_list_data [file_list_active], CHARACTER_TAB_HORIZONTAL, cursor, 0, CHARACTER_LINE_FEED);
curses_render_string (" I ", COLOUR_CYAN, EFFECT_REVERSE, length, 0);
length += 3;
curses_render_string (number_to_string (offset), COLOUR_WHITE, EFFECT_REVERSE, length, 0);
length += string_length (number_to_string (offset));
/* */
curses_render_string (" C ", COLOUR_PINK, EFFECT_REVERSE, length, 0);
length += 3;
curses_render_character (file_list_data [file_list_active] [cursor], COLOUR_WHITE, EFFECT_REVERSE, length, 0);
length += 1;
2023-09-21 01:12:01 -04:00
2023-10-31 09:40:50 -04:00
for (offset = length; offset != curses_screen_width; ++offset) {
2023-10-31 07:08:09 -04:00
curses_render_string (" ", COLOUR_WHITE, EFFECT_REVERSE, offset, 0);
}
2023-09-21 01:12:01 -04:00
2023-10-31 07:08:09 -04:00
for (offset = 1; offset != curses_screen_height; ++offset) {
2023-11-04 21:16:51 -04:00
curses_render_string (string_realign (number_to_string (offset), 4, ' '), (offset == cursor_y + 1) ? COLOUR_GREEN : COLOUR_WHITE, EFFECT_REVERSE, 0, offset);
2023-10-31 07:08:09 -04:00
}
2023-11-03 08:26:14 -04:00
cursor_x = curses_realign_x = 5; /* Border offset... */
2023-10-31 07:08:09 -04:00
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);
}
}
2023-11-04 21:16:51 -04:00
cursor_on_new_line = (file_list_data [file_list_active] [cursor] == CHARACTER_LINE_FEED);
/*cursor_y = character_count (file_list_data [file_list_active], CHARACTER_LINE_FEED, 0, cursor + cursor_on_new_line, CHARACTER_NULL) - cursor_on_new_line;
cursor_x = character_count (file_list_data [file_list_active], CHARACTER_NULL, cursor - cursor_on_new_line, 0, CHARACTER_LINE_FEED) + cursor_on_new_line;*/
cursor_y = character_count (file_list_data [file_list_active], CHARACTER_LINE_FEED, 0, cursor, CHARACTER_NULL);
cursor_x = character_count (file_list_data [file_list_active], CHARACTER_NULL, cursor - cursor_on_new_line, 0, CHARACTER_LINE_FEED) + cursor_on_new_line;
2023-11-03 08:26:14 -04:00
2023-11-04 21:16:51 -04:00
cursor_x += character_count (file_list_data [file_list_active], CHARACTER_TAB_HORIZONTAL, cursor, 0, CHARACTER_LINE_FEED) * (curses_tab_width - 1);
if (cursor_on_new_line && (file_list_data [file_list_active] [cursor - 1] == CHARACTER_LINE_FEED)) {
cursor_x = 0;
}
2023-10-29 14:14:13 -04:00
/*
2023-11-04 21:16:51 -04:00
dump ("a.log", string_concatenate (number_to_string (curses_signal), "\n"));
2023-10-29 14:14:13 -04:00
*/
2023-11-03 08:26:14 -04:00
curses_render_cursor (cursor_x + curses_realign_x, cursor_y + curses_realign_y);
2023-10-31 07:08:09 -04:00
curses_synchronize ();
2023-11-04 21:16:51 -04:00
if (curses_character == (int) ('Q' & 0X1F)) {
2023-09-21 01:12:01 -04:00
curses_active = 0;
2023-11-01 09:06:36 -04:00
} else if (curses_character == (int) '\r') {
2023-10-31 07:08:09 -04:00
file_list_insert_character ('\n', cursor);
++cursor;
2023-11-04 21:16:51 -04:00
} else if (curses_character == (int) ('S' & 0X1F)) {
2023-09-21 01:12:01 -04:00
file_list_export (file_list_name [file_list_active]);
2023-11-04 21:16:51 -04:00
} else if (curses_character == SIGNAL_DELETE) {
2023-10-31 07:08:09 -04:00
file_list_remove_character (cursor);
--cursor;
2023-11-04 21:16:51 -04:00
} else if (curses_character == SIGNAL_ARROW_UP) {
cursor -= character_count (file_list_data [file_list_active], CHARACTER_NULL, cursor - cursor_on_new_line, 0, CHARACTER_LINE_FEED) - 1;
} else if (curses_character == SIGNAL_ARROW_DOWN) {
cursor += character_count (file_list_data [file_list_active], CHARACTER_NULL, cursor + cursor_on_new_line, file_list_size [file_list_active] - 1, CHARACTER_LINE_FEED) + 1;
} else if (curses_character == SIGNAL_ARROW_RIGHT) {
2023-10-29 14:14:13 -04:00
++cursor;
2023-11-04 21:16:51 -04:00
} else if (curses_character == SIGNAL_ARROW_LEFT) {
2023-10-29 14:14:13 -04:00
--cursor;
2023-11-01 09:06:36 -04:00
} else if (character_is_visible ((char) curses_character) != 0) {
file_list_insert_character ((char) curses_character, cursor);
2023-10-31 07:08:09 -04:00
++cursor;
2023-09-21 01:12:01 -04:00
} else {
continue;
}
2023-10-31 07:08:09 -04:00
limit (& cursor, 0, file_list_size [file_list_active] - 1);
2023-09-21 01:12:01 -04:00
} while (curses_active != 0);
file_list_delete ();
2023-09-21 01:12:01 -04:00
syntax_delete ();
2023-09-21 01:12:01 -04:00
return (EXIT_SUCCESS);
}