-
Notifications
You must be signed in to change notification settings - Fork 0
/
short_punct_pre_speller_processor.lua
110 lines (100 loc) · 3.18 KB
/
short_punct_pre_speller_processor.lua
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
local punct_db
local short_punct_key_pressed = false
local long_press_used = false
local short_punct_key = 'z'
local short_punct_key_release = 'Release+' .. short_punct_key
local repeat_key = 'space'
local repeat2_key = 'r'
local function processor_pre_speller(key, env)
local repr = key:repr()
local context = env.engine.context
local has_menu = context:has_menu()
local composing = context:is_composing()
if repr == short_punct_key and composing and not has_menu then
context.input = ''
return kNoop
end
if repr == short_punct_key and composing and has_menu then
context:commit()
end
return kNoop
end
local function processor_pre_recognizer(key, env)
local context = env.engine.context
local repr = key:repr()
if repr == short_punct_key then
short_punct_key_pressed = true
elseif repr == short_punct_key_release then
short_punct_key_pressed = false
end
if (repr == short_punct_key_release) and long_press_used then
context.input = ''
long_press_used = false
return kAccepted
end
if context.input == short_punct_key then
-- 长按short_punct_key时需拦截住
if repr == short_punct_key then
return kAccepted
end
if repr ~= repeat_key and repr ~= repeat2_key and repr:match('^[a-z]$') then
local punct = punct_db:lookup(key:repr())
if punct == nil then
return kNoop
end
env.engine:commit_text(punct)
my_log_on_commit(punct)
if short_punct_key_pressed then
long_press_used = true
else
context.input = ''
end
return kAccepted
end
if repr == repeat_key then
local history = get_commit_history(context, 0)
if history ~= nil then
env.engine:commit_text(history)
my_log_on_commit(history)
end
if short_punct_key_pressed then
long_press_used = true
else
context.input = ''
end
return kAccepted
end
if repr == repeat2_key then
local history = get_commit_history(context, -1)
if history ~= nil then
env.engine:commit_text(history)
my_log_on_commit(history)
end
if short_punct_key_pressed then
long_press_used = true
else
context.input = ''
end
return kAccepted
end
if repr == 'semicolon' then
i_semicolon = ';'
env.engine:commit_text(i_semicolon)
my_log_on_commit(i_semicolon)
if short_punct_key_pressed then
long_press_used = true
else
context.input = ''
end
return kAccepted
end
end
return kNoop
end
_G.short_punct_pre_speller_processor = processor_pre_speller
_G.short_punct_pre_recognizer_processor = {
init = function(_)
punct_db = ReverseDb("build/short_punct.reverse.bin")
end,
func = processor_pre_recognizer
}