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