10 "github.com/flynn/json5"
11 "github.com/zyedidia/tcell"
14 var bindingsStr map[string]string
15 var bindings map[Key][]func(*View, bool) bool
16 var mouseBindings map[Key][]func(*View, bool, *tcell.EventMouse) bool
17 var helpBinding string
18 var kmenuBinding string
20 var mouseBindingActions = map[string]func(*View, bool, *tcell.EventMouse) bool{
21 "MousePress": (*View).MousePress,
22 "MouseMultiCursor": (*View).MouseMultiCursor,
25 var bindingActions = map[string]func(*View, bool) bool{
26 "CursorUp": (*View).CursorUp,
27 "CursorDown": (*View).CursorDown,
28 "CursorPageUp": (*View).CursorPageUp,
29 "CursorPageDown": (*View).CursorPageDown,
30 "CursorLeft": (*View).CursorLeft,
31 "CursorRight": (*View).CursorRight,
32 "CursorStart": (*View).CursorStart,
33 "CursorEnd": (*View).CursorEnd,
34 "SelectToStart": (*View).SelectToStart,
35 "SelectToEnd": (*View).SelectToEnd,
36 "SelectUp": (*View).SelectUp,
37 "SelectDown": (*View).SelectDown,
38 "SelectLeft": (*View).SelectLeft,
39 "SelectRight": (*View).SelectRight,
40 "WordRight": (*View).WordRight,
41 "WordLeft": (*View).WordLeft,
42 "SelectWordRight": (*View).SelectWordRight,
43 "SelectWordLeft": (*View).SelectWordLeft,
44 "DeleteWordRight": (*View).DeleteWordRight,
45 "DeleteWordLeft": (*View).DeleteWordLeft,
46 "SelectLine": (*View).SelectLine,
47 "SelectToStartOfLine": (*View).SelectToStartOfLine,
48 "SelectToEndOfLine": (*View).SelectToEndOfLine,
49 "ParagraphPrevious": (*View).ParagraphPrevious,
50 "ParagraphNext": (*View).ParagraphNext,
51 "InsertNewline": (*View).InsertNewline,
52 "InsertSpace": (*View).InsertSpace,
53 "Backspace": (*View).Backspace,
54 "Delete": (*View).Delete,
55 "InsertTab": (*View).InsertTab,
57 "SaveAll": (*View).SaveAll,
58 "SaveAs": (*View).SaveAs,
60 "FindNext": (*View).FindNext,
61 "FindPrevious": (*View).FindPrevious,
62 "Center": (*View).Center,
67 "CutLine": (*View).CutLine,
68 "DuplicateLine": (*View).DuplicateLine,
69 "DeleteLine": (*View).DeleteLine,
70 "MoveLinesUp": (*View).MoveLinesUp,
71 "MoveLinesDown": (*View).MoveLinesDown,
72 "IndentSelection": (*View).IndentSelection,
73 "OutdentSelection": (*View).OutdentSelection,
74 "OutdentLine": (*View).OutdentLine,
75 "Paste": (*View).Paste,
76 "PastePrimary": (*View).PastePrimary,
77 "SelectAll": (*View).SelectAll,
78 "OpenFile": (*View).OpenFile,
79 "Start": (*View).Start,
81 "PageUp": (*View).PageUp,
82 "PageDown": (*View).PageDown,
83 "HalfPageUp": (*View).HalfPageUp,
84 "HalfPageDown": (*View).HalfPageDown,
85 "StartOfLine": (*View).StartOfLine,
86 "EndOfLine": (*View).EndOfLine,
87 "ToggleHelp": (*View).ToggleHelp,
88 "ToggleKeyMenu": (*View).ToggleKeyMenu,
89 "ToggleRuler": (*View).ToggleRuler,
90 "JumpLine": (*View).JumpLine,
91 "ClearStatus": (*View).ClearStatus,
92 "ShellMode": (*View).ShellMode,
93 "CommandMode": (*View).CommandMode,
94 "ToggleOverwriteMode": (*View).ToggleOverwriteMode,
95 "Escape": (*View).Escape,
97 "QuitAll": (*View).QuitAll,
98 "AddTab": (*View).AddTab,
99 "PreviousTab": (*View).PreviousTab,
100 "NextTab": (*View).NextTab,
101 "NextSplit": (*View).NextSplit,
102 "PreviousSplit": (*View).PreviousSplit,
103 "Unsplit": (*View).Unsplit,
104 "VSplit": (*View).VSplitBinding,
105 "HSplit": (*View).HSplitBinding,
106 "ToggleMacro": (*View).ToggleMacro,
107 "PlayMacro": (*View).PlayMacro,
108 "Suspend": (*View).Suspend,
109 "ScrollUp": (*View).ScrollUpAction,
110 "ScrollDown": (*View).ScrollDownAction,
111 "SpawnMultiCursor": (*View).SpawnMultiCursor,
112 "SpawnMultiCursorSelect": (*View).SpawnMultiCursorSelect,
113 "RemoveMultiCursor": (*View).RemoveMultiCursor,
114 "RemoveAllMultiCursors": (*View).RemoveAllMultiCursors,
115 "SkipMultiCursor": (*View).SkipMultiCursor,
116 "JumpToMatchingBrace": (*View).JumpToMatchingBrace,
118 // This was changed to InsertNewline but I don't want to break backwards compatibility
119 "InsertEnter": (*View).InsertNewline,
122 var bindingMouse = map[string]tcell.ButtonMask{
123 "MouseLeft": tcell.Button1,
124 "MouseMiddle": tcell.Button2,
125 "MouseRight": tcell.Button3,
126 "MouseWheelUp": tcell.WheelUp,
127 "MouseWheelDown": tcell.WheelDown,
128 "MouseWheelLeft": tcell.WheelLeft,
129 "MouseWheelRight": tcell.WheelRight,
132 var bindingKeys = map[string]tcell.Key{
134 "Down": tcell.KeyDown,
135 "Right": tcell.KeyRight,
136 "Left": tcell.KeyLeft,
137 "UpLeft": tcell.KeyUpLeft,
138 "UpRight": tcell.KeyUpRight,
139 "DownLeft": tcell.KeyDownLeft,
140 "DownRight": tcell.KeyDownRight,
141 "Center": tcell.KeyCenter,
142 "PageUp": tcell.KeyPgUp,
143 "PageDown": tcell.KeyPgDn,
144 "Home": tcell.KeyHome,
146 "Insert": tcell.KeyInsert,
147 "Delete": tcell.KeyDelete,
148 "Help": tcell.KeyHelp,
149 "Exit": tcell.KeyExit,
150 "Clear": tcell.KeyClear,
151 "Cancel": tcell.KeyCancel,
152 "Print": tcell.KeyPrint,
153 "Pause": tcell.KeyPause,
154 "Backtab": tcell.KeyBacktab,
219 "CtrlSpace": tcell.KeyCtrlSpace,
220 "CtrlA": tcell.KeyCtrlA,
221 "CtrlB": tcell.KeyCtrlB,
222 "CtrlC": tcell.KeyCtrlC,
223 "CtrlD": tcell.KeyCtrlD,
224 "CtrlE": tcell.KeyCtrlE,
225 "CtrlF": tcell.KeyCtrlF,
226 "CtrlG": tcell.KeyCtrlG,
227 "CtrlH": tcell.KeyCtrlH,
228 "CtrlI": tcell.KeyCtrlI,
229 "CtrlJ": tcell.KeyCtrlJ,
230 "CtrlK": tcell.KeyCtrlK,
231 "CtrlL": tcell.KeyCtrlL,
232 "CtrlM": tcell.KeyCtrlM,
233 "CtrlN": tcell.KeyCtrlN,
234 "CtrlO": tcell.KeyCtrlO,
235 "CtrlP": tcell.KeyCtrlP,
236 "CtrlQ": tcell.KeyCtrlQ,
237 "CtrlR": tcell.KeyCtrlR,
238 "CtrlS": tcell.KeyCtrlS,
239 "CtrlT": tcell.KeyCtrlT,
240 "CtrlU": tcell.KeyCtrlU,
241 "CtrlV": tcell.KeyCtrlV,
242 "CtrlW": tcell.KeyCtrlW,
243 "CtrlX": tcell.KeyCtrlX,
244 "CtrlY": tcell.KeyCtrlY,
245 "CtrlZ": tcell.KeyCtrlZ,
246 "CtrlLeftSq": tcell.KeyCtrlLeftSq,
247 "CtrlBackslash": tcell.KeyCtrlBackslash,
248 "CtrlRightSq": tcell.KeyCtrlRightSq,
249 "CtrlCarat": tcell.KeyCtrlCarat,
250 "CtrlUnderscore": tcell.KeyCtrlUnderscore,
251 "CtrlPageUp": tcell.KeyCtrlPgUp,
252 "CtrlPageDown": tcell.KeyCtrlPgDn,
255 "Escape": tcell.KeyEscape,
256 "Enter": tcell.KeyEnter,
257 "Backspace": tcell.KeyBackspace2,
259 // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
260 "PgUp": tcell.KeyPgUp,
261 "PgDown": tcell.KeyPgDn,
264 // The Key struct holds the data for a keypress (keycode + modifiers)
267 modifiers tcell.ModMask
268 buttons tcell.ButtonMask
273 // InitBindings initializes the keybindings for micro
274 func InitBindings() {
275 bindings = make(map[Key][]func(*View, bool) bool)
276 bindingsStr = make(map[string]string)
277 mouseBindings = make(map[Key][]func(*View, bool, *tcell.EventMouse) bool)
279 var parsed map[string]string
280 defaults := DefaultBindings()
282 filename := configDir + "/bindings.json"
283 if _, e := os.Stat(filename); e == nil {
284 input, err := ioutil.ReadFile(filename)
286 TermMessage("Error reading bindings.json file: " + err.Error())
290 err = json5.Unmarshal(input, &parsed)
292 TermMessage("Error reading bindings.json:", err.Error())
296 parseBindings(defaults)
297 parseBindings(parsed)
300 func parseBindings(userBindings map[string]string) {
301 for k, v := range userBindings {
306 // findKey will find binding Key 'b' using string 'k'
307 func findKey(k string) (b Key, ok bool) {
308 modifiers := tcell.ModNone
310 // First, we'll strip off all the modifiers in the name and add them to the
315 case strings.HasPrefix(k, "-"):
316 // We optionally support dashes between modifiers
318 case strings.HasPrefix(k, "Ctrl") && k != "CtrlH":
319 // CtrlH technically does not have a 'Ctrl' modifier because it is really backspace
321 modifiers |= tcell.ModCtrl
322 case strings.HasPrefix(k, "Alt"):
324 modifiers |= tcell.ModAlt
325 case strings.HasPrefix(k, "Shift"):
327 modifiers |= tcell.ModShift
328 case strings.HasPrefix(k, "\x1b"):
331 modifiers: modifiers,
342 return Key{buttons: -1}, false
345 // Control is handled specially, since some character codes in bindingKeys
346 // are different when Control is depressed. We should check for Control keys
348 if modifiers&tcell.ModCtrl != 0 {
349 // see if the key is in bindingKeys with the Ctrl prefix.
350 k = string(unicode.ToUpper(rune(k[0]))) + k[1:]
351 if code, ok := bindingKeys["Ctrl"+k]; ok {
352 // It is, we're done.
355 modifiers: modifiers,
362 // See if we can find the key in bindingKeys
363 if code, ok := bindingKeys[k]; ok {
366 modifiers: modifiers,
372 // See if we can find the key in bindingMouse
373 if code, ok := bindingMouse[k]; ok {
375 modifiers: modifiers,
381 // If we were given one character, then we've got a rune.
384 keyCode: tcell.KeyRune,
385 modifiers: modifiers,
391 // We don't know what happened.
392 return Key{buttons: -1}, false
395 // findAction will find 'action' using string 'v'
396 func findAction(v string) (action func(*View, bool) bool) {
397 action, ok := bindingActions[v]
399 // If the user seems to be binding a function that doesn't exist
400 // We hope that it's a lua function that exists and bind it to that
401 action = LuaFunctionBinding(v)
406 func findMouseAction(v string) func(*View, bool, *tcell.EventMouse) bool {
407 action, ok := mouseBindingActions[v]
409 // If the user seems to be binding a function that doesn't exist
410 // We hope that it's a lua function that exists and bind it to that
411 action = LuaFunctionMouseBinding(v)
416 // TryBindKey tries to bind a key by writing to configDir/bindings.json
417 // This function is unused for now
418 func TryBindKey(k, v string) {
419 filename := configDir + "/bindings.json"
420 if _, e := os.Stat(filename); e == nil {
421 input, err := ioutil.ReadFile(filename)
423 TermMessage("Error reading bindings.json file: " + err.Error())
428 lines := strings.Split(string(input), "\n")
429 for i, l := range lines {
430 parts := strings.Split(l, ":")
432 if strings.Contains(parts[0], k) {
434 TermMessage("Warning: Keybinding conflict:", k, " has been overwritten")
439 binding := fmt.Sprintf(" \"%s\": \"%s\",", k, v)
441 lines = append([]string{lines[0], binding}, lines[conflict:]...)
443 lines = append(append(lines[:conflict], binding), lines[conflict+1:]...)
445 txt := strings.Join(lines, "\n")
446 err = ioutil.WriteFile(filename, []byte(txt), 0644)
453 // BindKey takes a key and an action and binds the two together
454 func BindKey(k, v string) {
455 key, ok := findKey(k)
457 TermMessage("Unknown keybinding: " + k)
460 if v == "ToggleHelp" {
463 if v == "ToggleKeyMenu" {
466 if helpBinding == k && v != "ToggleHelp" {
469 if kmenuBinding == k && v != "ToggleKeyMenu" {
473 actionNames := strings.Split(v, ",")
474 if actionNames[0] == "UnbindKey" {
475 delete(bindings, key)
476 delete(mouseBindings, key)
477 delete(bindingsStr, k)
478 if len(actionNames) == 1 {
481 actionNames = append(actionNames[:0], actionNames[1:]...)
483 actions := make([]func(*View, bool) bool, 0, len(actionNames))
484 mouseActions := make([]func(*View, bool, *tcell.EventMouse) bool, 0, len(actionNames))
485 for _, actionName := range actionNames {
486 if strings.HasPrefix(actionName, "Mouse") {
487 mouseActions = append(mouseActions, findMouseAction(actionName))
488 } else if strings.HasPrefix(actionName, "command:") {
489 cmd := strings.SplitN(actionName, ":", 2)[1]
490 actions = append(actions, CommandAction(cmd))
491 } else if strings.HasPrefix(actionName, "command-edit:") {
492 cmd := strings.SplitN(actionName, ":", 2)[1]
493 actions = append(actions, CommandEditAction(cmd))
495 actions = append(actions, findAction(actionName))
499 if len(actions) > 0 {
500 // Can't have a binding be both mouse and normal
501 delete(mouseBindings, key)
502 bindings[key] = actions
504 } else if len(mouseActions) > 0 {
505 // Can't have a binding be both mouse and normal
506 delete(bindings, key)
507 mouseBindings[key] = mouseActions
511 // DefaultBindings returns a map containing micro's default keybindings
512 func DefaultBindings() map[string]string {
513 return map[string]string{
515 "Down": "CursorDown",
516 "Right": "CursorRight",
517 "Left": "CursorLeft",
518 "ShiftUp": "SelectUp",
519 "ShiftDown": "SelectDown",
520 "ShiftLeft": "SelectLeft",
521 "ShiftRight": "SelectRight",
522 "AltLeft": "WordLeft",
523 "AltRight": "WordRight",
524 "AltUp": "MoveLinesUp",
525 "AltDown": "MoveLinesDown",
526 "AltShiftRight": "SelectWordRight",
527 "AltShiftLeft": "SelectWordLeft",
528 "CtrlLeft": "StartOfLine",
529 "CtrlRight": "EndOfLine",
530 "CtrlShiftLeft": "SelectToStartOfLine",
531 "ShiftHome": "SelectToStartOfLine",
532 "CtrlShiftRight": "SelectToEndOfLine",
533 "ShiftEnd": "SelectToEndOfLine",
534 "CtrlUp": "CursorStart",
535 "CtrlDown": "CursorEnd",
536 "CtrlShiftUp": "SelectToStart",
537 "CtrlShiftDown": "SelectToEnd",
538 "Alt-{": "ParagraphPrevious",
539 "Alt-}": "ParagraphNext",
540 "Enter": "InsertNewline",
541 "CtrlH": "Backspace",
542 "Backspace": "Backspace",
543 "Alt-CtrlH": "DeleteWordLeft",
544 "Alt-Backspace": "DeleteWordLeft",
545 "Tab": "IndentSelection,InsertTab",
546 "Backtab": "OutdentSelection,OutdentLine",
551 "CtrlP": "FindPrevious",
557 "CtrlD": "DuplicateLine",
559 "CtrlA": "SelectAll",
561 "Alt,": "PreviousTab",
563 "Home": "StartOfLine",
565 "CtrlHome": "CursorStart",
566 "CtrlEnd": "CursorEnd",
567 "PageUp": "CursorPageUp",
568 "PageDown": "CursorPageDown",
569 "CtrlPageUp": "PreviousTab",
570 "CtrlPageDown": "NextTab",
571 "CtrlG": "ToggleHelp",
572 "Alt-g": "ToggleKeyMenu",
573 "CtrlR": "ToggleRuler",
576 "CtrlB": "ShellMode",
578 "CtrlE": "CommandMode",
579 "CtrlW": "NextSplit",
580 "CtrlU": "ToggleMacro",
581 "CtrlJ": "PlayMacro",
582 "Insert": "ToggleOverwriteMode",
584 // Emacs-style keybindings
585 "Alt-f": "WordRight",
587 "Alt-a": "StartOfLine",
588 "Alt-e": "EndOfLine",
589 // "Alt-p": "CursorUp",
590 // "Alt-n": "CursorDown",
592 // Integration with file managers
601 "MouseWheelUp": "ScrollUp",
602 "MouseWheelDown": "ScrollDown",
603 "MouseLeft": "MousePress",
604 "MouseMiddle": "PastePrimary",
605 "Ctrl-MouseLeft": "MouseMultiCursor",
607 "Alt-n": "SpawnMultiCursor",
608 "Alt-m": "SpawnMultiCursorSelect",
609 "Alt-p": "RemoveMultiCursor",
610 "Alt-c": "RemoveAllMultiCursors",
611 "Alt-x": "SkipMultiCursor",