MCPcopy
hub / github.com/micro-editor/micro / InitBindings

Function InitBindings

internal/action/bindings.go:38–87  ·  view source on GitHub ↗

InitBindings intializes the bindings map by reading from bindings.json

()

Source from the content-addressed store, hash-verified

36
37// InitBindings intializes the bindings map by reading from bindings.json
38func InitBindings() {
39 var parsed map[string]any
40
41 filename := filepath.Join(config.ConfigDir, "bindings.json")
42 createBindingsIfNotExist(filename)
43
44 if _, e := os.Stat(filename); e == nil {
45 input, err := os.ReadFile(filename)
46 if err != nil {
47 screen.TermMessage("Error reading bindings.json file: " + err.Error())
48 return
49 }
50
51 err = json5.Unmarshal(input, &parsed)
52 if err != nil {
53 screen.TermMessage("Error reading bindings.json:", err.Error())
54 }
55 }
56
57 for p, bind := range Binder {
58 defaults := DefaultBindings(p)
59
60 for k, v := range defaults {
61 BindKey(k, v, bind)
62 }
63 }
64
65 for k, v := range parsed {
66 switch val := v.(type) {
67 case string:
68 BindKey(k, val, Binder["buffer"])
69 case map[string]any:
70 bind, ok := Binder[k]
71 if !ok || bind == nil {
72 screen.TermMessage(fmt.Sprintf("%s is not a valid pane type", k))
73 continue
74 }
75 for e, a := range val {
76 s, ok := a.(string)
77 if !ok {
78 screen.TermMessage("Error reading bindings.json: non-string and non-map entry", k)
79 } else {
80 BindKey(e, s, bind)
81 }
82 }
83 default:
84 screen.TermMessage("Error reading bindings.json: non-string and non-map entry", k)
85 }
86 }
87}
88
89func BindKey(k, v string, bind func(e Event, a string)) {
90 event, err := findEvent(k)

Callers 3

startupFunction · 0.92
mainFunction · 0.92
reloadRuntimeFunction · 0.85

Calls 6

TermMessageFunction · 0.92
createBindingsIfNotExistFunction · 0.85
DefaultBindingsFunction · 0.85
BindKeyFunction · 0.85
JoinMethod · 0.80
ErrorMethod · 0.45

Tested by 1

startupFunction · 0.74