diff --git a/docs/changelog.rst b/docs/changelog.rst index 9fe1458a7..294056c7e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -278,6 +278,9 @@ Detailed list of changes :opt:`scrollbar_handle_color` and :opt:`scrollbar_track_color` and not accepting their special values such as ``background`` +- Command palette: Show action and kitten aliases at the top when the search + query is empty, so they can be used to pin frequently used actions (:iss:`10711`) + - ssh kitten: Fix hanging when re-using a shared connection whose connection to the server has silently died, for example, after a network change or suspend. Now the kitten detects this and reconnects (:iss:`10691`) diff --git a/docs/kittens/command-palette.rst b/docs/kittens/command-palette.rst index 7682d70b9..512f5acd9 100644 --- a/docs/kittens/command-palette.rst +++ b/docs/kittens/command-palette.rst @@ -121,6 +121,19 @@ configured a shortcut for. You can run them directly from the palette, or note the action name and add a mapping in :file:`kitty.conf`. +Pinning frequently used actions +--------------------------------- + +When the search query is empty, actions defined with :opt:`action_alias` and +:opt:`kitten_alias` are shown at the top of the palette, above all other +actions. They are shown even when unmapped actions are hidden with :kbd:`F12`. +To keep actions you use often at the top, define an alias for each of them in +:file:`kitty.conf`, for example:: + + action_alias fav_session launch --type=background kitty --session ~/.config/kitty/last.session + action_alias fav_detach detach_tab ask + + Custom keyboard modes ----------------------- diff --git a/kittens/command_palette/main.go b/kittens/command_palette/main.go index d26086686..54d8cfea5 100644 --- a/kittens/command_palette/main.go +++ b/kittens/command_palette/main.go @@ -331,10 +331,9 @@ func bindingToDisplayItem(b Binding) DisplayItem { if keyText == "" { keyText = unmappedLabel } + // For aliases, ActionDisplay already starts with the alias name, so it is + // not prepended here. actionText := b.ActionDisplay - if b.Alias != "" { - actionText = b.Alias + " " + actionText - } return DisplayItem{ binding: b, keyText: keyText, @@ -412,6 +411,12 @@ func (h *Handler) flattenBindings() { } } +// isHidden reports whether an item is hidden by the show_unmapped toggle. +// Aliases are user defined, so they are always shown, even when unmapped. +func (h *Handler) isHidden(item *DisplayItem) bool { + return !h.show_unmapped && item.binding.Key == "" && item.binding.Alias == "" +} + // updateFilter rebuilds the filtered item list based on the current query. func (h *Handler) updateFilter() { tokens := tokenizeQuery(h.query) @@ -419,8 +424,8 @@ func (h *Handler) updateFilter() { if len(tokens) == 0 { // Show all items in original order, respecting the show_unmapped toggle h.filtered_idx = make([]int, 0, len(h.all_items)) - for i, item := range h.all_items { - if !h.show_unmapped && item.binding.Key == "" { + for i := range h.all_items { + if h.isHidden(&h.all_items[i]) { continue } h.filtered_idx = append(h.filtered_idx, i) @@ -433,7 +438,7 @@ func (h *Handler) updateFilter() { var matches []scoredItem for i := range h.all_items { - if !h.show_unmapped && h.all_items[i].binding.Key == "" { + if h.isHidden(&h.all_items[i]) { continue } item := &h.all_items[i] diff --git a/kittens/command_palette/main.py b/kittens/command_palette/main.py index 10785eb1f..3e6629815 100644 --- a/kittens/command_palette/main.py +++ b/kittens/command_palette/main.py @@ -273,6 +273,19 @@ def add_alias_sections(opts: Any, modes: dict[str, dict[str, list[dict[str, str] default_mode_cats[section_name] = existing +def move_alias_sections_to_top(modes: dict[str, dict[str, list[dict[str, str]]]]) -> None: + """Put the user defined alias sections first in the default mode.""" + default_mode_cats = modes.get('') + if not default_mode_cats: + return + ordered: dict[str, list[dict[str, str]]] = {} + for section_name in ('Action aliases', 'Kitten aliases'): + if section_name in default_mode_cats: + ordered[section_name] = default_mode_cats.pop(section_name) + ordered.update(default_mode_cats) + modes[''] = ordered + + def collect_mouse_bindings(opts: Any) -> list[dict[str, str]]: """Collect mouse mappings.""" mouse: list[dict[str, str]] = [] @@ -290,6 +303,7 @@ def collect_keys_data(opts: Any) -> dict[str, Any]: relocate_mode_entry_bindings(modes) add_unmapped_actions(modes) add_alias_sections(opts, modes) + move_alias_sections_to_top(modes) mode_order = list(modes.keys()) category_order: dict[str, list[str]] = {} diff --git a/kittens/command_palette/main_test.go b/kittens/command_palette/main_test.go index 1ce25e3a4..9695aafa5 100644 --- a/kittens/command_palette/main_test.go +++ b/kittens/command_palette/main_test.go @@ -753,6 +753,59 @@ func TestShowUnmappedToggle(t *testing.T) { } } +func TestUnmappedAliasesAlwaysShown(t *testing.T) { + h := newBuilder(). + addBinding("", "Action aliases", Binding{ + Action: "launch", ActionDisplay: "launch_bg", + Definition: "launch --type=background", Alias: "launch_bg", + }). + addBinding("", "Copy/paste", testBinding("ctrl+c", "copy", "Copy")). + addBinding("", "Copy/paste", testBinding("", "paste_from_buffer", "Paste from buffer")). + build() + h.show_unmapped = false + + h.updateFilter() + if len(h.filtered_idx) != 2 { + t.Fatalf("With show_unmapped=false, expected the alias and the mapped item, got %d items", len(h.filtered_idx)) + } + if h.all_items[h.filtered_idx[0]].binding.Alias != "launch_bg" { + t.Fatal("Unmapped alias should be shown first when show_unmapped=false") + } + + h.query = "launch" + h.updateFilter() + if len(h.filtered_idx) != 1 || h.all_items[h.filtered_idx[0]].binding.Alias != "launch_bg" { + t.Fatal("Unmapped alias should be found by search when show_unmapped=false") + } +} + +func TestAliasNotDuplicatedInActionText(t *testing.T) { + h := newBuilder(). + addBinding("", "Action aliases", Binding{ + Key: "f5", Action: "detach_tab", ActionDisplay: "fav_detach", + Definition: "fav_detach", Alias: "fav_detach", + }). + addBinding("", "Kitten aliases", Binding{ + Action: "kitten", ActionDisplay: "kitten hints", + Definition: "kitten hints --hints-offset=0", Alias: "kitten hints", + }). + build() + for _, want := range []string{"fav_detach", "kitten hints"} { + found := false + for _, item := range h.all_items { + if item.binding.Alias == want { + found = true + if item.actionText != want { + t.Fatalf("actionText for alias %q is %q, expected the alias exactly once", want, item.actionText) + } + } + } + if !found { + t.Fatalf("Alias %q not found in items", want) + } + } +} + // newMultiTokenTestHandler creates a handler with items designed to test // multi-token search. It has items where different tokens match different // columns, enabling cross-column and RRF ranking tests. diff --git a/kitty_tests/command_palette.py b/kitty_tests/command_palette.py index 852d07874..805a45729 100644 --- a/kitty_tests/command_palette.py +++ b/kitty_tests/command_palette.py @@ -169,6 +169,48 @@ class TestCommandPalette(BaseTest): for b in bindings: self.assertNotEqual(b.get('alias'), 'launch_tab', f'Alias binding should not appear in {cat_name!r}') + def test_alias_sections_come_first(self): + from kittens.command_palette.main import collect_keys_data + from kitty.options.utils import ActionAlias, AliasMap + + opts = self.set_options() + alias_map = AliasMap() + alias_map.append('launch_bg', ActionAlias('launch_bg', 'launch --type=background')) + alias_map.append('kitten', ActionAlias('hints', 'hints --hints-offset=0', replace_second_arg=True)) + opts.alias_map = alias_map + data = collect_keys_data(opts) + cat_names = list(data['modes'][''].keys()) + self.ae(cat_names[:2], ['Action aliases', 'Kitten aliases']) + self.ae(data['category_order'][''][:2], ['Action aliases', 'Kitten aliases']) + + # Without aliases there are no alias sections at the top + opts = self.set_options() + opts.alias_map = AliasMap() + data = collect_keys_data(opts) + self.assertNotIn('Action aliases', data['modes']['']) + self.assertNotIn('Kitten aliases', data['modes']['']) + + def test_alias_entries_display_alias_name(self): + # The Go side relies on action_display starting with the alias name + # for every entry that has an alias, so that it does not need to add it. + from kittens.command_palette.main import collect_keys_data + from kitty.options.utils import ActionAlias, AliasMap, parse_map + + opts = self.set_options() + alias_map = AliasMap() + alias_map.append('launch_tab', ActionAlias('launch_tab', 'launch --type=tab --cwd=current')) + alias_map.append('launch_bg', ActionAlias('launch_bg', 'launch --type=background')) + alias_map.append('kitten', ActionAlias('hints', 'hints --hints-offset=0', replace_second_arg=True)) + opts.alias_map = alias_map + for kd in parse_map('f1 launch_tab vim'): + kd = kd.resolve_and_copy(opts.kitty_mod) + opts.keyboard_modes[''].keymap.setdefault(kd.trigger, []).append(kd) + data = collect_keys_data(opts) + alias_entries = [b for cats in data['modes'].values() for bindings in cats.values() for b in bindings if 'alias' in b] + self.ae({b['alias'] for b in alias_entries}, {'launch_tab', 'launch_bg', 'kitten hints'}) + for b in alias_entries: + self.assertTrue(b['action_display'].startswith(b['alias']), f'{b["action_display"]!r} does not start with {b["alias"]!r}') + def test_kitten_alias_section(self): from kittens.command_palette.main import collect_keys_data from kitty.options.utils import ActionAlias, AliasMap