2023-05-04 19:41:21 +02:00
|
|
|
|
-- 以词定字
|
2024-01-28 09:06:57 +01:00
|
|
|
|
-- 原脚本 https://github.com/BlindingDark/rime-lua-select-character
|
2023-05-04 19:41:21 +02:00
|
|
|
|
-- 删除了默认按键 [ ],和方括号翻页冲突,需要在 key_binder 下指定才能生效
|
2024-05-08 05:21:15 +02:00
|
|
|
|
-- 20230526195910 不再错误地获取commit_text,而是直接获取get_selected_candidate().text
|
|
|
|
|
-- 20240128141207 重写:将读取设置移动到 init 方法中;简化中文取字方法;预先判断候选存在与否,无候选取 input
|
|
|
|
|
-- 20240508111725 当候选字数为 1 时,快捷键使该字上屏
|
2023-05-04 19:41:21 +02:00
|
|
|
|
|
2024-01-28 09:06:57 +01:00
|
|
|
|
local select = {}
|
2023-05-04 19:41:21 +02:00
|
|
|
|
|
2024-01-28 09:06:57 +01:00
|
|
|
|
function select.init(env)
|
|
|
|
|
local config = env.engine.schema.config
|
2024-05-08 05:21:15 +02:00
|
|
|
|
env.first_key = config:get_string('key_binder/select_first_character')
|
|
|
|
|
env.last_key = config:get_string('key_binder/select_last_character')
|
2023-05-04 19:41:21 +02:00
|
|
|
|
end
|
|
|
|
|
|
2024-01-28 09:06:57 +01:00
|
|
|
|
function select.func(key, env)
|
2023-05-04 19:41:21 +02:00
|
|
|
|
local engine = env.engine
|
2024-01-28 09:06:57 +01:00
|
|
|
|
local context = env.engine.context
|
|
|
|
|
|
|
|
|
|
if
|
|
|
|
|
not key:release()
|
|
|
|
|
and (context:is_composing() or context:has_menu())
|
2024-05-08 05:21:15 +02:00
|
|
|
|
and (env.first_key or env.last_key)
|
2024-01-28 09:06:57 +01:00
|
|
|
|
then
|
|
|
|
|
local text = context.input
|
|
|
|
|
if context:get_selected_candidate() then
|
|
|
|
|
text = context:get_selected_candidate().text
|
|
|
|
|
end
|
|
|
|
|
if utf8.len(text) > 1 then
|
2024-05-08 05:21:15 +02:00
|
|
|
|
if (key:repr() == env.first_key) then
|
2024-01-28 09:06:57 +01:00
|
|
|
|
engine:commit_text(text:sub(1, utf8.offset(text, 2) - 1))
|
2023-05-31 15:12:37 +02:00
|
|
|
|
context:clear()
|
2024-01-28 09:06:57 +01:00
|
|
|
|
return 1
|
2024-05-08 05:21:15 +02:00
|
|
|
|
elseif (key:repr() == env.last_key) then
|
2024-01-28 09:06:57 +01:00
|
|
|
|
engine:commit_text(text:sub(utf8.offset(text, -1)))
|
2023-05-31 15:12:37 +02:00
|
|
|
|
context:clear()
|
2024-01-28 09:06:57 +01:00
|
|
|
|
return 1
|
2023-05-31 15:12:37 +02:00
|
|
|
|
end
|
2024-05-08 05:21:15 +02:00
|
|
|
|
else
|
|
|
|
|
if key:repr() == env.first_key or key:repr() == env.last_key then
|
|
|
|
|
engine:commit_text(text)
|
|
|
|
|
context:clear()
|
|
|
|
|
return 1
|
|
|
|
|
end
|
2023-05-26 15:04:25 +02:00
|
|
|
|
end
|
2023-05-04 19:41:21 +02:00
|
|
|
|
end
|
2024-01-28 09:06:57 +01:00
|
|
|
|
return 2
|
2023-05-04 19:41:21 +02:00
|
|
|
|
end
|
|
|
|
|
|
2024-01-28 09:06:57 +01:00
|
|
|
|
return select
|