Subject: Re: bug#62592: Not creating new Tabs with small window

From: Juri Linkov <[email-masked]>
Date: Thu, 18 May 2023 18:50:29 +0300

>> Thanks for the suggestion.  Empirically I found that
>> '(split-window-horizontally 1)' always fails.
>> So the next patch uses '(split-window-horizontally 2)'.
>
> 1 is less than 'window-safe-min-width' (the doc-string of 'split-window'
> says in this regard that "this function can make a new window as small
> as one line or two columns").  And are you sure you want to copy the
> 'quit-restore' parameter?  As a rule, in Lisp code never use the
> -vertically/-horizontally postfixed forms of 'split-window' unless you
> want to mock their interactive behavior.

Then the following patch seems the right way to implement this.

>>> For Emacs 30 I'd recommend a new routine say 'clean-window' to make its
>>> argument window pristine.  The way you do it now loses all overlays with
>>> a window property in the window's buffer.
>>
>> The purpose of this trick is to change window identity.  So maybe
>> a better name would be 'clone-window'?
>
> Funny - here I use that for a window that can shadow an existing window
> for redisplay purposes.  If you want the selected window to change
> identity, you should say that in a comment.

Actually, there are 2 cases:

1. when 'tab-bar-new-tab-choice' is not equal to 'window'
   then need to remove all old window parameters and
   create a new window, this is what split-window/delete-window do;

2. when 'tab-bar-new-tab-choice' is 'window', then need to create
   a new identity that keeps all window parameters, this is done by:
   (window-state-put (window-state-get))
   And comments are trying to explain this.

diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index 9d703b5d048..aae80b28047 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -1585,7 +1585,8 @@ tab-bar-new-tab-to
               (window-state-put (window-state-get))
             ;; Create a new window to get rid of old window parameters
             ;; (e.g. prev/next buffers) of old window.
-            (split-window) (delete-window))))
+            (split-window nil window-safe-min-width t)
+            (delete-window))))
 
       (let ((buffer
              (if (and (functionp tab-bar-new-tab-choice)
diff --git a/lisp/window.el b/lisp/window.el
index aa7520f30fa..ab7dd5ced12 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -6391,7 +6391,7 @@ window-state-put
                      (selected-window)))
       (delete-other-windows-internal window root)
       ;; Create a new window to replace the existing one.
-      (setq window (prog1 (split-window window)
+      (setq window (prog1 (split-window window window-safe-min-width t)
                      (delete-window window)))))
 
   (set-window-dedicated-p window nil)