Skip to content

Commit

Permalink
Added support for backspace merge. (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
avalonbits authored Nov 14, 2023
1 parent 0700416 commit 691b3fb
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
Binary file modified bin/aed.bin
Binary file not shown.
40 changes: 40 additions & 0 deletions src/cmd_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ static void scroll_down_from_top(screen* scr, text_buffer* buf, uint8_t ch) {
}
vdp_cursor_tab(scr->currY_, scr->currX_);
scr_show_cursor_ch(scr, ch);

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]);
}
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) {
Expand Down Expand Up @@ -180,8 +201,27 @@ void cmd_del(screen* scr, text_buffer* buf) {
scr_del(scr, suffix, sz);
}

static void cmd_bksp_merge(screen* scr, text_buffer* buf) {
if (!tb_bksp_merge(buf)) {
return;
}
uint8_t ch = tb_peek(buf);
if (scr->currY_ > scr->topY_) {
scr->currY_--;
}
if (buf->x_ > scr->cols_-1) {
scr->currX_ = scr->cols_-1;
} else {
scr->currX_ = buf->x_;
}
scroll_down_from_top(scr, buf, ch);
}

void cmd_bksp(screen* scr, text_buffer* buf) {
if (tb_bol(buf)) {
if (tb_ypos(buf) > 1) {
cmd_bksp_merge(scr, buf);
}
return;
}

Expand Down
15 changes: 15 additions & 0 deletions src/line_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ bool lb_merge_next(line_buffer* lb) {
return true;
}

int lb_merge_prev(line_buffer* lb) {
if (lb->curr_ == lb->buf_) {
return -1;
}

int curr = *lb->curr_;
lb->curr_--;
(*lb->curr_) -= 2;
int next = *lb->curr_;
(*lb->curr_) += curr;

return next;
}


int lb_copy(line_buffer* lb, uint8_t* buf, int size) {
const int prefix = lb->curr_ - lb->buf_+1;
int used;
Expand Down
1 change: 1 addition & 0 deletions src/line_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ bool lb_down(line_buffer* lb);
bool lb_new(line_buffer* lb, int size);
bool lb_del(line_buffer* lb);
bool lb_merge_next(line_buffer* lb);
int lb_merge_prev(line_buffer* lb);

int lb_copy(line_buffer* cb, uint8_t* buf, int size);

Expand Down
11 changes: 11 additions & 0 deletions src/text_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ bool tb_del_merge(text_buffer* tb) {
return true;
}

bool tb_bksp_merge(text_buffer* tb) {
if (!tb_bol(tb) || tb_ypos(tb) == 1) {
return false;
}
cb_bksp(&tb->cb_);
cb_bksp(&tb->cb_);

tb->x_ = lb_merge_prev(&tb->lb_);
return true;
}


// Cursor ops.
uint8_t tb_next(text_buffer* tb) {
Expand Down
1 change: 1 addition & 0 deletions src/text_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ bool tb_bksp(text_buffer* tb);
bool tb_newline(text_buffer* tb);
bool tb_del_line(text_buffer* tb);
bool tb_del_merge(text_buffer* tb);
bool tb_bksp_merge(text_buffer* tb);

// Cursor ops.
uint8_t tb_next(text_buffer* tb);
Expand Down

0 comments on commit 691b3fb

Please sign in to comment.