import (
"errors"
"io/ioutil"
+ "os"
"strings"
"github.com/layeh/gopher-luar"
// 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 {
+func Call(function string, args []string) (lua.LValue, error) {
var luaFunc lua.LValue
if strings.Contains(function, ".") {
plugin := L.GetGlobal(strings.Split(function, ".")[0])
if plugin.String() == "nil" {
- return errors.New("function does not exist: " + function)
+ return nil, errors.New("function does not exist: " + function)
}
luaFunc = L.GetField(plugin, strings.Split(function, ".")[1])
} else {
}
if luaFunc.String() == "nil" {
- return errors.New("function does not exist: " + function)
+ return nil, errors.New("function does not exist: " + function)
}
var luaArgs []lua.LValue
for _, v := range args {
}
err := L.CallByParam(lua.P{
Fn: luaFunc,
- NRet: 0,
+ NRet: 1,
Protect: true,
}, luaArgs...)
- return err
+ ret := L.Get(-1) // returned value
+ if ret.String() != "nil" {
+ L.Pop(1) // remove received value
+ }
+ return ret, 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)
+func LuaFunctionBinding(function string) func(*View, bool) bool {
+ return func(v *View, _ bool) bool {
+ _, err := Call(function, nil)
if err != nil {
TermMessage(err)
}
// 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)
+ _, err := Call(function, args)
if err != nil {
TermMessage(err)
}
func LuaFunctionJob(function string) func(string, ...string) {
return func(output string, args ...string) {
- err := Call(function, append([]string{output}, args...))
+ _, err := Call(function, append([]string{output}, args...))
if err != nil {
TermMessage(err)
}
}
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
+ alreadyExists := false
+ for _, pl := range loadedPlugins {
+ if pl == pluginName {
+ alreadyExists = true
+ break
+ }
}
- pluginDef := "\nlocal P = {}\n" + pluginName + " = P\nsetmetatable(" + pluginName + ", {__index = _G})\nsetfenv(1, P)\n"
- if err := L.DoString(pluginDef + string(data)); err != nil {
+ if !alreadyExists {
+ plugin := "runtime/plugins/" + pluginName + "/" + pluginName + ".lua"
+ data, err := Asset(plugin)
+ if err != nil {
+ TermMessage("Error loading pre-installed plugin: " + pluginName)
+ continue
+ }
+ pluginDef := "\nlocal P = {}\n" + pluginName + " = P\nsetmetatable(" + pluginName + ", {__index = _G})\nsetfenv(1, P)\n"
+ if err := L.DoString(pluginDef + string(data)); err != nil {
+ TermMessage(err)
+ continue
+ }
+
+ loadedPlugins = append(loadedPlugins, pluginName)
+ }
+ }
+
+ if _, err := os.Stat(configDir + "/init.lua"); err == nil {
+ if err := L.DoFile(configDir + "/init.lua"); err != nil {
TermMessage(err)
- continue
}
- loadedPlugins = append(loadedPlugins, pluginName)
}
}