ref: 042c5ee134f92fdf7930eecff63d5edf4d25138c
parent: f1cc5bc056cb87c683a32df0e1d98c52ccf76484
author: phil9 <telephil9@gmail.com>
date: Tue Dec 28 02:28:25 EST 2021
implement insert and append commands pressing 'i' inserts a new byte before current selection pressing 'a' appends a new byte after current selection
--- a/a.h
+++ b/a.h
@@ -9,6 +9,8 @@
int readfile(Buffer*, char*);
int writefile(Buffer*, char*);
+int insert(Buffer*, int);
+int append(Buffer*, int);
/* COLORS */
enum
@@ -22,5 +24,4 @@
};
Image* cols[NCOLS];
-
void initcols(int);
--- a/buf.c
+++ b/buf.c
@@ -36,7 +36,8 @@
return 0;
}
-int writefile(Buffer *buf, char *filename)
+int
+writefile(Buffer *buf, char *filename)
{
int fd, n;
@@ -48,4 +49,26 @@
return -1;
close(fd);
return 0;
+}
+
+int
+insert(Buffer *buf, int index)
+{
+ if(buf->count == buf->size){
+ buf->size *= 1.5;
+ buf->data = realloc(buf->data, buf->size);
+ if(buf->data == nil)
+ return -1;
+ }
+ buf->count += 1;
+ memmove(&buf->data[index + 1], &buf->data[index], buf->count - index);
+ buf->data[index] = 0;
+ buf->data[buf->count] = 0;
+ return 1;
+}
+
+int
+append(Buffer *buf, int index)
+{
+ return insert(buf, index + 1);
}
--- a/vexed.c
+++ b/vexed.c
@@ -313,6 +313,19 @@
redraw();
}
break;
+ case 'i':
+ lastv = -1;
+ if(insert(&buf, sel) < 0)
+ sysfatal("insert: %r");
+ eresize();
+ break;
+ case 'a':
+ lastv = -1;
+ if(append(&buf, sel) < 0)
+ sysfatal("append: %r");
+ sel += 1;
+ eresize();
+ break;
default:
if(isxdigit(k)){
if(e - lastk < 2 && lastv > 0) {