forked from limetext/lime
/
key.go
430 lines (391 loc) · 9.37 KB
/
key.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
// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package backend
import (
"bytes"
"code.google.com/p/log4go"
"encoding/json"
"fmt"
. "github.com/limetext/lime/backend/util"
"sort"
"strings"
"unicode"
)
const (
Left Key = 0x2190 + iota
Up
Right
Down
Enter = '\n'
Escape = 0x001B
Backspace = 0x0008
Delete = 0x007F
KeypadEnter = '\n'
)
const (
OpEqual Op = iota //< Compare for equality.
OpNotEqual //< Compare for difference.
OpRegexMatch //< Compare for a regular expression match.
OpNotRegexMatch //< Compare for a regular expression difference.
OpRegexContains //< Compare whether the given regular expression matches some substring of the operand.
OpNotRegexContains //< Compare whether the given regular expression does not match some substring of the operand.
)
const (
// map to dingbats...
F1 Key = 0x2701 + iota
F2
F3
F4
F5
F6
F7
F8
F9
F10
F11
F12
Insert
PageUp
PageDown
Home
End
Break
Any Key = unicode.MaxRune
)
const (
shift = (1 << (29 - iota))
ctrl
alt
super
)
var keylut = map[string]Key{
"up": Up,
"left": Left,
"right": Right,
"down": Down,
"enter": Enter,
"tab": '\t',
"escape": Escape,
"space": ' ',
"f1": F1,
"f2": F2,
"f3": F3,
"f4": F4,
"f5": F5,
"f6": F6,
"f7": F7,
"f8": F8,
"f9": F9,
"f10": F10,
"f11": F11,
"f12": F12,
"backspace": Backspace,
"delete": Delete,
"keypad_enter": KeypadEnter,
"insert": Insert,
"pageup": PageUp,
"pagedown": PageDown,
"home": Home,
"end": End,
"break": Break,
"forward_slash": '/',
"backquote": '`',
"\\\"": '"',
"plus": '+',
"minus": '-',
"equals": '=',
"<character>": Any,
}
var rkeylut = map[Key]string{
Up: "up",
Left: "left",
Right: "right",
Down: "down",
Enter: "enter",
'\t': "tab",
Escape: "escape",
' ': "space",
F1: "f1",
F2: "f2",
F3: "f3",
F4: "f4",
F5: "f5",
F6: "f6",
F7: "f7",
F8: "f8",
F9: "f9",
F10: "f10",
F11: "f11",
F12: "f12",
Backspace: "backspace",
Delete: "delete",
Insert: "insert",
PageUp: "pageup",
PageDown: "pagedown",
Home: "home",
End: "end",
Break: "break",
'/': "forward_slash",
'`': "backquote",
'"': "\\\"",
'+': "plus",
'-': "minus",
'=': "equals",
Any: "<character>",
}
type (
Key rune
// A Key press with the given Key
// and modifiers.
KeyPress struct {
Key Key
Shift, Super, Alt, Ctrl bool
}
// A comparison operation used in context queries.
Op int
// A Context definition for which a key binding
// is to be considered.
KeyContext struct {
rawKeyContext
}
// TODO(.): HACK. This is because I want to use the default UnmarshalJSON
// behaviour on most of the struct member, but then also do some custom
// handling too.
//
// So the publicly exported KeyContext implements a custom UnmarshalJSON,
// which then invokes the default UnMarshalJSON handling on the embedded
// rawKeyContext, and then does it's own custom code after that.
//
// Is there a better way to do this?
rawKeyContext struct {
Key string //< The context's name.
Operator Op //< The operation to perform.
Operand interface{} //< The operand on which this operation should be performed.
MatchAll bool `json:"match_all"` //< Whether all selections should match the context or if it's enough for just one to match.
}
// A single KeyBinding for which after pressing the given
// sequence of Keys, and the Context matches,
// the Command will be invoked with the provided Args.
KeyBinding struct {
Keys []KeyPress
Command string
Args Args
Context []KeyContext
priority int
}
KeyBindings struct {
Bindings []*KeyBinding
keyOff int
}
)
func (k *Op) UnmarshalJSON(d []byte) error {
var tmp string
if err := json.Unmarshal(d, &tmp); err != nil {
return err
}
switch tmp {
default:
*k = OpEqual
case "not_equal":
*k = OpNotEqual
case "regex_match":
*k = OpRegexMatch
case "not_regex_match":
*k = OpNotRegexMatch
case "regex_contains":
*k = OpRegexContains
case "not_regex_contains":
*k = OpNotRegexContains
}
return nil
}
func (k *KeyContext) UnmarshalJSON(d []byte) error {
if err := json.Unmarshal(d, &k.rawKeyContext); err != nil {
return err
}
if k.Operand == nil {
k.Operand = true
}
return nil
}
// Returns an index used for sorting key presses.
// TODO(.): This is in no way a unique index with quite a lot of collisions and potentially resulting
// in bad lookups.
func (k KeyPress) Index() (ret int) {
ret = int(k.Key)
if k.Shift {
ret += shift
}
if k.Alt {
ret += alt
}
if k.Ctrl {
ret += ctrl
}
if k.Super {
ret += super
}
return
}
// Returns whether this KeyPress is a print character or not.
func (k KeyPress) IsCharacter() bool {
return unicode.IsPrint(rune(k.Key)) && !k.Super && !k.Ctrl
}
// Modifies the KeyPress so that it's Key is a unicode lower case
// rune and if it was in uppercase before this modification, the
// "Shift" modifier is also enabled.
func (k *KeyPress) fix() {
lower := Key(unicode.ToLower(rune(k.Key)))
if lower != k.Key {
k.Shift = true
k.Key = lower
}
}
func (k *KeyPress) UnmarshalJSON(d []byte) error {
combo := strings.Split(string(d[1:len(d)-1]), "+")
for _, c := range combo {
lower := strings.ToLower(c)
switch lower {
case "super":
k.Super = true
case "ctrl":
k.Ctrl = true
case "alt":
k.Alt = true
case "shift":
k.Shift = true
default:
if v, ok := keylut[lower]; ok {
k.Key = v
} else {
r := []Key(c)
if len(r) != 1 {
log4go.Warn("Unknown key value with %d bytes: %s", len(c), c)
return nil
}
k.Key = Key(c[0])
k.fix()
}
}
}
return nil
}
// Returns the number of KeyBindings.
func (k *KeyBindings) Len() int {
return len(k.Bindings)
}
// Compares one KeyBinding to another for sorting purposes.
func (k *KeyBindings) Less(i, j int) bool {
return k.Bindings[i].Keys[k.keyOff].Index() < k.Bindings[j].Keys[k.keyOff].Index()
}
// Swaps the two KeyBindings at the given positions.
func (k *KeyBindings) Swap(i, j int) {
k.Bindings[i], k.Bindings[j] = k.Bindings[j], k.Bindings[i]
}
// Drops all KeyBindings that are a sequence of key presses less or equal
// to the given number.
func (k *KeyBindings) DropLessEqualKeys(count int) {
for i := 0; i < len(k.Bindings); {
if len(k.Bindings[i].Keys) <= count {
k.Bindings[i] = k.Bindings[len(k.Bindings)-1]
k.Bindings = k.Bindings[:len(k.Bindings)-1]
} else {
i++
}
}
sort.Sort(k)
}
func (k *KeyBindings) UnmarshalJSON(d []byte) error {
if err := json.Unmarshal(d, &k.Bindings); err != nil {
return err
}
for i := range k.Bindings {
k.Bindings[i].priority = i
}
k.DropLessEqualKeys(0)
return nil
}
func (k *KeyBindings) merge(other *KeyBindings) {
// TODO(.): See issue #196
k.Bindings = append(k.Bindings, other.Bindings...)
k.DropLessEqualKeys(0)
}
func (k *KeyBindings) filter(ki int, ret *KeyBindings) {
idx := sort.Search(k.Len(), func(i int) bool {
return k.Bindings[i].Keys[k.keyOff].Index() >= ki
})
for i := idx; i < len(k.Bindings) && k.Bindings[i].Keys[k.keyOff].Index() == ki; i++ {
ret.Bindings = append(ret.Bindings, k.Bindings[i])
}
}
// Filters the KeyBindings, returning a new KeyBindings object containing
// a subset of matches for the given key press.
func (k *KeyBindings) Filter(kp KeyPress) (ret KeyBindings) {
p := Prof.Enter("key.filter")
defer p.Exit()
kp.fix()
k.DropLessEqualKeys(k.keyOff)
ret.keyOff = k.keyOff + 1
ki := kp.Index()
k.filter(ki, &ret)
if kp.IsCharacter() {
k.filter(int(Any), &ret)
}
return
}
// Tries to resolve all the current KeyBindings in k to a single
// action. If any action is appropriate as determined by context,
// the return value will be the specific KeyBinding that is possible
// to execute now, otherwise it is nil.
func (k *KeyBindings) Action(v *View) (kb *KeyBinding) {
p := Prof.Enter("key.action")
defer p.Exit()
for i := range k.Bindings {
if len(k.Bindings[i].Keys) > k.keyOff {
// This key binding is of a key sequence longer than what is currently
// probed for. For example, the binding is for the sequence ['a','b','c'], but
// the user has only pressed ['a','b'] so far.
continue
}
for _, c := range k.Bindings[i].Context {
if OnQueryContext.Call(v, c.Key, c.Operator, c.Operand, c.MatchAll) != True {
goto skip
}
}
if kb == nil || kb.priority < k.Bindings[i].priority {
kb = k.Bindings[i]
}
skip:
}
return
}
func (k Key) String() string {
if v, ok := rkeylut[k]; ok {
return v
}
return string(k)
}
func (k KeyPress) String() (ret string) {
if k.Super {
ret += "super+"
}
if k.Ctrl {
ret += "ctrl+"
}
if k.Alt {
ret += "alt+"
}
if k.Shift {
ret += "shift+"
}
ret += fmt.Sprintf("%s", k.Key)
return
}
func (k KeyBindings) String() string {
var buf bytes.Buffer
for _, b := range k.Bindings {
buf.WriteString(fmt.Sprintf("%+v\n", b))
}
return buf.String()
}