From 14c9599a30820272a00822ad6a8d6b1d10fd40d3 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 14 Dec 2013 15:38:21 -0500 Subject: [PATCH] couple fixes --- src/chat.c | 16 ++++++---------- src/groupchat.c | 18 +++++++----------- src/prompt.c | 36 ++++++++++++++++-------------------- src/toxic_strings.c | 3 ++- src/toxic_windows.h | 2 +- 5 files changed, 32 insertions(+), 43 deletions(-) diff --git a/src/chat.c b/src/chat.c index 382ed41..2db6a8e 100644 --- a/src/chat.c +++ b/src/chat.c @@ -373,19 +373,15 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) } else if (key == KEY_UP) { /* fetches previous item in history */ - if (ctx->hst_pos >= 0) { - fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, - &ctx->hst_pos, LN_HIST_MV_UP); - mv_curs_end(self->window, ctx->len, y2, x2); - } + fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, + &ctx->hst_pos, LN_HIST_MV_UP); + mv_curs_end(self->window, ctx->len, y2, x2); } else if (key == KEY_DOWN) { /* fetches next item in history */ - if (ctx->hst_pos < ctx->hst_tot) { - fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, - &ctx->hst_pos, LN_HIST_MV_DWN); - mv_curs_end(self->window, ctx->len, y2, x2); - } + fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, + &ctx->hst_pos, LN_HIST_MV_DWN); + mv_curs_end(self->window, ctx->len, y2, x2); } else if (key == '\t') { /* TAB key: command */ diff --git a/src/groupchat.c b/src/groupchat.c index 24f884b..798c284 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -301,7 +301,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */ if (ctx->pos > 0) { - cur_len = wcwidth(ctx->line[ctx->pos - 1]); + cur_len = MAX(1, wcwidth(ctx->line[ctx->pos - 1])); del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len); if (x == 0) @@ -367,19 +367,15 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) } else if (key == KEY_UP) { /* fetches previous item in history */ - if (ctx->hst_pos >= 0) { - fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, - &ctx->hst_pos, LN_HIST_MV_UP); - mv_curs_end(self->window, ctx->len, y2, x2); - } + fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, + &ctx->hst_pos, LN_HIST_MV_UP); + mv_curs_end(self->window, ctx->len, y2, x2); } else if (key == KEY_DOWN) { /* fetches next item in history */ - if (ctx->hst_pos < ctx->hst_tot) { - fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, - &ctx->hst_pos, LN_HIST_MV_DWN); - mv_curs_end(self->window, ctx->len, y2, x2); - } + fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, + &ctx->hst_pos, LN_HIST_MV_DWN); + mv_curs_end(self->window, ctx->len, y2, x2); } else if (key == '\t') { /* TAB key: completes peer name */ diff --git a/src/prompt.c b/src/prompt.c index 098053b..1ca50b8 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -166,35 +166,31 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) } else if (key == KEY_UP) { /* fetches previous item in history */ - if (prt->hst_pos >= 0) { - wmove(self->window, prt->orig_y, X_OFST); - fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, prt->hst_tot, - &prt->hst_pos, LN_HIST_MV_UP); + wmove(self->window, prt->orig_y, X_OFST); + fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, prt->hst_tot, + &prt->hst_pos, LN_HIST_MV_UP); - /* adjust line y origin appropriately when window scrolls down */ - if (prt->at_bottom && prt->len >= x2 - X_OFST) { - int px2 = prt->len >= x2 ? x2 : x2 - X_OFST; - int p_ofst = px2 != x2 ? 0 : X_OFST; + /* adjust line y origin appropriately when window scrolls down */ + if (prt->at_bottom && prt->len >= x2 - X_OFST) { + int px2 = prt->len >= x2 ? x2 : x2 - X_OFST; + int p_ofst = px2 != x2 ? 0 : X_OFST; - if (px2 <= 0) - return; + if (px2 <= 0) + return; - int k = prt->orig_y + ((prt->len + p_ofst) / px2); + int k = prt->orig_y + ((prt->len + p_ofst) / px2); - if (k >= y2) { - wprintw(self->window, "\n"); - --prt->orig_y; - } + if (k >= y2) { + wprintw(self->window, "\n"); + --prt->orig_y; } } } else if (key == KEY_DOWN) { /* fetches next item in history */ - if (prt->hst_pos < prt->hst_tot) { - wmove(self->window, prt->orig_y, X_OFST); - fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, prt->hst_tot, - &prt->hst_pos, LN_HIST_MV_DWN); - } + wmove(self->window, prt->orig_y, X_OFST); + fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, prt->hst_tot, + &prt->hst_pos, LN_HIST_MV_DWN); } else if (key == '\t') { /* TAB key: completes command */ diff --git a/src/toxic_strings.c b/src/toxic_strings.c index 829d89c..fc8f8af 100644 --- a/src/toxic_strings.c +++ b/src/toxic_strings.c @@ -130,7 +130,8 @@ void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, wchar_t (*hst)[MAX_ beep(); } } else { - if (++(*hst_pos) == hst_tot) { + if (++(*hst_pos) > hst_tot) { + --(*hst_pos); discard_buf(buf, pos, len); return; } diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 05b8ca7..4f8b025 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -22,7 +22,7 @@ #define MAX_STR_SIZE 256 #define MAX_CMDNAME_SIZE 64 #define KEY_SIZE_BYTES 32 -#define TOXIC_MAX_NAME_LENGTH 32 /* Must be < TOX_MAX_NAME_LENGTH */ +#define TOXIC_MAX_NAME_LENGTH 32 /* Must be <= TOX_MAX_NAME_LENGTH */ #define N_DEFAULT_WINS 2 /* number of permanent default windows */ #define CURS_Y_OFFSET 3 /* y-axis cursor offset for chat contexts */ #define CHATBOX_HEIGHT 4