Skip to content

Commit b39b5b5

Browse files
Changing behavior for TryBindKey() for lua to not write to bindings.json
1 parent 0b9c7c0 commit b39b5b5

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

cmd/micro/initlua.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func luaImportMicroConfig() *lua.LTable {
7373
ulua.L.SetField(pkg, "OptionComplete", luar.New(ulua.L, action.OptionComplete))
7474
ulua.L.SetField(pkg, "OptionValueComplete", luar.New(ulua.L, action.OptionValueComplete))
7575
ulua.L.SetField(pkg, "NoComplete", luar.New(ulua.L, nil))
76-
ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKey))
76+
ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKeyPlug))
7777
ulua.L.SetField(pkg, "Reload", luar.New(ulua.L, action.ReloadConfig))
7878
ulua.L.SetField(pkg, "AddRuntimeFileFromMemory", luar.New(ulua.L, config.PluginAddRuntimeFileFromMemory))
7979
ulua.L.SetField(pkg, "AddRuntimeFilesFromDirectory", luar.New(ulua.L, config.PluginAddRuntimeFilesFromDirectory))

internal/action/bindings.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,14 @@ func eventsEqual(e1 Event, e2 Event) bool {
261261
return e1 == e2
262262
}
263263

264+
// TryBindKeyPlug tries to bind a key for the plugin without writing to bindings.json.
265+
func TryBindKeyPlug(k, v string, overwrite bool) (bool, error) {
266+
return TryBindKey(k, v, overwrite, false)
267+
}
268+
264269
// TryBindKey tries to bind a key by writing to config.ConfigDir/bindings.json
265-
// Returns true if the keybinding already existed and a possible error
266-
func TryBindKey(k, v string, overwrite bool) (bool, error) {
270+
// Returns true if the keybinding already existed or is binded successfully and a possible error
271+
func TryBindKey(k, v string, overwrite bool, writeToFile bool) (bool, error) {
267272
var e error
268273
var parsed map[string]any
269274

@@ -310,7 +315,12 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) {
310315

311316
txt, _ := json.MarshalIndent(parsed, "", " ")
312317
txt = append(txt, '\n')
313-
return true, writeFile(filename, txt)
318+
319+
if writeToFile {
320+
return true, writeFile(filename, txt)
321+
} else {
322+
return true, nil
323+
}
314324
}
315325
return false, e
316326
}

internal/action/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ func (h *BufPane) BindCmd(args []string) {
844844
return
845845
}
846846

847-
_, err := TryBindKey(parseKeyArg(args[0]), args[1], true)
847+
_, err := TryBindKey(parseKeyArg(args[0]), args[1], true, true)
848848
if err != nil {
849849
if errors.Is(err, util.ErrOverwrite) {
850850
screen.TermMessage(err)

runtime/help/plugins.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,10 @@ The packages and their contents are listed below (in Go type signatures):
174174
values afterwards
175175
- `NoComplete`: no autocompletion suggestions
176176

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

183182
- `Reload()`: reload configuration files.
184183

0 commit comments

Comments
 (0)