diff-hl icon indicating copy to clipboard operation
diff-hl copied to clipboard

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

Open eval-exec opened this issue 1 year ago • 2 comments

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 avatar Dec 19 '24 12:12 eval-exec

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)

eval-exec avatar Dec 19 '24 13:12 eval-exec

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

dgutov avatar Dec 20 '24 03:12 dgutov