Subject: Re: bug#68235: 29.1.90; Switching tabs stops following process output in selected window

From: Juri Linkov <[email-masked]>
Date: Sun, 04 Feb 2024 19:27:40 +0200

>> Do you remember the bug this was supposed to fix?
>
> The dedicated bug report is this:
>
> bug#33871: Revert Dired window saved in window configuration

I finished designing the feature that will restore Dired positions
in window-configurations and window-states used on the tab-bar.

It will be easy also to add a default more general function
that would use bookmark-like rear/front context in any buffer.

diff --git a/lisp/dired.el b/lisp/dired.el
index cef93ab757c..3a80cfffc04 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -2743,6 +2745,8 @@ 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-get-filename nil t)))
+  (setq-local window-use-context-function (lambda (context) (dired-goto-file context)))
   (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..6c6f3ece9c2 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)))