Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor position not maintained after undo #8

Open
bear24rw opened this issue Apr 3, 2014 · 22 comments · May be fixed by #61
Open

Cursor position not maintained after undo #8

bear24rw opened this issue Apr 3, 2014 · 22 comments · May be fixed by #61
Assignees

Comments

@bear24rw
Copy link

bear24rw commented Apr 3, 2014

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

@rhysd rhysd added the bug label Apr 4, 2014
@rhysd rhysd self-assigned this Apr 4, 2014
@rhysd
Copy link
Owner

rhysd commented Apr 5, 2014

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
Copy link
Owner

rhysd commented Apr 5, 2014

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
Copy link
Owner

rhysd commented Apr 5, 2014

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 added a commit that referenced this issue Jul 4, 2014
This feature has some side effects.  So you should be careful if you
introduce this feature.

- Sometime, screen is moved on auto-formatting.
- Cursor position history is changed (Issue #8)
@oblitum
Copy link

oblitum commented Oct 2, 2014

+1

vim-go plugin got it solve here: fatih/vim-go#40

@oblitum
Copy link

oblitum commented Oct 2, 2014

after much struggle... by the way.

@rhysd
Copy link
Owner

rhysd commented Oct 2, 2014

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

@rhysd
Copy link
Owner

rhysd commented Oct 3, 2014

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

@oblitum
Copy link

oblitum commented Oct 3, 2014

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.

@rhysd
Copy link
Owner

rhysd commented Oct 3, 2014

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

@oblitum
Copy link

oblitum commented Oct 3, 2014

indeed.

rhysd added a commit that referenced this issue Oct 6, 2014
…do (issue #8)

Replace current buffer with workaround not to move
the cursor on undo (issue #8)

The points are:
  - Do not touch the first line.
  - Use :put (p, P and :put! is not available).

To meet above condition:
  - Delete all lines except for the first line.
  - Put formatted text except for the first line.
@oblitum
Copy link

oblitum commented Oct 8, 2014

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

@rhysd
Copy link
Owner

rhysd commented Oct 8, 2014

@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.

@oblitum
Copy link

oblitum commented Oct 8, 2014

@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
Copy link

oblitum commented Oct 8, 2014

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

@fatih
Copy link

fatih commented Oct 8, 2014

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.

@oblitum
Copy link

oblitum commented Nov 28, 2015

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

@oblitum
Copy link

oblitum commented Jan 8, 2017

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

@oblitum
Copy link

oblitum commented Jan 8, 2017

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

@rhysd
Copy link
Owner

rhysd commented Jan 8, 2017

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

@rhysd rhysd reopened this Jan 8, 2017
@jmnel
Copy link

jmnel commented Jul 16, 2017

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.

@TMVector
Copy link

TMVector commented Aug 8, 2017

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

@junzebao
Copy link

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants