commit 20115b04d28f23f0c02b5ef23e7357ca59f9cbf7
parent ed4cdcd4fac64fcf4582838a2885b723fa1662d6
Author: ssnf <ssnf@ssnf.xyz>
Date: Sun, 15 Aug 2021 21:39:58 +0000
file saving and file switching implemented
Diffstat:
M | config.def.h | | | 20 | +++++++++++--------- |
M | sim.c | | | 118 | ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
2 files changed, 83 insertions(+), 55 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -1,15 +1,17 @@
-Key normal[] = {
+Key keys[] = {
+ { '$', move, EndLine },
+ { '0', move, StartLine },
+ { 'G', move, Top },
+ { 'Q', quit },
+ { 'g', move, Bottom },
{ 'h', move, Left },
{ 'j', move, Down },
{ 'k', move, Up },
{ 'l', move, Right },
- { Ctrl + 'u', move, HalfUp },
- { Ctrl + 'd', move, HalfDown },
- { 'G', move, Top },
- { 'g', move, Bottom },
- { '0', move, StartLine },
- { '$', move, EndLine },
{ 'o', file_open },
- { 'q', file_close },
- { 'Q', quit }
+ { 'q', file_close , cur},
+ { 's', file_save , cur},
+ { Ctrl + 'd', move, HalfDown },
+ { Ctrl + 'u', move, HalfUp },
+ { Esc, escape }
};
diff --git a/sim.c b/sim.c
@@ -42,6 +42,7 @@ typedef struct {
String* s;
String* name;
Address dot;
+ uchar dirty;
} File;
typedef struct {
@@ -62,12 +63,13 @@ void* emalloc(ulong);
void* erealloc(void*, ulong);
static void blind_reader(Frame* fr, Posn p0);
static void blind_writer(ushort line, ushort offset, ushort top, ushort bot);
+static void escape(int c);
static void init();
static void file_close(int arg);
static void file_init(File* f);
static void file_load(char* name);
-static void file_open(int arg);
-static void file_save(File* f);
+static void file_open(int c);
+static void file_save(int arg);
static void fr_add(Frame* fr, Posn p0, Posn p1);
static void fr_close(Frame* fr);
static void fr_init(Frame* fr);
@@ -87,10 +89,10 @@ static void str_delete(String* p, Posn p0, Posn p1);
static void move(int);
static void quit(int);
-static Frame frame;
-static File* f;
-static File file[FILECOUNT];
+static Frame* fr, frame[FILECOUNT];
+static File* f, file[FILECOUNT];
static Window w;
+uchar cur;
#include "config.h"
@@ -112,7 +114,7 @@ die(char* fmt, ...)
if (!file[i].s->n)
continue;
str_adds(file[i].name, ".swap");
- file_save(&file[i]);
+ file_save(i);
fprintf(stderr, "file saved to %s\n", file[i].name->s);
}
*fmt = *(char*)0;
@@ -177,14 +179,26 @@ blind_writer(ushort line, ushort offset, ushort top, ushort bot)
{
ushort i, o;
- o = frame.n - 1;
+ o = fr->n - 1;
i = 0;
if (offset >= top)
i = offset - top + 1;
- if (frame.n - offset > bot)
+ if (fr->n - offset > bot)
o = offset + bot - 1;
printf(CSI "%uH", line - offset);
- fwrite(&f->s->s[frame.a[i].p0], frame.a[o].p1 - frame.a[i].p0, 1, stdout);
+ fwrite(&f->s->s[fr->a[i].p0], fr->a[o].p1 - fr->a[i].p0, 1, stdout);
+}
+
+static void
+escape(int c)
+{
+ c = fgetc(stdin) - 0x30;
+ if (c > 0 && c <= 8) {
+ --c;
+ f = &file[c];
+ fr = &frame[c];
+ cur = c;
+ }
}
static void
@@ -193,15 +207,23 @@ init()
uint i;
win_init();
- fr_init(&frame);
- for (i = 0; i < FILECOUNT; ++i)
+ for (i = 0; i < FILECOUNT; ++i) {
file_init(&file[i]);
+ fr_init(&frame[i]);
+ }
f = &file[0];
+ fr = &frame[0];
+ cur = 0;
}
static void
file_close(int arg)
{
+ if (file[arg].dirty) {
+ printf(CSI "%uH" EL CSI "31mSave %s?" CSI "0m [y/n]", w.wy/2, file[arg].name->s);
+ if (fgetc(stdin) != 'n')
+ file_save(arg);
+ }
str_zero(f->s);
str_zero(f->name);
f->dot.p0 = f->dot.p1 = 0;
@@ -214,7 +236,7 @@ file_init(File* f)
f->name = emalloc(sizeof(String));
str_init(f->s);
str_init(f->name);
- f->dot.p0 = f->dot.p1 = 0;
+ f->dirty = f->dot.p0 = f->dot.p1 = 0;
}
static void
@@ -232,6 +254,7 @@ file_load(char* name)
rewind(disk);
fread(f->s->s, f->s->n, 1, disk);
fclose(disk);
+ f->dot.p0 = f->dot.p1 = 0;
}
static void
@@ -262,13 +285,16 @@ file_open(int c)
}
static void
-file_save(File* f)
+file_save(int arg)
{
- FILE* disk;
+ FILE* f;
- disk = fopen(f->name->s, "w");
- fwrite(f->s->s, f->s->n, 1, disk);
- fclose(disk);
+ if (!file[arg].name->n)
+ return;
+ f = fopen(file[arg].name->s, "w");
+ fwrite(file[arg].s->s, file[arg].s->n, 1, f);
+ fclose(f);
+ file[arg].dirty = 0;
}
static void
@@ -316,7 +342,7 @@ fr_insure(Frame* fr, ushort n)
static void
fr_update()
{
- Frame fr;
+ Frame fr0;
Posn p0, p1;
ushort half;
@@ -325,38 +351,38 @@ fr_update()
printf(ED);
goto status;
}
- if (!frame.n || f->dot.p1 > frame.a[frame.n - 1].p1 || f->dot.p1 < frame.a[0].p0 || frame.n - frame.dot < half || frame.dot < half) {
- fr_zero(&frame);
+ if (!fr->n || f->dot.p1 > fr->a[fr->n - 1].p1 || f->dot.p1 < fr->a[0].p0 || fr->n - fr->dot < half || fr->dot < half) {
+ fr_zero(fr);
for (p0 = f->dot.p1; p0; --p0)
if (f->s->s[p0 - 1] == '\n')
break;
if ((p1 = p0)) {
- fr_init(&fr);
- for (--p1; p1 && frame.n < w.wy;) {
+ fr_init(&fr0);
+ for (--p1; p1 && fr->n < w.wy;) {
for (;p1 && f->s->s[p1 - 1] != '\n'; --p1);
- blind_reader(&fr, p1);
- if ((p1 = fr.a[0].p0))
+ blind_reader(&fr0, p1);
+ if ((p1 = fr0.a[0].p0))
--p1;
- fr_insert(&frame, &fr, 0);
- fr_zero(&fr);
+ fr_insert(fr, &fr0, 0);
+ fr_zero(&fr0);
}
- fr_close(&fr);
+ fr_close(&fr0);
}
- for (p1 = p0; p1 < f->s->n - 1 && frame.n < 2 * w.wy;) {
- blind_reader(&frame, p1);
- p1 = frame.a[frame.n - 1].p1 + 1;
+ for (p1 = p0; p1 < f->s->n - 1 && fr->n < 2 * w.wy;) {
+ blind_reader(fr, p1);
+ p1 = fr->a[fr->n - 1].p1 + 1;
}
- for (;f->dot.p1 > frame.a[frame.dot].p1; ++frame.dot);
+ for (;f->dot.p1 > fr->a[fr->dot].p1; ++fr->dot);
goto full_update;
}
- if (f->dot.p1 > frame.a[frame.dot].p1 || f->dot.p1 < frame.a[frame.dot].p0) {
- for (;f->dot.p1 > frame.a[frame.dot].p1; ++frame.dot);
- for (;f->dot.p1 < frame.a[frame.dot].p0; --frame.dot);
+ if (f->dot.p1 > fr->a[fr->dot].p1 || f->dot.p1 < fr->a[fr->dot].p0) {
+ for (;f->dot.p1 > fr->a[fr->dot].p1; ++fr->dot);
+ for (;f->dot.p1 < fr->a[fr->dot].p0; --fr->dot);
goto full_update;
}
status:
- printf(CSI "%uH" "%lu 0x%02x", w.wy, f->dot.p1, f->s->s[f->dot.p1]);
- for (p0 = frame.a[frame.dot].p0, p1 = 1; p0 < f->dot.p1; ++p0) {
+ printf(CSI "%uH" "%lu 0x%02x -%u- %s", w.wy, f->dot.p1, f->s->s[f->dot.p1], cur, f->name->s);
+ for (p0 = fr->a[fr->dot].p0, p1 = 1; p0 < f->dot.p1; ++p0) {
if (f->s->s[p0] == '\t' && p1 % 4) {
p1 += 5 - (p1 % 4);
} else ++p1;
@@ -365,7 +391,7 @@ fr_update()
return;
full_update:
printf(ED);
- blind_writer(half, frame.dot, half, half + (w.wy % 2));
+ blind_writer(half, fr->dot, half, half + (w.wy % 2));
goto status;
}
@@ -399,21 +425,21 @@ move(int arg)
f->dot.p0 = f->dot.p1;
break;
case HalfUp:
- if (frame.dot < w.wy/2)
+ if (fr->dot < w.wy/2)
f->dot.p0 = f->dot.p1 = 0;
else
- f->dot.p0 = f->dot.p1 = frame.a[frame.dot - w.wy/2].p0;
+ f->dot.p0 = f->dot.p1 = fr->a[fr->dot - w.wy/2].p0;
break;
case HalfDown:
if (!f->s->n)
break;
- if (frame.n - frame.dot <= w.wy/2) {
- if (frame.a[frame.n - 1].p1 <= f->s->n)
- f->dot.p0 = f->dot.p1 = frame.a[frame.n - 1].p0;
+ if (fr->n - fr->dot <= w.wy/2) {
+ if (fr->a[fr->n - 1].p1 <= f->s->n)
+ f->dot.p0 = f->dot.p1 = fr->a[fr->n - 1].p0;
else
f->dot.p0 = f->dot.p1 = f->s->n;
} else
- f->dot.p0 = f->dot.p1 = frame.a[frame.dot + w.wy/2].p0;
+ f->dot.p0 = f->dot.p1 = fr->a[fr->dot + w.wy/2].p0;
break;
case Top:
f->dot.p0 = f->dot.p1 = 0;
@@ -536,9 +562,9 @@ main(int argc, char* argv[])
win_query(&w);
fr_update();
c = fgetc(stdin);
- for (i = 0; i < LENGTH(normal); ++i) {
- if (normal[i].key == c) {
- normal[i].func(normal[i].value);
+ for (i = 0; i < LENGTH(keys); ++i) {
+ if (keys[i].key == c) {
+ keys[i].func(keys[i].value);
break;
}
}