6

I'm trying to write a simple function to divide the Emacs frame into four quadrants. Once I get the splitting to work, each window will have Dired for a different directory.

The splitting isn't giving the results I want, because I can't move point to the window I want to split.

Here's what I have for the window splitting:

(defun split-into-quadrants () "Manipulate windows" (interactive)
       (delete-other-windows)
       (split-window-vertically)
       (split-window-horizontally)
       (next-window)
       (next-window)
       (split-window-horizontally)
       (next-window)
       (next-window))

The splits work fine. next-window has no effect.

If I issue these commands interactively, Emacs does exactly what I want. But when I execute the function, point remains in the first window and I end up with three windows in the top half of the frame and one in the bottom half. What am I missing?

itsjeyd
  • 14,586
  • 3
  • 58
  • 87
BT56
  • 163
  • 2
  • Most of the answers I generally see use some of the built in functions, however, you might be interested in creating your own display something left, display something right, display something below, display something above, and select whatever window you want. Here is a display left and right example function, which includes a select window: http://stackoverflow.com/a/21544307/2112489. Here is a below example, without the select window -- but you can add that if you want: http://stackoverflow.com/a/21591259/2112489 The split window is creating during the display functions. – lawlist Dec 01 '14 at 04:45
  • 1
    BT56: `next-window` doesn't select a window; it just returns the window object for the next window. You're not doing anything with the return values. – phils Dec 01 '14 at 07:44
  • Slightly OT, but still important: You seem to be using the terms "function" and "command" interchangeably. Please note that [they are not the same](http://emacs.stackexchange.com/a/3556/504). – itsjeyd Dec 01 '14 at 09:29

2 Answers2

5

The functions split-window-horizontally and split-window-vertically return the new window created. You can use use the function select-window to select the newly created window, for example

(select-window (split-window-horizontally))

Here is one way (not necessarily the best way) to achieve what you want

(defun split-into-quadrants ()
  "Manipulate windows"
  (interactive)
  (delete-other-windows)
  (let ((current-window (selected-window))
        (new-window (split-window-vertically)))
    (split-window-horizontally)
    (select-window new-window)
    (split-window-horizontally)
    (select-window current-window)))
Iqbal Ansari
  • 7,468
  • 1
  • 28
  • 31
3

You are on the right track, but you should use other-window instead of next-window to navigate to a specific window. Conveniently, other-window allows you to specify how many windows to skip, which means that you don't need to call it multiple times when navigating to a window that is multiple hops away:

(defun split-into-quadrants () "Manipulate windows" (interactive)
       (delete-other-windows)
       (split-window-vertically)
       (split-window-horizontally)
       (other-window 2)
       (split-window-horizontally)
       (other-window 2))
itsjeyd
  • 14,586
  • 3
  • 58
  • 87