]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/plugin.go
Add cd and pwd commands to change the working dir
[micro.git] / cmd / micro / plugin.go
index f02f75df63bc9d6ba17ee8f8bdafed7d11474527..d77baea9ba5fdf4e3c6eaecc757bfe553323aa4b 100644 (file)
@@ -12,12 +12,6 @@ import (
 
 var loadedPlugins []string
 
-var preInstalledPlugins = []string{
-       "go",
-       "linter",
-       "autoclose",
-}
-
 // Call calls the lua function 'function'
 // If it does not exist nothing happens, if there is an error,
 // the error is returned
@@ -85,6 +79,30 @@ func LuaFunctionCommand(function string) func([]string) {
        }
 }
 
+// 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
+       }
+}
+
 func LuaFunctionJob(function string) func(string, ...string) {
        return func(output string, args ...string) {
                _, err := Call(function, unpack(append([]string{output}, args...))...)
@@ -96,47 +114,28 @@ func LuaFunctionJob(function string) func(string, ...string) {
 
 // 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)
-                               }
-                       }
-               }
-       }
-
-       for _, pluginName := range preInstalledPlugins {
+       for _, plugin := range ListRuntimeFiles(RTPlugin) {
                alreadyExists := false
+               pluginName := plugin.Name()
                for _, pl := range loadedPlugins {
                        if pl == pluginName {
                                alreadyExists = true
                                break
                        }
                }
+
                if !alreadyExists {
-                       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
                        }
                        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)
                }
        }