vim-clang-format
vim-clang-format copied to clipboard
Cursor position not maintained after undo
To replicate:
-
shift+v
to visually select a line -
,cf
to run clang format (cursor is in correct position) -
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
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.
It seems that undo information moves cursor. I've found :GoFmt
has the same issue. :undojoin
doesn't seem to help this issue solve...
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.
+1
vim-go plugin got it solve here: https://github.com/fatih/vim-go/issues/40
after much struggle... by the way.
Oh, it sounds great! I'll check the issue later. Thank you for the information.
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.
OK, thanks. I'll check commits in the pull request.
I think restoring undo history by :wundo
and :rundo
is the one.
indeed.
@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
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 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.
Anyway, if my comment can't help, my opinion is that, having an extra undo point is less worse than missing one.
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.
just like to point out that the guys on rust.vim were able to implement this based upon @fatih's work.
@rhysd maybe you have closed it by accident through commit comment? "doesn't resolve #8", I think GitHub doesn't understand negatives.
@rhysd yes, it's currently moving to the top.
thanks... I didn't notice the comment close this issue.
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.
I believe this problem is also causing my marks to be cleared
@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?