]> git.lizzy.rs Git - micro.git/blob - cmd/micro/bindings.go
Merge pull request #316 from elopio/snapcraft
[micro.git] / cmd / micro / bindings.go
1 package main
2
3 import (
4         "io/ioutil"
5         "os"
6         "strings"
7
8         "github.com/yosuke-furukawa/json5/encoding/json5"
9         "github.com/zyedidia/tcell"
10 )
11
12 var bindings map[Key][]func(*View, bool) bool
13 var helpBinding string
14
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,
43         "Save":                (*View).Save,
44         "Find":                (*View).Find,
45         "FindNext":            (*View).FindNext,
46         "FindPrevious":        (*View).FindPrevious,
47         "Center":              (*View).Center,
48         "Undo":                (*View).Undo,
49         "Redo":                (*View).Redo,
50         "Copy":                (*View).Copy,
51         "Cut":                 (*View).Cut,
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,
62         "End":                 (*View).End,
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,
75         "Quit":                (*View).Quit,
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,
84
85         // This was changed to InsertNewline but I don't want to break backwards compatibility
86         "InsertEnter": (*View).InsertNewline,
87 }
88
89 var bindingKeys = map[string]tcell.Key{
90         "Up":             tcell.KeyUp,
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,
102         "End":            tcell.KeyEnd,
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,
112         "F1":             tcell.KeyF1,
113         "F2":             tcell.KeyF2,
114         "F3":             tcell.KeyF3,
115         "F4":             tcell.KeyF4,
116         "F5":             tcell.KeyF5,
117         "F6":             tcell.KeyF6,
118         "F7":             tcell.KeyF7,
119         "F8":             tcell.KeyF8,
120         "F9":             tcell.KeyF9,
121         "F10":            tcell.KeyF10,
122         "F11":            tcell.KeyF11,
123         "F12":            tcell.KeyF12,
124         "F13":            tcell.KeyF13,
125         "F14":            tcell.KeyF14,
126         "F15":            tcell.KeyF15,
127         "F16":            tcell.KeyF16,
128         "F17":            tcell.KeyF17,
129         "F18":            tcell.KeyF18,
130         "F19":            tcell.KeyF19,
131         "F20":            tcell.KeyF20,
132         "F21":            tcell.KeyF21,
133         "F22":            tcell.KeyF22,
134         "F23":            tcell.KeyF23,
135         "F24":            tcell.KeyF24,
136         "F25":            tcell.KeyF25,
137         "F26":            tcell.KeyF26,
138         "F27":            tcell.KeyF27,
139         "F28":            tcell.KeyF28,
140         "F29":            tcell.KeyF29,
141         "F30":            tcell.KeyF30,
142         "F31":            tcell.KeyF31,
143         "F32":            tcell.KeyF32,
144         "F33":            tcell.KeyF33,
145         "F34":            tcell.KeyF34,
146         "F35":            tcell.KeyF35,
147         "F36":            tcell.KeyF36,
148         "F37":            tcell.KeyF37,
149         "F38":            tcell.KeyF38,
150         "F39":            tcell.KeyF39,
151         "F40":            tcell.KeyF40,
152         "F41":            tcell.KeyF41,
153         "F42":            tcell.KeyF42,
154         "F43":            tcell.KeyF43,
155         "F44":            tcell.KeyF44,
156         "F45":            tcell.KeyF45,
157         "F46":            tcell.KeyF46,
158         "F47":            tcell.KeyF47,
159         "F48":            tcell.KeyF48,
160         "F49":            tcell.KeyF49,
161         "F50":            tcell.KeyF50,
162         "F51":            tcell.KeyF51,
163         "F52":            tcell.KeyF52,
164         "F53":            tcell.KeyF53,
165         "F54":            tcell.KeyF54,
166         "F55":            tcell.KeyF55,
167         "F56":            tcell.KeyF56,
168         "F57":            tcell.KeyF57,
169         "F58":            tcell.KeyF58,
170         "F59":            tcell.KeyF59,
171         "F60":            tcell.KeyF60,
172         "F61":            tcell.KeyF61,
173         "F62":            tcell.KeyF62,
174         "F63":            tcell.KeyF63,
175         "F64":            tcell.KeyF64,
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,
208         "Backspace":      tcell.KeyBackspace,
209         "Tab":            tcell.KeyTab,
210         "Esc":            tcell.KeyEsc,
211         "Escape":         tcell.KeyEscape,
212         "Enter":          tcell.KeyEnter,
213         "Backspace2":     tcell.KeyBackspace2,
214
215         // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
216         "PgUp":   tcell.KeyPgUp,
217         "PgDown": tcell.KeyPgDn,
218 }
219
220 // The Key struct holds the data for a keypress (keycode + modifiers)
221 type Key struct {
222         keyCode   tcell.Key
223         modifiers tcell.ModMask
224         r         rune
225 }
226
227 // InitBindings initializes the keybindings for micro
228 func InitBindings() {
229         bindings = make(map[Key][]func(*View, bool) bool)
230
231         var parsed map[string]string
232         defaults := DefaultBindings()
233
234         filename := configDir + "/bindings.json"
235         if _, e := os.Stat(filename); e == nil {
236                 input, err := ioutil.ReadFile(filename)
237                 if err != nil {
238                         TermMessage("Error reading bindings.json file: " + err.Error())
239                         return
240                 }
241
242                 err = json5.Unmarshal(input, &parsed)
243                 if err != nil {
244                         TermMessage("Error reading bindings.json:", err.Error())
245                 }
246         }
247
248         parseBindings(defaults)
249         parseBindings(parsed)
250 }
251
252 func parseBindings(userBindings map[string]string) {
253         for k, v := range userBindings {
254                 BindKey(k, v)
255         }
256 }
257
258 // findKey will find binding Key 'b' using string 'k'
259 func findKey(k string) (b Key, ok bool) {
260         modifiers := tcell.ModNone
261
262         // First, we'll strip off all the modifiers in the name and add them to the
263         // ModMask
264 modSearch:
265         for {
266                 switch {
267                 case strings.HasPrefix(k, "-"):
268                         // We optionally support dashes between modifiers
269                         k = k[1:]
270                 case strings.HasPrefix(k, "Ctrl"):
271                         k = k[4:]
272                         modifiers |= tcell.ModCtrl
273                 case strings.HasPrefix(k, "Alt"):
274                         k = k[3:]
275                         modifiers |= tcell.ModAlt
276                 case strings.HasPrefix(k, "Shift"):
277                         k = k[5:]
278                         modifiers |= tcell.ModShift
279                 default:
280                         break modSearch
281                 }
282         }
283
284         // Control is handled specially, since some character codes in bindingKeys
285         // are different when Control is depressed. We should check for Control keys
286         // first.
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.
291                         return Key{
292                                 keyCode:   code,
293                                 modifiers: modifiers,
294                                 r:         0,
295                         }, true
296                 }
297         }
298
299         // See if we can find the key in bindingKeys
300         if code, ok := bindingKeys[k]; ok {
301                 return Key{
302                         keyCode:   code,
303                         modifiers: modifiers,
304                         r:         0,
305                 }, true
306         }
307
308         // If we were given one character, then we've got a rune.
309         if len(k) == 1 {
310                 return Key{
311                         keyCode:   tcell.KeyRune,
312                         modifiers: modifiers,
313                         r:         rune(k[0]),
314                 }, true
315         }
316
317         // We don't know what happened.
318         return Key{}, false
319 }
320
321 // findAction will find 'action' using string 'v'
322 func findAction(v string) (action func(*View, bool) bool) {
323         action, ok := bindingActions[v]
324         if !ok {
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)
328         }
329         return action
330 }
331
332 // BindKey takes a key and an action and binds the two together
333 func BindKey(k, v string) {
334         key, ok := findKey(k)
335         if !ok {
336                 return
337         }
338         if v == "ToggleHelp" {
339                 helpBinding = k
340         }
341
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))
346         }
347
348         bindings[key] = actions
349 }
350
351 // DefaultBindings returns a map containing micro's default keybindings
352 func DefaultBindings() map[string]string {
353         return map[string]string{
354                 "Up":             "CursorUp",
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                 "Backspace":      "Backspace",
377                 "Backspace2":     "Backspace",
378                 "Alt-Backspace":  "DeleteWordLeft",
379                 "Alt-Backspace2": "DeleteWordLeft",
380                 "Tab":            "IndentSelection,InsertTab",
381                 "Backtab":        "OutdentSelection",
382                 "CtrlO":          "OpenFile",
383                 "CtrlS":          "Save",
384                 "CtrlF":          "Find",
385                 "CtrlN":          "FindNext",
386                 "CtrlP":          "FindPrevious",
387                 "CtrlZ":          "Undo",
388                 "CtrlY":          "Redo",
389                 "CtrlC":          "Copy",
390                 "CtrlX":          "Cut",
391                 "CtrlK":          "CutLine",
392                 "CtrlD":          "DuplicateLine",
393                 "CtrlV":          "Paste",
394                 "CtrlA":          "SelectAll",
395                 "CtrlT":          "AddTab",
396                 "CtrlRightSq":    "PreviousTab",
397                 "CtrlBackslash":  "NextTab",
398                 "Home":           "StartOfLine",
399                 "End":            "EndOfLine",
400                 "CtrlHome":       "CursorStart",
401                 "CtrlEnd":        "CursorEnd",
402                 "PageUp":         "CursorPageUp",
403                 "PageDown":       "CursorPageDown",
404                 "CtrlG":          "ToggleHelp",
405                 "CtrlR":          "ToggleRuler",
406                 "CtrlL":          "JumpLine",
407                 "Delete":         "Delete",
408                 "Esc":            "ClearStatus",
409                 "CtrlB":          "ShellMode",
410                 "CtrlQ":          "Quit",
411                 "CtrlE":          "CommandMode",
412                 "CtrlW":          "NextSplit",
413                 "CtrlU":          "ToggleMacro",
414                 "CtrlJ":          "PlayMacro",
415
416                 // Emacs-style keybindings
417                 "Alt-f": "WordRight",
418                 "Alt-b": "WordLeft",
419                 "Alt-a": "StartOfLine",
420                 "Alt-e": "EndOfLine",
421                 "Alt-p": "CursorUp",
422                 "Alt-n": "CursorDown",
423         }
424 }