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

From: martin rudalics <[email-masked]>
Date: Wed, 23 Feb 2022 10:31:41 +0100

> Like in bug#54038 (about having access to windows in window configurations),
 > the problem is that there is no way to update and even peek inside
 > a window configuration.  In bug#54038 the cleanup function can't tell
 > if a window is still live in a window configuration.

We might be able to provide such a function but can you tell me why you
would need it and how you would use it?

 > And here 'dired-revert' can't update windows saved in a window configuration.

Updating window configurations can be hairy.  We have no function for
testing the validity of a configuration - we simply rely on the fact
that it is a 1-to-1 copy of original sizes and parent-child
relationships.  For example: When we resize a window we check the new
sizes of all windows on that frame before storing them.  When we restore
a window configuration, we simply store back the old sizes and then
maybe try to resize the windows if the frame size changed in between.

 > 'dired-save-positions' goes to great lengths to employ get-buffer-window-list,
 > and walk-windows with window-prev-buffers to update the Dired buffer
 > even in window-prev-buffers.  And 'dired-restore-positions' with the
 > same number of lines tries to restore all prev-buffers.
 >
 > But still 'dired-revert' fails to update the Dired buffer in
 > window configurations saved in tabs, or in winner.el, or
 > in 'C-x r w' (window-configuration-to-register), etc.

As a first step we could try to do that for window states.

 > A possible solution would be to add two hooks:
 >
 > 1. hook before finishing 'current-window-configuration' that will add all
 > windows to some variable that the cleanup function could check in bug#54038.
 > This is like 'dired-save-positions'.

We can easily add such a hook - an abnormal, buffer local one probably
that would be set by dired whenever it shows a directory in a window and
could get as argument the frame and/or the window(s) and probably the
return value of 'current-window-configuration' so dired can match it
with later invocations of 'set-window-configuration'.

 > 2. hook after calling 'set-window-configuration' that will restore
 > previous positions.  This is like 'dired-restore-positions' that
 > uses 'dired-goto-file'.

We can do that in a similar fashion.  But: Updating your "variable" when
dired does something (like copying, deleting or renaming files) would
still have to be synchronized with the position of, for example,
'window-point' in that window when the window is still live at the time
'set-window-configuration' is called.  So we might need yet another hook
called at the beginning of 'set-window-configuration' that allows dired
to check whether the window stored in your variable is live, take the
position of point from there if it is and keep it in the second hook.

(Note in this context: We traditionally say in the Elisp manual that "As
a special exception, the window configuration does not record the value
of point in the selected window for the current buffer." which is not
precise IIUC.  It does record the value but 'set-window-configuration'
does not or not necessarily restore it.)

martin