1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-06-26 20:37:46 +02:00

implement word deletion via ^W

This commit is contained in:
Jfreegman 2014-10-05 23:12:58 -04:00
parent e5d45fdf1d
commit 43552161f9
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
5 changed files with 57 additions and 3 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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 "*"

View File

@ -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;
}

View File

@ -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);