Files
CSPP-coursework/final/editor.c
T

83 lines
2.0 KiB
C

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "dynstr.h"
#include <ncurses.h>
typedef struct {
int lines;
int screen_line;
int screen_col;
bool editmode;
String* buffer;
} Editor;
Editor new_editor() {
Editor e;
e.lines = 0;
e.screen_line = 0;
e.screen_col = 0;
e.editmode = false;
e.buffer = NULL;
return e;
}
Editor editor_from(char* input_string) {
// TODO: fix this function
Editor e;
e.lines = 0;
e.screen_line = 0;
e.screen_col = 0;
e.editmode = false;
e.buffer = NULL;
return e;
}
void move_cursor(Editor* self, int x, int y) {
if (x != 0
&& self->screen_line + y >= 0
&& self->screen_line + y <= self->lines)
{
self->screen_line += y;
int line_width = str_len(&self->buffer[self->screen_line]);
if (self->screen_col > line_width) {
self->screen_col = line_width;
}
} else if (self->screen_col + x < 0) {
if (self->screen_line - 1 >= 0) {
self->screen_col = str_len(&self->buffer[self->screen_line]);
}
} else if (self->screen_col + x > str_len(&self->buffer[self->screen_line])) {
if (self->screen_line + 1 <= self->lines) {
self->screen_col = 0;
self->screen_line += 1;
}
} else if (x != 0) {
self->screen_col += x;
}
move(self->screen_line, self->screen_col);
}
void delchar(Editor* self) {
str_remove(&self->buffer[self->screen_line], self->screen_col);
delch();
}
void addchar(Editor* self, char c) {
insch(c);
// insert the character into the string at the given index
if (self->screen_line == self->lines) {
// reallocate self->buffer to be 1 larger
self->lines++;
self->buffer = realloc(self->buffer, sizeof(String) * (self->lines + 1));
}
str_insert(&self->buffer[self->screen_line], self->screen_col, c);
move_cursor(self, 1, 0);
}
String* to_string(Editor* self) {
}