diff --git a/internal/action/bindings.go b/internal/action/bindings.go index 01ea784507..36cf3ea434 100644 --- a/internal/action/bindings.go +++ b/internal/action/bindings.go @@ -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{} @@ -295,7 +298,7 @@ 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 @@ -303,8 +306,12 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) { 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 } diff --git a/runtime/help/plugins.md b/runtime/help/plugins.md index fbab646943..bb0bf5a6d6 100644 --- a/runtime/help/plugins.md +++ b/runtime/help/plugins.md @@ -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).