7 "github.com/layeh/gopher-luar"
8 "github.com/yuin/gopher-lua"
11 var loadedPlugins []string
13 var preInstalledPlugins = []string{
18 // Call calls the lua function 'function'
19 // If it does not exist nothing happens, if there is an error,
20 // the error is returned
21 func Call(function string, args []string) error {
22 luaFunc := L.GetGlobal(function)
23 if luaFunc.String() == "nil" {
24 return errors.New("function does not exist: " + function)
26 luaArgs := luar.New(L, args)
27 err := L.CallByParam(lua.P{
35 // LuaFunctionBinding is a function generator which takes the name of a lua function
36 // and creates a function that will call that lua function
37 // Specifically it creates a function that can be called as a binding because this is used
38 // to bind keys to lua functions
39 func LuaFunctionBinding(function string) func(*View) bool {
40 return func(v *View) bool {
41 err := Call(function, nil)
49 // LuaFunctionCommand is the same as LuaFunctionBinding except it returns a normal function
50 // so that a command can be bound to a lua function
51 func LuaFunctionCommand(function string) func([]string) {
52 return func(args []string) {
53 err := Call(function, args)
60 // LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins
62 files, _ := ioutil.ReadDir(configDir + "/plugins")
63 for _, plugin := range files {
65 pluginName := plugin.Name()
66 files, _ := ioutil.ReadDir(configDir + "/plugins/" + pluginName)
67 for _, f := range files {
68 if f.Name() == pluginName+".lua" {
69 if err := L.DoFile(configDir + "/plugins/" + pluginName + "/" + f.Name()); err != nil {
73 loadedPlugins = append(loadedPlugins, pluginName)
79 for _, pluginName := range preInstalledPlugins {
80 plugin := "runtime/plugins/" + pluginName + "/" + pluginName + ".lua"
81 data, err := Asset(plugin)
83 TermMessage("Error loading pre-installed plugin: " + pluginName)
86 if err := L.DoString(string(data)); err != nil {
90 loadedPlugins = append(loadedPlugins, pluginName)