From 4b86d9f487712e24b2288502f21c033a8d6790ad Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Tue, 9 May 2023 19:33:38 +0100 Subject: Add alt-j/k/n/p keybindings. Also generally clean up input handling a bit. --- doc/tofi.1.md | 5 ++-- doc/tofi.1.scd | 4 +-- src/input.c | 80 +++++++++++++++++++--------------------------------------- 3 files changed, 31 insertions(+), 58 deletions(-) diff --git a/doc/tofi.1.md b/doc/tofi.1.md index 925e59c..7af587b 100644 --- a/doc/tofi.1.md +++ b/doc/tofi.1.md @@ -40,11 +40,12 @@ the form **--key=value**. # KEYS -\ \| \ \| \-k \| \-p +\ \| \ \| \-k \| \-p \| \-k \| \-p > Move the selection back one entry. -\ \| \ \| \-j \| \-n \| \ +\ \| \ \| \-j \| \-n \| \-j \| +\-n \| \ > Move the selection forward one entry. diff --git a/doc/tofi.1.scd b/doc/tofi.1.scd index 7096d98..ccec46a 100644 --- a/doc/tofi.1.scd +++ b/doc/tofi.1.scd @@ -38,10 +38,10 @@ All config file options described in *tofi*(5) are also accepted, in the form # KEYS - | | -k | -p + | | -k | -p | -k | -p Move the selection back one entry. - | | -j | -n | + | | -j | -n | -j | -n | Move the selection forward one entry. diff --git a/src/input.c b/src/input.c index 573c5dd..7d9eb54 100644 --- a/src/input.c +++ b/src/input.c @@ -35,58 +35,43 @@ void input_handle_keypress(struct tofi *tofi, xkb_keycode_t keycode) const uint32_t key = keycode - 8; xkb_keysym_t sym = xkb_state_key_get_one_sym(tofi->xkb_state, keycode); - - uint32_t ch = xkb_state_key_get_utf32( + bool ctrl = xkb_state_mod_name_is_active( + tofi->xkb_state, + XKB_MOD_NAME_CTRL, + XKB_STATE_MODS_EFFECTIVE); + bool alt = xkb_state_mod_name_is_active( tofi->xkb_state, - keycode); - if (utf32_isprint(ch)) { + XKB_MOD_NAME_ALT, + XKB_STATE_MODS_EFFECTIVE); + + uint32_t ch = xkb_state_key_get_utf32(tofi->xkb_state, keycode); + /* + * Alt does not affect which character is selected, so we have to check + * for it explicitly. + */ + if (utf32_isprint(ch) && !alt) { add_character(tofi, keycode); - } else if ((sym == XKB_KEY_BackSpace || key == KEY_W) - && xkb_state_mod_name_is_active( - tofi->xkb_state, - XKB_MOD_NAME_CTRL, - XKB_STATE_MODS_EFFECTIVE)) - { + } else if ((sym == XKB_KEY_BackSpace || key == KEY_W) && ctrl) { delete_word(tofi); } else if (sym == XKB_KEY_BackSpace) { delete_character(tofi); - } else if (key == KEY_U - && xkb_state_mod_name_is_active( - tofi->xkb_state, - XKB_MOD_NAME_CTRL, - XKB_STATE_MODS_EFFECTIVE) - ) - { + } else if (key == KEY_U && ctrl) { clear_input(tofi); - } else if (key == KEY_V - && xkb_state_mod_name_is_active( - tofi->xkb_state, - XKB_MOD_NAME_CTRL, - XKB_STATE_MODS_EFFECTIVE) - ) - { + } else if (key == KEY_V && ctrl) { paste(tofi); } else if (sym == XKB_KEY_Left) { previous_cursor_or_result(tofi); } else if (sym == XKB_KEY_Right) { next_cursor_or_result(tofi); - } else if (sym == XKB_KEY_Up || sym == XKB_KEY_Left || sym == XKB_KEY_ISO_Left_Tab - || ((key == KEY_K || key == KEY_P) - && xkb_state_mod_name_is_active( - tofi->xkb_state, - XKB_MOD_NAME_CTRL, - XKB_STATE_MODS_EFFECTIVE) - ) - ) { + } else if (sym == XKB_KEY_Up + || sym == XKB_KEY_Left + || sym == XKB_KEY_ISO_Left_Tab + || ((key == KEY_K || key == KEY_P) && (ctrl || alt))) { select_previous_result(tofi); - } else if (sym == XKB_KEY_Down || sym == XKB_KEY_Right || sym == XKB_KEY_Tab - || ((key == KEY_J || key == KEY_N) - && xkb_state_mod_name_is_active( - tofi->xkb_state, - XKB_MOD_NAME_CTRL, - XKB_STATE_MODS_EFFECTIVE) - ) - ) { + } else if (sym == XKB_KEY_Down + || sym == XKB_KEY_Right + || sym == XKB_KEY_Tab + || ((key == KEY_J || key == KEY_N) && (ctrl || alt))) { select_next_result(tofi); } else if (sym == XKB_KEY_Home) { reset_selection(tofi); @@ -95,20 +80,7 @@ void input_handle_keypress(struct tofi *tofi, xkb_keycode_t keycode) } else if (sym == XKB_KEY_Page_Down) { select_next_page(tofi); } else if (sym == XKB_KEY_Escape - || (key == KEY_C - && xkb_state_mod_name_is_active( - tofi->xkb_state, - XKB_MOD_NAME_CTRL, - XKB_STATE_MODS_EFFECTIVE) - ) - || (key == KEY_LEFTBRACE - && xkb_state_mod_name_is_active( - tofi->xkb_state, - XKB_MOD_NAME_CTRL, - XKB_STATE_MODS_EFFECTIVE) - ) - ) - { + || ((key == KEY_C || key == KEY_LEFTBRACE) && ctrl)) { tofi->closed = true; return; } else if (sym == XKB_KEY_Return || sym == XKB_KEY_KP_Enter) { -- cgit v1.2.3