commit b0add8f26a35c1362109a43d5be9983273ddc2a7 Author: Rockingcool Date: Sun Apr 16 20:14:20 2023 -0500 First commit, need to add more helper methods for buffer (e.g. backspace, insert character, etc.) diff --git a/editor.c b/editor.c new file mode 100644 index 0000000..cd19566 --- /dev/null +++ b/editor.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +#include + +typedef struct Buffer_struct Buffer; +struct Buffer_struct { + char* text; + char* start; + char* end; + int size; + int gap_size; +}; + +Buffer* new_buffer(int size) { + Buffer* buffer = malloc(sizeof(Buffer)); + buffer->size = size; + buffer->text = malloc(buffer->size); + + buffer->gap_size = size; + buffer->start = buffer->text; + buffer->end = buffer->start + buffer->gap_size; + return buffer; +} + +void buffer_grow(Buffer* buffer) { + int start_offset = buffer->start - buffer->text; + buffer->size *= 2; + buffer->text = realloc(buffer->text,buffer->size); + buffer->start = buffer->text + start_offset; + + buffer->gap_size = 10; + for (int i=strlen(buffer->text)-1; i > (buffer->start - buffer->text); i--) { + *(buffer->text + i + buffer->gap_size) = *(buffer->start + i); + *(buffer->start + i) = 0; + } + + buffer->end = buffer->start + buffer->gap_size; +} + +void buffer_insert(char ch, Buffer* buffer) { + *(buffer->start) = ch; + buffer->start++; + buffer->gap_size--; + if (buffer->gap_size == 0) { + buffer_grow(buffer); + } +} + +void buffer_right(Buffer* buffer) { + buffer->start++; + buffer->end--; +} + +void buffer_left(Buffer* buffer) { + buffer->start--; + buffer->end--; + + +} + +void init_curses() { + initscr(); + noecho(); + keypad(stdscr,TRUE); + cbreak(); +} + +void sigint_handler(int dummy) { + endwin(); + exit(130); +} + +int main() { + signal(SIGINT,sigint_handler); + + Buffer* buffer = new_buffer(10); + init_curses(); + int ch; + while (true) { + clear(); + + int i=0; + if (buffer->start != buffer->text) { /* We don't want to print the string, if the + gap starts at the first index of the string */ + + while (i < strlen(buffer->text)) { + addch(*(buffer->text + i)); + i++; + if ((buffer->start - buffer->text) == i) { /* If we have encountered + the start of the gap */ + i += buffer->gap_size; + } + } + } + + ch = getch(); + + switch(ch) { + case KEY_BACKSPACE: + if (buffer->start != buffer->text) { + buffer->start--; + buffer->gap_size++; + } + goto continue_while_loop; + default: + } + buffer_insert(ch,buffer); + + continue_while_loop: + + } + endwin(); +}