help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: First split horizontally, and then vertically


From: Alexander Shukaev
Subject: Re: First split horizontally, and then vertically
Date: Wed, 12 Aug 2015 21:43:18 +0200

> You should look at the code of split-window-sensibly and make your own
> function, one that first checks split-width-threshold. Then you should set
> split-window-preferred-function to that function. This is not tested but
> could work:
>
> #+BEGIN_SRC emacs-lisp
> (defun split-window-more-sensibly (&optional window)
>  (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))))))))
>
> (setq split-window-preferred-function      'split-window-more-sensibly)
> #+END_SRC

Thanks for pointing this out Jorge.  Indeed, I've tailored a
comprehensive solution:

(with-eval-after-load "window"
  (defcustom split-window-below nil
    "If non-nil, vertical splits produce new windows below."
    :group 'windows
    :type 'boolean)

  (defcustom split-window-right nil
    "If non-nil, horizontal splits produce new windows to the right."
    :group 'windows
    :type 'boolean)

  (fmakunbound #'split-window-sensibly)

  (defun split-window-sensibly
      (&optional window)
    "Split WINDOW in a way suitable for `display-buffer'.
WINDOW defaults to the currently selected window.  If
`split-width-threshold' specifies an integer, WINDOW is at least
`split-width-threshold' columns wide and can be split horizontally,
split WINDOW into two windows side by side and return either the right
window if `split-window-right' is non-nil or the left window if
`split-window-right' is nil.  Otherwise, if `split-height-threshold'
specifies an integer, WINDOW is at least `split-height-threshold' lines
tall and can be split vertically, split WINDOW into two windows one
above the other and return either the lower window if
`split-window-below' is non-nil or the upper window if
`split-window-below' is nil.  If this can't be done either and WINDOW
is the only window on its frame, try to split WINDOW horizontally
disregarding any value specified by `split-width-threshold'.  If that
succeeds, return either the right window if `split-window-right' is
non-nil or the left window if `split-window-right' is nil.  Return nil
otherwise.

By default `display-buffer' routines call this function to split the
largest or least recently used window.  To change the default customize
the option `split-window-preferred-function'.

You can enforce this function to not split WINDOW horizontally, by
setting (or binding) the variable `split-width-threshold' to nil.  If,
in addition, you set `split-height-threshold' to zero, chances increase
that this function does split WINDOW vertically.

In order to not split WINDOW vertically, set (or bind) the variable
`split-height-threshold' to nil.  Additionally, you can set
`split-width-threshold' to zero to make a horizontal split more likely
to occur.

Have a look at the function `window-splittable-p' if you want to know
how `split-window-sensibly' determines whether WINDOW can be split."
    (setq window (or window (selected-window)))
    (or (and (window-splittable-p window t)
             ;; Split window horizontally.
             (split-window window nil (if split-window-right 'left  'right)))
        (and (window-splittable-p window)
             ;; Split window vertically.
             (split-window window nil (if split-window-below 'above '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 horizontally disregarding the
             ;; value of `split-width-threshold'.
             (let ((split-width-threshold 0))
               (when (window-splittable-p window t)
                 (split-window window nil (if split-window-right
                                              'left
                                            'right))))))))

It does exactly what I wanted: first horizontal splits (until the
reasonable limit is reached), and then only vertical splits; as long
as

(setq-default
  split-height-threshold  0
  split-width-threshold   160) ; the reasonable limit for horizontal splits

What I wanted to point out also, are the two new `defcustom'
variables, `split-window-below' and `split-window-right'.  I believe
they could extend configurability of the standard
`split-window-sensibly' quite well.  I would like to propose
developers to add these to Emacs's 'window.el'.

Kind regards,
Alexander



reply via email to

[Prev in Thread] Current Thread [Next in Thread]