From 415ceee46b6ef0870b32de98991286f6c1fa5a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Fri, 13 Oct 2023 01:56:16 +0200 Subject: [PATCH] plugin: Add new `onBufferOptionChanged` callback This can become handy in the moment a plugin needs to react on e.g. changed file type. --- internal/buffer/settings.go | 11 ++++++++++- runtime/help/plugins.md | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/buffer/settings.go b/internal/buffer/settings.go index 838df4a5..3bb3d0c0 100644 --- a/internal/buffer/settings.go +++ b/internal/buffer/settings.go @@ -5,7 +5,9 @@ import ( "reflect" "github.com/zyedidia/micro/v2/internal/config" + ulua "github.com/zyedidia/micro/v2/internal/lua" "github.com/zyedidia/micro/v2/internal/screen" + luar "layeh.com/gopher-luar" ) func (b *Buffer) ReloadSettings(reloadFiletype bool) { @@ -46,7 +48,8 @@ func (b *Buffer) ReloadSettings(reloadFiletype bool) { } func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) { - if reflect.DeepEqual(b.Settings[option], nativeValue) { + oldValue := b.Settings[option] + if reflect.DeepEqual(oldValue, nativeValue) { return } @@ -117,6 +120,12 @@ func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) { if b.OptionCallback != nil { b.OptionCallback(option, nativeValue) } + + if err := config.RunPluginFn("onBufferOptionChanged", + luar.New(ulua.L, b), luar.New(ulua.L, option), + luar.New(ulua.L, oldValue), luar.New(ulua.L, nativeValue)); err != nil { + screen.TermMessage(err) + } } func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error { diff --git a/runtime/help/plugins.md b/runtime/help/plugins.md index fbab6469..5d4830fb 100644 --- a/runtime/help/plugins.md +++ b/runtime/help/plugins.md @@ -62,6 +62,10 @@ that micro defines: * `onBufferOpen(buf)`: runs when a buffer is opened. The input contains the buffer object. +* `onBufferOptionChanged(buf, option, old, new)`: runs when an option of the + buffer has changed. The input contains the buffer object, the option name, + the old and the new value. + * `onBufPaneOpen(bufpane)`: runs when a bufpane is opened. The input contains the bufpane object.