]> git.lizzy.rs Git - micro.git/commitdiff
Store string keys for bindings
authorZachary Yedidia <zyedidia@gmail.com>
Mon, 4 Dec 2017 04:15:32 +0000 (23:15 -0500)
committerZachary Yedidia <zyedidia@gmail.com>
Mon, 4 Dec 2017 04:15:32 +0000 (23:15 -0500)
cmd/micro/bindings.go
cmd/micro/command.go
cmd/micro/messenger.go

index f899ee65f14b705fda58463883cc44c775e02a68..c08ef5da5ff8952a9259cce8fc7c13753fc6f1ad 100644 (file)
@@ -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)
index 6513ef96f62c58f0cebaf440f84b05c825ba4c2d..98e0bb673b238c3296e0776bfc6af82ab88841cf 100644 (file)
@@ -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")
        }
 }
 
index 808152a8675f0b5f6acc18c0785882b9212cdcfc..33021f97dc7bd357bc998eeb748f0e55a74f5761 100644 (file)
@@ -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:]
                        }
                }