1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-22 10:33:01 +01:00

Add ability to skip words in input field with ctrl-left/right arrow

This commit is contained in:
jfreegman 2020-10-10 11:21:15 -04:00
parent 015dbd9a96
commit 9c06ad608b
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
3 changed files with 61 additions and 5 deletions

View File

@ -100,7 +100,6 @@ static void input_del_word(ToxWindow *self)
if (del_word_buf(ctx) == -1) {
sound_notify(self, notif_error, 0, NULL);
return;
}
}
@ -172,17 +171,41 @@ static void input_mv_left(ToxWindow *self, int x, int mx_x)
}
int cur_len = ctx->pos > 0 ? wcwidth(ctx->line[ctx->pos - 1]) : 0;
int s_len = ctx->start > 0 ? wcwidth(ctx->line[ctx->start - 1]) : 0;
--ctx->pos;
if (ctx->start && (x >= mx_x - cur_len)) {
if (ctx->start > 0 && (x >= mx_x - cur_len)) {
int s_len = wcwidth(ctx->line[ctx->start - 1]);
ctx->start = MAX(0, ctx->start - 1 + (s_len - cur_len));
} else if (ctx->start) {
} else if (ctx->start > 0) {
ctx->start = MAX(0, ctx->start - cur_len);
}
}
/* moves the cursor to the beginning of the previous word in input field and buffer */
static void input_skip_left(ToxWindow *self, int x, int mx_x)
{
ChatContext *ctx = self->chatwin;
if (ctx->pos <= 0) {
return;
}
int count = 0;
do {
--ctx->pos;
count += wcwidth(ctx->line[ctx->pos]);
} while (ctx->pos > 0 && (ctx->line[ctx->pos - 1] != L' ' || ctx->line[ctx->pos] == L' '));
if (ctx->start > 0 && (x >= mx_x - count)) {
int s_len = wcwidth(ctx->line[ctx->start - 1]);
ctx->start = MAX(0, ctx->start - 1 + (s_len - count));
} else if (ctx->start > 0) {
ctx->start = MAX(0, ctx->start - count);
}
}
/* moves cursor/line position right in input field and buffer */
static void input_mv_right(ToxWindow *self, int x, int mx_x)
{
@ -202,6 +225,29 @@ static void input_mv_right(ToxWindow *self, int x, int mx_x)
}
}
/* moves the cursor to the end of the next word in input field and buffer */
static void input_skip_right(ToxWindow *self, int x, int mx_x)
{
ChatContext *ctx = self->chatwin;
if (ctx->pos >= ctx->len) {
return;
}
int count = 0;
do {
count += wcwidth(ctx->line[ctx->pos]);
++ctx->pos;
} while (ctx->pos < ctx->len && !(ctx->line[ctx->pos] == L' ' && ctx->line[ctx->pos - 1] != L' '));
int newpos = x + count;
if (newpos >= mx_x) {
ctx->start += (1 + (newpos - mx_x));
}
}
/* puts a line history item in input field and buffer */
static void input_history(ToxWindow *self, wint_t key, int mx_x)
{
@ -271,6 +317,14 @@ bool input_handle(ToxWindow *self, wint_t key, int x, int mx_x)
force_refresh(self->chatwin->history);
break;
case T_KEY_C_LEFT:
input_skip_left(self, x, mx_x);
break;
case T_KEY_C_RIGHT:
input_skip_right(self, x, mx_x);
break;
default:
match = false;
break;

View File

@ -343,4 +343,4 @@ int osx_video_read_device(uint8_t *y, uint8_t *u, uint8_t *v, uint16_t *width, u
* End of C-interface for OSXVideo
*/
#endif /* __OBJC__ */
#endif /* __OBJC__ */

View File

@ -71,6 +71,8 @@
#define T_KEY_C_W 0x17 /* ctrl-w */
#define T_KEY_C_B 0x02 /* ctrl-b */
#define T_KEY_C_T 0x14 /* ctrl-t */
#define T_KEY_C_LEFT 0x221 /* ctrl-left arrow */
#define T_KEY_C_RIGHT 0x230 /* ctrl-right arrow */
#define T_KEY_TAB 0x09 /* TAB key */
#define ONLINE_CHAR "*"