Skip to content

Commit ec579a5

Browse files
committed
Merge pull request #131 from shanesmith/master
Keep HTML attributes when changing tags
2 parents fd75eb2 + 5d6b91c commit ec579a5

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

doc/surround.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ code blocks in C-style languages, <C-}> (which is really <C-]>) adds braces on
130130
lines separate from the content.
131131

132132
If t or < is used, Vim prompts for an HTML/XML tag to insert. You may specify
133-
attributes here and they will be stripped from the closing tag. End your
134-
input by pressing <CR> or >. If <C-T> is used, the tags will appear on lines
135-
by themselves.
133+
attributes here and they will be stripped from the closing tag. If replacing a
134+
tag, its attributes are kept in the new tag. End your input with > to discard
135+
the those attributes. If <C-T> is used, the tags will appear on lines by
136+
themselves.
136137

137138
If s is used, a leading but not trailing space is added. This is useful for
138139
removing parentheses from a function call with csbs.

plugin/surround.vim

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,12 @@ function! s:process(string)
124124
return s
125125
endfunction
126126

127-
function! s:wrap(string,char,type,...)
127+
function! s:wrap(string,char,type,removed,special)
128128
let keeper = a:string
129129
let newchar = a:char
130130
let s:input = ""
131131
let type = a:type
132132
let linemode = type ==# 'V' ? 1 : 0
133-
let special = a:0 ? a:1 : 0
134133
let before = ""
135134
let after = ""
136135
if type ==# "V"
@@ -171,7 +170,7 @@ function! s:wrap(string,char,type,...)
171170
if !maparg(">","c")
172171
let dounmapb = 1
173172
" Hide from AsNeeded
174-
exe "cn"."oremap > <CR>"
173+
exe "cn"."oremap > ><CR>"
175174
endif
176175
let default = ""
177176
if newchar ==# "T"
@@ -181,18 +180,24 @@ function! s:wrap(string,char,type,...)
181180
let default = matchstr(s:lastdel,'<\zs.\{-\}\ze>')
182181
endif
183182
let tag = input("<",default)
184-
echo "<".substitute(tag,'>*$','>','')
185183
if dounmapb
186184
silent! cunmap >
187185
endif
188186
let s:input = tag
189187
if tag != ""
188+
let keepAttributes = ( match(tag, ">$") == -1 )
190189
let tag = substitute(tag,'>*$','','')
190+
let attributes = ""
191+
if keepAttributes
192+
let attributes = matchstr(a:removed, '<[^ \t\n]\+\zs\_.\{-\}\ze>')
193+
endif
191194
let s:input = tag . '>'
192-
let before = '<'.tag.'>'
193195
if tag =~ '/$'
196+
let tag = substitute(tag, '/$', '', '')
197+
let before = '<'.tag.attributes.' />'
194198
let after = ''
195199
else
200+
let before = '<'.tag.attributes.'>'
196201
let after = '</'.substitute(tag,' .*','','').'>'
197202
endif
198203
if newchar == "\<C-T>" || newchar == ","
@@ -246,7 +251,7 @@ function! s:wrap(string,char,type,...)
246251
let after = ''
247252
endif
248253
let after = substitute(after ,'\n','\n'.initspaces,'g')
249-
if type ==# 'V' || (special && type ==# "v")
254+
if type ==# 'V' || (a:special && type ==# "v")
250255
let before = substitute(before,' \+$','','')
251256
let after = substitute(after ,'^ \+','','')
252257
if after !~ '^\n'
@@ -259,7 +264,7 @@ function! s:wrap(string,char,type,...)
259264
endif
260265
if before !~ '\n\s*$'
261266
let before .= "\n"
262-
if special
267+
if a:special
263268
let before .= "\t"
264269
endif
265270
endif
@@ -289,11 +294,10 @@ function! s:wrap(string,char,type,...)
289294
return keeper
290295
endfunction
291296

292-
function! s:wrapreg(reg,char,...)
297+
function! s:wrapreg(reg,char,removed,special)
293298
let orig = getreg(a:reg)
294299
let type = substitute(getregtype(a:reg),'\d\+$','','')
295-
let special = a:0 ? a:1 : 0
296-
let new = s:wrap(orig,a:char,type,special)
300+
let new = s:wrap(orig,a:char,type,a:removed,a:special)
297301
call setreg(a:reg,new,type)
298302
endfunction
299303
" }}}1
@@ -314,7 +318,7 @@ function! s:insert(...) " {{{1
314318
set clipboard-=unnamed clipboard-=unnamedplus
315319
let reg_save = @@
316320
call setreg('"',"\r",'v')
317-
call s:wrapreg('"',char,linemode)
321+
call s:wrapreg('"',char,"",linemode)
318322
" If line mode is used and the surrounding consists solely of a suffix,
319323
" remove the initial newline. This fits a use case of mine but is a
320324
" little inconsistent. Is there anyone that would prefer the simpler
@@ -442,7 +446,7 @@ function! s:dosurround(...) " {{{1
442446
call setreg('"',keeper,regtype)
443447
if newchar != ""
444448
let special = a:0 > 2 ? a:3 : 0
445-
call s:wrapreg('"',newchar, special)
449+
call s:wrapreg('"',newchar,removed,special)
446450
endif
447451
silent exe 'norm! ""'.pcmd.'`['
448452
if removed =~ '\n' || okeeper =~ '\n' || getreg('"') =~ '\n'
@@ -518,7 +522,7 @@ function! s:opfunc(type,...) " {{{1
518522
let keeper = substitute(keeper,'\_s\@<!\s*$','','')
519523
endif
520524
call setreg(reg,keeper,type)
521-
call s:wrapreg(reg,char,a:0 && a:1)
525+
call s:wrapreg(reg,char,"",a:0 && a:1)
522526
if type ==# "v" && a:type !=# "v" && append != ""
523527
call setreg(reg,append,"ac")
524528
endif

0 commit comments

Comments
 (0)