summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c110
1 files changed, 75 insertions, 35 deletions
diff --git a/main.c b/main.c
index e20ff27..589f9e0 100644
--- a/main.c
+++ b/main.c
@@ -99,6 +99,12 @@ void ed_buf_free(EditBuf *b) {
arena_free(&b->arena);
}
+void ed_buf_change_path(Editor *e, u32 i, Str s) {
+ EditBuf *eb = &e->buf[i];
+ eb->path = str_dup(s, &eb->arena);
+ eb->type = ED_BUF_FILE;
+}
+
void ed_init(Editor *e) {
memset(e, 0, sizeof(Editor));
e->scratch = arena_init(1L << 30);
@@ -192,9 +198,8 @@ static inline int bracket_dir(u32 c) {
}
}
-int match_bracket(TxtLoc l, TxtLoc *out) {
+int match_bracket(TxtLoc l, TxtLoc *out, u32 c) {
u32 depth = 1;
- u32 c = txt_chr(l);
u32 o = bracket_opp(c);
int dir = bracket_dir(c);
if (dir < 0) {
@@ -240,13 +245,13 @@ TxtLoc prev_func_end(TxtLoc l) {
}
TxtLoc prev_func(TxtLoc l) {
- match_bracket(prev_func_end(l), &l);
+ match_bracket(prev_func_end(l), &l, '}');
return l;
}
TxtLoc next_func(TxtLoc l) {
- match_bracket(l, &l);
- match_bracket(next_func_end(l), &l);
+ match_bracket(l, &l, '{');
+ match_bracket(next_func_end(l), &l, '}');
return l;
}
@@ -674,7 +679,7 @@ loop:
l = next_func(l);
break;
case '%':
- if (!match_bracket(l, &l)) return 0;
+ if (!match_bracket(l, &l, txt_chr(l))) return 0;
break;
case KEY_PGUP:
@@ -935,6 +940,51 @@ static void debug_file(Str path) {
}
}
+/*
+case '<': {
+ TxtLoc end = eb->cur;
+ VuiKey k = vui_key();
+ if (k == '<' || motion(&end, k)) {
+ if (k == '<') end = next_line_start(end);
+ u32 ofs = txt_ofs(eb->cur);
+ TxtLoc start = start_of_line(eb->cur);
+ u32 lines = 0;
+ for (TxtLoc t = start; txt_before(t, end); t = next_line_start(t)) lines++;
+ while (lines--) {
+ if (txt_byte(start) == '\t')
+ start = txt_delete_c(cnext(start));
+ start = next_line_start(start);
+ }
+ eb->cur = txt_at(eb->txt, ofs);
+ }
+} break;
+*/
+
+static TxtLoc indent_dedent_lines(TxtLoc start, TxtLoc end, int n) {
+ if (txt_before(end, start)) {
+ TxtLoc t = start;
+ start = end;
+ end = t;
+ }
+ u32 ofs = txt_ofs(start);
+ start = start_of_line(start);
+ u32 lines = 0;
+ for (TxtLoc t = start; txt_before(t, end); t = next_line_start(t)) lines++;
+ while (lines--) {
+ if (n > 0) {
+ if (!empty_line(start))
+ for (u32 j = n; j--;)
+ start = txt_insert_c(start, '\t');
+ } else {
+ for (u32 j = n; j++;)
+ if (txt_byte(start) == '\t')
+ start = txt_delete_c(cnext(start));
+ }
+ start = next_line_start(start);
+ }
+ return txt_at(start.t, ofs);
+}
+
int main(int argc, const char **argv) {
ed_init(&e);
@@ -1107,44 +1157,34 @@ int main(int argc, const char **argv) {
}
break;
+ case '<':
case '>': {
TxtLoc end = eb->cur;
VuiKey k = vui_key();
- if (k == '>' || motion(&end, k)) {
- if (k == '>') end = next_line_start(end);
- u32 ofs = txt_ofs(eb->cur);
- TxtLoc start = start_of_line(eb->cur);
- u32 lines = 0;
- for (TxtLoc t = start; txt_before(t, end); t = next_line_start(t)) lines++;
- while (lines--) {
- if (!empty_line(start))
- start = txt_insert_c(start, '\t');
- start = next_line_start(start);
- }
- eb->cur = txt_at(eb->txt, ofs);
- }
- } break;
- case '<': {
- TxtLoc end = eb->cur;
- VuiKey k = vui_key();
- if (k == '<' || motion(&end, k)) {
- if (k == '<') end = next_line_start(end);
- u32 ofs = txt_ofs(eb->cur);
- TxtLoc start = start_of_line(eb->cur);
- u32 lines = 0;
- for (TxtLoc t = start; txt_before(t, end); t = next_line_start(t)) lines++;
- while (lines--) {
- if (txt_byte(start) == '\t')
- start = txt_delete_c(cnext(start));
- start = next_line_start(start);
- }
- eb->cur = txt_at(eb->txt, ofs);
+ if (k == '<' || k == '>' || motion(&end, k)) {
+ if (k == '<' || k == '>') end = next_line_start(end);
+ eb->cur = indent_dedent_lines(eb->cur, end, c == '<' ? -1 : 1);
}
} break;
+
case '=': {
Str s = get_input_line(S("File to open: "));
if (s.n > 0) e.bufi = ed_buf_open(&e, str_to_cstr(s, &e.scratch));
} break;
+
+ case '+': {
+ Str s = get_input_line(S("Replace file with: "));
+ if (s.n > 0) {
+ e.bufi = ed_buf_close(&e, e.bufi);
+ e.bufi = ed_buf_open(&e, str_to_cstr(s, &e.scratch));
+ }
+ } break;
+
+ case '-': {
+ Str s = get_input_line(S("Change path: "));
+ if (s.n > 0) ed_buf_change_path(&e, e.bufi, s);
+ } break;
+
default:
motion(&eb->cur, c);
break;