feat(shortcuts): add a leader key tree and palette remapping

Expose the multi-key mappings starting at the configured leader key in a
which-key panel, so that continuations are discoverable as soon as the prefix
is pressed. Both the panel and the command palette read the same structured
mapping data, and the palette can now write a new mapping for the selected
action.
This commit is contained in:
BlackAngelTV committed 2026-11-13 08:03:03 +01:00
1 parent dc13716431
commit bfc97997a1
10 files changed
+418 -13

No files matched your search

+162 -11
View File
@@ -38,6 +38,15 @@ type InputData struct {
Mouse []Binding `json:"mouse"`
ModeOrder []string `json:"mode_order"`
CategoryOrder map[string][]string `json:"category_order"`
Leader *LeaderData `json:"leader"`
}
// LeaderData is the which-key tree shown when the configured leader key is
// pressed. Key holds the key to press after the leader, not the full shortcut.
type LeaderData struct {
Prefix string `json:"prefix"`
Title string `json:"title"`
Bindings []Binding `json:"bindings"`
}
// wordToken represents a single word extracted from column text, with its
@@ -277,6 +286,11 @@ type Handler struct {
keyboard_shortcuts []*config.KeyAction
read_only bool
data_file string
leader *LeaderData
remap_mode bool
remap_binding *Binding
remap_key string
remap_action string
}
// initialize sets up the TUI: screen size, cursor, cached settings, and initial data load.
@@ -293,11 +307,20 @@ func (h *Handler) initialize() (string, error) {
if h.read_only {
title = "Keyboard shortcuts"
}
if h.input_data.Leader != nil {
title = h.input_data.Leader.Title
if title == "" {
title = "Leader keys"
}
}
h.lp.SetWindowTitle(title)
if h.read_only {
// A reference panel is about bindings, not about actions without one.
h.show_unmapped = false
} else if h.input_data.Leader != nil {
// The leader panel lists exactly the keys that are available.
h.show_unmapped = true
} else {
// Initialize with ShowUnmapped: true as the default; Load() returns this
// default when no cache file exists yet.
@@ -350,10 +373,26 @@ func (h *Handler) parseData(data []byte) error {
return fmt.Errorf("failed to parse input data: %w", err)
}
if h.input_data.Leader != nil {
h.leader = h.input_data.Leader
h.flattenLeaderBindings()
return nil
}
h.flattenBindings()
return nil
}
// flattenLeaderBindings turns the leader tree produced by Python into display
// items. The key column already holds only the key to press after the leader.
func (h *Handler) flattenLeaderBindings() {
for _, b := range h.input_data.Leader.Bindings {
b.Category = "Leader"
b.Mode = ""
b.IsMouse = false
h.all_items = append(h.all_items, bindingToDisplayItem(b))
}
}
// bindingToDisplayItem converts a Binding into a DisplayItem with pre-tokenized
// words for word-level matching.
func bindingToDisplayItem(b Binding) DisplayItem {
@@ -595,7 +634,9 @@ func (h *Handler) draw_screen() {
h.lp.QueueWriteString(h.query)
// Draw results
if h.query == "" {
if h.leader != nil {
h.drawFlatResults(resultsStartY, resultsHeight, width)
} else if h.query == "" {
h.drawGroupedResults(resultsStartY, resultsHeight, width)
} else {
h.drawFlatResults(resultsStartY, resultsHeight, width)
@@ -615,12 +656,24 @@ func (h *Handler) draw_screen() {
unmappedToggleLabel = "Hide"
}
footer := ""
if !h.read_only {
footer = h.lp.SprintStyled("fg=bright-yellow", "[Enter]") + " Run "
if h.leader != nil {
footer = h.lp.SprintStyled("fg=bright-yellow", "[Esc]") + " Close " +
h.lp.SprintStyled("fg=bright-yellow", "\u2191\u2193") + " Navigate " +
h.lp.SprintStyled("fg=bright-yellow", "[Enter]") + " Run or press the next key"
} else if h.remap_mode {
footer = h.lp.SprintStyled("fg=bright-yellow", "Press the new shortcut") + " " +
h.lp.SprintStyled("fg=bright-yellow", "[Esc]") + " Cancel"
} else {
if !h.read_only {
footer = h.lp.SprintStyled("fg=bright-yellow", "[Enter]") + " Run "
}
footer += h.lp.SprintStyled("fg=bright-yellow", "[Esc]") + " Quit " +
h.lp.SprintStyled("fg=bright-yellow", "\u2191\u2193") + " Navigate " +
h.lp.SprintStyled("fg=bright-yellow", "[F12]") + " " + unmappedToggleLabel + " unmapped"
if !h.read_only {
footer += " " + h.lp.SprintStyled("fg=bright-yellow", "[Ctrl+R]") + " Remap"
}
}
footer += h.lp.SprintStyled("fg=bright-yellow", "[Esc]") + " Quit " +
h.lp.SprintStyled("fg=bright-yellow", "\u2191\u2193") + " Navigate " +
h.lp.SprintStyled("fg=bright-yellow", "[F12]") + " " + unmappedToggleLabel + " unmapped"
matchCount := ""
if h.query != "" {
matchCount = fmt.Sprintf(" %d/%d", len(h.filtered_idx), len(h.all_items))
@@ -695,7 +748,9 @@ func (h *Handler) drawFlatResults(startY, maxRows, width int) {
b := &item.binding
keyDisplay := keyDisplayText(b)
catSuffix := ""
if b.Mode != "" {
if h.leader != nil {
// Leader rows have no category to disambiguate.
} else if b.Mode != "" {
catSuffix = fmt.Sprintf(" [%s/%s]", b.Mode, b.Category)
} else {
catSuffix = fmt.Sprintf(" [%s]", b.Category)
@@ -861,14 +916,14 @@ func (h *Handler) drawBindingLine(filteredIdx, width int, isSelected bool) {
}
// Render category suffix (only present in flat / search-results mode)
if h.query != "" {
if h.query != "" && h.leader == nil {
h.drawCategorySuffix(b, mi, baseStyle, matchStyle)
}
// For selected rows, pad the rest of the line with reverse video
if isSelected {
rendered := 4 + wcswidth.Stringwidth(keyDisplay) + paddingLen + 1 + wcswidth.Stringwidth(actionDisplay)
if h.query != "" {
if h.query != "" && h.leader == nil {
rendered += categorySuffixWidth(b)
}
if pad := width - rendered; pad > 0 {
@@ -916,6 +971,25 @@ func (h *Handler) onMouseEvent(ev *loop.MouseEvent) error {
// onKeyEvent handles keyboard input for navigation, selection, and query editing.
func (h *Handler) onKeyEvent(ev *loop.KeyEvent) error {
if h.remap_mode {
ev.Handled = true
if ev.MatchesPressOrRepeat("escape") || ev.MatchesPressOrRepeat("ctrl+c") {
h.remap_mode, h.remap_binding = false, nil
h.draw_screen()
return nil
}
if ev.Type&(loop.PRESS|loop.REPEAT) == 0 {
return nil
}
spec := keySpecForRemap(ev)
if spec == "" || h.remap_binding == nil {
h.lp.Beep()
return nil
}
h.remap_key, h.remap_action = spec, h.remap_binding.Definition
h.lp.Quit(0)
return nil
}
if ev.MatchesPressOrRepeat("escape") {
ev.Handled = true
if h.query != "" {
@@ -996,6 +1070,24 @@ func (h *Handler) onKeyEvent(ev *loop.KeyEvent) error {
}
return nil
}
if ev.MatchesPressOrRepeat("ctrl+r") && !h.read_only && h.leader == nil {
ev.Handled = true
if b := h.selectedBinding(); b != nil && !b.IsMouse {
h.remap_mode, h.remap_binding = true, b
h.draw_screen()
} else {
h.lp.Beep()
}
return nil
}
if h.leader != nil {
if b := h.matchLeaderBinding(ev); b != nil {
ev.Handled = true
h.leader_trigger(b)
return nil
}
return nil
}
if ev.MatchesPressOrRepeat("f12") {
ev.Handled = true
h.show_unmapped = !h.show_unmapped
@@ -1010,8 +1102,19 @@ func (h *Handler) onKeyEvent(ev *loop.KeyEvent) error {
return nil
}
// onText handles typed characters, appending them to the search query.
// onText handles typed characters, appending them to the search query, or
// running the binding whose key was typed in the leader panel.
func (h *Handler) onText(text string, from_key_event bool, in_bracketed_paste bool) error {
if h.leader != nil {
for i := range h.all_items {
if b := &h.all_items[i].binding; b.Key != "" && strings.EqualFold(b.Key, text) {
h.leader_trigger(b)
return nil
}
}
h.lp.Beep()
return nil
}
h.query += text
h.updateFilter()
h.draw_screen()
@@ -1036,6 +1139,49 @@ func (h *Handler) moveSelection(delta int) {
h.draw_screen()
}
// matchLeaderBinding finds the leader entry whose key matches the event.
func (h *Handler) matchLeaderBinding(ev *loop.KeyEvent) *Binding {
for i := range h.all_items {
if b := &h.all_items[i].binding; b.Key != "" && ev.MatchesPressOrRepeat(b.Key) {
return b
}
}
return nil
}
// leader_trigger runs a leader entry immediately, without waiting for the rest
// of a longer multi-key sequence.
func (h *Handler) leader_trigger(b *Binding) {
h.result = b.Definition
h.lp.Quit(0)
}
// keySpecForRemap renders a key event in kitty.conf syntax so that the palette
// can write a map directive for it.
func keySpecForRemap(ev *loop.KeyEvent) string {
mods := ev.Mods.WithoutLocks()
parts := make([]string, 0, 4)
for _, m := range []struct {
mod loop.KeyModifiers
name string
}{{loop.CTRL, "ctrl"}, {loop.ALT, "alt"}, {loop.SHIFT, "shift"}, {loop.SUPER, "super"}, {loop.HYPER, "hyper"}, {loop.META, "meta"}} {
if mods&m.mod != 0 {
parts = append(parts, m.name)
}
}
key := ev.Key
if key == "" {
key = ev.Text
}
if key == "" {
return ""
}
if len(key) == 1 && key[0] >= 'A' && key[0] <= 'Z' {
key = strings.ToLower(key)
}
return strings.Join(append(parts, key), "+")
}
// triggerSelected sets the selected binding's definition as the result and exits.
func (h *Handler) triggerSelected() {
if h.read_only {
@@ -1085,7 +1231,12 @@ func main(cmd *cli.Command, opts *Options, args []string) (rc int, err error) {
return
}
rc = lp.ExitCode()
if handler.result != "" {
if handler.remap_key != "" {
s, serr := output(map[string]string{"remap_key": handler.remap_key, "remap_action": handler.remap_action})
if serr == nil {
fmt.Println(s)
}
} else if handler.result != "" {
s, serr := output(map[string]string{"action": handler.result})
if serr == nil {
fmt.Println(s)