I'm reformatting an XML document with an external command from a vimscript function which accepts a range, and I need the cursor to stay at the same line it was before the reformatting. When trying to save the current cursor line number with line('.')
or getpos('.')
, the line number is always '1' and restoring it makes no sense. Any idea? Here is the functionL
" formats the XML source
function s:DapsXmlFormat() range
" save the current cursor position as a mark
let winview = winsaveview()
" check if the current buffer is valid
if s:DapsValidateFile() == 0
let indent_size = indent(a:firstline) / shiftwidth()
let cmd = '!' . b:xmlformat_script . ' -f ' . b:xmlformat_conf
silent execute(a:firstline.','.a:lastline.cmd)
if a:firstline > 1 && a:lastline < line('$')
" re-indent the visual block
let repeat = repeat(">", indent_size)
" a:lastline is probably not valid anymore after re-formatting, need
" matchit's % to mark the vusual block correctly
silent execute("normal lV%" . indent_size . ">")
endif
endif
" go back to the saved cursor position
call winrestview(winview)
endfunction