ref: 823cd957881a2b145faf53365bef958ec6ac192f
parent: 4bac4cbddf93e60475b5a51f8f28c2e1bb7ff207
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Feb 13 09:27:40 EST 2020
plan9: copy/cut/paste through snarf buffer
--- a/plan9.c
+++ b/plan9.c
@@ -2,6 +2,7 @@
#include "field.h"
#include "gbuffer.h"
#include "sim.h"
+#include <bio.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>
@@ -329,13 +330,55 @@
}
static void
-fieldset(Rune key)
+selset(Rune key)
{
int y;
+ for (y = cury; y < cury+selh && y < field.height; y++)
+ memset(&field.buffer[curx + field.width*y], key, selw);
+}
+
+static void
+selcopy(void)
+{
+ Biobuf *b;
+ int y;
+
+ if ((b = Bopen("/dev/snarf", OWRITE)) != nil) {
+ for (y = cury; y < cury+selh && y < field.height; y++) {
+ Bwrite(b, &field.buffer[curx + field.width*y], MIN(selw, field.width-curx));
+ Bputc(b, '\n');
+ }
+ Bterm(b);
+ }
+}
+
+static void
+selpaste(void)
+{
+ Biobuf *b;
+ char *s;
+ int cols, rows, n;
+
+ if ((b = Bopen("/dev/snarf", OREAD)) != nil) {
+ for (cols = rows = 0; (s = Brdstr(b, '\n', 1)) != nil; rows++) {
+ if ((n = Blinelen(b)) > cols)
+ cols = n;
+ if (cury+rows < field.height)
+ memmove(&field.buffer[curx + field.width*(cury+rows)], s, MIN(n, field.width-curx));
+ free(s);
+ }
+ selw = MAX(1, cols);
+ selh = MAX(1, rows);
+ Bterm(b);
+ }
+}
+
+static void
+fieldset(Rune key)
+{
if (insert) {
- for (y = cury; y < cury+selh && y < field.height; y++)
- memset(&field.buffer[curx + field.width*y], key, selw);
+ selset(key);
} else {
if (curx < field.width-1)
curx++;
@@ -620,6 +663,16 @@
case 0x12: /* C-r */
tick = 0;
break;
+ case 0x18: /* C-x */
+ selcopy();
+ selset('.');
+ break;
+ case Ketx: /* C-c */
+ selcopy();
+ break;
+ case 0x16: /* C-v */
+ selpaste();
+ break;
case '(':
w = MAX(1, w-gridw);
break;
@@ -648,7 +701,6 @@
insert = 1;
else
selw = selh = 1;
- /* FIXME else remove selection */
break;
case '`':
slide = !slide;