Subject: Re: bug#20189: 25.0.50; Feature request: Alternative split-window-sensibly functions

From: Tassilo Horn <[email-masked]>
Date: Wed, 25 Mar 2015 10:41:19 +0100

martin rudalics <[email-masked]> writes:

>> two consecutive C-x 3 starting from the big single window results in
>> three side-by-side *balanced* windows, each being 86x82 large.
>> That's actually what I wanted.  However, I'd like that this splitting
>> behavior was the default for `display-buffer'.  But that fails
>> because after the first horizontal split, `window-splittable-p' says
>> that both horizontal windows aren't horizontally splittable anymore
>> because they are narrower than `split-width-threshold'.
>
> If you set `split-width-threshold' to zero and
> `split-height-threshold' to nil you should get the behavior you want.

Wouldn't that mean that I get as many horizontal windows until they
become smaller than `window-min-width'?  I want them to alway be at
least 80 columns wide.

>> I've come up with a solution which seems to fit my preferences quite
>> nicely although it's probably quite hackish and ignores possible
>> contracts that might exist for dedicated windows, fixed-size windows,
>> side windows and what else there might be.
>
> You should be able to simplify your code by using idioms from the
> below.
>
> (defun th/split-window-sensibly (_window)
>   (let ((root (frame-root-window))
> 	(window-combination-resize 'resize))
>     (cond
>      ((>= (/ (window-total-width root) (window-combinations root t)) 80)
>       (split-window (window-last-child root) nil 'right))
>      ((>= (/ (window-total-height root) (window-combinations root)) 40)
>       (split-window (window-last-child root) nil 'below))
>      (t
>       (split-window-sensibly window)))))

With that and repeated `display-buffer' calls for different buffers
starting with a single 269x82 window, I get 3 balanced side-by-side
windows first (good!), but the next d-b creates another horizontal
window.  Then I have 4 side-by-side balanced windows where each one is
less than 80 columns wide.  And yet another d-b splits the rightmost
window vertically although I don't want vertical splits at all if there
are already horizontal splits.  In that case, it should have reused some
existing window.

Here's my test case:

--8<---------------cut here---------------start------------->8---
(defun th/split-window-sensibly (_window)
  (let ((root (frame-root-window))
 	(window-combination-resize 'resize))
    (cond
     ((>= (/ (window-total-width root) (window-combinations root t)) 80)
      (split-window (window-last-child root) nil 'right))
     ((>= (/ (window-total-height root) (window-combinations root)) 40)
      (split-window (window-last-child root) nil 'below))
     (t
      (split-window-sensibly window)))))

(setq split-window-preferred-function #'th/split-window-sensibly)

(defun test-splitting ()
  (interactive)
  (display-buffer (get-buffer-create "ONE"))
  (display-buffer (get-buffer-create "TWO"))
  (display-buffer (get-buffer-create "THREE"))
  (display-buffer (get-buffer-create "FOUR")))
--8<---------------cut here---------------end--------------->8---

So now when starting with emacs -Q, maximizing, putting the above in
*scratch*, evaling it, followed by M-x test-splitting RET, I get:

 +---------------+-------------+---------------+-------------+
 |               |             |               |             |
 |               |             |               |             |
 |               |             |               |             |
 |               |             |               |             |
 |               |             |               |             |
 |               |             |               |   THREE     |
 |               |             |               +-------------+
 |               |             |               |             |
 |               |             |               |             |
 |               |             |               |             |
 |               |             |               |             |
 | *scratch*     |    ONE      |    TWO        |   FOUR      |
 +---------------+-------------+---------------+-------------+

With my function, I get instead

 +--------------------+--------------------+-----------------+
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |   (ONE)         |
 |                    |                    |   (THREE)       |
 |    *scratch*       |     TWO            |    FOUR         |
 +--------------------+--------------------+-----------------+

where the (ONE), (THREE) denotes the buffer history, e.g., initially the
leftmost window showed buffer ONE, then it was reused to show THREE, and
finally it was reused again to show buffer FOUR.

That's ok for me although I think I'd like it better if it was this way:

 +--------------------+--------------------+-----------------+
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |                 |
 |                    |                    |   (TWO)         |
 |                    |                    |   (THREE)       |
 |    *scratch*       |     ONE            |    FOUR         |
 +--------------------+--------------------+-----------------+

Bye,
Tassilo