package main
import (
+ "fmt"
"io/ioutil"
"os"
"strings"
"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
// 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
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)
if actionNames[0] == "UnbindKey" {
delete(bindings, key)
delete(mouseBindings, key)
+ delete(bindingsStr, k)
if len(actionNames) == 1 {
return
}
// 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)
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")
}
}