-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrime.lua
289 lines (230 loc) · 7.62 KB
/
rime.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
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
local user_data_dir = rime_api.get_user_data_dir()
local user_dll_path = (user_data_dir.."/lua/?.dll;") .. (user_data_dir.."/lua/lib/?.dll;")
package.cpath = user_dll_path .. package.cpath
log.warning(package.cpath)
--mylib = require("mylib")
--log.warning(tostring(mylib.mysin(3.14 / 2)))
--log.warning(tostring(mylib.mysin(3.14 / 2)))
--log.warning(os.getenv("APPDATA"))
--mylib.messagebox("Hello,", "World!")
lw = log.warning
local kRejected = 0 --结束流程,字符上屏(该键被当下直通上屏)KeyEventTakenAsCodeText
local kAccepted = 1 --结束流程,字符不上屏(该键被当下抛弃)KeyEventThrown
local kNoop = 2 --不结束流程,字符不上屏(该键继续往后流通处理) KeyEventPassThrough
local kResultTable = {[0]="kRejected", "kAccepted", "kNoop"}
--这样写就没法按数字上屏了,有问题待解决!!!!!!!!!!!!
local key_name_table = {" ","'","-","="}
function wrap_speller(key_event, env)
local engine = env.engine
local context = engine.context
if env.wrapped_speller == nil then
local wrapped_speller = Component.Processor(engine, "", "speller")
if wrapped_speller then env.wrapped_speller = wrapped_speller end
end
if not context:is_composing() then
for i, kn in ipairs(key_name_table) do
if tostring(i)==key_event:repr() then
engine:commit_text(kn)
return kAccepted
end
end
end
local process_result = env.wrapped_speller:process_key_event(key_event)
return process_result
end
--!!!!!!!!!!!!!!!!!!!!!!
function set_soft_cursor(_, env)
env.engine.context:set_option("soft_cursor", false)
return kNoop
end
EMPTY_CAND = Candidate("phrase", 0, 127, "", "(END)")
--[[
local function get_code_from_raw_code(raw_code)
local last_char = string.sub(raw_code, -1)
if not rime_api.regex_match(last_char, "[0-9]") then
if string.len(raw_code) > 3 then
if not(rime_api.regex_match(last_char, "[0-9]")) then
-- local raise_error = false > 3
raw_code = string.sub(raw_code, 1, -4)
end
else
return raw_code, ""
end
end
local input_code = ""
local extra_code = ""
local raw_code_len= string.len(raw_code)
local last_stroke_start_pos = raw_code_len - raw_code_len % 3 + 1
local extra_code_end = string.sub(raw_code, last_stroke_start_pos, raw_code_len)
for i=1, raw_code_len//3 do
input_code = input_code .. string.sub(raw_code, 3*i-2, 3*i-1)
extra_code = extra_code .. string.sub(raw_code, 3*i, 3*i)
end
extra_code = extra_code .. extra_code_end
return input_code, extra_code
end
]]--
--[[
function main_translator_for_raw_code(raw_code, seg, env)
-- log.warning(
-- env.engine:commit_text("测试"..raw_code)
if env.main_mems == nil then
main_mems = {}
local config = env.engine.schema.config
-- 不合理的架构……
local dict_names = config:get_list("reverse_lookup_filter_with_temp_schema_mode/dicts_to_reverse_lookup")
-- 之后有空了我要把这个配置文件的调用架构重新设计一下
for i=1, dict_names.size do
local mem = Memory(env.engine, env.engine.schema, dict_names:get_value_at(i-1).value)
if mem ~= nil then table.insert(main_mems, mem) end
end
env.main_mems = main_mems
end
local raw_code_len = string.len(raw_code)
if not( seg:has_tag("tmp_schema_mode") or seg:has_tag("tmp_en_mode") ) then
-- if not( seg:has_tag("dbl_slash_pattern") or seg:has_tag("dbl_semicolon_pattern") ) then
if raw_code_len%3 ~= 0 then
yield(EMPTY_CAND)
return
end
local input_code = ""
local extra_code = ""
input_code, extra_code = get_code_from_raw_code(raw_code)
if input_code~="" then
for i, mem in ipairs(env.main_mems) do
mem:dict_lookup(input_code, true, 20)
for entry in mem:iter_dict() do
c = Candidate("table", 0, string.len(raw_code), entry.text, "")
c.preedit = input_code
yield(c)
end
end
end
end
end
]]--
-- tags: [ temp_schema_tag ]
function insert_repeat_cand_when_tmp_schema_mode(cands, env)
local is_first_cand = true
for cand in cands:iter() do
if is_first_cand then
is_first_cand = false
yield(Candidate(cand.type, cand.start, cand._end, cand.text, ""))
end
yield(cand)
end
end
--[[
function force_select(cands, env)
if env.is_force_select_enabled == nil then env.is_force_select_enabled = true end
local input_code = env.engine.context.input
local input_code_len = string.len(input_code)
local has_comment = false
local count = 0
-- log.warning("force_select被调用了...")
-- log.warning("=========")
-- log.warning("=========")
for c in cands:iter() do
count = count + 1
if string.len(c.comment) > 0 then
has_comment = true
end
-------------------------------------------------------
-- 应该使用一个新的函数来实现这个功能开关切换的功能 (TODO!)
if env.engine.context:get_option("force_select_mode") then
env.is_force_select_enabled = true
else
env.is_force_select_enabled = false
end
-- if input_code == "<<>>" then
-- env.is_force_select_enabled = false
-- c = Candidate(c.type, c.start, c._end, "", c.comment)
-- else if input_code == ">><<" then
-- env.is_force_select_enabled = true
-- c = Candidate(c.type, c.start, c._end, "", c.comment)
-- end end
-- 注意强制上屏功能和按空格上屏功能的解耦
---------------------------------------------------------
yield(c)
if env.is_force_select_enabled and not has_comment and ((input_code_len == 6 and string.sub(input_code, -1)=="0") or (input_code_len==3 and string.sub(input_code, -1)=="1")) then
break
end
end
end
]]--
--[[
function reverse_lookup_filter_when_tmp_schema_mode(cands, env)
if env.reverse_lookup_handles == nil then
reverse_lookup_handles = {}
local config = env.engine.schema.config
local dict_names = config:get_list("reverse_lookup_filter_when_tmp_schema_mode/dicts_to_reverse_lookup")
for i=1, dict_names.size do
local rl = ReverseLookup(dict_names:get_value_at(i-1).value)
if rl ~= nil then table.insert(reverse_lookup_handles, rl) end
end
env.reverse_lookup_handles = reverse_lookup_handles
end
for cand in cands:iter() do
local comments = {}
local cand_text_len = utf8.len(cand.text)
if cand_text_len > 0 then
local is_single_char = (cand_text_len == 1)
for i, rl in ipairs(env.reverse_lookup_handles) do
local cmt = rl:lookup(cand.text)
if string.len(cmt) > 0 then
table.insert(comments, cmt)
if not is_single_char then
break -- 如果是非单字的情况则只查一次编码
end
end
end
local cmt = comments[1]
if cmt~=nil then
for i=2, #comments do
if comments[i] == nil then break end
if comments[i] ~= comments[i-1] then
cmt = cmt .. ' ' .. comments[i]
end
end
else
cmt = ""
end
cand.comment = cmt
end
yield(cand)
end
end
]]--
--[[
function ensure_uniquifier(cands, env)
local history_cands = {}
local is_cand_repeated = false
for cand in cands:iter() do
for i, c in ipairs(history_cands) do
if c.text == cand.text then
is_cand_repeated = true
break
end
end
if not is_cand_repeated then
table.insert(history_cands, cand)
yield(cand)
else
is_cand_repeated = false
end
end
end
]]--
--function show_cand_info(cands, env)
-- local comment = ""
-- local is_first_cand = false
-- for cand in cands:iter() do
-- if is_first_cand then
-- yield(EMPTY_CAND)
-- is_first_cand = false
-- end
-- local comment = cand.comment .. " type:" .. cand.type .. ", start:" .. tostring(cand.start) .. ", end:" .. tostring(cand._end) .. ", text:" .. cand.text
-- local c = Candidate(cand.type, cand.start, cand._end, cand.text, comment)
-- yield(c)
-- end
--end