Skip to content

Commit

Permalink
Bug fixes for DEL/BACKSPACE merge + refactorings + correct tab spacin…
Browse files Browse the repository at this point in the history
…g. (#14)

This fixes all line merging bugs when using DELETE or BACKSPACE and
refactors the scrolling code, reducing the binary size.
  • Loading branch information
avalonbits authored Nov 15, 2023
1 parent 691b3fb commit e8cd809
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 86 deletions.
Binary file modified bin/aed.bin
Binary file not shown.
147 changes: 66 additions & 81 deletions src/cmd_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@

#define UN(x) (void)(x)

void cmd_noop(screen* scr, text_buffer* buf) {
UN(scr);UN(buf);
}

static bool save_file(text_buffer* buf) {
if (!tb_valid_file(buf)) {
return false;
Expand Down Expand Up @@ -71,7 +67,8 @@ void cmd_putc(screen* scr, text_buffer* buf, key k) {
if (k.key == '\t') {
// Convert tab to spaces because it is too damn hard to get it working correctly with line scrolling.
k.key = ' ';
for (uint8_t i = 0; i < scr->tab_size_; i++) {
const uint8_t spaces = scr->tab_size_ - ((tb_xpos(buf)-1) % scr->tab_size_);
for (uint8_t i = 0; i < spaces; i++) {
cmd_putc(scr, buf, k);
}
return;
Expand All @@ -85,18 +82,7 @@ void cmd_putc(screen* scr, text_buffer* buf, key k) {
scr_putc(scr, k.key, prefix, psz, suffix, ssz);
}

static void scroll_down_from_top(screen* scr, text_buffer* buf, uint8_t ch) {
line_itr next = tb_nline(buf, tb_ypos(buf));
uint8_t ypos = scr->currY_;
for (line l = next(); l.b != NULL && ypos < scr->bottomY_; l = next(), ++ypos) {
scr_overwrite_line(scr, ypos, l.b, l.sz, l.osz);
}
if (ypos < scr->bottomY_) {
scr_write_line(scr, ypos, NULL, 0);
}
vdp_cursor_tab(scr->currY_, scr->currX_);
scr_show_cursor_ch(scr, ch);

static void scr_write_padded(screen* scr, text_buffer* buf) {
vdp_cursor_tab(scr->currY_, 0);
int psz = 0;
uint8_t* prefix = tb_prefix(buf, &psz);
Expand All @@ -114,44 +100,40 @@ static void scroll_down_from_top(screen* scr, text_buffer* buf, uint8_t ch) {
for (int j = 0; j < sz && i < scr->cols_; i++, j++) {
putch(suffix[j]);
}
vdp_cursor_tab(scr->currY_, scr->currX_);
scr_show_cursor_ch(scr, ch);

}

static void scroll_up_from_top(screen* scr, text_buffer* buf, uint8_t ch) {

static void scroll_from_top(screen* scr, text_buffer* buf, uint8_t ch, const bool osz_is_last) {
line_itr next = tb_nline(buf, tb_ypos(buf));
uint8_t ypos = scr->currY_;
int osz = 255;
for (line l = next(); l.b != NULL && ypos < scr->bottomY_; l = next(), ++ypos) {
if (!osz_is_last) {
osz = l.osz;
}
scr_overwrite_line(scr, ypos, l.b, l.sz, osz);
osz = l.sz;
if (osz_is_last) {
osz = l.sz;
}
}
if (ypos < scr->bottomY_) {
scr_overwrite_line(scr, ypos, NULL, 0, osz);
}

vdp_cursor_tab(scr->currY_, 0);
int psz = 0;
uint8_t* prefix = tb_prefix(buf, &psz);
int i = 0;
int pad = buf->x_ - scr->currX_;
if (pad < 0) {
pad = 0;
}
for (; i < scr->currX_; i++) {
putch(prefix[i+pad]);
}

int sz = 0;
uint8_t* suffix = tb_suffix(buf, &sz);
for (int j = 0; j < sz && i < scr->cols_; i++, j++) {
putch(suffix[j]);
scr_write_line(scr, ypos, NULL, 0);
}
scr_write_padded(scr, buf);
vdp_cursor_tab(scr->currY_, scr->currX_);
scr_show_cursor_ch(scr, ch);

}

static void scroll_up_from_top(screen* scr, text_buffer* buf, uint8_t ch) {
scroll_from_top(scr, buf, ch, false);
}

static void scroll_down_from_top(screen* scr, text_buffer* buf, uint8_t ch) {
scroll_from_top(scr, buf, ch, true);
}


static void scroll_lines(screen* scr, text_buffer* buf, uint8_t ch) {
if (scr->currY_ < scr->bottomY_-1) {
line_itr next = tb_nline(buf, tb_ypos(buf));
Expand All @@ -172,16 +154,16 @@ static void scroll_lines(screen* scr, text_buffer* buf, uint8_t ch) {
}

void cmd_show(screen* scr, text_buffer* buf) {
uint8_t to_ch = tb_peek(buf);
const uint8_t to_ch = tb_peek(buf);
scroll_down_from_top(scr, buf, to_ch);
}

static void cmd_del_merge(screen* scr, text_buffer* buf) {
if (!tb_del_merge(buf)) {
return;
}
uint8_t ch = tb_peek(buf);
scroll_up_from_top(scr, buf, ch);
const uint8_t ch = tb_peek(buf);
scroll_down_from_top(scr, buf, ch);
}

void cmd_del(screen* scr, text_buffer* buf) {
Expand Down Expand Up @@ -253,7 +235,7 @@ void cmd_del_line(screen* scr, text_buffer* buf) {
}
scr->currX_ = 0;
uint8_t ch = tb_peek(buf);
scroll_up_from_top(scr, buf, ch);
scroll_down_from_top(scr, buf, ch);
}

void cmd_left(screen* scr, text_buffer* buf) {
Expand All @@ -280,9 +262,9 @@ void cmd_w_left(screen* scr, text_buffer* buf) {
}
return;
}
int from_x = tb_xpos(buf);
uint8_t from_ch = tb_peek(buf);
uint8_t to_ch = tb_w_prev(buf);
const int from_x = tb_xpos(buf);
const uint8_t from_ch = tb_peek(buf);
const uint8_t to_ch = tb_w_prev(buf);
const uint8_t deltaX = from_x - tb_xpos(buf);

int sz = 0;
Expand All @@ -304,8 +286,8 @@ void cmd_right(screen* scr, text_buffer* buf) {
if (from_ch == 0 ) {
return;
}
uint8_t to_ch = tb_next(buf);

const uint8_t to_ch = tb_next(buf);
int sz = 0;
uint8_t* prefix = tb_prefix(buf, &sz);
scr_right(scr, from_ch, to_ch, 1, prefix, sz);
Expand All @@ -321,9 +303,9 @@ void cmd_w_right(screen* scr, text_buffer* buf) {
return;
}

int from_x = tb_xpos(buf);
uint8_t from_ch = tb_peek(buf);
uint8_t to_ch = tb_w_next(buf);
const int from_x = tb_xpos(buf);
const uint8_t from_ch = tb_peek(buf);
const uint8_t to_ch = tb_w_next(buf);
const uint8_t deltaX = tb_xpos(buf) - from_x;

int sz = 0;
Expand All @@ -343,23 +325,25 @@ void cmd_up(screen* scr, text_buffer* buf) {
}

if (scr->currY_ <= scr->topY_) {
scroll_down_from_top(scr, buf, to_ch);
} else {
if (scr->currX_ >= scr->cols_-1) {
scr_write_line(scr, scr->currY_, prefix, psz);
if (tb_xpos(buf) >= scr->cols_) {
psz = 0;
prefix = tb_prefix(buf, &psz);
int pad = (tb_xpos(buf)-1) - scr->currX_;
scr_write_line(scr, scr->currY_-1, prefix+pad, psz-pad);
}
}
int xpos = tb_xpos(buf)-1;
if (xpos > scr->cols_-1) {
xpos = scr->cols_-1;
scroll_up_from_top(scr, buf, to_ch);
return;
}

if (scr->currX_ >= scr->cols_-1) {
scr_write_line(scr, scr->currY_, prefix, psz);
if (tb_xpos(buf) >= scr->cols_) {
psz = 0;
prefix = tb_prefix(buf, &psz);
int pad = (tb_xpos(buf)-1) - scr->currX_;
scr_write_line(scr, scr->currY_-1, prefix+pad, psz-pad);
}
scr_up(scr, from_ch, to_ch, xpos);
}

int xpos = tb_xpos(buf)-1;
if (xpos > scr->cols_-1) {
xpos = scr->cols_-1;
}
scr_up(scr, from_ch, to_ch, xpos);
}

void cmd_down(screen* scr, text_buffer* buf) {
Expand All @@ -373,22 +357,23 @@ void cmd_down(screen* scr, text_buffer* buf) {
}
if (scr->currY_ >= scr->bottomY_-1) {
scroll_lines(scr, buf, to_ch);
} else {
if (scr->currX_ >= scr->cols_-1) {
scr_write_line(scr, scr->currY_, prefix, psz);
if (tb_xpos(buf) >= scr->cols_) {
psz = 0;
prefix = tb_prefix(buf, &psz);
int pad = (tb_xpos(buf)-1) - scr->currX_;
scr_write_line(scr, scr->currY_+1, prefix+pad, psz-pad);
}
}
int xpos = tb_xpos(buf)-1;
if (xpos > scr->cols_-1) {
xpos = scr->cols_-1;
return;
}
if (scr->currX_ >= scr->cols_-1) {
scr_write_line(scr, scr->currY_, prefix, psz);
if (tb_xpos(buf) >= scr->cols_) {
psz = 0;
prefix = tb_prefix(buf, &psz);
int pad = (tb_xpos(buf)-1) - scr->currX_;
scr_write_line(scr, scr->currY_+1, prefix+pad, psz-pad);
}
scr_down(scr, from_ch, to_ch, xpos);
}

int xpos = tb_xpos(buf)-1;
if (xpos > scr->cols_-1) {
xpos = scr->cols_-1;
}
scr_down(scr, from_ch, to_ch, xpos);
}

void cmd_home(screen* scr, text_buffer* buf) {
Expand Down
1 change: 0 additions & 1 deletion src/cmd_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ typedef void(*cmd_op)(screen*, text_buffer*);
void cmd_show(screen* scr, text_buffer* buf);
bool cmd_quit(screen* scr, text_buffer* buf);

void cmd_noop(screen* scr, text_buffer* buf);
void cmd_putc(screen* scr, text_buffer* buf, key k);
void cmd_del(screen* scr, text_buffer* buf);
void cmd_bksp(screen* scr, text_buffer* buf);
Expand Down
7 changes: 3 additions & 4 deletions src/editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ void ed_destroy(editor* ed) {

#define CMD_PUTC (cmd_op) 0x01
#define CMD_QUIT (cmd_op) 0x02

typedef struct _key_command {
cmd_op cmd;
key k;
Expand All @@ -66,7 +65,7 @@ void ed_run(editor* ed) {
cmd_putc(scr, buf, kc.k);
} else if (kc.cmd == CMD_QUIT && cmd_quit(scr, buf)) {
break;
} else {
} else if (kc.cmd != NULL) {
kc.cmd(scr, buf);
}
}
Expand Down Expand Up @@ -94,7 +93,7 @@ key_command ctrlCmds(key_command kc) {
kc.cmd = cmd_del_line;
break;
default:
kc.cmd = cmd_noop;
kc.cmd = NULL;;
break;
}
return kc;
Expand Down Expand Up @@ -144,7 +143,7 @@ key_command editCmds(key_command kc) {
}

key_command read_input() {
key_command kc = {cmd_noop, {'\0', VK_NONE}};
key_command kc = {NULL, {'\0', VK_NONE}};
kc.k.key = getch();
kc.k.vkey = getsysvar_vkeycode();

Expand Down

0 comments on commit e8cd809

Please sign in to comment.