@@ -57,7 +57,7 @@ endfunction
57
57
58
58
" }}}1
59
59
60
- " Wrapping functions {{{1
60
+ " Wrapping/unwrapping functions {{{1
61
61
62
62
function ! s: extractbefore (str)
63
63
if a: str = ~ ' \r'
@@ -75,6 +75,23 @@ function! s:extractafter(str)
75
75
endif
76
76
endfunction
77
77
78
+ if exists (' *trim' )
79
+ function ! s: trim (txt) abort
80
+ return trim (a: txt )
81
+ endfunction
82
+ else
83
+ function ! s: trim (txt) abort
84
+ return substitute (a: txt , ' \%(^\s\+\|\s\+$\)' , ' ' , ' g' )
85
+ endfunction
86
+ endif
87
+
88
+ function ! s: customsurroundings (char, b , trim ) abort
89
+ let all = s: process (get (a: b ? b: : g: , ' surround_' .char2nr (a: char )))
90
+ let before = s: extractbefore (all )
91
+ let after = s: extractafter (all )
92
+ return a: trim ? [s: trim (before), s: trim (after)] : [before, after]
93
+ endfunction
94
+
78
95
function ! s: fixindent (str,spc )
79
96
let str = substitute (a: str ,' \t' ,repeat (' ' ,&sw ),' g' )
80
97
let spc = substitute (a: spc ,' \t' ,repeat (' ' ,&sw ),' g' )
@@ -148,13 +165,9 @@ function! s:wrap(string,char,type,removed,special)
148
165
let before = ' '
149
166
let after = ' '
150
167
elseif exists (" b:surround_" .char2nr (newchar))
151
- let all = s: process (b: surround_ {char2nr (newchar)})
152
- let before = s: extractbefore (all )
153
- let after = s: extractafter (all )
168
+ let [before, after] = s: customsurroundings (newchar, 1 , 0 )
154
169
elseif exists (" g:surround_" .char2nr (newchar))
155
- let all = s: process (g: surround_ {char2nr (newchar)})
156
- let before = s: extractbefore (all )
157
- let after = s: extractafter (all )
170
+ let [before, after] = s: customsurroundings (newchar, 0 , 0 )
158
171
elseif newchar == # " p"
159
172
let before = " \n "
160
173
let after = " \n\n "
@@ -306,6 +319,43 @@ function! s:wrapreg(reg,char,removed,special)
306
319
let new = s: wrap (orig,a: char ,type ,a: removed ,a: special )
307
320
call setreg (a: reg ,new ,type )
308
321
endfunction
322
+
323
+ function ! s: escape (str) abort
324
+ return escape (a: str , ' !#$%&()*+,-./:;<=>?@[\]^{|}~' )
325
+ endfunction
326
+
327
+ function ! s: deletecustom (char, b , count ) abort
328
+ let [before, after] = s: customsurroundings (a: char , a: b , 1 )
329
+ let [before_pat, after_pat] = [' \v\C' .s: escape (before), ' \v\C' .s: escape (after)]
330
+ let found = searchpair (before_pat, ' ' , after_pat, ' bcW' )
331
+ if found <= 0
332
+ return [' ' ,' ' ]
333
+ endif
334
+ " Handle count/nesting only for asymmetric surroundings
335
+ if before !=# after
336
+ for _ in range (a: count - 1 )
337
+ let found = searchpair (before_pat, ' ' , after_pat, ' bW' )
338
+ if found <= 0
339
+ return [' ' ,' ' ]
340
+ endif
341
+ endfor
342
+ endif
343
+ norm! v
344
+ if before == # after
345
+ call search (before_pat, ' ceW' )
346
+ let found = search (after_pat, ' eW' )
347
+ else
348
+ let found = searchpair (before_pat, ' ' , after_pat, ' W' )
349
+ call search (after_pat, ' ceW' )
350
+ endif
351
+ if found <= 0
352
+ exe " norm! \<Esc> "
353
+ return [' ' ,' ' ]
354
+ endif
355
+ norm! d
356
+ return [before, after]
357
+ endfunction
358
+
309
359
" }}}1
310
360
311
361
function ! s: insert (... ) " {{{1
@@ -380,11 +430,12 @@ function! s:dosurround(...) " {{{1
380
430
let char = strpart (char,1 )
381
431
let spc = 1
382
432
endif
383
- if char == ' a'
384
- let char = ' >'
385
- endif
386
- if char == ' r'
387
- let char = ' ]'
433
+ if ! exists (" b:surround_" .char2nr (char)) && ! exists (" g:surround_" .char2nr (char))
434
+ if char == ' a'
435
+ let char = ' >'
436
+ elseif char == ' r'
437
+ let char = ' ]'
438
+ endif
388
439
endif
389
440
let newchar = " "
390
441
if a: 0 > 1
@@ -405,6 +456,10 @@ function! s:dosurround(...) " {{{1
405
456
let strcount = (scount == 1 ? " " : scount)
406
457
if char == ' /'
407
458
exe ' norm! ' .strcount.' [/d' .strcount.' ]/'
459
+ elseif exists (" b:surround_" .char2nr (char))
460
+ let [before, after] = s: deletecustom (char, 1 , scount)
461
+ elseif exists (" g:surround_" .char2nr (char))
462
+ let [before, after] = s: deletecustom (char, 0 , scount)
408
463
elseif char = ~# ' [[:punct:][:space:]]' && char !~# ' [][(){}<>"'' `]'
409
464
exe ' norm! T' .char
410
465
if getline (' .' )[col (' .' )-1 ] == char
@@ -438,6 +493,9 @@ function! s:dosurround(...) " {{{1
438
493
norm! " _x
439
494
call setreg (' "' ,' /**/' ," c" )
440
495
let keeper = substitute (substitute (keeper,' ^/\*\s\=' ,' ' ,' ' ),' \s\=\*$' ,' ' ,' ' )
496
+ elseif exists (" b:surround_" .char2nr (char)) || exists (" g:surround_" .char2nr (char))
497
+ call setreg (' "' , before.after, " c" )
498
+ let keeper = substitute (substitute (keeper,' \v\C^' .s: escape (before).' \s=' ,' ' ,' ' ), ' \v\C\s=' .s: escape (after).' $' , ' ' ,' ' )
441
499
elseif char = ~# ' [[:punct:][:space:]]' && char !~# ' [][(){}<>]'
442
500
exe ' norm! F' .char
443
501
exe ' norm! df' .char
0 commit comments