Skip to content

Commit 7b69d21

Browse files
committed
feat(keybind): 支持按键绑定在特定tag下
1 parent 9e9fd14 commit 7b69d21

5 files changed

Lines changed: 37 additions & 16 deletions

File tree

flypy_xhfast.custom.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ patch:
6969
select_first_character: "9" # `9/minus` 以词定字(首字)
7070
select_last_character: "0" # `0/equal` 以词定字(尾字)
7171
bindings/+:
72+
# - {when: has_menu, accept: ";", send: 2} # 引号; 用于第2候选
7273
# - {when: has_menu, accept: "'", send: 3} # 引号' 用于第3候选
7374
- {when: paging, accept: comma, send: Page_Up} # 逗号, 返回上一页
7475
- {when: has_menu, accept: ']', send: Page_Down} # 括号] 下一页
@@ -82,7 +83,9 @@ patch:
8283
- {when: composing, accept: Control+i, send: Control+Left}
8384
- {when: composing, accept: Control+o, send: Control+Right}
8485
- {when: composing, accept: Control+w, send: Control+BackSpace}
85-
- {match: "^cC.*$", accept: "'", send_sequence: '{Page_Down}'}
86+
# 以下按键绑定规则(含`tag` / `match`)需 flypy_keybinder 支持
87+
- {tag: "calculator", accept: "'", send_sequence: '{Page_Down}'}
88+
# - {match: "^cC.*$", accept: "'", send_sequence: '{Page_Down}'}
8689

8790
recognizer/+:
8891
patterns/+:

flypy_xhfast.schema.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ switches:
3030
# 输入引擎
3131
engine:
3232
processors:
33+
- key_binder
34+
- lua_processor@*flypy_keybinder
3335
- lua_processor@*select_char
3436
- lua_processor@*symbol_menu
3537
- lua_processor@*easy_en*processor
@@ -43,8 +45,6 @@ engine:
4345
- lua_processor@*fuzzy_word_expand*processor
4446
- ascii_composer
4547
- recognizer
46-
- key_binder
47-
- lua_processor@*flypy_keybinder
4848
- speller
4949
- selector
5050
- navigator
@@ -199,7 +199,7 @@ custom_phrase: # 自定义短语, 包含固顶字
199199
enable_completion: false
200200

201201
# 键位帮助
202-
flypy_key_map: # 小鹤双拼键位帮助
202+
flypy_key_map: # 小鹤双拼键位帮助
203203
prefix: "/ok"
204204
dictionary: ""
205205
tag: flypy_key_map

lua/calculator.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
-- author: ChaosAlphard, boomker
2-
32
require("lib/string")
43
local T = {}
54

@@ -299,7 +298,7 @@ function T.func(input, seg, env)
299298
local segment = composition:back()
300299

301300
local trigger_tbl = env.prefix:match("|") and string.split(env.prefix, "|") or { env.prefix }
302-
if startsWith(input, trigger_tbl) or seg:has_tag("calculator") then
301+
if seg:has_tag("calculator") then
303302
segment.prompt = "" .. env.tips .. ""
304303
if input:match("?h$") or input:match("^/h$") then goto HELP end
305304
-- 提取算式
@@ -335,6 +334,8 @@ function T.func(input, seg, env)
335334
end
336335
end
337336
if startsWith(input, trigger_tbl) or seg:has_tag("calculator") then
337+
-- local cseg = Segment(seg.start, seg._end)
338+
-- cseg.tags = Set({ "calc_help" })
338339
yield(Candidate("calc", seg.start, seg._end, "'/h'、'?h' 查看支持的函数", ""))
339340
end
340341
end

lua/flypy_keybinder.lua

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local P = {}
66

77
---@class Binding
88
---element
9+
---@field tag string
910
---@field match string
1011
---@field accept KeyEvent
1112
---@field send_sequence KeySequence
@@ -14,14 +15,21 @@ local P = {}
1415
---@param value ConfigMap
1516
---@return Binding | nil
1617
local function parse(value)
18+
local tag = value:get_value("tag")
1719
local match = value:get_value("match")
1820
local accept = value:get_value("accept")
1921
local send_sequence = value:get_value("send_sequence")
20-
if not match or not accept or not send_sequence then return nil end
21-
local key_event = KeyEvent(accept:get_string())
22-
local sequence = KeySequence(send_sequence:get_string())
23-
local binding = { match = match:get_string(), accept = key_event, send_sequence = sequence }
24-
return binding
22+
if (not match) and (not tag) then return nil end
23+
local tag_match = tag and tag:get_string()
24+
local match_pattern = match and match:get_string()
25+
local key_event = accept and KeyEvent(accept:get_string())
26+
local sequence = send_sequence and KeySequence(send_sequence:get_string())
27+
if match_pattern and key_event and sequence then
28+
return { match = match_pattern, accept = key_event, send_sequence = sequence }
29+
elseif tag_match and key_event and sequence then
30+
return { tag = tag_match, accept = key_event, send_sequence = sequence }
31+
end
32+
return nil
2533
end
2634

2735
---@param env KeyBinderEnv
@@ -53,10 +61,15 @@ function P.func(key_event, env)
5361
if not input_code then return 2 end
5462

5563
if not (context.composition or context.composition:back()) then return 2 end
56-
-- if not env.engine.context.composition:back():has_tag("abc") then return 2 end
64+
local segment = context.composition:back()
65+
if (not segment) then return 2 end
66+
local tags = segment.tags
67+
5768
for _, binding in ipairs(env.bindings) do
5869
-- 只有当按键和当前输入的模式都匹配的时候,才起作用
59-
if key_event:eq(binding.accept) and rime_api.regex_match(input_code, binding.match) then
70+
local match_tag = (not tags:empty()) and binding.tag and tags[binding.tag]
71+
local match_input = binding.match and rime_api.regex_match(input_code, binding.match)
72+
if key_event:eq(binding.accept) and (match_input or match_tag) then
6073
env.redirecting = true
6174
for _, key in ipairs(binding.send_sequence:toKeyEvent()) do
6275
env.engine:process_key(key)

lua/select_char.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ end
2626
function P.func(key, env)
2727
local engine = env.engine
2828
local context = engine.context
29-
local input_code = context.input
3029
local composition = context.composition
3130
if composition:empty() then return 2 end
32-
if input_code:match("^cC$") then return 2 end
3331
local segment = composition:back()
34-
if (not segment) or (not segment:has_tag("abc")) then return 2 end
32+
if (not segment) then return 2 end
33+
local tags = segment.tags
34+
for tag, _ in pairs(tags) do
35+
if (tag ~= "abc") and (tag ~= "paging") then
36+
return 2
37+
end
38+
end
3539

3640
if (key:repr() == env.first_key) then
3741
local cand = context:get_selected_candidate()

0 commit comments

Comments
 (0)