From b70f0eb113b3b2e831e87be4d335c5b86f0330d3 Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Tue, 14 May 2024 18:01:15 +0200 Subject: [PATCH] Add onAnyEvent callback (#3244) Implement a radical approach to improving abilities of plugins to detect and handle various changes of micro's state: add onAnyEvent callback which is called, literally, after any event. A plugin can use this callback to compare a state after the previous event and after the current event, and thus is able to catch various events that cannot be detected using other callbacks. Some examples of such events: - change of current working directory - switching cursor focus between a bufpane and the command bar - change of message text in the status bar --- cmd/micro/micro.go | 27 +++++++++++++++------------ runtime/help/plugins.md | 4 ++++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index f45b7c12..ac36ec19 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -450,10 +450,6 @@ func DoEvent() { os.Exit(0) } - if event == nil { - return - } - if e, ok := event.(*tcell.EventError); ok { log.Println("tcell event error: ", e.Error()) @@ -473,13 +469,20 @@ func DoEvent() { return } - _, resize := event.(*tcell.EventResize) - if resize { - action.InfoBar.HandleEvent(event) - action.Tabs.HandleEvent(event) - } else if action.InfoBar.HasPrompt { - action.InfoBar.HandleEvent(event) - } else { - action.Tabs.HandleEvent(event) + if event != nil { + _, resize := event.(*tcell.EventResize) + if resize { + action.InfoBar.HandleEvent(event) + action.Tabs.HandleEvent(event) + } else if action.InfoBar.HasPrompt { + action.InfoBar.HandleEvent(event) + } else { + action.Tabs.HandleEvent(event) + } + } + + err := config.RunPluginFn("onAnyEvent") + if err != nil { + screen.TermMessage(err) } } diff --git a/runtime/help/plugins.md b/runtime/help/plugins.md index 948e7b72..9c4d4d42 100644 --- a/runtime/help/plugins.md +++ b/runtime/help/plugins.md @@ -72,6 +72,10 @@ which micro defines: * `preRune(bufpane, rune)`: runs before the composed rune will be inserted +* `onAnyEvent()`: runs when literally anything happens. It is useful for + detecting various changes of micro's state that cannot be detected + using other callbacks. + For example a function which is run every time the user saves the buffer would be: