diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-26 15:23:28 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-27 09:49:30 +1000 |
commit | 67bbeb195b52121976840de0cd1a20cf1462b5e0 (patch) | |
tree | bd9cf6649b9040813bf82510692d7503716a28a7 /pkg/gui/keybindings.go | |
parent | 92183de29edab53ccdfb99ddcfaa6fc2c359d925 (diff) |
support custom keybindings
Diffstat (limited to 'pkg/gui/keybindings.go')
-rw-r--r-- | pkg/gui/keybindings.go | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 31738a419..8b6e80d20 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -181,6 +181,10 @@ func GetKeyDisplay(key interface{}) string { func (gui *Gui) getKey(name string) interface{} { key := gui.Config.GetUserConfig().GetString("keybinding." + name) + if key == "" { + // if we don't have the keybinding in our local config we'll assume it's just a plain letter from a custom command + key = name + } runeCount := utf8.RuneCountInString(key) if runeCount > 1 { binding := keymap[strings.ToLower(key)] @@ -1559,8 +1563,45 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { return bindings } +func (gui *Gui) GetCustomCommandKeybindings() []*Binding { + bindings := []*Binding{} + + ms := gui.Config.GetUserConfig().GetStringMap("customCommands") + for contextKey := range ms { + var viewName string + if contextKey == "global" { + viewName = "" + } else { + context := gui.contextForContextKey(contextKey) + if context == nil { + log.Fatalf("Error when setting custom command keybindings: unknown context: %s", contextKey) + } + // here we assume that a given context will always belong to the same view. + // Currently this is a safe bet but it's by no means guaranteed in the long term + // and we might need to make some changes in the future to support it. + viewName = context.GetViewName() + } + + keyMap := gui.Config.GetUserConfig().GetStringMapString(fmt.Sprintf("customCommands.%s", contextKey)) + for key, command := range keyMap { + bindings = append(bindings, &Binding{ + ViewName: viewName, + Contexts: []string{contextKey}, + Key: gui.getKey(key), + Modifier: gocui.ModNone, + Handler: gui.wrappedHandler(gui.handleCustomCommandKeybinding(command)), + Description: command, + }) + } + } + + return bindings +} + func (gui *Gui) keybindings() error { - bindings := gui.GetInitialKeybindings() + bindings := gui.GetCustomCommandKeybindings() + + bindings = append(bindings, gui.GetInitialKeybindings()...) for _, binding := range bindings { if err := gui.g.SetKeybinding(binding.ViewName, binding.Contexts, binding.Key, binding.Modifier, binding.Handler); err != nil { |