8 "github.com/layeh/gopher-luar"
9 "github.com/yuin/gopher-lua"
12 var loadedPlugins []string
14 var preInstalledPlugins = []string{
20 // Call calls the lua function 'function'
21 // If it does not exist nothing happens, if there is an error,
22 // the error is returned
23 func Call(function string, args []string) error {
24 var luaFunc lua.LValue
25 if strings.Contains(function, ".") {
26 plugin := L.GetGlobal(strings.Split(function, ".")[0])
27 if plugin.String() == "nil" {
28 return errors.New("function does not exist: " + function)
30 luaFunc = L.GetField(plugin, strings.Split(function, ".")[1])
32 luaFunc = L.GetGlobal(function)
35 if luaFunc.String() == "nil" {
36 return errors.New("function does not exist: " + function)
38 var luaArgs []lua.LValue
39 for _, v := range args {
40 luaArgs = append(luaArgs, luar.New(L, v))
42 err := L.CallByParam(lua.P{
50 // LuaFunctionBinding is a function generator which takes the name of a lua function
51 // and creates a function that will call that lua function
52 // Specifically it creates a function that can be called as a binding because this is used
53 // to bind keys to lua functions
54 func LuaFunctionBinding(function string) func(*View) bool {
55 return func(v *View) bool {
56 err := Call(function, nil)
64 // LuaFunctionCommand is the same as LuaFunctionBinding except it returns a normal function
65 // so that a command can be bound to a lua function
66 func LuaFunctionCommand(function string) func([]string) {
67 return func(args []string) {
68 err := Call(function, args)
75 func LuaFunctionJob(function string) func(string, ...string) {
76 return func(output string, args ...string) {
77 err := Call(function, append([]string{output}, args...))
84 // LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins
86 files, _ := ioutil.ReadDir(configDir + "/plugins")
87 for _, plugin := range files {
89 pluginName := plugin.Name()
90 files, _ := ioutil.ReadDir(configDir + "/plugins/" + pluginName)
91 for _, f := range files {
92 if f.Name() == pluginName+".lua" {
93 data, _ := ioutil.ReadFile(configDir + "/plugins/" + pluginName + "/" + f.Name())
94 pluginDef := "\nlocal P = {}\n" + pluginName + " = P\nsetmetatable(" + pluginName + ", {__index = _G})\nsetfenv(1, P)\n"
96 if err := L.DoString(pluginDef + string(data)); err != nil {
100 loadedPlugins = append(loadedPlugins, pluginName)
106 for _, pluginName := range preInstalledPlugins {
107 plugin := "runtime/plugins/" + pluginName + "/" + pluginName + ".lua"
108 data, err := Asset(plugin)
110 TermMessage("Error loading pre-installed plugin: " + pluginName)
113 pluginDef := "\nlocal P = {}\n" + pluginName + " = P\nsetmetatable(" + pluginName + ", {__index = _G})\nsetfenv(1, P)\n"
114 if err := L.DoString(pluginDef + string(data)); err != nil {
118 loadedPlugins = append(loadedPlugins, pluginName)