X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=cmd%2Fmicro%2Fplugin.go;h=d77baea9ba5fdf4e3c6eaecc757bfe553323aa4b;hb=3ecdd96931ac75039105b5a4ac2fdd3b5526dbf6;hp=b3977965121337e4be1ea772a01e21432b99ee46;hpb=6665834cca6ac6858b5cd964e0c741eb1da79489;p=micro.git diff --git a/cmd/micro/plugin.go b/cmd/micro/plugin.go index b3977965..d77baea9 100644 --- a/cmd/micro/plugin.go +++ b/cmd/micro/plugin.go @@ -3,6 +3,7 @@ package main import ( "errors" "io/ioutil" + "os" "strings" "github.com/layeh/gopher-luar" @@ -11,21 +12,15 @@ import ( var loadedPlugins []string -var preInstalledPlugins = []string{ - "go", - "linter", - "autoclose", -} - // 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 ...interface{}) (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 { @@ -33,7 +28,7 @@ func Call(function string, args []string) error { } 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 { @@ -41,19 +36,23 @@ func Call(function string, args []string) error { } 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) } @@ -61,20 +60,52 @@ func LuaFunctionBinding(function string) func(*View) bool { } } +func unpack(old []string) []interface{} { + new := make([]interface{}, len(old)) + for i, v := range old { + new[i] = v + } + return new +} + // 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) + _, err := Call(function, unpack(args)...) if err != nil { TermMessage(err) } } } +// LuaFunctionComplete returns a function which can be used for autocomplete in plugins +func LuaFunctionComplete(function string) func(string) []string { + return func(input string) (result []string) { + + res, err := Call(function, input) + if err != nil { + TermMessage(err) + } + if tbl, ok := res.(*lua.LTable); !ok { + TermMessage(function, "should return a table of strings") + } else { + for i := 1; i <= tbl.Len(); i++ { + val := tbl.RawGetInt(i) + if v, ok := val.(lua.LString); !ok { + TermMessage(function, "should return a table of strings") + } else { + result = append(result, string(v)) + } + } + } + return result + } +} + func LuaFunctionJob(function string) func(string, ...string) { return func(output string, args ...string) { - err := Call(function, append([]string{output}, args...)) + _, err := Call(function, unpack(append([]string{output}, args...))...) if err != nil { TermMessage(err) } @@ -83,38 +114,38 @@ func LuaFunctionJob(function string) func(string, ...string) { // 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" { - data, _ := ioutil.ReadFile(configDir + "/plugins/" + pluginName + "/" + f.Name()) - pluginDef := "\nlocal P = {}\n" + pluginName + " = P\nsetmetatable(" + pluginName + ", {__index = _G})\nsetfenv(1, P)\n" + for _, plugin := range ListRuntimeFiles(RTPlugin) { + alreadyExists := false + pluginName := plugin.Name() + for _, pl := range loadedPlugins { + if pl == pluginName { + alreadyExists = true + break + } + } - if err := L.DoString(pluginDef + string(data)); err != nil { - TermMessage(err) - continue - } - loadedPlugins = append(loadedPlugins, pluginName) - } + if !alreadyExists { + data, err := plugin.Data() + if err != nil { + TermMessage("Error loading 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) } } - 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 - } - pluginDef := "\nlocal P = {}\n" + pluginName + " = P\nsetmetatable(" + pluginName + ", {__index = _G})\nsetfenv(1, P)\n" + if _, err := os.Stat(configDir + "/init.lua"); err == nil { + pluginDef := "\nlocal P = {}\n" + "init" + " = P\nsetmetatable(" + "init" + ", {__index = _G})\nsetfenv(1, P)\n" + data, _ := ioutil.ReadFile(configDir + "/init.lua") if err := L.DoString(pluginDef + string(data)); err != nil { TermMessage(err) - continue } - loadedPlugins = append(loadedPlugins, pluginName) + loadedPlugins = append(loadedPlugins, "init") } }