]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/plugin.go
Merge pull request #1028 from filalex77/patch-1
[micro.git] / cmd / micro / plugin.go
index eba09f33e662e4cd89dd53330c08a8b7bd4da6d7..b335078070ba02a4bb7471efe5b276c9d0061f16 100644 (file)
@@ -7,6 +7,7 @@ import (
        "strings"
 
        "github.com/yuin/gopher-lua"
+       "github.com/zyedidia/tcell"
        "layeh.com/gopher-luar"
 )
 
@@ -60,6 +61,20 @@ func LuaFunctionBinding(function string) func(*View, bool) 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 {
@@ -103,10 +118,13 @@ func LuaFunctionComplete(function string) func(string) []string {
        }
 }
 
+// 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, unpack(append([]string{output}, args...))...)
-               if err != nil {
+               if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
                        TermMessage(err)
                }
        }
@@ -119,11 +137,9 @@ func luaPluginName(name string) string {
 
 // LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins
 func LoadPlugins() {
-
        loadedPlugins = make(map[string]string)
 
        for _, plugin := range ListRuntimeFiles(RTPlugin) {
-
                pluginName := plugin.Name()
                if _, ok := loadedPlugins[pluginName]; ok {
                        continue
@@ -136,9 +152,8 @@ func LoadPlugins() {
                }
 
                pluginLuaName := luaPluginName(pluginName)
-               pluginDef := "\nlocal P = {}\n" + pluginLuaName + " = P\nsetmetatable(" + pluginLuaName + ", {__index = _G})\nsetfenv(1, P)\n"
 
-               if err := L.DoString(pluginDef + string(data)); err != nil {
+               if err := LoadFile(pluginLuaName, pluginLuaName, string(data)); err != nil {
                        TermMessage(err)
                        continue
                }
@@ -148,11 +163,22 @@ func LoadPlugins() {
        }
 
        if _, err := os.Stat(configDir + "/init.lua"); err == nil {
-               pluginDef := "\nlocal P = {}\n" + "init" + " = P\nsetmetatable(" + "init" + ", {__index = _G})\nsetfenv(1, P)\n"
                data, _ := ioutil.ReadFile(configDir + "/init.lua")
-               if err := L.DoString(pluginDef + string(data)); err != nil {
+               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
+               }
+       }
+}