Subject: bug#64373: 29.0.90; C-x t o while in minibuffer copies the current tab to the next tab

From: Juri Linkov <[email-masked]>
Date: Tue, 04 Jul 2023 21:28:18 +0300

> We should also restore the current tab, then.  Because right now we're
> restoring the window configuration, but not the current tab.
>
> If we did that, then this would behave as expected: We'd restore the
> current tab, then restore the window configuration in that tab.

I was unaware about this problem because I have customized
read-minibuffer-restore-windows to nil.  Now that you found the problem,
I looked at it from the point of view of users who don't mind the
default value t of read-minibuffer-restore-windows, and agree now
that for read-minibuffer-restore-windows to continue doing
what it's intended to do, the only way is to switch to the
original tab back.  Here is a preliminary patch that might
need more testing:

diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index 87ca80ce00a..f0668374773 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -1253,6 +1260,14 @@ tab-bar--tabs-recent
                              tabs))))
 
 
+(defvar tab-bar-minibuffer-restore-tab nil)
+
+(defun tab-bar-minibuffer-restore-windows ()
+  (when (and read-minibuffer-restore-windows
+             tab-bar-minibuffer-restore-tab)
+    (tab-bar-select-tab tab-bar-minibuffer-restore-tab)
+    (setq tab-bar-minibuffer-restore-tab nil)))
+
 (defun tab-bar-select-tab (&optional tab-number)
   "Switch to the tab by its absolute position TAB-NUMBER in the tab bar.
 When this command is bound to a numeric key (with a key prefix or modifier key
@@ -1278,6 +1293,11 @@ tab-bar-select-tab
          (to-index (1- (max 1 (min to-number (length tabs)))))
          (minibuffer-was-active (minibuffer-window-active-p (selected-window))))
 
+    (when (and read-minibuffer-restore-windows minibuffer-was-active
+               (not tab-bar-minibuffer-restore-tab))
+      (setq tab-bar-minibuffer-restore-tab (1+ from-index))
+      (add-hook 'minibuffer-exit-hook 'tab-bar-minibuffer-restore-windows))
+
     (unless (eq from-index to-index)
       (let* ((from-tab (tab-bar--tab))
              (to-tab (nth to-index tabs))