8 "github.com/yosuke-furukawa/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 "IndentSelection": (*View).IndentSelection,
56 "OutdentSelection": (*View).OutdentSelection,
57 "Paste": (*View).Paste,
58 "PastePrimary": (*View).PastePrimary,
59 "SelectAll": (*View).SelectAll,
60 "OpenFile": (*View).OpenFile,
61 "Start": (*View).Start,
63 "PageUp": (*View).PageUp,
64 "PageDown": (*View).PageDown,
65 "HalfPageUp": (*View).HalfPageUp,
66 "HalfPageDown": (*View).HalfPageDown,
67 "StartOfLine": (*View).StartOfLine,
68 "EndOfLine": (*View).EndOfLine,
69 "ToggleHelp": (*View).ToggleHelp,
70 "ToggleRuler": (*View).ToggleRuler,
71 "JumpLine": (*View).JumpLine,
72 "ClearStatus": (*View).ClearStatus,
73 "ShellMode": (*View).ShellMode,
74 "CommandMode": (*View).CommandMode,
76 "QuitAll": (*View).QuitAll,
77 "AddTab": (*View).AddTab,
78 "PreviousTab": (*View).PreviousTab,
79 "NextTab": (*View).NextTab,
80 "NextSplit": (*View).NextSplit,
81 "PreviousSplit": (*View).PreviousSplit,
82 "ToggleMacro": (*View).ToggleMacro,
83 "PlayMacro": (*View).PlayMacro,
85 // This was changed to InsertNewline but I don't want to break backwards compatibility
86 "InsertEnter": (*View).InsertNewline,
89 var bindingKeys = map[string]tcell.Key{
91 "Down": tcell.KeyDown,
92 "Right": tcell.KeyRight,
93 "Left": tcell.KeyLeft,
94 "UpLeft": tcell.KeyUpLeft,
95 "UpRight": tcell.KeyUpRight,
96 "DownLeft": tcell.KeyDownLeft,
97 "DownRight": tcell.KeyDownRight,
98 "Center": tcell.KeyCenter,
99 "PageUp": tcell.KeyPgUp,
100 "PageDown": tcell.KeyPgDn,
101 "Home": tcell.KeyHome,
103 "Insert": tcell.KeyInsert,
104 "Delete": tcell.KeyDelete,
105 "Help": tcell.KeyHelp,
106 "Exit": tcell.KeyExit,
107 "Clear": tcell.KeyClear,
108 "Cancel": tcell.KeyCancel,
109 "Print": tcell.KeyPrint,
110 "Pause": tcell.KeyPause,
111 "Backtab": tcell.KeyBacktab,
176 "CtrlSpace": tcell.KeyCtrlSpace,
177 "CtrlA": tcell.KeyCtrlA,
178 "CtrlB": tcell.KeyCtrlB,
179 "CtrlC": tcell.KeyCtrlC,
180 "CtrlD": tcell.KeyCtrlD,
181 "CtrlE": tcell.KeyCtrlE,
182 "CtrlF": tcell.KeyCtrlF,
183 "CtrlG": tcell.KeyCtrlG,
184 "CtrlH": tcell.KeyCtrlH,
185 "CtrlI": tcell.KeyCtrlI,
186 "CtrlJ": tcell.KeyCtrlJ,
187 "CtrlK": tcell.KeyCtrlK,
188 "CtrlL": tcell.KeyCtrlL,
189 "CtrlM": tcell.KeyCtrlM,
190 "CtrlN": tcell.KeyCtrlN,
191 "CtrlO": tcell.KeyCtrlO,
192 "CtrlP": tcell.KeyCtrlP,
193 "CtrlQ": tcell.KeyCtrlQ,
194 "CtrlR": tcell.KeyCtrlR,
195 "CtrlS": tcell.KeyCtrlS,
196 "CtrlT": tcell.KeyCtrlT,
197 "CtrlU": tcell.KeyCtrlU,
198 "CtrlV": tcell.KeyCtrlV,
199 "CtrlW": tcell.KeyCtrlW,
200 "CtrlX": tcell.KeyCtrlX,
201 "CtrlY": tcell.KeyCtrlY,
202 "CtrlZ": tcell.KeyCtrlZ,
203 "CtrlLeftSq": tcell.KeyCtrlLeftSq,
204 "CtrlBackslash": tcell.KeyCtrlBackslash,
205 "CtrlRightSq": tcell.KeyCtrlRightSq,
206 "CtrlCarat": tcell.KeyCtrlCarat,
207 "CtrlUnderscore": tcell.KeyCtrlUnderscore,
210 "Escape": tcell.KeyEscape,
211 "Enter": tcell.KeyEnter,
212 "Backspace": tcell.KeyBackspace2,
214 // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
215 "PgUp": tcell.KeyPgUp,
216 "PgDown": tcell.KeyPgDn,
219 // The Key struct holds the data for a keypress (keycode + modifiers)
222 modifiers tcell.ModMask
226 // InitBindings initializes the keybindings for micro
227 func InitBindings() {
228 bindings = make(map[Key][]func(*View, bool) bool)
230 var parsed map[string]string
231 defaults := DefaultBindings()
233 filename := configDir + "/bindings.json"
234 if _, e := os.Stat(filename); e == nil {
235 input, err := ioutil.ReadFile(filename)
237 TermMessage("Error reading bindings.json file: " + err.Error())
241 err = json5.Unmarshal(input, &parsed)
243 TermMessage("Error reading bindings.json:", err.Error())
247 parseBindings(defaults)
248 parseBindings(parsed)
251 func parseBindings(userBindings map[string]string) {
252 for k, v := range userBindings {
257 // findKey will find binding Key 'b' using string 'k'
258 func findKey(k string) (b Key, ok bool) {
259 modifiers := tcell.ModNone
261 // First, we'll strip off all the modifiers in the name and add them to the
266 case strings.HasPrefix(k, "-"):
267 // We optionally support dashes between modifiers
269 case strings.HasPrefix(k, "Ctrl") && !k == "CtrlH":
270 // CtrlH technically does not have a 'Ctrl' modifier because it is really backspace
272 modifiers |= tcell.ModCtrl
273 case strings.HasPrefix(k, "Alt"):
275 modifiers |= tcell.ModAlt
276 case strings.HasPrefix(k, "Shift"):
278 modifiers |= tcell.ModShift
284 // Control is handled specially, since some character codes in bindingKeys
285 // are different when Control is depressed. We should check for Control keys
287 if modifiers&tcell.ModCtrl != 0 {
288 // see if the key is in bindingKeys with the Ctrl prefix.
289 if code, ok := bindingKeys["Ctrl"+k]; ok {
290 // It is, we're done.
293 modifiers: modifiers,
299 // See if we can find the key in bindingKeys
300 if code, ok := bindingKeys[k]; ok {
303 modifiers: modifiers,
308 // If we were given one character, then we've got a rune.
311 keyCode: tcell.KeyRune,
312 modifiers: modifiers,
317 // We don't know what happened.
321 // findAction will find 'action' using string 'v'
322 func findAction(v string) (action func(*View, bool) bool) {
323 action, ok := bindingActions[v]
325 // If the user seems to be binding a function that doesn't exist
326 // We hope that it's a lua function that exists and bind it to that
327 action = LuaFunctionBinding(v)
332 // BindKey takes a key and an action and binds the two together
333 func BindKey(k, v string) {
334 key, ok := findKey(k)
338 if v == "ToggleHelp" {
342 actionNames := strings.Split(v, ",")
343 actions := make([]func(*View, bool) bool, 0, len(actionNames))
344 for _, actionName := range actionNames {
345 actions = append(actions, findAction(actionName))
348 bindings[key] = actions
351 // DefaultBindings returns a map containing micro's default keybindings
352 func DefaultBindings() map[string]string {
353 return map[string]string{
355 "Down": "CursorDown",
356 "Right": "CursorRight",
357 "Left": "CursorLeft",
358 "ShiftUp": "SelectUp",
359 "ShiftDown": "SelectDown",
360 "ShiftLeft": "SelectLeft",
361 "ShiftRight": "SelectRight",
362 "AltLeft": "WordLeft",
363 "AltRight": "WordRight",
364 "AltShiftRight": "SelectWordRight",
365 "AltShiftLeft": "SelectWordLeft",
366 "CtrlLeft": "StartOfLine",
367 "CtrlRight": "EndOfLine",
368 "CtrlShiftLeft": "SelectToStartOfLine",
369 "CtrlShiftRight": "SelectToEndOfLine",
370 "CtrlUp": "CursorStart",
371 "CtrlDown": "CursorEnd",
372 "CtrlShiftUp": "SelectToStart",
373 "CtrlShiftDown": "SelectToEnd",
374 "Enter": "InsertNewline",
375 "Space": "InsertSpace",
376 "CtrlH": "Backspace",
377 "Backspace": "Backspace",
378 "Alt-CtrlH": "DeleteWordLeft",
379 "Alt-Backspace": "DeleteWordLeft",
380 "Tab": "IndentSelection,InsertTab",
381 "Backtab": "OutdentSelection",
386 "CtrlP": "FindPrevious",
392 "CtrlD": "DuplicateLine",
394 "CtrlA": "SelectAll",
396 "CtrlRightSq": "PreviousTab",
397 "CtrlBackslash": "NextTab",
398 "Home": "StartOfLine",
400 "CtrlHome": "CursorStart",
401 "CtrlEnd": "CursorEnd",
402 "PageUp": "CursorPageUp",
403 "PageDown": "CursorPageDown",
404 "CtrlG": "ToggleHelp",
405 "CtrlR": "ToggleRuler",
408 "CtrlB": "ShellMode",
410 "CtrlE": "CommandMode",
411 "CtrlW": "NextSplit",
412 "CtrlU": "ToggleMacro",
413 "CtrlJ": "PlayMacro",
415 // Emacs-style keybindings
416 "Alt-f": "WordRight",
418 "Alt-a": "StartOfLine",
419 "Alt-e": "EndOfLine",
421 "Alt-n": "CursorDown",
423 // Integration with file managers