96 lines
2.3 KiB
C
96 lines
2.3 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_t* buffer;
|
|
} Editor;
|
|
|
|
Editor new_editor() {
|
|
Editor e;
|
|
e.lines = 0;
|
|
e.screen_line = 0;
|
|
e.screen_col = 0;
|
|
e.editmode = false;
|
|
|
|
String_t lines = str_from_chars("");
|
|
e.buffer = str_lines(&lines, &e.lines);
|
|
str_dealloc(&lines);
|
|
|
|
addstr("");
|
|
|
|
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;
|
|
|
|
String_t lines = str_from_chars(input_string);
|
|
e.buffer = str_lines(&lines, &e.lines);
|
|
str_dealloc(&lines);
|
|
|
|
addstr(input_string);
|
|
|
|
return e;
|
|
}
|
|
|
|
void move_cursor(Editor* self, int x, int y) {
|
|
if (y != 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_line -= 1;
|
|
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_t) * (self->lines + 1));
|
|
}
|
|
str_insert(&self->buffer[self->screen_line], self->screen_col, c);
|
|
move_cursor(self, 1, 0);
|
|
}
|
|
|
|
String_t* to_string(Editor* self) {
|
|
|
|
}
|
|
|