summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorWormHeamer2025-12-28 20:30:37 -0500
committerWormHeamer2025-12-28 20:30:37 -0500
commit2fe237e65a13545c053c31b7b413898d5b8734ac (patch)
tree66354165554feeb303010473c22e67b0b48cda87 /main.c
parent3ed6bbd8eb7214268d6e042736dcd5285cb4f4d7 (diff)
remove most explicit Txt* params, fix some utf-8 problems
Diffstat (limited to 'main.c')
-rw-r--r--main.c183
1 files changed, 68 insertions, 115 deletions
diff --git a/main.c b/main.c
index 0fe9513..91ef1dc 100644
--- a/main.c
+++ b/main.c
@@ -27,37 +27,37 @@ int is_space(u32 c) {
u32 move_word_back(Txt *t, u32 cur) {
TxtLoc l = txt_at(t, cur);
- while (!txt_at_start(t,l)) {
- TxtLoc n = txt_prev(t, l);
- if (!is_space(txt_chr(t, n))) break;
+ while (!at_start(l)) {
+ TxtLoc n = bprev(l);
+ if (!is_space(txt_chr(n))) break;
l = n;
}
- while (!txt_at_start(t,l)) {
- TxtLoc n = txt_prev(t, l);
- if (is_space(txt_chr(t, n))) break;
+ while (!at_start(l)) {
+ TxtLoc n = bprev(l);
+ if (is_space(txt_chr(n))) break;
l = n;
}
- return txt_ofs(t, l);
+ return txt_ofs(l);
}
u32 move_word_fwd(Txt *t, u32 cur) {
TxtLoc l = txt_at(t, cur);
- while (!txt_at_end(t,l) && is_space(txt_chr(t, l))) l = txt_next(t, l);
- while (!txt_at_end(t,l) && !is_space(txt_chr(t, l))) l = txt_next(t, l);
- return txt_ofs(t, l);
+ while (!at_end(l) && is_space(txt_chr(l))) l = cnext(l);
+ while (!at_end(l) && !is_space(txt_chr(l))) l = cnext(l);
+ return txt_ofs(l);
}
u32 move_char_back(Txt *t, u32 cur) {
while (cur > 0) {
cur--;
- u8 c = txt_byte(t, txt_at(t, cur));
+ u8 c = txt_byte(txt_at(t, cur));
if ((c & 0xc0) != 0x80) break;
}
return cur;
}
u32 move_char_fwd(Txt *t, u32 cur) {
- u8 b = txt_byte(t, txt_at(t, cur));
+ u8 b = txt_byte(txt_at(t, cur));
u32 n = stdc_leading_ones(b);
if (cur + n >= t->len) return t->len;
return cur + n + !n;
@@ -66,91 +66,40 @@ u32 move_char_fwd(Txt *t, u32 cur) {
u32 del_between(Txt *t, u32 a, u32 b) {
if (b < a) { u32 t = a; a = b; b = t; }
//if (b - a > 10) TRAP();
- return txt_delete(t, b, b - a);
-}
-
-TxtLoc next_newline(Txt *t, TxtLoc l) {
- do l = txt_next(t, l);
- while (!txt_at_end(t, l) && txt_byte(t, l) != '\n');
- return l;
-}
-
-TxtLoc prev_newline(Txt *t, TxtLoc l) {
- do l = txt_prev(t, l);
- while (!txt_at_start(t, l) && txt_byte(t, l) != '\n');
- return l;
-}
-
-TxtLoc start_of_line(Txt *t, TxtLoc l) {
- l = prev_newline(t, l);
- if (!txt_at_start(t,l) && txt_byte(t, l) == '\n') l = txt_next(t, l);
- return l;
-}
-
-TxtLoc end_of_line(Txt *t, TxtLoc l) {
- TxtLoc start = start_of_line(t, l);
- return txt_byte(t, start) == '\n' ? start : next_newline(t, start);
-}
-
-u32 get_col(Txt *t, TxtLoc l) {
- u32 n = 0;
- for (TxtLoc tmp = start_of_line(t, l); txt_before(tmp, l); txt_chr_next(t, &tmp)) n++;
- return n;
-}
-
-TxtLoc at_col(Txt *t, TxtLoc l, u32 col) {
- l = start_of_line(t, l);
- while (col--) {
- if (txt_chr_next(t, &l) == '\n') {
- l = txt_prev(t, l);
- break;
- }
- }
- return l;
-}
-
-TxtLoc prev_line(Txt *t, TxtLoc l) {
- TxtLoc start = start_of_line(t, l);
- return at_col(t, txt_prev(t, start), get_col(t, l));
-}
-
-TxtLoc next_line(Txt *t, TxtLoc l) {
- TxtLoc end = end_of_line(t, l);
- if (txt_at_end(t, end)) return end;
- return at_col(t, txt_next(t, end), get_col(t, l));
+ return txt_ofs(txt_delete(txt_at(t, b), b - a));
}
u32 move_line_up(Txt *t, u32 cur) {
- return txt_ofs(t, prev_line(t, txt_at(t, cur)));
+ return txt_ofs(prev_line(txt_at(t, cur)));
}
u32 move_line_down(Txt *t, u32 cur) {
- return txt_ofs(t, next_line(t, txt_at(t, cur)));
+ return txt_ofs(next_line(txt_at(t, cur)));
}
-int empty_line(Txt *t, TxtLoc l) {
- u8 b = txt_byte(t, start_of_line(t, l));
+int empty_line(TxtLoc l) {
+ u8 b = txt_byte(start_of_line(l));
return b == '\n' || b == 0 /* last line of buffer */;
}
-TxtLoc next_par(Txt *t, TxtLoc l) {
- while (!txt_at_end(t, l) && empty_line(t, l)) l = next_line(t, l);
- while (!txt_at_end(t, l) && !empty_line(t, l)) l = next_line(t, l);
+TxtLoc next_par(TxtLoc l) {
+ while (!at_end(l) && empty_line(l)) l = next_line(l);
+ while (!at_end(l) && !empty_line(l)) l = next_line(l);
return l;
}
-TxtLoc prev_par(Txt *t, TxtLoc l) {
- while (!txt_at_start(t, l) && empty_line(t, l)) l = prev_line(t, l);
- while (!txt_at_start(t, l) && !empty_line(t, l)) l = prev_line(t, l);
+TxtLoc prev_par(TxtLoc l) {
+ while (!at_start(l) && empty_line(l)) l = prev_line(l);
+ while (!at_start(l) && !empty_line(l)) l = prev_line(l);
return l;
}
u32 move_par_up(Txt *t, u32 cur) {
- return txt_ofs(t, prev_par(t, txt_at(t, cur)));
+ return txt_ofs(prev_par(txt_at(t, cur)));
}
u32 move_par_down(Txt *t, u32 cur) {
- return txt_ofs(t, next_par(t, txt_at(t, cur)));
+ return txt_ofs(next_par(txt_at(t, cur)));
}
/* main */
@@ -162,12 +111,12 @@ void find_view_window(Txt *t, u32 curs, TxtLoc *start, TxtLoc *end) {
TxtLoc l = txt_at(t, curs);
u32 u = LINES / 2;
TxtLoc a = l;
- for (u32 i = 0; i < u; i++) a = prev_newline(t, a);
+ for (u32 i = 0; i < u; i++) a = prev_newline(a);
u32 n = 0;
TxtLoc b = a;
- while (!txt_at_end(t, b) && n++ < LINES) b = next_newline(t, b);
- while (!txt_at_start(t, a) && n++ < LINES) a = prev_newline(t, a);
- if (!txt_at_start(t,a) && txt_byte(t,a) == '\n') a = txt_next(t, a);
+ while (!at_end(b) && n++ < LINES) b = next_newline(b);
+ while (!at_start(a) && n++ < LINES) a = prev_newline(a);
+ if (!at_start(a) && txt_byte(a) == '\n') a = bnext(a);
*start = a;
*end = b;
}
@@ -193,7 +142,7 @@ void draw(void *ctx) {
cur_found = 1;
vui_curs_pos(x, y);
}
- u32 c = txt_chr_next(&txt, &start);
+ u32 c = txt_chr_next(&start);
if (c == '\n') {
x = 0;
y++;
@@ -206,7 +155,7 @@ void draw(void *ctx) {
}
ASSERT(start.i <= txt.ptbl.v[start.p].n);
if (!cur_found) vui_curs_pos(x, y);
- u32 c = txt_chr(&txt, l);
+ u32 c = txt_chr(l);
vui_printf(-1, -4, "%u", count);
vui_aprintf(-1, -3, mode ? FG_BCYAN : FG_CYAN, "%s", mode ? "INSERT" : "NORMAL");
vui_printf(-1, -1, "%u - %u.%u - %02x (%c)", cur, l.p, l.i, c, (c < 0x20 || c > 0x7e) ? ' ' : c);
@@ -259,11 +208,11 @@ loop:
case KEY_HOME:
case '^':
case '0':
- cur = txt_ofs(&txt, start_of_line(&txt, txt_at(&txt, cur)));
+ cur = txt_ofs(start_of_line(txt_at(&txt, cur)));
break;
case KEY_END:
case '$':
- cur = txt_ofs(&txt, next_newline(&txt, txt_at(&txt, cur)));
+ cur = txt_ofs(next_newline(txt_at(&txt, cur)));
break;
case KEY_HOME | KEY_CTRL_BIT:
case 'g':
@@ -275,12 +224,12 @@ loop:
break;
case 'f': {
u32 n = vui_key();
- TxtLoc l = txt_next(&txt, txt_at(&txt, cur));
+ TxtLoc l = cnext(txt_at(&txt, cur));
for (TxtLoc t = l;;) {
- u32 c = txt_chr_next(&txt, &t);
+ u32 c = txt_chr_next(&t);
if (c == '\n') break;
if (c == n) {
- cur = txt_ofs(&txt, l);
+ cur = txt_ofs(l);
break;
}
l = t;
@@ -288,15 +237,15 @@ loop:
} break;
case 'F': {
u32 n = vui_key();
- TxtLoc l = txt_prev(&txt, txt_at(&txt, cur));
+ TxtLoc l = bprev(txt_at(&txt, cur));
for (;;) {
- u32 c = txt_chr(&txt, l);
+ u32 c = txt_chr(l);
if (c == '\n') break;
if (c == n) {
- cur = txt_ofs(&txt, l);
+ cur = txt_ofs(l);
break;
}
- l = txt_prev(&txt, l);
+ l = bprev(l);
}
} break;
default:
@@ -343,25 +292,25 @@ int main(int argc, const char **argv) {
case 'a': {
mode = 1;
TxtLoc l = txt_at(&txt, cur);
- TxtLoc e = end_of_line(&txt, l);
- l = txt_before(l, e) ? txt_next(&txt, l) : e;
- cur = txt_ofs(&txt, l);
+ TxtLoc e = end_of_line(l);
+ l = txt_before(l, e) ? cnext(l) : e;
+ cur = txt_ofs(l);
} break;
case 'I':
mode = 1;
- cur = txt_ofs(&txt, start_of_line(&txt, txt_at(&txt, cur)));
+ cur = txt_ofs(start_of_line(txt_at(&txt, cur)));
break;
case 'A':
mode = 1;
- cur = txt_ofs(&txt, end_of_line(&txt, txt_at(&txt, cur)));
+ cur = txt_ofs(end_of_line(txt_at(&txt, cur)));
break;
case 'o':
mode = 1;
- cur = txt_ofs(&txt, end_of_line(&txt, txt_at(&txt, cur)));
+ cur = txt_ofs(end_of_line(txt_at(&txt, cur)));
goto ins_newline;
case 'O':
mode = 1;
- cur = txt_ofs(&txt, txt_prev(&txt, start_of_line(&txt, txt_at(&txt, cur))));
+ cur = txt_ofs(bprev(start_of_line(txt_at(&txt, cur))));
goto ins_newline;
case 'd': {
u32 before = cur;
@@ -371,19 +320,19 @@ int main(int argc, const char **argv) {
}
} break;
case 'D': {
- u32 end = txt_ofs(&txt, end_of_line(&txt, txt_at(&txt, cur)));
+ u32 end = txt_ofs(end_of_line(txt_at(&txt, cur)));
del_between(&txt, cur, end);
} break;
case 'C': {
- u32 end = txt_ofs(&txt, end_of_line(&txt, txt_at(&txt, cur)));
+ u32 end = txt_ofs(end_of_line(txt_at(&txt, cur)));
del_between(&txt, cur, end);
mode = 1;
} break;
case 'S': {
- TxtLoc l = start_of_line(&txt, txt_at(&txt, cur));
- u32 end = txt_ofs(&txt, end_of_line(&txt, l));
- del_between(&txt, txt_ofs(&txt, l), end);
- cur = txt_ofs(&txt, l);
+ TxtLoc l = start_of_line(txt_at(&txt, cur));
+ u32 end = txt_ofs(end_of_line(l));
+ del_between(&txt, txt_ofs(l), end);
+ cur = txt_ofs(l);
mode = 1;
} break;
case 'c': {
@@ -396,10 +345,10 @@ int main(int argc, const char **argv) {
break;
}
case 'x':
- txt_delete_c(&txt, txt_ofs(&txt, txt_next(&txt, txt_at(&txt, cur))));
+ cur = txt_ofs(txt_delete_c(cnext(txt_at(&txt, cur))));
break;
case 's':
- txt_delete_c(&txt, txt_ofs(&txt, txt_next(&txt, txt_at(&txt, cur))));
+ cur = txt_ofs(txt_delete_c(cnext(txt_at(&txt, cur))));
mode = 1;
break;
case '1': case '2': case '3': case '4':
@@ -420,11 +369,13 @@ add_to_count:
case 1:
switch (c) {
case KEY_ESC:
- if (txt_after(txt_at(&txt, cur), start_of_line(&txt, txt_at(&txt, cur)))) cur--;
+ if (txt_after(txt_at(&txt, cur), start_of_line(txt_at(&txt, cur)))) {
+ cur = txt_ofs(cprev(txt_at(&txt, cur)));
+ }
mode = 0;
break;
case KEY_BKSP:
- cur = txt_delete_c(&txt, cur);
+ cur = txt_ofs(txt_delete_c(txt_at(&txt, cur)));
break;
case 0x17 /* ^W */:
cur = del_between(&txt, move_word_back(&txt, cur), cur);
@@ -434,19 +385,21 @@ add_to_count:
break;
case '\r': {
ins_newline:;
+ /*
u32 tabs = 0;
- TxtLoc start = start_of_line(&txt, txt_at(&txt, cur));
- for (TxtLoc l = start; txt_byte(&txt, l) == '\t'; l = txt_next(&txt, l))
+ TxtLoc start = start_of_line(txt_at(&txt, cur));
+ for (TxtLoc l = start; txt_byte(l) == '\t'; l = bnext(l))
tabs++;
- while (txt_byte(&txt, txt_at(&txt, cur - 1)) == '\t') {
- cur = txt_delete(&txt, cur, 1);
+ while (txt_byte(txt_at(&txt, cur - 1)) == '\t') {
+ cur = txt_ofs(txt_delete(txt_at(&txt, cur), 1));
}
- cur = txt_insert_c(&txt, cur, '\n');
- while (tabs--) cur = txt_insert_c(&txt, cur, '\t');
+ */
+ cur = txt_ofs(txt_insert_c(txt_at(&txt, cur), '\n'));
+ //while (tabs--) cur = txt_ofs(txt_insert_c(txt_at(&txt, cur), '\t'));
} break;
default:
- if ((c == '\t' || c >= ' ') && c <= KEY_UTF8_MAX) cur = txt_insert_c(&txt, cur, c);
+ if ((c == '\t' || c >= ' ') && c <= KEY_UTF8_MAX) cur = txt_ofs(txt_insert_c(txt_at(&txt, cur), c));
break;
}
break;