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,
341 // Control is handled specially, since some character codes in bindingKeys
342 // are different when Control is depressed. We should check for Control keys
344 if modifiers&tcell.ModCtrl != 0 {
345 // see if the key is in bindingKeys with the Ctrl prefix.
346 k = string(unicode.ToUpper(rune(k[0]))) + k[1:]
347 if code, ok := bindingKeys["Ctrl"+k]; ok {
348 // It is, we're done.
351 modifiers: modifiers,
358 // See if we can find the key in bindingKeys
359 if code, ok := bindingKeys[k]; ok {
362 modifiers: modifiers,
368 // See if we can find the key in bindingMouse
369 if code, ok := bindingMouse[k]; ok {
371 modifiers: modifiers,
377 // If we were given one character, then we've got a rune.
380 keyCode: tcell.KeyRune,
381 modifiers: modifiers,
387 // We don't know what happened.
388 return Key{buttons: -1}, false
391 // findAction will find 'action' using string 'v'
392 func findAction(v string) (action func(*View, bool) bool) {
393 action, ok := bindingActions[v]
395 // If the user seems to be binding a function that doesn't exist
396 // We hope that it's a lua function that exists and bind it to that
397 action = LuaFunctionBinding(v)
402 func findMouseAction(v string) func(*View, bool, *tcell.EventMouse) bool {
403 action, ok := mouseBindingActions[v]
405 // If the user seems to be binding a function that doesn't exist
406 // We hope that it's a lua function that exists and bind it to that
407 action = LuaFunctionMouseBinding(v)
412 // TryBindKey tries to bind a key by writing to configDir/bindings.json
413 // This function is unused for now
414 func TryBindKey(k, v string) {
415 filename := configDir + "/bindings.json"
416 if _, e := os.Stat(filename); e == nil {
417 input, err := ioutil.ReadFile(filename)
419 TermMessage("Error reading bindings.json file: " + err.Error())
424 lines := strings.Split(string(input), "\n")
425 for i, l := range lines {
426 parts := strings.Split(l, ":")
428 if strings.Contains(parts[0], k) {
430 TermMessage("Warning: Keybinding conflict:", k, " has been overwritten")
435 binding := fmt.Sprintf(" \"%s\": \"%s\",", k, v)
437 lines = append([]string{lines[0], binding}, lines[conflict:]...)
439 lines = append(append(lines[:conflict], binding), lines[conflict+1:]...)
441 txt := strings.Join(lines, "\n")
442 err = ioutil.WriteFile(filename, []byte(txt), 0644)
449 // BindKey takes a key and an action and binds the two together
450 func BindKey(k, v string) {
451 key, ok := findKey(k)
453 TermMessage("Unknown keybinding: " + k)
456 if v == "ToggleHelp" {
459 if v == "ToggleKeyMenu" {
462 if helpBinding == k && v != "ToggleHelp" {
465 if kmenuBinding == k && v != "ToggleKeyMenu" {
469 actionNames := strings.Split(v, ",")
470 if actionNames[0] == "UnbindKey" {
471 delete(bindings, key)
472 delete(mouseBindings, key)
473 delete(bindingsStr, k)
474 if len(actionNames) == 1 {
477 actionNames = append(actionNames[:0], actionNames[1:]...)
479 actions := make([]func(*View, bool) bool, 0, len(actionNames))
480 mouseActions := make([]func(*View, bool, *tcell.EventMouse) bool, 0, len(actionNames))
481 for _, actionName := range actionNames {
482 if strings.HasPrefix(actionName, "Mouse") {
483 mouseActions = append(mouseActions, findMouseAction(actionName))
484 } else if strings.HasPrefix(actionName, "command:") {
485 cmd := strings.SplitN(actionName, ":", 2)[1]
486 actions = append(actions, CommandAction(cmd))
487 } else if strings.HasPrefix(actionName, "command-edit:") {
488 cmd := strings.SplitN(actionName, ":", 2)[1]
489 actions = append(actions, CommandEditAction(cmd))
491 actions = append(actions, findAction(actionName))
495 if len(actions) > 0 {
496 // Can't have a binding be both mouse and normal
497 delete(mouseBindings, key)
498 bindings[key] = actions
500 } else if len(mouseActions) > 0 {
501 // Can't have a binding be both mouse and normal
502 delete(bindings, key)
503 mouseBindings[key] = mouseActions
507 // DefaultBindings returns a map containing micro's default keybindings
508 func DefaultBindings() map[string]string {
509 return map[string]string{
511 "Down": "CursorDown",
512 "Right": "CursorRight",
513 "Left": "CursorLeft",
514 "ShiftUp": "SelectUp",
515 "ShiftDown": "SelectDown",
516 "ShiftLeft": "SelectLeft",
517 "ShiftRight": "SelectRight",
518 "AltLeft": "WordLeft",
519 "AltRight": "WordRight",
520 "AltUp": "MoveLinesUp",
521 "AltDown": "MoveLinesDown",
522 "AltShiftRight": "SelectWordRight",
523 "AltShiftLeft": "SelectWordLeft",
524 "CtrlLeft": "StartOfLine",
525 "CtrlRight": "EndOfLine",
526 "CtrlShiftLeft": "SelectToStartOfLine",
527 "ShiftHome": "SelectToStartOfLine",
528 "CtrlShiftRight": "SelectToEndOfLine",
529 "ShiftEnd": "SelectToEndOfLine",
530 "CtrlUp": "CursorStart",
531 "CtrlDown": "CursorEnd",
532 "CtrlShiftUp": "SelectToStart",
533 "CtrlShiftDown": "SelectToEnd",
534 "Alt-{": "ParagraphPrevious",
535 "Alt-}": "ParagraphNext",
536 "Enter": "InsertNewline",
537 "CtrlH": "Backspace",
538 "Backspace": "Backspace",
539 "Alt-CtrlH": "DeleteWordLeft",
540 "Alt-Backspace": "DeleteWordLeft",
541 "Tab": "IndentSelection,InsertTab",
542 "Backtab": "OutdentSelection,OutdentLine",
547 "CtrlP": "FindPrevious",
553 "CtrlD": "DuplicateLine",
555 "CtrlA": "SelectAll",
557 "Alt,": "PreviousTab",
559 "Home": "StartOfLine",
561 "CtrlHome": "CursorStart",
562 "CtrlEnd": "CursorEnd",
563 "PageUp": "CursorPageUp",
564 "PageDown": "CursorPageDown",
565 "CtrlPageUp": "PreviousTab",
566 "CtrlPageDown": "NextTab",
567 "CtrlG": "ToggleHelp",
568 "Alt-g": "ToggleKeyMenu",
569 "CtrlR": "ToggleRuler",
572 "CtrlB": "ShellMode",
574 "CtrlE": "CommandMode",
575 "CtrlW": "NextSplit",
576 "CtrlU": "ToggleMacro",
577 "CtrlJ": "PlayMacro",
578 "Insert": "ToggleOverwriteMode",
580 // Emacs-style keybindings
581 "Alt-f": "WordRight",
583 "Alt-a": "StartOfLine",
584 "Alt-e": "EndOfLine",
585 // "Alt-p": "CursorUp",
586 // "Alt-n": "CursorDown",
588 // Integration with file managers
597 "MouseWheelUp": "ScrollUp",
598 "MouseWheelDown": "ScrollDown",
599 "MouseLeft": "MousePress",
600 "MouseMiddle": "PastePrimary",
601 "Ctrl-MouseLeft": "MouseMultiCursor",
603 "Alt-n": "SpawnMultiCursor",
604 "Alt-m": "SpawnMultiCursorSelect",
605 "Alt-p": "RemoveMultiCursor",
606 "Alt-c": "RemoveAllMultiCursors",
607 "Alt-x": "SkipMultiCursor",