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,
44 "SaveAs": (*View).SaveAs,
46 "FindNext": (*View).FindNext,
47 "FindPrevious": (*View).FindPrevious,
48 "Center": (*View).Center,
53 "CutLine": (*View).CutLine,
54 "DuplicateLine": (*View).DuplicateLine,
55 "DeleteLine": (*View).DeleteLine,
56 "MoveLinesUp": (*View).MoveLinesUp,
57 "MoveLinesDown": (*View).MoveLinesDown,
58 "IndentSelection": (*View).IndentSelection,
59 "OutdentSelection": (*View).OutdentSelection,
60 "OutdentLine": (*View).OutdentLine,
61 "Paste": (*View).Paste,
62 "PastePrimary": (*View).PastePrimary,
63 "SelectAll": (*View).SelectAll,
64 "OpenFile": (*View).OpenFile,
65 "Start": (*View).Start,
67 "PageUp": (*View).PageUp,
68 "PageDown": (*View).PageDown,
69 "HalfPageUp": (*View).HalfPageUp,
70 "HalfPageDown": (*View).HalfPageDown,
71 "StartOfLine": (*View).StartOfLine,
72 "EndOfLine": (*View).EndOfLine,
73 "ToggleHelp": (*View).ToggleHelp,
74 "ToggleRuler": (*View).ToggleRuler,
75 "JumpLine": (*View).JumpLine,
76 "ClearStatus": (*View).ClearStatus,
77 "ShellMode": (*View).ShellMode,
78 "CommandMode": (*View).CommandMode,
79 "Escape": (*View).Escape,
81 "QuitAll": (*View).QuitAll,
82 "AddTab": (*View).AddTab,
83 "PreviousTab": (*View).PreviousTab,
84 "NextTab": (*View).NextTab,
85 "NextSplit": (*View).NextSplit,
86 "PreviousSplit": (*View).PreviousSplit,
87 "Unsplit": (*View).Unsplit,
88 "VSplit": (*View).VSplitBinding,
89 "HSplit": (*View).HSplitBinding,
90 "ToggleMacro": (*View).ToggleMacro,
91 "PlayMacro": (*View).PlayMacro,
93 // This was changed to InsertNewline but I don't want to break backwards compatibility
94 "InsertEnter": (*View).InsertNewline,
97 var bindingKeys = map[string]tcell.Key{
99 "Down": tcell.KeyDown,
100 "Right": tcell.KeyRight,
101 "Left": tcell.KeyLeft,
102 "UpLeft": tcell.KeyUpLeft,
103 "UpRight": tcell.KeyUpRight,
104 "DownLeft": tcell.KeyDownLeft,
105 "DownRight": tcell.KeyDownRight,
106 "Center": tcell.KeyCenter,
107 "PageUp": tcell.KeyPgUp,
108 "PageDown": tcell.KeyPgDn,
109 "Home": tcell.KeyHome,
111 "Insert": tcell.KeyInsert,
112 "Delete": tcell.KeyDelete,
113 "Help": tcell.KeyHelp,
114 "Exit": tcell.KeyExit,
115 "Clear": tcell.KeyClear,
116 "Cancel": tcell.KeyCancel,
117 "Print": tcell.KeyPrint,
118 "Pause": tcell.KeyPause,
119 "Backtab": tcell.KeyBacktab,
184 "CtrlSpace": tcell.KeyCtrlSpace,
185 "CtrlA": tcell.KeyCtrlA,
186 "CtrlB": tcell.KeyCtrlB,
187 "CtrlC": tcell.KeyCtrlC,
188 "CtrlD": tcell.KeyCtrlD,
189 "CtrlE": tcell.KeyCtrlE,
190 "CtrlF": tcell.KeyCtrlF,
191 "CtrlG": tcell.KeyCtrlG,
192 "CtrlH": tcell.KeyCtrlH,
193 "CtrlI": tcell.KeyCtrlI,
194 "CtrlJ": tcell.KeyCtrlJ,
195 "CtrlK": tcell.KeyCtrlK,
196 "CtrlL": tcell.KeyCtrlL,
197 "CtrlM": tcell.KeyCtrlM,
198 "CtrlN": tcell.KeyCtrlN,
199 "CtrlO": tcell.KeyCtrlO,
200 "CtrlP": tcell.KeyCtrlP,
201 "CtrlQ": tcell.KeyCtrlQ,
202 "CtrlR": tcell.KeyCtrlR,
203 "CtrlS": tcell.KeyCtrlS,
204 "CtrlT": tcell.KeyCtrlT,
205 "CtrlU": tcell.KeyCtrlU,
206 "CtrlV": tcell.KeyCtrlV,
207 "CtrlW": tcell.KeyCtrlW,
208 "CtrlX": tcell.KeyCtrlX,
209 "CtrlY": tcell.KeyCtrlY,
210 "CtrlZ": tcell.KeyCtrlZ,
211 "CtrlLeftSq": tcell.KeyCtrlLeftSq,
212 "CtrlBackslash": tcell.KeyCtrlBackslash,
213 "CtrlRightSq": tcell.KeyCtrlRightSq,
214 "CtrlCarat": tcell.KeyCtrlCarat,
215 "CtrlUnderscore": tcell.KeyCtrlUnderscore,
218 "Escape": tcell.KeyEscape,
219 "Enter": tcell.KeyEnter,
220 "Backspace": tcell.KeyBackspace2,
222 // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
223 "PgUp": tcell.KeyPgUp,
224 "PgDown": tcell.KeyPgDn,
227 // The Key struct holds the data for a keypress (keycode + modifiers)
230 modifiers tcell.ModMask
234 // InitBindings initializes the keybindings for micro
235 func InitBindings() {
236 bindings = make(map[Key][]func(*View, bool) bool)
238 var parsed map[string]string
239 defaults := DefaultBindings()
241 filename := configDir + "/bindings.json"
242 if _, e := os.Stat(filename); e == nil {
243 input, err := ioutil.ReadFile(filename)
245 TermMessage("Error reading bindings.json file: " + err.Error())
249 err = json5.Unmarshal(input, &parsed)
251 TermMessage("Error reading bindings.json:", err.Error())
255 parseBindings(defaults)
256 parseBindings(parsed)
259 func parseBindings(userBindings map[string]string) {
260 for k, v := range userBindings {
265 // findKey will find binding Key 'b' using string 'k'
266 func findKey(k string) (b Key, ok bool) {
267 modifiers := tcell.ModNone
269 // First, we'll strip off all the modifiers in the name and add them to the
274 case strings.HasPrefix(k, "-"):
275 // We optionally support dashes between modifiers
277 case strings.HasPrefix(k, "Ctrl") && k != "CtrlH":
278 // CtrlH technically does not have a 'Ctrl' modifier because it is really backspace
280 modifiers |= tcell.ModCtrl
281 case strings.HasPrefix(k, "Alt"):
283 modifiers |= tcell.ModAlt
284 case strings.HasPrefix(k, "Shift"):
286 modifiers |= tcell.ModShift
292 // Control is handled specially, since some character codes in bindingKeys
293 // are different when Control is depressed. We should check for Control keys
295 if modifiers&tcell.ModCtrl != 0 {
296 // see if the key is in bindingKeys with the Ctrl prefix.
297 if code, ok := bindingKeys["Ctrl"+k]; ok {
298 // It is, we're done.
301 modifiers: modifiers,
307 // See if we can find the key in bindingKeys
308 if code, ok := bindingKeys[k]; ok {
311 modifiers: modifiers,
316 // If we were given one character, then we've got a rune.
319 keyCode: tcell.KeyRune,
320 modifiers: modifiers,
325 // We don't know what happened.
329 // findAction will find 'action' using string 'v'
330 func findAction(v string) (action func(*View, bool) bool) {
331 action, ok := bindingActions[v]
333 // If the user seems to be binding a function that doesn't exist
334 // We hope that it's a lua function that exists and bind it to that
335 action = LuaFunctionBinding(v)
340 // BindKey takes a key and an action and binds the two together
341 func BindKey(k, v string) {
342 key, ok := findKey(k)
344 TermMessage("Unknown keybinding: " + k)
347 if v == "ToggleHelp" {
350 if helpBinding == k && v != "ToggleHelp" {
354 actionNames := strings.Split(v, ",")
355 if actionNames[0] == "UnbindKey" {
356 delete(bindings, key)
357 if len(actionNames) == 1 {
358 actionNames = make([]string, 0, 0)
360 actionNames = append(actionNames[:0], actionNames[1:]...)
363 actions := make([]func(*View, bool) bool, 0, len(actionNames))
364 for _, actionName := range actionNames {
365 actions = append(actions, findAction(actionName))
368 bindings[key] = actions
371 // DefaultBindings returns a map containing micro's default keybindings
372 func DefaultBindings() map[string]string {
373 return map[string]string{
375 "Down": "CursorDown",
376 "Right": "CursorRight",
377 "Left": "CursorLeft",
378 "ShiftUp": "SelectUp",
379 "ShiftDown": "SelectDown",
380 "ShiftLeft": "SelectLeft",
381 "ShiftRight": "SelectRight",
382 "AltLeft": "WordLeft",
383 "AltRight": "WordRight",
384 "AltUp": "MoveLinesUp",
385 "AltDown": "MoveLinesDown",
386 "AltShiftRight": "SelectWordRight",
387 "AltShiftLeft": "SelectWordLeft",
388 "CtrlLeft": "StartOfLine",
389 "CtrlRight": "EndOfLine",
390 "CtrlShiftLeft": "SelectToStartOfLine",
391 "CtrlShiftRight": "SelectToEndOfLine",
392 "CtrlUp": "CursorStart",
393 "CtrlDown": "CursorEnd",
394 "CtrlShiftUp": "SelectToStart",
395 "CtrlShiftDown": "SelectToEnd",
396 "Enter": "InsertNewline",
397 "CtrlH": "Backspace",
398 "Backspace": "Backspace",
399 "Alt-CtrlH": "DeleteWordLeft",
400 "Alt-Backspace": "DeleteWordLeft",
401 "Tab": "IndentSelection,InsertTab",
402 "Backtab": "OutdentSelection,OutdentLine",
407 "CtrlP": "FindPrevious",
413 "CtrlD": "DuplicateLine",
415 "CtrlA": "SelectAll",
417 "CtrlRightSq": "PreviousTab",
418 "CtrlBackslash": "NextTab",
419 "Home": "StartOfLine",
421 "CtrlHome": "CursorStart",
422 "CtrlEnd": "CursorEnd",
423 "PageUp": "CursorPageUp",
424 "PageDown": "CursorPageDown",
425 "CtrlG": "ToggleHelp",
426 "CtrlR": "ToggleRuler",
429 "CtrlB": "ShellMode",
431 "CtrlE": "CommandMode",
432 "CtrlW": "NextSplit",
433 "CtrlU": "ToggleMacro",
434 "CtrlJ": "PlayMacro",
436 // Emacs-style keybindings
437 "Alt-f": "WordRight",
439 "Alt-a": "StartOfLine",
440 "Alt-e": "EndOfLine",
442 "Alt-n": "CursorDown",
444 // Integration with file managers