]> git.lizzy.rs Git - micro.git/blob - cmd/micro/bindings.go
a55286d0b9d1a02105b3fa5527c7de6f1d064ea6
[micro.git] / cmd / micro / bindings.go
1 package main
2
3 import (
4         "io/ioutil"
5         "os"
6         "strings"
7
8         "github.com/zyedidia/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         "SaveAs":              (*View).SaveAs,
45         "Find":                (*View).Find,
46         "FindNext":            (*View).FindNext,
47         "FindPrevious":        (*View).FindPrevious,
48         "Center":              (*View).Center,
49         "Undo":                (*View).Undo,
50         "Redo":                (*View).Redo,
51         "Copy":                (*View).Copy,
52         "Cut":                 (*View).Cut,
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,
66         "End":                 (*View).End,
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,
80         "Quit":                (*View).Quit,
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,
92
93         // This was changed to InsertNewline but I don't want to break backwards compatibility
94         "InsertEnter": (*View).InsertNewline,
95 }
96
97 var bindingKeys = map[string]tcell.Key{
98         "Up":             tcell.KeyUp,
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,
110         "End":            tcell.KeyEnd,
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,
120         "F1":             tcell.KeyF1,
121         "F2":             tcell.KeyF2,
122         "F3":             tcell.KeyF3,
123         "F4":             tcell.KeyF4,
124         "F5":             tcell.KeyF5,
125         "F6":             tcell.KeyF6,
126         "F7":             tcell.KeyF7,
127         "F8":             tcell.KeyF8,
128         "F9":             tcell.KeyF9,
129         "F10":            tcell.KeyF10,
130         "F11":            tcell.KeyF11,
131         "F12":            tcell.KeyF12,
132         "F13":            tcell.KeyF13,
133         "F14":            tcell.KeyF14,
134         "F15":            tcell.KeyF15,
135         "F16":            tcell.KeyF16,
136         "F17":            tcell.KeyF17,
137         "F18":            tcell.KeyF18,
138         "F19":            tcell.KeyF19,
139         "F20":            tcell.KeyF20,
140         "F21":            tcell.KeyF21,
141         "F22":            tcell.KeyF22,
142         "F23":            tcell.KeyF23,
143         "F24":            tcell.KeyF24,
144         "F25":            tcell.KeyF25,
145         "F26":            tcell.KeyF26,
146         "F27":            tcell.KeyF27,
147         "F28":            tcell.KeyF28,
148         "F29":            tcell.KeyF29,
149         "F30":            tcell.KeyF30,
150         "F31":            tcell.KeyF31,
151         "F32":            tcell.KeyF32,
152         "F33":            tcell.KeyF33,
153         "F34":            tcell.KeyF34,
154         "F35":            tcell.KeyF35,
155         "F36":            tcell.KeyF36,
156         "F37":            tcell.KeyF37,
157         "F38":            tcell.KeyF38,
158         "F39":            tcell.KeyF39,
159         "F40":            tcell.KeyF40,
160         "F41":            tcell.KeyF41,
161         "F42":            tcell.KeyF42,
162         "F43":            tcell.KeyF43,
163         "F44":            tcell.KeyF44,
164         "F45":            tcell.KeyF45,
165         "F46":            tcell.KeyF46,
166         "F47":            tcell.KeyF47,
167         "F48":            tcell.KeyF48,
168         "F49":            tcell.KeyF49,
169         "F50":            tcell.KeyF50,
170         "F51":            tcell.KeyF51,
171         "F52":            tcell.KeyF52,
172         "F53":            tcell.KeyF53,
173         "F54":            tcell.KeyF54,
174         "F55":            tcell.KeyF55,
175         "F56":            tcell.KeyF56,
176         "F57":            tcell.KeyF57,
177         "F58":            tcell.KeyF58,
178         "F59":            tcell.KeyF59,
179         "F60":            tcell.KeyF60,
180         "F61":            tcell.KeyF61,
181         "F62":            tcell.KeyF62,
182         "F63":            tcell.KeyF63,
183         "F64":            tcell.KeyF64,
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,
216         "Tab":            tcell.KeyTab,
217         "Esc":            tcell.KeyEsc,
218         "Escape":         tcell.KeyEscape,
219         "Enter":          tcell.KeyEnter,
220         "Backspace":      tcell.KeyBackspace2,
221
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,
225 }
226
227 // The Key struct holds the data for a keypress (keycode + modifiers)
228 type Key struct {
229         keyCode   tcell.Key
230         modifiers tcell.ModMask
231         r         rune
232 }
233
234 // InitBindings initializes the keybindings for micro
235 func InitBindings() {
236         bindings = make(map[Key][]func(*View, bool) bool)
237
238         var parsed map[string]string
239         defaults := DefaultBindings()
240
241         filename := configDir + "/bindings.json"
242         if _, e := os.Stat(filename); e == nil {
243                 input, err := ioutil.ReadFile(filename)
244                 if err != nil {
245                         TermMessage("Error reading bindings.json file: " + err.Error())
246                         return
247                 }
248
249                 err = json5.Unmarshal(input, &parsed)
250                 if err != nil {
251                         TermMessage("Error reading bindings.json:", err.Error())
252                 }
253         }
254
255         parseBindings(defaults)
256         parseBindings(parsed)
257 }
258
259 func parseBindings(userBindings map[string]string) {
260         for k, v := range userBindings {
261                 BindKey(k, v)
262         }
263 }
264
265 // findKey will find binding Key 'b' using string 'k'
266 func findKey(k string) (b Key, ok bool) {
267         modifiers := tcell.ModNone
268
269         // First, we'll strip off all the modifiers in the name and add them to the
270         // ModMask
271 modSearch:
272         for {
273                 switch {
274                 case strings.HasPrefix(k, "-"):
275                         // We optionally support dashes between modifiers
276                         k = k[1:]
277                 case strings.HasPrefix(k, "Ctrl") && k != "CtrlH":
278                         // CtrlH technically does not have a 'Ctrl' modifier because it is really backspace
279                         k = k[4:]
280                         modifiers |= tcell.ModCtrl
281                 case strings.HasPrefix(k, "Alt"):
282                         k = k[3:]
283                         modifiers |= tcell.ModAlt
284                 case strings.HasPrefix(k, "Shift"):
285                         k = k[5:]
286                         modifiers |= tcell.ModShift
287                 default:
288                         break modSearch
289                 }
290         }
291
292         // Control is handled specially, since some character codes in bindingKeys
293         // are different when Control is depressed. We should check for Control keys
294         // first.
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.
299                         return Key{
300                                 keyCode:   code,
301                                 modifiers: modifiers,
302                                 r:         0,
303                         }, true
304                 }
305         }
306
307         // See if we can find the key in bindingKeys
308         if code, ok := bindingKeys[k]; ok {
309                 return Key{
310                         keyCode:   code,
311                         modifiers: modifiers,
312                         r:         0,
313                 }, true
314         }
315
316         // If we were given one character, then we've got a rune.
317         if len(k) == 1 {
318                 return Key{
319                         keyCode:   tcell.KeyRune,
320                         modifiers: modifiers,
321                         r:         rune(k[0]),
322                 }, true
323         }
324
325         // We don't know what happened.
326         return Key{}, false
327 }
328
329 // findAction will find 'action' using string 'v'
330 func findAction(v string) (action func(*View, bool) bool) {
331         action, ok := bindingActions[v]
332         if !ok {
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)
336         }
337         return action
338 }
339
340 // BindKey takes a key and an action and binds the two together
341 func BindKey(k, v string) {
342         key, ok := findKey(k)
343         if !ok {
344                 TermMessage("Unknown keybinding: " + k)
345                 return
346         }
347         if v == "ToggleHelp" {
348                 helpBinding = k
349         }
350         if helpBinding == k && v != "ToggleHelp" {
351                 helpBinding = ""
352         }
353
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)
359                 } else {
360                         actionNames = append(actionNames[:0], actionNames[1:]...)
361                 }
362         }
363         actions := make([]func(*View, bool) bool, 0, len(actionNames))
364         for _, actionName := range actionNames {
365                 actions = append(actions, findAction(actionName))
366         }
367
368         bindings[key] = actions
369 }
370
371 // DefaultBindings returns a map containing micro's default keybindings
372 func DefaultBindings() map[string]string {
373         return map[string]string{
374                 "Up":             "CursorUp",
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                 "ShiftHome":      "SelectToStartOfLine",
392                 "CtrlShiftRight": "SelectToEndOfLine",
393                 "ShiftEnd":       "SelectToEndOfLine",
394                 "CtrlUp":         "CursorStart",
395                 "CtrlDown":       "CursorEnd",
396                 "CtrlShiftUp":    "SelectToStart",
397                 "CtrlShiftDown":  "SelectToEnd",
398                 "Enter":          "InsertNewline",
399                 "CtrlH":          "Backspace",
400                 "Backspace":      "Backspace",
401                 "Alt-CtrlH":      "DeleteWordLeft",
402                 "Alt-Backspace":  "DeleteWordLeft",
403                 "Tab":            "IndentSelection,InsertTab",
404                 "Backtab":        "OutdentSelection,OutdentLine",
405                 "CtrlO":          "OpenFile",
406                 "CtrlS":          "Save",
407                 "CtrlF":          "Find",
408                 "CtrlN":          "FindNext",
409                 "CtrlP":          "FindPrevious",
410                 "CtrlZ":          "Undo",
411                 "CtrlY":          "Redo",
412                 "CtrlC":          "Copy",
413                 "CtrlX":          "Cut",
414                 "CtrlK":          "CutLine",
415                 "CtrlD":          "DuplicateLine",
416                 "CtrlV":          "Paste",
417                 "CtrlA":          "SelectAll",
418                 "CtrlT":          "AddTab",
419                 "Alt,":           "PreviousTab",
420                 "Alt.":           "NextTab",
421                 "Home":           "StartOfLine",
422                 "End":            "EndOfLine",
423                 "CtrlHome":       "CursorStart",
424                 "CtrlEnd":        "CursorEnd",
425                 "PageUp":         "CursorPageUp",
426                 "PageDown":       "CursorPageDown",
427                 "CtrlG":          "ToggleHelp",
428                 "CtrlR":          "ToggleRuler",
429                 "CtrlL":          "JumpLine",
430                 "Delete":         "Delete",
431                 "CtrlB":          "ShellMode",
432                 "CtrlQ":          "Quit",
433                 "CtrlE":          "CommandMode",
434                 "CtrlW":          "NextSplit",
435                 "CtrlU":          "ToggleMacro",
436                 "CtrlJ":          "PlayMacro",
437
438                 // Emacs-style keybindings
439                 "Alt-f": "WordRight",
440                 "Alt-b": "WordLeft",
441                 "Alt-a": "StartOfLine",
442                 "Alt-e": "EndOfLine",
443                 "Alt-p": "CursorUp",
444                 "Alt-n": "CursorDown",
445
446                 // Integration with file managers
447                 "F1":  "ToggleHelp",
448                 "F2":  "Save",
449                 "F3":  "Find",
450                 "F4":  "Quit",
451                 "F7":  "Find",
452                 "F10": "Quit",
453                 "Esc": "Escape",
454         }
455 }