branch: elpa/magit
commit 1f5f15558f8899dac082dbd82e8ff15f4b58efe7
Author: Jonas Bernoulli <jo...@bernoul.li>
Commit: Jonas Bernoulli <jo...@bernoul.li>

    Restore window-start after refreshing a buffer
---
 CHANGELOG          |  4 ++++
 lisp/magit-mode.el | 22 +++++++++++++++++++---
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 8d99b3e3b8..5ab8e3c011 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,10 @@
 # -*- mode: org -*-
 * v4.3.7    UNRELEASED
 
+- Refreshing a buffer causes its content to be recreated, which can
+  result in scrolling.  Now we attempt to restore the display-start
+  positions of the windows displaying the buffer.  #5403
+
 - When running ~git~ for side-effects and that signals an error, we
   did not augment the error message to inform the user that the full
   output can be found in the process buffer.  When the error is not
diff --git a/lisp/magit-mode.el b/lisp/magit-mode.el
index ecd34033c5..12d594c65f 100644
--- a/lisp/magit-mode.el
+++ b/lisp/magit-mode.el
@@ -1120,16 +1120,32 @@ Run hooks `magit-pre-refresh-hook' and 
`magit-post-refresh-hook'."
                (and-let* ((section (magit-section-at)))
                  `((,window
                     ,section
-                    ,@(magit-section-get-relative-position section)))))))
+                    ,@(magit-section-get-relative-position section)
+                    ,@(and-let* ((ws (magit-section-at (window-start))))
+                        (list ws
+                              (car (magit-section-get-relative-position ws))
+                              (window-start)))))))))
          (get-buffer-window-list buffer nil t)))
       (and-let* ((section (magit-section-at)))
         `((nil ,section ,@(magit-section-get-relative-position section))))))
 
 (defun magit--refresh-buffer-set-positions (positions)
-  (pcase-dolist (`(,window ,section ,line ,char) positions)
+  (pcase-dolist
+      (`(,window ,section ,line ,char ,ws-section ,ws-line ,window-start)
+       positions)
     (if window
         (with-selected-window window
-          (magit-section-goto-successor section line char))
+          (magit-section-goto-successor section line char)
+          (cond
+           ((or (not window-start)
+                (> window-start (point))))
+           ((magit-section-equal ws-section (magit-section-at window-start))
+            (set-window-start window window-start t))
+           ((when-let ((pos (save-excursion
+                              (and (magit-section-goto-successor--same
+                                    ws-section ws-line 0)
+                                   (point)))))
+              (set-window-start window pos t)))))
       (magit-section-goto-successor section line char))))
 
 (defun magit-revert-buffer (_ignore-auto _noconfirm)

Reply via email to