@@ -2,6 +2,37 @@ vim9script
22
33const PAIRS = " b()B{}r[]a<>"
44
5+ g: surround_maps = g: - >get (' surround_maps' , {})
6+
7+ def UpgradeSurroundMap (d : dict <any> , name: string )
8+ var match = name- >matchlist (' ^surround_(\d+)$' )
9+
10+ if match - >empty ()
11+ return
12+ endif
13+
14+ var [_, value] = name- >matchlist (' ^surround_(\d+)$' )
15+
16+ if value- >empty ()
17+ return
18+ endif
19+
20+ var maps = d - >get (' surround_maps' , {})
21+ var key = value- >str2nr ()- >nr2char ()
22+
23+ maps[key ] = d - >get (name)
24+
25+ d [' surround_maps' ] = maps
26+ enddef
27+
28+ export def UpgradeSurroundMaps ()
29+ for d in [g: , b: ]
30+ for k in d - >keys ()
31+ UpgradeSurroundMap (d , k )
32+ endfor
33+ endfor
34+ enddef
35+
536export def InputTarget (): string
637 var c = getcharstr ()
738 while c = ~ ' ^\d+$'
@@ -52,7 +83,13 @@ def ExtractAfter(str: string): string
5283enddef
5384
5485def CustomSurroundings (char: string , d : dict <any> , trim : bool): list <string>
55- var all = Process (get (d , printf (' surround_%d' , char2nr (char))))
86+ var key = d - >get (' surround_maps' , {})- >get (char)
87+
88+ if key == null
89+ throw ' Invalid surround map ' .. char
90+ endif
91+
92+ var all = Process (key )
5693 var before = ExtractBefore (all )
5794 var after = ExtractAfter (all )
5895
@@ -150,14 +187,12 @@ def Wrap(str: string, char: string, wrapType: string, removed: string, linebreak
150187
151188 var idx = PAIRS- >stridx (newchar)
152189
153- var custom = printf (' surround_%d' , char2nr (newchar))
154-
155190 if newchar == ' '
156191 before = ' '
157192 after = ' '
158- elseif ! b: - >get (custom )- >empty ()
193+ elseif ! b: - >get (' surround_maps ' , {}) - > get (newchar )- >empty ()
159194 [before, after] = CustomSurroundings (newchar, b: , false)
160- elseif ! g: - >get (custom )- >empty ()
195+ elseif ! g: - >get (' surround_maps ' , {}) - > get (newchar )- >empty ()
161196 [before, after] = CustomSurroundings (newchar, g: , false)
162197 elseif newchar == # " p"
163198 before = " \n "
@@ -483,9 +518,7 @@ export def DoSurround(value: string = null_string, new_value: string = null_stri
483518 spc = true
484519 endif
485520
486- var custom = printf (' surround_%d' , char2nr (char))
487-
488- if b: - >get (custom , g: - >get (custom ))- >empty ()
521+ if b: - >get (' surround_maps' , g: - >get (' surround_maps' , {}))- >get (char)- >empty ()
489522 if char == ' a'
490523 char = ' >'
491524 elseif char == ' r'
@@ -522,9 +555,9 @@ export def DoSurround(value: string = null_string, new_value: string = null_stri
522555
523556 if char == ' /'
524557 execute ' normal! ' .. strcount .. " [/\<Plug> (surround-d)" .. strcount .. ' ]/'
525- elseif exists ( printf ( ' b:surround_%d ' , char2nr (char)) )
558+ elseif ! b: - > get ( ' surround_maps ' , {}) - > get (char)- > empty ( )
526559 [before, after] = DeleteCustom (char, b: , scount)
527- elseif exists ( printf ( ' g:surround_%d ' , char2nr (char)) )
560+ elseif ! g: - > get ( ' surround_maps ' , {}) - > get (char)- > empty ( )
528561 [before, after] = DeleteCustom (char, g: , scount)
529562 elseif char = ~# ' [[:punct:][:space:]]' && char !~# ' [][(){}<>"'' `]'
530563 execute ' normal! T' .. char
@@ -553,9 +586,7 @@ export def DoSurround(value: string = null_string, new_value: string = null_stri
553586 var oldline = getline (' .' )
554587 var oldlnum = line (' .' )
555588
556- custom = printf (' surround_%d' , char2nr (char))
557-
558- if ! b: - >get (custom , g: - >get (custom ))- >empty ()
589+ if ! b: - >get (' surround_maps' , g: - >get (' surround_maps' , {}))- >get (char)- >empty ()
559590 setreg (' "' , before .. after, " c" )
560591 keeper = keeper
561592 - >substitute (' \v\C^' .. Escape (before) .. ' \s=' , ' ' , ' ' )
0 commit comments