mirror of
https://github.com/d0zingcat/rime-ice.git
synced 2026-05-13 23:16:54 +00:00
refactor: Lua 模块化
删除 `rime.lua`,拆分到 `lua/` 文件夹内。 方案中的 `- lua_xxx@xxx` 修改为 `- lua_xxx@*xxx`(加一个星号)。
This commit is contained in:
83
lua/select_character.lua
Normal file
83
lua/select_character.lua
Normal file
@@ -0,0 +1,83 @@
|
||||
-- 以词定字
|
||||
-- 来源 https://github.com/BlindingDark/rime-lua-select-character
|
||||
-- 删除了默认按键 [ ],和方括号翻页冲突,需要在 key_binder 下指定才能生效
|
||||
local function utf8_sub(s, i, j)
|
||||
i = i or 1
|
||||
j = j or -1
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
local function first_character(s)
|
||||
return utf8_sub(s, 1, 1)
|
||||
end
|
||||
|
||||
local function last_character(s)
|
||||
return utf8_sub(s, -1, -1)
|
||||
end
|
||||
|
||||
local function select_character(key, env)
|
||||
local engine = env.engine
|
||||
local context = engine.context
|
||||
local commit_text = context:get_commit_text()
|
||||
local config = engine.schema.config
|
||||
|
||||
-- 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 (key:repr() == first_key and commit_text ~= "") then
|
||||
engine:commit_text(first_character(commit_text))
|
||||
context:clear()
|
||||
|
||||
return 1 -- kAccepted
|
||||
end
|
||||
|
||||
if (key:repr() == last_key and commit_text ~= "") then
|
||||
engine:commit_text(last_character(commit_text))
|
||||
context:clear()
|
||||
|
||||
return 1 -- kAccepted
|
||||
end
|
||||
|
||||
return 2 -- kNoop
|
||||
end
|
||||
|
||||
return select_character
|
||||
Reference in New Issue
Block a user