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

From: Juri Linkov <[email-masked]>
Date: Wed, 21 Feb 2024 19:18:17 +0200

>> (setq-default window-use-context-function
>>                (lambda (w context)
>>                  (let ((point (window-point w)))
>>                    (with-current-buffer (window-buffer w)
>
> I suppose that in the general case you need a
>
> (save-excursion

Thanks.  I just noticed this is broken in two windows case,
and your suggestion fixed it.

> Probably something similar is needed for dired, for example to handle
> the case where its buffer is simultaneously shown in two windows with
> their points on different lines.

So here is the same for dired:

diff --git a/lisp/dired.el b/lisp/dired.el
index 9e3b888df14..0510db1c409 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -2743,6 +2745,26 @@ dired-mode
               '(dired-font-lock-keywords t nil nil beginning-of-line))
   (setq-local desktop-save-buffer 'dired-desktop-buffer-misc-data)
   (setq-local grep-read-files-function #'dired-grep-read-files)
+  (setq-local window-set-context-function
+              (lambda (w)
+                (let ((point (window-point w)))
+                  (with-current-buffer (window-buffer w)
+                    (save-excursion
+                      (goto-char point)
+                      (if-let ((f (dired-get-filename nil t)))
+                          `((dired-filename . ,f))
+                        `((position . ,(point)))))))))
+  (setq-local window-use-context-function
+              (lambda (w context)
+                (let ((point (window-point w)))
+                  (with-current-buffer (window-buffer w)
+                    (save-excursion
+                      (if-let ((f (alist-get 'dired-filename context)))
+                          (dired-goto-file f)
+                        (when-let ((p (alist-get 'position context)))
+                          (goto-char p)))
+                      (setq point (point))))
+                  (set-window-point w point))))
   (setq dired-switches-alist nil)
   (hack-dir-local-variables-non-file-buffer) ; before sorting
   (dired-sort-other dired-actual-switches t)