diff --git a/cmd/micro/initlua.go b/cmd/micro/initlua.go index 7eac5637..583085a0 100644 --- a/cmd/micro/initlua.go +++ b/cmd/micro/initlua.go @@ -73,7 +73,7 @@ func luaImportMicroConfig() *lua.LTable { ulua.L.SetField(pkg, "OptionComplete", luar.New(ulua.L, action.OptionComplete)) ulua.L.SetField(pkg, "OptionValueComplete", luar.New(ulua.L, action.OptionValueComplete)) ulua.L.SetField(pkg, "NoComplete", luar.New(ulua.L, nil)) - ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKey)) + ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKeyPlug)) ulua.L.SetField(pkg, "Reload", luar.New(ulua.L, action.ReloadConfig)) ulua.L.SetField(pkg, "AddRuntimeFileFromMemory", luar.New(ulua.L, config.PluginAddRuntimeFileFromMemory)) ulua.L.SetField(pkg, "AddRuntimeFilesFromDirectory", luar.New(ulua.L, config.PluginAddRuntimeFilesFromDirectory)) diff --git a/internal/action/bindings.go b/internal/action/bindings.go index 37517d9b..af187d43 100644 --- a/internal/action/bindings.go +++ b/internal/action/bindings.go @@ -261,9 +261,14 @@ func eventsEqual(e1 Event, e2 Event) bool { return e1 == e2 } +// TryBindKeyPlug tries to bind a key for the plugin without writing to bindings.json. +func TryBindKeyPlug(k, v string, overwrite bool) (bool, error) { + return TryBindKey(k, v, overwrite, false) +} + // TryBindKey tries to bind a key by writing to config.ConfigDir/bindings.json -// Returns true if the keybinding already existed and a possible error -func TryBindKey(k, v string, overwrite bool) (bool, error) { +// Returns true if the keybinding already existed or is binded successfully and a possible error +func TryBindKey(k, v string, overwrite bool, writeToFile bool) (bool, error) { var e error var parsed map[string]any @@ -310,7 +315,12 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) { txt, _ := json.MarshalIndent(parsed, "", " ") txt = append(txt, '\n') - return true, writeFile(filename, txt) + + if writeToFile { + return true, writeFile(filename, txt) + } else { + return true, nil + } } return false, e } diff --git a/internal/action/command.go b/internal/action/command.go index 7cb5e523..3828afdb 100644 --- a/internal/action/command.go +++ b/internal/action/command.go @@ -844,7 +844,7 @@ func (h *BufPane) BindCmd(args []string) { return } - _, err := TryBindKey(parseKeyArg(args[0]), args[1], true) + _, err := TryBindKey(parseKeyArg(args[0]), args[1], true, true) if err != nil { if errors.Is(err, util.ErrOverwrite) { screen.TermMessage(err) diff --git a/runtime/help/plugins.md b/runtime/help/plugins.md index c683804f..8ce8fe6c 100644 --- a/runtime/help/plugins.md +++ b/runtime/help/plugins.md @@ -174,11 +174,10 @@ The packages and their contents are listed below (in Go type signatures): values afterwards - `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 - if the binding was made, and a possible error (for example writing to - `bindings.json` can cause an error). + - `TryBindKey(k, v string, overwrite bool) (bool, error)`: + bind the key `k` to the string `v`. If `overwrite` is true, this will + overwrite any existing binding to key `k`. + Returns true if the binding was made, and a possible error. - `Reload()`: reload configuration files.