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 "SelectAll": (*View).SelectAll,
59 "OpenFile": (*View).OpenFile,
60 "Start": (*View).Start,
62 "PageUp": (*View).PageUp,
63 "PageDown": (*View).PageDown,
64 "HalfPageUp": (*View).HalfPageUp,
65 "HalfPageDown": (*View).HalfPageDown,
66 "StartOfLine": (*View).StartOfLine,
67 "EndOfLine": (*View).EndOfLine,
68 "ToggleHelp": (*View).ToggleHelp,
69 "ToggleRuler": (*View).ToggleRuler,
70 "JumpLine": (*View).JumpLine,
71 "ClearStatus": (*View).ClearStatus,
72 "ShellMode": (*View).ShellMode,
73 "CommandMode": (*View).CommandMode,
75 "QuitAll": (*View).QuitAll,
76 "AddTab": (*View).AddTab,
77 "PreviousTab": (*View).PreviousTab,
78 "NextTab": (*View).NextTab,
79 "NextSplit": (*View).NextSplit,
80 "PreviousSplit": (*View).PreviousSplit,
82 // This was changed to InsertNewline but I don't want to break backwards compatibility
83 "InsertEnter": (*View).InsertNewline,
86 var bindingKeys = map[string]tcell.Key{
88 "Down": tcell.KeyDown,
89 "Right": tcell.KeyRight,
90 "Left": tcell.KeyLeft,
91 "UpLeft": tcell.KeyUpLeft,
92 "UpRight": tcell.KeyUpRight,
93 "DownLeft": tcell.KeyDownLeft,
94 "DownRight": tcell.KeyDownRight,
95 "Center": tcell.KeyCenter,
96 "PageUp": tcell.KeyPgUp,
97 "PageDown": tcell.KeyPgDn,
98 "Home": tcell.KeyHome,
100 "Insert": tcell.KeyInsert,
101 "Delete": tcell.KeyDelete,
102 "Help": tcell.KeyHelp,
103 "Exit": tcell.KeyExit,
104 "Clear": tcell.KeyClear,
105 "Cancel": tcell.KeyCancel,
106 "Print": tcell.KeyPrint,
107 "Pause": tcell.KeyPause,
108 "Backtab": tcell.KeyBacktab,
173 "CtrlSpace": tcell.KeyCtrlSpace,
174 "CtrlA": tcell.KeyCtrlA,
175 "CtrlB": tcell.KeyCtrlB,
176 "CtrlC": tcell.KeyCtrlC,
177 "CtrlD": tcell.KeyCtrlD,
178 "CtrlE": tcell.KeyCtrlE,
179 "CtrlF": tcell.KeyCtrlF,
180 "CtrlG": tcell.KeyCtrlG,
181 "CtrlH": tcell.KeyCtrlH,
182 "CtrlI": tcell.KeyCtrlI,
183 "CtrlJ": tcell.KeyCtrlJ,
184 "CtrlK": tcell.KeyCtrlK,
185 "CtrlL": tcell.KeyCtrlL,
186 "CtrlM": tcell.KeyCtrlM,
187 "CtrlN": tcell.KeyCtrlN,
188 "CtrlO": tcell.KeyCtrlO,
189 "CtrlP": tcell.KeyCtrlP,
190 "CtrlQ": tcell.KeyCtrlQ,
191 "CtrlR": tcell.KeyCtrlR,
192 "CtrlS": tcell.KeyCtrlS,
193 "CtrlT": tcell.KeyCtrlT,
194 "CtrlU": tcell.KeyCtrlU,
195 "CtrlV": tcell.KeyCtrlV,
196 "CtrlW": tcell.KeyCtrlW,
197 "CtrlX": tcell.KeyCtrlX,
198 "CtrlY": tcell.KeyCtrlY,
199 "CtrlZ": tcell.KeyCtrlZ,
200 "CtrlLeftSq": tcell.KeyCtrlLeftSq,
201 "CtrlBackslash": tcell.KeyCtrlBackslash,
202 "CtrlRightSq": tcell.KeyCtrlRightSq,
203 "CtrlCarat": tcell.KeyCtrlCarat,
204 "CtrlUnderscore": tcell.KeyCtrlUnderscore,
205 "Backspace": tcell.KeyBackspace,
208 "Escape": tcell.KeyEscape,
209 "Enter": tcell.KeyEnter,
210 "Backspace2": tcell.KeyBackspace2,
212 // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
213 "PgUp": tcell.KeyPgUp,
214 "PgDown": tcell.KeyPgDn,
217 // The Key struct holds the data for a keypress (keycode + modifiers)
220 modifiers tcell.ModMask
224 // InitBindings initializes the keybindings for micro
225 func InitBindings() {
226 bindings = make(map[Key][]func(*View, bool) bool)
228 var parsed map[string]string
229 defaults := DefaultBindings()
231 filename := configDir + "/bindings.json"
232 if _, e := os.Stat(filename); e == nil {
233 input, err := ioutil.ReadFile(filename)
235 TermMessage("Error reading bindings.json file: " + err.Error())
239 err = json5.Unmarshal(input, &parsed)
241 TermMessage("Error reading bindings.json:", err.Error())
245 parseBindings(defaults)
246 parseBindings(parsed)
249 func parseBindings(userBindings map[string]string) {
250 for k, v := range userBindings {
255 // findKey will find binding Key 'b' using string 'k'
256 func findKey(k string) (b Key, ok bool) {
257 modifiers := tcell.ModNone
259 // First, we'll strip off all the modifiers in the name and add them to the
264 case strings.HasPrefix(k, "-"):
265 // We optionally support dashes between modifiers
267 case strings.HasPrefix(k, "Ctrl"):
269 modifiers |= tcell.ModCtrl
270 case strings.HasPrefix(k, "Alt"):
272 modifiers |= tcell.ModAlt
273 case strings.HasPrefix(k, "Shift"):
275 modifiers |= tcell.ModShift
281 // Control is handled specially, since some character codes in bindingKeys
282 // are different when Control is depressed. We should check for Control keys
284 if modifiers&tcell.ModCtrl != 0 {
285 // see if the key is in bindingKeys with the Ctrl prefix.
286 if code, ok := bindingKeys["Ctrl"+k]; ok {
287 // It is, we're done.
290 modifiers: modifiers,
296 // See if we can find the key in bindingKeys
297 if code, ok := bindingKeys[k]; ok {
300 modifiers: modifiers,
305 // If we were given one character, then we've got a rune.
308 keyCode: tcell.KeyRune,
309 modifiers: modifiers,
314 // We don't know what happened.
318 // findAction will find 'action' using string 'v'
319 func findAction(v string) (action func(*View, bool) bool) {
320 action, ok := bindingActions[v]
322 // If the user seems to be binding a function that doesn't exist
323 // We hope that it's a lua function that exists and bind it to that
324 action = LuaFunctionBinding(v)
329 // BindKey takes a key and an action and binds the two together
330 func BindKey(k, v string) {
331 key, ok := findKey(k)
335 if v == "ToggleHelp" {
339 actionNames := strings.Split(v, ",")
340 actions := make([]func(*View, bool) bool, 0, len(actionNames))
341 for _, actionName := range actionNames {
342 actions = append(actions, findAction(actionName))
345 bindings[key] = actions
348 // DefaultBindings returns a map containing micro's default keybindings
349 func DefaultBindings() map[string]string {
350 return map[string]string{
352 "Down": "CursorDown",
353 "Right": "CursorRight",
354 "Left": "CursorLeft",
355 "ShiftUp": "SelectUp",
356 "ShiftDown": "SelectDown",
357 "ShiftLeft": "SelectLeft",
358 "ShiftRight": "SelectRight",
359 "AltLeft": "WordLeft",
360 "AltRight": "WordRight",
361 "AltShiftRight": "SelectWordRight",
362 "AltShiftLeft": "SelectWordLeft",
363 "CtrlLeft": "StartOfLine",
364 "CtrlRight": "EndOfLine",
365 "CtrlShiftLeft": "SelectToStartOfLine",
366 "CtrlShiftRight": "SelectToEndOfLine",
367 "CtrlUp": "CursorStart",
368 "CtrlDown": "CursorEnd",
369 "CtrlShiftUp": "SelectToStart",
370 "CtrlShiftDown": "SelectToEnd",
371 "Enter": "InsertNewline",
372 "Space": "InsertSpace",
373 "Backspace": "Backspace",
374 "Backspace2": "Backspace",
375 "Alt-Backspace": "DeleteWordLeft",
376 "Alt-Backspace2": "DeleteWordLeft",
377 "Tab": "IndentSelection,InsertTab",
378 "Backtab": "OutdentSelection",
383 "CtrlP": "FindPrevious",
389 "CtrlD": "DuplicateLine",
391 "CtrlA": "SelectAll",
393 "CtrlRightSq": "PreviousTab",
394 "CtrlBackslash": "NextTab",
395 "Home": "StartOfLine",
397 "CtrlHome": "CursorStart",
398 "CtrlEnd": "CursorEnd",
399 "PageUp": "CursorPageUp",
400 "PageDown": "CursorPageDown",
401 "CtrlG": "ToggleHelp",
402 "CtrlR": "ToggleRuler",
405 "Esc": "ClearStatus",
406 "CtrlB": "ShellMode",
408 "CtrlE": "CommandMode",
409 "CtrlW": "NextSplit",
411 // Emacs-style keybindings
412 "Alt-f": "WordRight",
414 "Alt-a": "StartOfLine",
415 "Alt-e": "EndOfLine",
417 "Alt-n": "CursorDown",