From bf33ab532c57e6fe3cfacae09c2a0e62a2bffb31 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sun, 3 Dec 2017 23:15:32 -0500 Subject: [PATCH] Store string keys for bindings --- cmd/micro/bindings.go | 40 ++++++++++++++++++++++++++++++++++++++++ cmd/micro/command.go | 19 +++---------------- cmd/micro/messenger.go | 2 +- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index f899ee65..c08ef5da 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io/ioutil" "os" "strings" @@ -10,6 +11,7 @@ import ( "github.com/zyedidia/tcell" ) +var bindingsStr map[string]string var bindings map[Key][]func(*View, bool) bool var mouseBindings map[Key][]func(*View, bool, *tcell.EventMouse) bool var helpBinding string @@ -267,6 +269,7 @@ type Key struct { // InitBindings initializes the keybindings for micro func InitBindings() { bindings = make(map[Key][]func(*View, bool) bool) + bindingsStr = make(map[string]string) mouseBindings = make(map[Key][]func(*View, bool, *tcell.EventMouse) bool) var parsed map[string]string @@ -402,6 +405,41 @@ func findMouseAction(v string) func(*View, bool, *tcell.EventMouse) bool { return action } +func TryBindKey(k, v string) { + filename := configDir + "/bindings.json" + if _, e := os.Stat(filename); e == nil { + input, err := ioutil.ReadFile(filename) + if err != nil { + TermMessage("Error reading bindings.json file: " + err.Error()) + return + } + + conflict := -1 + lines := strings.Split(string(input), "\n") + for i, l := range lines { + parts := strings.Split(l, ":") + if len(parts) >= 2 { + if strings.Contains(parts[0], k) { + conflict = i + TermMessage("Warning: Keybinding conflict:", k, " has been overwritten") + } + } + } + + binding := fmt.Sprintf(" \"%s\": \"%s\",", k, v) + if conflict == -1 { + lines = append([]string{lines[0], binding}, lines[conflict:]...) + } else { + lines = append(append(lines[:conflict], binding), lines[conflict+1:]...) + } + txt := strings.Join(lines, "\n") + err = ioutil.WriteFile(filename, []byte(txt), 0644) + if err != nil { + TermMessage("Error") + } + } +} + // BindKey takes a key and an action and binds the two together func BindKey(k, v string) { key, ok := findKey(k) @@ -426,6 +464,7 @@ func BindKey(k, v string) { if actionNames[0] == "UnbindKey" { delete(bindings, key) delete(mouseBindings, key) + delete(bindingsStr, k) if len(actionNames) == 1 { return } @@ -445,6 +484,7 @@ func BindKey(k, v string) { // Can't have a binding be both mouse and normal delete(mouseBindings, key) bindings[key] = actions + bindingsStr[k] = v } else if len(mouseActions) > 0 { // Can't have a binding be both mouse and normal delete(bindings, key) diff --git a/cmd/micro/command.go b/cmd/micro/command.go index 6513ef96..98e0bb67 100644 --- a/cmd/micro/command.go +++ b/cmd/micro/command.go @@ -520,23 +520,10 @@ func ShowKey(args []string) { 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") + if action, ok := bindingsStr[args[0]]; ok { + messenger.Message(action) } else { - actions := bindings[key] - msg := "" - for i, a := range actions { - msg += FuncName(a) - if i != len(actions)-1 { - msg += ", " - } - } - messenger.Message(msg) + messenger.Message(args[0], " has no binding") } } diff --git a/cmd/micro/messenger.go b/cmd/micro/messenger.go index 808152a8..33021f97 100644 --- a/cmd/micro/messenger.go +++ b/cmd/micro/messenger.go @@ -606,7 +606,7 @@ func (m *Messenger) SaveHistory() { // Don't save history past 100 for k, v := range m.history { if len(v) > 100 { - m.history[k] = v[0:100] + m.history[k] = v[len(m.history[k])-100:] } }