Execute keybind handlers before edition

Fix issue #54
This commit is contained in:
Roi Martin 2016-10-11 08:27:26 +02:00
parent 8d16527c1d
commit 76554e4f84
1 changed files with 16 additions and 6 deletions

22
gui.go
View File

@ -604,14 +604,14 @@ func horizontalRune(ch rune) bool {
// a key-press or mouse event satisfies a configured keybinding. Furthermore,
// currentView's internal buffer is modified if currentView.Editable is true.
func (g *Gui) onKey(ev *termbox.Event) error {
var curView *View
switch ev.Type {
case termbox.EventKey:
if err := g.execKeybindings(g.currentView, ev); err != nil {
return err
}
if g.currentView != nil && g.currentView.Editable && g.Editor != nil {
g.Editor.Edit(g.currentView, Key(ev.Key), ev.Ch, Modifier(ev.Mod))
}
curView = g.currentView
case termbox.EventMouse:
mx, my := ev.MouseX, ev.MouseY
v, err := g.ViewByPosition(mx, my)
@ -621,15 +621,25 @@ func (g *Gui) onKey(ev *termbox.Event) error {
if err := v.SetCursor(mx-v.x0-1, my-v.y0-1); err != nil {
return err
}
curView = v
if err := g.execKeybindings(g.currentView, ev); err != nil {
return err
}
default:
return errors.New("unknown event type")
}
return nil
}
// execKeybindings executes the keybinding handlers that match the passed view
// and event.
func (g *Gui) execKeybindings(v *View, ev *termbox.Event) error {
for _, kb := range g.keybindings {
if kb.h == nil {
continue
}
if kb.matchKeypress(Key(ev.Key), ev.Ch, Modifier(ev.Mod)) && kb.matchView(curView) {
if err := kb.h(g, curView); err != nil {
if kb.matchKeypress(Key(ev.Key), ev.Ch, Modifier(ev.Mod)) && kb.matchView(v) {
if err := kb.h(g, v); err != nil {
return err
}
}