-
Notifications
You must be signed in to change notification settings - Fork 11
/
bindings.go
479 lines (455 loc) · 16.7 KB
/
bindings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package femto
import (
"strings"
"unicode"
"github.com/gdamore/tcell/v2"
)
// Actions
const (
ActionCursorUp = "CursorUp"
ActionCursorDown = "CursorDown"
ActionCursorPageUp = "CursorPageUp"
ActionCursorPageDown = "CursorPageDown"
ActionCursorLeft = "CursorLeft"
ActionCursorRight = "CursorRight"
ActionCursorStart = "CursorStart"
ActionCursorEnd = "CursorEnd"
ActionSelectToStart = "SelectToStart"
ActionSelectToEnd = "SelectToEnd"
ActionSelectUp = "SelectUp"
ActionSelectDown = "SelectDown"
ActionSelectLeft = "SelectLeft"
ActionSelectRight = "SelectRight"
ActionWordRight = "WordRight"
ActionWordLeft = "WordLeft"
ActionSelectWordRight = "SelectWordRight"
ActionSelectWordLeft = "SelectWordLeft"
ActionDeleteWordRight = "DeleteWordRight"
ActionDeleteWordLeft = "DeleteWordLeft"
ActionSelectLine = "SelectLine"
ActionSelectToStartOfLine = "SelectToStartOfLine"
ActionSelectToEndOfLine = "SelectToEndOfLine"
ActionParagraphPrevious = "ParagraphPrevious"
ActionParagraphNext = "ParagraphNext"
ActionInsertNewline = "InsertNewline"
ActionInsertSpace = "InsertSpace"
ActionBackspace = "Backspace"
ActionDelete = "Delete"
ActionInsertTab = "InsertTab"
ActionCenter = "Center"
ActionUndo = "Undo"
ActionRedo = "Redo"
ActionCopy = "Copy"
ActionCut = "Cut"
ActionCutLine = "CutLine"
ActionDuplicateLine = "DuplicateLine"
ActionDeleteLine = "DeleteLine"
ActionMoveLinesUp = "MoveLinesUp"
ActionMoveLinesDown = "MoveLinesDown"
ActionIndentSelection = "IndentSelection"
ActionOutdentSelection = "OutdentSelection"
ActionOutdentLine = "OutdentLine"
ActionPaste = "Paste"
ActionSelectAll = "SelectAll"
ActionStart = "Start"
ActionEnd = "End"
ActionPageUp = "PageUp"
ActionPageDown = "PageDown"
ActionSelectPageUp = "SelectPageUp"
ActionSelectPageDown = "SelectPageDown"
ActionHalfPageUp = "HalfPageUp"
ActionHalfPageDown = "HalfPageDown"
ActionStartOfLine = "StartOfLine"
ActionEndOfLine = "EndOfLine"
ActionToggleRuler = "ToggleRuler"
ActionToggleOverwriteMode = "ToggleOverwriteMode"
ActionEscape = "Escape"
ActionScrollUp = "ScrollUp"
ActionScrollDown = "ScrollDown"
ActionSpawnMultiCursor = "SpawnMultiCursor"
ActionSpawnMultiCursorSelect = "SpawnMultiCursorSelect"
ActionRemoveMultiCursor = "RemoveMultiCursor"
ActionRemoveAllMultiCursors = "RemoveAllMultiCursors"
ActionSkipMultiCursor = "SkipMultiCursor"
ActionJumpToMatchingBrace = "JumpToMatchingBrace"
ActionInsertEnter = "InsertEnter"
ActionUnbindKey = "UnbindKey"
)
// keyDesc holds the data for a keypress (keycode + modifiers)
type keyDesc struct {
keyCode tcell.Key
modifiers tcell.ModMask
r rune
}
// KeyBindings associates key presses with view actions.
type KeyBindings map[keyDesc][]func(*View) bool
// NewKeyBindings returns a new set of keybindings from the given set of binding descriptions.
func NewKeyBindings(bindings map[string]string) KeyBindings {
return make(KeyBindings).BindKeys(bindings)
}
// BindKey binds a key to a list of actions. If the key is not found or if any action is not found, this function has
// no effect.
func (bindings KeyBindings) BindKey(key string, actions string) KeyBindings {
k, ok := findKey(key)
if !ok {
return bindings
}
actionNames := strings.Split(actions, ",")
if actionNames[0] == "UnbindKey" {
delete(bindings, k)
if len(actionNames) == 1 {
return bindings
}
actionNames = append(actionNames[:0], actionNames[1:]...)
}
acts := make([]func(*View) bool, 0, len(actionNames))
for _, actionName := range actionNames {
action := findAction(actionName)
if action == nil {
return bindings
}
acts = append(acts, action)
}
if len(acts) > 0 {
bindings[k] = acts
}
return bindings
}
// BindKeys binds a set of keys to actions.
func (bindings KeyBindings) BindKeys(keys map[string]string) KeyBindings {
for k, v := range keys {
bindings.BindKey(k, v)
}
return bindings
}
var bindingActions = map[string]func(*View) bool{
ActionCursorUp: (*View).CursorUp,
ActionCursorDown: (*View).CursorDown,
ActionCursorPageUp: (*View).CursorPageUp,
ActionCursorPageDown: (*View).CursorPageDown,
ActionCursorLeft: (*View).CursorLeft,
ActionCursorRight: (*View).CursorRight,
ActionCursorStart: (*View).CursorStart,
ActionCursorEnd: (*View).CursorEnd,
ActionSelectToStart: (*View).SelectToStart,
ActionSelectToEnd: (*View).SelectToEnd,
ActionSelectUp: (*View).SelectUp,
ActionSelectDown: (*View).SelectDown,
ActionSelectLeft: (*View).SelectLeft,
ActionSelectRight: (*View).SelectRight,
ActionWordRight: (*View).WordRight,
ActionWordLeft: (*View).WordLeft,
ActionSelectWordRight: (*View).SelectWordRight,
ActionSelectWordLeft: (*View).SelectWordLeft,
ActionDeleteWordRight: (*View).DeleteWordRight,
ActionDeleteWordLeft: (*View).DeleteWordLeft,
ActionSelectLine: (*View).SelectLine,
ActionSelectToStartOfLine: (*View).SelectToStartOfLine,
ActionSelectToEndOfLine: (*View).SelectToEndOfLine,
ActionParagraphPrevious: (*View).ParagraphPrevious,
ActionParagraphNext: (*View).ParagraphNext,
ActionInsertNewline: (*View).InsertNewline,
ActionInsertSpace: (*View).InsertSpace,
ActionBackspace: (*View).Backspace,
ActionDelete: (*View).Delete,
ActionInsertTab: (*View).InsertTab,
ActionCenter: (*View).Center,
ActionUndo: (*View).Undo,
ActionRedo: (*View).Redo,
ActionCopy: (*View).Copy,
ActionCut: (*View).Cut,
ActionCutLine: (*View).CutLine,
ActionDuplicateLine: (*View).DuplicateLine,
ActionDeleteLine: (*View).DeleteLine,
ActionMoveLinesUp: (*View).MoveLinesUp,
ActionMoveLinesDown: (*View).MoveLinesDown,
ActionIndentSelection: (*View).IndentSelection,
ActionOutdentSelection: (*View).OutdentSelection,
ActionOutdentLine: (*View).OutdentLine,
ActionPaste: (*View).Paste,
ActionSelectAll: (*View).SelectAll,
ActionStart: (*View).Start,
ActionEnd: (*View).End,
ActionPageUp: (*View).PageUp,
ActionPageDown: (*View).PageDown,
ActionSelectPageUp: (*View).SelectPageUp,
ActionSelectPageDown: (*View).SelectPageDown,
ActionHalfPageUp: (*View).HalfPageUp,
ActionHalfPageDown: (*View).HalfPageDown,
ActionStartOfLine: (*View).StartOfLine,
ActionEndOfLine: (*View).EndOfLine,
ActionToggleRuler: (*View).ToggleRuler,
ActionToggleOverwriteMode: (*View).ToggleOverwriteMode,
ActionEscape: (*View).Escape,
ActionScrollUp: (*View).ScrollUpAction,
ActionScrollDown: (*View).ScrollDownAction,
ActionSpawnMultiCursor: (*View).SpawnMultiCursor,
ActionSpawnMultiCursorSelect: (*View).SpawnMultiCursorSelect,
ActionRemoveMultiCursor: (*View).RemoveMultiCursor,
ActionRemoveAllMultiCursors: (*View).RemoveAllMultiCursors,
ActionSkipMultiCursor: (*View).SkipMultiCursor,
ActionJumpToMatchingBrace: (*View).JumpToMatchingBrace,
ActionInsertEnter: (*View).InsertNewline,
}
var bindingKeys = map[string]tcell.Key{
"Up": tcell.KeyUp,
"Down": tcell.KeyDown,
"Right": tcell.KeyRight,
"Left": tcell.KeyLeft,
"UpLeft": tcell.KeyUpLeft,
"UpRight": tcell.KeyUpRight,
"DownLeft": tcell.KeyDownLeft,
"DownRight": tcell.KeyDownRight,
"Center": tcell.KeyCenter,
"PageUp": tcell.KeyPgUp,
"PageDown": tcell.KeyPgDn,
"Home": tcell.KeyHome,
"End": tcell.KeyEnd,
"Insert": tcell.KeyInsert,
"Delete": tcell.KeyDelete,
"Help": tcell.KeyHelp,
"Exit": tcell.KeyExit,
"Clear": tcell.KeyClear,
"Cancel": tcell.KeyCancel,
"Print": tcell.KeyPrint,
"Pause": tcell.KeyPause,
"Backtab": tcell.KeyBacktab,
"F1": tcell.KeyF1,
"F2": tcell.KeyF2,
"F3": tcell.KeyF3,
"F4": tcell.KeyF4,
"F5": tcell.KeyF5,
"F6": tcell.KeyF6,
"F7": tcell.KeyF7,
"F8": tcell.KeyF8,
"F9": tcell.KeyF9,
"F10": tcell.KeyF10,
"F11": tcell.KeyF11,
"F12": tcell.KeyF12,
"F13": tcell.KeyF13,
"F14": tcell.KeyF14,
"F15": tcell.KeyF15,
"F16": tcell.KeyF16,
"F17": tcell.KeyF17,
"F18": tcell.KeyF18,
"F19": tcell.KeyF19,
"F20": tcell.KeyF20,
"F21": tcell.KeyF21,
"F22": tcell.KeyF22,
"F23": tcell.KeyF23,
"F24": tcell.KeyF24,
"F25": tcell.KeyF25,
"F26": tcell.KeyF26,
"F27": tcell.KeyF27,
"F28": tcell.KeyF28,
"F29": tcell.KeyF29,
"F30": tcell.KeyF30,
"F31": tcell.KeyF31,
"F32": tcell.KeyF32,
"F33": tcell.KeyF33,
"F34": tcell.KeyF34,
"F35": tcell.KeyF35,
"F36": tcell.KeyF36,
"F37": tcell.KeyF37,
"F38": tcell.KeyF38,
"F39": tcell.KeyF39,
"F40": tcell.KeyF40,
"F41": tcell.KeyF41,
"F42": tcell.KeyF42,
"F43": tcell.KeyF43,
"F44": tcell.KeyF44,
"F45": tcell.KeyF45,
"F46": tcell.KeyF46,
"F47": tcell.KeyF47,
"F48": tcell.KeyF48,
"F49": tcell.KeyF49,
"F50": tcell.KeyF50,
"F51": tcell.KeyF51,
"F52": tcell.KeyF52,
"F53": tcell.KeyF53,
"F54": tcell.KeyF54,
"F55": tcell.KeyF55,
"F56": tcell.KeyF56,
"F57": tcell.KeyF57,
"F58": tcell.KeyF58,
"F59": tcell.KeyF59,
"F60": tcell.KeyF60,
"F61": tcell.KeyF61,
"F62": tcell.KeyF62,
"F63": tcell.KeyF63,
"F64": tcell.KeyF64,
"CtrlSpace": tcell.KeyCtrlSpace,
"CtrlA": tcell.KeyCtrlA,
"CtrlB": tcell.KeyCtrlB,
"CtrlC": tcell.KeyCtrlC,
"CtrlD": tcell.KeyCtrlD,
"CtrlE": tcell.KeyCtrlE,
"CtrlF": tcell.KeyCtrlF,
"CtrlG": tcell.KeyCtrlG,
"CtrlH": tcell.KeyCtrlH,
"CtrlI": tcell.KeyCtrlI,
"CtrlJ": tcell.KeyCtrlJ,
"CtrlK": tcell.KeyCtrlK,
"CtrlL": tcell.KeyCtrlL,
"CtrlM": tcell.KeyCtrlM,
"CtrlN": tcell.KeyCtrlN,
"CtrlO": tcell.KeyCtrlO,
"CtrlP": tcell.KeyCtrlP,
"CtrlQ": tcell.KeyCtrlQ,
"CtrlR": tcell.KeyCtrlR,
"CtrlS": tcell.KeyCtrlS,
"CtrlT": tcell.KeyCtrlT,
"CtrlU": tcell.KeyCtrlU,
"CtrlV": tcell.KeyCtrlV,
"CtrlW": tcell.KeyCtrlW,
"CtrlX": tcell.KeyCtrlX,
"CtrlY": tcell.KeyCtrlY,
"CtrlZ": tcell.KeyCtrlZ,
"CtrlLeftSq": tcell.KeyCtrlLeftSq,
"CtrlBackslash": tcell.KeyCtrlBackslash,
"CtrlRightSq": tcell.KeyCtrlRightSq,
"CtrlCarat": tcell.KeyCtrlCarat,
"CtrlUnderscore": tcell.KeyCtrlUnderscore,
"Tab": tcell.KeyTab,
"Esc": tcell.KeyEsc,
"Escape": tcell.KeyEscape,
"Enter": tcell.KeyEnter,
"Backspace": tcell.KeyBackspace2,
"OldBackspace": tcell.KeyBackspace,
// I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
"PgUp": tcell.KeyPgUp,
"PgDown": tcell.KeyPgDn,
}
var DefaultKeyBindings KeyBindings
// InitBindings initializes the keybindings for micro
func init() {
DefaultKeyBindings = NewKeyBindings(map[string]string{
"Up": ActionCursorUp,
"Down": ActionCursorDown,
"Right": ActionCursorRight,
"Left": ActionCursorLeft,
"ShiftUp": ActionSelectUp,
"ShiftDown": ActionSelectDown,
"ShiftLeft": ActionSelectLeft,
"ShiftRight": ActionSelectRight,
"AltLeft": ActionWordLeft,
"AltRight": ActionWordRight,
"AltUp": ActionMoveLinesUp,
"AltDown": ActionMoveLinesDown,
"AltShiftRight": ActionSelectWordRight,
"AltShiftLeft": ActionSelectWordLeft,
"CtrlLeft": ActionStartOfLine,
"CtrlRight": ActionEndOfLine,
"CtrlShiftLeft": ActionSelectToStartOfLine,
"ShiftHome": ActionSelectToStartOfLine,
"CtrlShiftRight": ActionSelectToEndOfLine,
"ShiftEnd": ActionSelectToEndOfLine,
"CtrlUp": ActionCursorStart,
"CtrlDown": ActionCursorEnd,
"CtrlShiftUp": ActionSelectToStart,
"CtrlShiftDown": ActionSelectToEnd,
"Alt-{": ActionParagraphPrevious,
"Alt-}": ActionParagraphNext,
"Enter": ActionInsertNewline,
"CtrlH": ActionBackspace,
"Backspace": ActionBackspace,
"Alt-CtrlH": ActionDeleteWordLeft,
"Alt-Backspace": ActionDeleteWordLeft,
"Tab": ActionIndentSelection + "," + ActionInsertTab,
"Backtab": ActionOutdentSelection + "," + ActionOutdentLine,
"CtrlZ": ActionUndo,
"CtrlY": ActionRedo,
"CtrlC": ActionCopy,
"CtrlX": ActionCut,
"CtrlK": ActionCutLine,
"CtrlD": ActionDuplicateLine,
"CtrlV": ActionPaste,
"CtrlA": ActionSelectAll,
"Home": ActionStartOfLine,
"End": ActionEndOfLine,
"CtrlHome": ActionCursorStart,
"CtrlEnd": ActionCursorEnd,
"PageUp": ActionCursorPageUp,
"PageDown": ActionCursorPageDown,
"CtrlR": ActionToggleRuler,
"Delete": ActionDelete,
"Insert": ActionToggleOverwriteMode,
"Alt-f": ActionWordRight,
"Alt-b": ActionWordLeft,
"Alt-a": ActionStartOfLine,
"Alt-e": ActionEndOfLine,
"Esc": ActionEscape,
"Alt-n": ActionSpawnMultiCursor,
"Alt-m": ActionSpawnMultiCursorSelect,
"Alt-p": ActionRemoveMultiCursor,
"Alt-c": ActionRemoveAllMultiCursors,
"Alt-x": ActionSkipMultiCursor,
})
}
// findKey will find binding Key 'b' using string 'k'
func findKey(k string) (b keyDesc, ok bool) {
modifiers := tcell.ModNone
// First, we'll strip off all the modifiers in the name and add them to the
// ModMask
modSearch:
for {
switch {
case strings.HasPrefix(k, "-"):
// We optionally support dashes between modifiers
k = k[1:]
case strings.HasPrefix(k, "Ctrl") && k != "CtrlH":
// CtrlH technically does not have a 'Ctrl' modifier because it is really backspace
k = k[4:]
modifiers |= tcell.ModCtrl
case strings.HasPrefix(k, "Alt"):
k = k[3:]
modifiers |= tcell.ModAlt
case strings.HasPrefix(k, "Shift"):
k = k[5:]
modifiers |= tcell.ModShift
default:
break modSearch
}
}
if len(k) == 0 {
return keyDesc{}, false
}
// Control is handled specially, since some character codes in bindingKeys
// are different when Control is depressed. We should check for Control keys
// first.
if modifiers&tcell.ModCtrl != 0 {
// see if the key is in bindingKeys with the Ctrl prefix.
k = string(unicode.ToUpper(rune(k[0]))) + k[1:]
if code, ok := bindingKeys["Ctrl"+k]; ok {
// It is, we're done.
return keyDesc{
keyCode: code,
modifiers: modifiers,
r: 0,
}, true
}
}
// See if we can find the key in bindingKeys
if code, ok := bindingKeys[k]; ok {
return keyDesc{
keyCode: code,
modifiers: modifiers,
r: 0,
}, true
}
// If we were given one character, then we've got a rune.
if len(k) == 1 {
return keyDesc{
keyCode: tcell.KeyRune,
modifiers: modifiers,
r: rune(k[0]),
}, true
}
// We don't know what happened.
return keyDesc{}, false
}
// findAction will find 'action' using string 'v'
func findAction(v string) (action func(*View) bool) {
action, _ = bindingActions[v]
return action
}