9 "github.com/yuin/gopher-lua"
10 "github.com/zyedidia/tcell"
11 "layeh.com/gopher-luar"
14 var loadedPlugins map[string]string
16 // Call calls the lua function 'function'
17 // If it does not exist nothing happens, if there is an error,
18 // the error is returned
19 func Call(function string, args ...interface{}) (lua.LValue, error) {
20 var luaFunc lua.LValue
21 if strings.Contains(function, ".") {
22 plugin := L.GetGlobal(strings.Split(function, ".")[0])
23 if plugin.String() == "nil" {
24 return nil, errors.New("function does not exist: " + function)
26 luaFunc = L.GetField(plugin, strings.Split(function, ".")[1])
28 luaFunc = L.GetGlobal(function)
31 if luaFunc.String() == "nil" {
32 return nil, errors.New("function does not exist: " + function)
34 var luaArgs []lua.LValue
35 for _, v := range args {
36 luaArgs = append(luaArgs, luar.New(L, v))
38 err := L.CallByParam(lua.P{
43 ret := L.Get(-1) // returned value
44 if ret.String() != "nil" {
45 L.Pop(1) // remove received value
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) bool {
55 return func(v *View, _ bool) bool {
56 _, err := Call(function, nil)
64 func LuaFunctionMouseBinding(function string) func(*View, bool, *tcell.EventMouse) bool {
65 return func(v *View, _ bool, e *tcell.EventMouse) bool {
66 _, err := Call(function, e)
74 func unpack(old []string) []interface{} {
75 new := make([]interface{}, len(old))
76 for i, v := range old {
82 // LuaFunctionCommand is the same as LuaFunctionBinding except it returns a normal function
83 // so that a command can be bound to a lua function
84 func LuaFunctionCommand(function string) func([]string) {
85 return func(args []string) {
86 _, err := Call(function, unpack(args)...)
93 // LuaFunctionComplete returns a function which can be used for autocomplete in plugins
94 func LuaFunctionComplete(function string) func(string) []string {
95 return func(input string) (result []string) {
97 res, err := Call(function, input)
101 if tbl, ok := res.(*lua.LTable); !ok {
102 TermMessage(function, "should return a table of strings")
104 for i := 1; i <= tbl.Len(); i++ {
105 val := tbl.RawGetInt(i)
106 if v, ok := val.(lua.LString); !ok {
107 TermMessage(function, "should return a table of strings")
109 result = append(result, string(v))
117 func LuaFunctionJob(function string) func(string, ...string) {
118 return func(output string, args ...string) {
119 _, err := Call(function, unpack(append([]string{output}, args...))...)
126 // luaPluginName convert a human-friendly plugin name into a valid lua variable name.
127 func luaPluginName(name string) string {
128 return strings.Replace(name, "-", "_", -1)
131 // LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins
133 loadedPlugins = make(map[string]string)
135 for _, plugin := range ListRuntimeFiles(RTPlugin) {
136 pluginName := plugin.Name()
137 if _, ok := loadedPlugins[pluginName]; ok {
141 data, err := plugin.Data()
143 TermMessage("Error loading plugin: " + pluginName)
147 pluginLuaName := luaPluginName(pluginName)
149 if err := LoadFile(pluginLuaName, pluginLuaName, string(data)); err != nil {
154 loadedPlugins[pluginName] = pluginLuaName
158 if _, err := os.Stat(configDir + "/init.lua"); err == nil {
159 data, _ := ioutil.ReadFile(configDir + "/init.lua")
160 if err := LoadFile("init", configDir+"init.lua", string(data)); err != nil {
163 loadedPlugins["init"] = "init"