Skip to content

Commit 5eaff40

Browse files
committed
feat: (341) Add counts for replacement arguments
Ported: tpope#341
1 parent a76799b commit 5eaff40

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

doc/surround.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ All targets are currently just one character.
9696

9797
Eight punctuation marks, (, ), {, }, [, ], <, and >, represent themselves
9898
and their counterparts. If the opening mark is used, contained whitespace is
99-
also trimmed. The targets b, B, r, and a are aliases for ), }, ], and >
99+
also trimmed. The targets b, B, r, and a are aliases for ), }, ], and >
100100
(the first two mirror Vim; the second two are completely arbitrary and
101101
subject to change).
102102

@@ -129,6 +129,12 @@ b, B, r, and a are aliases for ), }, ], and >. To fulfill the common need for
129129
code blocks in C-style languages, <C-}> (which is really <C-]>) adds braces on
130130
lines separate from the content.
131131

132+
If a single digit number is used, the remaining replacement argument is
133+
repeated by that count.
134+
135+
Old text Command New text ~
136+
hello ysW2* **hello**
137+
132138
If t or < is used, Vim prompts for an HTML/XML tag to insert. You may specify
133139
attributes here and they will be stripped from the closing tag. If replacing a
134140
tag, its attributes are kept in the new tag. End your input with > to discard

import/surround.vim

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ enddef
1818
def InputReplacement(): string
1919
var c = getcharstr()
2020

21-
if c == " "
21+
if c =~ '[ 0-9]'
2222
c ..= getcharstr()
2323
endif
2424

@@ -135,8 +135,14 @@ def Wrap(str: string, char: string, wrapType: string, removed: string, linebreak
135135
var before = ""
136136
var after = ""
137137
var initSpaces = linemode ? matchstr(keeper, '\%^\s*') : matchstr(getline('.'), '\%^\s*')
138+
var scount = 1
138139
var extraspace = ""
139140

141+
if newchar =~ '^[0-9]'
142+
scount = newchar->strpart(0, 1)->str2nr()
143+
newchar = newchar->strpart(1)
144+
endif
145+
140146
if newchar =~ '^ '
141147
newchar = newchar->strpart(1)
142148
extraspace = ' '
@@ -258,6 +264,18 @@ def Wrap(str: string, char: string, wrapType: string, removed: string, linebreak
258264
after = ''
259265
endif
260266

267+
if before =~ '.*\n\t$'
268+
before = repeat(before->substitute('\n\t', '', ''), scount) .. '\n\t'
269+
else
270+
before = repeat(before, scount)
271+
endif
272+
273+
if after =~ '.*\n\t$'
274+
after = repeat(after->substitute('\n\t', '', ''), scount) .. '\n\t'
275+
else
276+
after = repeat(after, scount)
277+
endif
278+
261279
after = after->substitute('\n', '\n' .. initSpaces, 'g')
262280

263281
if wrapType ==# 'V' || (linebreak && wrapType ==# "v")

0 commit comments

Comments
 (0)