From 54ba3cdb4f92cbc066675fe9d75d7ebb65ca1d98 Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Sat, 21 Jun 2025 03:04:37 +0200 Subject: [PATCH 1/3] Make PluginCB() a variadic function So that we can pass extra arguments to bufpane callbacks easily, by passing them directly to PluginCB(). --- internal/action/bufpane.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/action/bufpane.go b/internal/action/bufpane.go index f748b085..9cb54861 100644 --- a/internal/action/bufpane.go +++ b/internal/action/bufpane.go @@ -321,9 +321,16 @@ func (h *BufPane) ResizePane(size int) { } // PluginCB calls all plugin callbacks with a certain name and displays an -// error if there is one and returns the aggregate boolean response -func (h *BufPane) PluginCB(cb string) bool { - b, err := config.RunPluginFnBool(h.Buf.Settings, cb, luar.New(ulua.L, h)) +// error if there is one and returns the aggregate boolean response. +// The bufpane is passed as the first argument to the callbacks, +// optional args are passed as the next arguments. +func (h *BufPane) PluginCB(cb string, args ...interface{}) bool { + largs := []lua.LValue{luar.New(ulua.L, h)} + for _, a := range args { + largs = append(largs, luar.New(ulua.L, a)) + } + + b, err := config.RunPluginFnBool(h.Buf.Settings, cb, largs...) if err != nil { screen.TermMessage(err) } From 7861b00cd1f63b32bf897e88b26cefb21cd7da26 Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Sat, 21 Jun 2025 02:31:23 +0200 Subject: [PATCH 2/3] Pass mouse info to {on,pre}MouseXXX callbacks Pass *tcell.EventMouse to action callbacks for "mouse actions", i.e. to onMousePress, preMouseDrag and so on, similarly to how we pass it to lua functions bound to mouse events. --- internal/action/bufpane.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/action/bufpane.go b/internal/action/bufpane.go index 9cb54861..8c99a3a4 100644 --- a/internal/action/bufpane.go +++ b/internal/action/bufpane.go @@ -566,7 +566,7 @@ func (h *BufPane) execAction(action BufAction, name string, te *tcell.EventMouse h.Buf.HasSuggestions = false } - if !h.PluginCB("pre" + name) { + if !h.PluginCB("pre"+name, te) { return false } @@ -577,7 +577,7 @@ func (h *BufPane) execAction(action BufAction, name string, te *tcell.EventMouse case BufMouseAction: success = a(h, te) } - success = success && h.PluginCB("on"+name) + success = success && h.PluginCB("on"+name, te) if _, ok := MultiActions[name]; ok { if recordingMacro { From baa632e6ccba02828360329919f50f54767180ec Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Sat, 21 Jun 2025 03:09:55 +0200 Subject: [PATCH 3/3] Remove PluginCBRune() Now that we can pass extra args directly to PluginCB(), we can remove PluginCBRune() for simplicity. --- internal/action/bufpane.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/internal/action/bufpane.go b/internal/action/bufpane.go index 8c99a3a4..b4030a95 100644 --- a/internal/action/bufpane.go +++ b/internal/action/bufpane.go @@ -337,15 +337,6 @@ func (h *BufPane) PluginCB(cb string, args ...interface{}) bool { return b } -// PluginCBRune is the same as PluginCB but also passes a rune to the plugins -func (h *BufPane) PluginCBRune(cb string, r rune) bool { - b, err := config.RunPluginFnBool(h.Buf.Settings, cb, luar.New(ulua.L, h), luar.New(ulua.L, string(r))) - if err != nil { - screen.TermMessage(err) - } - return b -} - func (h *BufPane) resetMouse() { for me := range h.mousePressed { delete(h.mousePressed, me) @@ -633,7 +624,7 @@ func (h *BufPane) DoRuneInsert(r rune) { // Insert a character h.Buf.SetCurCursor(c.Num) h.Cursor = c - if !h.PluginCBRune("preRune", r) { + if !h.PluginCB("preRune", string(r)) { continue } if c.HasSelection() { @@ -652,7 +643,7 @@ func (h *BufPane) DoRuneInsert(r rune) { curmacro = append(curmacro, r) } h.Relocate() - h.PluginCBRune("onRune", r) + h.PluginCB("onRune", string(r)) } }