vim-clang-format icon indicating copy to clipboard operation
vim-clang-format copied to clipboard

Cursor position not maintained after undo

Open bear24rw opened this issue 10 years ago • 22 comments

To replicate:

  1. shift+v to visually select a line
  2. ,cf to run clang format (cursor is in correct position)
  3. u to undo (cursor is now at top of file)

Is there a way to have the cursor remain in the original position after the undo?

clang-format version 3.4

bear24rw avatar Apr 03 '14 22:04 bear24rw

Thank you for your report, @bear24rw.

Actually, I've already noticed this issue but I don't know a way to fix. I still investigate the way. :keepjump can avoid modifying jumplist, but not for undolist. :undojoin may be able to do it.

rhysd avatar Apr 05 '14 04:04 rhysd

It seems that undo information moves cursor. I've found :GoFmt has the same issue. :undojoin doesn't seem to help this issue solve...

rhysd avatar Apr 05 '14 06:04 rhysd

I investigated the way not to move a cursor at undo but I couldn't... I'll remain this issue open. If anyone knows how to fix this issue, please let me know with a comment. Thank you in advance.

rhysd avatar Apr 05 '14 14:04 rhysd

+1

vim-go plugin got it solve here: https://github.com/fatih/vim-go/issues/40

oblitum avatar Oct 02 '14 23:10 oblitum

after much struggle... by the way.

oblitum avatar Oct 02 '14 23:10 oblitum

Oh, it sounds great! I'll check the issue later. Thank you for the information.

rhysd avatar Oct 02 '14 23:10 rhysd

The commits for the solution in vim-go are 8f86bbce and c58d85b5, right?

rhysd avatar Oct 03 '14 03:10 rhysd

I don't know exactly which ones fixes all the problems. He told me to track pull request 150 at the time. The first one you refer to seems to be the most relevant.

oblitum avatar Oct 03 '14 03:10 oblitum

OK, thanks. I'll check commits in the pull request. I think restoring undo history by :wundo and :rundo is the one.

rhysd avatar Oct 03 '14 03:10 rhysd

indeed.

oblitum avatar Oct 03 '14 03:10 oblitum

@rhysd hi, I'm just looking around, and your last commits suffer from the side-effect mentioned at the beginning of https://github.com/fatih/vim-go/issues/40. You can just follow the procedure I mention there using a C++ file, the same thing will happen.

oblitum avatar Oct 08 '14 00:10 oblitum

@oblitum Actually, the procedure in vim-go couldn't help this issue solve. The cursor still moved even if I used :wundo and :rundo. This is because vim-clang-format replaces the current buffer by Vim command although vim-go rewrites the buffer directly by go fmt, I think. So I did workaround as first aid.

rhysd avatar Oct 08 '14 01:10 rhysd

@rhysd I'm wondering whether you have stumbled upon an issue vim-go author has gone through while he asked me to test his solution. He had issues with BufWritePre, which can be checked from this comment. If you look at the changes applied, you can see he documents the issue regarding BufWritePre.

oblitum avatar Oct 08 '14 01:10 oblitum

Anyway, if my comment can't help, my opinion is that, having an extra undo point is less worse than missing one.

oblitum avatar Oct 08 '14 01:10 oblitum

Check out this file:

https://github.com/fatih/vim-go/blob/master/autoload/go/fmt.vim#L44

All the functionality is there and I've tried to write many comments for each step so it can be followed.

fatih avatar Oct 08 '14 06:10 fatih

just like to point out that the guys on rust.vim were able to implement this based upon @fatih's work.

oblitum avatar Nov 28 '15 05:11 oblitum

@rhysd maybe you have closed it by accident through commit comment? "doesn't resolve #8", I think GitHub doesn't understand negatives.

oblitum avatar Jan 08 '17 13:01 oblitum

@rhysd yes, it's currently moving to the top.

oblitum avatar Jan 08 '17 13:01 oblitum

thanks... I didn't notice the comment close this issue.

rhysd avatar Jan 08 '17 13:01 rhysd

I have found a temporary solution. It might be of help to others, until this issue is fixed. I have put the following in my .vimrc (init.vim) file. This save the cursor position and scroll position, before undo / redo, then restores it after calling the builtin undo / redo command.

function! s:safeundo()
    let s:pos = getpos( '. ')
    let s:view = winsaveview()        
    undo
    call setpos( '.', s:pos )
    call winrestview( s:view )
endfunc

function! s:saferedo()
    let s:pos = getpos( '.' )
    let s:view = winsaveview()
    redo
    call setpos( '.', s:pos )
    call winrestview( s:view )
endfunc

nnoremap u : SafeUndo <CR>
nnoremap <C-u> : SafeRedo <CR>

I am a vimscript novice, so please feel free to critique.

jmnel avatar Jul 16 '17 17:07 jmnel

I believe this problem is also causing my marks to be cleared

TMVector avatar Aug 08 '17 16:08 TMVector

@jmnel I got "not an editor command" error when trying to undo with key "u", after adding your script to my .vimrc. Does it work for you?

junzebao avatar Oct 17 '17 08:10 junzebao