2

Q: how do I detect/specify the monitor a frame is on?

I'm working off of a laptop that has an external monitor connected to it. I therefore have two monitors (call them INTERNAL and EXTERNAL).

On occasion, I want to do the following:

  1. Adjust an existing frame's parameters based on the monitor it's on
  2. Make a new frame on the other monitor

I think I can tell which monitor I'm currently on with frame-monitor-attributes.

However, I don't know how to address point 2: given that I'm on monitor EXTERNAL, how do I make a new frame on monitor INTERNAL (and vice versa)?

I suspect I could rig something up by accessing the x location, but that seems hacky to me.

Notes:

3
  • I am assuming that the displays are not mirrors, and that one is an extension of the other. Do you know the top and left coordinates for each monitor? If so, then that is one way to accomplish the goal since make-frame accepts those arguments/parameters (among others) when creating a frame. [Note that both Emacs 25 and the master branch have some important bug fixes regarding placement of new frames, which are not available in earlier versions.]
    – lawlist
    Commented Jul 8, 2016 at 5:12
  • @lawlist: thank you, I've been able to extract the monitor coordinates from the function Lindydancer cited.
    – Dan
    Commented Jul 8, 2016 at 14:23
  • 1
    In case you are interested in exact pixel sizing of frames concurrently with frame creation, that feature was implemented with bug #21415, along with a bunch of other frame-related fixes at that time. I don't know if it is documented anywhere (other than in the bug thread), but the parameters for pixels sizing on frame creation look like this: '(width . (text-pixels . 1900)) and '(height . (text-pixels . 1054)). Those would be in lieu of the standard width and height parameters that we are all accustomed to seeing.
    – lawlist
    Commented Jul 8, 2016 at 14:48

2 Answers 2

1

You can use display-monitor-attributes-list. It contains a list of displays and, for each display a list of properties. Properties of interest to you would be frames which is a list of frames on that display and geometry which specified the location and the size of the monitor.

Also, when you want to position a frame on a display, you will have to compensate for things like the borders, menus, window titles. etc. You can use frame-geometry to determine them. (I think this is new as of Emacs 25.)

3
  • You can do a lot with these functions. For example, when I do presentations I use a (yet unpublished) function that makes a frame move to the next monitor and adapts itself to the size of that monitor. Commented Jul 8, 2016 at 10:01
  • Thanks, I hadn't looked closely enough at this function. I wrote up a quick one that works for what I want, but if you want to post a version of yours, I'll gladly accept it.
    – Dan
    Commented Jul 8, 2016 at 14:22
  • It's rather complex. My plan is to make a package out of it (or include it in my github.com/Lindydancer/multicolumn package, as they are heavily connected). At earliest, I'll have time for this in the fall, so feel free to accept your own answer. Commented Jul 8, 2016 at 14:51
1

Here's a quick function that does what I want: a new frame at the top-left of the laptop's screen. (That is: I'm almost always on the external monitor, so the laptop screen is the "other" monitor.)

Everything's hard-coded here (including the monitor name, "eDP-1"), but it should be easy enough to generalize.

(defun make-frame-on-laptop-screen ()
  "Make a new frame on the top-left of the laptop screen."
  (interactive)
  (let ((dims (car (delete nil
                           (mapcar (lambda (monitor)
                                     (when (string-equal "eDP-1"
                                                         (cdr (assq 'name monitor)))
                                       (cdr (assq 'geometry monitor))))
                                   (display-monitor-attributes-list))))))
    (when dims
      (make-frame `((left . ,(nth 0 dims))
                    (top  . ,(nth 1 dims)))))))

Thanks to @Lindydancer for pointing me to display-monitor-attributes-list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.