Subject: Re: bug#33871: 27.0.50; Revert Dired window saved in window configuration

From: Juri Linkov <[email-masked]>
Date: Thu, 17 Feb 2022 19:28:18 +0200

>> Instead of (goto-char p-m), after (set-window-configuration w-c)
>> something like this should be performed:
>>
>>    (dired-goto-file (car (cdr (assoc (current-buffer)
>>                                      (window-parameter nil 'dired-positions)))))
>
> Once more: Your scenario is
>
> (progn (dired "/tmp") (dired-next-line 1)
>        (split-window) (other-window 1)
>        (dired "/tmp") (dired-next-line 2)
>        (let ((w-c (current-window-configuration))
> 	     (p-m (point-marker)))
> 	 (view-emacs-todo) (delete-other-windows)
> 	 (with-current-buffer (get-buffer "tmp")
> 	   (revert-buffer))
> 	 (set-window-configuration w-c)
> 	 (goto-char p-m)))
>
> But this cannot work with dired buffers because they are reverted in a
> special way that does not care about 'window-point' and the like.  This
> means that the value of 'point-marker' you saved in p-m has become just
> meaningless after the 'revert-buffer' call.
>
> Basically, this is a hard problem we'll probably never be able to solve
> satisfactorily.

But this works fine:

  (progn (dired "/tmp") (dired-next-line 1)
         (split-window) (other-window 1)
         (dired "/tmp") (dired-next-line 2)
         (walk-windows
          (lambda (w)
            (set-window-parameter
             w 'dired-positions
             (list (with-selected-window w
                     (dired-get-filename nil t))
                   (window-start w)
                   (window-point w)))))
         (let ((w-c (current-window-configuration)))
           (view-emacs-todo) (delete-other-windows)
           (with-current-buffer (get-buffer "tmp")
             (revert-buffer))
           (set-window-configuration w-c)
           (walk-windows
            (lambda (w)
              (let ((pos (window-parameter w 'dired-positions)))
                (when pos
                  (with-selected-window w
                    (dired-goto-file (nth 0 pos)))
                  (set-window-start w (nth 1 pos))
                  (set-window-point w (nth 2 pos))))))))

> We could handle 'window-point' separately when saving and restoring
> window configurations and the window's buffer is a dired buffer.  For
> example, 'dired-revert' could call a function 'window-revert-timestamp'
> to store the current time of the revert operation for this window and
> 'current-window-configuration' (and 'window-state-get') would store
> their current time stamp in the configuration (or state).  Then
> 'set-window-configuration' (and 'window-state-put') could check whether
> a window's revert time stamp is larger than the time stamp stored in the
> configuration and not set point for that window when the revert time
> stamp is later.  Is it worth the hassle?

I think it's not worth the hassle.  The above solution is simpler.