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

From: Tassilo Horn <[email-masked]>
Date: Thu, 26 Mar 2015 12:01:05 +0100

Tassilo Horn <[email-masked]> writes:

>> 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.

With your suggestions, I finally came up with this which seems to work
exactly as I like it:

--8<---------------cut here---------------start------------->8---
(setq window-min-height 30
      window-min-width 80
      window-combination-resize t
      window-combination-limit nil)

(defun th/split-window-sensibly (window)
  (let ((root (frame-root-window)))
    (cond
     ((and (< (window-combinations root) 2)
           (>= (/ (window-total-width root)
                  (1+ (window-combinations root t)))
               window-min-width))
      (split-window (window-last-child root) nil 'right))
     ((and (< (window-combinations root t) 2)
           (>= (/ (window-total-height root)
                  (1+ (window-combinations root)))
               window-min-height))
      (split-window (window-last-child root) nil 'below))
     (t
      ;; Reuse the LRU window
      (get-lru-window)))))
--8<---------------cut here---------------end--------------->8---

Bye,
Tassilo