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 func TryBindKey(k, v string) {
410 filename := configDir + "/bindings.json"
411 if _, e := os.Stat(filename); e == nil {
412 input, err := ioutil.ReadFile(filename)
414 TermMessage("Error reading bindings.json file: " + err.Error())
419 lines := strings.Split(string(input), "\n")
420 for i, l := range lines {
421 parts := strings.Split(l, ":")
423 if strings.Contains(parts[0], k) {
425 TermMessage("Warning: Keybinding conflict:", k, " has been overwritten")
430 binding := fmt.Sprintf(" \"%s\": \"%s\",", k, v)
432 lines = append([]string{lines[0], binding}, lines[conflict:]...)
434 lines = append(append(lines[:conflict], binding), lines[conflict+1:]...)
436 txt := strings.Join(lines, "\n")
437 err = ioutil.WriteFile(filename, []byte(txt), 0644)
444 // BindKey takes a key and an action and binds the two together
445 func BindKey(k, v string) {
446 key, ok := findKey(k)
448 TermMessage("Unknown keybinding: " + k)
451 if v == "ToggleHelp" {
454 if v == "ToggleKeyMenu" {
457 if helpBinding == k && v != "ToggleHelp" {
460 if kmenuBinding == k && v != "ToggleKeyMenu" {
464 actionNames := strings.Split(v, ",")
465 if actionNames[0] == "UnbindKey" {
466 delete(bindings, key)
467 delete(mouseBindings, key)
468 delete(bindingsStr, k)
469 if len(actionNames) == 1 {
472 actionNames = append(actionNames[:0], actionNames[1:]...)
474 actions := make([]func(*View, bool) bool, 0, len(actionNames))
475 mouseActions := make([]func(*View, bool, *tcell.EventMouse) bool, 0, len(actionNames))
476 for _, actionName := range actionNames {
477 if strings.HasPrefix(actionName, "Mouse") {
478 mouseActions = append(mouseActions, findMouseAction(actionName))
479 } else if strings.HasPrefix(actionName, "command:") {
480 cmd := strings.SplitN(actionName, ":", 2)[1]
481 actions = append(actions, CommandAction(cmd))
483 actions = append(actions, findAction(actionName))
487 if len(actions) > 0 {
488 // Can't have a binding be both mouse and normal
489 delete(mouseBindings, key)
490 bindings[key] = actions
492 } else if len(mouseActions) > 0 {
493 // Can't have a binding be both mouse and normal
494 delete(bindings, key)
495 mouseBindings[key] = mouseActions
499 // DefaultBindings returns a map containing micro's default keybindings
500 func DefaultBindings() map[string]string {
501 return map[string]string{
503 "Down": "CursorDown",
504 "Right": "CursorRight",
505 "Left": "CursorLeft",
506 "ShiftUp": "SelectUp",
507 "ShiftDown": "SelectDown",
508 "ShiftLeft": "SelectLeft",
509 "ShiftRight": "SelectRight",
510 "AltLeft": "WordLeft",
511 "AltRight": "WordRight",
512 "AltUp": "MoveLinesUp",
513 "AltDown": "MoveLinesDown",
514 "AltShiftRight": "SelectWordRight",
515 "AltShiftLeft": "SelectWordLeft",
516 "CtrlLeft": "StartOfLine",
517 "CtrlRight": "EndOfLine",
518 "CtrlShiftLeft": "SelectToStartOfLine",
519 "ShiftHome": "SelectToStartOfLine",
520 "CtrlShiftRight": "SelectToEndOfLine",
521 "ShiftEnd": "SelectToEndOfLine",
522 "CtrlUp": "CursorStart",
523 "CtrlDown": "CursorEnd",
524 "CtrlShiftUp": "SelectToStart",
525 "CtrlShiftDown": "SelectToEnd",
526 "Alt-{": "ParagraphPrevious",
527 "Alt-}": "ParagraphNext",
528 "Enter": "InsertNewline",
529 "CtrlH": "Backspace",
530 "Backspace": "Backspace",
531 "Alt-CtrlH": "DeleteWordLeft",
532 "Alt-Backspace": "DeleteWordLeft",
533 "Tab": "IndentSelection,InsertTab",
534 "Backtab": "OutdentSelection,OutdentLine",
539 "CtrlP": "FindPrevious",
545 "CtrlD": "DuplicateLine",
547 "CtrlA": "SelectAll",
549 "Alt,": "PreviousTab",
551 "Home": "StartOfLine",
553 "CtrlHome": "CursorStart",
554 "CtrlEnd": "CursorEnd",
555 "PageUp": "CursorPageUp",
556 "PageDown": "CursorPageDown",
557 "CtrlPageUp": "PreviousTab",
558 "CtrlPageDown": "NextTab",
559 "CtrlG": "ToggleHelp",
560 "Alt-g": "ToggleKeyMenu",
561 "CtrlR": "ToggleRuler",
564 "CtrlB": "ShellMode",
566 "CtrlE": "CommandMode",
567 "CtrlW": "NextSplit",
568 "CtrlU": "ToggleMacro",
569 "CtrlJ": "PlayMacro",
570 "Insert": "ToggleOverwriteMode",
572 // Emacs-style keybindings
573 "Alt-f": "WordRight",
575 "Alt-a": "StartOfLine",
576 "Alt-e": "EndOfLine",
577 // "Alt-p": "CursorUp",
578 // "Alt-n": "CursorDown",
580 // Integration with file managers
589 "MouseWheelUp": "ScrollUp",
590 "MouseWheelDown": "ScrollDown",
591 "MouseLeft": "MousePress",
592 "MouseMiddle": "PastePrimary",
593 "Ctrl-MouseLeft": "MouseMultiCursor",
595 "Alt-n": "SpawnMultiCursor",
596 "Alt-p": "RemoveMultiCursor",
597 "Alt-c": "RemoveAllMultiCursors",
598 "Alt-x": "SkipMultiCursor",