refactor: select_character.lua (#647)
将获取按键移动到 init 函数 简化了中文取字方法 预先判断无候选(加入了 alphabet 但无候选词)防止 error log 产生,使用 input
This commit is contained in:
parent
daa80b55c9
commit
a142d50a94
@ -1,78 +1,43 @@
|
||||
-- 以词定字
|
||||
-- 来源 https://github.com/BlindingDark/rime-lua-select-character
|
||||
-- 原脚本 https://github.com/BlindingDark/rime-lua-select-character
|
||||
-- 删除了默认按键 [ ],和方括号翻页冲突,需要在 key_binder 下指定才能生效
|
||||
-- 20230526195910 不再错误地获取commit_text,而是直接获取get_selected_candidate().text。
|
||||
local function utf8_sub(s, i, j)
|
||||
i = i or 1
|
||||
j = j or -1
|
||||
-- 20240128141207 重写:将读取设置移动到 init 方法中;简化中文取字方法;预先判断候选存在与否,无候选取 input;
|
||||
|
||||
if i < 1 or j < 1 then
|
||||
local n = utf8.len(s)
|
||||
if not n then
|
||||
return nil
|
||||
end
|
||||
if i < 0 then
|
||||
i = n + 1 + i
|
||||
end
|
||||
if j < 0 then
|
||||
j = n + 1 + j
|
||||
end
|
||||
if i < 0 then
|
||||
i = 1
|
||||
elseif i > n then
|
||||
i = n
|
||||
end
|
||||
if j < 0 then
|
||||
j = 1
|
||||
elseif j > n then
|
||||
j = n
|
||||
end
|
||||
end
|
||||
local select = {}
|
||||
|
||||
if j < i then
|
||||
return ""
|
||||
end
|
||||
|
||||
i = utf8.offset(s, i)
|
||||
j = utf8.offset(s, j + 1)
|
||||
|
||||
if i and j then
|
||||
return s:sub(i, j - 1)
|
||||
elseif i then
|
||||
return s:sub(i)
|
||||
else
|
||||
return ""
|
||||
end
|
||||
function select.init(env)
|
||||
local config = env.engine.schema.config
|
||||
select.first_key = config:get_string('key_binder/select_first_character')
|
||||
select.last_key = config:get_string('key_binder/select_last_character')
|
||||
end
|
||||
|
||||
local function select_character(key, env)
|
||||
function select.func(key, env)
|
||||
local engine = env.engine
|
||||
local context = engine.context
|
||||
local commit_text = context:get_commit_text()
|
||||
local config = engine.schema.config
|
||||
local context = env.engine.context
|
||||
|
||||
-- local first_key = config:get_string('key_binder/select_first_character') or 'bracketleft'
|
||||
-- local last_key = config:get_string('key_binder/select_last_character') or 'bracketright'
|
||||
local first_key = config:get_string('key_binder/select_first_character')
|
||||
local last_key = config:get_string('key_binder/select_last_character')
|
||||
|
||||
if context:has_menu() then
|
||||
if (key:repr() == first_key) then
|
||||
if (context:get_selected_candidate().text) then
|
||||
engine:commit_text(utf8_sub(context:get_selected_candidate().text, 1, 1))
|
||||
if
|
||||
not key:release()
|
||||
and (context:is_composing() or context:has_menu())
|
||||
and (select.first_key or select.last_key)
|
||||
then
|
||||
local text = context.input
|
||||
if context:get_selected_candidate() then
|
||||
text = context:get_selected_candidate().text
|
||||
end
|
||||
if utf8.len(text) > 1 then
|
||||
if (key:repr() == select.first_key) then
|
||||
engine:commit_text(text:sub(1, utf8.offset(text, 2) - 1))
|
||||
context:clear()
|
||||
end
|
||||
return 1 -- kAccepted
|
||||
elseif (key:repr() == last_key) then
|
||||
if (context:get_selected_candidate().text) then
|
||||
engine:commit_text(utf8_sub(context:get_selected_candidate().text, -1, -1))
|
||||
return 1
|
||||
elseif (key:repr() == select.last_key) then
|
||||
engine:commit_text(text:sub(utf8.offset(text, -1)))
|
||||
context:clear()
|
||||
return 1
|
||||
end
|
||||
return 1 -- kAccepted
|
||||
end
|
||||
end
|
||||
|
||||
return 2 -- kNoop
|
||||
return 2
|
||||
end
|
||||
|
||||
return select_character
|
||||
return select
|
||||
|
Loading…
x
Reference in New Issue
Block a user