]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/bindings.go
Merge pull request #316 from elopio/snapcraft
[micro.git] / cmd / micro / bindings.go
index 85d5357ba1594159315644d163fed3ef4dca344d..da6328bebb68279811b779e37c42c2b2982268a6 100644 (file)
@@ -1,18 +1,18 @@
 package main
 
 import (
-       "encoding/json"
        "io/ioutil"
        "os"
        "strings"
 
+       "github.com/yosuke-furukawa/json5/encoding/json5"
        "github.com/zyedidia/tcell"
 )
 
-var bindings map[Key][]func(*View) bool
+var bindings map[Key][]func(*View, bool) bool
 var helpBinding string
 
-var bindingActions = map[string]func(*View) bool{
+var bindingActions = map[string]func(*View, bool) bool{
        "CursorUp":            (*View).CursorUp,
        "CursorDown":          (*View).CursorDown,
        "CursorPageUp":        (*View).CursorPageUp,
@@ -35,7 +35,7 @@ var bindingActions = map[string]func(*View) bool{
        "DeleteWordLeft":      (*View).DeleteWordLeft,
        "SelectToStartOfLine": (*View).SelectToStartOfLine,
        "SelectToEndOfLine":   (*View).SelectToEndOfLine,
-       "InsertEnter":         (*View).InsertEnter,
+       "InsertNewline":       (*View).InsertNewline,
        "InsertSpace":         (*View).InsertSpace,
        "Backspace":           (*View).Backspace,
        "Delete":              (*View).Delete,
@@ -44,6 +44,7 @@ var bindingActions = map[string]func(*View) bool{
        "Find":                (*View).Find,
        "FindNext":            (*View).FindNext,
        "FindPrevious":        (*View).FindPrevious,
+       "Center":              (*View).Center,
        "Undo":                (*View).Undo,
        "Redo":                (*View).Redo,
        "Copy":                (*View).Copy,
@@ -51,7 +52,10 @@ var bindingActions = map[string]func(*View) bool{
        "CutLine":             (*View).CutLine,
        "DuplicateLine":       (*View).DuplicateLine,
        "DeleteLine":          (*View).DeleteLine,
+       "IndentSelection":     (*View).IndentSelection,
+       "OutdentSelection":    (*View).OutdentSelection,
        "Paste":               (*View).Paste,
+       "PastePrimary":        (*View).PastePrimary,
        "SelectAll":           (*View).SelectAll,
        "OpenFile":            (*View).OpenFile,
        "Start":               (*View).Start,
@@ -69,11 +73,17 @@ var bindingActions = map[string]func(*View) bool{
        "ShellMode":           (*View).ShellMode,
        "CommandMode":         (*View).CommandMode,
        "Quit":                (*View).Quit,
+       "QuitAll":             (*View).QuitAll,
        "AddTab":              (*View).AddTab,
        "PreviousTab":         (*View).PreviousTab,
        "NextTab":             (*View).NextTab,
        "NextSplit":           (*View).NextSplit,
        "PreviousSplit":       (*View).PreviousSplit,
+       "ToggleMacro":         (*View).ToggleMacro,
+       "PlayMacro":           (*View).PlayMacro,
+
+       // This was changed to InsertNewline but I don't want to break backwards compatibility
+       "InsertEnter": (*View).InsertNewline,
 }
 
 var bindingKeys = map[string]tcell.Key{
@@ -216,7 +226,7 @@ type Key struct {
 
 // InitBindings initializes the keybindings for micro
 func InitBindings() {
-       bindings = make(map[Key][]func(*View) bool)
+       bindings = make(map[Key][]func(*View, bool) bool)
 
        var parsed map[string]string
        defaults := DefaultBindings()
@@ -229,7 +239,7 @@ func InitBindings() {
                        return
                }
 
-               err = json.Unmarshal(input, &parsed)
+               err = json5.Unmarshal(input, &parsed)
                if err != nil {
                        TermMessage("Error reading bindings.json:", err.Error())
                }
@@ -309,7 +319,7 @@ modSearch:
 }
 
 // findAction will find 'action' using string 'v'
-func findAction(v string) (action func(*View) bool) {
+func findAction(v string) (action func(*View, bool) bool) {
        action, ok := bindingActions[v]
        if !ok {
                // If the user seems to be binding a function that doesn't exist
@@ -330,7 +340,7 @@ func BindKey(k, v string) {
        }
 
        actionNames := strings.Split(v, ",")
-       actions := make([]func(*View) bool, 0, len(actionNames))
+       actions := make([]func(*View, bool) bool, 0, len(actionNames))
        for _, actionName := range actionNames {
                actions = append(actions, findAction(actionName))
        }
@@ -361,13 +371,14 @@ func DefaultBindings() map[string]string {
                "CtrlDown":       "CursorEnd",
                "CtrlShiftUp":    "SelectToStart",
                "CtrlShiftDown":  "SelectToEnd",
-               "Enter":          "InsertEnter",
+               "Enter":          "InsertNewline",
                "Space":          "InsertSpace",
                "Backspace":      "Backspace",
                "Backspace2":     "Backspace",
                "Alt-Backspace":  "DeleteWordLeft",
                "Alt-Backspace2": "DeleteWordLeft",
-               "Tab":            "InsertTab",
+               "Tab":            "IndentSelection,InsertTab",
+               "Backtab":        "OutdentSelection",
                "CtrlO":          "OpenFile",
                "CtrlS":          "Save",
                "CtrlF":          "Find",
@@ -384,8 +395,10 @@ func DefaultBindings() map[string]string {
                "CtrlT":          "AddTab",
                "CtrlRightSq":    "PreviousTab",
                "CtrlBackslash":  "NextTab",
-               "Home":           "Start",
-               "End":            "End",
+               "Home":           "StartOfLine",
+               "End":            "EndOfLine",
+               "CtrlHome":       "CursorStart",
+               "CtrlEnd":        "CursorEnd",
                "PageUp":         "CursorPageUp",
                "PageDown":       "CursorPageDown",
                "CtrlG":          "ToggleHelp",
@@ -397,6 +410,8 @@ func DefaultBindings() map[string]string {
                "CtrlQ":          "Quit",
                "CtrlE":          "CommandMode",
                "CtrlW":          "NextSplit",
+               "CtrlU":          "ToggleMacro",
+               "CtrlJ":          "PlayMacro",
 
                // Emacs-style keybindings
                "Alt-f": "WordRight",