add some more convenient keybindings

This commit is contained in:
Florian Stinglmayr 2019-07-19 15:25:40 +02:00
parent 85d6134d33
commit 288fb83ad4
4 changed files with 59 additions and 3 deletions

View File

@ -21,6 +21,9 @@ void ncdc_input_set_callback(ncdc_input_t i, ncdc_input_callback_t c, void *a);
/* keybinding functions /* keybinding functions
*/ */
void ncdc_input_kill_right(ncdc_input_t input);
void ncdc_input_kill_left(ncdc_input_t input);
void ncdc_input_kill_word_left(ncdc_input_t input);
void ncdc_input_backward(ncdc_input_t i); void ncdc_input_backward(ncdc_input_t i);
void ncdc_input_forward(ncdc_input_t i); void ncdc_input_forward(ncdc_input_t i);
void ncdc_input_delete(ncdc_input_t input); void ncdc_input_delete(ncdc_input_t input);

View File

@ -105,6 +105,46 @@ static void ncdc_input_enter(ncdc_input_t input)
input->cursor = 0; input->cursor = 0;
} }
void ncdc_input_kill_word_left(ncdc_input_t input)
{
ssize_t i = 0, j = 0;
return_if_true(input->cursor == 0,);
return_if_true(input->buffer->len == 0,);
i = input->cursor;
if (i == input->buffer->len) {
--i;
}
for (; i >= 0 && iswspace(g_array_index(input->buffer, wchar_t, i)); i--, j++)
;
for (; i >= 0 && !iswspace(g_array_index(input->buffer, wchar_t, i)); i--, j++)
;
if (i < 0) {
i = 0;
}
g_array_remove_range(input->buffer, i, j);
input->cursor = i;
}
void ncdc_input_kill_left(ncdc_input_t input)
{
return_if_true(input->cursor == 0,);
g_array_remove_range(input->buffer, 0, input->cursor);
input->cursor = 0;
}
void ncdc_input_kill_right(ncdc_input_t input)
{
return_if_true(input->cursor == input->buffer->len,);
g_array_remove_range(input->buffer, input->cursor,
input->buffer->len - input->cursor
);
}
void ncdc_input_delete(ncdc_input_t input) void ncdc_input_delete(ncdc_input_t input)
{ {
return_if_true(input->cursor == input->buffer->len,); return_if_true(input->cursor == input->buffer->len,);

View File

@ -9,7 +9,7 @@ ncdc_find_keybinding(ncdc_keybinding_t *keys, wchar_t const *key, size_t l)
size_t i = 0; size_t i = 0;
for (i = 0; keys[i].name != NULL; i++) { for (i = 0; keys[i].name != NULL; i++) {
if ((l == sizeof(wchar_t) && key[0] == keys[i].key[0]) || if ((l == 1 && key[0] == keys[i].key[0]) ||
wcscmp(key, keys[i].key) == 0) { wcscmp(key, keys[i].key) == 0) {
return keys+i; return keys+i;
} }
@ -41,6 +41,15 @@ ncdc_keybinding_t keys_emacs[] = {
/* CTRL+B /* CTRL+B
*/ */
NCDC_BINDING(L"\x02", L"backward", ncdc_input_backward), NCDC_BINDING(L"\x02", L"backward", ncdc_input_backward),
/* CTRL+K
*/
NCDC_BINDING(L"\x0B", L"kill-right", ncdc_input_kill_right),
/* CTRL+U
*/
NCDC_BINDING(L"\x15", L"kill-left", ncdc_input_kill_left),
/* CTRL+W
*/
NCDC_BINDING(L"\x17", L"kill-word-left", ncdc_input_kill_word_left),
/* CTRL+D /* CTRL+D
*/ */
NCDC_BINDING(L"\x04", L"delete", ncdc_input_delete), NCDC_BINDING(L"\x04", L"delete", ncdc_input_delete),

View File

@ -267,10 +267,14 @@ void ncdc_mainwindow_input_ready(ncdc_mainwindow_t n)
return; return;
} }
keylen = wcslen(key); keylen = wcslen(key);
FILE *f = fopen("keys.txt", "a+");
fwprintf(f, L"KEY: %ls\n", key);
fclose(f);
} }
FILE *f = fopen("keys.txt", "a+"); FILE *f = fopen("keys.txt", "a+");
fwprintf(f, L"%d - %ls\n", i, (key == NULL ? L"n/a" : &key[1])); fwprintf(f, L"%X\n", i);
fclose(f); fclose(f);
if (key != NULL && if (key != NULL &&
@ -281,7 +285,7 @@ void ncdc_mainwindow_input_ready(ncdc_mainwindow_t n)
if (n->focus == FOCUS_INPUT) { if (n->focus == FOCUS_INPUT) {
if (key == NULL) { if (key == NULL) {
ncdc_input_feed(n->in, (wchar_t const *)&i, sizeof(wchar_t)); ncdc_input_feed(n->in, (wchar_t const *)&i, 1);
} else { } else {
ncdc_input_feed(n->in, key, wcslen(key)); ncdc_input_feed(n->in, key, wcslen(key));
} }