ref: e69ffd89570593a69de5a3c708eec848e07278af
parent: 217c74a16cbcc2495149c56bf501868c4991c945
author: phil9 <telephil9@gmail.com>
date: Tue Dec 28 04:26:52 EST 2021
implement deletion pressing 'x' deletes the currently selected byte
--- a/a.h
+++ b/a.h
@@ -9,6 +9,7 @@
int readfile(Buffer*, char*);
int writefile(Buffer*, char*);
+int delete(Buffer*, int);
int insert(Buffer*, int);
int append(Buffer*, int);
--- a/buf.c
+++ b/buf.c
@@ -52,6 +52,20 @@
}
int
+delete(Buffer *buf, int index)
+{
+ if(index != buf->count - 1)
+ memmove(&buf->data[index], &buf->data[index + 1], buf->count - index);
+ buf->count -= 1;
+ /* TODO: is that really what we want ? */
+ if(buf->count == 0){
+ buf->count = 1;
+ buf->data[0] = 0;
+ }
+ return 0;
+}
+
+int
insert(Buffer *buf, int index)
{
if(buf->count == buf->size){
--- a/vexed.c
+++ b/vexed.c
@@ -315,11 +315,21 @@
redraw();
}
break;
+ case 'x':
+ case 'X':
+ if(delete(&buf, sel) < 0)
+ sysfatal("delete: %r");
+ if(sel == buf.count)
+ --sel;
+ modified = 1;
+ eresize();
+ break;
case 'i':
case 'I':
lastv = -1;
if(insert(&buf, sel) < 0)
sysfatal("insert: %r");
+ modified = 1;
eresize();
break;
case 'p':
@@ -328,6 +338,7 @@
if(append(&buf, sel) < 0)
sysfatal("append: %r");
sel += 1;
+ modified = 1;
eresize();
break;
default: