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