diff --git a/plugin/exchange.vim b/plugin/exchange.vim index 439ac87..b4fe1db 100644 --- a/plugin/exchange.vim +++ b/plugin/exchange.vim @@ -41,10 +41,8 @@ function! s:exchange(x, y, reverse, expand) call winrestview(view) - if a:reverse - call cursor(a:x.start.line, a:x.start.column) - else - call cursor(a:y.start.line, a:y.start.column) + if !a:expand + call s:fix_cursor(a:x, a:y, a:reverse) endif let &selection = selection @@ -54,6 +52,20 @@ function! s:exchange(x, y, reverse, expand) call s:restore_reg('+', reg_plus) endfunction +function! s:fix_cursor(x, y, reverse) + if a:reverse + call cursor(a:x.start.line, a:x.start.column) + else + if a:x.start.line == a:y.start.line + let horizontal_offset = a:x.end.column - a:y.end.column + call cursor(a:x.start.line, a:x.start.column - horizontal_offset) + else + let vertical_offset = a:x.end.line - a:y.end.line + call cursor(a:x.start.line - vertical_offset, a:x.start.column) + endif + endif +endfunction + function! s:reindent(start, lines, new_indent) if (exists('b:exchange_indent') ? b:exchange_indent : g:exchange_indent) == '==' let lnum = nextnonblank(a:start)