Files
CSPP-coursework/final/editor.c
T
2024-11-10 00:40:57 +00:00

276 lines
7.5 KiB
C

#include <stdbool.h>
#include <stdlib.h>
#include "dynstr.h"
#include <ncurses.h>
typedef struct {
uint32_t lines;
uint32_t buffer_line;
uint32_t buffer_col;
uint32_t y_offset;
uint32_t x_offset;
bool unsaved_changes;
bool editmode;
String_t* buffer;
} Editor;
void add_toolbar(Editor* self) {
int max_x, max_y;
getmaxyx(stdscr, max_y, max_x);
attron(COLOR_PAIR(2));
// add an entry for the current mode
move(max_y - 1, 0);
char mode[8];
snprintf(mode, 9, "[%6s]", self->editmode ? "Insert" : "Normal");
addstr(mode);
// add an entry for the current line and col
char line_and_col[24];
snprintf(line_and_col, 24, " [ln: %d, col: %d]", self->buffer_line + 1, self->buffer_col + 1);
addstr(line_and_col);
attroff(COLOR_PAIR(2));
// add an entry for unsaved changes
attron(COLOR_PAIR(1));
char unsaved_changes[20];
snprintf(unsaved_changes, 20, " [%s]", self->unsaved_changes ? "Unsaved Changes!" : "No Changes Yet! ");
addstr(unsaved_changes);
attroff(COLOR_PAIR(1));
}
#define min(x, y) ((x) < (y) ? (x) : (y))
void refresh_buffer(Editor* self) {
move(0, 0);
clear();
// get the screen size so we can figure out where to place characters
int max_x, max_y;
getmaxyx(stdscr, max_y, max_x);
for (size_t i = self->y_offset; i < self->lines && i < self->y_offset + max_y; i++) {
// adding the line number
attron(COLOR_PAIR(1));
char line_no[6];
snprintf(line_no, 6, "%-6d", i + 1);
addstr(line_no);
attroff(COLOR_PAIR(1));
// refreshing the text for the line
String_t line_segment = str_slice(
&self->buffer[i],
self->x_offset,
min(str_len(&self->buffer[i]), self->x_offset + max_x - 5)
);
addstr(to_chars(&line_segment));
str_dealloc(&line_segment);
addstr("\n");
}
add_toolbar(self);
move(self->buffer_line - self->y_offset, self->buffer_col + 5 - self->x_offset);
}
void move_cursor_on_screen(Editor* editor, int x, int y) {
add_toolbar(editor);
int newx = x + 5; // account for the line number
int max_x, max_y;
getmaxyx(stdscr, max_y, max_x);
bool flag = false;
while (newx + 3 > max_x + editor->x_offset) {
editor->x_offset += 1;
flag = true;
}
while (newx - 3 < editor->x_offset) {
editor->x_offset -= 1;
flag = true;
}
while (y + 3 > max_y + editor->y_offset) {
editor->y_offset += 1;
flag = true;
}
while (y - 3 < editor->y_offset) {
editor->y_offset -= 1;
flag = true;
}
if (flag) {
refresh_buffer(editor);
}
move(y - editor->y_offset, newx - editor->x_offset);
}
void switch_mode(Editor* self) {
self->editmode = !self->editmode;
refresh_buffer(self);
}
void newline(Editor* self) {
move(self->buffer_line, 0);
char line_no[6];
snprintf(line_no, 6, "%-6d", self->buffer_line + 1);
fprintf(stderr, "ln: %d, col: %d", self->buffer_line, self->buffer_col);
// add the line number
attron(COLOR_PAIR(1));
addstr(line_no);
attroff(COLOR_PAIR(1));
move_cursor_on_screen(self, self->buffer_col, self->buffer_line);
}
Editor editor_from(String_t input_string) {
Editor e;
e.lines = 0;
e.buffer_line = 0;
e.buffer_col = 0;
e.y_offset = 0;
e.x_offset = 0;
e.editmode = false;
e.unsaved_changes = false;
int linenum = 0;
e.buffer = str_lines(&input_string, &linenum);
e.lines = (size_t)linenum;
str_dealloc(&input_string);
refresh_buffer(&e);
return e;
}
Editor new_editor() {
return editor_from(str_new());
}
void move_cursor(Editor* self, int x, int y) {
if (y != 0
&& (int)(self->buffer_line) + y >= 0
&& self->buffer_line + y <= self->lines)
{
// move the cursor up or down
self->buffer_line += y;
int line_width = str_len(&self->buffer[self->buffer_line]);
if (self->buffer_col > line_width) {
self->buffer_col = line_width;
}
} else if ((int)(self->buffer_col) + x < 0) {
// moving the cursor off the left hand side of the line
if ((int)self->buffer_line > 0) {
self->buffer_line -= 1;
self->buffer_col = str_len(&self->buffer[self->buffer_line]);
}
} else if (self->buffer_col + x > str_len(&self->buffer[self->buffer_line])) {
// moving the cursor off the right hand side of the line
if (self->buffer_line < self->lines) {
self->buffer_col = 0;
self->buffer_line += 1;
}
} else if (x != 0) {
// moving the cursor left or right in any other case
self->buffer_col += x;
}
fprintf(stderr, "ln: %d, col: %d xoff: %d yoff: %d\n", self->buffer_line, self->buffer_col, self->x_offset, self->y_offset);
move_cursor_on_screen(self, self->buffer_col, self->buffer_line);
}
void delchar(Editor* self) {
self->unsaved_changes = true;
if (self->buffer_col == str_len(&self->buffer[self->buffer_line])) {
if (self->buffer_line +1 == self->lines) {
return;
}
self->buffer[self->buffer_line] = str_merge(&self->buffer[self->buffer_line], &self->buffer[self->buffer_line + 1]);
for (size_t i = self->buffer_line + 2; i < self->lines; i++) {
self->buffer[i - 1] = self->buffer[i];
}
self->buffer[self->lines - 1] = str_new();
self->lines--;
refresh_buffer(self);
} else {
// removes the character from the current line
str_remove(&self->buffer[self->buffer_line], self->buffer_col);
delch();
}
}
void pressed_enter(Editor* self) {
self->unsaved_changes = true;
// allocate memory immediately since we know a new line is being added
self->lines++;
self->buffer = realloc(self->buffer, sizeof(String_t) * (self->lines));
if (self->buffer_line == self->lines -1) {
// if at end of file add an empty line
self->buffer[self->lines - 1] = str_new();
} else {
// else shift each line downwards including everything past the cursor on the current line
for (size_t i = self->lines - 1; i > self->buffer_line + 1; i--) {
self->buffer[i] = self->buffer[i - 1];
}
self->buffer[self->buffer_line + 1] = str_slice(
&self->buffer[self->buffer_line],
self->buffer_col,
str_len(&self->buffer[self->buffer_line])
);
self->buffer[self->buffer_line] = str_slice(
&self->buffer[self->buffer_line],
0,
self->buffer_col
);
}
// refresh the screen with the new data
refresh_buffer(self);
move_cursor(self, 1, 0);
}
// inserts a character at the cursor
void addchar(Editor* self, char c) {
self->unsaved_changes = true;
if (self->buffer_line == self->lines) {
// if we are at the end of the file then we need to add a new line
newline(self);
// reallocate self->buffer to be 1 larger
self->lines++;
self->buffer = realloc(self->buffer, sizeof(String_t) * (self->lines));
// allocate the memory space for the new line and add it to the buffer
self->buffer[self->buffer_line] = str_new();
}
insch(c);
// insert the character into the string at the given index
str_insert(&self->buffer[self->buffer_line], self->buffer_col, c);
move_cursor(self, 1, 0);
}
String_t* to_string(Editor* self) {
}