summary refs log tree commit diff
path: root/edit.c
diff options
context:
space:
mode:
authorC. McEnroe2020-02-09 07:09:51 -0500
committerC. McEnroe2020-02-09 07:09:56 -0500
commit2aa2005339750e64a587f6117ae21960e975e211 (patch)
tree93254aa5f14f6da95b0217738a82233d4b104986 /edit.c
parentcbc6ff2da722df93fdff4df7268128937d6dd9b0 (diff)
Add C-y
This is weechat's binding for it.
Diffstat (limited to 'edit.c')
-rw-r--r--edit.c26
1 files changed, 22 insertions, 4 deletions
diff --git a/edit.c b/edit.c
index 47478ec..16fa910 100644
--- a/edit.c
+++ b/edit.c
@@ -16,6 +16,7 @@
 
 #include <assert.h>
 #include <limits.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <wchar.h>
@@ -46,14 +47,24 @@ char *editBuffer(size_t *mbsPos) {
 	return mbs;
 }
 
-static void reserve(size_t index, size_t count) {
-	if (len + count > Cap) return;
+static struct {
+	wchar_t buf[Cap];
+	size_t len;
+} cut;
+
+static bool reserve(size_t index, size_t count) {
+	if (len + count > Cap) return false;
 	memmove(&buf[index + count], &buf[index], sizeof(*buf) * (len - index));
 	len += count;
+	return true;
 }
 
 static void delete(size_t index, size_t count) {
 	if (index + count > len) return;
+	if (count > 1) {
+		memcpy(cut.buf, &buf[index], sizeof(*buf) * count);
+		cut.len = count;
+	}
 	memmove(
 		&buf[index], &buf[index + count], sizeof(*buf) * (len - index - count)
 	);
@@ -163,10 +174,17 @@ void edit(size_t id, enum Edit op, wchar_t ch) {
 			while (word < len && buf[word] != L' ') word++;
 			delete(pos, word - pos);
 		}
+		break; case EditPaste: {
+			if (reserve(pos, cut.len)) {
+				memcpy(&buf[pos], cut.buf, sizeof(*buf) * cut.len);
+				pos += cut.len;
+			}
+		}
 
 		break; case EditInsert: {
-			reserve(pos, 1);
-			if (pos < Cap) buf[pos++] = ch;
+			if (reserve(pos, 1)) {
+				buf[pos++] = ch;
+			}
 		}
 		break; case EditComplete: {
 			tabComplete(id);