diff --git a/src/chat.c b/src/chat.c index 341b414..0fbb60b 100644 --- a/src/chat.c +++ b/src/chat.c @@ -280,17 +280,26 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) getyx(self->window, y, x); getmaxyx(self->window, y2, x2); - /* BACKSPACE key: Remove one character from line */ - if (key == 0x107 || key == 0x8 || key == 0x7f) { + if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */ if (ctx->pos > 0) { - del_char_from_buf(key, ctx->line, &ctx->pos, &ctx->len); + del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len); if (x == 0) wmove(self->window, y-1, x2-1); else wmove(self->window, y, x-1); } - } else if (key == KEY_LEFT && ctx->pos > 0) { + } else if (key == KEY_DC) { /* DEL key: Remove character at pos */ + del_char_buf_frnt(ctx->line, &ctx->pos, &ctx->len); + } else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */ + ctx->pos = 0; + wmove(self->window, y2 - CURS_Y_OFFSET, 0); + } else if (key == KEY_END) { /* END key: move cursor to end of line */ + ctx->pos = ctx->len; + int end_y = (ctx->pos / x2) + y; + int end_x = ctx->len % x2; + wmove(self->window, end_y, end_x); + } else if (key == KEY_LEFT && ctx->pos > 0) { --ctx->pos; if (x == 0) @@ -305,7 +314,6 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) else wmove(self->window, y, x+1); } else - /* Add printable chars to buffer and print on input space */ #if HAVE_WIDECHAR if (iswprint(key)) #else diff --git a/src/groupchat.c b/src/groupchat.c index 5344ab4..70efe7a 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -220,17 +220,26 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) getyx(self->window, y, x); getmaxyx(self->window, y2, x2); - /* BACKSPACE key: Remove one character from line */ - if (key == 0x107 || key == 0x8 || key == 0x7f) { + if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */ if (ctx->pos > 0) { - del_char_from_buf(key, ctx->line, &ctx->pos, &ctx->len); + del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len); if (x == 0) wmove(self->window, y-1, x2-1); else wmove(self->window, y, x-1); } - } else if (key == KEY_LEFT && ctx->pos > 0) { + } else if (key == KEY_DC) { /* DEL key: Remove character at pos */ + del_char_buf_frnt(ctx->line, &ctx->pos, &ctx->len); + } else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */ + ctx->pos = 0; + wmove(self->window, y2 - CURS_Y_OFFSET, 0); + } else if (key == KEY_END) { /* END key: move cursor to end of line */ + ctx->pos = ctx->len; + int end_y = (ctx->pos / x2) + y; + int end_x = ctx->len % x2; + wmove(self->window, end_y, end_x); + } else if (key == KEY_LEFT && ctx->pos > 0) { --ctx->pos; if (x == 0) diff --git a/src/misc_tools.c b/src/misc_tools.c index 7199857..72386c6 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -188,8 +188,8 @@ void add_char_to_buf(wint_t ch, wchar_t *buf, size_t *pos, size_t *len) ++(*len); } -/* Deletes the character before pos via the backspace key */ -void del_char_from_buf(wint_t ch, wchar_t *buf, size_t *pos, size_t *len) +/* Deletes the character before pos */ +void del_char_buf_bck(wchar_t *buf, size_t *pos, size_t *len) { if (*pos <= 0) return; @@ -211,7 +211,21 @@ void del_char_from_buf(wint_t ch, wchar_t *buf, size_t *pos, size_t *len) --(*len); } -/* sets pos and len to 0 */ +/* Deletes the character at pos */ +void del_char_buf_frnt(wchar_t *buf, size_t *pos, size_t *len) +{ + if (*pos < 0 || *pos >= *len) + return; + + int i; + + for (i = *pos; i < *len; ++i) + buf[i] = buf[i+1]; + + --(*len); +} + +/* nulls buf and sets pos and len to 0 */ void reset_buf(wchar_t *buf, size_t *pos, size_t *len) { buf[0] = L'\0'; diff --git a/src/misc_tools.h b/src/misc_tools.h index ec83734..cd5fe3b 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -44,8 +44,11 @@ bool valid_nick(uint8_t *nick); /* Adds char to buffer at pos */ void add_char_to_buf(wint_t ch, wchar_t *buf, size_t *pos, size_t *len); -/* Deletes the character before pos via the backspace key */ -void del_char_from_buf(wint_t ch, wchar_t *buf, size_t *pos, size_t *len); +/* Deletes the character before pos */ +void del_char_buf_bck(wchar_t *buf, size_t *pos, size_t *len); -/* sets pos and len to 0 */ -void reset_buf(wchar_t *buf, size_t *pos, size_t *len); \ No newline at end of file +/* Deletes the character at pos */ +void del_char_buf_frnt(wchar_t *buf, size_t *pos, size_t *len); + +/* nulls buf and sets pos and len to 0 */ +void reset_buf(wchar_t *buf, size_t *pos, size_t *len);