]> git.lizzy.rs Git - micro.git/blob - cmd/micro/bindings.go
Merge pull request #282 from boombuler/windows
[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         "SelectAll":           (*View).SelectAll,
59         "OpenFile":            (*View).OpenFile,
60         "Start":               (*View).Start,
61         "End":                 (*View).End,
62         "PageUp":              (*View).PageUp,
63         "PageDown":            (*View).PageDown,
64         "HalfPageUp":          (*View).HalfPageUp,
65         "HalfPageDown":        (*View).HalfPageDown,
66         "StartOfLine":         (*View).StartOfLine,
67         "EndOfLine":           (*View).EndOfLine,
68         "ToggleHelp":          (*View).ToggleHelp,
69         "ToggleRuler":         (*View).ToggleRuler,
70         "JumpLine":            (*View).JumpLine,
71         "ClearStatus":         (*View).ClearStatus,
72         "ShellMode":           (*View).ShellMode,
73         "CommandMode":         (*View).CommandMode,
74         "Quit":                (*View).Quit,
75         "QuitAll":             (*View).QuitAll,
76         "AddTab":              (*View).AddTab,
77         "PreviousTab":         (*View).PreviousTab,
78         "NextTab":             (*View).NextTab,
79         "NextSplit":           (*View).NextSplit,
80         "PreviousSplit":       (*View).PreviousSplit,
81
82         // This was changed to InsertNewline but I don't want to break backwards compatibility
83         "InsertEnter": (*View).InsertNewline,
84 }
85
86 var bindingKeys = map[string]tcell.Key{
87         "Up":             tcell.KeyUp,
88         "Down":           tcell.KeyDown,
89         "Right":          tcell.KeyRight,
90         "Left":           tcell.KeyLeft,
91         "UpLeft":         tcell.KeyUpLeft,
92         "UpRight":        tcell.KeyUpRight,
93         "DownLeft":       tcell.KeyDownLeft,
94         "DownRight":      tcell.KeyDownRight,
95         "Center":         tcell.KeyCenter,
96         "PageUp":         tcell.KeyPgUp,
97         "PageDown":       tcell.KeyPgDn,
98         "Home":           tcell.KeyHome,
99         "End":            tcell.KeyEnd,
100         "Insert":         tcell.KeyInsert,
101         "Delete":         tcell.KeyDelete,
102         "Help":           tcell.KeyHelp,
103         "Exit":           tcell.KeyExit,
104         "Clear":          tcell.KeyClear,
105         "Cancel":         tcell.KeyCancel,
106         "Print":          tcell.KeyPrint,
107         "Pause":          tcell.KeyPause,
108         "Backtab":        tcell.KeyBacktab,
109         "F1":             tcell.KeyF1,
110         "F2":             tcell.KeyF2,
111         "F3":             tcell.KeyF3,
112         "F4":             tcell.KeyF4,
113         "F5":             tcell.KeyF5,
114         "F6":             tcell.KeyF6,
115         "F7":             tcell.KeyF7,
116         "F8":             tcell.KeyF8,
117         "F9":             tcell.KeyF9,
118         "F10":            tcell.KeyF10,
119         "F11":            tcell.KeyF11,
120         "F12":            tcell.KeyF12,
121         "F13":            tcell.KeyF13,
122         "F14":            tcell.KeyF14,
123         "F15":            tcell.KeyF15,
124         "F16":            tcell.KeyF16,
125         "F17":            tcell.KeyF17,
126         "F18":            tcell.KeyF18,
127         "F19":            tcell.KeyF19,
128         "F20":            tcell.KeyF20,
129         "F21":            tcell.KeyF21,
130         "F22":            tcell.KeyF22,
131         "F23":            tcell.KeyF23,
132         "F24":            tcell.KeyF24,
133         "F25":            tcell.KeyF25,
134         "F26":            tcell.KeyF26,
135         "F27":            tcell.KeyF27,
136         "F28":            tcell.KeyF28,
137         "F29":            tcell.KeyF29,
138         "F30":            tcell.KeyF30,
139         "F31":            tcell.KeyF31,
140         "F32":            tcell.KeyF32,
141         "F33":            tcell.KeyF33,
142         "F34":            tcell.KeyF34,
143         "F35":            tcell.KeyF35,
144         "F36":            tcell.KeyF36,
145         "F37":            tcell.KeyF37,
146         "F38":            tcell.KeyF38,
147         "F39":            tcell.KeyF39,
148         "F40":            tcell.KeyF40,
149         "F41":            tcell.KeyF41,
150         "F42":            tcell.KeyF42,
151         "F43":            tcell.KeyF43,
152         "F44":            tcell.KeyF44,
153         "F45":            tcell.KeyF45,
154         "F46":            tcell.KeyF46,
155         "F47":            tcell.KeyF47,
156         "F48":            tcell.KeyF48,
157         "F49":            tcell.KeyF49,
158         "F50":            tcell.KeyF50,
159         "F51":            tcell.KeyF51,
160         "F52":            tcell.KeyF52,
161         "F53":            tcell.KeyF53,
162         "F54":            tcell.KeyF54,
163         "F55":            tcell.KeyF55,
164         "F56":            tcell.KeyF56,
165         "F57":            tcell.KeyF57,
166         "F58":            tcell.KeyF58,
167         "F59":            tcell.KeyF59,
168         "F60":            tcell.KeyF60,
169         "F61":            tcell.KeyF61,
170         "F62":            tcell.KeyF62,
171         "F63":            tcell.KeyF63,
172         "F64":            tcell.KeyF64,
173         "CtrlSpace":      tcell.KeyCtrlSpace,
174         "CtrlA":          tcell.KeyCtrlA,
175         "CtrlB":          tcell.KeyCtrlB,
176         "CtrlC":          tcell.KeyCtrlC,
177         "CtrlD":          tcell.KeyCtrlD,
178         "CtrlE":          tcell.KeyCtrlE,
179         "CtrlF":          tcell.KeyCtrlF,
180         "CtrlG":          tcell.KeyCtrlG,
181         "CtrlH":          tcell.KeyCtrlH,
182         "CtrlI":          tcell.KeyCtrlI,
183         "CtrlJ":          tcell.KeyCtrlJ,
184         "CtrlK":          tcell.KeyCtrlK,
185         "CtrlL":          tcell.KeyCtrlL,
186         "CtrlM":          tcell.KeyCtrlM,
187         "CtrlN":          tcell.KeyCtrlN,
188         "CtrlO":          tcell.KeyCtrlO,
189         "CtrlP":          tcell.KeyCtrlP,
190         "CtrlQ":          tcell.KeyCtrlQ,
191         "CtrlR":          tcell.KeyCtrlR,
192         "CtrlS":          tcell.KeyCtrlS,
193         "CtrlT":          tcell.KeyCtrlT,
194         "CtrlU":          tcell.KeyCtrlU,
195         "CtrlV":          tcell.KeyCtrlV,
196         "CtrlW":          tcell.KeyCtrlW,
197         "CtrlX":          tcell.KeyCtrlX,
198         "CtrlY":          tcell.KeyCtrlY,
199         "CtrlZ":          tcell.KeyCtrlZ,
200         "CtrlLeftSq":     tcell.KeyCtrlLeftSq,
201         "CtrlBackslash":  tcell.KeyCtrlBackslash,
202         "CtrlRightSq":    tcell.KeyCtrlRightSq,
203         "CtrlCarat":      tcell.KeyCtrlCarat,
204         "CtrlUnderscore": tcell.KeyCtrlUnderscore,
205         "Backspace":      tcell.KeyBackspace,
206         "Tab":            tcell.KeyTab,
207         "Esc":            tcell.KeyEsc,
208         "Escape":         tcell.KeyEscape,
209         "Enter":          tcell.KeyEnter,
210         "Backspace2":     tcell.KeyBackspace2,
211
212         // I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
213         "PgUp":   tcell.KeyPgUp,
214         "PgDown": tcell.KeyPgDn,
215 }
216
217 // The Key struct holds the data for a keypress (keycode + modifiers)
218 type Key struct {
219         keyCode   tcell.Key
220         modifiers tcell.ModMask
221         r         rune
222 }
223
224 // InitBindings initializes the keybindings for micro
225 func InitBindings() {
226         bindings = make(map[Key][]func(*View, bool) bool)
227
228         var parsed map[string]string
229         defaults := DefaultBindings()
230
231         filename := configDir + "/bindings.json"
232         if _, e := os.Stat(filename); e == nil {
233                 input, err := ioutil.ReadFile(filename)
234                 if err != nil {
235                         TermMessage("Error reading bindings.json file: " + err.Error())
236                         return
237                 }
238
239                 err = json5.Unmarshal(input, &parsed)
240                 if err != nil {
241                         TermMessage("Error reading bindings.json:", err.Error())
242                 }
243         }
244
245         parseBindings(defaults)
246         parseBindings(parsed)
247 }
248
249 func parseBindings(userBindings map[string]string) {
250         for k, v := range userBindings {
251                 BindKey(k, v)
252         }
253 }
254
255 // findKey will find binding Key 'b' using string 'k'
256 func findKey(k string) (b Key, ok bool) {
257         modifiers := tcell.ModNone
258
259         // First, we'll strip off all the modifiers in the name and add them to the
260         // ModMask
261 modSearch:
262         for {
263                 switch {
264                 case strings.HasPrefix(k, "-"):
265                         // We optionally support dashes between modifiers
266                         k = k[1:]
267                 case strings.HasPrefix(k, "Ctrl"):
268                         k = k[4:]
269                         modifiers |= tcell.ModCtrl
270                 case strings.HasPrefix(k, "Alt"):
271                         k = k[3:]
272                         modifiers |= tcell.ModAlt
273                 case strings.HasPrefix(k, "Shift"):
274                         k = k[5:]
275                         modifiers |= tcell.ModShift
276                 default:
277                         break modSearch
278                 }
279         }
280
281         // Control is handled specially, since some character codes in bindingKeys
282         // are different when Control is depressed. We should check for Control keys
283         // first.
284         if modifiers&tcell.ModCtrl != 0 {
285                 // see if the key is in bindingKeys with the Ctrl prefix.
286                 if code, ok := bindingKeys["Ctrl"+k]; ok {
287                         // It is, we're done.
288                         return Key{
289                                 keyCode:   code,
290                                 modifiers: modifiers,
291                                 r:         0,
292                         }, true
293                 }
294         }
295
296         // See if we can find the key in bindingKeys
297         if code, ok := bindingKeys[k]; ok {
298                 return Key{
299                         keyCode:   code,
300                         modifiers: modifiers,
301                         r:         0,
302                 }, true
303         }
304
305         // If we were given one character, then we've got a rune.
306         if len(k) == 1 {
307                 return Key{
308                         keyCode:   tcell.KeyRune,
309                         modifiers: modifiers,
310                         r:         rune(k[0]),
311                 }, true
312         }
313
314         // We don't know what happened.
315         return Key{}, false
316 }
317
318 // findAction will find 'action' using string 'v'
319 func findAction(v string) (action func(*View, bool) bool) {
320         action, ok := bindingActions[v]
321         if !ok {
322                 // If the user seems to be binding a function that doesn't exist
323                 // We hope that it's a lua function that exists and bind it to that
324                 action = LuaFunctionBinding(v)
325         }
326         return action
327 }
328
329 // BindKey takes a key and an action and binds the two together
330 func BindKey(k, v string) {
331         key, ok := findKey(k)
332         if !ok {
333                 return
334         }
335         if v == "ToggleHelp" {
336                 helpBinding = k
337         }
338
339         actionNames := strings.Split(v, ",")
340         actions := make([]func(*View, bool) bool, 0, len(actionNames))
341         for _, actionName := range actionNames {
342                 actions = append(actions, findAction(actionName))
343         }
344
345         bindings[key] = actions
346 }
347
348 // DefaultBindings returns a map containing micro's default keybindings
349 func DefaultBindings() map[string]string {
350         return map[string]string{
351                 "Up":             "CursorUp",
352                 "Down":           "CursorDown",
353                 "Right":          "CursorRight",
354                 "Left":           "CursorLeft",
355                 "ShiftUp":        "SelectUp",
356                 "ShiftDown":      "SelectDown",
357                 "ShiftLeft":      "SelectLeft",
358                 "ShiftRight":     "SelectRight",
359                 "AltLeft":        "WordLeft",
360                 "AltRight":       "WordRight",
361                 "AltShiftRight":  "SelectWordRight",
362                 "AltShiftLeft":   "SelectWordLeft",
363                 "CtrlLeft":       "StartOfLine",
364                 "CtrlRight":      "EndOfLine",
365                 "CtrlShiftLeft":  "SelectToStartOfLine",
366                 "CtrlShiftRight": "SelectToEndOfLine",
367                 "CtrlUp":         "CursorStart",
368                 "CtrlDown":       "CursorEnd",
369                 "CtrlShiftUp":    "SelectToStart",
370                 "CtrlShiftDown":  "SelectToEnd",
371                 "Enter":          "InsertNewline",
372                 "Space":          "InsertSpace",
373                 "Backspace":      "Backspace",
374                 "Backspace2":     "Backspace",
375                 "Alt-Backspace":  "DeleteWordLeft",
376                 "Alt-Backspace2": "DeleteWordLeft",
377                 "Tab":            "IndentSelection,InsertTab",
378                 "Backtab":        "OutdentSelection",
379                 "CtrlO":          "OpenFile",
380                 "CtrlS":          "Save",
381                 "CtrlF":          "Find",
382                 "CtrlN":          "FindNext",
383                 "CtrlP":          "FindPrevious",
384                 "CtrlZ":          "Undo",
385                 "CtrlY":          "Redo",
386                 "CtrlC":          "Copy",
387                 "CtrlX":          "Cut",
388                 "CtrlK":          "CutLine",
389                 "CtrlD":          "DuplicateLine",
390                 "CtrlV":          "Paste",
391                 "CtrlA":          "SelectAll",
392                 "CtrlT":          "AddTab",
393                 "CtrlRightSq":    "PreviousTab",
394                 "CtrlBackslash":  "NextTab",
395                 "Home":           "StartOfLine",
396                 "End":            "EndOfLine",
397                 "CtrlHome":       "CursorStart",
398                 "CtrlEnd":        "CursorEnd",
399                 "PageUp":         "CursorPageUp",
400                 "PageDown":       "CursorPageDown",
401                 "CtrlG":          "ToggleHelp",
402                 "CtrlR":          "ToggleRuler",
403                 "CtrlL":          "JumpLine",
404                 "Delete":         "Delete",
405                 "Esc":            "ClearStatus",
406                 "CtrlB":          "ShellMode",
407                 "CtrlQ":          "Quit",
408                 "CtrlE":          "CommandMode",
409                 "CtrlW":          "NextSplit",
410
411                 // Emacs-style keybindings
412                 "Alt-f": "WordRight",
413                 "Alt-b": "WordLeft",
414                 "Alt-a": "StartOfLine",
415                 "Alt-e": "EndOfLine",
416                 "Alt-p": "CursorUp",
417                 "Alt-n": "CursorDown",
418         }
419 }