diff --git a/lua/number_translator.lua b/lua/number_translator.lua index 4253117..710dee9 100644 --- a/lua/number_translator.lua +++ b/lua/number_translator.lua @@ -110,9 +110,11 @@ local function number_translatorFunc(num) end -- 触发模式为任意大写字母(除了 U,U 用在 Unicode 了)开头,可在 recognizer/patterns 中自定义 -local function number_translator(input, seg) +local function number_translator(input, seg, env) + -- 获取 recognizer/patterns/number 的第 2 个字符作为触发前缀 + env.number_keyword = env.number_keyword or env.engine.schema.config:get_string('recognizer/patterns/rmb'):sub(2, 2) local str, num, numberPart - if string.match(input, "^([A-TV-Z]+%d+)(%.?)(%d*)$") ~= nil then + if env.number_keyword ~= '' and input:sub(1, 1) == env.number_keyword then str = string.gsub(input, "^(%a+)", "") numberPart = number_translatorFunc(str) if #numberPart > 0 then diff --git a/lua/select_character.lua b/lua/select_character.lua new file mode 100644 index 0000000..1fdaa03 --- /dev/null +++ b/lua/select_character.lua @@ -0,0 +1,79 @@ +-- 依次定字 +-- 来源: +-- https://github.com/BlindingDark/rime-lua-select-character/blob/master/lua/select_character.lua +-- http://lua-users.org/lists/lua-l/2014-04/msg00590.html +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 get_commit_text(context, fun) + local candidate_text = context:get_selected_candidate().text + local selected_character = fun(candidate_text) + + context:clear_previous_segment() + local commit_text = context:get_commit_text() + + context:clear() + + return commit_text .. selected_character +end + +local function select_character(key, env) + local engine = env.engine + local context = engine.context + 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 commit_text = context:get_commit_text() + + if (key:repr() == first_key and commit_text ~= "") then + engine:commit_text(get_commit_text(context, first_character)) + + return 1 -- kAccepted + end + + if (key:repr() == last_key and commit_text ~= "") then + engine:commit_text(get_commit_text(context, last_character)) + + return 1 -- kAccepted + end + + return 2 -- kNoop +end + +return select_character \ No newline at end of file diff --git a/rime.lua b/rime.lua index a665bcd..3181595 100755 --- a/rime.lua +++ b/rime.lua @@ -9,3 +9,5 @@ reduce_english_filter = require("reduce_english_filter") corrector = require("corrector") -- 中国农历 Chinese_lunar_calendar = require("chineseLunarCalendar") +-- 以词定字(默认为「[」和「]」;在 key_binder 下配置快捷键 +select_character = require("select_character") \ No newline at end of file diff --git a/rime_mint.schema.yaml b/rime_mint.schema.yaml index 67f4354..832e136 100755 --- a/rime_mint.schema.yaml +++ b/rime_mint.schema.yaml @@ -48,6 +48,7 @@ switches: engine: processors: + - lua_processor@select_character # 以词定字 - ascii_composer # ※ 處理西文模式及中西文切換 - recognizer # ※ 與 matcher 搭配,處理符合特定規則的輸入碼,如網址、反查等 - key_binder # ※ 在特定條件下將按鍵綁定到其他按鍵,如重定義逗號、句號爲候選翻頁鍵 @@ -290,5 +291,5 @@ recognizer: wubi98_mint: "uw[a-z]*'?$" stroke: "ui[a-z]*'?$" chaizi: "uu[a-z]*'?$" - number: "^R[0-9]+[.]?[0-9]*" # 响应 lua_translator@number_translator 脚本将自动获取第 2 个字符作为触发前缀 + rmb: "^R[0-9]+[.]?[0-9]*" # 响应 lua_translator@number_translator 脚本将自动获取第 2 个字符作为触发前缀 gregorian_to_lunar: "^N[0-9]{1,8}" # 响应 lua_translator@Chinese_lunar_calendar 公历转农历,输入 N20240115 得到「二〇二四年腊月初五」,脚本将自动获取第 2 个字符作为触发前缀