Skip to content

Commit 58f3b7f

Browse files
Merge branch 'LockConfig' into dev
2 parents 9c88f56 + 8843128 commit 58f3b7f

File tree

6 files changed

+49
-27
lines changed

6 files changed

+49
-27
lines changed

cmd/micro/initlua.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +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, "RegisterKeybinding", luar.New(ulua.L, action.RegisterKeybindingPlug))
7677
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))
@@ -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.SetGlobalOptionPlug))
92-
ulua.L.SetField(pkg, "SetGlobalOptionNative", luar.New(ulua.L, action.SetGlobalOptionNativePlug))
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: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,23 @@ func eventsEqual(e1 Event, e2 Event) bool {
261261
return e1 == e2
262262
}
263263

264-
func TryBindKeyPlug(k, v string, overwrite bool) (bool, error) {
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) {
265267
if l, ok := config.GlobalSettings["lockbindings"]; ok && l.(bool) {
266-
return false, errors.New("bindings.json file locked by user for all plugins")
268+
return false, errors.New("bindings is locked by the user")
267269
}
268-
return TryBindKey(k, v, overwrite)
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)
269276
}
270277

271278
// TryBindKey tries to bind a key by writing to config.ConfigDir/bindings.json
272-
// Returns true if the keybinding already existed and a possible error
273-
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) {
274281
var e error
275282
var parsed map[string]interface{}
276283

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

318325
txt, _ := json.MarshalIndent(parsed, "", " ")
319326
txt = append(txt, '\n')
320-
return true, writeFile(filename, txt)
327+
328+
if writeToFile {
329+
return true, writeFile(filename, txt)
330+
} else {
331+
return true, nil
332+
}
321333
}
322334
return false, e
323335
}

internal/action/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ func (h *BufPane) BindCmd(args []string) {
821821
return
822822
}
823823

824-
_, err := TryBindKey(parseKeyArg(args[0]), args[1], true)
824+
_, err := TryBindKey(parseKeyArg(args[0]), args[1], true, true)
825825
if err != nil {
826826
if errors.Is(err, util.ErrOverwrite) {
827827
screen.TermMessage(err)

internal/config/settings.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ var DefaultGlobalOnlySettings = map[string]interface{}{
119119
"infobar": true,
120120
"keymenu": false,
121121
"lockbindings": false,
122-
"locksettings": false,
123122
"mouse": true,
124123
"multiopen": "tab",
125124
"parsecursor": false,
@@ -410,6 +409,16 @@ func RegisterGlobalOptionPlug(pl string, name string, defaultvalue interface{})
410409
return RegisterGlobalOption(pl+"."+name, defaultvalue)
411410
}
412411

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+
413422
// RegisterCommonOption creates a new option
414423
func RegisterCommonOption(name string, defaultvalue interface{}) error {
415424
if _, ok := GlobalSettings[name]; !ok {

runtime/help/options.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,7 @@ Here are the available options:
241241

242242
default value: `false`
243243

244-
* `lockbindings`: Disable plugins to modify the `bindings.json` in your config
245-
directory.
246-
247-
default value: `false`
248-
249-
* `locksettings`: Disable plugins to modify the `settings.json` in your config
244+
* `lockbindings`: disable plugins to modify the `bindings.json` in your config
250245
directory.
251246

252247
default value: `false`

runtime/help/plugins.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,16 @@ The packages and their contents are listed below (in Go type signatures):
167167
values afterwards
168168
- `NoComplete`: no autocompletion suggestions
169169

170-
- `TryBindKey(k, v string, overwrite bool) (bool, error)`: bind the key
171-
`k` to the string `v` in the `bindings.json` file. If `overwrite` is
172-
true, this will overwrite any existing binding to key `k`. Returns true
173-
if the binding was made, and a possible error (for example writing to
174-
`bindings.json` can cause an error).
170+
- `RegisterKeybindingPlug(k, v string) (bool, error)`: registers a default
171+
keybinding `k` with action `v` for the plugin without writing to
172+
`bindings.json.` This operation can be rejected by `lockbindings` to
173+
prevent unexpected actions by the user.
174+
175+
- **Drepecated** `TryBindKey(k, v string, overwrite bool) (bool, error)`:
176+
bind the key `k` to the string `v` in the `bindings.json` file.
177+
If `overwrite` is true, this will overwrite any existing binding to key
178+
`k`. Returns true if the binding was made, and a possible error
179+
(for example writing to `bindings.json` can cause an error).
175180

176181
- `Reload()`: reload configuration files.
177182

@@ -216,13 +221,13 @@ The packages and their contents are listed below (in Go type signatures):
216221
- `GetGlobalOption(name string) interface{}`: returns the value of a
217222
given plugin in the `GlobalSettings` map.
218223

219-
- `SetGlobalOption(option, value string) error`: sets an option to a
220-
given value. Same as using the `> set` command. This will try to convert
221-
the value into the proper type for the option. Can return an error if the
222-
option name is not valid, or the value can not be converted.
224+
- **Deprecated** `SetGlobalOption(option, value string) error`: sets an
225+
option to a given value. Same as using the `> set` command. This will try
226+
to convert the value into the proper type for the option. Can return an
227+
error if the option name is not valid, or the value can not be converted.
223228

224-
- `SetGlobalOptionNative(option string, value interface{}) error`: sets
225-
an option to a given value, where the type of value is the actual
229+
- **Deprecated** `SetGlobalOptionNative(option string, value interface{}) error`:
230+
sets an option to a given value, where the type of value is the actual
226231
type of the value internally. Can return an error if the provided value
227232
is not valid for the given option.
228233

0 commit comments

Comments
 (0)