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