mirror of
https://github.com/zyedidia/micro
synced 2024-09-30 13:14:00 +00:00
Add preAction plugin callbacks which can return false to cancel the action
This commit is contained in:
parent
1c077247f6
commit
92c28d81b9
|
@ -20,12 +20,12 @@ var preInstalledPlugins = []string{
|
|||
// 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 {
|
||||
|
@ -33,7 +33,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,10 +41,14 @@ 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
|
||||
|
@ -53,7 +57,7 @@ func Call(function string, args []string) error {
|
|||
// to bind keys to lua functions
|
||||
func LuaFunctionBinding(function string) func(*View) bool {
|
||||
return func(v *View) bool {
|
||||
err := Call(function, nil)
|
||||
_, err := Call(function, nil)
|
||||
if err != nil {
|
||||
TermMessage(err)
|
||||
}
|
||||
|
@ -65,7 +69,7 @@ func LuaFunctionBinding(function string) func(*View) bool {
|
|||
// 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)
|
||||
}
|
||||
|
@ -74,7 +78,7 @@ func LuaFunctionCommand(function string) func([]string) {
|
|||
|
||||
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)
|
||||
}
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -8,6 +8,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/mattn/go-runewidth"
|
||||
"github.com/yuin/gopher-lua"
|
||||
"github.com/zyedidia/tcell"
|
||||
)
|
||||
|
||||
|
@ -366,7 +367,7 @@ func (v *View) HandleEvent(event tcell.Event) {
|
|||
v.Cursor.Right()
|
||||
|
||||
for _, pl := range loadedPlugins {
|
||||
err := Call(pl+".onRune", []string{string(e.Rune())})
|
||||
_, err := Call(pl+".onRune", []string{string(e.Rune())})
|
||||
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
|
||||
TermMessage(err)
|
||||
}
|
||||
|
@ -382,12 +383,26 @@ func (v *View) HandleEvent(event tcell.Event) {
|
|||
if e.Modifiers() == key.modifiers {
|
||||
relocate = false
|
||||
for _, action := range actions {
|
||||
relocate = action(v) || relocate
|
||||
executeAction := true
|
||||
funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(action).Pointer()).Name(), ".")
|
||||
for _, pl := range loadedPlugins {
|
||||
funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(action).Pointer()).Name(), ".")
|
||||
err := Call(pl+".on"+funcName[len(funcName)-1], nil)
|
||||
ret, err := Call(pl+".pre"+funcName[len(funcName)-1], nil)
|
||||
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
|
||||
TermMessage(err)
|
||||
continue
|
||||
}
|
||||
if ret == lua.LFalse {
|
||||
executeAction = false
|
||||
}
|
||||
}
|
||||
if executeAction {
|
||||
relocate = action(v) || relocate
|
||||
for _, pl := range loadedPlugins {
|
||||
_, err := Call(pl+".on"+funcName[len(funcName)-1], nil)
|
||||
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
|
||||
TermMessage(err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue