Skip to content

Commit 8843128

Browse files
Ability to lock bindings, migrate to register* functions instead
1 parent 06fe85c commit 8843128

6 files changed

Lines changed: 59 additions & 18 deletions

File tree

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]interface{}
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
@@ -783,7 +783,7 @@ func (h *BufPane) BindCmd(args []string) {
783783
return
784784
}
785785

786-
_, err := TryBindKey(parseKeyArg(args[0]), args[1], true)
786+
_, err := TryBindKey(parseKeyArg(args[0]), args[1], true, true)
787787
if err != nil {
788788
if errors.Is(err, util.ErrOverwrite) {
789789
screen.TermMessage(err)

internal/config/settings.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ var DefaultGlobalOnlySettings = map[string]interface{}{
115115
"helpsplit": "hsplit",
116116
"infobar": true,
117117
"keymenu": false,
118+
"lockbindings": false,
118119
"mouse": true,
119120
"multiopen": "tab",
120121
"parsecursor": false,
@@ -398,6 +399,16 @@ func RegisterGlobalOptionPlug(pl string, name string, defaultvalue interface{})
398399
return RegisterGlobalOption(pl+"."+name, defaultvalue)
399400
}
400401

402+
// **Deprecated**
403+
func SetGlobalOptionNativePlug(option string, nativeValue interface{}) error {
404+
return RegisterGlobalOption(option, nativeValue)
405+
}
406+
407+
// **Deprecated**
408+
func SetGlobalOptionPlug(option, value string) error {
409+
return RegisterGlobalOption(option, value)
410+
}
411+
401412
// RegisterCommonOption creates a new option
402413
func RegisterCommonOption(name string, defaultvalue interface{}) error {
403414
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
@@ -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)