From 1ffdce6a84f641eb784829d0822432ac96be445a Mon Sep 17 00:00:00 2001 From: mirtlecn Date: Wed, 7 Feb 2024 16:09:51 +0800 Subject: [PATCH] lua: fix #673 (#676) fix: committed incorrect puncts when user sets non-corresponding keys. fix: lua hijacked keys when in ascii_mode fix: handle keys correctly combined with shift --- double_pinyin.schema.yaml | 2 +- double_pinyin_abc.schema.yaml | 2 +- double_pinyin_flypy.schema.yaml | 2 +- double_pinyin_mspy.schema.yaml | 2 +- double_pinyin_sogou.schema.yaml | 2 +- double_pinyin_ziguang.schema.yaml | 2 +- lua/auto_commit_single_punct.lua | 75 ++++++++++++++++++++++--------- rime_ice.schema.yaml | 2 +- 8 files changed, 62 insertions(+), 27 deletions(-) diff --git a/double_pinyin.schema.yaml b/double_pinyin.schema.yaml index c7f590f..11c0279 100644 --- a/double_pinyin.schema.yaml +++ b/double_pinyin.schema.yaml @@ -53,8 +53,8 @@ switches: engine: processors: - lua_processor@select_character # 以词定字 - - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - ascii_composer + - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - recognizer - key_binder - speller diff --git a/double_pinyin_abc.schema.yaml b/double_pinyin_abc.schema.yaml index 838e8aa..7ae3a23 100644 --- a/double_pinyin_abc.schema.yaml +++ b/double_pinyin_abc.schema.yaml @@ -53,8 +53,8 @@ switches: engine: processors: - lua_processor@select_character # 以词定字 - - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - ascii_composer + - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - recognizer - key_binder - speller diff --git a/double_pinyin_flypy.schema.yaml b/double_pinyin_flypy.schema.yaml index 5acd238..1eb271b 100644 --- a/double_pinyin_flypy.schema.yaml +++ b/double_pinyin_flypy.schema.yaml @@ -53,8 +53,8 @@ switches: engine: processors: - lua_processor@select_character # 以词定字 - - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - ascii_composer + - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - recognizer - key_binder - speller diff --git a/double_pinyin_mspy.schema.yaml b/double_pinyin_mspy.schema.yaml index 8e9c04a..bcc2df4 100644 --- a/double_pinyin_mspy.schema.yaml +++ b/double_pinyin_mspy.schema.yaml @@ -53,8 +53,8 @@ switches: engine: processors: - lua_processor@select_character # 以词定字 - - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - ascii_composer + - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - recognizer - key_binder - speller diff --git a/double_pinyin_sogou.schema.yaml b/double_pinyin_sogou.schema.yaml index ea1c476..335febc 100644 --- a/double_pinyin_sogou.schema.yaml +++ b/double_pinyin_sogou.schema.yaml @@ -53,8 +53,8 @@ switches: engine: processors: - lua_processor@select_character # 以词定字 - - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - ascii_composer + - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - recognizer - key_binder - speller diff --git a/double_pinyin_ziguang.schema.yaml b/double_pinyin_ziguang.schema.yaml index 681c2db..6d71f9d 100644 --- a/double_pinyin_ziguang.schema.yaml +++ b/double_pinyin_ziguang.schema.yaml @@ -53,8 +53,8 @@ switches: engine: processors: - lua_processor@select_character # 以词定字 - - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - ascii_composer + - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - recognizer - key_binder - speller diff --git a/lua/auto_commit_single_punct.lua b/lua/auto_commit_single_punct.lua index 5fc1440..33c8eee 100644 --- a/lua/auto_commit_single_punct.lua +++ b/lua/auto_commit_single_punct.lua @@ -3,36 +3,71 @@ -- 用途示例: `(反引号)被添加到了 speller/alphabet 来响应辅码,如 gan`shuijin 得到「淦」。 -- 这样导致在输入单个的 ` 时仍然需要按空格选择一下。 -- 因为 ` 只在非开头状态下产生作用,所以我希望输入单个的 ` 时和其他标点一样都直接上屏。 - local P = {} +function P.get_punct_text(cfg, path) + if cfg:is_list(path) then + -- null value or list -> return nil + -- `: [`,-] -> nil + return nil + elseif cfg:is_map(path) then + local map = cfg:get_map(path) + -- map and has commit key -> return commit text + -- `: {commit: '*'} -> * + -- `: {pair: ["‘", "’"]} -> nil + if map:has_key("commit") then + return map:get_value("commit").value + end + elseif cfg:is_value(path) then + -- string -> return value + -- `: '`' -> ` + return cfg:get_string(path) + end + return nil +end + function P.init(env) - P.punct = env.engine.schema.config:get_string(env.name_space:gsub('^*', '')) + local cfg = env.engine.schema.config + P.punct = cfg:get_string(env.name_space:gsub('^*', '')) + if not P.punct then + return + end + + local full_shape_path = "punctuator/full_shape/" .. P.punct + local half_shape_path = "punctuator/half_shape/" .. P.punct + P.full_shape_text = P.get_punct_text(cfg, full_shape_path) + P.half_shape_text = P.get_punct_text(cfg, half_shape_path) end function P.func(key, env) - local context = env.engine.context + local context = env.engine.context - -- 不影响组合键 - if not P.punct or key:release() or key:ctrl() or key:alt() or key:super() or key:shift() then - return 2 -- kNoop - end + -- 不影响组合键 + if not P.punct or key:release() or key:ctrl() or key:alt() or key:super() then + return 2 -- kNoop + end - local ascii_str = '' - if key.keycode > 0x20 and key.keycode < 0x7f then - ascii_str = string.char(key.keycode) - end + local ascii_str = '' + if key.keycode > 0x20 and key.keycode < 0x7f then + ascii_str = string.char(key.keycode) + end - -- 解开下面三行,将只允许一次辅码上屏(辅码检索时,将会阻止再次输入辅码) - -- if context.input:find("^[a-z;]+" .. P.char) and ascii_str == P.char then - -- return 1 - -- end + -- 解开下面三行,将只允许一次辅码上屏(辅码检索时,将会阻止再次输入辅码) + -- if context.input:find("^[a-z;]+" .. P.char) and ascii_str == P.char then + -- return 1 + -- end - if not context:is_composing() and ascii_str == P.punct then - return 0 -- kRejected - end - - return 2 -- kNoop + if not context:is_composing() and ascii_str == P.punct then + local is_full_shape = env.engine.context:get_option("full_shape") + if is_full_shape and P.full_shape_text then + env.engine:commit_text(P.full_shape_text) + return 1 + elseif (not is_full_shape) and P.half_shape_text then + env.engine:commit_text(P.half_shape_text) + return 1 + end + end + return 2 -- kNoop end return P diff --git a/rime_ice.schema.yaml b/rime_ice.schema.yaml index f08fa84..a197886 100644 --- a/rime_ice.schema.yaml +++ b/rime_ice.schema.yaml @@ -42,8 +42,8 @@ switches: engine: processors: - lua_processor@select_character # 以词定字 - - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - ascii_composer + - lua_processor@auto_commit_single_punct # 让写在 alphabet 中的某标点自动上屏 - recognizer - key_binder - speller