]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/plugin.go
Code optimisation (#1117)
[micro.git] / cmd / micro / plugin.go
index c01cc28bee41f91698136388eccba1f572ee1b99..b335078070ba02a4bb7471efe5b276c9d0061f16 100644 (file)
@@ -3,24 +3,20 @@ package main
 import (
        "errors"
        "io/ioutil"
+       "os"
        "strings"
 
-       "github.com/layeh/gopher-luar"
        "github.com/yuin/gopher-lua"
+       "github.com/zyedidia/tcell"
+       "layeh.com/gopher-luar"
 )
 
-var loadedPlugins []string
-
-var preInstalledPlugins = []string{
-       "go",
-       "linter",
-       "autoclose",
-}
+var loadedPlugins map[string]string
 
 // Call calls the lua function 'function'
 // If it does not exist nothing happens, if there is an error,
 // the error is returned
-func Call(function string, args []string) (lua.LValue, error) {
+func Call(function string, args ...interface{}) (lua.LValue, error) {
        var luaFunc lua.LValue
        if strings.Contains(function, ".") {
                plugin := L.GetGlobal(strings.Split(function, ".")[0])
@@ -55,8 +51,8 @@ func Call(function string, args []string) (lua.LValue, error) {
 // and creates a function that will call that lua function
 // Specifically it creates a function that can be called as a binding because this is used
 // to bind keys to lua functions
-func LuaFunctionBinding(function string) func(*View) bool {
-       return func(v *View) bool {
+func LuaFunctionBinding(function string) func(*View, bool) bool {
+       return func(v *View, _ bool) bool {
                _, err := Call(function, nil)
                if err != nil {
                        TermMessage(err)
@@ -65,60 +61,124 @@ func LuaFunctionBinding(function string) func(*View) bool {
        }
 }
 
+// LuaFunctionMouseBinding is a function generator which takes the name of a lua function
+// and creates a function that will call that lua function
+// Specifically it creates a function that can be called as a mouse binding because this is used
+// to bind mouse actions to lua functions
+func LuaFunctionMouseBinding(function string) func(*View, bool, *tcell.EventMouse) bool {
+       return func(v *View, _ bool, e *tcell.EventMouse) bool {
+               _, err := Call(function, e)
+               if err != nil {
+                       TermMessage(err)
+               }
+               return false
+       }
+}
+
+func unpack(old []string) []interface{} {
+       new := make([]interface{}, len(old))
+       for i, v := range old {
+               new[i] = v
+       }
+       return new
+}
+
 // LuaFunctionCommand is the same as LuaFunctionBinding except it returns a normal function
 // so that a command can be bound to a lua function
 func LuaFunctionCommand(function string) func([]string) {
        return func(args []string) {
-               _, err := Call(function, args)
+               _, err := Call(function, unpack(args)...)
                if err != nil {
                        TermMessage(err)
                }
        }
 }
 
+// LuaFunctionComplete returns a function which can be used for autocomplete in plugins
+func LuaFunctionComplete(function string) func(string) []string {
+       return func(input string) (result []string) {
+
+               res, err := Call(function, input)
+               if err != nil {
+                       TermMessage(err)
+               }
+               if tbl, ok := res.(*lua.LTable); !ok {
+                       TermMessage(function, "should return a table of strings")
+               } else {
+                       for i := 1; i <= tbl.Len(); i++ {
+                               val := tbl.RawGetInt(i)
+                               if v, ok := val.(lua.LString); !ok {
+                                       TermMessage(function, "should return a table of strings")
+                               } else {
+                                       result = append(result, string(v))
+                               }
+                       }
+               }
+               return result
+       }
+}
+
+// LuaFunctionJob returns a function that will call the given lua function
+// structured as a job call i.e. the job output and arguments are provided
+// to the lua function
 func LuaFunctionJob(function string) func(string, ...string) {
        return func(output string, args ...string) {
-               _, err := Call(function, append([]string{output}, args...))
-               if err != nil {
+               _, err := Call(function, unpack(append([]string{output}, args...))...)
+               if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
                        TermMessage(err)
                }
        }
 }
 
+// luaPluginName convert a human-friendly plugin name into a valid lua variable name.
+func luaPluginName(name string) string {
+       return strings.Replace(name, "-", "_", -1)
+}
+
 // LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins
 func LoadPlugins() {
-       files, _ := ioutil.ReadDir(configDir + "/plugins")
-       for _, plugin := range files {
-               if plugin.IsDir() {
-                       pluginName := plugin.Name()
-                       files, _ := ioutil.ReadDir(configDir + "/plugins/" + pluginName)
-                       for _, f := range files {
-                               if f.Name() == pluginName+".lua" {
-                                       data, _ := ioutil.ReadFile(configDir + "/plugins/" + pluginName + "/" + f.Name())
-                                       pluginDef := "\nlocal P = {}\n" + pluginName + " = P\nsetmetatable(" + pluginName + ", {__index = _G})\nsetfenv(1, P)\n"
-
-                                       if err := L.DoString(pluginDef + string(data)); err != nil {
-                                               TermMessage(err)
-                                               continue
-                                       }
-                                       loadedPlugins = append(loadedPlugins, pluginName)
-                               }
-                       }
+       loadedPlugins = make(map[string]string)
+
+       for _, plugin := range ListRuntimeFiles(RTPlugin) {
+               pluginName := plugin.Name()
+               if _, ok := loadedPlugins[pluginName]; ok {
+                       continue
                }
-       }
 
-       for _, pluginName := range preInstalledPlugins {
-               plugin := "runtime/plugins/" + pluginName + "/" + pluginName + ".lua"
-               data, err := Asset(plugin)
+               data, err := plugin.Data()
                if err != nil {
-                       TermMessage("Error loading pre-installed plugin: " + pluginName)
+                       TermMessage("Error loading plugin: " + pluginName)
+                       continue
+               }
+
+               pluginLuaName := luaPluginName(pluginName)
+
+               if err := LoadFile(pluginLuaName, pluginLuaName, string(data)); err != nil {
+                       TermMessage(err)
                        continue
                }
-               pluginDef := "\nlocal P = {}\n" + pluginName + " = P\nsetmetatable(" + pluginName + ", {__index = _G})\nsetfenv(1, P)\n"
-               if err := L.DoString(pluginDef + string(data)); err != nil {
+
+               loadedPlugins[pluginName] = pluginLuaName
+
+       }
+
+       if _, err := os.Stat(configDir + "/init.lua"); err == nil {
+               data, _ := ioutil.ReadFile(configDir + "/init.lua")
+               if err := LoadFile("init", configDir+"init.lua", string(data)); err != nil {
+                       TermMessage(err)
+               }
+               loadedPlugins["init"] = "init"
+       }
+}
+
+// GlobalCall makes a call to a function in every plugin that is currently
+// loaded
+func GlobalPluginCall(function string, args ...interface{}) {
+       for pl := range loadedPlugins {
+               _, err := Call(pl+"."+function, args...)
+               if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
                        TermMessage(err)
                        continue
                }
-               loadedPlugins = append(loadedPlugins, pluginName)
        }
 }