]> git.lizzy.rs Git - micro.git/blob - cmd/micro/plugin.go
Add showkey command
[micro.git] / cmd / micro / plugin.go
1 package main
2
3 import (
4         "errors"
5         "io/ioutil"
6         "os"
7         "strings"
8
9         "github.com/yuin/gopher-lua"
10         "github.com/zyedidia/tcell"
11         "layeh.com/gopher-luar"
12 )
13
14 var loadedPlugins map[string]string
15
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)
25                 }
26                 luaFunc = L.GetField(plugin, strings.Split(function, ".")[1])
27         } else {
28                 luaFunc = L.GetGlobal(function)
29         }
30
31         if luaFunc.String() == "nil" {
32                 return nil, errors.New("function does not exist: " + function)
33         }
34         var luaArgs []lua.LValue
35         for _, v := range args {
36                 luaArgs = append(luaArgs, luar.New(L, v))
37         }
38         err := L.CallByParam(lua.P{
39                 Fn:      luaFunc,
40                 NRet:    1,
41                 Protect: true,
42         }, luaArgs...)
43         ret := L.Get(-1) // returned value
44         if ret.String() != "nil" {
45                 L.Pop(1) // remove received value
46         }
47         return ret, err
48 }
49
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)
57                 if err != nil {
58                         TermMessage(err)
59                 }
60                 return false
61         }
62 }
63
64 // LuaFunctionMouseBinding is a function generator which takes the name of a lua function
65 // and creates a function that will call that lua function
66 // Specifically it creates a function that can be called as a mouse binding because this is used
67 // to bind mouse actions to lua functions
68 func LuaFunctionMouseBinding(function string) func(*View, bool, *tcell.EventMouse) bool {
69         return func(v *View, _ bool, e *tcell.EventMouse) bool {
70                 _, err := Call(function, e)
71                 if err != nil {
72                         TermMessage(err)
73                 }
74                 return false
75         }
76 }
77
78 func unpack(old []string) []interface{} {
79         new := make([]interface{}, len(old))
80         for i, v := range old {
81                 new[i] = v
82         }
83         return new
84 }
85
86 // LuaFunctionCommand is the same as LuaFunctionBinding except it returns a normal function
87 // so that a command can be bound to a lua function
88 func LuaFunctionCommand(function string) func([]string) {
89         return func(args []string) {
90                 _, err := Call(function, unpack(args)...)
91                 if err != nil {
92                         TermMessage(err)
93                 }
94         }
95 }
96
97 // LuaFunctionComplete returns a function which can be used for autocomplete in plugins
98 func LuaFunctionComplete(function string) func(string) []string {
99         return func(input string) (result []string) {
100
101                 res, err := Call(function, input)
102                 if err != nil {
103                         TermMessage(err)
104                 }
105                 if tbl, ok := res.(*lua.LTable); !ok {
106                         TermMessage(function, "should return a table of strings")
107                 } else {
108                         for i := 1; i <= tbl.Len(); i++ {
109                                 val := tbl.RawGetInt(i)
110                                 if v, ok := val.(lua.LString); !ok {
111                                         TermMessage(function, "should return a table of strings")
112                                 } else {
113                                         result = append(result, string(v))
114                                 }
115                         }
116                 }
117                 return result
118         }
119 }
120
121 // LuaFunctionJob returns a function that will call the given lua function
122 // structured as a job call i.e. the job output and arguments are provided
123 // to the lua function
124 func LuaFunctionJob(function string) func(string, ...string) {
125         return func(output string, args ...string) {
126                 _, err := Call(function, unpack(append([]string{output}, args...))...)
127                 if err != nil {
128                         TermMessage(err)
129                 }
130         }
131 }
132
133 // luaPluginName convert a human-friendly plugin name into a valid lua variable name.
134 func luaPluginName(name string) string {
135         return strings.Replace(name, "-", "_", -1)
136 }
137
138 // LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins
139 func LoadPlugins() {
140         loadedPlugins = make(map[string]string)
141
142         for _, plugin := range ListRuntimeFiles(RTPlugin) {
143                 pluginName := plugin.Name()
144                 if _, ok := loadedPlugins[pluginName]; ok {
145                         continue
146                 }
147
148                 data, err := plugin.Data()
149                 if err != nil {
150                         TermMessage("Error loading plugin: " + pluginName)
151                         continue
152                 }
153
154                 pluginLuaName := luaPluginName(pluginName)
155
156                 if err := LoadFile(pluginLuaName, pluginLuaName, string(data)); err != nil {
157                         TermMessage(err)
158                         continue
159                 }
160
161                 loadedPlugins[pluginName] = pluginLuaName
162
163         }
164
165         if _, err := os.Stat(configDir + "/init.lua"); err == nil {
166                 data, _ := ioutil.ReadFile(configDir + "/init.lua")
167                 if err := LoadFile("init", configDir+"init.lua", string(data)); err != nil {
168                         TermMessage(err)
169                 }
170                 loadedPlugins["init"] = "init"
171         }
172 }