Maintain cursor position when exchanging

Closes #16.
This commit is contained in:
Tom McDonald 2014-01-01 11:32:54 -05:00
parent 39895312ec
commit 53b2b497b0

View File

@ -1,4 +1,4 @@
function! s:exchange(x, y) function! s:exchange(x, y, reverse)
let a = getpos("'a") let a = getpos("'a")
let b = getpos("'b") let b = getpos("'b")
let reg = getreg('"') let reg = getreg('"')
@ -14,6 +14,12 @@ function! s:exchange(x, y)
call setreg('"', a:y[0], a:y[1]) call setreg('"', a:y[0], a:y[1])
silent exe "normal! `a" . a:x[1] . "`b\"\"p" silent exe "normal! `a" . a:x[1] . "`b\"\"p"
if a:reverse
call cursor(a:x[2][1], a:x[2][2])
else
call cursor(a:y[2][1], a:y[2][2])
endif
call setpos("'a", a) call setpos("'a", a)
call setpos("'b", b) call setpos("'b", b)
call setreg('"', reg, reg_mode) call setreg('"', reg, reg_mode)
@ -54,16 +60,18 @@ function! s:exchange_set(type, ...)
else else
let exchange1 = b:exchange let exchange1 = b:exchange
let exchange2 = s:exchange_get(a:type, a:0) let exchange2 = s:exchange_get(a:type, a:0)
let reverse = 0
let cmp = s:compare(exchange1, exchange2) let cmp = s:compare(exchange1, exchange2)
if cmp == 0 if cmp == 0
echohl WarningMsg | echo "Exchange aborted: overlapping text" | echohl None echohl WarningMsg | echo "Exchange aborted: overlapping text" | echohl None
return s:exchange_clear() return s:exchange_clear()
elseif cmp > 0 elseif cmp > 0
let reverse = 1
let [exchange1, exchange2] = [exchange2, exchange1] let [exchange1, exchange2] = [exchange2, exchange1]
endif endif
call s:exchange(exchange1, exchange2) call s:exchange(exchange1, exchange2, reverse)
call s:exchange_clear() call s:exchange_clear()
endif endif
endfunction endfunction