From 2ae9f88eaa49951fa1b0ceb59335445654f4c4d8 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sun, 3 Dec 2017 13:05:46 -0500 Subject: [PATCH] Add showkey command --- cmd/micro/bindings.go | 2 ++ cmd/micro/command.go | 29 +++++++++++++++++++++++++++++ cmd/micro/plugin.go | 7 +++++++ runtime/help/commands.md | 5 +++++ 4 files changed, 43 insertions(+) diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index fc0c2d3a..f899ee65 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -4,6 +4,7 @@ import ( "io/ioutil" "os" "strings" + "unicode" "github.com/flynn/json5" "github.com/zyedidia/tcell" @@ -335,6 +336,7 @@ modSearch: // first. if modifiers&tcell.ModCtrl != 0 { // see if the key is in bindingKeys with the Ctrl prefix. + k = string(unicode.ToUpper(rune(k[0]))) + k[1:] if code, ok := bindingKeys["Ctrl"+k]; ok { // It is, we're done. return Key{ diff --git a/cmd/micro/command.go b/cmd/micro/command.go index e429986d..6513ef96 100644 --- a/cmd/micro/command.go +++ b/cmd/micro/command.go @@ -37,6 +37,7 @@ func init() { "Set": Set, "SetLocal": SetLocal, "Show": Show, + "ShowKey": ShowKey, "Run": Run, "Bind": Bind, "Quit": Quit, @@ -94,6 +95,7 @@ func DefaultCommands() map[string]StrCommand { "set": {"Set", []Completion{OptionCompletion, OptionValueCompletion}}, "setlocal": {"SetLocal", []Completion{OptionCompletion, OptionValueCompletion}}, "show": {"Show", []Completion{OptionCompletion, NoCompletion}}, + "showkey": {"ShowKey", []Completion{NoCompletion}}, "bind": {"Bind", []Completion{NoCompletion}}, "run": {"Run", []Completion{NoCompletion}}, "quit": {"Quit", []Completion{NoCompletion}}, @@ -511,6 +513,33 @@ func Show(args []string) { messenger.Message(option) } +// ShowKey displays the action that a key is bound to +func ShowKey(args []string) { + if len(args) < 1 { + messenger.Error("Please provide a key to show") + return + } + + key, ok := findKey(args[0]) + if !ok { + messenger.Error(args[0], " is not a valid key") + return + } + if _, ok := bindings[key]; !ok { + messenger.Message(args[0], " has no binding") + } else { + actions := bindings[key] + msg := "" + for i, a := range actions { + msg += FuncName(a) + if i != len(actions)-1 { + msg += ", " + } + } + messenger.Message(msg) + } +} + // Bind creates a new keybinding func Bind(args []string) { if len(args) < 2 { diff --git a/cmd/micro/plugin.go b/cmd/micro/plugin.go index ab9a9af5..db5490c9 100644 --- a/cmd/micro/plugin.go +++ b/cmd/micro/plugin.go @@ -61,6 +61,10 @@ func LuaFunctionBinding(function string) func(*View, bool) bool { } } +// LuaFunctionMouseBinding is a function generator which takes the name of a lua function +// and creates a function that will call that lua function +// Specifically it creates a function that can be called as a mouse binding because this is used +// to bind mouse actions to lua functions func LuaFunctionMouseBinding(function string) func(*View, bool, *tcell.EventMouse) bool { return func(v *View, _ bool, e *tcell.EventMouse) bool { _, err := Call(function, e) @@ -114,6 +118,9 @@ func LuaFunctionComplete(function string) func(string) []string { } } +// LuaFunctionJob returns a function that will call the given lua function +// structured as a job call i.e. the job output and arguments are provided +// to the lua function func LuaFunctionJob(function string) func(string, ...string) { return func(output string, args ...string) { _, err := Call(function, unpack(append([]string{output}, args...))...) diff --git a/runtime/help/commands.md b/runtime/help/commands.md index b0a7d418..a1e64cbc 100644 --- a/runtime/help/commands.md +++ b/runtime/help/commands.md @@ -85,6 +85,11 @@ Here are the possible commands that you can use. the terminal and helps you see which bindings aren't possible and why. This is most useful for debugging keybindings. +* `showkey`: Show the action(s) bound to a given key. For example + running `> showkey CtrlC` will display `main.(*View).Copy`. Unfortuately + showkey does not work well for keys bound to plugin actions. For those + it just shows "LuaFunctionBinding." + --- The following commands are provided by the default plugins: -- 2.44.0