tommcdo / vim-exchange

Easy text exchange operator for Vim
MIT License
756 stars 23 forks source link

Work around Vim bug fixed in patch 7.3.411 #18

Closed bps closed 10 years ago

bps commented 10 years ago

I was curious as to why exchange silently failed under the system version of Vim on OS X 10.9.1, when it worked fine under MacVim snapshot 72. I dug into it and found that the default register (") was being overwritten upon paste with the visually-selected text — which meant that the exchange was effectively a noop.

OS X 10.9.1 ships with Vim 7.3 (and no patches), whereas MacVim snapshot 72 is Vim 7.4 and patches 1–52. I searched the changelog and found patch 7.3.411:

Pasting in Visual mode using the "" register does not work.

Pasting from register 0 works around the bug:

diff --git a/plugin/exchange.vim b/plugin/exchange.vim
index 0f2902d..7effdb1 100644
--- a/plugin/exchange.vim
+++ b/plugin/exchange.vim
@@ -7,12 +7,12 @@ function! s:exchange(x, y, reverse)
        call setpos("'a", a:y[2])
        call setpos("'b", a:y[3])
        call setreg('"', a:x[0], a:x[1])
-       silent exe "normal! `a" . a:y[1] . "`b\"\"p"
+       silent exe "normal! `a" . a:y[1] . "`b\"0p"

        call setpos("'a", a:x[2])
        call setpos("'b", a:x[3])
        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\"0p"

        if a:reverse
                call cursor(a:x[2][1], a:x[2][2])

It would also work to use a register other than the default, saving it up top and restoring it before returning. I can submit a pull request if you'd like — if so let me know which way you prefer.

tommcdo commented 10 years ago

I would prefer using another named register over "0; I feel it would be more reliable. If you'd like to submit a PR with that change I'd be happy to accept it :)