-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind.go
77 lines (65 loc) · 1.56 KB
/
bind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package commands
import (
"fmt"
"github.com/ambientsound/visp/input/keys"
"strings"
"github.com/ambientsound/visp/api"
"github.com/ambientsound/visp/input/lexer"
"github.com/ambientsound/visp/keysequence"
)
// Bind maps a key sequence to the execution of a command.
type Bind struct {
command
api api.API
sentence string
context string
seq keysequence.KeySequence
}
// NewBind returns Bind.
func NewBind(api api.API) Command {
return &Bind{
api: api,
}
}
// Parse implements Command.
func (cmd *Bind) Parse() error {
var err error
// Bind keyboard sequence to a specific program context.
cmd.context, err = cmd.ParseContext()
if err != nil {
return err
}
// Use the key sequence parser for parsing the next token.
parser := keysequence.NewParser(cmd.S)
// Parse a valid key sequence from the scanner.
seq, err := parser.ParseKeySequence()
if err != nil {
return err
}
cmd.seq = seq
// Treat the rest of the line as the literal action to execute when the bind succeeds.
sentence := make([]string, 0, 32)
for {
tok, lit := cmd.Scan()
if tok == lexer.TokenEnd {
break
} else if tok == lexer.TokenIdentifier {
// Quote identifiers?
}
sentence = append(sentence, lit)
}
if len(sentence) == 0 {
return fmt.Errorf("unexpected END, expected identifier")
}
cmd.sentence = strings.Join(sentence, "")
return nil
}
// Exec implements Command.
func (cmd *Bind) Exec() error {
sequencer := cmd.api.Sequencer()
return sequencer.AddBind(keys.Binding{
Context: cmd.context,
Sequence: cmd.seq,
Command: cmd.sentence,
})
}