@@ -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
1617local 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
2533end
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 )
0 commit comments