all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Alexander Shukaev <haroogan@gmail.com>
To: "Jorge A. Alfaro-Murillo" <jorge.alfaro-murillo@yale.edu>
Cc: help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: First split horizontally, and then vertically
Date: Wed, 12 Aug 2015 21:43:18 +0200	[thread overview]
Message-ID: <CAKu-7WwEf=0u2YV1iL4EnSdRzVgfdHCWej4R5pu-hghwB6rXjg@mail.gmail.com> (raw)
In-Reply-To: <87si7oh2vd.fsf@yale.edu>

> 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	other threads:[~2015-08-12 19:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-11 20:37 First split horizontally, and then vertically Alexander Shukaev
2015-08-12 14:45 ` Jorge A. Alfaro-Murillo
2015-08-12 16:06   ` Alexander Shukaev
2015-08-12 16:45     ` Jorge A. Alfaro-Murillo
2015-08-12 19:43       ` Alexander Shukaev [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAKu-7WwEf=0u2YV1iL4EnSdRzVgfdHCWej4R5pu-hghwB6rXjg@mail.gmail.com' \
    --to=haroogan@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=jorge.alfaro-murillo@yale.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.