mirror of
https://github.com/zyedidia/micro.git
synced 2026-02-06 07:00:24 +09:00
Plugins can now create their own commands using the `MakeCommand` function. Plugins can also now create their own keybindings with the `BindKey` function. See the go plugin for an example of `MakeCommand`.
93 lines
2.3 KiB
Go
93 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"io/ioutil"
|
|
|
|
"github.com/layeh/gopher-luar"
|
|
"github.com/yuin/gopher-lua"
|
|
)
|
|
|
|
var loadedPlugins []string
|
|
|
|
var preInstalledPlugins = []string{
|
|
"go",
|
|
"linter",
|
|
}
|
|
|
|
// Call calls the lua function 'function'
|
|
// If it does not exist nothing happens, if there is an error,
|
|
// the error is returned
|
|
func Call(function string, args []string) error {
|
|
luaFunc := L.GetGlobal(function)
|
|
if luaFunc.String() == "nil" {
|
|
return errors.New("function does not exist: " + function)
|
|
}
|
|
luaArgs := luar.New(L, args)
|
|
err := L.CallByParam(lua.P{
|
|
Fn: luaFunc,
|
|
NRet: 0,
|
|
Protect: true,
|
|
}, luaArgs)
|
|
return err
|
|
}
|
|
|
|
// LuaFunctionBinding is a function generator which takes the name of a lua function
|
|
// and creates a function that will call that lua function
|
|
// Specifically it creates a function that can be called as a binding because this is used
|
|
// to bind keys to lua functions
|
|
func LuaFunctionBinding(function string) func(*View) bool {
|
|
return func(v *View) bool {
|
|
err := Call(function, nil)
|
|
if err != nil {
|
|
TermMessage(err)
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
// LuaFunctionCommand is the same as LuaFunctionBinding except it returns a normal function
|
|
// so that a command can be bound to a lua function
|
|
func LuaFunctionCommand(function string) func([]string) {
|
|
return func(args []string) {
|
|
err := Call(function, args)
|
|
if err != nil {
|
|
TermMessage(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins
|
|
func LoadPlugins() {
|
|
files, _ := ioutil.ReadDir(configDir + "/plugins")
|
|
for _, plugin := range files {
|
|
if plugin.IsDir() {
|
|
pluginName := plugin.Name()
|
|
files, _ := ioutil.ReadDir(configDir + "/plugins/" + pluginName)
|
|
for _, f := range files {
|
|
if f.Name() == pluginName+".lua" {
|
|
if err := L.DoFile(configDir + "/plugins/" + pluginName + "/" + f.Name()); err != nil {
|
|
TermMessage(err)
|
|
continue
|
|
}
|
|
loadedPlugins = append(loadedPlugins, pluginName)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, pluginName := range preInstalledPlugins {
|
|
plugin := "runtime/plugins/" + pluginName + "/" + pluginName + ".lua"
|
|
data, err := Asset(plugin)
|
|
if err != nil {
|
|
TermMessage("Error loading pre-installed plugin: " + pluginName)
|
|
continue
|
|
}
|
|
if err := L.DoString(string(data)); err != nil {
|
|
TermMessage(err)
|
|
continue
|
|
}
|
|
loadedPlugins = append(loadedPlugins, pluginName)
|
|
}
|
|
}
|