9 "github.com/zyedidia/tcell"
12 var bindings map[Key][]func(*View) bool
13 var helpBinding string
15 var bindingActions = map[string]func(*View) 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,
51 "CutLine": (*View).CutLine,
52 "DuplicateLine": (*View).DuplicateLine,
53 "DeleteLine": (*View).DeleteLine,
54 "IndentSelection": (*View).IndentSelection,
55 "OutdentSelection": (*View).OutdentSelection,
56 "Paste": (*View).Paste,
57 "SelectAll": (*View).SelectAll,
58 "OpenFile": (*View).OpenFile,
59 "Start": (*View).Start,
61 "PageUp": (*View).PageUp,
62 "PageDown": (*View).PageDown,
63 "HalfPageUp": (*View).HalfPageUp,
64 "HalfPageDown": (*View).HalfPageDown,
65 "StartOfLine": (*View).StartOfLine,
66 "EndOfLine": (*View).EndOfLine,
67 "ToggleHelp": (*View).ToggleHelp,
68 "ToggleRuler": (*View).ToggleRuler,
69 "JumpLine": (*View).JumpLine,
70 "ClearStatus": (*View).ClearStatus,
71 "ShellMode": (*View).ShellMode,
72 "CommandMode": (*View).CommandMode,
74 "AddTab": (*View).AddTab,
75 "PreviousTab": (*View).PreviousTab,
76 "NextTab": (*View).NextTab,
77 "NextSplit": (*View).NextSplit,
78 "PreviousSplit": (*View).PreviousSplit,
80 // This was changed to InsertNewline but I don't want to break backwards compatibility
81 "InsertEnter": (*View).InsertNewline,
84 var bindingKeys = map[string]tcell.Key{
86 "Down": tcell.KeyDown,
87 "Right": tcell.KeyRight,
88 "Left": tcell.KeyLeft,
89 "UpLeft": tcell.KeyUpLeft,
90 "UpRight": tcell.KeyUpRight,
91 "DownLeft": tcell.KeyDownLeft,
92 "DownRight": tcell.KeyDownRight,
93 "Center": tcell.KeyCenter,
94 "PageUp": tcell.KeyPgUp,
95 "PageDown": tcell.KeyPgDn,
96 "Home": tcell.KeyHome,
98 "Insert": tcell.KeyInsert,
99 "Delete": tcell.KeyDelete,
100 "Help": tcell.KeyHelp,
101 "Exit": tcell.KeyExit,
102 "Clear": tcell.KeyClear,
103 "Cancel": tcell.KeyCancel,
104 "Print": tcell.KeyPrint,
105 "Pause": tcell.KeyPause,
106 "Backtab": tcell.KeyBacktab,
171 "CtrlSpace": tcell.KeyCtrlSpace,
172 "CtrlA": tcell.KeyCtrlA,
173 "CtrlB": tcell.KeyCtrlB,
174 "CtrlC": tcell.KeyCtrlC,
175 "CtrlD": tcell.KeyCtrlD,
176 "CtrlE": tcell.KeyCtrlE,
177 "CtrlF": tcell.KeyCtrlF,
178 "CtrlG": tcell.KeyCtrlG,
179 "CtrlH": tcell.KeyCtrlH,
180 "CtrlI": tcell.KeyCtrlI,
181 "CtrlJ": tcell.KeyCtrlJ,
182 "CtrlK": tcell.KeyCtrlK,
183 "CtrlL": tcell.KeyCtrlL,
184 "CtrlM": tcell.KeyCtrlM,
185 "CtrlN": tcell.KeyCtrlN,
186 "CtrlO": tcell.KeyCtrlO,
187 "CtrlP": tcell.KeyCtrlP,
188 "CtrlQ": tcell.KeyCtrlQ,
189 "CtrlR": tcell.KeyCtrlR,
190 "CtrlS": tcell.KeyCtrlS,
191 "CtrlT": tcell.KeyCtrlT,
192 "CtrlU": tcell.KeyCtrlU,
193 "CtrlV": tcell.KeyCtrlV,
194 "CtrlW": tcell.KeyCtrlW,
195 "CtrlX": tcell.KeyCtrlX,
196 "CtrlY": tcell.KeyCtrlY,
197 "CtrlZ": tcell.KeyCtrlZ,
198 "CtrlLeftSq": tcell.KeyCtrlLeftSq,
199 "CtrlBackslash": tcell.KeyCtrlBackslash,
200 "CtrlRightSq": tcell.KeyCtrlRightSq,
201 "CtrlCarat": tcell.KeyCtrlCarat,
202 "CtrlUnderscore": tcell.KeyCtrlUnderscore,
203 "Backspace": tcell.KeyBackspace,
206 "Escape": tcell.KeyEscape,
207 "Enter": tcell.KeyEnter,
208 "Backspace2": tcell.KeyBackspace2,
210 // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
211 "PgUp": tcell.KeyPgUp,
212 "PgDown": tcell.KeyPgDn,
215 // The Key struct holds the data for a keypress (keycode + modifiers)
218 modifiers tcell.ModMask
222 // InitBindings initializes the keybindings for micro
223 func InitBindings() {
224 bindings = make(map[Key][]func(*View) bool)
226 var parsed map[string]string
227 defaults := DefaultBindings()
229 filename := configDir + "/bindings.json"
230 if _, e := os.Stat(filename); e == nil {
231 input, err := ioutil.ReadFile(filename)
233 TermMessage("Error reading bindings.json file: " + err.Error())
237 err = json.Unmarshal(input, &parsed)
239 TermMessage("Error reading bindings.json:", err.Error())
243 parseBindings(defaults)
244 parseBindings(parsed)
247 func parseBindings(userBindings map[string]string) {
248 for k, v := range userBindings {
253 // findKey will find binding Key 'b' using string 'k'
254 func findKey(k string) (b Key, ok bool) {
255 modifiers := tcell.ModNone
257 // First, we'll strip off all the modifiers in the name and add them to the
262 case strings.HasPrefix(k, "-"):
263 // We optionally support dashes between modifiers
265 case strings.HasPrefix(k, "Ctrl"):
267 modifiers |= tcell.ModCtrl
268 case strings.HasPrefix(k, "Alt"):
270 modifiers |= tcell.ModAlt
271 case strings.HasPrefix(k, "Shift"):
273 modifiers |= tcell.ModShift
279 // Control is handled specially, since some character codes in bindingKeys
280 // are different when Control is depressed. We should check for Control keys
282 if modifiers&tcell.ModCtrl != 0 {
283 // see if the key is in bindingKeys with the Ctrl prefix.
284 if code, ok := bindingKeys["Ctrl"+k]; ok {
285 // It is, we're done.
288 modifiers: modifiers,
294 // See if we can find the key in bindingKeys
295 if code, ok := bindingKeys[k]; ok {
298 modifiers: modifiers,
303 // If we were given one character, then we've got a rune.
306 keyCode: tcell.KeyRune,
307 modifiers: modifiers,
312 // We don't know what happened.
316 // findAction will find 'action' using string 'v'
317 func findAction(v string) (action func(*View) bool) {
318 action, ok := bindingActions[v]
320 // If the user seems to be binding a function that doesn't exist
321 // We hope that it's a lua function that exists and bind it to that
322 action = LuaFunctionBinding(v)
327 // BindKey takes a key and an action and binds the two together
328 func BindKey(k, v string) {
329 key, ok := findKey(k)
333 if v == "ToggleHelp" {
337 actionNames := strings.Split(v, ",")
338 actions := make([]func(*View) bool, 0, len(actionNames))
339 for _, actionName := range actionNames {
340 actions = append(actions, findAction(actionName))
343 bindings[key] = actions
346 // DefaultBindings returns a map containing micro's default keybindings
347 func DefaultBindings() map[string]string {
348 return map[string]string{
350 "Down": "CursorDown",
351 "Right": "CursorRight",
352 "Left": "CursorLeft",
353 "ShiftUp": "SelectUp",
354 "ShiftDown": "SelectDown",
355 "ShiftLeft": "SelectLeft",
356 "ShiftRight": "SelectRight",
357 "AltLeft": "WordLeft",
358 "AltRight": "WordRight",
359 "AltShiftRight": "SelectWordRight",
360 "AltShiftLeft": "SelectWordLeft",
361 "CtrlLeft": "StartOfLine",
362 "CtrlRight": "EndOfLine",
363 "CtrlShiftLeft": "SelectToStartOfLine",
364 "CtrlShiftRight": "SelectToEndOfLine",
365 "CtrlUp": "CursorStart",
366 "CtrlDown": "CursorEnd",
367 "CtrlShiftUp": "SelectToStart",
368 "CtrlShiftDown": "SelectToEnd",
369 "Enter": "InsertNewline",
370 "Space": "InsertSpace",
371 "Backspace": "Backspace",
372 "Backspace2": "Backspace",
373 "Alt-Backspace": "DeleteWordLeft",
374 "Alt-Backspace2": "DeleteWordLeft",
375 "Tab": "IndentSelection,InsertTab",
376 "Backtab": "OutdentSelection",
381 "CtrlP": "FindPrevious",
387 "CtrlD": "DuplicateLine",
389 "CtrlA": "SelectAll",
391 "CtrlRightSq": "PreviousTab",
392 "CtrlBackslash": "NextTab",
393 "Home": "StartOfLine",
395 "PageUp": "CursorPageUp",
396 "PageDown": "CursorPageDown",
397 "CtrlG": "ToggleHelp",
398 "CtrlR": "ToggleRuler",
401 "Esc": "ClearStatus",
402 "CtrlB": "ShellMode",
404 "CtrlE": "CommandMode",
405 "CtrlW": "NextSplit",
407 // Emacs-style keybindings
408 "Alt-f": "WordRight",
410 "Alt-a": "StartOfLine",
411 "Alt-e": "EndOfLine",
413 "Alt-n": "CursorDown",