summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--page.c8
-rw-r--r--point.c11
2 files changed, 13 insertions, 6 deletions
diff --git a/page.c b/page.c
index 1244df4..864623e 100644
--- a/page.c
+++ b/page.c
@@ -46,6 +46,13 @@ void split_page(struct page *back) {
back->next = front;
}
+void copy_page(struct page *dest, struct page *src) {
+ memcpy(dest->elements, src->elements, PAGE_SIZE);
+ dest->gap_start = src->gap_start;
+ dest->gap_end = src->gap_end;
+ dest->element_count = src->element_count;
+}
+
void free_page(struct page *page) {
if (page->prev) {
page->prev->next = page->next;
@@ -53,6 +60,7 @@ void free_page(struct page *page) {
if (page->next) {
page->next->prev = page->prev;
}
+ free(page->elements);
free(page);
}
diff --git a/point.c b/point.c
index 7977a4e..27d62be 100644
--- a/point.c
+++ b/point.c
@@ -35,8 +35,8 @@ void move_point_backward(struct point *point) {
if (point->index > 1) {
point->index--;
} else if (point->page->prev) {
- point->index = point->page->prev->element_count;
point->page = point->page->prev;
+ point->index = point->page->element_count;
} else {
point->index = 0;
}
@@ -65,15 +65,14 @@ void insert_at_point(struct point *point, uint8_t c) {
}
void delete_at_point(struct point *point) {
- if (point->page->element_count == 1) {
+ if (point->page->element_count == 1 && point->index == 1) {
if (point->page->prev) {
- point->page = point->page->prev;
- point->index = point->page->element_count;
+ move_point_backward(point);
free_page(point->page->next);
} else if (point->page->next) {
- point->page = point->page->next;
+ copy_page(point->page, point->page->next);
+ free_page(point->page->next);
point->index = 0;
- free_page(point->page->prev);
} else {
align_gap(point);
delete_at_gap(point->page);