Automatically split window horizontally if current window is wide enough

Posted: March 9, 2007 in Emacs

I normally use Emacs in fullscreen mode. When I split a window horizontally I get 2 side-by-side windows and each is about 95 columns wide. But by default all pop-up-windows like completion or compilation buffers split the window vertically. mulligan pointed me to the variable display-buffer-function on the #emacs IRC channel. It should be a function symbol which will be called instead of display-buffer. In that function the user can manage the splitting and assigning of buffers to windows on his own.

I came up with this function, which makes a fullscreen emacs much more convenient and behaves like the default if emacs is not wide enough for 2 side-by-side windows that are at least 80 columns wide each.

(defun th-display-buffer (buffer force-other-window)
  "If BUFFER is visible, select it.

If it's not visible and there's only one window, split the
current window and select BUFFER in the new window. If the
current window (before the split) is more than 165 columns wide,
split horizontally, else split vertically.

If the current buffer contains more than one window, select
BUFFER in the least recently used window.

This function returns the window which holds BUFFER.

FORCE-OTHER-WINDOW is ignored."
  (or (get-buffer-window buffer)
      (if (one-window-p)
          (let ((new-win (if (> (window-width) 165)
                             (split-window-horizontally)
                           (split-window-vertically))))
            (set-window-buffer new-win buffer)
            new-win)
        (let ((new-win (get-lru-window)))
          (set-window-buffer new-win buffer)
          new-win))))

(setq display-buffer-function 'th-display-buffer)

For the functions exact behavior see its docstring.

UPDATE: Fredrik Axelsson created a patch which adds a new variable prefer-window-split-horizontally. See his message on gmane.emacs.pretest.bugs. The patch is delayed at least till the release of Emacs 22.

About these ads
Comments
  1. Paul says:

    Thanks for this – I’ve been after such functionality for a while but I haven’t the lisp-foo to tackle it. Great stuff.

  2. Tassilo Horn says:

    Thanks for the compliments, Paul.

  3. shvet says:

    Somehow the function didnt behave for me the same way. Now when emacs has to split the window to display for eg. auto-completion it takes away my current window instead. Ill try to tweak it. But is it that what was intended.

  4. Tassilo Horn says:

    Hi shvet,

    That shouldn’t happen. What emacs version do you use? Maybe you have some variable set that influence display-buffer?

  5. thanks for the GREAT post! Very useful…

  6. k says:

    Ah, thanks, I’ve been bugged by the lack of this feature for a while.

    I’m guessing it might be included in Emacs 23?

  7. Tassilo Horn says:

    Hi k,

    Yes, this feature will be in Emacs 23.

    Bye,
    Tassilo

  8. Jaemyung Kim says:

    Thank you! It works well. :)

  9. George D. says:

    Hey, thanks for posting this. Works great!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s