Skip to content

Commit 989c2c4

Browse files
Merge branch 'LockConfig' into dev
2 parents 6ed1184 + 0525ad3 commit 989c2c4

File tree

5 files changed

+36
-47
lines changed

5 files changed

+36
-47
lines changed

cmd/micro/initlua.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ 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, "RegisterKeybinding", luar.New(ulua.L, action.RegisterKeybindingPlug))
7776
ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKeyPlug))
7877
ulua.L.SetField(pkg, "Reload", luar.New(ulua.L, action.ReloadConfig))
7978
ulua.L.SetField(pkg, "AddRuntimeFileFromMemory", luar.New(ulua.L, config.PluginAddRuntimeFileFromMemory))
@@ -89,8 +88,8 @@ func luaImportMicroConfig() *lua.LTable {
8988
ulua.L.SetField(pkg, "RegisterCommonOption", luar.New(ulua.L, config.RegisterCommonOptionPlug))
9089
ulua.L.SetField(pkg, "RegisterGlobalOption", luar.New(ulua.L, config.RegisterGlobalOptionPlug))
9190
ulua.L.SetField(pkg, "GetGlobalOption", luar.New(ulua.L, config.GetGlobalOption))
92-
ulua.L.SetField(pkg, "SetGlobalOption", luar.New(ulua.L, config.SetGlobalOptionPlug))
93-
ulua.L.SetField(pkg, "SetGlobalOptionNative", luar.New(ulua.L, config.SetGlobalOptionNativePlug))
91+
ulua.L.SetField(pkg, "SetGlobalOption", luar.New(ulua.L, action.SetGlobalOptionPlug))
92+
ulua.L.SetField(pkg, "SetGlobalOptionNative", luar.New(ulua.L, action.SetGlobalOptionNativePlug))
9493
ulua.L.SetField(pkg, "ConfigDir", luar.New(ulua.L, config.ConfigDir))
9594

9695
return pkg

internal/action/bindings.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,13 @@ 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.
264+
// TryBindKeyPlug tries to bind key for the plugin without writing to bindings.json.
265265
// This operation can be rejected by lockbindings to prevent unexpected actions by the user.
266-
func RegisterKeybindingPlug(k, v string) (bool, error) {
266+
func TryBindKeyPlug(k, v string, overwrite bool) (bool, error) {
267267
if l, ok := config.GlobalSettings["lockbindings"]; ok && l.(bool) {
268268
return false, errors.New("bindings is locked by the user")
269269
}
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)
270+
return TryBindKey(k, v, overwrite, false)
276271
}
277272

278273
// TryBindKey tries to bind a key by writing to config.ConfigDir/bindings.json

internal/action/command.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -654,14 +654,7 @@ func doSetGlobalOptionNative(option string, nativeValue any) error {
654654
return nil
655655
}
656656

657-
func SetGlobalOptionNativePlug(option string, nativeValue interface{}) error {
658-
if l, ok := config.GlobalSettings["locksettings"]; ok && l.(bool) {
659-
return errors.New("settings.json file locked by user for all plugins")
660-
}
661-
return SetGlobalOptionNative(option, nativeValue)
662-
}
663-
664-
func SetGlobalOptionNative(option string, nativeValue interface{}) error {
657+
func SetGlobalOptionNative(option string, nativeValue any, writeToFile bool) error {
665658
if err := config.OptionIsValid(option, nativeValue); err != nil {
666659
return err
667660
}
@@ -684,6 +677,10 @@ func SetGlobalOptionNative(option string, nativeValue interface{}) error {
684677
delete(b.LocalSettings, option)
685678
}
686679

680+
if !writeToFile {
681+
return nil
682+
}
683+
687684
err := config.WriteSettings(filepath.Join(config.ConfigDir, "settings.json"))
688685
if err != nil {
689686
if errors.Is(err, util.ErrOverwrite) {
@@ -696,14 +693,7 @@ func SetGlobalOptionNative(option string, nativeValue interface{}) error {
696693
return nil
697694
}
698695

699-
func SetGlobalOptionPlug(option, value string) error {
700-
if l, ok := config.GlobalSettings["locksettings"]; ok && l.(bool) {
701-
return errors.New("settings.json file locked by user for all plugins")
702-
}
703-
return SetGlobalOption(option, value)
704-
}
705-
706-
func SetGlobalOption(option, value string) error {
696+
func SetGlobalOption(option, value string, writeToFile bool) error {
707697
if _, ok := config.GlobalSettings[option]; !ok {
708698
return config.ErrInvalidOption
709699
}
@@ -713,7 +703,15 @@ func SetGlobalOption(option, value string) error {
713703
return err
714704
}
715705

716-
return SetGlobalOptionNative(option, nativeValue)
706+
return SetGlobalOptionNative(option, nativeValue, writeToFile)
707+
}
708+
709+
func SetGlobalOptionNativePlug(option string, nativeValue any) error {
710+
return SetGlobalOptionNative(option, nativeValue, false)
711+
}
712+
713+
func SetGlobalOptionPlug(option, value string) error {
714+
return SetGlobalOption(option, value, false)
717715
}
718716

719717
// ResetCmd resets a setting to its default value
@@ -727,7 +725,7 @@ func (h *BufPane) ResetCmd(args []string) {
727725
defaults := config.DefaultAllSettings()
728726

729727
if _, ok := defaults[option]; ok {
730-
SetGlobalOptionNative(option, defaults[option])
728+
SetGlobalOptionNative(option, defaults[option], true)
731729
return
732730
}
733731
InfoBar.Error(config.ErrInvalidOption)
@@ -743,7 +741,7 @@ func (h *BufPane) SetCmd(args []string) {
743741
option := args[0]
744742
value := args[1]
745743

746-
err := SetGlobalOption(option, value)
744+
err := SetGlobalOption(option, value, true)
747745
if err == config.ErrInvalidOption {
748746
err := h.Buf.SetOption(option, value)
749747
if err != nil {
@@ -799,7 +797,7 @@ func (h *BufPane) toggleOption(option string, local bool) error {
799797
return err
800798
}
801799
} else {
802-
if err := SetGlobalOptionNative(option, newVal); err != nil {
800+
if err := SetGlobalOptionNative(option, newVal, true); err != nil {
803801
return err
804802
}
805803
}

runtime/help/options.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,9 @@ Here are the available options:
227227

228228
default value: `false`
229229

230-
* `lockbindings`: disable plugins to modify the `bindings.json` in your config
231-
directory.
230+
* `lockbindings`: prevent plugins and lua scripts from binding any keys.
231+
Any custom actions must be binded manually either via commands like `bind`
232+
or by modifying the `bindings.json` file.
232233

233234
default value: `false`
234235

runtime/help/plugins.md

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

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).
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.
181+
This operation can be rejected by `lockbindings` to prevent undesired
182+
actions by the user.
187183

188184
- `Reload()`: reload configuration files.
189185

@@ -228,10 +224,10 @@ The packages and their contents are listed below (in Go type signatures):
228224
- `GetGlobalOption(name string) any`: returns the value of a
229225
given plugin in the `GlobalSettings` map.
230226

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.
227+
- `SetGlobalOption(option, value string) error`: sets an option to a
228+
given value. This will try to convert the value into the proper
229+
type for the option. Can return an error if the option name is not
230+
valid, or the value can not be converted.
235231

236232
- **Deprecated** `SetGlobalOptionNative(option string, value interface{}) error`:
237233
sets an option to a given value, where the type of value is the actual

0 commit comments

Comments
 (0)