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,
83 // This was changed to InsertNewline but I don't want to break backwards compatibility
84 "InsertEnter": (*View).InsertNewline,
87 var bindingKeys = map[string]tcell.Key{
89 "Down": tcell.KeyDown,
90 "Right": tcell.KeyRight,
91 "Left": tcell.KeyLeft,
92 "UpLeft": tcell.KeyUpLeft,
93 "UpRight": tcell.KeyUpRight,
94 "DownLeft": tcell.KeyDownLeft,
95 "DownRight": tcell.KeyDownRight,
96 "Center": tcell.KeyCenter,
97 "PageUp": tcell.KeyPgUp,
98 "PageDown": tcell.KeyPgDn,
99 "Home": tcell.KeyHome,
101 "Insert": tcell.KeyInsert,
102 "Delete": tcell.KeyDelete,
103 "Help": tcell.KeyHelp,
104 "Exit": tcell.KeyExit,
105 "Clear": tcell.KeyClear,
106 "Cancel": tcell.KeyCancel,
107 "Print": tcell.KeyPrint,
108 "Pause": tcell.KeyPause,
109 "Backtab": tcell.KeyBacktab,
174 "CtrlSpace": tcell.KeyCtrlSpace,
175 "CtrlA": tcell.KeyCtrlA,
176 "CtrlB": tcell.KeyCtrlB,
177 "CtrlC": tcell.KeyCtrlC,
178 "CtrlD": tcell.KeyCtrlD,
179 "CtrlE": tcell.KeyCtrlE,
180 "CtrlF": tcell.KeyCtrlF,
181 "CtrlG": tcell.KeyCtrlG,
182 "CtrlH": tcell.KeyCtrlH,
183 "CtrlI": tcell.KeyCtrlI,
184 "CtrlJ": tcell.KeyCtrlJ,
185 "CtrlK": tcell.KeyCtrlK,
186 "CtrlL": tcell.KeyCtrlL,
187 "CtrlM": tcell.KeyCtrlM,
188 "CtrlN": tcell.KeyCtrlN,
189 "CtrlO": tcell.KeyCtrlO,
190 "CtrlP": tcell.KeyCtrlP,
191 "CtrlQ": tcell.KeyCtrlQ,
192 "CtrlR": tcell.KeyCtrlR,
193 "CtrlS": tcell.KeyCtrlS,
194 "CtrlT": tcell.KeyCtrlT,
195 "CtrlU": tcell.KeyCtrlU,
196 "CtrlV": tcell.KeyCtrlV,
197 "CtrlW": tcell.KeyCtrlW,
198 "CtrlX": tcell.KeyCtrlX,
199 "CtrlY": tcell.KeyCtrlY,
200 "CtrlZ": tcell.KeyCtrlZ,
201 "CtrlLeftSq": tcell.KeyCtrlLeftSq,
202 "CtrlBackslash": tcell.KeyCtrlBackslash,
203 "CtrlRightSq": tcell.KeyCtrlRightSq,
204 "CtrlCarat": tcell.KeyCtrlCarat,
205 "CtrlUnderscore": tcell.KeyCtrlUnderscore,
206 "Backspace": tcell.KeyBackspace,
209 "Escape": tcell.KeyEscape,
210 "Enter": tcell.KeyEnter,
211 "Backspace2": tcell.KeyBackspace2,
213 // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
214 "PgUp": tcell.KeyPgUp,
215 "PgDown": tcell.KeyPgDn,
218 // The Key struct holds the data for a keypress (keycode + modifiers)
221 modifiers tcell.ModMask
225 // InitBindings initializes the keybindings for micro
226 func InitBindings() {
227 bindings = make(map[Key][]func(*View, bool) bool)
229 var parsed map[string]string
230 defaults := DefaultBindings()
232 filename := configDir + "/bindings.json"
233 if _, e := os.Stat(filename); e == nil {
234 input, err := ioutil.ReadFile(filename)
236 TermMessage("Error reading bindings.json file: " + err.Error())
240 err = json.Unmarshal(input, &parsed)
242 TermMessage("Error reading bindings.json:", err.Error())
246 parseBindings(defaults)
247 parseBindings(parsed)
250 func parseBindings(userBindings map[string]string) {
251 for k, v := range userBindings {
256 // findKey will find binding Key 'b' using string 'k'
257 func findKey(k string) (b Key, ok bool) {
258 modifiers := tcell.ModNone
260 // First, we'll strip off all the modifiers in the name and add them to the
265 case strings.HasPrefix(k, "-"):
266 // We optionally support dashes between modifiers
268 case strings.HasPrefix(k, "Ctrl"):
270 modifiers |= tcell.ModCtrl
271 case strings.HasPrefix(k, "Alt"):
273 modifiers |= tcell.ModAlt
274 case strings.HasPrefix(k, "Shift"):
276 modifiers |= tcell.ModShift
282 // Control is handled specially, since some character codes in bindingKeys
283 // are different when Control is depressed. We should check for Control keys
285 if modifiers&tcell.ModCtrl != 0 {
286 // see if the key is in bindingKeys with the Ctrl prefix.
287 if code, ok := bindingKeys["Ctrl"+k]; ok {
288 // It is, we're done.
291 modifiers: modifiers,
297 // See if we can find the key in bindingKeys
298 if code, ok := bindingKeys[k]; ok {
301 modifiers: modifiers,
306 // If we were given one character, then we've got a rune.
309 keyCode: tcell.KeyRune,
310 modifiers: modifiers,
315 // We don't know what happened.
319 // findAction will find 'action' using string 'v'
320 func findAction(v string) (action func(*View, bool) bool) {
321 action, ok := bindingActions[v]
323 // If the user seems to be binding a function that doesn't exist
324 // We hope that it's a lua function that exists and bind it to that
325 action = LuaFunctionBinding(v)
330 // BindKey takes a key and an action and binds the two together
331 func BindKey(k, v string) {
332 key, ok := findKey(k)
336 if v == "ToggleHelp" {
340 actionNames := strings.Split(v, ",")
341 actions := make([]func(*View, bool) bool, 0, len(actionNames))
342 for _, actionName := range actionNames {
343 actions = append(actions, findAction(actionName))
346 bindings[key] = actions
349 // DefaultBindings returns a map containing micro's default keybindings
350 func DefaultBindings() map[string]string {
351 return map[string]string{
353 "Down": "CursorDown",
354 "Right": "CursorRight",
355 "Left": "CursorLeft",
356 "ShiftUp": "SelectUp",
357 "ShiftDown": "SelectDown",
358 "ShiftLeft": "SelectLeft",
359 "ShiftRight": "SelectRight",
360 "AltLeft": "WordLeft",
361 "AltRight": "WordRight",
362 "AltShiftRight": "SelectWordRight",
363 "AltShiftLeft": "SelectWordLeft",
364 "CtrlLeft": "StartOfLine",
365 "CtrlRight": "EndOfLine",
366 "CtrlShiftLeft": "SelectToStartOfLine",
367 "CtrlShiftRight": "SelectToEndOfLine",
368 "CtrlUp": "CursorStart",
369 "CtrlDown": "CursorEnd",
370 "CtrlShiftUp": "SelectToStart",
371 "CtrlShiftDown": "SelectToEnd",
372 "Enter": "InsertNewline",
373 "Space": "InsertSpace",
374 "Backspace": "Backspace",
375 "Backspace2": "Backspace",
376 "Alt-Backspace": "DeleteWordLeft",
377 "Alt-Backspace2": "DeleteWordLeft",
378 "Tab": "IndentSelection,InsertTab",
379 "Backtab": "OutdentSelection",
384 "CtrlP": "FindPrevious",
390 "CtrlD": "DuplicateLine",
392 "CtrlA": "SelectAll",
394 "CtrlRightSq": "PreviousTab",
395 "CtrlBackslash": "NextTab",
396 "Home": "StartOfLine",
398 "PageUp": "CursorPageUp",
399 "PageDown": "CursorPageDown",
400 "CtrlG": "ToggleHelp",
401 "CtrlR": "ToggleRuler",
404 "Esc": "ClearStatus",
405 "CtrlB": "ShellMode",
407 "CtrlE": "CommandMode",
408 "CtrlW": "NextSplit",
410 // Emacs-style keybindings
411 "Alt-f": "WordRight",
413 "Alt-a": "StartOfLine",
414 "Alt-e": "EndOfLine",
416 "Alt-n": "CursorDown",