]> git.lizzy.rs Git - micro.git/commitdiff
Add hooks for every action that's bindable
authorZachary Yedidia <zyedidia@gmail.com>
Wed, 27 Apr 2016 18:37:58 +0000 (14:37 -0400)
committerZachary Yedidia <zyedidia@gmail.com>
Thu, 5 May 2016 16:53:26 +0000 (12:53 -0400)
cmd/micro/bindings.go
cmd/micro/plugin.go
cmd/micro/view.go

index dbac7dabe0d79932afed6d4bd8f493026557b28a..762147807a20bc8be5993d0bcda8f24fd19f3f8c 100644 (file)
@@ -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
 }
 
index 667dac2ae5f71e7cfe8fcc767d3a977a8052f2f0..242a49f7ca2d68942a60a83da9fd8f659342e931 100644 (file)
@@ -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")
index bf1468fd86b0385c3d5ae17ddfa565406bc853f5..489d7bb173ef778bb334a4dddfc61b8d10fd6510 100644 (file)
@@ -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)
+                                               }
+                                       }
                                }
                        }
                }