diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index dbac7dab..76214780 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -9,7 +9,6 @@ import ( "time" "github.com/mitchellh/go-homedir" - "github.com/yuin/gopher-lua" "github.com/zyedidia/clipboard" "github.com/zyedidia/tcell" ) @@ -614,16 +613,6 @@ func (v *View) Save() bool { } else { messenger.Message("Saved " + v.Buf.Path) } - for _, pl := range loadedPlugins { - if err := L.CallByParam(lua.P{ - Fn: L.GetGlobal(pl + "_onSave"), - NRet: 0, - Protect: true, - }); err != nil { - // The function isn't defined by this plugin - messenger.Error(err) - } - } return true } diff --git a/cmd/micro/plugin.go b/cmd/micro/plugin.go index 667dac2a..242a49f7 100644 --- a/cmd/micro/plugin.go +++ b/cmd/micro/plugin.go @@ -1,6 +1,7 @@ package main import ( + "github.com/yuin/gopher-lua" "io/ioutil" ) @@ -10,6 +11,22 @@ var preInstalledPlugins = []string{ "go", } +// 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) error { + luaFunc := L.GetGlobal(function) + if luaFunc.String() == "nil" { + return nil + } + err := L.CallByParam(lua.P{ + Fn: luaFunc, + NRet: 0, + Protect: true, + }) + return err +} + // LoadPlugins loads the pre-installed plugins and the plugins located in ~/.config/micro/plugins func LoadPlugins() { files, _ := ioutil.ReadDir(configDir + "/plugins") diff --git a/cmd/micro/view.go b/cmd/micro/view.go index bf1468fd..489d7bb1 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -2,6 +2,8 @@ package main import ( "io/ioutil" + "reflect" + "runtime" "strconv" "strings" "time" @@ -266,6 +268,13 @@ func (v *View) HandleEvent(event tcell.Event) { for key, action := range bindings { if e.Key() == key { relocate = action(v) + for _, pl := range loadedPlugins { + funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(action).Pointer()).Name(), ".") + err := Call(pl + "_on" + funcName[len(funcName)-1]) + if err != nil { + TermMessage(err) + } + } } } }