Skip to content

Commit

Permalink
341: Add counts for replacement arguments
Browse files Browse the repository at this point in the history
Ported: tpope#341
  • Loading branch information
halostatue committed Aug 6, 2024
1 parent 848888d commit 2f6a7b6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 7 additions & 1 deletion doc/surround.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ All targets are currently just one character.

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

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

If a single digit number is used, the remaining replacement argument is
repeated by that count.

Old text Command New text ~
hello ysW2* **hello**

If t or < is used, Vim prompts for an HTML/XML tag to insert. You may specify
attributes here and they will be stripped from the closing tag. If replacing a
tag, its attributes are kept in the new tag. End your input with > to discard
Expand Down
20 changes: 19 additions & 1 deletion import/surround.vim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enddef
def InputReplacement(): string
var c = getcharstr()

if c == " "
if c =~ '[ 0-9]'
c ..= getcharstr()
endif

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

if newchar =~ '^[0-9]'
scount = newchar->strpart(0, 1)->str2nr()
newchar = newchar->strpart(1)
endif

if newchar =~ '^ '
newchar = newchar->strpart(1)
extraspace = ' '
Expand Down Expand Up @@ -258,6 +264,18 @@ def Wrap(str: string, char: string, wrapType: string, removed: string, linebreak
after = ''
endif

if before =~ '.*\n\t$'
before = repeat(before->substitute('\n\t', '', ''), scount) .. '\n\t'
else
before = repeat(before, scount)
endif

if after =~ '.*\n\t$'
after = repeat(after->substitute('\n\t', '', ''), scount) .. '\n\t'
else
after = repeat(after, scount)
endif

after = after->substitute('\n', '\n' .. initSpaces, 'g')

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

0 comments on commit 2f6a7b6

Please sign in to comment.