ref: c40b9d21694a42dbf9cbcc25dd52ab4dd9751fcf
parent: 9bcd2b4ba984c92709794baa422695f873f8e828
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Feb 13 17:51:00 EST 2020
plan9: better handling of modes, select mode with '
--- a/plan9.c
+++ b/plan9.c
@@ -14,14 +14,18 @@
enum {
Txtoff = 16,
Coloff = 2,
- Msgredraw = 0,
- Chanchar = 0,
- Chankey,
- Chanmouse,
- Chanresize,
- Chanredraw,
+ Cchar = 0,
+ Ckey,
+ Cmouse,
+ Cresize,
+ Credraw,
Numchan,
+
+ Minsert = 0,
+ Mappend,
+ Mslide,
+ Mselect,
};
typedef struct Key Key;
@@ -39,7 +43,7 @@
static Rune *linebuf;
static Usz tick;
static int gridw = 8, gridh = 8;
-static int bpm = 120, insert = 1, pause;
+static int bpm = 120, pause;
static int curx, cury;
static int selw = 1, selh = 1;
static Image *curbg;
@@ -50,9 +54,17 @@
static char filename[256];
static Channel *cchan;
static Field copyfield;
-static int altdown, ctldown, slide;
+static int altdown;
+static int mode = Minsert;
static long framedev; /* frame deviation >= 1µs */
+static char *modes[] = {
+ [Minsert] = "insert",
+ [Mappend] = "append",
+ [Mslide] = "slide",
+ [Mselect] = "select",
+};
+
static char *menu3i[] = {
"load",
"save",
@@ -193,7 +205,7 @@
processnew = nanosec();
process(&events);
tick++;
- nbsendul(drawchan, Msgredraw);
+ nbsendul(drawchan, 0);
oldn = start;
end = start + 1;
@@ -275,7 +287,7 @@
/* insert/append mode */
p.y += font->height;
- len = MAX(6, runesprint(linebuf, "%s", (altdown || slide) ? "slide" : (insert ? "insert" : "append")));
+ len = MAX(6, runesprint(linebuf, "%s", modes[altdown ? Mslide : mode]));
runestring(screen, p, display->white, ZP, font, linebuf);
/* bpm */
@@ -384,7 +396,7 @@
static void
fieldset(Rune key)
{
- if (insert) {
+ if (mode == Minsert) {
selset(key);
} else {
field.buffer[curx + field.width*cury] = key;
@@ -525,11 +537,11 @@
Channel *kchan;
int oldw, oldh, w, h, n, shiftdown;
Alt a[Numchan+1] = {
- [Chanchar] = { nil, &key.rune, CHANRCV },
- [Chankey] = { nil, &key, CHANRCV },
- [Chanmouse] = { nil, &m, CHANRCV },
- [Chanresize] = { nil, nil, CHANRCV },
- [Chanredraw] = { nil, nil, CHANRCV },
+ [Cchar] = { nil, &key.rune, CHANRCV },
+ [Ckey] = { nil, &key, CHANRCV },
+ [Cmouse] = { nil, &m, CHANRCV },
+ [Cresize] = { nil, nil, CHANRCV },
+ [Credraw] = { nil, nil, CHANRCV },
{ nil, nil, CHANEND },
};
@@ -550,11 +562,11 @@
kctl.file = "/dev/null";
kctl.consfd = kctl.pid = kctl.ctlfd = -1;
- a[Chanchar].c = cchan;
- a[Chankey].c = kchan;
- a[Chanmouse].c = mctl->c;
- a[Chanresize].c = mctl->resizec;
- a[Chanredraw].c = chancreate(sizeof(ulong), 1);
+ a[Cchar].c = cchan;
+ a[Ckey].c = kchan;
+ a[Cmouse].c = mctl->c;
+ a[Cresize].c = mctl->resizec;
+ a[Credraw].c = chancreate(sizeof(ulong), 1);
curbg = allocimage(display, Rect(0, 0, 1, 1), RGBA32, 1, setalpha(DYellow, 128));
charw = stringwidth(font, "X");
@@ -570,7 +582,7 @@
mbuf_reusable_ensure_size(&mbuf, h, w);
oevent_list_init(&events);
- proccreate(orcathread, a[Chanredraw].c, mainstacksize);
+ proccreate(orcathread, a[Credraw].c, mainstacksize);
shiftdown = 0;
altdown = 0;
@@ -580,7 +592,7 @@
oldh = h = field.height;
switch (alt(a)) {
- case Chanmouse: /* mouse */
+ case Cmouse:
switch (m.buttons & 7) {
case 1:
select();
@@ -605,11 +617,11 @@
}
break;
- case Chanresize: /* resize */
+ case Cresize:
getwindow(display, Refnone);
break;
- case Chankey: /* key up/down */
+ case Ckey:
switch (key.rune) {
case Kshift:
shiftdown = key.down;
@@ -617,23 +629,20 @@
case Kalt:
altdown = key.down;
break;
- case Kctl:
- ctldown = key.down;
- break;
}
break;
- case Chanredraw: /* redraw */
+ case Credraw:
break;
- case Chanchar: /* key */
+ case Cchar:
switch (key.rune) {
case 0x0b: /* C-k */
case Kup:
- if (shiftdown)
+ if (shiftdown || mode == Mselect)
selh = MAX(1, selh-1);
else {
- if (altdown || slide)
+ if (altdown || mode == Mslide)
selmove(0, -1);
cury = MAX(0, cury-1);
}
@@ -640,10 +649,10 @@
break;
case '\n': /* C-j */
case Kdown:
- if (shiftdown)
+ if (shiftdown || mode == Mselect)
selh++;
else {
- if (altdown || slide)
+ if (altdown || mode == Mslide)
selmove(0, 1);
cury = MIN(h-1, cury+1);
}
@@ -650,10 +659,10 @@
break;
case Kbs: /* C-h */
case Kleft:
- if (shiftdown)
+ if (shiftdown || mode == Mselect)
selw = MAX(1, selw-1);
else {
- if (altdown || slide)
+ if (altdown || mode == Mslide)
selmove(-1, 0);
curx = MAX(0, curx-1);
}
@@ -664,10 +673,10 @@
break;
}
case Kright:
- if (shiftdown)
+ if (shiftdown || mode == Mselect)
selw++;
else {
- if (altdown || slide)
+ if (altdown || mode == Mslide)
selmove(1, 0);
curx = MIN(w-1, curx+1);
}
@@ -725,26 +734,27 @@
bpm = MAX(1, bpm-1);
break;
case Kins:
- insert = !insert;
+ mode = mode != Mappend ? Mappend : Minsert;
break;
case Kesc:
- if (slide)
- slide = 0;
- else if (!insert)
- insert = 1;
+ if (mode == Mslide || mode != Minsert)
+ mode = Minsert;
else
selw = selh = 1;
break;
case '`':
case '~':
- slide = !slide;
+ mode = mode != Mslide ? Mslide : Minsert;
break;
+ case '\'':
+ mode = mode != Mselect ? Mselect : Minsert;
+ break;
case Knack: /* C-u */
if (shiftdown)
selmap(toupper);
break;
case ' ':
- if (insert) {
+ if (mode == Minsert) {
pause = !pause;
break;
}