Skip to content

Commit b70f0eb

Browse files
authored
Add onAnyEvent callback (zyedidia#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
1 parent 1f51d0b commit b70f0eb

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

cmd/micro/micro.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,6 @@ func DoEvent() {
450450
os.Exit(0)
451451
}
452452

453-
if event == nil {
454-
return
455-
}
456-
457453
if e, ok := event.(*tcell.EventError); ok {
458454
log.Println("tcell event error: ", e.Error())
459455

@@ -473,13 +469,20 @@ func DoEvent() {
473469
return
474470
}
475471

476-
_, resize := event.(*tcell.EventResize)
477-
if resize {
478-
action.InfoBar.HandleEvent(event)
479-
action.Tabs.HandleEvent(event)
480-
} else if action.InfoBar.HasPrompt {
481-
action.InfoBar.HandleEvent(event)
482-
} else {
483-
action.Tabs.HandleEvent(event)
472+
if event != nil {
473+
_, resize := event.(*tcell.EventResize)
474+
if resize {
475+
action.InfoBar.HandleEvent(event)
476+
action.Tabs.HandleEvent(event)
477+
} else if action.InfoBar.HasPrompt {
478+
action.InfoBar.HandleEvent(event)
479+
} else {
480+
action.Tabs.HandleEvent(event)
481+
}
482+
}
483+
484+
err := config.RunPluginFn("onAnyEvent")
485+
if err != nil {
486+
screen.TermMessage(err)
484487
}
485488
}

runtime/help/plugins.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ which micro defines:
7272

7373
* `preRune(bufpane, rune)`: runs before the composed rune will be inserted
7474

75+
* `onAnyEvent()`: runs when literally anything happens. It is useful for
76+
detecting various changes of micro's state that cannot be detected
77+
using other callbacks.
78+
7579
For example a function which is run every time the user saves the buffer
7680
would be:
7781

0 commit comments

Comments
 (0)