Monday, March 14, 2022

Re: Avoid jumping cursor after undo


On Mon, Mar 14, 2022, 14:10 Lifepillar <lifepillar@lifepillar.me> wrote:
I have defined this function:

fun! Lint()
  let l:view = winsaveview()
  keepjumps normal gggqG
  call winrestview(l:view)
endf

When the function is executed, the cursor does not move, as expected.
Unfortunately, pressing u after that makes the cursor jump to the top of
the buffer. I had hoped that keepjumps would prevent that, but that does
not seem the case. How can I avoid moving the cursor upon undoing
(without touching registers)?

Thanks,
Life.

I don't know of a built-in way, but maybe you could write a function as above that saves and restores the view, though it would be tricky if the undo adds or removes lines above the cursor location. Then, map u to it.

It's easy to figure out whether the undo added or removed lines (and how many). Then, you could look at where the cursor has ended up. If it's before your original location, then you need to adjust the values you pass to winrestview to move the return location up or down by the number of rows added. 

It'll probably still be less than perfect if the change is right around the cursor location, but those don't move the cursor much. 

Salman

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CANuxnEe_vn87LiVR%2Bf-rOPQsCJaYTnDcXEh7BhwHdrRDuE1cww%40mail.gmail.com.

No comments:

Post a Comment