Lokvin Wiki
Edit Page
Vim

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 40: Line 40:
 
" XML formatter
 
" XML formatter
 
function! DoFormatXML() range
 
function! DoFormatXML() range
" Save the file type
+
" Save the file type
let l:origft = &ft
+
let l:origft = &ft
  +
 
" Clean the file type
+
" Clean the file type
set ft=
+
set ft=
  +
 
" Add fake initial tag (so we can process multiple top-level elements)
+
" Add fake initial tag (so we can process multiple top-level elements)
exe ":let l:beforeFirstLine=" . a:firstline . "-1"
+
exe ":let l:beforeFirstLine=" . a:firstline . "-1"
if l:beforeFirstLine < 0
+
if l:beforeFirstLine < 0
let l:beforeFirstLine=0
+
let l:beforeFirstLine=0
endif
+
endif
exe a:lastline . "put ='</PrettyXML>'"
+
exe a:lastline . "put ='</PrettyXML>'"
exe l:beforeFirstLine . "put ='<PrettyXML>'"
+
exe l:beforeFirstLine . "put ='<PrettyXML>'"
exe ":let l:newLastLine=" . a:lastline . "+2"
+
exe ":let l:newLastLine=" . a:lastline . "+2"
if l:newLastLine > line('$')
+
if l:newLastLine > line('$')
let l:newLastLine=line('$')
+
let l:newLastLine=line('$')
endif
+
endif
  +
 
" Remove XML header
+
" Remove XML header
exe ":" . a:firstline . "," . a:lastline . "s/<\?xml\\_.*\?>\\_s*//e"
+
exe ":" . a:firstline . "," . a:lastline . "s/<\?xml\\_.*\?>\\_s*//e"
  +
 
" Recalculate last line of the edited code
+
" Recalculate last line of the edited code
let l:newLastLine=search('</PrettyXML>')
+
let l:newLastLine=search('</PrettyXML>')
  +
 
" Execute external formatter
+
" Execute external formatter
exe ":silent " . a:firstline . "," . l:newLastLine . "!xmllint --noblanks --format --recover -"
+
exe ":silent " . a:firstline . "," . l:newLastLine . "!xmllint --noblanks --format --recover -"
  +
 
" Recalculate first and last lines of the edited code
+
" Recalculate first and last lines of the edited code
let l:newFirstLine=search('<PrettyXML>')
+
let l:newFirstLine=search('<PrettyXML>')
let l:newLastLine=search('</PrettyXML>')
+
let l:newLastLine=search('</PrettyXML>')
 
 
" Get inner range
+
" Get inner range
let l:innerFirstLine=l:newFirstLine+1
+
let l:innerFirstLine=l:newFirstLine+1
let l:innerLastLine=l:newLastLine-1
+
let l:innerLastLine=l:newLastLine-1
  +
 
" Remove extra unnecessary indentation
+
" Remove extra unnecessary indentation
exe ":silent " . l:innerFirstLine . "," . l:innerLastLine "s/^ //e"
+
exe ":silent " . l:innerFirstLine . "," . l:innerLastLine "s/^ //e"
  +
 
" Remove fake tag
+
" Remove fake tag
exe l:newLastLine . "d"
+
exe l:newLastLine . "d"
exe l:newFirstLine . "d"
+
exe l:newFirstLine . "d"
  +
 
" Put the cursor at the first line of the edited code
+
" Put the cursor at the first line of the edited code
exe ":" . l:newFirstLine
+
exe ":" . l:newFirstLine
  +
 
" Restore the file type
+
" Restore the file type
exe "set ft=" . l:origft
+
exe "set ft=" . l:origft
 
endfunction
 
endfunction
 
command! -range=% FormatXML <line1>,<line2>call DoFormatXML()
 
command! -range=% FormatXML <line1>,<line2>call DoFormatXML()

Please note that all contributions to the Lokvin Wiki are considered to be released under the CC-BY-SA

Cancel Editing help (opens in new window)