From 43552161f92bdd45928ec4eaa8d9d2af3b158716 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 5 Oct 2014 23:12:58 -0400 Subject: [PATCH] implement word deletion via ^W --- src/groupchat.c | 6 +++--- src/input.c | 15 +++++++++++++++ src/toxic.h | 1 + src/toxic_strings.c | 33 +++++++++++++++++++++++++++++++++ src/toxic_strings.h | 5 +++++ 5 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/groupchat.c b/src/groupchat.c index 6c16abf..1b1df53 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -482,9 +482,9 @@ static void groupchat_onDraw(ToxWindow *self, Tox *m) /* truncate nick to fit in side panel without modifying list */ char tmpnck[TOX_MAX_NAME_LENGTH]; - memcpy(tmpnck, &groupchats[self->num].peer_names[peer * N], SIDEBAR_WIDTH - 2); - int len = SIDEBAR_WIDTH - 2; - tmpnck[len] = '\0'; + int maxlen = SIDEBAR_WIDTH - 2; + memcpy(tmpnck, &groupchats[self->num].peer_names[peer * N], maxlen); + tmpnck[maxlen] = '\0'; wprintw(ctx->sidebar, "%s\n", tmpnck); } diff --git a/src/input.c b/src/input.c index b037f74..35f03a4 100644 --- a/src/input.c +++ b/src/input.c @@ -83,6 +83,17 @@ static void input_delete(ToxWindow *self) sound_notify(self, error, 0, NULL); } +/* delete last typed word */ +static void input_del_word(ToxWindow *self, int x, int mx_x) +{ + ChatContext *ctx = self->chatwin; + + if (del_word_buf(ctx) == -1) { + sound_notify(self, error, 0, NULL); + return; + } +} + /* deletes entire line before cursor from input field and buffer */ static void input_discard(ToxWindow *self) { @@ -213,6 +224,10 @@ bool input_handle(ToxWindow *self, wint_t key, int x, int y, int mx_x, int mx_y) input_yank(self, x, mx_x); break; + case T_KEY_C_W: + input_del_word(self, x, mx_x); + break; + case KEY_HOME: case T_KEY_C_A: input_mv_home(self); diff --git a/src/toxic.h b/src/toxic.h index 836991d..1f214ea 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -63,6 +63,7 @@ #define T_KEY_C_H 0x08 /* ctrl-h */ #define T_KEY_C_Y 0x19 /* ctrl-y */ #define T_KEY_C_L 0x0C /* ctrl-l */ +#define T_KEY_C_W 0x17 /* ctrl-w */ #define T_KEY_TAB 0x09 /* TAB key */ #define ONLINE_CHAR "*" diff --git a/src/toxic_strings.c b/src/toxic_strings.c index a564f77..0709f13 100644 --- a/src/toxic_strings.c +++ b/src/toxic_strings.c @@ -121,6 +121,39 @@ int yank_buf(ChatContext *ctx) ctx->pos += ctx->yank_len; ctx->len += ctx->yank_len; ctx->line[ctx->len] = L'\0'; + + return 0; +} + +/* Deletes all characters from line starting at pos and going backwards + until we find a space or run out of characters. + Return 0 on success, -1 if nothing to delete */ +int del_word_buf(ChatContext *ctx) +{ + if (ctx->len == 0 || ctx->pos == 0) + return -1; + + int i = ctx->pos, count = 0; + + /* traverse past empty space */ + while (i > 0 && ctx->line[i-1] == L' ') { + ++count; + --i; + } + + /* traverse past last entered word */ + while (i > 0 && ctx->line[i-1] != L' ') { + ++count; + --i; + } + + wmemmove(&ctx->line[i], &ctx->line[ctx->pos], ctx->len - ctx->pos); + + ctx->start = MAX(0, ctx->start - count); /* TODO: take into account widechar */ + ctx->len -= count; + ctx->pos -= count; + ctx->line[ctx->len] = L'\0'; + return 0; } diff --git a/src/toxic_strings.h b/src/toxic_strings.h index 91408a8..3c957ef 100644 --- a/src/toxic_strings.h +++ b/src/toxic_strings.h @@ -49,6 +49,11 @@ void reset_buf(ChatContext *ctx); Return 0 on success, -1 if yank buffer is empty or too long */ int yank_buf(ChatContext *ctx); +/* Deletes all characters from line starting at pos and going backwards + until we find a space or run out of characters. + Return 0 on success, -1 if no line or already at the beginning */ +int del_word_buf(ChatContext *ctx); + /* Removes trailing spaces from line. */ void rm_trailing_spaces_buf(ChatContext *ctx);