Skip to content

Commit 25c8278

Browse files
Ability to lock bindings, migrate to register* functions instead
1 parent 0b9c7c0 commit 25c8278

File tree

6 files changed

+59
-18
lines changed

6 files changed

+59
-18
lines changed

cmd/micro/initlua.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ 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, "RegisterKeybinding", luar.New(ulua.L, action.RegisterKeybindingPlug))
77+
ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKeyPlug))
7778
ulua.L.SetField(pkg, "Reload", luar.New(ulua.L, action.ReloadConfig))
7879
ulua.L.SetField(pkg, "AddRuntimeFileFromMemory", luar.New(ulua.L, config.PluginAddRuntimeFileFromMemory))
7980
ulua.L.SetField(pkg, "AddRuntimeFilesFromDirectory", luar.New(ulua.L, config.PluginAddRuntimeFilesFromDirectory))
@@ -88,8 +89,8 @@ func luaImportMicroConfig() *lua.LTable {
8889
ulua.L.SetField(pkg, "RegisterCommonOption", luar.New(ulua.L, config.RegisterCommonOptionPlug))
8990
ulua.L.SetField(pkg, "RegisterGlobalOption", luar.New(ulua.L, config.RegisterGlobalOptionPlug))
9091
ulua.L.SetField(pkg, "GetGlobalOption", luar.New(ulua.L, config.GetGlobalOption))
91-
ulua.L.SetField(pkg, "SetGlobalOption", luar.New(ulua.L, action.SetGlobalOption))
92-
ulua.L.SetField(pkg, "SetGlobalOptionNative", luar.New(ulua.L, action.SetGlobalOptionNative))
92+
ulua.L.SetField(pkg, "SetGlobalOption", luar.New(ulua.L, config.SetGlobalOptionPlug))
93+
ulua.L.SetField(pkg, "SetGlobalOptionNative", luar.New(ulua.L, config.SetGlobalOptionNativePlug))
9394
ulua.L.SetField(pkg, "ConfigDir", luar.New(ulua.L, config.ConfigDir))
9495

9596
return pkg

internal/action/bindings.go

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

264+
// RegisterKeybindingPlug registers a default keybinding for the plugin without writing to bindings.json.
265+
// This operation can be rejected by lockbindings to prevent unexpected actions by the user.
266+
func RegisterKeybindingPlug(k, v string) (bool, error) {
267+
if l, ok := config.GlobalSettings["lockbindings"]; ok && l.(bool) {
268+
return false, errors.New("bindings is locked by the user")
269+
}
270+
return TryBindKey(k, v, false, false)
271+
}
272+
273+
// **Deprecated**
274+
func TryBindKeyPlug(k, v string, overwrite bool) (bool, error) {
275+
return RegisterKeybindingPlug(k, v)
276+
}
277+
264278
// 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) {
279+
// Returns true if the keybinding already existed or is binded successfully and a possible error
280+
func TryBindKey(k, v string, overwrite bool, writeToFile bool) (bool, error) {
267281
var e error
268282
var parsed map[string]any
269283

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

311325
txt, _ := json.MarshalIndent(parsed, "", " ")
312326
txt = append(txt, '\n')
313-
return true, writeFile(filename, txt)
327+
328+
if writeToFile {
329+
return true, writeFile(filename, txt)
330+
} else {
331+
return true, nil
332+
}
314333
}
315334
return false, e
316335
}

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)

internal/config/settings.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ var DefaultGlobalOnlySettings = map[string]any{
118118
"helpsplit": "hsplit",
119119
"infobar": true,
120120
"keymenu": false,
121+
"lockbindings": false,
121122
"mouse": true,
122123
"multiopen": "tab",
123124
"parsecursor": false,
@@ -408,6 +409,16 @@ func RegisterGlobalOptionPlug(pl string, name string, defaultvalue any) error {
408409
return RegisterGlobalOption(pl+"."+name, defaultvalue)
409410
}
410411

412+
// **Deprecated**
413+
func SetGlobalOptionNativePlug(option string, nativeValue interface{}) error {
414+
return RegisterGlobalOption(option, nativeValue)
415+
}
416+
417+
// **Deprecated**
418+
func SetGlobalOptionPlug(option, value string) error {
419+
return RegisterGlobalOption(option, value)
420+
}
421+
411422
// RegisterCommonOption creates a new option
412423
func RegisterCommonOption(name string, defaultvalue any) error {
413424
if _, ok := GlobalSettings[name]; !ok {

runtime/help/options.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ Here are the available options:
231231

232232
default value: `false`
233233

234+
* `lockbindings`: disable plugins to modify the `bindings.json` in your config
235+
directory.
236+
237+
default value: `false`
238+
234239
* `matchbrace`: show matching braces for '()', '{}', '[]' when the cursor
235240
is on a brace character or (if `matchbraceleft` is enabled) next to it.
236241

runtime/help/plugins.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,16 @@ 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+
- `RegisterKeybindingPlug(k, v string) (bool, error)`: registers a default
178+
keybinding `k` with action `v` for the plugin without writing to
179+
`bindings.json.` This operation can be rejected by `lockbindings` to
180+
prevent unexpected actions by the user.
181+
182+
- **Drepecated** `TryBindKey(k, v string, overwrite bool) (bool, error)`:
183+
bind the key `k` to the string `v` in the `bindings.json` file.
184+
If `overwrite` is true, this will overwrite any existing binding to key
185+
`k`. Returns true if the binding was made, and a possible error
186+
(for example writing to `bindings.json` can cause an error).
182187

183188
- `Reload()`: reload configuration files.
184189

@@ -223,13 +228,13 @@ The packages and their contents are listed below (in Go type signatures):
223228
- `GetGlobalOption(name string) any`: returns the value of a
224229
given plugin in the `GlobalSettings` map.
225230

226-
- `SetGlobalOption(option, value string) error`: sets an option to a
227-
given value. Same as using the `> set` command. This will try to convert
228-
the value into the proper type for the option. Can return an error if the
229-
option name is not valid, or the value can not be converted.
231+
- **Deprecated** `SetGlobalOption(option, value string) error`: sets an
232+
option to a given value. Same as using the `> set` command. This will try
233+
to convert the value into the proper type for the option. Can return an
234+
error if the option name is not valid, or the value can not be converted.
230235

231-
- `SetGlobalOptionNative(option string, value any) error`: sets
232-
an option to a given value, where the type of value is the actual
236+
- **Deprecated** `SetGlobalOptionNative(option string, value any) error`:
237+
sets an option to a given value, where the type of value is the actual
233238
type of the value internally. Can return an error if the provided value
234239
is not valid for the given option.
235240

0 commit comments

Comments
 (0)