Start implementing shortcut handling

This commit is contained in:
Kovid Goyal
2023-03-20 22:17:36 +05:30
parent 924cd4cadd
commit 425ab4f6d8
3 changed files with 68 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ package config
import (
"fmt"
"kitty/tools/tui/loop"
"kitty/tools/utils"
"regexp"
"strconv"
@@ -253,3 +254,37 @@ func ParseMap(val string) (*KeyAction, error) {
action_name, action_args, _ := strings.Cut(action, " ")
return &KeyAction{Name: action_name, Args: action_args, Normalized_keys: NormalizeShortcuts(spec)}, nil
}
type ShortcutTracker struct {
partial_matches []*KeyAction
partial_num_consumed int
}
func (self *ShortcutTracker) Match(ev *loop.KeyEvent, all_actions []*KeyAction) *KeyAction {
if self.partial_num_consumed > 0 {
ev.Handled = true
self.partial_matches = utils.Filter(self.partial_matches, func(ac *KeyAction) bool {
return self.partial_num_consumed < len(ac.Normalized_keys) && ev.MatchesPressOrRepeat(ac.Normalized_keys[self.partial_num_consumed])
})
if len(self.partial_matches) == 0 {
self.partial_num_consumed = 0
return nil
}
} else {
self.partial_matches = utils.Filter(all_actions, func(ac *KeyAction) bool {
return ev.MatchesPressOrRepeat(ac.Normalized_keys[0])
})
if len(self.partial_matches) == 0 {
return nil
}
ev.Handled = true
}
self.partial_num_consumed++
for _, x := range self.partial_matches {
if self.partial_num_consumed >= len(x.Normalized_keys) {
self.partial_num_consumed = 0
return x
}
}
return nil
}