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 "SelectToStartOfLine": (*View).SelectToStartOfLine,
47 "SelectToEndOfLine": (*View).SelectToEndOfLine,
48 "ParagraphPrevious": (*View).ParagraphPrevious,
49 "ParagraphNext": (*View).ParagraphNext,
50 "InsertNewline": (*View).InsertNewline,
51 "InsertSpace": (*View).InsertSpace,
52 "Backspace": (*View).Backspace,
53 "Delete": (*View).Delete,
54 "InsertTab": (*View).InsertTab,
56 "SaveAll": (*View).SaveAll,
57 "SaveAs": (*View).SaveAs,
59 "FindNext": (*View).FindNext,
60 "FindPrevious": (*View).FindPrevious,
61 "Center": (*View).Center,
66 "CutLine": (*View).CutLine,
67 "DuplicateLine": (*View).DuplicateLine,
68 "DeleteLine": (*View).DeleteLine,
69 "MoveLinesUp": (*View).MoveLinesUp,
70 "MoveLinesDown": (*View).MoveLinesDown,
71 "IndentSelection": (*View).IndentSelection,
72 "OutdentSelection": (*View).OutdentSelection,
73 "OutdentLine": (*View).OutdentLine,
74 "Paste": (*View).Paste,
75 "PastePrimary": (*View).PastePrimary,
76 "SelectAll": (*View).SelectAll,
77 "OpenFile": (*View).OpenFile,
78 "Start": (*View).Start,
80 "PageUp": (*View).PageUp,
81 "PageDown": (*View).PageDown,
82 "HalfPageUp": (*View).HalfPageUp,
83 "HalfPageDown": (*View).HalfPageDown,
84 "StartOfLine": (*View).StartOfLine,
85 "EndOfLine": (*View).EndOfLine,
86 "ToggleHelp": (*View).ToggleHelp,
87 "ToggleKeyMenu": (*View).ToggleKeyMenu,
88 "ToggleRuler": (*View).ToggleRuler,
89 "JumpLine": (*View).JumpLine,
90 "ClearStatus": (*View).ClearStatus,
91 "ShellMode": (*View).ShellMode,
92 "CommandMode": (*View).CommandMode,
93 "ToggleOverwriteMode": (*View).ToggleOverwriteMode,
94 "Escape": (*View).Escape,
96 "QuitAll": (*View).QuitAll,
97 "AddTab": (*View).AddTab,
98 "PreviousTab": (*View).PreviousTab,
99 "NextTab": (*View).NextTab,
100 "NextSplit": (*View).NextSplit,
101 "PreviousSplit": (*View).PreviousSplit,
102 "Unsplit": (*View).Unsplit,
103 "VSplit": (*View).VSplitBinding,
104 "HSplit": (*View).HSplitBinding,
105 "ToggleMacro": (*View).ToggleMacro,
106 "PlayMacro": (*View).PlayMacro,
107 "Suspend": (*View).Suspend,
108 "ScrollUp": (*View).ScrollUpAction,
109 "ScrollDown": (*View).ScrollDownAction,
110 "SpawnMultiCursor": (*View).SpawnMultiCursor,
111 "RemoveMultiCursor": (*View).RemoveMultiCursor,
112 "RemoveAllMultiCursors": (*View).RemoveAllMultiCursors,
113 "SkipMultiCursor": (*View).SkipMultiCursor,
115 // This was changed to InsertNewline but I don't want to break backwards compatibility
116 "InsertEnter": (*View).InsertNewline,
119 var bindingMouse = map[string]tcell.ButtonMask{
120 "MouseLeft": tcell.Button1,
121 "MouseMiddle": tcell.Button2,
122 "MouseRight": tcell.Button3,
123 "MouseWheelUp": tcell.WheelUp,
124 "MouseWheelDown": tcell.WheelDown,
125 "MouseWheelLeft": tcell.WheelLeft,
126 "MouseWheelRight": tcell.WheelRight,
129 var bindingKeys = map[string]tcell.Key{
131 "Down": tcell.KeyDown,
132 "Right": tcell.KeyRight,
133 "Left": tcell.KeyLeft,
134 "UpLeft": tcell.KeyUpLeft,
135 "UpRight": tcell.KeyUpRight,
136 "DownLeft": tcell.KeyDownLeft,
137 "DownRight": tcell.KeyDownRight,
138 "Center": tcell.KeyCenter,
139 "PageUp": tcell.KeyPgUp,
140 "PageDown": tcell.KeyPgDn,
141 "Home": tcell.KeyHome,
143 "Insert": tcell.KeyInsert,
144 "Delete": tcell.KeyDelete,
145 "Help": tcell.KeyHelp,
146 "Exit": tcell.KeyExit,
147 "Clear": tcell.KeyClear,
148 "Cancel": tcell.KeyCancel,
149 "Print": tcell.KeyPrint,
150 "Pause": tcell.KeyPause,
151 "Backtab": tcell.KeyBacktab,
216 "CtrlSpace": tcell.KeyCtrlSpace,
217 "CtrlA": tcell.KeyCtrlA,
218 "CtrlB": tcell.KeyCtrlB,
219 "CtrlC": tcell.KeyCtrlC,
220 "CtrlD": tcell.KeyCtrlD,
221 "CtrlE": tcell.KeyCtrlE,
222 "CtrlF": tcell.KeyCtrlF,
223 "CtrlG": tcell.KeyCtrlG,
224 "CtrlH": tcell.KeyCtrlH,
225 "CtrlI": tcell.KeyCtrlI,
226 "CtrlJ": tcell.KeyCtrlJ,
227 "CtrlK": tcell.KeyCtrlK,
228 "CtrlL": tcell.KeyCtrlL,
229 "CtrlM": tcell.KeyCtrlM,
230 "CtrlN": tcell.KeyCtrlN,
231 "CtrlO": tcell.KeyCtrlO,
232 "CtrlP": tcell.KeyCtrlP,
233 "CtrlQ": tcell.KeyCtrlQ,
234 "CtrlR": tcell.KeyCtrlR,
235 "CtrlS": tcell.KeyCtrlS,
236 "CtrlT": tcell.KeyCtrlT,
237 "CtrlU": tcell.KeyCtrlU,
238 "CtrlV": tcell.KeyCtrlV,
239 "CtrlW": tcell.KeyCtrlW,
240 "CtrlX": tcell.KeyCtrlX,
241 "CtrlY": tcell.KeyCtrlY,
242 "CtrlZ": tcell.KeyCtrlZ,
243 "CtrlLeftSq": tcell.KeyCtrlLeftSq,
244 "CtrlBackslash": tcell.KeyCtrlBackslash,
245 "CtrlRightSq": tcell.KeyCtrlRightSq,
246 "CtrlCarat": tcell.KeyCtrlCarat,
247 "CtrlUnderscore": tcell.KeyCtrlUnderscore,
248 "CtrlPageUp": tcell.KeyCtrlPgUp,
249 "CtrlPageDown": tcell.KeyCtrlPgDn,
252 "Escape": tcell.KeyEscape,
253 "Enter": tcell.KeyEnter,
254 "Backspace": tcell.KeyBackspace2,
256 // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
257 "PgUp": tcell.KeyPgUp,
258 "PgDown": tcell.KeyPgDn,
261 // The Key struct holds the data for a keypress (keycode + modifiers)
264 modifiers tcell.ModMask
265 buttons tcell.ButtonMask
270 // InitBindings initializes the keybindings for micro
271 func InitBindings() {
272 bindings = make(map[Key][]func(*View, bool) bool)
273 bindingsStr = make(map[string]string)
274 mouseBindings = make(map[Key][]func(*View, bool, *tcell.EventMouse) bool)
276 var parsed map[string]string
277 defaults := DefaultBindings()
279 filename := configDir + "/bindings.json"
280 if _, e := os.Stat(filename); e == nil {
281 input, err := ioutil.ReadFile(filename)
283 TermMessage("Error reading bindings.json file: " + err.Error())
287 err = json5.Unmarshal(input, &parsed)
289 TermMessage("Error reading bindings.json:", err.Error())
293 parseBindings(defaults)
294 parseBindings(parsed)
297 func parseBindings(userBindings map[string]string) {
298 for k, v := range userBindings {
303 // findKey will find binding Key 'b' using string 'k'
304 func findKey(k string) (b Key, ok bool) {
305 modifiers := tcell.ModNone
307 // First, we'll strip off all the modifiers in the name and add them to the
312 case strings.HasPrefix(k, "-"):
313 // We optionally support dashes between modifiers
315 case strings.HasPrefix(k, "Ctrl") && k != "CtrlH":
316 // CtrlH technically does not have a 'Ctrl' modifier because it is really backspace
318 modifiers |= tcell.ModCtrl
319 case strings.HasPrefix(k, "Alt"):
321 modifiers |= tcell.ModAlt
322 case strings.HasPrefix(k, "Shift"):
324 modifiers |= tcell.ModShift
325 case strings.HasPrefix(k, "\x1b"):
328 modifiers: modifiers,
338 // Control is handled specially, since some character codes in bindingKeys
339 // are different when Control is depressed. We should check for Control keys
341 if modifiers&tcell.ModCtrl != 0 {
342 // see if the key is in bindingKeys with the Ctrl prefix.
343 k = string(unicode.ToUpper(rune(k[0]))) + k[1:]
344 if code, ok := bindingKeys["Ctrl"+k]; ok {
345 // It is, we're done.
348 modifiers: modifiers,
355 // See if we can find the key in bindingKeys
356 if code, ok := bindingKeys[k]; ok {
359 modifiers: modifiers,
365 // See if we can find the key in bindingMouse
366 if code, ok := bindingMouse[k]; ok {
368 modifiers: modifiers,
374 // If we were given one character, then we've got a rune.
377 keyCode: tcell.KeyRune,
378 modifiers: modifiers,
384 // We don't know what happened.
385 return Key{buttons: -1}, false
388 // findAction will find 'action' using string 'v'
389 func findAction(v string) (action func(*View, bool) bool) {
390 action, ok := bindingActions[v]
392 // If the user seems to be binding a function that doesn't exist
393 // We hope that it's a lua function that exists and bind it to that
394 action = LuaFunctionBinding(v)
399 func findMouseAction(v string) func(*View, bool, *tcell.EventMouse) bool {
400 action, ok := mouseBindingActions[v]
402 // If the user seems to be binding a function that doesn't exist
403 // We hope that it's a lua function that exists and bind it to that
404 action = LuaFunctionMouseBinding(v)
409 // TryBindKey tries to bind a key by writing to configDir/bindings.json
410 // This function is unused for now
411 func TryBindKey(k, v string) {
412 filename := configDir + "/bindings.json"
413 if _, e := os.Stat(filename); e == nil {
414 input, err := ioutil.ReadFile(filename)
416 TermMessage("Error reading bindings.json file: " + err.Error())
421 lines := strings.Split(string(input), "\n")
422 for i, l := range lines {
423 parts := strings.Split(l, ":")
425 if strings.Contains(parts[0], k) {
427 TermMessage("Warning: Keybinding conflict:", k, " has been overwritten")
432 binding := fmt.Sprintf(" \"%s\": \"%s\",", k, v)
434 lines = append([]string{lines[0], binding}, lines[conflict:]...)
436 lines = append(append(lines[:conflict], binding), lines[conflict+1:]...)
438 txt := strings.Join(lines, "\n")
439 err = ioutil.WriteFile(filename, []byte(txt), 0644)
446 // BindKey takes a key and an action and binds the two together
447 func BindKey(k, v string) {
448 key, ok := findKey(k)
450 TermMessage("Unknown keybinding: " + k)
453 if v == "ToggleHelp" {
456 if v == "ToggleKeyMenu" {
459 if helpBinding == k && v != "ToggleHelp" {
462 if kmenuBinding == k && v != "ToggleKeyMenu" {
466 actionNames := strings.Split(v, ",")
467 if actionNames[0] == "UnbindKey" {
468 delete(bindings, key)
469 delete(mouseBindings, key)
470 delete(bindingsStr, k)
471 if len(actionNames) == 1 {
474 actionNames = append(actionNames[:0], actionNames[1:]...)
476 actions := make([]func(*View, bool) bool, 0, len(actionNames))
477 mouseActions := make([]func(*View, bool, *tcell.EventMouse) bool, 0, len(actionNames))
478 for _, actionName := range actionNames {
479 if strings.HasPrefix(actionName, "Mouse") {
480 mouseActions = append(mouseActions, findMouseAction(actionName))
481 } else if strings.HasPrefix(actionName, "command:") {
482 cmd := strings.SplitN(actionName, ":", 2)[1]
483 actions = append(actions, CommandAction(cmd))
484 } else if strings.HasPrefix(actionName, "command-edit:") {
485 cmd := strings.SplitN(actionName, ":", 2)[1]
486 actions = append(actions, CommandEditAction(cmd))
488 actions = append(actions, findAction(actionName))
492 if len(actions) > 0 {
493 // Can't have a binding be both mouse and normal
494 delete(mouseBindings, key)
495 bindings[key] = actions
497 } else if len(mouseActions) > 0 {
498 // Can't have a binding be both mouse and normal
499 delete(bindings, key)
500 mouseBindings[key] = mouseActions
504 // DefaultBindings returns a map containing micro's default keybindings
505 func DefaultBindings() map[string]string {
506 return map[string]string{
508 "Down": "CursorDown",
509 "Right": "CursorRight",
510 "Left": "CursorLeft",
511 "ShiftUp": "SelectUp",
512 "ShiftDown": "SelectDown",
513 "ShiftLeft": "SelectLeft",
514 "ShiftRight": "SelectRight",
515 "AltLeft": "WordLeft",
516 "AltRight": "WordRight",
517 "AltUp": "MoveLinesUp",
518 "AltDown": "MoveLinesDown",
519 "AltShiftRight": "SelectWordRight",
520 "AltShiftLeft": "SelectWordLeft",
521 "CtrlLeft": "StartOfLine",
522 "CtrlRight": "EndOfLine",
523 "CtrlShiftLeft": "SelectToStartOfLine",
524 "ShiftHome": "SelectToStartOfLine",
525 "CtrlShiftRight": "SelectToEndOfLine",
526 "ShiftEnd": "SelectToEndOfLine",
527 "CtrlUp": "CursorStart",
528 "CtrlDown": "CursorEnd",
529 "CtrlShiftUp": "SelectToStart",
530 "CtrlShiftDown": "SelectToEnd",
531 "Alt-{": "ParagraphPrevious",
532 "Alt-}": "ParagraphNext",
533 "Enter": "InsertNewline",
534 "CtrlH": "Backspace",
535 "Backspace": "Backspace",
536 "Alt-CtrlH": "DeleteWordLeft",
537 "Alt-Backspace": "DeleteWordLeft",
538 "Tab": "IndentSelection,InsertTab",
539 "Backtab": "OutdentSelection,OutdentLine",
544 "CtrlP": "FindPrevious",
550 "CtrlD": "DuplicateLine",
552 "CtrlA": "SelectAll",
554 "Alt,": "PreviousTab",
556 "Home": "StartOfLine",
558 "CtrlHome": "CursorStart",
559 "CtrlEnd": "CursorEnd",
560 "PageUp": "CursorPageUp",
561 "PageDown": "CursorPageDown",
562 "CtrlPageUp": "PreviousTab",
563 "CtrlPageDown": "NextTab",
564 "CtrlG": "ToggleHelp",
565 "Alt-g": "ToggleKeyMenu",
566 "CtrlR": "ToggleRuler",
569 "CtrlB": "ShellMode",
571 "CtrlE": "CommandMode",
572 "CtrlW": "NextSplit",
573 "CtrlU": "ToggleMacro",
574 "CtrlJ": "PlayMacro",
575 "Insert": "ToggleOverwriteMode",
577 // Emacs-style keybindings
578 "Alt-f": "WordRight",
580 "Alt-a": "StartOfLine",
581 "Alt-e": "EndOfLine",
582 // "Alt-p": "CursorUp",
583 // "Alt-n": "CursorDown",
585 // Integration with file managers
594 "MouseWheelUp": "ScrollUp",
595 "MouseWheelDown": "ScrollDown",
596 "MouseLeft": "MousePress",
597 "MouseMiddle": "PastePrimary",
598 "Ctrl-MouseLeft": "MouseMultiCursor",
600 "Alt-n": "SpawnMultiCursor",
601 "Alt-p": "RemoveMultiCursor",
602 "Alt-c": "RemoveAllMultiCursors",
603 "Alt-x": "SkipMultiCursor",