commit 72c781bf356dddf0dffc5363a4ba46920a481dfe
parent 7fb5f9bd6648277484a8d68e2fcc6b9398065cd8
Author: ssnf <ssnf@ssnf.xyz>
Date: Sat, 21 Aug 2021 21:23:50 +0000
added hacky search command
Diffstat:
M | config.def.h | | | 3 | +++ |
M | sim.c | | | 53 | ++++++++++++++++++++++++++++++++++++++++++++++++----- |
2 files changed, 51 insertions(+), 5 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -28,6 +28,9 @@ Key keys[] = {
{ 'q', file_close, -1 },
{ 'S', file_save, -1 },
{ 'Q', quit },
+ { '/', search, '/' },
+ { 'n', search, 'n' },
+ { 'N', search, 'N' },
{ Esc, escape }
};
diff --git a/sim.c b/sim.c
@@ -70,7 +70,7 @@ static void change(int arg);
static void delete(int arg);
static void escape(int c);
static void init();
-static void input(String* s, char* msg);
+static void input(String* s, ushort line, char* msg);
static void insert(int arg);
static int isword(uchar c);
static void file_close(int arg);
@@ -85,6 +85,7 @@ static void fr_insert(Frame* p, Frame* q, ushort n);
static void fr_insure(Frame* fr, ushort n);
static void fr_update(int arg);
static void fr_zero(Frame*);
+static void search(int arg);
static void str_init(String* p);
static void str_close(String* p);
static void str_zero(String* p);
@@ -99,6 +100,7 @@ static void quit(int);
static Frame* fr, frame[FILECOUNT];
static File* f, file[FILECOUNT];
+static String srch;
static Window w;
#include "config.h"
@@ -298,12 +300,12 @@ escape(int c)
}
static void
-input(String* s, char* msg)
+input(String* s, ushort line, char* msg)
{
uchar c;
for (;;) {
- printf(CSI "%uH" EL "%s%s", w.wy/2, msg, s->s);
+ printf(CSI "%uH" EL "%s%s", line, msg, s->s);
switch (c = fgetc(stdin)) {
case Esc:
str_zero(s);
@@ -407,6 +409,7 @@ init()
}
f = &file[0];
fr = &frame[0];
+ str_init(&srch);
}
static void
@@ -459,7 +462,7 @@ file_open(int c)
if (fgetc(stdin) == 'y')
file_save(f - file);
}
- input(f->name, CSI "32m$ " CSI "m");
+ input(f->name, w.wy/2, CSI "32m$ " CSI "m");
file_load(f);
}
@@ -471,7 +474,7 @@ file_save(int arg)
if (arg == -1)
arg = f - file;
if (!file[arg].name->n)
- input(file[arg].name, "File name: ");
+ input(file[arg].name, w.wy/2, "File name: ");
disk = fopen(file[arg].name->s, "w");
fwrite(file[arg].s->s, file[arg].s->n, 1, disk);
fclose(disk);
@@ -672,6 +675,46 @@ quit(int arg)
}
static void
+search(int arg)
+{
+ Posn pos;
+ uchar* p;
+
+ if (arg == '/') {
+ str_zero(&srch);
+ input(&srch, w.wy, "/");
+ for (pos = 0; pos < srch.n; ++pos)
+ if (srch.s[pos] == '^')
+ srch.s[pos] = '\n';
+ }
+ if (arg == '/' || arg == 'n') {
+ move(Right);
+ p = (uchar*)strstr(f->s->s + f->dot.p0, srch.s);
+ if (p == NULL) {
+ move(Left);
+ return;
+ }
+ if (srch.s[0] == '\n')
+ ++p;
+ } else {
+ for (;;) {
+ pos = f->dot.p1;
+ for (;move(Left), f->dot.p1 && f->s->s[f->dot.p1] != srch.s[0];);
+ if (!strncmp(f->s->s + f->dot.p1, srch.s, srch.n))
+ break;
+ if (!f->dot.p1) {
+ f->dot.p0 = f->dot.p1 = pos;
+ return;
+ }
+ }
+ if (srch.s[0] == '\n')
+ move(Right);
+ p = (uchar*)f->s->s + f->dot.p1;
+ }
+ f->dot.p0 = f->dot.p1 = p - (uchar*)f->s->s;
+}
+
+static void
str_init(String* p)
{
p->s = emalloc(MINSIZE * sizeof(*p->s));