From fbef83c8b87b24c54869de02d9224b24975c5e50 Mon Sep 17 00:00:00 2001 From: Dvel Date: Sat, 24 Feb 2024 17:20:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20auto=5Fcommit=5Fsingle=5Fpunct.lua=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AE=E5=A4=9A=E4=B8=AA=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=EF=BC=9B=20=E5=B8=A6=E5=88=86=E5=8F=B7=E7=9A=84?= =?UTF-8?q?=E5=8F=8C=E6=8B=BC=E9=BB=98=E8=AE=A4=E5=8D=95=E4=B8=AA=E5=88=86?= =?UTF-8?q?=E5=8F=B7=E7=9B=B4=E6=8E=A5=E4=B8=8A=E5=B1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- double_pinyin_mspy.schema.yaml | 2 +- double_pinyin_sogou.schema.yaml | 2 +- double_pinyin_ziguang.schema.yaml | 2 +- lua/auto_commit_single_punct.lua | 47 +++++++++++++++++++++---------- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/double_pinyin_mspy.schema.yaml b/double_pinyin_mspy.schema.yaml index b6a3155..1f47c1d 100644 --- a/double_pinyin_mspy.schema.yaml +++ b/double_pinyin_mspy.schema.yaml @@ -109,7 +109,7 @@ lunar: lunar # 农历: 二〇二三年冬月二十 癸卯年(兔)冬月 # Lua 配置:直接上屏单个字符 # lua_filter@search@radical_pinyin 辅码的引导符在 key_binder/search 定义,目前为 `(反引号)。 # ` 只在中间时起辅码查询作用,但是输入单个 ` 时仍然需要按空格选择,这个 Lua 让单个的 ` 直接上屏。 -auto_commit_single_punct: "`" +auto_commit_single_punct: "`;" # Lua 配置:为 corrector 格式化 comment,占位符为 {comment} diff --git a/double_pinyin_sogou.schema.yaml b/double_pinyin_sogou.schema.yaml index 54b180f..b1d460e 100644 --- a/double_pinyin_sogou.schema.yaml +++ b/double_pinyin_sogou.schema.yaml @@ -109,7 +109,7 @@ lunar: lunar # 农历: 二〇二三年冬月二十 癸卯年(兔)冬月 # Lua 配置:直接上屏单个字符 # lua_filter@search@radical_pinyin 辅码的引导符在 key_binder/search 定义,目前为 `(反引号)。 # ` 只在中间时起辅码查询作用,但是输入单个 ` 时仍然需要按空格选择,这个 Lua 让单个的 ` 直接上屏。 -auto_commit_single_punct: "`" +auto_commit_single_punct: "`;" # Lua 配置:为 corrector 格式化 comment,占位符为 {comment} diff --git a/double_pinyin_ziguang.schema.yaml b/double_pinyin_ziguang.schema.yaml index b06587e..81ca7d4 100644 --- a/double_pinyin_ziguang.schema.yaml +++ b/double_pinyin_ziguang.schema.yaml @@ -109,7 +109,7 @@ lunar: lunar # 农历: 二〇二三年冬月二十 癸卯年(兔)冬月 # Lua 配置:直接上屏单个字符 # lua_filter@search@radical_pinyin 辅码的引导符在 key_binder/search 定义,目前为 `(反引号)。 # ` 只在中间时起辅码查询作用,但是输入单个 ` 时仍然需要按空格选择,这个 Lua 让单个的 ` 直接上屏。 -auto_commit_single_punct: "`" +auto_commit_single_punct: "`;" # Lua 配置:为 corrector 格式化 comment,占位符为 {comment} diff --git a/lua/auto_commit_single_punct.lua b/lua/auto_commit_single_punct.lua index 33c8eee..1a61e4d 100644 --- a/lua/auto_commit_single_punct.lua +++ b/lua/auto_commit_single_punct.lua @@ -1,8 +1,13 @@ -- 让写在 alphabet 中的某标点自动上屏 +-- +-- 感谢 @[Mirtle](https://github.com/mirtlecn) 的 PR +-- -- 配置,在方案中填写 auto_commit_single_punct: '`' -- 用途示例: `(反引号)被添加到了 speller/alphabet 来响应辅码,如 gan`shuijin 得到「淦」。 -- 这样导致在输入单个的 ` 时仍然需要按空格选择一下。 -- 因为 ` 只在非开头状态下产生作用,所以我希望输入单个的 ` 时和其他标点一样都直接上屏。 +-- 支持设置多个符号,例如双拼可以设置为 auto_commit_single_punct: '`;' 让单个的分号也直接上屏 + local P = {} function P.get_punct_text(cfg, path) @@ -28,22 +33,31 @@ end function P.init(env) local cfg = env.engine.schema.config - P.punct = cfg:get_string(env.name_space:gsub('^*', '')) - if not P.punct then + local puncts = cfg:get_string(env.name_space:gsub('^*', '')) + if not puncts 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) + P.puncts = {} + for punct in puncts:gmatch(".") do + table.insert(P.puncts, punct) + end + + P.full_shape_texts = {} + P.half_shape_texts = {} + for _, punct in ipairs(P.puncts) do + local full_shape_path = "punctuator/full_shape/" .. punct + local half_shape_path = "punctuator/half_shape/" .. punct + P.full_shape_texts[punct] = P.get_punct_text(cfg, full_shape_path) + P.half_shape_texts[punct] = P.get_punct_text(cfg, half_shape_path) + end end function P.func(key, env) local context = env.engine.context -- 不影响组合键 - if not P.punct or key:release() or key:ctrl() or key:alt() or key:super() then + if not P.puncts or key:release() or key:ctrl() or key:alt() or key:super() then return 2 -- kNoop end @@ -57,16 +71,19 @@ function P.func(key, env) -- return 1 -- end - 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 + for _, punct in ipairs(P.puncts) do + if not context:is_composing() and ascii_str == punct then + local is_full_shape = env.engine.context:get_option("full_shape") + if is_full_shape and P.full_shape_texts[punct] then + env.engine:commit_text(P.full_shape_texts[punct]) + return 1 + elseif (not is_full_shape) and P.half_shape_texts[punct] then + env.engine:commit_text(P.half_shape_texts[punct]) + return 1 + end end end + return 2 -- kNoop end