Skip to content

Commit a837c05

Browse files
committed
Support matchpairs
1 parent baf89ad commit a837c05

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

plugin/surround.vim

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ function! s:wrap(string,char,type,removed,special)
130130
let s:input = ""
131131
let type = a:type
132132
let linemode = type ==# 'V' ? 1 : 0
133+
let mp = split(&matchpairs, '[:,]') " assumes it's well-formed
133134
let before = ""
134135
let after = ""
135136
if type ==# "V"
@@ -144,6 +145,7 @@ function! s:wrap(string,char,type,removed,special)
144145
let extraspace = ' '
145146
endif
146147
let idx = stridx(pairs,newchar)
148+
let mpidx = index(mp, newchar)
147149
if newchar == ' '
148150
let before = ''
149151
let after = ''
@@ -241,6 +243,11 @@ function! s:wrap(string,char,type,removed,special)
241243
let idx = idx / 3 * 3
242244
let before = strpart(pairs,idx+1,1) . spc
243245
let after = spc . strpart(pairs,idx+2,1)
246+
elseif mpidx >= 0
247+
let spc = (mpidx % 2) == 1 ? " " : ""
248+
let mpidx = mpidx / 2 * 2
249+
let before = mp[mpidx] . spc
250+
let after = spc . mp[mpidx+1]
244251
elseif newchar == "\<C-[>" || newchar == "\<C-]>"
245252
let before = "{\n\t"
246253
let after = "\n}"
@@ -391,6 +398,8 @@ function! s:dosurround(...) " {{{1
391398
return s:beep()
392399
endif
393400
endif
401+
let mp = split(&matchpairs, '[:,]') " assumes it's well-formed
402+
let mpidx = index(mp, char)
394403
let cb_save = &clipboard
395404
set clipboard-=unnamed clipboard-=unnamedplus
396405
let append = ""
@@ -406,6 +415,13 @@ function! s:dosurround(...) " {{{1
406415
exe 'norm! l'
407416
endif
408417
exe 'norm! dt'.char
418+
elseif mpidx >= 0
419+
let mpidx = mpidx / 2 * 2
420+
if getline('.')[col('.')-1] == mp[mpidx]
421+
exe 'norm! l'
422+
endif
423+
exe 'norm! T'.mp[mpidx]
424+
exe 'norm! dt'.mp[mpidx+1]
409425
else
410426
exe 'norm! d'.strcount.'i'.char
411427
endif
@@ -433,6 +449,9 @@ function! s:dosurround(...) " {{{1
433449
elseif char =~# '[[:punct:][:space:]]' && char !~# '[][(){}<>]'
434450
exe 'norm! F'.char
435451
exe 'norm! df'.char
452+
elseif mpidx >= 0
453+
exe 'norm! F'.mp[mpidx]
454+
exe 'norm! df'.mp[mpidx+1]
436455
else
437456
" One character backwards
438457
call search('\m.', 'bW')

0 commit comments

Comments
 (0)