Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability lock settings.json and bindings.json for plugins #3618

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/micro/initlua.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func luaImportMicroConfig() *lua.LTable {
ulua.L.SetField(pkg, "OptionComplete", luar.New(ulua.L, action.OptionComplete))
ulua.L.SetField(pkg, "OptionValueComplete", luar.New(ulua.L, action.OptionValueComplete))
ulua.L.SetField(pkg, "NoComplete", luar.New(ulua.L, nil))
ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKey))
ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKeyPlug))
ulua.L.SetField(pkg, "Reload", luar.New(ulua.L, action.ReloadConfig))
ulua.L.SetField(pkg, "AddRuntimeFileFromMemory", luar.New(ulua.L, config.PluginAddRuntimeFileFromMemory))
ulua.L.SetField(pkg, "AddRuntimeFilesFromDirectory", luar.New(ulua.L, config.PluginAddRuntimeFilesFromDirectory))
Expand All @@ -88,8 +88,8 @@ func luaImportMicroConfig() *lua.LTable {
ulua.L.SetField(pkg, "RegisterCommonOption", luar.New(ulua.L, config.RegisterCommonOptionPlug))
ulua.L.SetField(pkg, "RegisterGlobalOption", luar.New(ulua.L, config.RegisterGlobalOptionPlug))
ulua.L.SetField(pkg, "GetGlobalOption", luar.New(ulua.L, config.GetGlobalOption))
ulua.L.SetField(pkg, "SetGlobalOption", luar.New(ulua.L, action.SetGlobalOption))
ulua.L.SetField(pkg, "SetGlobalOptionNative", luar.New(ulua.L, action.SetGlobalOptionNative))
ulua.L.SetField(pkg, "SetGlobalOption", luar.New(ulua.L, action.SetGlobalOptionPlug))
ulua.L.SetField(pkg, "SetGlobalOptionNative", luar.New(ulua.L, action.SetGlobalOptionNativePlug))
ulua.L.SetField(pkg, "ConfigDir", luar.New(ulua.L, config.ConfigDir))

return pkg
Expand Down
7 changes: 7 additions & 0 deletions internal/action/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ func eventsEqual(e1 Event, e2 Event) bool {
return e1 == e2
}

func TryBindKeyPlug(k, v string, overwrite bool) (bool, error) {
if l, ok := config.GlobalSettings["lockbindings"]; ok && l.(bool) {
return false, errors.New("bindings.json file locked by user for all plugins")
}
return TryBindKey(k, v, overwrite)
}

// TryBindKey tries to bind a key by writing to config.ConfigDir/bindings.json
// Returns true if the keybinding already existed and a possible error
func TryBindKey(k, v string, overwrite bool) (bool, error) {
Expand Down
14 changes: 14 additions & 0 deletions internal/action/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,13 @@ func doSetGlobalOptionNative(option string, nativeValue interface{}) error {
return nil
}

func SetGlobalOptionNativePlug(option string, nativeValue interface{}) error {
if l, ok := config.GlobalSettings["locksettings"]; ok && l.(bool) {
return errors.New("settings.json file locked by user for all plugins")
}
return SetGlobalOptionNative(option, nativeValue)
}

func SetGlobalOptionNative(option string, nativeValue interface{}) error {
if err := config.OptionIsValid(option, nativeValue); err != nil {
return err
Expand All @@ -660,6 +667,13 @@ func SetGlobalOptionNative(option string, nativeValue interface{}) error {
return config.WriteSettings(filepath.Join(config.ConfigDir, "settings.json"))
}

func SetGlobalOptionPlug(option, value string) error {
if l, ok := config.GlobalSettings["locksettings"]; ok && l.(bool) {
return errors.New("settings.json file locked by user for all plugins")
}
return SetGlobalOption(option, value)
}

func SetGlobalOption(option, value string) error {
if _, ok := config.GlobalSettings[option]; !ok {
return config.ErrInvalidOption
Expand Down
2 changes: 2 additions & 0 deletions internal/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ var DefaultGlobalOnlySettings = map[string]interface{}{
"helpsplit": "hsplit",
"infobar": true,
"keymenu": false,
"lockbindings": false,
"locksettings": false,
"mouse": true,
"multiopen": "tab",
"parsecursor": false,
Expand Down
10 changes: 10 additions & 0 deletions runtime/help/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ Here are the available options:

default value: `false`

* `lockbindings`: Disable plugins to modify the `bindings.json` in your config
directory.

default value: `false`

* `locksettings`: Disable plugins to modify the `settings.json` in your config
directory.

default value: `false`

* `matchbrace`: show matching braces for '()', '{}', '[]' when the cursor
is on a brace character or (if `matchbraceleft` is enabled) next to it.

Expand Down
Loading