]> git.lizzy.rs Git - micro.git/blob - cmd/micro/plugin.go
Merge pull request #168 from onodera-punpun/syntax_css
[micro.git] / cmd / micro / plugin.go
1 package main
2
3 import (
4         "errors"
5         "io/ioutil"
6
7         "github.com/layeh/gopher-luar"
8         "github.com/yuin/gopher-lua"
9 )
10
11 var loadedPlugins []string
12
13 var preInstalledPlugins = []string{
14         "go",
15         "linter",
16 }
17
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)
25         }
26         luaArgs := luar.New(L, args)
27         err := L.CallByParam(lua.P{
28                 Fn:      luaFunc,
29                 NRet:    0,
30                 Protect: true,
31         }, luaArgs)
32         return err
33 }
34
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)
42                 if err != nil {
43                         TermMessage(err)
44                 }
45                 return false
46         }
47 }
48
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)
54                 if err != nil {
55                         TermMessage(err)
56                 }
57         }
58 }
59
60 // LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins
61 func LoadPlugins() {
62         files, _ := ioutil.ReadDir(configDir + "/plugins")
63         for _, plugin := range files {
64                 if plugin.IsDir() {
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 {
70                                                 TermMessage(err)
71                                                 continue
72                                         }
73                                         loadedPlugins = append(loadedPlugins, pluginName)
74                                 }
75                         }
76                 }
77         }
78
79         for _, pluginName := range preInstalledPlugins {
80                 plugin := "runtime/plugins/" + pluginName + "/" + pluginName + ".lua"
81                 data, err := Asset(plugin)
82                 if err != nil {
83                         TermMessage("Error loading pre-installed plugin: " + pluginName)
84                         continue
85                 }
86                 if err := L.DoString(string(data)); err != nil {
87                         TermMessage(err)
88                         continue
89                 }
90                 loadedPlugins = append(loadedPlugins, pluginName)
91         }
92 }