9 "github.com/layeh/gopher-luar"
10 "github.com/yuin/gopher-lua"
13 var loadedPlugins []string
15 var preInstalledPlugins = []string{
21 // Call calls the lua function 'function'
22 // If it does not exist nothing happens, if there is an error,
23 // the error is returned
24 func Call(function string, args ...interface{}) (lua.LValue, error) {
25 var luaFunc lua.LValue
26 if strings.Contains(function, ".") {
27 plugin := L.GetGlobal(strings.Split(function, ".")[0])
28 if plugin.String() == "nil" {
29 return nil, errors.New("function does not exist: " + function)
31 luaFunc = L.GetField(plugin, strings.Split(function, ".")[1])
33 luaFunc = L.GetGlobal(function)
36 if luaFunc.String() == "nil" {
37 return nil, errors.New("function does not exist: " + function)
39 var luaArgs []lua.LValue
40 for _, v := range args {
41 luaArgs = append(luaArgs, luar.New(L, v))
43 err := L.CallByParam(lua.P{
48 ret := L.Get(-1) // returned value
49 if ret.String() != "nil" {
50 L.Pop(1) // remove received value
55 // LuaFunctionBinding is a function generator which takes the name of a lua function
56 // and creates a function that will call that lua function
57 // Specifically it creates a function that can be called as a binding because this is used
58 // to bind keys to lua functions
59 func LuaFunctionBinding(function string) func(*View, bool) bool {
60 return func(v *View, _ bool) bool {
61 _, err := Call(function, nil)
69 func unpack(old []string) []interface{} {
70 new := make([]interface{}, len(old))
71 for i, v := range old {
77 // LuaFunctionCommand is the same as LuaFunctionBinding except it returns a normal function
78 // so that a command can be bound to a lua function
79 func LuaFunctionCommand(function string) func([]string) {
80 return func(args []string) {
81 _, err := Call(function, unpack(args)...)
88 // LuaFunctionComplete returns a function which can be used for autocomplete in plugins
89 func LuaFunctionComplete(function string) func(string) []string {
90 return func(input string) (result []string) {
92 res, err := Call(function, input)
96 if tbl, ok := res.(*lua.LTable); !ok {
97 TermMessage(function, "should return a table of strings")
99 for i := 1; i <= tbl.Len(); i++ {
100 val := tbl.RawGetInt(i)
101 if v, ok := val.(lua.LString); !ok {
102 TermMessage(function, "should return a table of strings")
104 result = append(result, string(v))
112 func LuaFunctionJob(function string) func(string, ...string) {
113 return func(output string, args ...string) {
114 _, err := Call(function, unpack(append([]string{output}, args...))...)
121 // LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins
123 files, _ := ioutil.ReadDir(configDir + "/plugins")
124 for _, plugin := range files {
126 pluginName := plugin.Name()
127 files, _ := ioutil.ReadDir(configDir + "/plugins/" + pluginName)
128 for _, f := range files {
129 if f.Name() == pluginName+".lua" {
130 data, _ := ioutil.ReadFile(configDir + "/plugins/" + pluginName + "/" + f.Name())
131 pluginDef := "\nlocal P = {}\n" + pluginName + " = P\nsetmetatable(" + pluginName + ", {__index = _G})\nsetfenv(1, P)\n"
133 if err := L.DoString(pluginDef + string(data)); err != nil {
137 loadedPlugins = append(loadedPlugins, pluginName)
143 for _, pluginName := range preInstalledPlugins {
144 alreadyExists := false
145 for _, pl := range loadedPlugins {
146 if pl == pluginName {
152 plugin := "runtime/plugins/" + pluginName + "/" + pluginName + ".lua"
153 data, err := Asset(plugin)
155 TermMessage("Error loading pre-installed plugin: " + pluginName)
158 pluginDef := "\nlocal P = {}\n" + pluginName + " = P\nsetmetatable(" + pluginName + ", {__index = _G})\nsetfenv(1, P)\n"
159 if err := L.DoString(pluginDef + string(data)); err != nil {
164 loadedPlugins = append(loadedPlugins, pluginName)
168 if _, err := os.Stat(configDir + "/init.lua"); err == nil {
169 pluginDef := "\nlocal P = {}\n" + "init" + " = P\nsetmetatable(" + "init" + ", {__index = _G})\nsetfenv(1, P)\n"
170 data, _ := ioutil.ReadFile(configDir + "/init.lua")
171 if err := L.DoString(pluginDef + string(data)); err != nil {
174 loadedPlugins = append(loadedPlugins, "init")