On mac, open an Emacs frame in a specific monitor?
Asked Answered
I

2

9

Background: I use a mac with dual monitor. When I start Emacs, it always opens in the “main” monitor (the one on my macbook pro), which is not the one I want to look at. Is there any elisp function to open a new frame in a specific monitor, so that I can have frames in both monitors after emacs starts?

Or, is there any way to let Emacs starts in the external monitor (if it exists)?

BTW, my Emacs is 24.1.1 from emacsforosx.com.

Iny answered 12/7, 2012 at 17:28 Comment(0)
M
7

The visible area across your displays has a contiguous coordinate space. The top left of the main display (the one with the menu bar on it) is (0, 0). So, opening a frame on a specific display is just a special case of opening a frame in a particular location.

First move the frame where you want it, then use (frame-parameters) to get the top/left values. Normally the result of (frame-parameters) is an alist (list of pairs), something like this:

(... (top . 54) (left . 107) ...)

But if you have displays above or to the left of the main display, the top/left values for windows on that display will be negative. Emacs's GUI support, coming originally from X11, didn't have any concept of negative coordinates. If you try to use negative values, Emacs interprets them as relative to the bottom/right of the main display instead — probably not what you want.

There's a hack in the frame parameter alist to handle this situation: when storing negative top/left values, the corresponding list items are a list of three items instead. For example:

(... (top + -51) (left + -1674) ...)

Normally you can provide these items directly to make-frame, but it doesn't understand this hack. Instead, you can make the frame and immediately move it by modifying its parameters. For example, I've got a 1680x1050 display to the left and slightly above my main display, so this works to display a new frame on that display:

(modify-frame-parameters (make-frame) '((top + -51) (left + -1674)))

Personally I just use Moom, since it's easier to use and more flexible. I create a new frame with C-x 5 2 in Emacs then use Moom keyboard shortcuts to move it to another display, or wherever else I want it.

Medora answered 12/7, 2012 at 20:51 Comment(2)
Is there any way to tell if there is a second monitor? I don't want to have a frame outside my screen if there is only one monitor XDIny
You can use (x-display-pixel-height) and (x-display-pixel-width) to get the width and height of the display coordinate space.Medora
J
5

When I tried the (frame-parameters) approach, it created a strange thing: one window on the primary display and one on the secondary display. However, they were the "same" window, since when I C-x C-c'd one, the other one would get killed as well.

Here is what works for me. I put in my .emacs file. Not sure if you have to set the width and height, but it certainly is a convenient way to do that as well:

(setq default-frame-alist
  '((top + -550) (left + -1800) (width . 108) (height . 60)))
Jemie answered 13/11, 2013 at 1:33 Comment(1)
Are you sure you were running an Emacs server the first time? If there's no server, C-x C-c does destroy all the frames.Roxi

© 2022 - 2024 — McMap. All rights reserved.