Allow any plugin to be enabled or disabled via settings

This commit is contained in:
Zachary Yedidia
2019-08-02 13:32:44 -07:00
parent 576036f251
commit a47e1f0ca5
3 changed files with 28 additions and 6 deletions

View File

@@ -365,6 +365,12 @@ func SetGlobalOptionNative(option string, nativeValue interface{}) error {
} else {
screen.Screen.EnableMouse()
}
} else {
for _, pl := range config.Plugins {
if option == pl.Name && nativeValue.(bool) && !pl.Loaded {
pl.Load()
}
}
}
for _, b := range buffer.OpenBuffers {

View File

@@ -2,7 +2,6 @@ package config
import (
"errors"
"log"
lua "github.com/yuin/gopher-lua"
ulua "github.com/zyedidia/micro/internal/lua"
@@ -21,7 +20,9 @@ func LoadAllPlugins() {
func RunPluginFn(fn string, args ...lua.LValue) error {
var reterr error
for _, p := range Plugins {
log.Println(p.Name, fn)
if !p.IsEnabled() {
continue
}
_, err := p.Call(fn, args...)
if err != nil && err != ErrNoSuchFunction {
reterr = errors.New("Plugin " + p.Name + ": " + err.Error())
@@ -31,15 +32,26 @@ func RunPluginFn(fn string, args ...lua.LValue) error {
}
type Plugin struct {
Name string // name of plugin
Info RuntimeFile // json file containing info
Srcs []RuntimeFile // lua files
Name string // name of plugin
Info RuntimeFile // json file containing info
Srcs []RuntimeFile // lua files
Loaded bool
}
func (p *Plugin) IsEnabled() bool {
if v, ok := GlobalSettings[p.Name]; ok {
return v.(bool)
}
return true
}
var Plugins []*Plugin
func (p *Plugin) Load() error {
for _, f := range p.Srcs {
if !p.IsEnabled() {
return nil
}
dat, err := f.Data()
if err != nil {
return err
@@ -48,6 +60,10 @@ func (p *Plugin) Load() error {
if err != nil {
return err
}
p.Loaded = true
if _, ok := GlobalSettings[p.Name]; !ok {
AddOption(p.Name, true)
}
}
return nil
}

View File

@@ -62,7 +62,7 @@ func SetStatusInfoFnLua(s string, fn string) {
}
}
statusInfo[s] = func(b *buffer.Buffer) string {
if pl == nil {
if pl == nil || !pl.IsEnabled() {
return ""
}
val, err := pl.Call(plFn, luar.New(ulua.L, b))