chrisbra / Recover.vim

A Plugin to show a diff, whenever recovering a buffer
http://www.vim.org/scripts/script.php?script_id=3068
247 stars 25 forks source link

splitright/tagbar/feedkeys issues #5

Closed lyokha closed 12 years ago

lyokha commented 12 years ago

Hi Christian.

I found 2 new issues and 1 workaround for feedkeys() in CheckRecover(). Here is a patch (i do not know how attach file here), i followed your indentation rules:

--- autoload/recover.vim    2012-04-02 15:24:39.449061803 +0400
+++ autoload/recover.vim.new    2012-04-02 15:24:28.647890097 +0400
@@ -70,7 +70,12 @@
        call recover#DiffRecoveredFile()
        " Not sure, why this needs feedkeys
        " Sometimes cursor is wrong, I hate when this happens
-       call feedkeys(":wincmd p\n:0\n", 't')
+       " Cursor is wrong only when there is a single buffer open, a simple
+       " workaround for this is to check if bufnr('') is 1: in this case
+       " ':wincmd l\n:0\n' must be fed
+       if bufnr('') == 1
+       call feedkeys(":wincmd l\n:0\n", 't')
+       endif
    endif
    let b:did_recovery = 1
     endif
@@ -104,7 +109,10 @@
     let b:mod='recovered version'
     let l:filetype = &ft
     " saved version
+    let curspr = &spr
+    set nospr
     noa vert new
+    let &l:spr = curspr
     0r #
     $d _
     if l:filetype != ""
@@ -116,7 +124,7 @@
     setl noswapfile buftype=nowrite bufhidden=delete nobuflisted
     let b:mod='unmodified version on-disk'
     let swapbufnr=bufnr('')
-    noa wincmd p
+    noa wincmd l
     let b:swapbufnr = swapbufnr
     command! -buffer RecoverPluginFinish :FinishRecovery
     command! -buffer FinishRecovery :call recover#RecoverFinish()
  1. Let's start with second chunk: i just found that Recover do not work for me exactly as expected, because i use 'set splitright' in my .vimrc. The easiest way to fix it - is just to set nospr temporarily just before 'vert' as it is done in the second chunk.
  2. Replacement of 'wincmd p' to 'wincmd l' is due to interference with tagbar (and with taglist as well i believe) which regards 'previous' window on its own and therefore cursor moves inside it. 'wincmd l' works fine as far as buffer to edit is always on the right.
  3. I found that 'wincmd p' (now 'wincmd l') are used second time in CheckRecover() and there is verbose comment on this. I check that this strange cursor behaviour really takes place but only when a buffer to recover is the first open in vim. If i open buffer to recover alongside with already open files then this issue go away: but in this case cursor moves 'correctly' to the window on the right of open buffer, which is wrong. The first chunk just tries to figure out if it is the first buffer in vim or there are others (just testing its bufnr('') against 1) - it works fine for me: now cursor takes correct place in all cases - whether or not the open buffer is the first open buffer in vim or not.
lyokha commented 12 years ago

Additional comments: it is better to change 1st chunk to:

if bufnr('') ==1 call feedkeys(":wincmd l\n", 't') endif call feedkeys(":0\n", 't')

Also i found 2 new issues when one or more buffers are already open in current vim instance:

  1. b:swapname gets undefined in recover#RecoverFinish():4, simple solution is to define local variable before bd!, and also delcommand must also be moved before bd!
  2. after 2nd diffoff cursor may move to wrong window (e.g. left to the open recovered buffer), simple solution: remember bufnr('') and then move there after all is finished.

Here is final patch:

--- autoload/recover.vim    2012-04-02 17:09:12.698403668 +0400
+++ autoload/recover.vim.new    2012-04-02 16:52:45.761707457 +0400
@@ -70,7 +70,13 @@
        call recover#DiffRecoveredFile()
        " Not sure, why this needs feedkeys
        " Sometimes cursor is wrong, I hate when this happens
-       call feedkeys(":wincmd p\n:0\n", 't')
+       " Cursor is wrong only when there is a single buffer open, a simple
+       " workaround for that is to check if bufnr('') is 1: in this case
+       " ':wincmd l\n:0\n' must be fed
+       if bufnr('') == 1
+       call feedkeys(":wincmd l\n", 't')
+       endif
+       call feedkeys(":0\n", 't')
    endif
    let b:did_recovery = 1
     endif
@@ -104,7 +110,10 @@
     let b:mod='recovered version'
     let l:filetype = &ft
     " saved version
+    let curspr = &spr
+    set nospr
     noa vert new
+    let &l:spr = curspr
     0r #
     $d _
     if l:filetype != ""
@@ -116,7 +125,7 @@
     setl noswapfile buftype=nowrite bufhidden=delete nobuflisted
     let b:mod='unmodified version on-disk'
     let swapbufnr=bufnr('')
-    noa wincmd p
+    noa wincmd l
     let b:swapbufnr = swapbufnr
     command! -buffer RecoverPluginFinish :FinishRecovery
     command! -buffer FinishRecovery :call recover#RecoverFinish()
@@ -182,13 +191,16 @@
 endfun

 fu! recover#RecoverFinish() abort "{{{1
+    let swapname = b:swapname
+    let curbufnr = bufnr('')
+    delcommand FinishRecovery
     exe bufwinnr(b:swapbufnr) " wincmd w"
     diffoff
     bd!
-    call delete(b:swapname)
-    delcommand FinishRecovery
+    call delete(swapname)
     diffoff
     call s:ModifySTL(0)
+    exe bufwinnr(curbufnr) " wincmd w"
     unlet! b:swapname b:did_recovery b:swapbufnr
 endfun

Regards, Alexey.

chrisbra commented 12 years ago

Thanks for your feedback . I'll include it.