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

From: Juri Linkov <[email-masked]>
Date: Fri, 16 Feb 2024 09:37:39 +0200

>>              ;; set-window-configuration does not restore the value of
>>              ;; point in the current buffer, so restore it separately.
>>              (when (and (markerp wc-point)
>>                         (marker-buffer wc-point)
>>                         ;; FIXME: After dired-revert, marker relocates to 1.
>>                         ;; window-configuration restores point to global point
>>                         ;; in this dired buffer, not to its window point,
>>                         ;; but this is slightly better than 1.
>>                         ;; Maybe better to save dired-filename in each window?
>>                         (not (eq 1 (marker-position wc-point))))
>>                (goto-char wc-point))
>>
>> Please note that this bug report is related to FIXME above.
>>
>> So a possible solution is also to save more context information
>> like dired-filename, and then restore it using dired-goto-file.
>
> We obviously then should strive for a solution that stores any kind of
> information via a hook in an alist of entries, one for each window in a
> buffer local way, and one global entry for the frame itself.

Here is the patch that stores any kind of information via a hook,
one for each window in a buffer local way.

Currently only dired positions for the revert case are supported.
But also I tried to save context of any buffer in a bookmark way,
and this works nicely, and correctly restores old positions even
from window states from the desktop:

#+begin_src emacs-lisp
;; Like ‘bookmark-make-record-default’:
(setq-default window-set-context-function
              (lambda ()
                `((front-context-string
                   . ,(buffer-substring-no-properties
                       (point) (min (+ (point) 16) (point-max))))
                  (rear-context-string
                   . ,(buffer-substring-no-properties
                       (point) (max (- (point) 16) (point-min)))))))
;; Like ‘bookmark-default-handler’:
(setq-default window-use-context-function
              (lambda (context)
                (when (search-forward (alist-get 'front-context-string context)
                                      (point-max) t)
                  (goto-char (match-beginning 0)))
                (when (search-backward (alist-get 'rear-context-string context)
                                       (point-min) t)
                  (goto-char (match-end 0)))))
#+end_src

diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index 3e1d8278b04..fc1b20287d3 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -1283,6 +1283,10 @@ frameset-filter-tabs
 
 (push '(tabs . frameset-filter-tabs) frameset-filter-alist)
 
+(defvar window-set-context-function nil)
+(defvar window-use-context-function nil)
+(add-to-list 'window-persistent-parameters '(context . writable))
+
 (defun tab-bar--tab (&optional frame)
   "Make a new tab data structure that can be added to tabs on the FRAME."
   (let* ((tab (tab-bar--current-tab-find nil frame))
@@ -1292,6 +1296,14 @@ tab-bar--tab
                                            frame 'buffer-list)))
          (bbl (seq-filter #'buffer-live-p (frame-parameter
                                            frame 'buried-buffer-list))))
+    (walk-windows
+     (lambda (w)
+       (with-selected-window w
+         (when (functionp window-set-context-function)
+           (when-let ((context (funcall window-set-context-function)))
+             (set-window-parameter w 'context (cons (buffer-name) context))))))
+     'nomini)
+
     `(tab
       (name . ,(if tab-explicit-name
                    (alist-get 'name tab)
@@ -1479,6 +1491,15 @@ tab-bar-select-tab
             (select-window (get-mru-window)))
           (window-state-put ws nil 'safe)))
 
+        (walk-windows
+         (lambda (w)
+           (with-selected-window w
+             (when-let ((context (window-parameter w 'context)))
+               (when (and (functionp window-use-context-function)
+                          (equal (buffer-name) (car context)))
+                 (funcall window-use-context-function (cdr context))))))
+         'nomini)
+
         ;; Select the minibuffer when it was active before switching tabs
         (when (and minibuffer-was-active (active-minibuffer-window))
           (select-window (active-minibuffer-window)))
diff --git a/lisp/dired.el b/lisp/dired.el
index 9e3b888df14..df50f37df7b 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -2743,6 +2745,12 @@ 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 ()
+                `((dired-filename . ,(dired-get-filename nil t)))))
+  (setq-local window-use-context-function
+              (lambda (context)
+                (dired-goto-file (alist-get 'dired-filename context))))
   (setq dired-switches-alist nil)
   (hack-dir-local-variables-non-file-buffer) ; before sorting
   (dired-sort-other dired-actual-switches t)