Tassilo’s Blog

The personal blog of Tassilo Horn

Automatically split window horizontally if current window is wide enough

with 9 comments

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.

Written by Tassilo Horn

March 9, 2007 at 7:44 pm

Posted in Emacs

9 Responses

Subscribe to comments with RSS.

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

    Paul

    April 14, 2007 at 8:46 am

  2. Thanks for the compliments, Paul.

    Tassilo Horn

    April 14, 2007 at 9:22 am

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

    shvet

    June 8, 2007 at 9:22 pm

  4. Hi shvet,

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

    Tassilo Horn

    June 9, 2007 at 7:58 am

  5. thanks for the GREAT post! Very useful…

    Whatever-ishere

    November 21, 2007 at 5:09 pm

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

    k

    May 24, 2008 at 10:07 am

  7. Hi k,

    Yes, this feature will be in Emacs 23.

    Bye,
    Tassilo

    Tassilo Horn

    May 24, 2008 at 10:42 am

  8. Thank you! It works well. :)

    Jaemyung Kim

    July 17, 2008 at 6:50 am

  9. Hey, thanks for posting this. Works great!

    George D.

    May 5, 2009 at 1:28 pm


Leave a Reply