diff --git a/src/misc_tools.h b/src/misc_tools.h index 74e8c7c..93aa907 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -3,6 +3,7 @@ */ // #define MIN(x, y) (((x) < (y)) ? (x) : (y)) + #define MAX(x, y) (((x) > (y)) ? (x) : (y)) /* convert a hex string to binary */ unsigned char *hex_string_to_bin(char hex_string[]); diff --git a/src/toxic_strings.c b/src/toxic_strings.c index 3f92cc0..829d89c 100644 --- a/src/toxic_strings.c +++ b/src/toxic_strings.c @@ -89,6 +89,20 @@ void reset_buf(wchar_t *buf, size_t *pos, size_t *len) *len = 0; } +#define HIST_PURGE MAX_LINE_HIST / 4 + +/* shifts hist items back and makes room for HIST_PURGE new entries */ +static void shift_hist_back(wchar_t (*hst)[MAX_STR_SIZE], int *hst_tot) +{ + int i; + int n = MAX_LINE_HIST - HIST_PURGE; + + for (i = 0; i < n; ++i) + wmemcpy(hst[i], hst[i+HIST_PURGE], MAX_STR_SIZE); + + *hst_tot = n; +} + /* adds a line to the ln_history buffer at hst_pos and sets hst_pos to end of history. */ void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZE], int *hst_tot, int *hst_pos) @@ -96,17 +110,12 @@ void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZ if (len > MAX_STR_SIZE) return; - /* If history is full make room for newest entry and don't increment hst_tot */ - if (*hst_tot == MAX_LINE_HIST) { - int i; - - for (i = 0; i < MAX_LINE_HIST - 1; ++i) - wmemcpy(hst[i], hst[i+1], MAX_STR_SIZE); - } else { - ++(*hst_tot); - } + if (*hst_tot >= MAX_LINE_HIST) + shift_hist_back(hst, hst_tot); + ++(*hst_tot); *hst_pos = *hst_tot; + wmemcpy(hst[*hst_tot-1], buf, len + 1); } diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 30011c0..05b8ca7 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -34,7 +34,7 @@ #define T_KEY_KILL 0xB /* ctrl-k */ #define T_KEY_DISCARD 0x15 /* ctrl-u */ #define T_KEY_NEXT 0x10 /* ctrl-p */ -#define T_KEY_PREV 0x0f /* ctrl-o */ +#define T_KEY_PREV 0x0F /* ctrl-o */ /* Curses foreground colours (background is black) */ enum { @@ -111,7 +111,7 @@ struct StatusBar { bool is_online; }; -#define MAX_LINE_HIST 64 +#define MAX_LINE_HIST 128 /* chat and groupchat window/buffer holder */ struct ChatContext {