2022-11-14 15:42:04 +05:30

59 lines
1.2 KiB
Go

// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
package readline
import (
"errors"
"fmt"
"kitty/tools/tui/loop"
)
var _ = fmt.Print
var default_shortcuts = map[string]Action{
"backspace": ActionBackspace,
"ctrl+h": ActionBackspace,
"delete": ActionDelete,
"ctrl+d": ActionDelete,
"home": ActionMoveToStartOfLine,
"ctrl+a": ActionMoveToStartOfLine,
"end": ActionMoveToEndOfLine,
"ctrl+e": ActionMoveToEndOfLine,
"ctrl+home": ActionMoveToStartOfDocument,
"ctrl+end": ActionMoveToEndOfDocument,
"left": ActionCursorLeft,
"ctrl+b": ActionCursorLeft,
"right": ActionCursorRight,
"ctrl+f": ActionCursorRight,
}
func action_for_key_event(event *loop.KeyEvent, shortcuts map[string]Action) Action {
for sc, ac := range shortcuts {
if event.MatchesPressOrRepeat(sc) {
return ac
}
}
return ActionNil
}
var ErrCouldNotPerformAction = errors.New("Could not perform the specified action")
func (self *Readline) handle_key_event(event *loop.KeyEvent) error {
if event.Text != "" {
return nil
}
ac := action_for_key_event(event, default_shortcuts)
if ac != ActionNil {
event.Handled = true
if !self.perform_action(ac, 1) {
return ErrCouldNotPerformAction
}
}
return nil
}