Skip to content

diff-hl-revert-hunk cause cursor move, should remain on the same line and column after a revert. #229

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

Open
eval-exec opened this issue Dec 19, 2024 · 2 comments

Comments

@eval-exec
Copy link

eval-exec commented Dec 19, 2024

Hello, thanks for you diff-hl project, it helps me a lot.

When I edited a file, and want to revert to by diff-hl-revert-hunk, after revert, the cursor will moved.

I think cursor should be stick to same line and same column after diff-hl-revert-hunk.

What do you think? Thank you.

@eval-exec
Copy link
Author

I write a hack to make this:

  (defun exec/save-cursor-position-and-revert-hunk (orig-fun &rest args)
	"Advice to remember the cursor position and restore it after reverting a hunk."
	(let ((line (line-number-at-pos))
           (column (current-column)))
      (apply orig-fun args)
      (goto-char (point-min))
      (forward-line (1- line))
      (move-to-column column)))

  (advice-add 'diff-hl-revert-hunk :around #'exec/save-cursor-position-and-revert-hunk)

@dgutov
Copy link
Owner

dgutov commented Dec 20, 2024

Hi!

Do you mean specifically the scenario when point is inside the hunk? Why do we stay on the same column, though? There seems to be no guarantee that the text under point does not change, or itself move left or right.

When happens currently, is the region is replaced wholesale and point is moved out because the region's text can (and often does) change a lot.

The advice you posted should indeed work, but I wonder if it'll make sense in more cases that it will get things more confusing (e.g. when the hunk was a long addition, so the revert removes it all, but point will jump the corresponding number of lines down).

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

No branches or pull requests

2 participants