Subject: Re: bug#27243: dired-auto-revert-buffer jumps point to beginning of buffer

From: Eli Zaretskii <[email-masked]>
Date: Mon, 05 Jun 2017 18:02:14 +0300

> From: Stephen Berman <[email-masked]>
> Date: Mon, 05 Jun 2017 15:37:47 +0200
> Cc: [email-masked]
> 
> I think I see why this happens.  I assume when you opened "the same
> folder again" you pressed RET on the line in the Dired listing.  That
> calls dired-find-file, which calls find-file, which first calls
> find-file-noselect (to see if it's a live buffer), which calls (via
> run-hook-with-args-until-success) dired-noselect, which calls (via
> dired-internal-noselect) revert-buffer (since dired-auto-revert-buffer
> is t), which calls dired-revert, which saves point (via
> dired-save-positions) but then erases the buffer and inserts it anew --
> and this is the problem, because now continuing in find-file, it calls
> switch-to-buffer (since it's a live buffer that you're revisiting),
> which sets window-point by consulting window-prev-buffers.  Prior to the
> invocation of erase-buffer, window-prev-buffers contained this entry for
> the buffer being reverted ("test" in my test case):
> 
> (#<buffer test> #<marker at 1 in test> #<marker at 232 in test>)
> 
> which shows window-point on the first file name in the Dired listing,
> but after erase-buffer it's now this:
> 
> (#<buffer test> #<marker at 1 in test> #<marker at 1 in test>)
> 
> Since dired-revert restored the saved point (232) but does not return it
> to the caller, switch-to-buffer does not have this information, but only
> what window-prev-buffers shows, which is now 1.  So that's what it sets
> window-point to.

Right, I see the same.

> One way to fix this is to make dired-restore-positions restore not only
> point but also window-point.  The attached patch does this.

An alternative is to bind switch-to-buffer-preserve-window-point to
nil (the whole problem was caused by the fact that this variable is
now non-nil by default, and dired-revert runs afoul of it, as you
point out):

--- lisp/dired.el~0	2017-02-28 06:27:41.000000000 +0200
+++ lisp/dired.el	2017-06-05 17:51:50.633645200 +0300
@@ -2126,7 +2126,11 @@
   (interactive)
   ;; Bind `find-file-run-dired' so that the command works on directories
   ;; too, independent of the user's setting.
-  (let ((find-file-run-dired t))
+  (let ((find-file-run-dired t)
+        (switch-to-buffer-preserve-window-point
+         (if dired-auto-revert-buffer
+             nil
+           switch-to-buffer-preserve-window-point)))
     (find-file (dired-get-file-for-visit))))
 
 (defun dired-find-alternate-file ()

I'm not sure which solution is better.  Maybe someone will come up
with a more elegant one.

Thanks.