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 "Paste": (*View).Paste,
60 "PastePrimary": (*View).PastePrimary,
61 "SelectAll": (*View).SelectAll,
62 "OpenFile": (*View).OpenFile,
63 "Start": (*View).Start,
65 "PageUp": (*View).PageUp,
66 "PageDown": (*View).PageDown,
67 "HalfPageUp": (*View).HalfPageUp,
68 "HalfPageDown": (*View).HalfPageDown,
69 "StartOfLine": (*View).StartOfLine,
70 "EndOfLine": (*View).EndOfLine,
71 "ToggleHelp": (*View).ToggleHelp,
72 "ToggleRuler": (*View).ToggleRuler,
73 "JumpLine": (*View).JumpLine,
74 "ClearStatus": (*View).ClearStatus,
75 "ShellMode": (*View).ShellMode,
76 "CommandMode": (*View).CommandMode,
77 "Escape": (*View).Escape,
79 "QuitAll": (*View).QuitAll,
80 "AddTab": (*View).AddTab,
81 "PreviousTab": (*View).PreviousTab,
82 "NextTab": (*View).NextTab,
83 "NextSplit": (*View).NextSplit,
84 "PreviousSplit": (*View).PreviousSplit,
85 "Unsplit": (*View).Unsplit,
86 "VSplit": (*View).VSplitBinding,
87 "HSplit": (*View).HSplitBinding,
88 "ToggleMacro": (*View).ToggleMacro,
89 "PlayMacro": (*View).PlayMacro,
91 // This was changed to InsertNewline but I don't want to break backwards compatibility
92 "InsertEnter": (*View).InsertNewline,
95 var bindingKeys = map[string]tcell.Key{
97 "Down": tcell.KeyDown,
98 "Right": tcell.KeyRight,
99 "Left": tcell.KeyLeft,
100 "UpLeft": tcell.KeyUpLeft,
101 "UpRight": tcell.KeyUpRight,
102 "DownLeft": tcell.KeyDownLeft,
103 "DownRight": tcell.KeyDownRight,
104 "Center": tcell.KeyCenter,
105 "PageUp": tcell.KeyPgUp,
106 "PageDown": tcell.KeyPgDn,
107 "Home": tcell.KeyHome,
109 "Insert": tcell.KeyInsert,
110 "Delete": tcell.KeyDelete,
111 "Help": tcell.KeyHelp,
112 "Exit": tcell.KeyExit,
113 "Clear": tcell.KeyClear,
114 "Cancel": tcell.KeyCancel,
115 "Print": tcell.KeyPrint,
116 "Pause": tcell.KeyPause,
117 "Backtab": tcell.KeyBacktab,
182 "CtrlSpace": tcell.KeyCtrlSpace,
183 "CtrlA": tcell.KeyCtrlA,
184 "CtrlB": tcell.KeyCtrlB,
185 "CtrlC": tcell.KeyCtrlC,
186 "CtrlD": tcell.KeyCtrlD,
187 "CtrlE": tcell.KeyCtrlE,
188 "CtrlF": tcell.KeyCtrlF,
189 "CtrlG": tcell.KeyCtrlG,
190 "CtrlH": tcell.KeyCtrlH,
191 "CtrlI": tcell.KeyCtrlI,
192 "CtrlJ": tcell.KeyCtrlJ,
193 "CtrlK": tcell.KeyCtrlK,
194 "CtrlL": tcell.KeyCtrlL,
195 "CtrlM": tcell.KeyCtrlM,
196 "CtrlN": tcell.KeyCtrlN,
197 "CtrlO": tcell.KeyCtrlO,
198 "CtrlP": tcell.KeyCtrlP,
199 "CtrlQ": tcell.KeyCtrlQ,
200 "CtrlR": tcell.KeyCtrlR,
201 "CtrlS": tcell.KeyCtrlS,
202 "CtrlT": tcell.KeyCtrlT,
203 "CtrlU": tcell.KeyCtrlU,
204 "CtrlV": tcell.KeyCtrlV,
205 "CtrlW": tcell.KeyCtrlW,
206 "CtrlX": tcell.KeyCtrlX,
207 "CtrlY": tcell.KeyCtrlY,
208 "CtrlZ": tcell.KeyCtrlZ,
209 "CtrlLeftSq": tcell.KeyCtrlLeftSq,
210 "CtrlBackslash": tcell.KeyCtrlBackslash,
211 "CtrlRightSq": tcell.KeyCtrlRightSq,
212 "CtrlCarat": tcell.KeyCtrlCarat,
213 "CtrlUnderscore": tcell.KeyCtrlUnderscore,
216 "Escape": tcell.KeyEscape,
217 "Enter": tcell.KeyEnter,
218 "Backspace": tcell.KeyBackspace2,
220 // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
221 "PgUp": tcell.KeyPgUp,
222 "PgDown": tcell.KeyPgDn,
225 // The Key struct holds the data for a keypress (keycode + modifiers)
228 modifiers tcell.ModMask
232 // InitBindings initializes the keybindings for micro
233 func InitBindings() {
234 bindings = make(map[Key][]func(*View, bool) bool)
236 var parsed map[string]string
237 defaults := DefaultBindings()
239 filename := configDir + "/bindings.json"
240 if _, e := os.Stat(filename); e == nil {
241 input, err := ioutil.ReadFile(filename)
243 TermMessage("Error reading bindings.json file: " + err.Error())
247 err = json5.Unmarshal(input, &parsed)
249 TermMessage("Error reading bindings.json:", err.Error())
253 parseBindings(defaults)
254 parseBindings(parsed)
257 func parseBindings(userBindings map[string]string) {
258 for k, v := range userBindings {
263 // findKey will find binding Key 'b' using string 'k'
264 func findKey(k string) (b Key, ok bool) {
265 modifiers := tcell.ModNone
267 // First, we'll strip off all the modifiers in the name and add them to the
272 case strings.HasPrefix(k, "-"):
273 // We optionally support dashes between modifiers
275 case strings.HasPrefix(k, "Ctrl") && k != "CtrlH":
276 // CtrlH technically does not have a 'Ctrl' modifier because it is really backspace
278 modifiers |= tcell.ModCtrl
279 case strings.HasPrefix(k, "Alt"):
281 modifiers |= tcell.ModAlt
282 case strings.HasPrefix(k, "Shift"):
284 modifiers |= tcell.ModShift
290 // Control is handled specially, since some character codes in bindingKeys
291 // are different when Control is depressed. We should check for Control keys
293 if modifiers&tcell.ModCtrl != 0 {
294 // see if the key is in bindingKeys with the Ctrl prefix.
295 if code, ok := bindingKeys["Ctrl"+k]; ok {
296 // It is, we're done.
299 modifiers: modifiers,
305 // See if we can find the key in bindingKeys
306 if code, ok := bindingKeys[k]; ok {
309 modifiers: modifiers,
314 // If we were given one character, then we've got a rune.
317 keyCode: tcell.KeyRune,
318 modifiers: modifiers,
323 // We don't know what happened.
327 // findAction will find 'action' using string 'v'
328 func findAction(v string) (action func(*View, bool) bool) {
329 action, ok := bindingActions[v]
331 // If the user seems to be binding a function that doesn't exist
332 // We hope that it's a lua function that exists and bind it to that
333 action = LuaFunctionBinding(v)
338 // BindKey takes a key and an action and binds the two together
339 func BindKey(k, v string) {
340 key, ok := findKey(k)
344 if v == "ToggleHelp" {
348 actionNames := strings.Split(v, ",")
349 actions := make([]func(*View, bool) bool, 0, len(actionNames))
350 for _, actionName := range actionNames {
351 actions = append(actions, findAction(actionName))
354 bindings[key] = actions
357 // DefaultBindings returns a map containing micro's default keybindings
358 func DefaultBindings() map[string]string {
359 return map[string]string{
361 "Down": "CursorDown",
362 "Right": "CursorRight",
363 "Left": "CursorLeft",
364 "ShiftUp": "SelectUp",
365 "ShiftDown": "SelectDown",
366 "ShiftLeft": "SelectLeft",
367 "ShiftRight": "SelectRight",
368 "AltLeft": "WordLeft",
369 "AltRight": "WordRight",
370 "AltUp": "MoveLinesUp",
371 "AltDown": "MoveLinesDown",
372 "AltShiftRight": "SelectWordRight",
373 "AltShiftLeft": "SelectWordLeft",
374 "CtrlLeft": "StartOfLine",
375 "CtrlRight": "EndOfLine",
376 "CtrlShiftLeft": "SelectToStartOfLine",
377 "CtrlShiftRight": "SelectToEndOfLine",
378 "CtrlUp": "CursorStart",
379 "CtrlDown": "CursorEnd",
380 "CtrlShiftUp": "SelectToStart",
381 "CtrlShiftDown": "SelectToEnd",
382 "Enter": "InsertNewline",
383 "Space": "InsertSpace",
384 "CtrlH": "Backspace",
385 "Backspace": "Backspace",
386 "Alt-CtrlH": "DeleteWordLeft",
387 "Alt-Backspace": "DeleteWordLeft",
388 "Tab": "IndentSelection,InsertTab",
389 "Backtab": "OutdentSelection",
394 "CtrlP": "FindPrevious",
400 "CtrlD": "DuplicateLine",
402 "CtrlA": "SelectAll",
404 "CtrlRightSq": "PreviousTab",
405 "CtrlBackslash": "NextTab",
406 "Home": "StartOfLine",
408 "CtrlHome": "CursorStart",
409 "CtrlEnd": "CursorEnd",
410 "PageUp": "CursorPageUp",
411 "PageDown": "CursorPageDown",
412 "CtrlG": "ToggleHelp",
413 "CtrlR": "ToggleRuler",
416 "CtrlB": "ShellMode",
418 "CtrlE": "CommandMode",
419 "CtrlW": "NextSplit",
420 "CtrlU": "ToggleMacro",
421 "CtrlJ": "PlayMacro",
423 // Emacs-style keybindings
424 "Alt-f": "WordRight",
426 "Alt-a": "StartOfLine",
427 "Alt-e": "EndOfLine",
429 "Alt-n": "CursorDown",
431 // Integration with file managers