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

From: Juri Linkov <[email-masked]>
Date: Sun, 18 Feb 2024 09:39:31 +0200

> +       (with-selected-window w
> +         (when (functionp window-set-context-function)
>
> and
>
> +           (with-selected-window w
> +             (when-let ((context (window-parameter w 'context)))
>
> would set up a precedence case for the future use of
> 'window-set-context-function'.  Are you sure that you always want to
> select the window in question here or would it suffice to pass the
> window as argument to the context function?

Ok, let's add the WINDOW argument.  This means that most hooks
will need to call 'with-selected-window' explicitly.  But maybe
there will be hooks that don't need to select a window.

So here how a bookmark-like default context could look.
I'm still not sure whether it's a good idea to enable this
by default.

#+begin_src emacs-lisp
;; Like ‘bookmark-make-record-default’
(setq-default window-set-context-function
              (lambda (w)
                (with-selected-window w
                  `((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 (w context)
                (with-selected-window w
                  (when-let ((f (alist-get 'front-context-string context)))
                    (search-forward f (point-max) t)
                    (goto-char (match-beginning 0)))
                  (when-let ((r (alist-get 'rear-context-string context)))
                    (search-backward r (point-min) t)
                    (goto-char (match-end 0))))))
#+end_src

And here is the patch that at least handles the dired revert.

diff --git a/lisp/dired.el b/lisp/dired.el
index 9e3b888df14..dd397eb5c46 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -2743,6 +2745,19 @@ 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)
+                (with-selected-window w
+                  (if-let ((f (dired-get-filename nil t)))
+                      `((dired-filename . ,f))
+                    `((position . ,(point)))))))
+  (setq-local window-use-context-function
+              (lambda (w context)
+                (with-selected-window w
+                  (if-let ((f (alist-get 'dired-filename context)))
+                      (dired-goto-file f)
+                    (when-let ((p (alist-get 'position context)))
+                      (goto-char p))))))
   (setq dired-switches-alist nil)
   (hack-dir-local-variables-non-file-buffer) ; before sorting
   (dired-sort-other dired-actual-switches t)
diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index 3e1d8278b04..7718262acbb 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)
+       (when-let (((functionp window-set-context-function))
+                  (context (funcall window-set-context-function w)))
+         (set-window-parameter
+          w 'context (cons (buffer-name (window-buffer w)) context))))
+     'nomini)
+
     `(tab
       (name . ,(if tab-explicit-name
                    (alist-get 'name tab)
@@ -1479,6 +1491,14 @@ tab-bar-select-tab
             (select-window (get-mru-window)))
           (window-state-put ws nil 'safe)))
 
+        (walk-windows
+         (lambda (w)
+           (when-let (((functionp window-use-context-function))
+                      (context (window-parameter w 'context))
+                      ((equal (buffer-name (window-buffer w)) (car context))))
+             (funcall window-use-context-function w (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)))