package keybind import ( "fmt" "strings" ) // Source tells where an effective binding comes from. type Source int const ( Default Source = iota // Ghostty's built-in binding Changed // a built-in trigger the user bound to another action Custom // a user binding on a trigger without built-in binding Unbound // a built-in binding the user removed ) func (s Source) String() string { switch s { case Changed: return "changed" case Custom: return "custom" case Unbound: return "unbound" } return "default" } // Entry is one parsed `keybind` value. type Entry struct { Value string Flags string // e.g. "global:" Trigger string // without flags, as written Norm string // canonical identity of the trigger Action string Clear bool // "keybind = clear" Chain bool // "keybind = chain=ACTION" } // ParseEntry parses a raw `keybind` config value. func ParseEntry(v string) Entry { v = strings.TrimSpace(v) if len(v) >= 2 && v[0] == '"' && v[len(v)-1] == '"' { v = v[1 : len(v)-1] } e := Entry{Value: v} if v == "clear" { e.Clear = true return e } t, a := Split(v) e.Action = a if t == "chain" { e.Chain = true return e } e.Flags, e.Trigger = StripFlags(t) e.Norm = NormalizeLoose(t) return e } // Binding is one row of the effective key binding table. type Binding struct { Norm string Trigger string // as written, including flag prefixes Action string Chain []string // actions chained with "chain=" Source Source Default string // Ghostty's built-in action for this trigger, if any InConfig bool // the user's config has an entry for this trigger } // group is a binding entry together with the "chain=" entries after it. type group struct { entries []string e Entry } func (g group) chain() []string { return g.entries[1:] } // Set is the user's keybind entries on top of Ghostty's defaults. type Set struct { defaults []Entry defByNorm map[string]Entry groups []group } // NewSet builds a set from Ghostty's default values and the user's values. func NewSet(defaults, user []string) *Set { s := &Set{defByNorm: map[string]Entry{}} for _, d := range defaults { e := ParseEntry(d) if e.Norm == "" || e.Action == "" { continue } if _, dup := s.defByNorm[e.Norm]; !dup { s.defaults = append(s.defaults, e) } s.defByNorm[e.Norm] = e } for _, v := range user { e := ParseEntry(v) if e.Chain && len(s.groups) > 0 && !s.groups[len(s.groups)-1].e.Clear { last := &s.groups[len(s.groups)-1] last.entries = append(last.entries, v) continue } s.groups = append(s.groups, group{entries: []string{v}, e: e}) } return s } // Values returns the user's keybind entries. func (s *Set) Values() []string { var out []string for _, g := range s.groups { out = append(out, g.entries...) } return out } // HasClear reports whether the user's entries contain "clear". func (s *Set) HasClear() bool { for _, g := range s.groups { if g.e.Clear { return true } } return false } // effective computes the active bindings in insertion order. func (s *Set) effective() (map[string]*Binding, []string) { m := map[string]*Binding{} var order []string put := func(b *Binding) { if _, ok := m[b.Norm]; !ok { order = append(order, b.Norm) } m[b.Norm] = b } for _, d := range s.defaults { put(&Binding{Norm: d.Norm, Trigger: d.Flags + d.Trigger, Action: d.Action, Source: Default, Default: d.Action}) } for _, g := range s.groups { e := g.e switch { case e.Clear: m, order = map[string]*Binding{}, nil case e.Chain || e.Norm == "": case e.Action == "unbind": delete(m, e.Norm) default: b := &Binding{Norm: e.Norm, Trigger: e.Flags + e.Trigger, Action: e.Action, InConfig: true, Source: Custom} for _, c := range g.chain() { _, a := Split(c) b.Chain = append(b.Chain, a) } if d, ok := s.defByNorm[e.Norm]; ok { b.Default = d.Action b.Source = Changed if d.Action == e.Action && d.Flags == e.Flags && len(b.Chain) == 0 { b.Source = Default } } put(b) } } return m, order } // Bindings returns every effective binding plus the built-in bindings that // were removed (as Unbound). func (s *Set) Bindings() []Binding { m, order := s.effective() var out []Binding for _, n := range order { if b, ok := m[n]; ok { out = append(out, *b) } } for _, d := range s.defaults { if _, ok := m[d.Norm]; ok { continue } out = append(out, Binding{ Norm: d.Norm, Trigger: d.Flags + d.Trigger, Action: d.Action, Source: Unbound, Default: d.Action, InConfig: s.lastGroup(d.Norm) >= 0, }) } return out } // Lookup returns the effective binding for a trigger identity. func (s *Set) Lookup(norm string) (Binding, bool) { m, _ := s.effective() if b, ok := m[norm]; ok { return *b, true } return Binding{}, false } // lastGroup returns the index of the last user group for norm, or -1. func (s *Set) lastGroup(norm string) int { for i := len(s.groups) - 1; i >= 0; i-- { if s.groups[i].e.Norm == norm && !s.groups[i].e.Chain && !s.groups[i].e.Clear { return i } } return -1 } // drop removes all user groups for norm except the one at keep (-1 for // none) and returns the new index of the kept group. func (s *Set) drop(norm string, keep int) int { var out []group newKeep := -1 for i, g := range s.groups { if g.e.Norm == norm && !g.e.Clear && !g.e.Chain && i != keep { continue } if i == keep { newKeep = len(out) } out = append(out, g) } s.groups = out return newKeep } func (s *Set) active(norm string) (Binding, bool) { return s.Lookup(norm) } func (s *Set) appendValue(v string) { s.groups = append(s.groups, group{entries: []string{v}, e: ParseEntry(v)}) } // Change binds value ("trigger=action"). When old is given, that binding is // being edited: if its trigger changes, a built-in binding is unbound from // its old trigger, while a user binding simply moves (the old trigger falls // back to Ghostty's default, if it has one). Any other binding of the new // trigger is replaced. func (s *Set) Change(old *Binding, value string) error { ne := ParseEntry(value) if ne.Clear || ne.Chain || ne.Norm == "" || ne.Action == "" { return fmt.Errorf("expected trigger=action") } if _, err := Normalize(ne.Flags + ne.Trigger); err != nil { return err } // The user's own entry for the edited binding, if any. oldIdx := -1 if old != nil && old.Source != Unbound { if i := s.lastGroup(old.Norm); i >= 0 && s.groups[i].e.Action != "unbind" { oldIdx = i } } var chain []string if oldIdx >= 0 { chain = s.groups[oldIdx].chain() } g := group{entries: append([]string{ne.Value}, chain...), e: ne} var idx int switch { case oldIdx >= 0 && old.Source != Default: // Rewrite the user's entry in place, then remove other entries for // the old and the new trigger. If the trigger changed, the old one // falls back to Ghostty's default (if it has one). s.groups[oldIdx] = g idx = s.drop(old.Norm, oldIdx) idx = s.drop(ne.Norm, idx) default: if old != nil && old.Norm != ne.Norm && old.Source == Default { // Moving a built-in binding: the old trigger must stop doing it. s.drop(old.Norm, -1) if _, still := s.active(old.Norm); still { _, trigger := StripFlags(old.Trigger) s.appendValue(trigger + "=unbind") } } idx = s.drop(ne.Norm, s.lastGroup(ne.Norm)) if idx >= 0 { s.groups[idx] = g } else { s.groups = append(s.groups, g) idx = len(s.groups) - 1 } } // A binding identical to Ghostty's default needs no config entry. if d, ok := s.defByNorm[ne.Norm]; ok && d.Action == ne.Action && d.Flags == ne.Flags && len(chain) == 0 { saved := append([]group(nil), s.groups...) s.groups = append(s.groups[:idx:idx], s.groups[idx+1:]...) if b, ok := s.active(ne.Norm); !ok || b.Action != ne.Action { s.groups = saved // needed after all (e.g. after "clear") } } return nil } // Unbind removes the binding of b's trigger. func (s *Set) Unbind(b Binding) { if b.Source == Unbound { return } s.drop(b.Norm, -1) if _, still := s.active(b.Norm); still { _, trigger := StripFlags(b.Trigger) s.appendValue(trigger + "=unbind") } } // Restore resets b's trigger to Ghostty's built-in behavior: the built-in // binding if there is one, otherwise no binding. func (s *Set) Restore(b Binding) { s.drop(b.Norm, -1) d, ok := s.defByNorm[b.Norm] if !ok { return } if cur, active := s.active(b.Norm); !active || cur.Action != d.Action { s.appendValue(d.Value) // e.g. after "keybind = clear" } }