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 "RemoveMultiCursor": (*View).RemoveMultiCursor,
113 "RemoveAllMultiCursors": (*View).RemoveAllMultiCursors,
114 "SkipMultiCursor": (*View).SkipMultiCursor,
115 "JumpToMatchingBrace": (*View).JumpToMatchingBrace,
117 // This was changed to InsertNewline but I don't want to break backwards compatibility
118 "InsertEnter": (*View).InsertNewline,
121 var bindingMouse = map[string]tcell.ButtonMask{
122 "MouseLeft": tcell.Button1,
123 "MouseMiddle": tcell.Button2,
124 "MouseRight": tcell.Button3,
125 "MouseWheelUp": tcell.WheelUp,
126 "MouseWheelDown": tcell.WheelDown,
127 "MouseWheelLeft": tcell.WheelLeft,
128 "MouseWheelRight": tcell.WheelRight,
131 var bindingKeys = map[string]tcell.Key{
133 "Down": tcell.KeyDown,
134 "Right": tcell.KeyRight,
135 "Left": tcell.KeyLeft,
136 "UpLeft": tcell.KeyUpLeft,
137 "UpRight": tcell.KeyUpRight,
138 "DownLeft": tcell.KeyDownLeft,
139 "DownRight": tcell.KeyDownRight,
140 "Center": tcell.KeyCenter,
141 "PageUp": tcell.KeyPgUp,
142 "PageDown": tcell.KeyPgDn,
143 "Home": tcell.KeyHome,
145 "Insert": tcell.KeyInsert,
146 "Delete": tcell.KeyDelete,
147 "Help": tcell.KeyHelp,
148 "Exit": tcell.KeyExit,
149 "Clear": tcell.KeyClear,
150 "Cancel": tcell.KeyCancel,
151 "Print": tcell.KeyPrint,
152 "Pause": tcell.KeyPause,
153 "Backtab": tcell.KeyBacktab,
218 "CtrlSpace": tcell.KeyCtrlSpace,
219 "CtrlA": tcell.KeyCtrlA,
220 "CtrlB": tcell.KeyCtrlB,
221 "CtrlC": tcell.KeyCtrlC,
222 "CtrlD": tcell.KeyCtrlD,
223 "CtrlE": tcell.KeyCtrlE,
224 "CtrlF": tcell.KeyCtrlF,
225 "CtrlG": tcell.KeyCtrlG,
226 "CtrlH": tcell.KeyCtrlH,
227 "CtrlI": tcell.KeyCtrlI,
228 "CtrlJ": tcell.KeyCtrlJ,
229 "CtrlK": tcell.KeyCtrlK,
230 "CtrlL": tcell.KeyCtrlL,
231 "CtrlM": tcell.KeyCtrlM,
232 "CtrlN": tcell.KeyCtrlN,
233 "CtrlO": tcell.KeyCtrlO,
234 "CtrlP": tcell.KeyCtrlP,
235 "CtrlQ": tcell.KeyCtrlQ,
236 "CtrlR": tcell.KeyCtrlR,
237 "CtrlS": tcell.KeyCtrlS,
238 "CtrlT": tcell.KeyCtrlT,
239 "CtrlU": tcell.KeyCtrlU,
240 "CtrlV": tcell.KeyCtrlV,
241 "CtrlW": tcell.KeyCtrlW,
242 "CtrlX": tcell.KeyCtrlX,
243 "CtrlY": tcell.KeyCtrlY,
244 "CtrlZ": tcell.KeyCtrlZ,
245 "CtrlLeftSq": tcell.KeyCtrlLeftSq,
246 "CtrlBackslash": tcell.KeyCtrlBackslash,
247 "CtrlRightSq": tcell.KeyCtrlRightSq,
248 "CtrlCarat": tcell.KeyCtrlCarat,
249 "CtrlUnderscore": tcell.KeyCtrlUnderscore,
250 "CtrlPageUp": tcell.KeyCtrlPgUp,
251 "CtrlPageDown": tcell.KeyCtrlPgDn,
254 "Escape": tcell.KeyEscape,
255 "Enter": tcell.KeyEnter,
256 "Backspace": tcell.KeyBackspace2,
258 // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
259 "PgUp": tcell.KeyPgUp,
260 "PgDown": tcell.KeyPgDn,
263 // The Key struct holds the data for a keypress (keycode + modifiers)
266 modifiers tcell.ModMask
267 buttons tcell.ButtonMask
272 // InitBindings initializes the keybindings for micro
273 func InitBindings() {
274 bindings = make(map[Key][]func(*View, bool) bool)
275 bindingsStr = make(map[string]string)
276 mouseBindings = make(map[Key][]func(*View, bool, *tcell.EventMouse) bool)
278 var parsed map[string]string
279 defaults := DefaultBindings()
281 filename := configDir + "/bindings.json"
282 if _, e := os.Stat(filename); e == nil {
283 input, err := ioutil.ReadFile(filename)
285 TermMessage("Error reading bindings.json file: " + err.Error())
289 err = json5.Unmarshal(input, &parsed)
291 TermMessage("Error reading bindings.json:", err.Error())
295 parseBindings(defaults)
296 parseBindings(parsed)
299 func parseBindings(userBindings map[string]string) {
300 for k, v := range userBindings {
305 // findKey will find binding Key 'b' using string 'k'
306 func findKey(k string) (b Key, ok bool) {
307 modifiers := tcell.ModNone
309 // First, we'll strip off all the modifiers in the name and add them to the
314 case strings.HasPrefix(k, "-"):
315 // We optionally support dashes between modifiers
317 case strings.HasPrefix(k, "Ctrl") && k != "CtrlH":
318 // CtrlH technically does not have a 'Ctrl' modifier because it is really backspace
320 modifiers |= tcell.ModCtrl
321 case strings.HasPrefix(k, "Alt"):
323 modifiers |= tcell.ModAlt
324 case strings.HasPrefix(k, "Shift"):
326 modifiers |= tcell.ModShift
327 case strings.HasPrefix(k, "\x1b"):
330 modifiers: modifiers,
340 // Control is handled specially, since some character codes in bindingKeys
341 // are different when Control is depressed. We should check for Control keys
343 if modifiers&tcell.ModCtrl != 0 {
344 // see if the key is in bindingKeys with the Ctrl prefix.
345 k = string(unicode.ToUpper(rune(k[0]))) + k[1:]
346 if code, ok := bindingKeys["Ctrl"+k]; ok {
347 // It is, we're done.
350 modifiers: modifiers,
357 // See if we can find the key in bindingKeys
358 if code, ok := bindingKeys[k]; ok {
361 modifiers: modifiers,
367 // See if we can find the key in bindingMouse
368 if code, ok := bindingMouse[k]; ok {
370 modifiers: modifiers,
376 // If we were given one character, then we've got a rune.
379 keyCode: tcell.KeyRune,
380 modifiers: modifiers,
386 // We don't know what happened.
387 return Key{buttons: -1}, false
390 // findAction will find 'action' using string 'v'
391 func findAction(v string) (action func(*View, bool) bool) {
392 action, ok := bindingActions[v]
394 // If the user seems to be binding a function that doesn't exist
395 // We hope that it's a lua function that exists and bind it to that
396 action = LuaFunctionBinding(v)
401 func findMouseAction(v string) func(*View, bool, *tcell.EventMouse) bool {
402 action, ok := mouseBindingActions[v]
404 // If the user seems to be binding a function that doesn't exist
405 // We hope that it's a lua function that exists and bind it to that
406 action = LuaFunctionMouseBinding(v)
411 // TryBindKey tries to bind a key by writing to configDir/bindings.json
412 // This function is unused for now
413 func TryBindKey(k, v string) {
414 filename := configDir + "/bindings.json"
415 if _, e := os.Stat(filename); e == nil {
416 input, err := ioutil.ReadFile(filename)
418 TermMessage("Error reading bindings.json file: " + err.Error())
423 lines := strings.Split(string(input), "\n")
424 for i, l := range lines {
425 parts := strings.Split(l, ":")
427 if strings.Contains(parts[0], k) {
429 TermMessage("Warning: Keybinding conflict:", k, " has been overwritten")
434 binding := fmt.Sprintf(" \"%s\": \"%s\",", k, v)
436 lines = append([]string{lines[0], binding}, lines[conflict:]...)
438 lines = append(append(lines[:conflict], binding), lines[conflict+1:]...)
440 txt := strings.Join(lines, "\n")
441 err = ioutil.WriteFile(filename, []byte(txt), 0644)
448 // BindKey takes a key and an action and binds the two together
449 func BindKey(k, v string) {
450 key, ok := findKey(k)
452 TermMessage("Unknown keybinding: " + k)
455 if v == "ToggleHelp" {
458 if v == "ToggleKeyMenu" {
461 if helpBinding == k && v != "ToggleHelp" {
464 if kmenuBinding == k && v != "ToggleKeyMenu" {
468 actionNames := strings.Split(v, ",")
469 if actionNames[0] == "UnbindKey" {
470 delete(bindings, key)
471 delete(mouseBindings, key)
472 delete(bindingsStr, k)
473 if len(actionNames) == 1 {
476 actionNames = append(actionNames[:0], actionNames[1:]...)
478 actions := make([]func(*View, bool) bool, 0, len(actionNames))
479 mouseActions := make([]func(*View, bool, *tcell.EventMouse) bool, 0, len(actionNames))
480 for _, actionName := range actionNames {
481 if strings.HasPrefix(actionName, "Mouse") {
482 mouseActions = append(mouseActions, findMouseAction(actionName))
483 } else if strings.HasPrefix(actionName, "command:") {
484 cmd := strings.SplitN(actionName, ":", 2)[1]
485 actions = append(actions, CommandAction(cmd))
486 } else if strings.HasPrefix(actionName, "command-edit:") {
487 cmd := strings.SplitN(actionName, ":", 2)[1]
488 actions = append(actions, CommandEditAction(cmd))
490 actions = append(actions, findAction(actionName))
494 if len(actions) > 0 {
495 // Can't have a binding be both mouse and normal
496 delete(mouseBindings, key)
497 bindings[key] = actions
499 } else if len(mouseActions) > 0 {
500 // Can't have a binding be both mouse and normal
501 delete(bindings, key)
502 mouseBindings[key] = mouseActions
506 // DefaultBindings returns a map containing micro's default keybindings
507 func DefaultBindings() map[string]string {
508 return map[string]string{
510 "Down": "CursorDown",
511 "Right": "CursorRight",
512 "Left": "CursorLeft",
513 "ShiftUp": "SelectUp",
514 "ShiftDown": "SelectDown",
515 "ShiftLeft": "SelectLeft",
516 "ShiftRight": "SelectRight",
517 "AltLeft": "WordLeft",
518 "AltRight": "WordRight",
519 "AltUp": "MoveLinesUp",
520 "AltDown": "MoveLinesDown",
521 "AltShiftRight": "SelectWordRight",
522 "AltShiftLeft": "SelectWordLeft",
523 "CtrlLeft": "StartOfLine",
524 "CtrlRight": "EndOfLine",
525 "CtrlShiftLeft": "SelectToStartOfLine",
526 "ShiftHome": "SelectToStartOfLine",
527 "CtrlShiftRight": "SelectToEndOfLine",
528 "ShiftEnd": "SelectToEndOfLine",
529 "CtrlUp": "CursorStart",
530 "CtrlDown": "CursorEnd",
531 "CtrlShiftUp": "SelectToStart",
532 "CtrlShiftDown": "SelectToEnd",
533 "Alt-{": "ParagraphPrevious",
534 "Alt-}": "ParagraphNext",
535 "Enter": "InsertNewline",
536 "CtrlH": "Backspace",
537 "Backspace": "Backspace",
538 "Alt-CtrlH": "DeleteWordLeft",
539 "Alt-Backspace": "DeleteWordLeft",
540 "Tab": "IndentSelection,InsertTab",
541 "Backtab": "OutdentSelection,OutdentLine",
546 "CtrlP": "FindPrevious",
552 "CtrlD": "DuplicateLine",
554 "CtrlA": "SelectAll",
556 "Alt,": "PreviousTab",
558 "Home": "StartOfLine",
560 "CtrlHome": "CursorStart",
561 "CtrlEnd": "CursorEnd",
562 "PageUp": "CursorPageUp",
563 "PageDown": "CursorPageDown",
564 "CtrlPageUp": "PreviousTab",
565 "CtrlPageDown": "NextTab",
566 "CtrlG": "ToggleHelp",
567 "Alt-g": "ToggleKeyMenu",
568 "CtrlR": "ToggleRuler",
571 "CtrlB": "ShellMode",
573 "CtrlE": "CommandMode",
574 "CtrlW": "NextSplit",
575 "CtrlU": "ToggleMacro",
576 "CtrlJ": "PlayMacro",
577 "Insert": "ToggleOverwriteMode",
579 // Emacs-style keybindings
580 "Alt-f": "WordRight",
582 "Alt-a": "StartOfLine",
583 "Alt-e": "EndOfLine",
584 // "Alt-p": "CursorUp",
585 // "Alt-n": "CursorDown",
587 // Integration with file managers
596 "MouseWheelUp": "ScrollUp",
597 "MouseWheelDown": "ScrollDown",
598 "MouseLeft": "MousePress",
599 "MouseMiddle": "PastePrimary",
600 "Ctrl-MouseLeft": "MouseMultiCursor",
602 "Alt-n": "SpawnMultiCursor",
603 "Alt-p": "RemoveMultiCursor",
604 "Alt-c": "RemoveAllMultiCursors",
605 "Alt-x": "SkipMultiCursor",