Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions internal/action/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,11 @@ func eventsEqual(e1 Event, e2 Event) bool {
return e1 == e2
}

// TryBindKey tries to bind a key by writing to config.ConfigDir/bindings.json
// Returns true if the keybinding already existed and a possible error
// TryBindKey tries to bind a key to an action. If there is already a binding
// for this key and `overwrite` is true, TryBindKey replaces this binding with
// the new one and writes the new binding to `bindings.json`.
// Returns true if the new binding has been applied, and a possible error
// if failed to write to `bindings.json`.
func TryBindKey(k, v string, overwrite bool) (bool, error) {
var e error
var parsed map[string]interface{}
Expand Down Expand Up @@ -295,16 +298,20 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) {
if overwrite {
parsed[ev] = v
} else {
return true, nil
return parsed[ev] == v, nil
}
} else {
parsed[k] = v
}

BindKey(k, v, Binder["buffer"])

txt, _ := json.MarshalIndent(parsed, "", " ")
return true, ioutil.WriteFile(filename, append(txt, '\n'), 0644)
if overwrite {
txt, _ := json.MarshalIndent(parsed, "", " ")
return true, ioutil.WriteFile(filename, append(txt, '\n'), 0644)
}

return true, nil
}
return false, e
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/help/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ The packages and their contents are listed below (in Go type signatures):
- `NoComplete`: no autocompletion suggestions

- `TryBindKey(k, v string, overwrite bool) (bool, error)`: bind the key
`k` to the string `v` in the `bindings.json` file. If `overwrite` is
true, this will overwrite any existing binding to key `k`. Returns true
`k` to the action `v`. If `overwrite` is true, this will overwrite any
existing binding to key `k` in the `bindings.json` file. Returns true
if the binding was made, and a possible error (for example writing to
`bindings.json` can cause an error).

Expand Down