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