Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增两个lua #63

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion double_pinyin.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ switches:
engine:
processors:
- lua_processor@select_character # 以词定字
- lua_processor@*quick_symbol_text #快符引导以及重复上屏
- lua_processor@*userdb_sync_delete #通过输入 /del 触发,用于清理自定义同步目录下txt用户词典里被标记c<0的词条
- ascii_composer
- recognizer
- key_binder
Expand Down Expand Up @@ -294,7 +296,8 @@ recognizer:
number: "^R[0-9]+[.]?[0-9]*" # 脚本将自动获取第 2 个字符 R 作为触发前缀,响应 lua_translator@*number_translator,数字金额大写
gregorian_to_lunar: "^N[0-9]{1,8}" # 脚本将自动获取第 2 个字符 N 作为触发前缀,响应 lua_translator@*lunar,公历转农历,输入 N20240115 得到「二〇二三年腊月初五」
calculator: "^V.*$" #计算器功能引导

quick_symbol: "^'.*$" # 快符引导,例如输入'q 后自动上屏快速符号,双击''重复上屏符号
quick_text: "^;.*$" # 双击;;重复上屏
# 从 default 继承快捷键
key_binder:
import_preset: default # 从 default.yaml 继承通用的
Expand Down
5 changes: 4 additions & 1 deletion double_pinyin_abc.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ switches:
engine:
processors:
- lua_processor@*select_character # 以词定字
- lua_processor@*quick_symbol_text #快符引导以及重复上屏
- lua_processor@*userdb_sync_delete #通过输入 /del 触发,用于清理自定义同步目录下txt用户词典里被标记c<0的词条
- ascii_composer
- recognizer
- key_binder
Expand Down Expand Up @@ -297,7 +299,8 @@ recognizer:
number: "^R[0-9]+[.]?[0-9]*" # 脚本将自动获取第 2 个字符 R 作为触发前缀,响应 lua_translator@*number_translator,数字金额大写
gregorian_to_lunar: "^N[0-9]{1,8}" # 脚本将自动获取第 2 个字符 N 作为触发前缀,响应 lua_translator@*lunar,公历转农历,输入 N20240115 得到「二〇二三年腊月初五」
calculator: "^V.*$" #计算器功能引导

quick_symbol: "^'.*$" # 快符引导,例如输入'q 后自动上屏快速符号,双击''重复上屏符号
quick_text: "^;.*$" # 双击;;重复上屏
# 从 default 继承快捷键
key_binder:
import_preset: default # 从 default.yaml 继承通用的
Expand Down
5 changes: 4 additions & 1 deletion double_pinyin_flypy.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ switches:
engine:
processors:
- lua_processor@select_character # 以词定字
- lua_processor@*quick_symbol_text #快符引导以及重复上屏
- lua_processor@*userdb_sync_delete #通过输入 /del 触发,用于清理自定义同步目录下txt用户词典里被标记c<0的词条
- ascii_composer
- recognizer
- key_binder
Expand Down Expand Up @@ -294,7 +296,8 @@ recognizer:
number: "^R[0-9]+[.]?[0-9]*" # 脚本将自动获取第 2 个字符 R 作为触发前缀,响应 lua_translator@*number_translator,数字金额大写
gregorian_to_lunar: "^N[0-9]{1,8}" # 脚本将自动获取第 2 个字符 N 作为触发前缀,响应 lua_translator@*lunar,公历转农历,输入 N20240115 得到「二〇二三年腊月初五」
calculator: "^V.*$" #计算器功能引导

quick_symbol: "^'.*$" # 快符引导,例如输入'q 后自动上屏快速符号,双击''重复上屏符号
quick_text: "^;.*$" # 双击;;重复上屏
# 从 default 继承快捷键
key_binder:
import_preset: default # 从 default.yaml 继承通用的
Expand Down
5 changes: 4 additions & 1 deletion double_pinyin_mspy.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ switches:
engine:
processors:
- lua_processor@select_character # 以词定字
- lua_processor@*quick_symbol_text #快符引导以及重复上屏
- lua_processor@*userdb_sync_delete #通过输入 /del 触发,用于清理自定义同步目录下txt用户词典里被标记c<0的词条
- ascii_composer
- recognizer
- key_binder
Expand Down Expand Up @@ -298,7 +300,8 @@ recognizer:
number: "^R[0-9]+[.]?[0-9]*" # 脚本将自动获取第 2 个字符 R 作为触发前缀,响应 lua_translator@*number_translator,数字金额大写
gregorian_to_lunar: "^N[0-9]{1,8}" # 脚本将自动获取第 2 个字符 N 作为触发前缀,响应 lua_translator@*lunar,公历转农历,输入 N20240115 得到「二〇二三年腊月初五」
calculator: "^V.*$" #计算器功能引导

quick_symbol: "^'.*$" # 快符引导,例如输入'q 后自动上屏快速符号,双击''重复上屏符号
quick_text: "^;.*$" # 双击;;重复上屏
# 从 default 继承快捷键
key_binder:
import_preset: default # 从 default.yaml 继承通用的
Expand Down
5 changes: 4 additions & 1 deletion double_pinyin_sogou.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ switches:
engine:
processors:
- lua_processor@select_character # 以词定字
- lua_processor@*quick_symbol_text #快符引导以及重复上屏
- lua_processor@*userdb_sync_delete #通过输入 /del 触发,用于清理自定义同步目录下txt用户词典里被标记c<0的词条
- ascii_composer
- recognizer
- key_binder
Expand Down Expand Up @@ -298,7 +300,8 @@ recognizer:
number: "^R[0-9]+[.]?[0-9]*" # 脚本将自动获取第 2 个字符 R 作为触发前缀,响应 lua_translator@*number_translator,数字金额大写
gregorian_to_lunar: "^N[0-9]{1,8}" # 脚本将自动获取第 2 个字符 N 作为触发前缀,响应 lua_translator@*lunar,公历转农历,输入 N20240115 得到「二〇二三年腊月初五」
calculator: "^V.*$" #计算器功能引导

quick_symbol: "^'.*$" # 快符引导,例如输入'q 后自动上屏快速符号,双击''重复上屏符号
quick_text: "^;.*$" # 双击;;重复上屏
# 从 default 继承快捷键
key_binder:
import_preset: default # 从 default.yaml 继承通用的
Expand Down
5 changes: 4 additions & 1 deletion double_pinyin_ziguang.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ switches:
engine:
processors:
- lua_processor@select_character # 以词定字
- lua_processor@*quick_symbol_text #快符引导以及重复上屏
- lua_processor@*userdb_sync_delete #通过输入 /del 触发,用于清理自定义同步目录下txt用户词典里被标记c<0的词条
- ascii_composer
- recognizer
- key_binder
Expand Down Expand Up @@ -296,7 +298,8 @@ recognizer:
number: "^R[0-9]+[.]?[0-9]*" # 脚本将自动获取第 2 个字符 R 作为触发前缀,响应 lua_translator@*number_translator,数字金额大写
gregorian_to_lunar: "^N[0-9]{1,8}" # 脚本将自动获取第 2 个字符 N 作为触发前缀,响应 lua_translator@*lunar,公历转农历,输入 N20240115 得到「二〇二三年腊月初五」
calculator: "^V.*$" #计算器功能引导

quick_symbol: "^'.*$" # 快符引导,例如输入'q 后自动上屏快速符号,双击''重复上屏符号
quick_text: "^;.*$" # 双击;;重复上屏
# 从 default 继承快捷键
key_binder:
import_preset: default # 从 default.yaml 继承通用的
Expand Down
159 changes: 159 additions & 0 deletions lua/quick_symbol_text.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
-- 欢迎使用带声调的拼音词库
-- @amzxyz
-- https://github.com/amzxyz/rime_feisheng
-- https://github.com/amzxyz/rime_wanxiang_pinyin
-- 本lua通过定义一个不直接上屏的引导符号搭配26字母实现快速符号输入,并在双击''上屏上一次的符号,双击;;重复上屏上次的汉字和字母
-- 使用方式加入到函数 - lua_processor@*quick_symbol_text 下面
-- 方案文件配置,
-- recognizer/patterns/quick_symbol: "^'.*$"
-- recognizer/patterns/quick_text: "^;.*$"
-- 定义符号映射表
local mapping = {
q = "“",
w = "?",
e = "(",
r = ")",
t = "~",
y = "·",
u = "『",
i = "』",
o = "〖",
p = "〗",
a = "!",
s = "……",
d = "、",
f = "“",
g = "”",
h = "‘",
j = "’",
k = "——",
l = "%",
z = "。”",
x = "?”",
c = "!”",
v = "【",
b = "】",
n = "《",
m = "》"
}

-- 记录上次上屏的内容
local last_commit_symbol = "" -- 存储符号的上屏历史
local last_commit_text = "" -- 存储文本(汉字/字母)的上屏历史

-- 判断字符是否为符号,包括半角和全角符号
local function is_symbol(char)
-- 检测半角符号范围
if string.match(char, "[!@#$%%%^&*()%-_=+%[%]{}\\|;:'\",.<>/?`~]") then
return true
end
-- 检测全角符号范围
if string.match(char, "[!@#$%^&*()_+-=[]{};:‘’“”、,。/?`~]") then
return true
end
return false
end

-- 判断上屏内容是符号还是文本(汉字/字母)
local function classify_commit_text(commit_text)
-- 如果上屏内容中全是符号,则认为是符号
if commit_text and #commit_text > 0 then
for i = 1, #commit_text do
local char = string.sub(commit_text, i, i)
if not is_symbol(char) then
return "text" -- 上屏内容包含非符号字符,认为是文本
end
end
return "symbol" -- 上屏内容全是符号
end
return "unknown"
end

-- 初始化符号输入的状态
local function init(env)
-- 读取 RIME 配置文件中的引导符号模式
local config = env.engine.schema.config

-- 动态读取符号和文本重复的引导模式
local quick_symbol_pattern = config:get_string("recognizer/patterns/quick_symbol") or "^'.*$"
local quick_text_pattern = config:get_string("recognizer/patterns/quick_text") or "^;.*$"

-- 提取配置值中的第二个字符作为引导符
local quick_symbol = string.sub(quick_symbol_pattern, 2, 2) or "'"
local quick_text = string.sub(quick_text_pattern, 2, 2) or ";"

-- 生成单引导符和双引导符模式
env.single_symbol_pattern = "^" .. quick_symbol .. "([a-zA-Z])$"
env.double_symbol_pattern_symbol = "^" .. quick_symbol .. quick_symbol .. "$"
env.double_symbol_pattern_text = "^" .. quick_text .. quick_text .. "$"

-- 捕获上屏事件,分类存储符号和文本
env.engine.context.commit_notifier:connect(function(ctx)
local commit_text = ctx:get_commit_text()

-- 通过分类判断是符号还是文本
local classification = classify_commit_text(commit_text)
if classification == "symbol" then
last_commit_symbol = commit_text -- 保存符号到 last_commit_symbol
elseif classification == "text" then
last_commit_text = commit_text -- 保存文本到 last_commit_text
end
end)
end

-- 捕获符号键盘事件并记录符号
local function capture_symbol_key(key_event)
-- 获取按键字符表示
local keychar = key_event:repr()

-- 判断该字符是否为符号(半角或全角)
if is_symbol(keychar) then
return keychar -- 返回符号字符
end

return nil
end

-- 处理符号和文本的重复上屏逻辑
local function processor(key_event, env)
local engine = env.engine
local context = engine.context
local input = context.input -- 当前输入的字符串

-- 1. 检查是否输入的编码为双引导符 ;;,用于汉字或字母重复上屏
if string.match(input, env.double_symbol_pattern_text) then
if last_commit_text ~= "" then
engine:commit_text(last_commit_text) -- 上屏上次的汉字或文本
context:clear() -- 清空输入
return 1 -- 捕获事件,处理完成
end
end

-- 2. 检查是否输入的编码为双引导符 '',用于符号重复上屏
if string.match(input, env.double_symbol_pattern_symbol) then
if last_commit_symbol ~= "" then
engine:commit_text(last_commit_symbol) -- 上屏上次的符号
context:clear() -- 清空输入
return 1 -- 捕获事件,处理完成
end
end

-- 3. 检查当前输入是否匹配单引导符符号模式 'q、'w 等
local match = string.match(input, env.single_symbol_pattern)
if match then
local symbol = mapping[match] -- 获取匹配的符号
if symbol then
-- 将符号直接上屏并保存到符号历史
engine:commit_text(symbol)
last_commit_symbol = symbol -- 保存符号到 last_commit_symbol
context:clear() -- 清空输入
return 1 -- 捕获事件,处理完成
end
end

return 2 -- 未处理事件,继续传播
end

-- 导出到 RIME
return { init = init, func = processor }

Loading