8 "github.com/zyedidia/json5/encoding/json5"
9 "github.com/zyedidia/tcell"
12 var bindings map[Key][]func(*View, bool) bool
13 var helpBinding string
15 var bindingActions = map[string]func(*View, bool) bool{
16 "CursorUp": (*View).CursorUp,
17 "CursorDown": (*View).CursorDown,
18 "CursorPageUp": (*View).CursorPageUp,
19 "CursorPageDown": (*View).CursorPageDown,
20 "CursorLeft": (*View).CursorLeft,
21 "CursorRight": (*View).CursorRight,
22 "CursorStart": (*View).CursorStart,
23 "CursorEnd": (*View).CursorEnd,
24 "SelectToStart": (*View).SelectToStart,
25 "SelectToEnd": (*View).SelectToEnd,
26 "SelectUp": (*View).SelectUp,
27 "SelectDown": (*View).SelectDown,
28 "SelectLeft": (*View).SelectLeft,
29 "SelectRight": (*View).SelectRight,
30 "WordRight": (*View).WordRight,
31 "WordLeft": (*View).WordLeft,
32 "SelectWordRight": (*View).SelectWordRight,
33 "SelectWordLeft": (*View).SelectWordLeft,
34 "DeleteWordRight": (*View).DeleteWordRight,
35 "DeleteWordLeft": (*View).DeleteWordLeft,
36 "SelectToStartOfLine": (*View).SelectToStartOfLine,
37 "SelectToEndOfLine": (*View).SelectToEndOfLine,
38 "InsertNewline": (*View).InsertNewline,
39 "InsertSpace": (*View).InsertSpace,
40 "Backspace": (*View).Backspace,
41 "Delete": (*View).Delete,
42 "InsertTab": (*View).InsertTab,
45 "FindNext": (*View).FindNext,
46 "FindPrevious": (*View).FindPrevious,
47 "Center": (*View).Center,
52 "CutLine": (*View).CutLine,
53 "DuplicateLine": (*View).DuplicateLine,
54 "DeleteLine": (*View).DeleteLine,
55 "MoveLinesUp": (*View).MoveLinesUp,
56 "MoveLinesDown": (*View).MoveLinesDown,
57 "IndentSelection": (*View).IndentSelection,
58 "OutdentSelection": (*View).OutdentSelection,
59 "OutdentLine": (*View).OutdentLine,
60 "Paste": (*View).Paste,
61 "PastePrimary": (*View).PastePrimary,
62 "SelectAll": (*View).SelectAll,
63 "OpenFile": (*View).OpenFile,
64 "Start": (*View).Start,
66 "PageUp": (*View).PageUp,
67 "PageDown": (*View).PageDown,
68 "HalfPageUp": (*View).HalfPageUp,
69 "HalfPageDown": (*View).HalfPageDown,
70 "StartOfLine": (*View).StartOfLine,
71 "EndOfLine": (*View).EndOfLine,
72 "ToggleHelp": (*View).ToggleHelp,
73 "ToggleRuler": (*View).ToggleRuler,
74 "JumpLine": (*View).JumpLine,
75 "ClearStatus": (*View).ClearStatus,
76 "ShellMode": (*View).ShellMode,
77 "CommandMode": (*View).CommandMode,
78 "Escape": (*View).Escape,
80 "QuitAll": (*View).QuitAll,
81 "AddTab": (*View).AddTab,
82 "PreviousTab": (*View).PreviousTab,
83 "NextTab": (*View).NextTab,
84 "NextSplit": (*View).NextSplit,
85 "PreviousSplit": (*View).PreviousSplit,
86 "Unsplit": (*View).Unsplit,
87 "VSplit": (*View).VSplitBinding,
88 "HSplit": (*View).HSplitBinding,
89 "ToggleMacro": (*View).ToggleMacro,
90 "PlayMacro": (*View).PlayMacro,
92 // This was changed to InsertNewline but I don't want to break backwards compatibility
93 "InsertEnter": (*View).InsertNewline,
96 var bindingKeys = map[string]tcell.Key{
98 "Down": tcell.KeyDown,
99 "Right": tcell.KeyRight,
100 "Left": tcell.KeyLeft,
101 "UpLeft": tcell.KeyUpLeft,
102 "UpRight": tcell.KeyUpRight,
103 "DownLeft": tcell.KeyDownLeft,
104 "DownRight": tcell.KeyDownRight,
105 "Center": tcell.KeyCenter,
106 "PageUp": tcell.KeyPgUp,
107 "PageDown": tcell.KeyPgDn,
108 "Home": tcell.KeyHome,
110 "Insert": tcell.KeyInsert,
111 "Delete": tcell.KeyDelete,
112 "Help": tcell.KeyHelp,
113 "Exit": tcell.KeyExit,
114 "Clear": tcell.KeyClear,
115 "Cancel": tcell.KeyCancel,
116 "Print": tcell.KeyPrint,
117 "Pause": tcell.KeyPause,
118 "Backtab": tcell.KeyBacktab,
183 "CtrlSpace": tcell.KeyCtrlSpace,
184 "CtrlA": tcell.KeyCtrlA,
185 "CtrlB": tcell.KeyCtrlB,
186 "CtrlC": tcell.KeyCtrlC,
187 "CtrlD": tcell.KeyCtrlD,
188 "CtrlE": tcell.KeyCtrlE,
189 "CtrlF": tcell.KeyCtrlF,
190 "CtrlG": tcell.KeyCtrlG,
191 "CtrlH": tcell.KeyCtrlH,
192 "CtrlI": tcell.KeyCtrlI,
193 "CtrlJ": tcell.KeyCtrlJ,
194 "CtrlK": tcell.KeyCtrlK,
195 "CtrlL": tcell.KeyCtrlL,
196 "CtrlM": tcell.KeyCtrlM,
197 "CtrlN": tcell.KeyCtrlN,
198 "CtrlO": tcell.KeyCtrlO,
199 "CtrlP": tcell.KeyCtrlP,
200 "CtrlQ": tcell.KeyCtrlQ,
201 "CtrlR": tcell.KeyCtrlR,
202 "CtrlS": tcell.KeyCtrlS,
203 "CtrlT": tcell.KeyCtrlT,
204 "CtrlU": tcell.KeyCtrlU,
205 "CtrlV": tcell.KeyCtrlV,
206 "CtrlW": tcell.KeyCtrlW,
207 "CtrlX": tcell.KeyCtrlX,
208 "CtrlY": tcell.KeyCtrlY,
209 "CtrlZ": tcell.KeyCtrlZ,
210 "CtrlLeftSq": tcell.KeyCtrlLeftSq,
211 "CtrlBackslash": tcell.KeyCtrlBackslash,
212 "CtrlRightSq": tcell.KeyCtrlRightSq,
213 "CtrlCarat": tcell.KeyCtrlCarat,
214 "CtrlUnderscore": tcell.KeyCtrlUnderscore,
217 "Escape": tcell.KeyEscape,
218 "Enter": tcell.KeyEnter,
219 "Backspace": tcell.KeyBackspace2,
221 // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
222 "PgUp": tcell.KeyPgUp,
223 "PgDown": tcell.KeyPgDn,
226 // The Key struct holds the data for a keypress (keycode + modifiers)
229 modifiers tcell.ModMask
233 // InitBindings initializes the keybindings for micro
234 func InitBindings() {
235 bindings = make(map[Key][]func(*View, bool) bool)
237 var parsed map[string]string
238 defaults := DefaultBindings()
240 filename := configDir + "/bindings.json"
241 if _, e := os.Stat(filename); e == nil {
242 input, err := ioutil.ReadFile(filename)
244 TermMessage("Error reading bindings.json file: " + err.Error())
248 err = json5.Unmarshal(input, &parsed)
250 TermMessage("Error reading bindings.json:", err.Error())
254 parseBindings(defaults)
255 parseBindings(parsed)
258 func parseBindings(userBindings map[string]string) {
259 for k, v := range userBindings {
264 // findKey will find binding Key 'b' using string 'k'
265 func findKey(k string) (b Key, ok bool) {
266 modifiers := tcell.ModNone
268 // First, we'll strip off all the modifiers in the name and add them to the
273 case strings.HasPrefix(k, "-"):
274 // We optionally support dashes between modifiers
276 case strings.HasPrefix(k, "Ctrl") && k != "CtrlH":
277 // CtrlH technically does not have a 'Ctrl' modifier because it is really backspace
279 modifiers |= tcell.ModCtrl
280 case strings.HasPrefix(k, "Alt"):
282 modifiers |= tcell.ModAlt
283 case strings.HasPrefix(k, "Shift"):
285 modifiers |= tcell.ModShift
291 // Control is handled specially, since some character codes in bindingKeys
292 // are different when Control is depressed. We should check for Control keys
294 if modifiers&tcell.ModCtrl != 0 {
295 // see if the key is in bindingKeys with the Ctrl prefix.
296 if code, ok := bindingKeys["Ctrl"+k]; ok {
297 // It is, we're done.
300 modifiers: modifiers,
306 // See if we can find the key in bindingKeys
307 if code, ok := bindingKeys[k]; ok {
310 modifiers: modifiers,
315 // If we were given one character, then we've got a rune.
318 keyCode: tcell.KeyRune,
319 modifiers: modifiers,
324 // We don't know what happened.
328 // findAction will find 'action' using string 'v'
329 func findAction(v string) (action func(*View, bool) bool) {
330 action, ok := bindingActions[v]
332 // If the user seems to be binding a function that doesn't exist
333 // We hope that it's a lua function that exists and bind it to that
334 action = LuaFunctionBinding(v)
339 // BindKey takes a key and an action and binds the two together
340 func BindKey(k, v string) {
341 key, ok := findKey(k)
343 TermMessage("Unknown keybinding: " + k)
346 if v == "ToggleHelp" {
350 actionNames := strings.Split(v, ",")
351 actions := make([]func(*View, bool) bool, 0, len(actionNames))
352 for _, actionName := range actionNames {
353 actions = append(actions, findAction(actionName))
356 bindings[key] = actions
359 // DefaultBindings returns a map containing micro's default keybindings
360 func DefaultBindings() map[string]string {
361 return map[string]string{
363 "Down": "CursorDown",
364 "Right": "CursorRight",
365 "Left": "CursorLeft",
366 "ShiftUp": "SelectUp",
367 "ShiftDown": "SelectDown",
368 "ShiftLeft": "SelectLeft",
369 "ShiftRight": "SelectRight",
370 "AltLeft": "WordLeft",
371 "AltRight": "WordRight",
372 "AltUp": "MoveLinesUp",
373 "AltDown": "MoveLinesDown",
374 "AltShiftRight": "SelectWordRight",
375 "AltShiftLeft": "SelectWordLeft",
376 "CtrlLeft": "StartOfLine",
377 "CtrlRight": "EndOfLine",
378 "CtrlShiftLeft": "SelectToStartOfLine",
379 "CtrlShiftRight": "SelectToEndOfLine",
380 "CtrlUp": "CursorStart",
381 "CtrlDown": "CursorEnd",
382 "CtrlShiftUp": "SelectToStart",
383 "CtrlShiftDown": "SelectToEnd",
384 "Enter": "InsertNewline",
385 "CtrlH": "Backspace",
386 "Backspace": "Backspace",
387 "Alt-CtrlH": "DeleteWordLeft",
388 "Alt-Backspace": "DeleteWordLeft",
389 "Tab": "IndentSelection,InsertTab",
390 "Backtab": "OutdentSelection,OutdentLine",
395 "CtrlP": "FindPrevious",
401 "CtrlD": "DuplicateLine",
403 "CtrlA": "SelectAll",
405 "CtrlRightSq": "PreviousTab",
406 "CtrlBackslash": "NextTab",
407 "Home": "StartOfLine",
409 "CtrlHome": "CursorStart",
410 "CtrlEnd": "CursorEnd",
411 "PageUp": "CursorPageUp",
412 "PageDown": "CursorPageDown",
413 "CtrlG": "ToggleHelp",
414 "CtrlR": "ToggleRuler",
417 "CtrlB": "ShellMode",
419 "CtrlE": "CommandMode",
420 "CtrlW": "NextSplit",
421 "CtrlU": "ToggleMacro",
422 "CtrlJ": "PlayMacro",
424 // Emacs-style keybindings
425 "Alt-f": "WordRight",
427 "Alt-a": "StartOfLine",
428 "Alt-e": "EndOfLine",
430 "Alt-n": "CursorDown",
432 // Integration with file managers