8 "github.com/zyedidia/json5/encoding/json5"
9 "github.com/zyedidia/tcell"
12 var bindings map[Key][]func(*View, bool) bool
13 var mouseBindings map[Key][]func(*View, bool, *tcell.EventMouse) bool
14 var helpBinding string
16 var mouseBindingActions = map[string]func(*View, bool, *tcell.EventMouse) bool{
17 "MousePress": (*View).MousePress,
18 "MouseMultiCursor": (*View).MouseMultiCursor,
21 var bindingActions = map[string]func(*View, bool) bool{
22 "CursorUp": (*View).CursorUp,
23 "CursorDown": (*View).CursorDown,
24 "CursorPageUp": (*View).CursorPageUp,
25 "CursorPageDown": (*View).CursorPageDown,
26 "CursorLeft": (*View).CursorLeft,
27 "CursorRight": (*View).CursorRight,
28 "CursorStart": (*View).CursorStart,
29 "CursorEnd": (*View).CursorEnd,
30 "SelectToStart": (*View).SelectToStart,
31 "SelectToEnd": (*View).SelectToEnd,
32 "SelectUp": (*View).SelectUp,
33 "SelectDown": (*View).SelectDown,
34 "SelectLeft": (*View).SelectLeft,
35 "SelectRight": (*View).SelectRight,
36 "WordRight": (*View).WordRight,
37 "WordLeft": (*View).WordLeft,
38 "SelectWordRight": (*View).SelectWordRight,
39 "SelectWordLeft": (*View).SelectWordLeft,
40 "DeleteWordRight": (*View).DeleteWordRight,
41 "DeleteWordLeft": (*View).DeleteWordLeft,
42 "SelectToStartOfLine": (*View).SelectToStartOfLine,
43 "SelectToEndOfLine": (*View).SelectToEndOfLine,
44 "InsertNewline": (*View).InsertNewline,
45 "InsertSpace": (*View).InsertSpace,
46 "Backspace": (*View).Backspace,
47 "Delete": (*View).Delete,
48 "InsertTab": (*View).InsertTab,
50 "SaveAll": (*View).SaveAll,
51 "SaveAs": (*View).SaveAs,
53 "FindNext": (*View).FindNext,
54 "FindPrevious": (*View).FindPrevious,
55 "Center": (*View).Center,
60 "CutLine": (*View).CutLine,
61 "DuplicateLine": (*View).DuplicateLine,
62 "DeleteLine": (*View).DeleteLine,
63 "MoveLinesUp": (*View).MoveLinesUp,
64 "MoveLinesDown": (*View).MoveLinesDown,
65 "IndentSelection": (*View).IndentSelection,
66 "OutdentSelection": (*View).OutdentSelection,
67 "OutdentLine": (*View).OutdentLine,
68 "Paste": (*View).Paste,
69 "PastePrimary": (*View).PastePrimary,
70 "SelectAll": (*View).SelectAll,
71 "OpenFile": (*View).OpenFile,
72 "Start": (*View).Start,
74 "PageUp": (*View).PageUp,
75 "PageDown": (*View).PageDown,
76 "HalfPageUp": (*View).HalfPageUp,
77 "HalfPageDown": (*View).HalfPageDown,
78 "StartOfLine": (*View).StartOfLine,
79 "EndOfLine": (*View).EndOfLine,
80 "ToggleHelp": (*View).ToggleHelp,
81 "ToggleRuler": (*View).ToggleRuler,
82 "JumpLine": (*View).JumpLine,
83 "ClearStatus": (*View).ClearStatus,
84 "ShellMode": (*View).ShellMode,
85 "CommandMode": (*View).CommandMode,
86 "Escape": (*View).Escape,
88 "QuitAll": (*View).QuitAll,
89 "AddTab": (*View).AddTab,
90 "PreviousTab": (*View).PreviousTab,
91 "NextTab": (*View).NextTab,
92 "NextSplit": (*View).NextSplit,
93 "PreviousSplit": (*View).PreviousSplit,
94 "Unsplit": (*View).Unsplit,
95 "VSplit": (*View).VSplitBinding,
96 "HSplit": (*View).HSplitBinding,
97 "ToggleMacro": (*View).ToggleMacro,
98 "PlayMacro": (*View).PlayMacro,
99 "Suspend": (*View).Suspend,
100 "ScrollUp": (*View).ScrollUpAction,
101 "ScrollDown": (*View).ScrollDownAction,
102 "SpawnMultiCursor": (*View).SpawnMultiCursor,
103 "RemoveMultiCursor": (*View).RemoveMultiCursor,
104 "RemoveAllMultiCursors": (*View).RemoveAllMultiCursors,
105 "SkipMultiCursor": (*View).SkipMultiCursor,
107 // This was changed to InsertNewline but I don't want to break backwards compatibility
108 "InsertEnter": (*View).InsertNewline,
111 var bindingMouse = map[string]tcell.ButtonMask{
112 "MouseLeft": tcell.Button1,
113 "MouseMiddle": tcell.Button2,
114 "MouseRight": tcell.Button3,
115 "MouseWheelUp": tcell.WheelUp,
116 "MouseWheelDown": tcell.WheelDown,
117 "MouseWheelLeft": tcell.WheelLeft,
118 "MouseWheelRight": tcell.WheelRight,
121 var bindingKeys = map[string]tcell.Key{
123 "Down": tcell.KeyDown,
124 "Right": tcell.KeyRight,
125 "Left": tcell.KeyLeft,
126 "UpLeft": tcell.KeyUpLeft,
127 "UpRight": tcell.KeyUpRight,
128 "DownLeft": tcell.KeyDownLeft,
129 "DownRight": tcell.KeyDownRight,
130 "Center": tcell.KeyCenter,
131 "PageUp": tcell.KeyPgUp,
132 "PageDown": tcell.KeyPgDn,
133 "Home": tcell.KeyHome,
135 "Insert": tcell.KeyInsert,
136 "Delete": tcell.KeyDelete,
137 "Help": tcell.KeyHelp,
138 "Exit": tcell.KeyExit,
139 "Clear": tcell.KeyClear,
140 "Cancel": tcell.KeyCancel,
141 "Print": tcell.KeyPrint,
142 "Pause": tcell.KeyPause,
143 "Backtab": tcell.KeyBacktab,
208 "CtrlSpace": tcell.KeyCtrlSpace,
209 "CtrlA": tcell.KeyCtrlA,
210 "CtrlB": tcell.KeyCtrlB,
211 "CtrlC": tcell.KeyCtrlC,
212 "CtrlD": tcell.KeyCtrlD,
213 "CtrlE": tcell.KeyCtrlE,
214 "CtrlF": tcell.KeyCtrlF,
215 "CtrlG": tcell.KeyCtrlG,
216 "CtrlH": tcell.KeyCtrlH,
217 "CtrlI": tcell.KeyCtrlI,
218 "CtrlJ": tcell.KeyCtrlJ,
219 "CtrlK": tcell.KeyCtrlK,
220 "CtrlL": tcell.KeyCtrlL,
221 "CtrlM": tcell.KeyCtrlM,
222 "CtrlN": tcell.KeyCtrlN,
223 "CtrlO": tcell.KeyCtrlO,
224 "CtrlP": tcell.KeyCtrlP,
225 "CtrlQ": tcell.KeyCtrlQ,
226 "CtrlR": tcell.KeyCtrlR,
227 "CtrlS": tcell.KeyCtrlS,
228 "CtrlT": tcell.KeyCtrlT,
229 "CtrlU": tcell.KeyCtrlU,
230 "CtrlV": tcell.KeyCtrlV,
231 "CtrlW": tcell.KeyCtrlW,
232 "CtrlX": tcell.KeyCtrlX,
233 "CtrlY": tcell.KeyCtrlY,
234 "CtrlZ": tcell.KeyCtrlZ,
235 "CtrlLeftSq": tcell.KeyCtrlLeftSq,
236 "CtrlBackslash": tcell.KeyCtrlBackslash,
237 "CtrlRightSq": tcell.KeyCtrlRightSq,
238 "CtrlCarat": tcell.KeyCtrlCarat,
239 "CtrlUnderscore": tcell.KeyCtrlUnderscore,
242 "Escape": tcell.KeyEscape,
243 "Enter": tcell.KeyEnter,
244 "Backspace": tcell.KeyBackspace2,
246 // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
247 "PgUp": tcell.KeyPgUp,
248 "PgDown": tcell.KeyPgDn,
251 // The Key struct holds the data for a keypress (keycode + modifiers)
254 modifiers tcell.ModMask
255 buttons tcell.ButtonMask
259 // InitBindings initializes the keybindings for micro
260 func InitBindings() {
261 bindings = make(map[Key][]func(*View, bool) bool)
262 mouseBindings = make(map[Key][]func(*View, bool, *tcell.EventMouse) bool)
264 var parsed map[string]string
265 defaults := DefaultBindings()
267 filename := configDir + "/bindings.json"
268 if _, e := os.Stat(filename); e == nil {
269 input, err := ioutil.ReadFile(filename)
271 TermMessage("Error reading bindings.json file: " + err.Error())
275 err = json5.Unmarshal(input, &parsed)
277 TermMessage("Error reading bindings.json:", err.Error())
281 parseBindings(defaults)
282 parseBindings(parsed)
285 func parseBindings(userBindings map[string]string) {
286 for k, v := range userBindings {
291 // findKey will find binding Key 'b' using string 'k'
292 func findKey(k string) (b Key, ok bool) {
293 modifiers := tcell.ModNone
295 // First, we'll strip off all the modifiers in the name and add them to the
300 case strings.HasPrefix(k, "-"):
301 // We optionally support dashes between modifiers
303 case strings.HasPrefix(k, "Ctrl") && k != "CtrlH":
304 // CtrlH technically does not have a 'Ctrl' modifier because it is really backspace
306 modifiers |= tcell.ModCtrl
307 case strings.HasPrefix(k, "Alt"):
309 modifiers |= tcell.ModAlt
310 case strings.HasPrefix(k, "Shift"):
312 modifiers |= tcell.ModShift
318 // Control is handled specially, since some character codes in bindingKeys
319 // are different when Control is depressed. We should check for Control keys
321 if modifiers&tcell.ModCtrl != 0 {
322 // see if the key is in bindingKeys with the Ctrl prefix.
323 if code, ok := bindingKeys["Ctrl"+k]; ok {
324 // It is, we're done.
327 modifiers: modifiers,
334 // See if we can find the key in bindingKeys
335 if code, ok := bindingKeys[k]; ok {
338 modifiers: modifiers,
344 // See if we can find the key in bindingMouse
345 if code, ok := bindingMouse[k]; ok {
347 modifiers: modifiers,
353 // If we were given one character, then we've got a rune.
356 keyCode: tcell.KeyRune,
357 modifiers: modifiers,
363 // We don't know what happened.
364 return Key{buttons: -1}, false
367 // findAction will find 'action' using string 'v'
368 func findAction(v string) (action func(*View, bool) bool) {
369 action, ok := bindingActions[v]
371 // If the user seems to be binding a function that doesn't exist
372 // We hope that it's a lua function that exists and bind it to that
373 action = LuaFunctionBinding(v)
378 func findMouseAction(v string) func(*View, bool, *tcell.EventMouse) bool {
379 action, ok := mouseBindingActions[v]
381 // If the user seems to be binding a function that doesn't exist
382 // We hope that it's a lua function that exists and bind it to that
383 action = LuaFunctionMouseBinding(v)
388 // BindKey takes a key and an action and binds the two together
389 func BindKey(k, v string) {
390 key, ok := findKey(k)
392 TermMessage("Unknown keybinding: " + k)
395 if v == "ToggleHelp" {
398 if helpBinding == k && v != "ToggleHelp" {
402 actionNames := strings.Split(v, ",")
403 if actionNames[0] == "UnbindKey" {
404 delete(bindings, key)
405 delete(mouseBindings, key)
406 if len(actionNames) == 1 {
409 actionNames = append(actionNames[:0], actionNames[1:]...)
411 actions := make([]func(*View, bool) bool, 0, len(actionNames))
412 mouseActions := make([]func(*View, bool, *tcell.EventMouse) bool, 0, len(actionNames))
413 for _, actionName := range actionNames {
414 if strings.HasPrefix(actionName, "Mouse") {
415 mouseActions = append(mouseActions, findMouseAction(actionName))
417 actions = append(actions, findAction(actionName))
421 if len(actions) > 0 {
422 // Can't have a binding be both mouse and normal
423 delete(mouseBindings, key)
424 bindings[key] = actions
425 } else if len(mouseActions) > 0 {
426 // Can't have a binding be both mouse and normal
427 delete(bindings, key)
428 mouseBindings[key] = mouseActions
432 // DefaultBindings returns a map containing micro's default keybindings
433 func DefaultBindings() map[string]string {
434 return map[string]string{
436 "Down": "CursorDown",
437 "Right": "CursorRight",
438 "Left": "CursorLeft",
439 "ShiftUp": "SelectUp",
440 "ShiftDown": "SelectDown",
441 "ShiftLeft": "SelectLeft",
442 "ShiftRight": "SelectRight",
443 "AltLeft": "WordLeft",
444 "AltRight": "WordRight",
445 "AltUp": "MoveLinesUp",
446 "AltDown": "MoveLinesDown",
447 "AltShiftRight": "SelectWordRight",
448 "AltShiftLeft": "SelectWordLeft",
449 "CtrlLeft": "StartOfLine",
450 "CtrlRight": "EndOfLine",
451 "CtrlShiftLeft": "SelectToStartOfLine",
452 "ShiftHome": "SelectToStartOfLine",
453 "CtrlShiftRight": "SelectToEndOfLine",
454 "ShiftEnd": "SelectToEndOfLine",
455 "CtrlUp": "CursorStart",
456 "CtrlDown": "CursorEnd",
457 "CtrlShiftUp": "SelectToStart",
458 "CtrlShiftDown": "SelectToEnd",
459 "Enter": "InsertNewline",
460 "CtrlH": "Backspace",
461 "Backspace": "Backspace",
462 "Alt-CtrlH": "DeleteWordLeft",
463 "Alt-Backspace": "DeleteWordLeft",
464 "Tab": "IndentSelection,InsertTab",
465 "Backtab": "OutdentSelection,OutdentLine",
470 "CtrlP": "FindPrevious",
476 "CtrlD": "DuplicateLine",
478 "CtrlA": "SelectAll",
480 "Alt,": "PreviousTab",
482 "Home": "StartOfLine",
484 "CtrlHome": "CursorStart",
485 "CtrlEnd": "CursorEnd",
486 "PageUp": "CursorPageUp",
487 "PageDown": "CursorPageDown",
488 "CtrlG": "ToggleHelp",
489 "CtrlR": "ToggleRuler",
492 "CtrlB": "ShellMode",
494 "CtrlE": "CommandMode",
495 "CtrlW": "NextSplit",
496 "CtrlU": "ToggleMacro",
497 "CtrlJ": "PlayMacro",
499 // Emacs-style keybindings
500 "Alt-f": "WordRight",
502 "Alt-a": "StartOfLine",
503 "Alt-e": "EndOfLine",
504 // "Alt-p": "CursorUp",
505 // "Alt-n": "CursorDown",
507 // Integration with file managers
517 "MouseWheelUp": "ScrollUp",
518 "MouseWheelDown": "ScrollDown",
519 "MouseLeft": "MousePress",
520 "MouseMiddle": "PastePrimary",
521 "Ctrl-MouseLeft": "MouseMultiCursor",
523 "Alt-n": "SpawnMultiCursor",
524 "Alt-p": "RemoveMultiCursor",
525 "Alt-c": "RemoveAllMultiCursors",
526 "Alt-x": "SkipMultiCursor",