From 197936e69767f92eb33998d1ab73b155383ccfc8 Mon Sep 17 00:00:00 2001 From: Luca Nimmrichter Date: Thu, 20 Oct 2022 16:53:06 +0200 Subject: [PATCH] Add new bindings H/M/L to goto first/mid/last item in view X to delete character before the cursor v to toggle item selection Change goto first item from g to gg --- lib/vim.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/lib/vim.c b/lib/vim.c index ec5cfcf..64d38b0 100644 --- a/lib/vim.c +++ b/lib/vim.c @@ -69,6 +69,29 @@ static void menu_last(struct bm_menu *menu, uint32_t count){ } } +static void menu_view_high(struct bm_menu *menu, uint32_t count){ + if(count > 0){ + uint32_t nth_view_item = menu->index % menu->lines; + bm_menu_set_highlighted_index(menu, menu->index - nth_view_item); + } +} + +static void menu_view_mid(struct bm_menu *menu, uint32_t count, uint32_t displayed){ + if(count > 0){ + uint32_t nth_view_item = menu->index % menu->lines; + uint32_t half_view_height = (displayed - 1) / 2; + bm_menu_set_highlighted_index(menu, menu->index - nth_view_item + half_view_height); + } +} + +static void menu_view_low(struct bm_menu *menu, uint32_t count, uint32_t displayed){ + if(count > 0){ + uint32_t nth_view_item = menu->index % menu->lines; + uint32_t view_bottom_offset = (displayed - 2) - nth_view_item; + bm_menu_set_highlighted_index(menu, menu->index + view_bottom_offset); + } +} + static void menu_page_down(struct bm_menu *menu, uint32_t count, uint32_t displayed){ bm_menu_set_highlighted_index(menu, (menu->index + displayed >= count ? count - 1 : menu->index + (displayed - 1))); } @@ -113,6 +136,16 @@ static void delete_char(struct bm_menu *menu){ } } +static void delete_char_back(struct bm_menu *menu){ + if(menu->filter){ + if(menu->cursor > 0){ + size_t width; + bm_utf8_rune_remove(menu->filter, menu->cursor, &width); + move_left(menu); + } + } +} + static void delete_word(struct bm_menu *menu){ if(!menu->filter) return; @@ -159,6 +192,16 @@ static void delete_to_line_start(struct bm_menu *menu){ } } +static void toggle_item_selection(struct bm_menu *menu){ + struct bm_item *highlighted = bm_menu_get_highlighted_item(menu); + if (highlighted){ + if(!bm_menu_item_is_selected(menu, highlighted)){ + list_add_item(&menu->selection, highlighted); + } else { + list_remove_item(&menu->selection, highlighted); + } + } +} enum bm_vim_code bm_vim_key_press(struct bm_menu *menu, enum bm_key key, uint32_t unicode, uint32_t item_count, uint32_t items_displayed){ if(key == BM_KEY_ESCAPE && unicode == 99) return BM_VIM_EXIT; @@ -186,6 +229,10 @@ static enum bm_vim_code vim_on_first_key(struct bm_menu *menu, uint32_t unicode, case 'q': LOG("quit\n"); return BM_VIM_EXIT; + case 'v': + LOG("toggle item selection\n"); + toggle_item_selection(menu); + goto action_executed; case 'i': LOG("insert mode\n"); goto insert_action_executed; @@ -237,6 +284,10 @@ static enum bm_vim_code vim_on_first_key(struct bm_menu *menu, uint32_t unicode, LOG("delete char\n"); delete_char(menu); goto action_executed; + case 'X': + LOG("delete char back\n"); + delete_char_back(menu); + goto action_executed; case '0': LOG("move line start\n"); move_line_start(menu); @@ -245,14 +296,22 @@ static enum bm_vim_code vim_on_first_key(struct bm_menu *menu, uint32_t unicode, LOG("move line end\n"); move_line_end(menu); goto action_executed; - case 'g': - LOG("move line end\n"); - menu_first(menu, item_count); - goto action_executed; case 'G': - LOG("move line end\n"); + LOG("menu last\n"); menu_last(menu, item_count); goto action_executed; + case 'H': + LOG("menu view high\n"); + menu_view_high(menu, item_count); + goto action_executed; + case 'M': + LOG("menu view mid\n"); + menu_view_mid(menu, item_count, items_displayed); + goto action_executed; + case 'L': + LOG("menu view low\n"); + menu_view_low(menu, item_count, items_displayed); + goto action_executed; case 'F': LOG("menu page down\n"); menu_page_down(menu, item_count, items_displayed); @@ -263,6 +322,7 @@ static enum bm_vim_code vim_on_first_key(struct bm_menu *menu, uint32_t unicode, goto action_executed; case 'c': case 'd': + case 'g': menu->vim_last_key = unicode; return BM_VIM_CONSUME; default: @@ -324,6 +384,13 @@ static enum bm_vim_code vim_on_second_key(struct bm_menu *menu, uint32_t unicode delete_to_line_start(menu); goto insert_action_executed; } + } else if(menu->vim_last_key == 'g'){ + switch(unicode){ + case 'g': + LOG("menu first\n"); + menu_first(menu, item_count); + goto action_executed; + } } return vim_on_first_key(menu, unicode, item_count, items_displayed);