import (
"errors"
+ "log"
lua "github.com/yuin/gopher-lua"
- ulua "github.com/zyedidia/micro/internal/lua"
+ ulua "github.com/zyedidia/micro/v2/internal/lua"
)
+// ErrNoSuchFunction is returned when Call is executed on a function that does not exist
var ErrNoSuchFunction = errors.New("No such function exists")
// LoadAllPlugins loads all detected plugins (in runtime/plugins and ConfigDir/plugins)
reterr = errors.New("Plugin " + p.Name + ": " + err.Error())
continue
}
- if v, ok := val.(lua.LBool); !ok {
- reterr = errors.New(p.Name + "." + fn + " should return a boolean")
- } else {
+ if v, ok := val.(lua.LBool); ok {
retbool = retbool && bool(v)
}
}
return retbool, reterr
}
+// Plugin stores information about the source files/info for a plugin
type Plugin struct {
+ DirName string // name of plugin folder
Name string // name of plugin
Info *PluginInfo // json file containing info
Srcs []RuntimeFile // lua files
Default bool // pre-installed plugin
}
+// IsEnabled returns if a plugin is enabled
func (p *Plugin) IsEnabled() bool {
if v, ok := GlobalSettings[p.Name]; ok {
return v.(bool) && p.Loaded
return true
}
+// Plugins is a list of all detected plugins (enabled or disabled)
var Plugins []*Plugin
+// Load creates an option for the plugin and runs all source files
func (p *Plugin) Load() error {
+ if v, ok := GlobalSettings[p.Name]; ok && !v.(bool) {
+ return nil
+ }
for _, f := range p.Srcs {
- if v, ok := GlobalSettings[p.Name]; ok && !v.(bool) {
- return nil
- }
dat, err := f.Data()
if err != nil {
return err
if err != nil {
return err
}
- p.Loaded = true
- RegisterGlobalOption(p.Name, true)
}
+ p.Loaded = true
+ RegisterGlobalOption(p.Name, true)
return nil
}
+// Call calls a given function in this plugin
func (p *Plugin) Call(fn string, args ...lua.LValue) (lua.LValue, error) {
plug := ulua.L.GetGlobal(p.Name)
+ if plug == lua.LNil {
+ log.Println("Plugin does not exist:", p.Name, "at", p.DirName, ":", p)
+ return nil, nil
+ }
luafn := ulua.L.GetField(plug, fn)
if luafn == lua.LNil {
return nil, ErrNoSuchFunction
return ret, nil
}
+// FindPlugin returns the plugin with the given name
func FindPlugin(name string) *Plugin {
+ var pl *Plugin
+ for _, p := range Plugins {
+ if !p.IsEnabled() {
+ continue
+ }
+ if p.Name == name {
+ pl = p
+ break
+ }
+ }
+ return pl
+}
+
+// FindAnyPlugin does not require the plugin to be enabled
+func FindAnyPlugin(name string) *Plugin {
var pl *Plugin
for _, p := range Plugins {
if p.Name == name {