Skip to content

Commit d598697

Browse files
committed
Implement tpope#226, tpope#200, and tpope#118
1 parent f51a26d commit d598697

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

plugin/surround.vim

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ endfunction
3535

3636
function! s:inputreplacement()
3737
let c = s:getchar()
38+
while c =~ '^\d\+$'
39+
let c .= s:getchar()
40+
endwhile
3841
if c == " "
3942
let c .= s:getchar()
4043
endif
@@ -139,6 +142,11 @@ function! s:wrap(string,char,type,removed,special)
139142
endif
140143
let pairs = "b()B{}r[]a<>"
141144
let extraspace = ""
145+
let multi = 1
146+
if newchar =~ '^\d\+'
147+
let multi = matchstr(newchar, '^\d\+')
148+
let newchar = substitute(newchar,'^\d\+','','')
149+
endif
142150
if newchar =~ '^ '
143151
let newchar = strpart(newchar,1)
144152
let extraspace = ' '
@@ -275,6 +283,8 @@ function! s:wrap(string,char,type,removed,special)
275283
let keeper = strcharpart(keeper,0,strchars(keeper) - 1)
276284
endif
277285
endif
286+
let before = repeat(before, multi)
287+
let after = repeat(after, multi)
278288
if type ==# 'V'
279289
let before = initspaces.before
280290
endif
@@ -406,8 +416,18 @@ function! s:dosurround(...) " {{{1
406416
exe 'norm! l'
407417
endif
408418
exe 'norm! dt'.char
419+
elseif char =~# '[fF]'
420+
let [sfline,sfcol,mfline,mfcol,efline,efcol] = [0,0,0,0,0,0]
421+
for i in range(scount)
422+
let [sfline,sfcol,mfline,mfcol,efline,efcol] = s:searchfuncpos()
423+
call cursor(sfline, sfcol)
424+
endfor
425+
if sfline != 0
426+
call cursor(mfline, mfcol)
427+
norm! di(
428+
endif
409429
else
410-
exe 'norm! d'.strcount.'i'.char
430+
exe 'norm d'.strcount.'i'.char
411431
endif
412432
let keeper = getreg('"')
413433
let okeeper = keeper " for reindent below
@@ -433,17 +453,20 @@ function! s:dosurround(...) " {{{1
433453
elseif char =~# '[[:punct:][:space:]]' && char !~# '[][(){}<>]'
434454
exe 'norm! F'.char
435455
exe 'norm! df'.char
456+
elseif char =~# '[fF]'
457+
call cursor(sfline, sfcol)
458+
norm! df)
436459
else
437460
" One character backwards
438461
call search('\m.', 'bW')
439-
exe "norm! da".char
462+
exe "norm da".char
440463
endif
441464
let removed = getreg('"')
442465
let rem2 = substitute(removed,'\n.*','','')
443466
let oldhead = strpart(oldline,0,strlen(oldline)-strlen(rem2))
444467
let oldtail = strpart(oldline, strlen(oldline)-strlen(rem2))
445468
let regtype = getregtype('"')
446-
if char =~# '[\[({<T]' || spc
469+
if char =~# '[\[({<TF]' || spc
447470
let keeper = substitute(keeper,'^\s\+','','')
448471
let keeper = substitute(keeper,'\s\+$','','')
449472
endif
@@ -510,7 +533,7 @@ function! s:opfunc(type, ...) abort " {{{1
510533
let reg_type = getregtype(reg)
511534
let type = a:type
512535
if a:type == "char"
513-
silent exe 'norm! v`[o`]"'.reg.'y'
536+
silent exe 'norm v`[o`]"'.reg.'y'
514537
let type = 'v'
515538
elseif a:type == "line"
516539
silent exe 'norm! `[V`]"'.reg.'y'
@@ -582,6 +605,47 @@ function! s:closematch(str) " {{{1
582605
return ""
583606
endif
584607
endfunction " }}}1
608+
function! s:searchfuncpos() " {{{1
609+
" Save cursor position and last visual mode
610+
let ppos = getpos(".")
611+
let svpos = getpos("'<")
612+
let evpos = getpos("'>")
613+
let pvmode = visualmode()
614+
615+
let [sfline,sfcol,mfline,mfcol,efline,efcol] = [0,0,0,0,0,0]
616+
while 1
617+
" Get surounding paren text object
618+
norm! va(
619+
let [ebuf,eline,ecol,eoff] = getpos(".")
620+
let [sbuf,sline,scol,soff] = getpos("v")
621+
execute "norm! \<esc>"
622+
623+
if eline == sline && ecol == scol
624+
break
625+
endif
626+
627+
call cursor(sline,scol,soff)
628+
let [fline,fcol] = searchpos('\i\+\s*(','bnec')
629+
if fline == sline && fcol == scol
630+
let [sfline,sfcol] = searchpos('\i\+\s*(','bnc')
631+
let [mfline,mfcol] = [sline,scol]
632+
let [efline,efcol] = [eline,ecol]
633+
break
634+
elseif fline == 0 && fcol == 0
635+
break
636+
else
637+
norm! h
638+
endif
639+
endwhile
640+
641+
" Reset cursor position and last visual mode
642+
call setpos(".",ppos)
643+
call setpos("'<",svpos)
644+
call setpos("'>",evpos)
645+
call visualmode(pvmode)
646+
647+
return [sfline,sfcol,mfline,mfcol,efline,efcol]
648+
endfunction " }}}1
585649

586650
nnoremap <silent> <Plug>SurroundRepeat .
587651
nnoremap <silent> <Plug>Dsurround :<C-U>call <SID>dosurround(<SID>inputtarget())<CR>

0 commit comments

Comments
 (0)