package keymap import ( "encoding/json" "100gex/internal/rex" ) // Binding is one resolved key binding, as client.keymap returns it. type Binding struct { // Keys are the keys pressed, in order: one for a plain shortcut, more // for a sequence. Keys []string `json:"keys"` Action string `json:"action"` Args json.RawMessage `json:"args,omitempty"` // Mode is the mode the binding belongs to, or "" for none. Mode string `json:"mode,omitempty"` Source string `json:"source,omitempty"` } // Mode is a key mode: bindings that apply only while it is active. type Mode struct { Name string `json:"name"` // Exclusive modes ignore keys they don't bind. Exclusive bool `json:"exclusive"` // Blocked keys do nothing while the mode is active, unless the mode // binds them. Blocked [][]string `json:"blocked"` } // binding is a Binding with its keys parsed. A binding whose keys don't // parse, such as one a newer client wrote, never matches. type binding struct { *Binding chords []Chord } // Keymap is a set of resolved bindings, in the order to try them. type Keymap struct { bindings []binding modes map[string]Mode blocked map[string][]Chord // single blocked keys, by mode } // New parses resolved bindings and modes. func New(bindings []Binding, modes []Mode) *Keymap { km := &Keymap{modes: make(map[string]Mode), blocked: make(map[string][]Chord)} for i := range bindings { b := binding{Binding: &bindings[i]} for _, k := range b.Keys { c, err := ParseChord(k) if err != nil { b.chords = nil break } b.chords = append(b.chords, c) } if len(b.chords) > 0 { km.bindings = append(km.bindings, b) } } for _, m := range modes { km.modes[m.Name] = m for _, keys := range m.Blocked { if len(keys) != 1 { continue } if c, err := ParseChord(keys[0]); err == nil { km.blocked[m.Name] = append(km.blocked[m.Name], c) } } } return km } // Outcome is what a key press does. type Outcome int const ( // Pass: no binding took the key, so it goes to the focused block. Pass Outcome = iota // Consumed: the key started or continued a sequence, or was used up // some other way, such as cancelling a sequence or leaving a mode. Consumed // Run: the key completed a binding, which Result.Binding holds. Run ) // Result is what Matcher.Key decided. type Result struct { Outcome Outcome // Binding is the binding to run, for Run. Binding *Binding // Prefix holds the keys before the last one, for a sequence, as the // binding spells them. client.key takes them for a sequence bound to a // Lua function. Prefix []string } // Matcher follows the keys a person types against a keymap: it keeps the // active modes and any unfinished sequence. It is not safe for concurrent // use. type Matcher struct { km *Keymap // Available reports whether this client can perform an action. A // binding to an action it can't perform is skipped, so the next binding // on the same key gets its chance. Available func(action string) bool modes []activeMode // innermost last candidates []binding // bindings that the keys so far begin depth int // keys of the sequence typed so far } type activeMode struct { name string once bool } // NewMatcher returns a matcher for km. func NewMatcher(km *Keymap, available func(string) bool) *Matcher { return &Matcher{km: km, Available: available} } // SetKeymap replaces the keymap, as after the configuration file changes. It // cancels an unfinished sequence and leaves modes the new keymap lacks. func (m *Matcher) SetKeymap(km *Keymap) { m.km = km m.candidates, m.depth = nil, 0 modes := m.modes[:0] for _, am := range m.modes { if _, ok := km.modes[am.name]; ok { modes = append(modes, am) } } m.modes = modes } // EnterMode makes a mode's bindings apply until it is left. With once, it // is left after its first binding runs. func (m *Matcher) EnterMode(name string, once bool) { m.modes = append(m.modes, activeMode{name, once}) } // ExitMode leaves the most recently entered mode. func (m *Matcher) ExitMode() { if len(m.modes) > 0 { m.modes = m.modes[:len(m.modes)-1] } } // ExitAllModes leaves every mode. func (m *Matcher) ExitAllModes() { m.modes = nil } // Modes returns the active modes, innermost last. func (m *Matcher) Modes() []string { names := make([]string, len(m.modes)) for i, am := range m.modes { names[i] = am.name } return names } // Pending returns the keys of an unfinished sequence, to show people. func (m *Matcher) Pending() []string { if m.depth == 0 || len(m.candidates) == 0 { return nil } return m.candidates[0].Keys[:m.depth] } // Key decides what a key press does. func (m *Matcher) Key(k rex.KeyEvent) Result { if m.depth > 0 { return m.continueSequence(k) } // The innermost mode is tried first, then the ones it was entered from, // then the bindings outside every mode. An exclusive mode stops there. scopes := make([]string, 0, len(m.modes)+1) exclusive := false for i := len(m.modes) - 1; i >= 0 && !exclusive; i-- { scopes = append(scopes, m.modes[i].name) exclusive = m.km.modes[m.modes[i].name].Exclusive } if !exclusive { scopes = append(scopes, "") } for _, scope := range scopes { if r, ok := m.start(scope, k); ok { return r } if scope != "" && m.blocks(scope, k) { return Result{Outcome: Consumed} } } // Escape leaves the innermost mode, unless a binding took it. if len(m.modes) > 0 && isEscape(k) { m.ExitMode() return Result{Outcome: Consumed} } if exclusive { return Result{Outcome: Consumed} } return Result{Outcome: Pass} } // start tries the first key of the bindings in one mode, or outside every // mode for "". func (m *Matcher) start(mode string, k rex.KeyEvent) (Result, bool) { var matching []binding for _, b := range m.km.bindings { if b.Mode == mode && b.chords[0].Matches(k) && m.available(b.Action) { matching = append(matching, b) } } if len(matching) == 0 { return Result{}, false } return m.advance(matching), true } // advance acts on the bindings that the keys so far match, in try order. // The first decides: if it is complete it runs, and otherwise the keys // begin a sequence. func (m *Matcher) advance(matching []binding) Result { first := matching[0] if len(first.chords) == m.depth+1 { r := Result{Outcome: Run, Binding: first.Binding, Prefix: first.Keys[:m.depth]} m.candidates, m.depth = nil, 0 m.ranBinding() return r } m.depth++ m.candidates = m.candidates[:0] for _, b := range matching { if len(b.chords) > m.depth { m.candidates = append(m.candidates, b) } } return Result{Outcome: Consumed} } func (m *Matcher) continueSequence(k rex.KeyEvent) Result { var matching []binding for _, b := range m.candidates { if b.chords[m.depth].Matches(k) { matching = append(matching, b) } } if len(matching) > 0 { return m.advance(matching) } m.candidates, m.depth = nil, 0 // Escape cancels the sequence. Any other key that doesn't continue it // drops the keys typed so far and goes to the block as typed; it isn't // tried as the start of another shortcut. if isEscape(k) { return Result{Outcome: Consumed} } return Result{Outcome: Pass} } // ranBinding leaves a mode entered with once, after its first binding runs. func (m *Matcher) ranBinding() { if n := len(m.modes); n > 0 && m.modes[n-1].once { m.modes = m.modes[:n-1] } } func (m *Matcher) blocks(mode string, k rex.KeyEvent) bool { for _, c := range m.km.blocked[mode] { if c.Matches(k) { return true } } return false } func (m *Matcher) available(action string) bool { return m.Available == nil || m.Available(action) }