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

From: Stephen Berman <[email-masked]>
Date: Mon, 05 Jun 2017 15:37:47 +0200

On Sun, 04 Jun 2017 16:45:05 -0700 Antoine Levitt <[email-masked]> wrote:

> Tested on git master, the bug is not present on 25.
>
> Recipe from emacs -Q:
>
> (setq dired-auto-revert-buffer t)
>
> open dired, open a folder, C-x b back to the previous dired buffer, open
> the same folder again, see point jump back to the beginning of buffer.
>
> I'm not sure why, since dired-auto-revert-buffer just seems to call
> revert-buffer, which does not jump point.

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.

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

Steve Berman

diff --git a/lisp/dired.el b/lisp/dired.el
index 8396652d50..26d3d76817 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -1382,9 +1382,13 @@ dired-restore-positions
   (let* ((buf-file-pos (nth 0 positions))
 	 (buffer (nth 0 buf-file-pos)))
     (unless (and (nth 1 buf-file-pos)
-		 (dired-goto-file (nth 1 buf-file-pos)))
+		 (prog1 (dired-goto-file (nth 1 buf-file-pos))
+		   (set-window-buffer nil buffer)
+		   (set-window-point (selected-window) (nth 2 buf-file-pos))))
       (goto-char (nth 2 buf-file-pos))
-      (dired-move-to-filename))
+      (dired-move-to-filename)
+      (set-window-buffer nil buffer)
+      (set-window-point (selected-window) (nth 2 buf-file-pos)))
     (dolist (win-file-pos (nth 1 positions))
       ;; Ensure that window still displays the original buffer.
       (when (eq (window-buffer (nth 0 win-file-pos)) buffer)
@@ -1392,7 +1396,8 @@ dired-restore-positions
 	  (unless (and (nth 1 win-file-pos)
 		       (dired-goto-file (nth 1 win-file-pos)))
 	    (goto-char (nth 2 win-file-pos))
-	    (dired-move-to-filename)))))))
+	    (dired-move-to-filename)
+	    (set-window-point nil (point))))))))
 
 (defun dired-remember-marks (beg end)
   "Return alist of files and their marks, from BEG to END."