11local flash = require (" flash" )
2- local flypy = require (" flash-zh.flypy " )
2+ local char_map = require (" flash-zh.char_map " )
33
44local M = {}
55
66function M .jump (opts )
77 opts = opts or {}
8- local mode = M .mix_mode
9- if opts .chinese_only then
10- mode = M .zh_mode
8+
9+ -- Allow per-jump override so users can verify schemes quickly,
10+ -- and so it still works even if setup() was not called (common with lazy.nvim misconfig).
11+ if opts .scheme then
12+ require (" flash-zh.char_map" ).set (opts .scheme )
13+ opts .scheme = nil
1114 end
12- opts = vim .tbl_deep_extend (" force" , {
13- labels = " asdfghjklqwertyuiopzxcvbnm" ,
14- search = {
15- mode = mode ,
16- },
17- labeler = function (_ , state )
18- require (" flash-zh.labeler" ).new (state ):update ()
19- end ,
20- }, opts )
21- flash .jump (opts )
22- end
2315
24- function M .remote (opts )
25- opts = opts or {}
2616 local mode = M .mix_mode
2717 if opts .chinese_only then
2818 mode = M .zh_mode
@@ -36,7 +26,7 @@ function M.remote(opts)
3626 require (" flash-zh.labeler" ).new (state ):update ()
3727 end ,
3828 }, opts )
39- flash .remote (opts )
29+ flash .jump (opts )
4030end
4131
4232function M .mix_mode (str )
@@ -52,38 +42,45 @@ function M.mix_mode(str)
5242end
5343
5444function M .zh_mode (str )
45+ local map = char_map .get ()
5546 local regexs = {}
5647 while string.len (str ) > 1 do
57- regexs [# regexs + 1 ] = flypy .char2patterns [string.sub (str , 1 , 2 )]
48+ local k = string.sub (str , 1 , 2 )
49+ -- Be defensive: unknown keys should not crash the search.
50+ regexs [# regexs + 1 ] = map .char2patterns [k ] or (" [" .. k .. string.upper (k ) .. " ]" )
5851 str = string.sub (str , 3 )
5952 end
6053 if string.len (str ) == 1 then
61- regexs [# regexs + 1 ] = flypy .char1patterns [str ]
54+ regexs [# regexs + 1 ] = map .char1patterns [str ] or ( " [ " .. str .. string.upper ( str ) .. " ] " )
6255 end
6356 local ret = table.concat (regexs )
6457 return ret , ret
6558end
6659
67- local nodes = {
68- alpha = function (str )
69- return " [" .. str .. string.upper (str ) .. " ]"
70- end ,
71- pinyin = function (str )
72- return flypy .char2patterns [str ]
73- end ,
74- comma = function (str )
75- return flypy .comma [str ]
76- end ,
77- singlepin = function (str )
78- return flypy .char1patterns [str ]
79- end ,
80- other = function (str )
81- str = flypy .escape [str ] or str
82- return str
83- end ,
84- }
60+ local function get_nodes (map )
61+ return {
62+ alpha = function (str )
63+ return " [" .. str .. string.upper (str ) .. " ]"
64+ end ,
65+ pinyin = function (str )
66+ return map .char2patterns [str ]
67+ end ,
68+ comma = function (str )
69+ return map .comma [str ]
70+ end ,
71+ singlepin = function (str )
72+ return map .char1patterns [str ]
73+ end ,
74+ other = function (str )
75+ str = map .escape [str ] or str
76+ return str
77+ end ,
78+ }
79+ end
8580
8681function M .regex (parser )
82+ local map = char_map .get ()
83+ local nodes = get_nodes (map )
8784 local regexs = {}
8885 for _ , v in ipairs (parser ) do
8986 regexs [# regexs + 1 ] = nodes [v .type ](v .str )
@@ -92,10 +89,11 @@ function M.regex(parser)
9289end
9390
9491function M .parser (str , prefix )
92+ local map = char_map .get ()
9593 prefix = prefix or {}
9694 local firstchar = string.sub (str , 1 , 1 )
9795 local chars = {}
98- for k , _ in pairs (flypy .comma ) do
96+ for k , _ in pairs (map .comma ) do
9997 table.insert (chars , k )
10098 end
10199 if firstchar == " " then
@@ -108,7 +106,7 @@ function M.parser(str, prefix)
108106 prefix2 [# prefix2 + 1 ] = { str = firstchar , type = " singlepin" }
109107 return { prefix , prefix2 }
110108 elseif string.match (secondchar , " %a" ) then
111- if flypy .char2patterns [firstchar .. secondchar ] then
109+ if map .char2patterns [firstchar .. secondchar ] then
112110 local prefix2 = M .copy (prefix )
113111 prefix2 [# prefix2 + 1 ] = { str = firstchar , type = " alpha" }
114112 prefix [# prefix + 1 ] = { str = firstchar .. secondchar , type = " pinyin" }
@@ -158,24 +156,32 @@ function M.copy(table)
158156end
159157
160158-- @param opts table
159+ -- @field [opt] opts.scheme string Choose the built-in shuangpin scheme. One of: "flypy", "pyjj".
161160-- @field opts.char_map table Char map for flypy.
162161-- @field [opt] opts.char_map.comma table Override the default comma map.
163162-- @field [opt] opts.char_map.append_comma table Append to the default comma map.
164163-- @field [opt] opts.char_map.append_char1 table Append to the default char1patterns map.
165164-- @field [opt] opts.char_map.append_char2 table Append to the default char2patterns map.
166165function M .setup (opts )
167166 opts = opts or {}
167+
168+ if opts .scheme then
169+ char_map .set (opts .scheme )
170+ end
171+
168172 if not opts .char_map then
169173 return
170174 end
175+
176+ local map = char_map .get ()
171177 local to_escape = " \\ ^$*+?.%|[]()"
172178 if opts .char_map .comma then
173179 for k , v in pairs (opts .char_map .comma ) do
174180 if # k ~= 1 then
175181 error (" comma key must be a single character" )
176182 else
177183 v = vim .fn .escape (v , to_escape )
178- flypy .comma [k ] = " [" .. v .. " ]"
184+ map .comma [k ] = " [" .. v .. " ]"
179185 end
180186 end
181187 end
@@ -184,9 +190,9 @@ function M.setup(opts)
184190 if # k ~= 1 then
185191 error (" append_comma key must be a single character" )
186192 else
187- local chars = flypy .comma [k ] or " "
193+ local chars = map .comma [k ] or " "
188194 chars = string.sub (chars , 2 , - 2 ) .. vim .fn .escape (v , to_escape )
189- flypy .comma [k ] = " [" .. chars .. " ]"
195+ map .comma [k ] = " [" .. chars .. " ]"
190196 end
191197 end
192198 end
@@ -195,9 +201,9 @@ function M.setup(opts)
195201 if # k ~= 1 then
196202 error (" append_char1 key must be a single character" )
197203 else
198- local chars = flypy .char1patterns [k ] or " "
204+ local chars = map .char1patterns [k ] or " "
199205 chars = string.sub (chars , 2 , - 2 ) .. vim .fn .escape (v , to_escape )
200- flypy .char1patterns [k ] = " [" .. chars .. " ]"
206+ map .char1patterns [k ] = " [" .. chars .. " ]"
201207 end
202208 end
203209 end
@@ -206,9 +212,9 @@ function M.setup(opts)
206212 if # k ~= 2 then
207213 error (" append_char2 key must be two characters" )
208214 else
209- local chars = flypy .char2patterns [k ] or " "
215+ local chars = map .char2patterns [k ] or " "
210216 chars = string.sub (chars , 2 , - 2 ) .. vim .fn .escape (v , to_escape )
211- flypy .char2patterns [k ] = " [" .. chars .. " ]"
217+ map .char2patterns [k ] = " [" .. chars .. " ]"
212218 end
213219 end
214220 end
0 commit comments