ref: 01272ec75cde8d296d2749ecd377af1e3bd5a9df
parent: 1aec5b522d5b0ea875254adf69fa64548d6fbbad
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun Mar 8 19:24:22 EDT 2020
plan9: add "inject" cmd; use tokenize(2) for command parsing
--- a/orca.man
+++ b/orca.man
@@ -254,6 +254,11 @@
Writes the text at the cursor position or at
.I X,Y
if specified.
+.TP
+.B inject FILE [X] [Y]
+Injects a file at the cursor position or at
+.I X,Y
+if specified
.PP
.I Orca
treats
--- a/plan9.c
+++ b/plan9.c
@@ -437,12 +437,14 @@
}
static void
-selpaste(void)
+inject(char *filename)
{
Biobuf *b;
- if ((b = Bopen("/dev/snarf", OREAD)) != nil)
+ if ((b = Bopen(filename, OREAD)) != nil) {
selpasteb(b);
+ Bterm(b);
+ }
}
static void
@@ -450,12 +452,30 @@
{
}
+static Point
+ptclamp(Point p)
+{
+ p.x = MAX(0, MIN((int)field.width-1, p.x));
+ p.y = MAX(0, MIN((int)field.height-1, p.y));
+ return p;
+}
+
static void
+curmove(int x, int y)
+{
+ Point xy;
+
+ xy = Pt(x, y);
+ cur = ptclamp(addpt(cur, xy));
+ sel.min = ptclamp(addpt(sel.min, xy));
+ sel.max = ptclamp(addpt(sel.max, xy));
+}
+
+static void
command(char *s, void (*snapshot)(void))
{
- char tmp[256];
- char *a, *b;
- int x, y;
+ char tmp[256], *argv[8];
+ int argc, x, y;
snprint(tmp, sizeof(tmp), "%s", s);
s = tmp;
@@ -474,8 +494,9 @@
return;
}
- if ((a = strpbrk(s, " \t")) != nil)
- *a++ = 0;
+ if ((argc = tokenize(tmp, argv, nelem(argv))) < 1)
+ return;
+ s = argv[0];
if (s[0] == 'p' && s[1] == 'l') /* play */
pause = false;
@@ -487,7 +508,7 @@
selcopy();
else if (s[0] == 'p' && s[1] == 'a') { /* paste */
snapshot();
- selpaste();
+ inject("/dev/snarf");
} else if (s[0] == 'e' && s[1] == 'r') { /* erase */
snapshot();
selset('.');
@@ -498,8 +519,12 @@
putchar('\n');
}
fflush(stdout);
- } else if (a != nil) {
- x = atoi(a);
+ } else if (strcmp(s, "undo") == 0) {
+ undo();
+ } else if (strcmp(s, "redo") == 0) {
+ redo();
+ } else if (argc > 1) {
+ x = atoi(argv[1]);
if (s[0] == 'b' && s[1] == 'p') /* bpm */
apm = bpm = MAX(1, x);
@@ -512,30 +537,33 @@
else if (s[0] == 'r' && s[1] == 'e') /* rewind */
tick = MAX(0, tick-x);
else if (s[0] == 'i' && s[1] == 'p') /* ip */
- netdial(a, nil);
+ netdial(argv[1], nil);
else if (s[0] == 'u' && s[1] == 'd') /* udp */
- netdial(nil, a);
+ netdial(nil, argv[1]);
else if (s[0] == 'm' && s[1] == 'i') /* midi */
- midiopen(a);
- else if (s[0] == 'w' && s[1] == 'r') { /* write:abcd;13;14 */
- b = a;
- x = cur.x;
- y = cur.y;
- if ((a = strpbrk(b, " \t")) != nil) {
- *a++ = 0;
- x = atoi(a);
- if ((a = strpbrk(a, " \t")) != nil)
- y = atoi(++a);
+ midiopen(argv[1]);
+ else if (s[0] == 'i' && s[1] == 'n' && argc > 1) {
+ snapshot();
+ if (argc > 2) {
+ cur.x = 0;
+ sel.min.x = 0;
+ curmove(atoi(argv[2]), 0);
}
- for (; *b != 0; x++, b++)
- fieldset(x, y, *b);
+ if (argc > 3) {
+ cur.y = 0;
+ sel.min.y = 0;
+ curmove(0, atoi(argv[3]));
+ }
+ inject(argv[1]);
+ } else if (s[0] == 'w' && s[1] == 'r') { /* write abcd 13 14 */
+ s = argv[1];
+ x = argc > 2 ? atoi(argv[2]) : cur.x;
+ y = argc > 3 ? atoi(argv[3]) : cur.y;
+ for (; *s != 0; x++, s++)
+ fieldset(x, y, *s);
}
- /* FIXME color, find, select, inject, write, time */
- } else if (strcmp(s, "undo") == 0) {
- undo();
- } else if (strcmp(s, "redo") == 0) {
- redo();
+ /* FIXME color, find, select, time */
}
}
@@ -972,26 +1000,7 @@
return 0;
}
-static Point
-ptclamp(Point p)
-{
- p.x = MAX(0, MIN((int)field.width-1, p.x));
- p.y = MAX(0, MIN((int)field.height-1, p.y));
- return p;
-}
-
static void
-curmove(int x, int y)
-{
- Point xy;
-
- xy = Pt(x, y);
- cur = ptclamp(addpt(cur, xy));
- sel.min = ptclamp(addpt(sel.min, xy));
- sel.max = ptclamp(addpt(sel.max, xy));
-}
-
-static void
selmove(int x, int y)
{
int i;
@@ -1398,7 +1407,7 @@
selcopy();
selset('.');
} else if (m.buttons == 5) { /* paste */
- selpaste();
+ inject("/dev/snarf");
} else if (m.buttons == 4) { /* menu */
menu3i[Menu3dotstyle] = styles[(dotstyle+1) % Numstyles].dot.menu;
menu3i[Menu3rulerstyle] = styles[(rulerstyle+1) % Numstyles].ruler.menu;
@@ -1529,7 +1538,7 @@
break;
case 0x16: /* C-v */
snapshot();
- selpaste();
+ inject("/dev/snarf");
break;
case '[':
rulers.x = MAX(4, rulers.x-1);