Debian Codegraph @

config/binds.go

raw
1 package config
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "io"
8 "os"
9 "path"
10 "regexp"
11 "strings"
12 "sync/atomic"
13 "unicode"
14 "unicode/utf8"
15
16 "git.sr.ht/~rjarry/aerc/lib/log"
17 "github.com/go-ini/ini"
18 "go.rockorager.dev/vaxis"
19 )
20
21 type BindingConfig struct {
22 Global *KeyBindings
23 AccountWizard *KeyBindings
24 Compose *KeyBindings
25 ComposeEditor *KeyBindings
26 ComposeReview *KeyBindings
27 MessageList *KeyBindings
28 MessageView *KeyBindings
29 MessageViewPassthrough *KeyBindings
30 Terminal *KeyBindings
31 }
32
33 type bindsContextType int
34
35 const (
36 bindsContextFolder bindsContextType = iota
37 bindsContextAccount
38 )
39
40 type BindingConfigContext struct {
41 ContextType bindsContextType
42 Regex *regexp.Regexp
43 Bindings *KeyBindings
44 }
45
46 type KeyStroke struct {
47 Modifiers vaxis.ModifierMask
48 Key rune
49 }
50
51 type Binding struct {
52 Output []KeyStroke
53 Input []KeyStroke
54
55 Annotation string
56 }
57
58 type KeyBindings struct {
59 Bindings []*Binding
60 // If false, disable global keybindings in this context
61 Globals bool
62 // Which key opens the ex line (default is :)
63 ExKey KeyStroke
64 // Which key triggers completion (default is <tab>)
65 CompleteKey KeyStroke
66
67 // private
68 contextualBinds []*BindingConfigContext
69 contextualCounts map[bindsContextType]int
70 contextualCache map[bindsContextKey]*KeyBindings
71 }
72
73 type bindsContextKey struct {
74 ctxType bindsContextType
75 value string
76 }
77
78 const (
79 BINDING_FOUND = iota
80 BINDING_INCOMPLETE
81 BINDING_NOT_FOUND
82 )
83
84 type BindingSearchResult int
85
86 var bindsConfig atomic.Pointer[BindingConfig]
87
88 func Binds() *BindingConfig {
89 return bindsConfig.Load()
90 }
91
92 func defaultBindsConfig() *BindingConfig {
93 // These bindings are not configurable
94 wizard := NewKeyBindings()
95 wizard.ExKey = KeyStroke{Key: 'x', Modifiers: vaxis.ModCtrl}
96 wizard.Globals = false
97 quit, _ := ParseBinding("<C-q>", ":quit<Enter>", "Quit aerc")
98 wizard.Add(quit)
99 return &BindingConfig{
100 Global: NewKeyBindings(),
101 AccountWizard: wizard,
102 Compose: NewKeyBindings(),
103 ComposeEditor: NewKeyBindings(),
104 ComposeReview: NewKeyBindings(),
105 MessageList: NewKeyBindings(),
106 MessageView: NewKeyBindings(),
107 MessageViewPassthrough: NewKeyBindings(),
108 Terminal: NewKeyBindings(),
109 }
110 }
111
112 func parseBindsFromFile(root string, filename string) error {
113 conf := defaultBindsConfig()
114
115 log.Debugf("Parsing key bindings configuration from %s", filename)
116 binds, err := ini.LoadSources(ini.LoadOptions{
117 KeyValueDelimiters: "=",
118 // IgnoreInlineComment is set to true which tells ini's parser
119 // to treat comments (#) on the same line as part of the value;
120 // hence we need cut the comment off ourselves later
121 IgnoreInlineComment: true,
122 }, filename)
123 if err != nil {
124 return err
125 }
126
127 baseGroups := map[string]**KeyBindings{
128 "default": &conf.Global,
129 "compose": &conf.Compose,
130 "messages": &conf.MessageList,
131 "terminal": &conf.Terminal,
132 "view": &conf.MessageView,
133 "view::passthrough": &conf.MessageViewPassthrough,
134 "compose::editor": &conf.ComposeEditor,
135 "compose::review": &conf.ComposeReview,
136 }
137
138 // Base Bindings
139 for _, sectionName := range binds.SectionStrings() {
140 // Handle :: delimiter
141 baseSectionName := strings.ReplaceAll(sectionName, "::", "////")
142 sections := strings.Split(baseSectionName, ":")
143 baseOnly := len(sections) == 1
144 baseSectionName = strings.ReplaceAll(sections[0], "////", "::")
145
146 group, ok := baseGroups[strings.ToLower(baseSectionName)]
147 if !ok {
148 return errors.New("Unknown keybinding group " + sectionName)
149 }
150
151 if baseOnly {
152 err = LoadBinds(binds, baseSectionName, group)
153 if err != nil {
154 return err
155 }
156 }
157 }
158
159 log.Debugf("binds.conf: %#v", conf)
160 bindsConfig.Store(conf)
161
162 return nil
163 }
164
165 func parseBinds(root string, filename string) error {
166 if filename == "" {
167 filename = path.Join(root, "binds.conf")
168 if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
169 fmt.Printf("%s not found, installing the system default\n", filename)
170 if err := installTemplate(root, "binds.conf"); err != nil {
171 return err
172 }
173 }
174 }
175 SetBindsFilename(filename)
176 if err := parseBindsFromFile(root, filename); err != nil {
177 return fmt.Errorf("%s: %w", filename, err)
178 }
179
180 return nil
181 }
182
183 func LoadBindingSection(sec *ini.Section) (*KeyBindings, error) {
184 bindings := NewKeyBindings()
185 for _, k := range sec.Keys() {
186 key := k.Name()
187 value, annotation, _ := strings.Cut(k.String(), " # ")
188 value = strings.TrimSpace(value)
189 switch key {
190 case "$ex":
191 strokes, err := ParseKeyStrokes(value)
192 if err != nil {
193 return nil, err
194 }
195 if len(strokes) != 1 {
196 return nil, errors.New("Invalid binding")
197 }
198 bindings.ExKey = strokes[0]
199 case "$noinherit":
200 if value == "false" {
201 continue
202 }
203 if value != "true" {
204 return nil, errors.New("Invalid binding")
205 }
206 bindings.Globals = false
207 case "$complete":
208 strokes, err := ParseKeyStrokes(value)
209 if err != nil {
210 return nil, err
211 }
212 if len(strokes) != 1 {
213 return nil, errors.New("Invalid binding")
214 }
215 bindings.CompleteKey = strokes[0]
216 default:
217 annotation = strings.TrimSpace(annotation)
218 binding, err := ParseBinding(key, value, annotation)
219 if err != nil {
220 return nil, err
221 }
222 bindings.Add(binding)
223 }
224 }
225 return bindings, nil
226 }
227
228 func LoadBinds(binds *ini.File, baseName string, baseGroup **KeyBindings) error {
229 if sec, err := binds.GetSection(baseName); err == nil {
230 binds, err := LoadBindingSection(sec)
231 if err != nil {
232 return err
233 }
234 *baseGroup = MergeBindings(binds, *baseGroup)
235 }
236
237 b := *baseGroup
238
239 if baseName == "default" {
240 b.Globals = false
241 }
242
243 for _, sectionName := range binds.SectionStrings() {
244 if !strings.HasPrefix(sectionName, baseName+":") ||
245 strings.HasPrefix(sectionName, baseName+"::") {
246 continue
247 }
248
249 bindSection, err := binds.GetSection(sectionName)
250 if err != nil {
251 return err
252 }
253
254 binds, err := LoadBindingSection(bindSection)
255 if err != nil {
256 return err
257 }
258 if baseName == "default" {
259 binds.Globals = false
260 }
261
262 contextualBind := BindingConfigContext{
263 Bindings: binds,
264 }
265
266 var index int
267 if strings.Contains(sectionName, "=") {
268 index = strings.Index(sectionName, "=")
269 value := string(sectionName[index+1:])
270 contextualBind.Regex, err = regexp.Compile(value)
271 if err != nil {
272 return err
273 }
274 } else {
275 return fmt.Errorf("Invalid Bind Context regex in %s", sectionName)
276 }
277
278 switch sectionName[len(baseName)+1 : index] {
279 case "account":
280 acctName := sectionName[index+1:]
281 valid := false
282 for _, acctConf := range Accounts {
283 matches := contextualBind.Regex.FindString(acctConf.Name)
284 if matches != "" {
285 valid = true
286 }
287 }
288 if !valid {
289 log.Warnf("binds.conf: unexistent account: %s", acctName)
290 continue
291 }
292 contextualBind.ContextType = bindsContextAccount
293 case "folder":
294 // No validation needed. If the folder doesn't exist, the binds
295 // never get used
296 contextualBind.ContextType = bindsContextFolder
297 default:
298 return fmt.Errorf("Unknown Context Bind Section: %s", sectionName)
299 }
300 b.contextualBinds = append(b.contextualBinds, &contextualBind)
301 b.contextualCounts[contextualBind.ContextType]++
302 }
303
304 return nil
305 }
306
307 func NewKeyBindings() *KeyBindings {
308 return &KeyBindings{
309 ExKey: KeyStroke{0, ':'},
310 CompleteKey: KeyStroke{0, vaxis.KeyTab},
311 Globals: true,
312 contextualCache: make(map[bindsContextKey]*KeyBindings),
313 contextualCounts: make(map[bindsContextType]int),
314 }
315 }
316
317 func areBindingsInputsEqual(a, b *Binding) bool {
318 if len(a.Input) != len(b.Input) {
319 return false
320 }
321
322 for idx := range a.Input {
323 if a.Input[idx] != b.Input[idx] {
324 return false
325 }
326 }
327
328 return true
329 }
330
331 // this scans the bindings slice for copies and leaves just the first ones
332 // it also removes empty bindings, the ones that do nothing, so you can
333 // override and erase parent bindings with the context ones
334 func filterAndCleanBindings(bindings []*Binding) []*Binding {
335 // 1. remove a binding if we already have one with the same input
336 res1 := []*Binding{}
337 for _, b := range bindings {
338 // do we already have one here?
339 found := false
340 for _, r := range res1 {
341 if areBindingsInputsEqual(b, r) {
342 found = true
343 break
344 }
345 }
346
347 // add it if we don't
348 if !found {
349 res1 = append(res1, b)
350 }
351 }
352
353 // 2. clean up the empty bindings
354 res2 := []*Binding{}
355 for _, b := range res1 {
356 if len(b.Output) > 0 {
357 res2 = append(res2, b)
358 }
359 }
360
361 return res2
362 }
363
364 func MergeBindings(bindings ...*KeyBindings) *KeyBindings {
365 merged := NewKeyBindings()
366 for _, b := range bindings {
367 merged.Bindings = append(merged.Bindings, b.Bindings...)
368 if !b.Globals {
369 break
370 }
371 }
372 merged.Bindings = filterAndCleanBindings(merged.Bindings)
373 merged.ExKey = bindings[0].ExKey
374 merged.CompleteKey = bindings[0].CompleteKey
375 merged.Globals = bindings[0].Globals
376 for _, b := range bindings {
377 merged.contextualBinds = append(merged.contextualBinds, b.contextualBinds...)
378 for t, c := range b.contextualCounts {
379 merged.contextualCounts[t] += c
380 }
381 }
382 return merged
383 }
384
385 func (base *KeyBindings) contextual(
386 contextType bindsContextType, reg string,
387 ) *KeyBindings {
388 if base.contextualCounts[contextType] == 0 {
389 // shortcut if no contextual binds for that type
390 return base
391 }
392
393 key := bindsContextKey{ctxType: contextType, value: reg}
394 c, found := base.contextualCache[key]
395 if found {
396 return c
397 }
398
399 c = base
400 for _, contextualBind := range base.contextualBinds {
401 if contextualBind.ContextType != contextType {
402 continue
403 }
404 if !contextualBind.Regex.Match([]byte(reg)) {
405 continue
406 }
407 c = MergeBindings(contextualBind.Bindings, c)
408 }
409 base.contextualCache[key] = c
410
411 return c
412 }
413
414 func (bindings *KeyBindings) ForAccount(account string) *KeyBindings {
415 return bindings.contextual(bindsContextAccount, account)
416 }
417
418 func (bindings *KeyBindings) ForFolder(folder string) *KeyBindings {
419 return bindings.contextual(bindsContextFolder, folder)
420 }
421
422 func (bindings *KeyBindings) Add(binding *Binding) {
423 // TODO: Search for conflicts?
424 bindings.Bindings = append(bindings.Bindings, binding)
425 }
426
427 func (bindings *KeyBindings) GetBinding(
428 input []KeyStroke,
429 ) (BindingSearchResult, []KeyStroke) {
430 incomplete := false
431 // TODO: This could probably be a sorted list to speed things up
432 // TODO: Deal with bindings that share a prefix
433 for _, binding := range bindings.Bindings {
434 if len(binding.Input) < len(input) {
435 continue
436 }
437 for i, stroke := range input {
438 if stroke.Modifiers != binding.Input[i].Modifiers {
439 goto next
440 }
441 if stroke.Key != binding.Input[i].Key {
442 goto next
443 }
444 }
445 if len(binding.Input) != len(input) {
446 incomplete = true
447 } else {
448 return BINDING_FOUND, binding.Output
449 }
450 next:
451 }
452 if incomplete {
453 return BINDING_INCOMPLETE, nil
454 }
455 return BINDING_NOT_FOUND, nil
456 }
457
458 func (bindings *KeyBindings) GetReverseBindings(output []KeyStroke) [][]KeyStroke {
459 var inputs [][]KeyStroke
460
461 for _, binding := range bindings.Bindings {
462 if len(binding.Output) != len(output) {
463 continue
464 }
465 for i, stroke := range output {
466 if stroke.Modifiers != binding.Output[i].Modifiers {
467 goto next
468 }
469 if stroke.Key != binding.Output[i].Key {
470 goto next
471 }
472 }
473 inputs = append(inputs, binding.Input)
474 next:
475 }
476 return inputs
477 }
478
479 func FormatKeyStrokes(keystrokes []KeyStroke) string {
480 var sb strings.Builder
481
482 for _, stroke := range keystrokes {
483 special := false
484 s := ""
485 for name, ks := range keyNames {
486 if (ks.Modifiers == stroke.Modifiers || ks.Modifiers == vaxis.ModifierMask(0)) && ks.Key == stroke.Key {
487 switch name {
488 case "cr":
489 special = true
490 s = "enter"
491 case "space":
492 s = " "
493 case "dash":
494 s = "-"
495 case "semicolon":
496 s = ";"
497 default:
498 special = true
499 s = name
500 }
501 // remove any modifiers this named key comes
502 // with so we format properly
503 stroke.Modifiers &^= ks.Modifiers
504 break
505 }
506 }
507 if stroke.Modifiers != vaxis.ModifierMask(0) {
508 special = true
509 }
510 if special {
511 sb.WriteString("<")
512 }
513 if stroke.Modifiers&vaxis.ModCtrl > 0 {
514 sb.WriteString("c-")
515 }
516 if stroke.Modifiers&vaxis.ModAlt > 0 {
517 sb.WriteString("a-")
518 }
519 if stroke.Modifiers&vaxis.ModShift > 0 {
520 sb.WriteString("s-")
521 }
522 if s == "" && stroke.Key < unicode.MaxRune {
523 s = string(stroke.Key)
524 }
525 sb.WriteString(s)
526 if special {
527 sb.WriteString(">")
528 }
529 }
530
531 // replace leading & trailing spaces with explicit <space> keystrokes
532 buf := sb.String()
533 match := spaceTrimRe.FindStringSubmatch(buf)
534 if len(match) == 4 {
535 prefix := strings.ReplaceAll(match[1], " ", "<space>")
536 suffix := strings.ReplaceAll(match[3], " ", "<space>")
537 buf = prefix + match[2] + suffix
538 }
539
540 return buf
541 }
542
543 var spaceTrimRe = regexp.MustCompile(`^(\s*)(.*?)(\s*)$`)
544
545 var keyNames = map[string]KeyStroke{
546 "space": {vaxis.ModifierMask(0), ' '},
547 "dash": {vaxis.ModifierMask(0), '-'},
548 "semicolon": {vaxis.ModifierMask(0), ';'},
549 "enter": {vaxis.ModifierMask(0), vaxis.KeyEnter},
550 "up": {vaxis.ModifierMask(0), vaxis.KeyUp},
551 "down": {vaxis.ModifierMask(0), vaxis.KeyDown},
552 "right": {vaxis.ModifierMask(0), vaxis.KeyRight},
553 "left": {vaxis.ModifierMask(0), vaxis.KeyLeft},
554 "upleft": {vaxis.ModifierMask(0), vaxis.KeyUpLeft},
555 "upright": {vaxis.ModifierMask(0), vaxis.KeyUpRight},
556 "downleft": {vaxis.ModifierMask(0), vaxis.KeyDownLeft},
557 "downright": {vaxis.ModifierMask(0), vaxis.KeyDownRight},
558 "center": {vaxis.ModifierMask(0), vaxis.KeyCenter},
559 "pgup": {vaxis.ModifierMask(0), vaxis.KeyPgUp},
560 "pgdn": {vaxis.ModifierMask(0), vaxis.KeyPgDown},
561 "home": {vaxis.ModifierMask(0), vaxis.KeyHome},
562 "end": {vaxis.ModifierMask(0), vaxis.KeyEnd},
563 "insert": {vaxis.ModifierMask(0), vaxis.KeyInsert},
564 "delete": {vaxis.ModifierMask(0), vaxis.KeyDelete},
565 "backspace": {vaxis.ModifierMask(0), vaxis.KeyBackspace},
566 // "help": {vaxis.ModifierMask(0), vaxis.KeyHelp},
567 "exit": {vaxis.ModifierMask(0), vaxis.KeyExit},
568 "clear": {vaxis.ModifierMask(0), vaxis.KeyClear},
569 "cancel": {vaxis.ModifierMask(0), vaxis.KeyCancel},
570 "print": {vaxis.ModifierMask(0), vaxis.KeyPrint},
571 "pause": {vaxis.ModifierMask(0), vaxis.KeyPause},
572 "backtab": {vaxis.ModShift, vaxis.KeyTab},
573 "f1": {vaxis.ModifierMask(0), vaxis.KeyF01},
574 "f2": {vaxis.ModifierMask(0), vaxis.KeyF02},
575 "f3": {vaxis.ModifierMask(0), vaxis.KeyF03},
576 "f4": {vaxis.ModifierMask(0), vaxis.KeyF04},
577 "f5": {vaxis.ModifierMask(0), vaxis.KeyF05},
578 "f6": {vaxis.ModifierMask(0), vaxis.KeyF06},
579 "f7": {vaxis.ModifierMask(0), vaxis.KeyF07},
580 "f8": {vaxis.ModifierMask(0), vaxis.KeyF08},
581 "f9": {vaxis.ModifierMask(0), vaxis.KeyF09},
582 "f10": {vaxis.ModifierMask(0), vaxis.KeyF10},
583 "f11": {vaxis.ModifierMask(0), vaxis.KeyF11},
584 "f12": {vaxis.ModifierMask(0), vaxis.KeyF12},
585 "f13": {vaxis.ModifierMask(0), vaxis.KeyF13},
586 "f14": {vaxis.ModifierMask(0), vaxis.KeyF14},
587 "f15": {vaxis.ModifierMask(0), vaxis.KeyF15},
588 "f16": {vaxis.ModifierMask(0), vaxis.KeyF16},
589 "f17": {vaxis.ModifierMask(0), vaxis.KeyF17},
590 "f18": {vaxis.ModifierMask(0), vaxis.KeyF18},
591 "f19": {vaxis.ModifierMask(0), vaxis.KeyF19},
592 "f20": {vaxis.ModifierMask(0), vaxis.KeyF20},
593 "f21": {vaxis.ModifierMask(0), vaxis.KeyF21},
594 "f22": {vaxis.ModifierMask(0), vaxis.KeyF22},
595 "f23": {vaxis.ModifierMask(0), vaxis.KeyF23},
596 "f24": {vaxis.ModifierMask(0), vaxis.KeyF24},
597 "f25": {vaxis.ModifierMask(0), vaxis.KeyF25},
598 "f26": {vaxis.ModifierMask(0), vaxis.KeyF26},
599 "f27": {vaxis.ModifierMask(0), vaxis.KeyF27},
600 "f28": {vaxis.ModifierMask(0), vaxis.KeyF28},
601 "f29": {vaxis.ModifierMask(0), vaxis.KeyF29},
602 "f30": {vaxis.ModifierMask(0), vaxis.KeyF30},
603 "f31": {vaxis.ModifierMask(0), vaxis.KeyF31},
604 "f32": {vaxis.ModifierMask(0), vaxis.KeyF32},
605 "f33": {vaxis.ModifierMask(0), vaxis.KeyF33},
606 "f34": {vaxis.ModifierMask(0), vaxis.KeyF34},
607 "f35": {vaxis.ModifierMask(0), vaxis.KeyF35},
608 "f36": {vaxis.ModifierMask(0), vaxis.KeyF36},
609 "f37": {vaxis.ModifierMask(0), vaxis.KeyF37},
610 "f38": {vaxis.ModifierMask(0), vaxis.KeyF38},
611 "f39": {vaxis.ModifierMask(0), vaxis.KeyF39},
612 "f40": {vaxis.ModifierMask(0), vaxis.KeyF40},
613 "f41": {vaxis.ModifierMask(0), vaxis.KeyF41},
614 "f42": {vaxis.ModifierMask(0), vaxis.KeyF42},
615 "f43": {vaxis.ModifierMask(0), vaxis.KeyF43},
616 "f44": {vaxis.ModifierMask(0), vaxis.KeyF44},
617 "f45": {vaxis.ModifierMask(0), vaxis.KeyF45},
618 "f46": {vaxis.ModifierMask(0), vaxis.KeyF46},
619 "f47": {vaxis.ModifierMask(0), vaxis.KeyF47},
620 "f48": {vaxis.ModifierMask(0), vaxis.KeyF48},
621 "f49": {vaxis.ModifierMask(0), vaxis.KeyF49},
622 "f50": {vaxis.ModifierMask(0), vaxis.KeyF50},
623 "f51": {vaxis.ModifierMask(0), vaxis.KeyF51},
624 "f52": {vaxis.ModifierMask(0), vaxis.KeyF52},
625 "f53": {vaxis.ModifierMask(0), vaxis.KeyF53},
626 "f54": {vaxis.ModifierMask(0), vaxis.KeyF54},
627 "f55": {vaxis.ModifierMask(0), vaxis.KeyF55},
628 "f56": {vaxis.ModifierMask(0), vaxis.KeyF56},
629 "f57": {vaxis.ModifierMask(0), vaxis.KeyF57},
630 "f58": {vaxis.ModifierMask(0), vaxis.KeyF58},
631 "f59": {vaxis.ModifierMask(0), vaxis.KeyF59},
632 "f60": {vaxis.ModifierMask(0), vaxis.KeyF60},
633 "f61": {vaxis.ModifierMask(0), vaxis.KeyF61},
634 "f62": {vaxis.ModifierMask(0), vaxis.KeyF62},
635 "f63": {vaxis.ModifierMask(0), vaxis.KeyF63},
636 "tab": {vaxis.ModifierMask(0), vaxis.KeyTab},
637 "cr": {vaxis.ModifierMask(0), vaxis.KeyEnter},
638 "esc": {vaxis.ModifierMask(0), vaxis.KeyEsc},
639 "del": {vaxis.ModifierMask(0), vaxis.KeyDelete},
640 }
641
642 func ParseKeyStrokes(keystrokes string) ([]KeyStroke, error) {
643 var strokes []KeyStroke
644 buf := bytes.NewBufferString(keystrokes)
645 for {
646 tok, _, err := buf.ReadRune()
647 if err == io.EOF {
648 break
649 } else if err != nil {
650 return nil, err
651 }
652 // TODO: make it possible to bind to < or > themselves (and default to
653 // switching accounts)
654 switch tok {
655 case '<':
656 name, err := buf.ReadString(byte('>'))
657 switch {
658 case err == io.EOF:
659 return nil, errors.New("Expecting '>'")
660 case err != nil:
661 return nil, err
662 case name == ">":
663 return nil, errors.New("Expected a key name")
664 }
665 name = name[:len(name)-1]
666 args := strings.Split(name, "-")
667 // check if the last char was a '-' and we'll add it
668 // back. We check for "--" in case it was an invalid
669 // keystroke (ie <C->)
670 if strings.HasSuffix(name, "--") {
671 args = append(args, "-")
672 }
673 ks := KeyStroke{}
674 for i, arg := range args {
675 if i == len(args)-1 {
676 key, ok := keyNames[strings.ToLower(arg)]
677 if !ok {
678 r, n := utf8.DecodeRuneInString(arg)
679 if n != len(arg) {
680 return nil, fmt.Errorf("Unknown key '%s'", name)
681 }
682 key = KeyStroke{Key: r}
683 }
684 ks.Key = key.Key
685 ks.Modifiers |= key.Modifiers
686 strokes = append(strokes, ks)
687 } else {
688 switch strings.ToLower(arg) {
689 case "s", "S":
690 ks.Modifiers |= vaxis.ModShift
691 case "a", "A":
692 ks.Modifiers |= vaxis.ModAlt
693 case "c", "C":
694 ks.Modifiers |= vaxis.ModCtrl
695 case "":
696 default:
697 return nil, fmt.Errorf("Unknown modifier key '%s'", strings.ToLower(arg))
698 }
699 }
700 }
701 case '>':
702 return nil, errors.New("Found '>' without '<'")
703 case '\\':
704 tok, _, err = buf.ReadRune()
705 if err == io.EOF {
706 tok = '\\'
707 } else if err != nil {
708 return nil, err
709 }
710 fallthrough
711 default:
712 strokes = append(strokes, KeyStroke{
713 Modifiers: vaxis.ModifierMask(0),
714 Key: tok,
715 })
716 }
717 }
718 return strokes, nil
719 }
720
721 func ParseBinding(input, output, annotation string) (*Binding, error) {
722 in, err := ParseKeyStrokes(input)
723 if err != nil {
724 return nil, err
725 }
726 out, err := ParseKeyStrokes(output)
727 if err != nil {
728 return nil, err
729 }
730 return &Binding{
731 Input: in,
732 Output: out,
733 Annotation: annotation,
734 }, nil
735 }