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,
240 "CtrlPageUp": tcell.KeyCtrlPgUp,
241 "CtrlPageDown": tcell.KeyCtrlPgDn,
244 "Escape": tcell.KeyEscape,
245 "Enter": tcell.KeyEnter,
246 "Backspace": tcell.KeyBackspace2,
248 // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
249 "PgUp": tcell.KeyPgUp,
250 "PgDown": tcell.KeyPgDn,
253 // The Key struct holds the data for a keypress (keycode + modifiers)
256 modifiers tcell.ModMask
257 buttons tcell.ButtonMask
261 // InitBindings initializes the keybindings for micro
262 func InitBindings() {
263 bindings = make(map[Key][]func(*View, bool) bool)
264 mouseBindings = make(map[Key][]func(*View, bool, *tcell.EventMouse) bool)
266 var parsed map[string]string
267 defaults := DefaultBindings()
269 filename := configDir + "/bindings.json"
270 if _, e := os.Stat(filename); e == nil {
271 input, err := ioutil.ReadFile(filename)
273 TermMessage("Error reading bindings.json file: " + err.Error())
277 err = json5.Unmarshal(input, &parsed)
279 TermMessage("Error reading bindings.json:", err.Error())
283 parseBindings(defaults)
284 parseBindings(parsed)
287 func parseBindings(userBindings map[string]string) {
288 for k, v := range userBindings {
293 // findKey will find binding Key 'b' using string 'k'
294 func findKey(k string) (b Key, ok bool) {
295 modifiers := tcell.ModNone
297 // First, we'll strip off all the modifiers in the name and add them to the
302 case strings.HasPrefix(k, "-"):
303 // We optionally support dashes between modifiers
305 case strings.HasPrefix(k, "Ctrl") && k != "CtrlH":
306 // CtrlH technically does not have a 'Ctrl' modifier because it is really backspace
308 modifiers |= tcell.ModCtrl
309 case strings.HasPrefix(k, "Alt"):
311 modifiers |= tcell.ModAlt
312 case strings.HasPrefix(k, "Shift"):
314 modifiers |= tcell.ModShift
320 // Control is handled specially, since some character codes in bindingKeys
321 // are different when Control is depressed. We should check for Control keys
323 if modifiers&tcell.ModCtrl != 0 {
324 // see if the key is in bindingKeys with the Ctrl prefix.
325 if code, ok := bindingKeys["Ctrl"+k]; ok {
326 // It is, we're done.
329 modifiers: modifiers,
336 // See if we can find the key in bindingKeys
337 if code, ok := bindingKeys[k]; ok {
340 modifiers: modifiers,
346 // See if we can find the key in bindingMouse
347 if code, ok := bindingMouse[k]; ok {
349 modifiers: modifiers,
355 // If we were given one character, then we've got a rune.
358 keyCode: tcell.KeyRune,
359 modifiers: modifiers,
365 // We don't know what happened.
366 return Key{buttons: -1}, false
369 // findAction will find 'action' using string 'v'
370 func findAction(v string) (action func(*View, bool) bool) {
371 action, ok := bindingActions[v]
373 // If the user seems to be binding a function that doesn't exist
374 // We hope that it's a lua function that exists and bind it to that
375 action = LuaFunctionBinding(v)
380 func findMouseAction(v string) func(*View, bool, *tcell.EventMouse) bool {
381 action, ok := mouseBindingActions[v]
383 // If the user seems to be binding a function that doesn't exist
384 // We hope that it's a lua function that exists and bind it to that
385 action = LuaFunctionMouseBinding(v)
390 // BindKey takes a key and an action and binds the two together
391 func BindKey(k, v string) {
392 key, ok := findKey(k)
394 TermMessage("Unknown keybinding: " + k)
397 if v == "ToggleHelp" {
400 if helpBinding == k && v != "ToggleHelp" {
404 actionNames := strings.Split(v, ",")
405 if actionNames[0] == "UnbindKey" {
406 delete(bindings, key)
407 delete(mouseBindings, key)
408 if len(actionNames) == 1 {
411 actionNames = append(actionNames[:0], actionNames[1:]...)
413 actions := make([]func(*View, bool) bool, 0, len(actionNames))
414 mouseActions := make([]func(*View, bool, *tcell.EventMouse) bool, 0, len(actionNames))
415 for _, actionName := range actionNames {
416 if strings.HasPrefix(actionName, "Mouse") {
417 mouseActions = append(mouseActions, findMouseAction(actionName))
419 actions = append(actions, findAction(actionName))
423 if len(actions) > 0 {
424 // Can't have a binding be both mouse and normal
425 delete(mouseBindings, key)
426 bindings[key] = actions
427 } else if len(mouseActions) > 0 {
428 // Can't have a binding be both mouse and normal
429 delete(bindings, key)
430 mouseBindings[key] = mouseActions
434 // DefaultBindings returns a map containing micro's default keybindings
435 func DefaultBindings() map[string]string {
436 return map[string]string{
438 "Down": "CursorDown",
439 "Right": "CursorRight",
440 "Left": "CursorLeft",
441 "ShiftUp": "SelectUp",
442 "ShiftDown": "SelectDown",
443 "ShiftLeft": "SelectLeft",
444 "ShiftRight": "SelectRight",
445 "AltLeft": "WordLeft",
446 "AltRight": "WordRight",
447 "AltUp": "MoveLinesUp",
448 "AltDown": "MoveLinesDown",
449 "AltShiftRight": "SelectWordRight",
450 "AltShiftLeft": "SelectWordLeft",
451 "CtrlLeft": "StartOfLine",
452 "CtrlRight": "EndOfLine",
453 "CtrlShiftLeft": "SelectToStartOfLine",
454 "ShiftHome": "SelectToStartOfLine",
455 "CtrlShiftRight": "SelectToEndOfLine",
456 "ShiftEnd": "SelectToEndOfLine",
457 "CtrlUp": "CursorStart",
458 "CtrlDown": "CursorEnd",
459 "CtrlShiftUp": "SelectToStart",
460 "CtrlShiftDown": "SelectToEnd",
461 "Enter": "InsertNewline",
462 "CtrlH": "Backspace",
463 "Backspace": "Backspace",
464 "Alt-CtrlH": "DeleteWordLeft",
465 "Alt-Backspace": "DeleteWordLeft",
466 "Tab": "IndentSelection,InsertTab",
467 "Backtab": "OutdentSelection,OutdentLine",
472 "CtrlP": "FindPrevious",
478 "CtrlD": "DuplicateLine",
480 "CtrlA": "SelectAll",
482 "Alt,": "PreviousTab",
484 "Home": "StartOfLine",
486 "CtrlHome": "CursorStart",
487 "CtrlEnd": "CursorEnd",
488 "PageUp": "CursorPageUp",
489 "PageDown": "CursorPageDown",
490 "CtrlPageUp": "PreviousTab",
491 "CtrlPageDown": "NextTab",
492 "CtrlG": "ToggleHelp",
493 "CtrlR": "ToggleRuler",
496 "CtrlB": "ShellMode",
498 "CtrlE": "CommandMode",
499 "CtrlW": "NextSplit",
500 "CtrlU": "ToggleMacro",
501 "CtrlJ": "PlayMacro",
503 // Emacs-style keybindings
504 "Alt-f": "WordRight",
506 "Alt-a": "StartOfLine",
507 "Alt-e": "EndOfLine",
508 // "Alt-p": "CursorUp",
509 // "Alt-n": "CursorDown",
511 // Integration with file managers
521 "MouseWheelUp": "ScrollUp",
522 "MouseWheelDown": "ScrollDown",
523 "MouseLeft": "MousePress",
524 "MouseMiddle": "PastePrimary",
525 "Ctrl-MouseLeft": "MouseMultiCursor",
527 "Alt-n": "SpawnMultiCursor",
528 "Alt-p": "RemoveMultiCursor",
529 "Alt-c": "RemoveAllMultiCursors",
530 "Alt-x": "SkipMultiCursor",