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

From: Tassilo Horn <[email-masked]>
Date: Tue, 24 Mar 2015 15:59:08 +0100

martin rudalics <[email-masked]> writes:

>>>> With that and a maximized frame of size 269x82, I'd eventually end
>>>> up with three side-by-side windows each of size 86x82.
>>>
>>> Interesting.  What is your value of `window-combination-resize'?
>>
>> I didn't know that, so it has the default value nil.
>
> Then how comes you "eventually end up with three side-by-side windows
> each of size 86x82"?

No, I don't end up with that.  If I had the split function I imagined,
then I would end up with that configuration.  So that's the goal I want
to achieve but for which I have no solution.

> By default you should get one window with 135 columns and two windows
> with 67 columns, approximately.

Indeed, that's what I get with C-x 3 C-x 3 starting from a single window
of size 269x82 and the default value of `window-combination-resize' and
`window-combination-limit'.

With

  (setq window-combination-resize t
        window-combination-limit nil)

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

>> Reading its docs I get the impression that I want that to be t
>> instead, and to have it effective, I'd also need to set
>> `window-combination-limit' to nil.
>>
>> But with
>>
>>    (setq window-combination-resize t
>>          window-combination-limit nil) ;; was window-size before
>>
>> and a single 269x82 window, I still get a horizontal split first
>> (forming a horizontal combination IIUC) and then a vertical split.
>
> Yes - these are unrelated.  `split-window-sensibly' controls the
> "split-direction".
>
> I share most of your concerns.  But I'm not sure though where to put
> a/the solution.

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.

--8<---------------cut here---------------start------------->8---
(defun th/split-window-sensibly-prefer-horizontal-split (&optional window)
  "Like `split-window-sensibly' but prefer horizontal splits."
  (let ((window (or window (selected-window))))
    (or (and (window-splittable-p window t)
             ;; Split window horizontally.
             (with-selected-window window
               (split-window-right)))
        (and (window-splittable-p window)
             ;; Split window vertically.
             (with-selected-window window
               (split-window-below)))
        (and (eq window (frame-root-window (window-frame window)))
             (not (window-minibuffer-p window))
             ;; If WINDOW is the only window on its frame and is not the
             ;; minibuffer window, try to split it vertically disregarding
             ;; the value of `split-height-threshold'.
             (let ((split-height-threshold 0))
               (when (window-splittable-p window)
                 (with-selected-window window
                   (split-window-below))))))))

(defun th/split-window-sensibly (&optional window)
  (interactive)
  (let* ((root-tree (car (window-tree)))
         (new-window (when (listp root-tree)
                       (let ((dir (car root-tree))
                             (windows (cddr root-tree)))
                         (cond
                          ;; Split horizontally if we're horizontally split
                          ;; already and have enough space for another
                          ;; horizontal window after balancing.
                          ((and (null dir)
                                (cl-every #'windowp windows)
                                (> (apply #'+ (mapcar (lambda (w)
                                                        (- (window-width w)
                                                           (/ split-width-threshold 2)))
                                                      windows))
                                   (/ split-width-threshold 2)))
                           (split-window-right))
                          ;; Split vertically if we're vertically split already
                          ;; and have enough space for another vertical window.
                          ((and dir
                                (cl-every #'windowp windows)
                                (> (apply #'+ (mapcar (lambda (w)
                                                        (- (window-height w)
                                                           (/ split-height-threshold 2)))
                                                      windows))
                                   (/ split-height-threshold 2)))
                           (split-window-below))
                          ;; If we're already split either only horizontally or
                          ;; vertically, reuse the right-most/bottom window
                          ;; unless that's the one which should be split (which
                          ;; is usually the selected one).  In that case, reuse
                          ;; the LRU window.
                          ((cl-every #'windowp windows)
                           (if (eq window (car (last windows)))
                               (get-lru-window)
                             (car (last windows)))))))))
    (unless new-window
      ;; Ok, if we arrive here this means we've already destroyed our preferred
      ;; either-only-horizontal-or-only-vertical splits layout, so just let my
      ;; horizontal splitting favoring sensible split function do its job.  We
      ;; have to arrange windows manually anyhow to come back to
      (setq new-window (th/split-window-sensibly-prefer-horizontal-split window)))
    (balance-windows (window-parent new-window))
    new-window))

(setq split-window-preferred-function #'th/split-window-sensibly)
--8<---------------cut here---------------end--------------->8---

Bye,
Tassilo