]> git.lizzy.rs Git - micro.git/blob - cmd/micro/plugin.go
Fix replace cursor relocation
[micro.git] / cmd / micro / plugin.go
1 package main
2
3 import (
4         "errors"
5         "io/ioutil"
6         "strings"
7
8         "github.com/layeh/gopher-luar"
9         "github.com/yuin/gopher-lua"
10 )
11
12 var loadedPlugins []string
13
14 var preInstalledPlugins = []string{
15         "go",
16         "linter",
17         "autoclose",
18 }
19
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)
29                 }
30                 luaFunc = L.GetField(plugin, strings.Split(function, ".")[1])
31         } else {
32                 luaFunc = L.GetGlobal(function)
33         }
34
35         if luaFunc.String() == "nil" {
36                 return errors.New("function does not exist: " + function)
37         }
38         var luaArgs []lua.LValue
39         for _, v := range args {
40                 luaArgs = append(luaArgs, luar.New(L, v))
41         }
42         err := L.CallByParam(lua.P{
43                 Fn:      luaFunc,
44                 NRet:    0,
45                 Protect: true,
46         }, luaArgs...)
47         return 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 {
55         return func(v *View) bool {
56                 err := Call(function, nil)
57                 if err != nil {
58                         TermMessage(err)
59                 }
60                 return false
61         }
62 }
63
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)
69                 if err != nil {
70                         TermMessage(err)
71                 }
72         }
73 }
74
75 func LuaFunctionJob(function string) func(string, ...string) {
76         return func(output string, args ...string) {
77                 err := Call(function, append([]string{output}, args...))
78                 if err != nil {
79                         TermMessage(err)
80                 }
81         }
82 }
83
84 // LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins
85 func LoadPlugins() {
86         files, _ := ioutil.ReadDir(configDir + "/plugins")
87         for _, plugin := range files {
88                 if plugin.IsDir() {
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"
95
96                                         if err := L.DoString(pluginDef + string(data)); err != nil {
97                                                 TermMessage(err)
98                                                 continue
99                                         }
100                                         loadedPlugins = append(loadedPlugins, pluginName)
101                                 }
102                         }
103                 }
104         }
105
106         for _, pluginName := range preInstalledPlugins {
107                 plugin := "runtime/plugins/" + pluginName + "/" + pluginName + ".lua"
108                 data, err := Asset(plugin)
109                 if err != nil {
110                         TermMessage("Error loading pre-installed plugin: " + pluginName)
111                         continue
112                 }
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 {
115                         TermMessage(err)
116                         continue
117                 }
118                 loadedPlugins = append(loadedPlugins, pluginName)
119         }
120 }